summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--industry.h30
-rw-r--r--industry_cmd.c61
-rw-r--r--industry_gui.c4
-rw-r--r--oldloader.c3
-rw-r--r--ttd.c23
5 files changed, 80 insertions, 41 deletions
diff --git a/industry.h b/industry.h
index daca10888..7fac15f28 100644
--- a/industry.h
+++ b/industry.h
@@ -1,6 +1,8 @@
#ifndef INDUSTRY_H
#define INDUSTRY_H
+#include "pool.h"
+
struct Industry {
TileIndex xy;
byte width; /* swapped order of w/h with town */
@@ -27,22 +29,32 @@ struct Industry {
uint16 index;
};
-VARDEF int _total_industries; // For the AI: the amount of industries active
-
-VARDEF Industry _industries[250];
-VARDEF uint _industries_size;
-
-VARDEF uint16 *_industry_sort;
+extern MemoryPool _industry_pool;
+/**
+ * Get the pointer to the industry with index 'index'
+ */
static inline Industry *GetIndustry(uint index)
{
- assert(index < _industries_size);
- return &_industries[index];
+ return (Industry*)GetItemFromPool(&_industry_pool, index);
+}
+
+/**
+ * Get the current size of the IndustryPool
+ */
+static inline uint16 GetIndustryPoolSize(void)
+{
+ return _industry_pool.total_items;
}
-#define FOR_ALL_INDUSTRIES(i) for(i = _industries; i != &_industries[_industries_size]; i++)
+#define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL)
+#define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0)
+
+VARDEF int _total_industries; // For the AI: the amount of industries active
+VARDEF uint16 *_industry_sort;
VARDEF bool _industry_sort_dirty;
+
void DeleteIndustry(Industry *is);
enum {
diff --git a/industry_cmd.c b/industry_cmd.c
index 4a2705eb1..88a8d605d 100644
--- a/industry_cmd.c
+++ b/industry_cmd.c
@@ -13,6 +13,26 @@
#include "economy.h"
#include "sound.h"
+enum {
+ /* Max industries: 64000 (8 * 8000) */
+ INDUSTRY_POOL_BLOCK_SIZE_BITS = 3, /* In bits, so (1 << 3) == 8 */
+ INDUSTRY_POOL_MAX_BLOCKS = 8000,
+};
+
+/**
+ * Called if a new block is added to the industry-pool
+ */
+void IndustryPoolNewBlock(uint start_item)
+{
+ Industry *i;
+
+ FOR_ALL_INDUSTRIES_FROM(i, start_item)
+ i->index = start_item++;
+}
+
+/* Initialize the industry-pool */
+MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, 0, 0, NULL };
+
byte _industry_sound_ctr;
TileIndex _industry_sound_tile;
@@ -1406,12 +1426,22 @@ static Industry *AllocateIndustry(void)
FOR_ALL_INDUSTRIES(i) {
if (i->xy == 0) {
+ uint index = i->index;
+
if (i->index > _total_industries)
_total_industries = i->index;
+ memset(i, 0, sizeof(Industry));
+ i->index = index;
+
return i;
}
}
+
+ /* Check if we can add a block to the pool */
+ if (AddBlockToPool(&_industry_pool))
+ return AllocateIndustry();
+
return NULL;
}
@@ -1827,13 +1857,11 @@ void IndustryMonthlyLoop(void)
UpdateIndustryStatistics(i);
}
- i = GetIndustry(RandomRange(_industries_size));
-
- if (i->xy == 0) {
- uint32 r;
- if (CHANCE16I(1,9,r=Random()))
- MaybeNewIndustry(r);
+ /* 3% chance that we start a new industry */
+ if (CHANCE16(3, 100)) {
+ MaybeNewIndustry(Random());
} else if (!_patches.smooth_economy) {
+ i = GetIndustry(RandomRange(_total_industries));
MaybeCloseIndustry(i);
}
@@ -1847,14 +1875,9 @@ void IndustryMonthlyLoop(void)
void InitializeIndustries(void)
{
- Industry *i;
- int j;
- memset(_industries, 0, sizeof(_industries[0]) * _industries_size);
-
- j = 0;
- FOR_ALL_INDUSTRIES(i)
- i->index = j++;
+ CleanPool(&_industry_pool);
+ AddBlockToPool(&_industry_pool);
_total_industries = 0;
_industry_sort_dirty = true;
@@ -1924,12 +1947,20 @@ static void Save_INDY(void)
static void Load_INDY(void)
{
int index;
+
_total_industries = 0;
+
while ((index = SlIterateArray()) != -1) {
- Industry *i = GetIndustry(index);
+ Industry *i;
+ if (!AddBlockIfNeeded(&_industry_pool, index))
+ error("Industries: failed loading savegame: too many industries");
+
+ i = GetIndustry(index);
SlObject(i, _industry_desc);
- if (index > _total_industries) _total_industries = index;
+
+ if (index > _total_industries)
+ _total_industries = index;
}
}
diff --git a/industry_gui.c b/industry_gui.c
index c8774286a..11854c501 100644
--- a/industry_gui.c
+++ b/industry_gui.c
@@ -493,7 +493,7 @@ static byte _industry_sort_order;
static int CDECL GeneralIndustrySorter(const void *a, const void *b)
{
char buf1[96];
- byte val;
+ uint16 val;
Industry *i = GetIndustry(*(const uint16*)a);
Industry *j = GetIndustry(*(const uint16*)b);
int r = 0;
@@ -557,7 +557,7 @@ static void MakeSortedIndustryList(void)
int n = 0;
/* Create array for sorting */
- _industry_sort = realloc(_industry_sort, _industries_size * sizeof(_industry_sort[0]));
+ _industry_sort = realloc(_industry_sort, GetIndustryPoolSize() * sizeof(_industry_sort[0]));
if (_industry_sort == NULL)
error("Could not allocate memory for the industry-sorting-list");
diff --git a/oldloader.c b/oldloader.c
index 1b8bca9c2..6de0c483e 100644
--- a/oldloader.c
+++ b/oldloader.c
@@ -647,6 +647,9 @@ static void FixIndustry(OldIndustry *o, int num)
if (o->xy == 0)
continue;
+ if (!AddBlockIfNeeded(&_industry_pool, j))
+ error("Industries: failed loading savegame: too many industries");
+
i = GetIndustry(j);
i->xy = o->xy;
diff --git a/ttd.c b/ttd.c
index d95db53be..62509d763 100644
--- a/ttd.c
+++ b/ttd.c
@@ -495,34 +495,27 @@ static void ParseResolution(int res[2], char *s)
static void InitializeDynamicVariables(void)
{
/* Dynamic stuff needs to be initialized somewhere... */
- _stations_size = lengthof(_stations);
- _station_sort = NULL;
-
+ _stations_size = lengthof(_stations);
_roadstops_size = lengthof(_roadstops);
+ _vehicles_size = lengthof(_vehicles);
+ _sign_size = lengthof(_sign_list);
+ _orders_size = lengthof(_orders);
- _vehicles_size = lengthof(_vehicles);
- _vehicle_sort = NULL;
-
- _town_sort = NULL;
-
- _industries_size = lengthof(_industries);
+ _station_sort = NULL;
+ _vehicle_sort = NULL;
+ _town_sort = NULL;
_industry_sort = NULL;
-
- _sign_size = lengthof(_sign_list);
- _orders_size = lengthof(_orders);
}
static void UnInitializeDynamicVariables(void)
{
/* Dynamic stuff needs to be free'd somewhere... */
CleanPool(&_town_pool);
+ CleanPool(&_industry_pool);
free(_station_sort);
-
free(_vehicle_sort);
-
free(_town_sort);
-
free(_industry_sort);
}