From 5fd9aeb12b75a3971e86e5b7d1701115f57fbc12 Mon Sep 17 00:00:00 2001 From: truelight Date: Tue, 22 Aug 2006 20:41:26 +0000 Subject: [PATCH] (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. --- ai/default/default.c | 8 ++++---- ai/trolly/trolly.c | 16 ++++++++-------- economy.c | 10 +++++----- graph_gui.c | 2 +- industry.h | 14 ++++++++++++-- industry_cmd.c | 4 ++-- industry_gui.c | 2 +- network_gui.c | 2 +- order.h | 10 ++++++++++ signs.h | 10 ++++++++++ station.h | 10 ++++++++++ station_cmd.c | 4 ++-- station_gui.c | 2 +- town.h | 14 ++++++++++++-- town_cmd.c | 6 +++--- town_gui.c | 2 +- vehicle.h | 10 ++++++++++ vehicle_gui.c | 2 +- 18 files changed, 94 insertions(+), 34 deletions(-) diff --git a/ai/default/default.c b/ai/default/default.c index c57ba0c51b..3de583bf48 100644 --- a/ai/default/default.c +++ b/ai/default/default.c @@ -441,13 +441,13 @@ typedef struct FoundRoute { 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 @@ static void AiStateRemoveStation(Player *p) 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 --git a/ai/trolly/trolly.c b/ai/trolly/trolly.c index ea41cdac36..7d3398cec0 100644 --- a/ai/trolly/trolly.c +++ b/ai/trolly/trolly.c @@ -379,9 +379,9 @@ static void AiNew_State_LocateRoute(Player *p) 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 @@ static void AiNew_State_LocateRoute(Player *p) // 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 @@ static void AiNew_State_LocateRoute(Player *p) 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 @@ static void AiNew_State_LocateRoute(Player *p) // 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 --git a/economy.c b/economy.c index b2a85bdc47..19fa8952ca 100644 --- a/economy.c +++ b/economy.c @@ -882,11 +882,11 @@ static void FindSubsidyPassengerRoute(FoundRoute *fr) 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 @@ static void FindSubsidyCargoRoute(FoundRoute *fr) 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 @@ static void FindSubsidyCargoRoute(FoundRoute *fr) 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 @@ static void FindSubsidyCargoRoute(FoundRoute *fr) 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 --git a/graph_gui.c b/graph_gui.c index 677952e0fc..e41356aea6 100644 --- a/graph_gui.c +++ b/graph_gui.c @@ -1123,7 +1123,7 @@ static void GlobalSortSignList(void) 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 --git a/industry.h b/industry.h index ee92c6969c..dcd489b958 100644 --- a/industry.h +++ b/industry.h @@ -95,11 +95,21 @@ static inline uint16 GetIndustryPoolSize(void) 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 --git a/industry_cmd.c b/industry_cmd.c index 454c2b9b10..b36f01ff5e 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -1878,8 +1878,8 @@ void IndustryMonthlyLoop(void) /* 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 --git a/industry_gui.c b/industry_gui.c index 0b6c3c63c6..4b3a1a2c0f 100644 --- a/industry_gui.c +++ b/industry_gui.c @@ -557,7 +557,7 @@ static void MakeSortedIndustryList(void) 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 --git a/network_gui.c b/network_gui.c index 44e109479f..044e4d6661 100644 --- a/network_gui.c +++ b/network_gui.c @@ -1508,7 +1508,7 @@ static const char *ChatTabCompletionNextItem(uint *item) } /* 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 --git a/order.h b/order.h index ea210063c1..bf96e92eef 100644 --- a/order.h +++ b/order.h @@ -117,6 +117,16 @@ static inline uint16 GetOrderPoolSize(void) 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 --git a/signs.h b/signs.h index e6689d0b48..f61db4511c 100644 --- a/signs.h +++ b/signs.h @@ -34,6 +34,16 @@ static inline uint16 GetSignPoolSize(void) 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 --git a/station.h b/station.h index 1a343410d3..6d753183dc 100644 --- a/station.h +++ b/station.h @@ -161,6 +161,16 @@ static inline uint16 GetStationPoolSize(void) 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 --git a/station_cmd.c b/station_cmd.c index e5124c9272..49fd410173 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -2571,7 +2571,7 @@ void OnTick_Station(void) 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 @@ static void Load_STNS(void) } /* 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 --git a/station_gui.c b/station_gui.c index bd4b89fc74..28d12983bb 100644 --- a/station_gui.c +++ b/station_gui.c @@ -180,7 +180,7 @@ static void BuildStationsList(plstations_d* sl, PlayerID owner, byte facilities, 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 --git a/town.h b/town.h index 4bccadb1be..122a373dc8 100644 --- a/town.h +++ b/town.h @@ -179,6 +179,18 @@ static inline uint16 GetTownPoolSize(void) 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 @@ static inline bool IsValidTownID(uint index) #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 --git a/town_cmd.c b/town_cmd.c index 46837cbde5..59a9945b28 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -405,12 +405,12 @@ void OnTick_Town(void) /* 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 @@ static void Load_TOWN(void) /* 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 --git a/town_gui.c b/town_gui.c index 29f3f38650..cc0b0d2402 100644 --- a/town_gui.c +++ b/town_gui.c @@ -410,7 +410,7 @@ static void MakeSortedTownList(void) 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 --git a/vehicle.h b/vehicle.h index 6a434a15c8..baeccd70c0 100644 --- a/vehicle.h +++ b/vehicle.h @@ -359,6 +359,16 @@ static inline uint16 GetVehiclePoolSize(void) 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 --git a/vehicle_gui.c b/vehicle_gui.c index 7ff1f65fd0..d5d60f7808 100644 --- a/vehicle_gui.c +++ b/vehicle_gui.c @@ -124,7 +124,7 @@ void BuildVehicleList(vehiclelist_d* vl, int type, PlayerID owner, StationID sta 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"); }