# HG changeset patch # User truelight # Date 1156279286 0 # Node ID 10f4ce894eb1c486e200357815634f9b1efe5b14 # Parent 1523114f43f03259c734fc1dfec40735c9bf21e5 (svn r6055) -Codechange: added GetXXXArraySize, which returns HighestID + 1 (or, will do that). It isn't the best name, but we couldn't find any better. This unifies the pool-system even more. diff -r 1523114f43f0 -r 10f4ce894eb1 ai/default/default.c --- a/ai/default/default.c Tue Aug 22 20:39:18 2006 +0000 +++ b/ai/default/default.c Tue Aug 22 20:41:26 2006 +0000 @@ -441,13 +441,13 @@ static Town *AiFindRandomTown(void) { - Town *t = GetTown(RandomRange(_total_towns)); + Town *t = GetTown(RandomRange(GetTownArraySize())); return IsValidTown(t) ? t : NULL; } static Industry *AiFindRandomIndustry(void) { - Industry *i = GetIndustry(RandomRange(_total_industries)); + Industry *i = GetIndustry(RandomRange(GetIndustryArraySize())); return IsValidIndustry(i) ? i : NULL; } @@ -3566,8 +3566,8 @@ p->ai.state = AIS_1; // Get a list of all stations that are in use by a vehicle - in_use = malloc(GetStationPoolSize()); - memset(in_use, 0, GetStationPoolSize()); + in_use = malloc(GetStationArraySize()); + memset(in_use, 0, GetStationArraySize()); FOR_ALL_ORDERS(ord) { if (ord->type == OT_GOTO_STATION) in_use[ord->station] = 1; } diff -r 1523114f43f0 -r 10f4ce894eb1 ai/trolly/trolly.c --- a/ai/trolly/trolly.c Tue Aug 22 20:39:18 2006 +0000 +++ b/ai/trolly/trolly.c Tue Aug 22 20:41:26 2006 +0000 @@ -379,9 +379,9 @@ if (p->ainew.temp == -1) { // First, we pick a random spot to search from if (p->ainew.from_type == AI_CITY) { - p->ainew.temp = AI_RandomRange(_total_towns); + p->ainew.temp = AI_RandomRange(GetTownArraySize()); } else { - p->ainew.temp = AI_RandomRange(_total_industries); + p->ainew.temp = AI_RandomRange(GetIndustryArraySize()); } } @@ -391,9 +391,9 @@ // to try again p->ainew.temp++; if (p->ainew.from_type == AI_CITY) { - if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0; + if (p->ainew.temp >= GetTownArraySize()) p->ainew.temp = 0; } else { - if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; + if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0; } // Don't do an attempt if we are trying the same id as the last time... @@ -415,9 +415,9 @@ if (p->ainew.temp == -1) { // First, we pick a random spot to search to if (p->ainew.to_type == AI_CITY) { - p->ainew.temp = AI_RandomRange(_total_towns); + p->ainew.temp = AI_RandomRange(GetTownArraySize()); } else { - p->ainew.temp = AI_RandomRange(_total_industries); + p->ainew.temp = AI_RandomRange(GetIndustryArraySize()); } } @@ -531,9 +531,9 @@ // to try again p->ainew.temp++; if (p->ainew.to_type == AI_CITY) { - if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0; + if (p->ainew.temp >= GetTownArraySize()) p->ainew.temp = 0; } else { - if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; + if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0; } // Don't do an attempt if we are trying the same id as the last time... diff -r 1523114f43f0 -r 10f4ce894eb1 economy.c --- a/economy.c Tue Aug 22 20:39:18 2006 +0000 +++ b/economy.c Tue Aug 22 20:41:26 2006 +0000 @@ -882,11 +882,11 @@ fr->distance = (uint)-1; - fr->from = from = GetTown(RandomRange(_total_towns)); + fr->from = from = GetTown(RandomRange(GetTownArraySize())); if (!IsValidTown(from) || from->population < 400) return; - fr->to = to = GetTown(RandomRange(_total_towns)); + fr->to = to = GetTown(RandomRange(GetTownArraySize())); if (from == to || !IsValidTown(to) || to->population < 400 || to->pct_pass_transported > 42) return; @@ -901,7 +901,7 @@ fr->distance = (uint)-1; - fr->from = i = GetIndustry(RandomRange(_total_industries)); + fr->from = i = GetIndustry(RandomRange(GetIndustryArraySize())); if (!IsValidIndustry(i)) return; // Randomize cargo type @@ -925,7 +925,7 @@ if (cargo == CT_GOODS || cargo == CT_FOOD) { // The destination is a town - Town *t = GetTown(RandomRange(_total_towns)); + Town *t = GetTown(RandomRange(GetTownArraySize())); // Only want big towns if (!IsValidTown(t) || t->population < 900) return; @@ -934,7 +934,7 @@ fr->to = t; } else { // The destination is an industry - Industry *i2 = GetIndustry(RandomRange(_total_industries)); + Industry *i2 = GetIndustry(RandomRange(GetIndustryArraySize())); // The industry must accept the cargo if (i == i2 || !IsValidIndustry(i2) || diff -r 1523114f43f0 -r 10f4ce894eb1 graph_gui.c --- a/graph_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/graph_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -1123,7 +1123,7 @@ uint n = 0; /* Create array for sorting */ - _sign_sort = realloc(_sign_sort, GetSignPoolSize() * sizeof(_sign_sort[0])); + _sign_sort = realloc(_sign_sort, GetSignArraySize() * sizeof(_sign_sort[0])); if (_sign_sort == NULL) { error("Could not allocate memory for the sign-sorting-list"); } diff -r 1523114f43f0 -r 10f4ce894eb1 industry.h --- a/industry.h Tue Aug 22 20:39:18 2006 +0000 +++ b/industry.h Tue Aug 22 20:41:26 2006 +0000 @@ -95,11 +95,21 @@ return _industry_pool.total_items; } +VARDEF int _total_industries; + +static inline IndustryID GetIndustryArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return _total_industries + 1; +} + #define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL) if (IsValidIndustry(i)) #define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0) -VARDEF int _total_industries; // For the AI: the amount of industries active - VARDEF const Industry** _industry_sort; VARDEF bool _industry_sort_dirty; diff -r 1523114f43f0 -r 10f4ce894eb1 industry_cmd.c --- a/industry_cmd.c Tue Aug 22 20:39:18 2006 +0000 +++ b/industry_cmd.c Tue Aug 22 20:41:26 2006 +0000 @@ -1878,8 +1878,8 @@ /* 3% chance that we start a new industry */ if (CHANCE16(3, 100)) { MaybeNewIndustry(Random()); - } else if (!_patches.smooth_economy && _total_industries > 0) { - i = GetIndustry(RandomRange(_total_industries)); + } else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) { + i = GetIndustry(RandomRange(GetIndustryArraySize())); if (IsValidIndustry(i)) ChangeIndustryProduction(i); } diff -r 1523114f43f0 -r 10f4ce894eb1 industry_gui.c --- a/industry_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/industry_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -557,7 +557,7 @@ int n = 0; /* Create array for sorting */ - _industry_sort = realloc((void*)_industry_sort, GetIndustryPoolSize() * sizeof(_industry_sort[0])); + _industry_sort = realloc((void *)_industry_sort, GetIndustryArraySize() * sizeof(_industry_sort[0])); if (_industry_sort == NULL) error("Could not allocate memory for the industry-sorting-list"); diff -r 1523114f43f0 -r 10f4ce894eb1 network_gui.c --- a/network_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/network_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -1508,7 +1508,7 @@ } /* Then, try townnames */ - if (*item < (uint)MAX_CLIENT_INFO + GetTownPoolSize()) { + if (*item < (uint)MAX_CLIENT_INFO + GetTownArraySize()) { const Town *t; FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_INFO) { diff -r 1523114f43f0 -r 10f4ce894eb1 order.h --- a/order.h Tue Aug 22 20:39:18 2006 +0000 +++ b/order.h Tue Aug 22 20:41:26 2006 +0000 @@ -117,6 +117,16 @@ return _order_pool.total_items; } +static inline OrderID GetOrderArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return GetOrderPoolSize(); +} + /** * Check if a Order really exists. */ diff -r 1523114f43f0 -r 10f4ce894eb1 signs.h --- a/signs.h Tue Aug 22 20:39:18 2006 +0000 +++ b/signs.h Tue Aug 22 20:41:26 2006 +0000 @@ -34,6 +34,16 @@ return _sign_pool.total_items; } +static inline SignID GetSignArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return GetSignPoolSize(); +} + /** * Check if a Sign really exists. */ diff -r 1523114f43f0 -r 10f4ce894eb1 station.h --- a/station.h Tue Aug 22 20:39:18 2006 +0000 +++ b/station.h Tue Aug 22 20:41:26 2006 +0000 @@ -161,6 +161,16 @@ return _station_pool.total_items; } +static inline StationID GetStationArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return GetStationPoolSize(); +} + /** * Check if a station really exists. */ diff -r 1523114f43f0 -r 10f4ce894eb1 station_cmd.c --- a/station_cmd.c Tue Aug 22 20:39:18 2006 +0000 +++ b/station_cmd.c Tue Aug 22 20:41:26 2006 +0000 @@ -2571,7 +2571,7 @@ if (_game_mode == GM_EDITOR) return; i = _station_tick_ctr; - if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0; + if (++_station_tick_ctr == GetStationArraySize()) _station_tick_ctr = 0; if (IsValidStationID(i)) StationHandleBigTick(GetStation(i)); @@ -3120,7 +3120,7 @@ } /* This is to ensure all pointers are within the limits of _stations_size */ - if (_station_tick_ctr > GetStationPoolSize()) _station_tick_ctr = 0; + if (_station_tick_ctr > GetStationArraySize()) _station_tick_ctr = 0; } static void Save_ROADSTOP(void) diff -r 1523114f43f0 -r 10f4ce894eb1 station_gui.c --- a/station_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/station_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -180,7 +180,7 @@ if (!(sl->flags & SL_REBUILD)) return; /* Create array for sorting */ - station_sort = malloc(GetStationPoolSize() * sizeof(station_sort[0])); + station_sort = malloc(GetStationArraySize() * sizeof(station_sort[0])); if (station_sort == NULL) error("Could not allocate memory for the station-sorting-list"); diff -r 1523114f43f0 -r 10f4ce894eb1 town.h --- a/town.h Tue Aug 22 20:39:18 2006 +0000 +++ b/town.h Tue Aug 22 20:41:26 2006 +0000 @@ -179,6 +179,18 @@ return _town_pool.total_items; } +VARDEF uint _total_towns; + +static inline TownID GetTownArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return _total_towns + 1; +} + static inline bool IsValidTownID(uint index) { return index < GetTownPoolSize() && IsValidTown(GetTown(index)); @@ -187,8 +199,6 @@ #define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) if (IsValidTown(t)) #define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0) -VARDEF uint _total_towns; // For the AI: the amount of towns active - VARDEF bool _town_sort_dirty; VARDEF byte _town_sort_order; diff -r 1523114f43f0 -r 10f4ce894eb1 town_cmd.c --- a/town_cmd.c Tue Aug 22 20:39:18 2006 +0000 +++ b/town_cmd.c Tue Aug 22 20:41:26 2006 +0000 @@ -405,12 +405,12 @@ /* Make sure each town's tickhandler invocation frequency is about the * same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */ - for (_cur_town_iter += GetTownPoolSize(); + for (_cur_town_iter += GetTownArraySize(); _cur_town_iter >= TOWN_GROWTH_FREQUENCY; _cur_town_iter -= TOWN_GROWTH_FREQUENCY) { uint32 i = _cur_town_ctr; - if (++_cur_town_ctr >= GetTownPoolSize()) + if (++_cur_town_ctr >= GetTownArraySize()) _cur_town_ctr = 0; if (IsValidTownID(i)) TownTickHandler(GetTown(i)); @@ -1964,7 +1964,7 @@ /* This is to ensure all pointers are within the limits of * the size of the TownPool */ - if (_cur_town_ctr >= GetTownPoolSize()) + if (_cur_town_ctr >= GetTownArraySize()) _cur_town_ctr = 0; } diff -r 1523114f43f0 -r 10f4ce894eb1 town_gui.c --- a/town_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/town_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -410,7 +410,7 @@ uint n = 0; /* Create array for sorting */ - _town_sort = realloc((void*)_town_sort, GetTownPoolSize() * sizeof(_town_sort[0])); + _town_sort = realloc((void*)_town_sort, GetTownArraySize() * sizeof(_town_sort[0])); if (_town_sort == NULL) error("Could not allocate memory for the town-sorting-list"); diff -r 1523114f43f0 -r 10f4ce894eb1 vehicle.h --- a/vehicle.h Tue Aug 22 20:39:18 2006 +0000 +++ b/vehicle.h Tue Aug 22 20:41:26 2006 +0000 @@ -359,6 +359,16 @@ return _vehicle_pool.total_items; } +static inline VehicleID GetVehicleArraySize(void) +{ + /* TODO - This isn't the real content of the function, but + * with the new pool-system this will be replaced with one that + * _really_ returns the highest index + 1. Now it just returns + * the next safe value we are sure about everything is below. + */ + return GetVehiclePoolSize(); +} + /** * Check if a Vehicle really exists. */ diff -r 1523114f43f0 -r 10f4ce894eb1 vehicle_gui.c --- a/vehicle_gui.c Tue Aug 22 20:39:18 2006 +0000 +++ b/vehicle_gui.c Tue Aug 22 20:41:26 2006 +0000 @@ -124,7 +124,7 @@ if (!(vl->flags & VL_REBUILD)) return; - sort_list = malloc(GetVehiclePoolSize() * sizeof(sort_list[0])); + sort_list = malloc(GetVehicleArraySize() * sizeof(sort_list[0])); if (sort_list == NULL) { error("Could not allocate memory for the vehicle-sorting-list"); }