From 9218fc16e6fff350e6209b6b90b8597fc431f00f Mon Sep 17 00:00:00 2001 From: matthijs Date: Tue, 5 Dec 2006 13:58:20 +0000 Subject: (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface. --- ai/default/default.c | 4 ++-- ai/trolly/trolly.c | 16 ++++++++-------- graph_gui.c | 2 +- industry.h | 15 ++++++++++----- industry_cmd.c | 2 +- industry_gui.c | 4 ++-- network_gui.c | 4 +++- order.h | 9 +++++++-- signs.h | 9 +++++++-- station.h | 9 +++++++-- station_cmd.c | 4 ++-- station_gui.c | 2 +- town.h | 13 +++++++++---- town_cmd.c | 8 ++++---- town_gui.c | 2 +- vehicle.c | 4 ++-- vehicle.h | 9 +++++++-- 17 files changed, 74 insertions(+), 42 deletions(-) diff --git a/ai/default/default.c b/ai/default/default.c index 496a17a62..f21a9174a 100644 --- a/ai/default/default.c +++ b/ai/default/default.c @@ -3586,8 +3586,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(GetStationArraySize()); - memset(in_use, 0, GetStationArraySize()); + in_use = malloc(GetMaxStationIndex() + 1); + memset(in_use, 0, GetMaxStationIndex() + 1); FOR_ALL_ORDERS(ord) { if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1; } diff --git a/ai/trolly/trolly.c b/ai/trolly/trolly.c index 26a7172ff..23a700b06 100644 --- a/ai/trolly/trolly.c +++ b/ai/trolly/trolly.c @@ -377,9 +377,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(GetTownArraySize()); + p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1); } else { - p->ainew.temp = AI_RandomRange(GetIndustryArraySize()); + p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1); } } @@ -389,9 +389,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 >= GetTownArraySize()) p->ainew.temp = 0; + if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 0; } else { - if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0; + if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 0; } // Don't do an attempt if we are trying the same id as the last time... @@ -413,9 +413,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(GetTownArraySize()); + p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1); } else { - p->ainew.temp = AI_RandomRange(GetIndustryArraySize()); + p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1); } } @@ -529,9 +529,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 >= GetTownArraySize()) p->ainew.temp = 0; + if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 0; } else { - if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0; + if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 0; } // Don't do an attempt if we are trying the same id as the last time... diff --git a/graph_gui.c b/graph_gui.c index eedeb949d..2968e3aa9 100644 --- a/graph_gui.c +++ b/graph_gui.c @@ -1151,7 +1151,7 @@ static void GlobalSortSignList(void) uint n = 0; /* Create array for sorting */ - _sign_sort = realloc((void *)_sign_sort, GetSignArraySize() * sizeof(_sign_sort[0])); + _sign_sort = realloc((void *)_sign_sort, (GetMaxSignIndex() + 1)* 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 db1b915f3..65da9bd0e 100644 --- a/industry.h +++ b/industry.h @@ -92,13 +92,18 @@ static inline bool IsValidIndustry(const Industry *industry) VARDEF int _total_industries; -static inline IndustryID GetIndustryArraySize(void) +static inline IndustryID GetMaxIndustryIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return _total_industries - 1; +} + +static inline uint GetNumIndustries(void) +{ return _total_industries; } @@ -107,10 +112,10 @@ static inline IndustryID GetIndustryArraySize(void) */ static inline Industry *GetRandomIndustry(void) { - uint num = RandomRange(GetIndustryArraySize()); + uint num = RandomRange(GetNumIndustries()); uint index = 0; - if (GetIndustryArraySize() == 0) return NULL; + if (GetNumIndustries() == 0) return NULL; while (num > 0) { num--; @@ -119,7 +124,7 @@ static inline Industry *GetRandomIndustry(void) /* Make sure we have a valid industry */ while (GetIndustry(index) == NULL) { index++; - if (index == GetIndustryArraySize()) index = 0; + if (index > GetMaxIndustryIndex()) index = 0; } } diff --git a/industry_cmd.c b/industry_cmd.c index 4516de189..ec4693304 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -1820,7 +1820,7 @@ void IndustryMonthlyLoop(void) /* 3% chance that we start a new industry */ if (CHANCE16(3, 100)) { MaybeNewIndustry(Random()); - } else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) { + } else if (!_patches.smooth_economy) { i = GetRandomIndustry(); if (i != NULL) ChangeIndustryProduction(i); } diff --git a/industry_gui.c b/industry_gui.c index 445422a4a..13fbac9cc 100644 --- a/industry_gui.c +++ b/industry_gui.c @@ -559,10 +559,10 @@ static void MakeSortedIndustryList(void) int n = 0; /* Don't attempt a sort if there are no industries */ - if (GetIndustryArraySize() == 0) return; + if (GetNumIndustries() == 0) return; /* Create array for sorting */ - _industry_sort = realloc((void *)_industry_sort, GetIndustryArraySize() * sizeof(_industry_sort[0])); + _industry_sort = realloc((void *)_industry_sort, (GetMaxIndustryIndex() + 1) * 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 05ed7379d..30efc8e51 100644 --- a/network_gui.c +++ b/network_gui.c @@ -1490,7 +1490,9 @@ static const char *ChatTabCompletionNextItem(uint *item) } /* Then, try townnames */ - if (*item < (uint)MAX_CLIENT_INFO + GetTownArraySize()) { + /* Not that the following assumes all town indices are adjacent, ie no + * towns have been deleted. */ + if (*item <= (uint)MAX_CLIENT_INFO + GetMaxTownIndex()) { const Town *t; FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_INFO) { diff --git a/order.h b/order.h index d0265cd2a..7031f2455 100644 --- a/order.h +++ b/order.h @@ -109,13 +109,18 @@ VARDEF BackuppedOrders _backup_orders_data[1]; DECLARE_OLD_POOL(Order, Order, 6, 1000) -static inline VehicleOrderID GetOrderArraySize(void) +static inline VehicleOrderID GetMaxOrderIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return GetOrderPoolSize() - 1; +} + +static inline VehicleOrderID GetNumOrders(void) +{ return GetOrderPoolSize(); } diff --git a/signs.h b/signs.h index 0ded78353..9c9fd2982 100644 --- a/signs.h +++ b/signs.h @@ -18,13 +18,18 @@ typedef struct Sign { DECLARE_OLD_POOL(Sign, Sign, 2, 16000) -static inline SignID GetSignArraySize(void) +static inline SignID GetMaxSignIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return GetSignPoolSize() - 1; +} + +static inline uint GetNumSigns(void) +{ return GetSignPoolSize(); } diff --git a/station.h b/station.h index 1187c1f35..2018f0777 100644 --- a/station.h +++ b/station.h @@ -144,13 +144,18 @@ void ResortStationLists(void); DECLARE_OLD_POOL(Station, Station, 6, 1000) -static inline StationID GetStationArraySize(void) +static inline StationID GetMaxStationIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return GetStationPoolSize() - 1; +} + +static inline uint GetNumStations(void) +{ return GetStationPoolSize(); } diff --git a/station_cmd.c b/station_cmd.c index d2f8288a5..21c98a9e4 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -2548,7 +2548,7 @@ void OnTick_Station(void) if (_game_mode == GM_EDITOR) return; i = _station_tick_ctr; - if (++_station_tick_ctr == GetStationArraySize()) _station_tick_ctr = 0; + if (++_station_tick_ctr > GetMaxStationIndex()) _station_tick_ctr = 0; if (IsValidStationID(i)) StationHandleBigTick(GetStation(i)); @@ -3098,7 +3098,7 @@ static void Load_STNS(void) } /* This is to ensure all pointers are within the limits of _stations_size */ - if (_station_tick_ctr > GetStationArraySize()) _station_tick_ctr = 0; + if (_station_tick_ctr > GetMaxStationIndex()) _station_tick_ctr = 0; } static void Save_ROADSTOP(void) diff --git a/station_gui.c b/station_gui.c index c148d9cf6..247b3fb05 100644 --- a/station_gui.c +++ b/station_gui.c @@ -182,7 +182,7 @@ static void BuildStationsList(plstations_d* sl, PlayerID owner, byte facilities, if (!(sl->flags & SL_REBUILD)) return; /* Create array for sorting */ - station_sort = malloc(GetStationArraySize() * sizeof(station_sort[0])); + station_sort = malloc((GetMaxStationIndex() + 1) * 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 896e3415c..50a47c14b 100644 --- a/town.h +++ b/town.h @@ -164,13 +164,18 @@ static inline bool IsValidTown(const Town* town) VARDEF uint _total_towns; -static inline TownID GetTownArraySize(void) +static inline TownID GetMaxTownIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return _total_towns - 1; +} + +static inline uint GetNumTowns(void) +{ return _total_towns; } @@ -179,7 +184,7 @@ static inline TownID GetTownArraySize(void) */ static inline Town *GetRandomTown(void) { - uint num = RandomRange(GetTownArraySize()); + uint num = RandomRange(GetNumTowns()); uint index = 0; while (num > 0) { @@ -189,7 +194,7 @@ static inline Town *GetRandomTown(void) /* Make sure we have a valid industry */ while (GetTown(index) == NULL) { index++; - if (index == GetTownArraySize()) index = 0; + if (index > GetMaxTownIndex()) index = 0; } } diff --git a/town_cmd.c b/town_cmd.c index d5c0b10b2..c56e01b98 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -436,12 +436,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 += GetTownArraySize(); + for (_cur_town_iter += GetMaxTownIndex() + 1; _cur_town_iter >= TOWN_GROWTH_FREQUENCY; _cur_town_iter -= TOWN_GROWTH_FREQUENCY) { uint32 i = _cur_town_ctr; - if (++_cur_town_ctr >= GetTownArraySize()) + if (++_cur_town_ctr > GetMaxTownIndex()) _cur_town_ctr = 0; if (IsValidTownID(i)) TownTickHandler(GetTown(i)); @@ -1093,7 +1093,7 @@ bool GenerateTowns(void) // give it a last try, but now more aggressive if (num == 0 && CreateRandomTown(10000, 0) == NULL) { - if (GetTownArraySize() == 0) { + if (GetNumTowns() == 0) { /* XXX - can we handle that more gracefully? */ if (_game_mode != GM_EDITOR) error("Could not generate any town"); @@ -1937,7 +1937,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 >= GetTownArraySize()) + if (_cur_town_ctr > GetMaxTownIndex()) _cur_town_ctr = 0; } diff --git a/town_gui.c b/town_gui.c index 03f87edad..1434edcdd 100644 --- a/town_gui.c +++ b/town_gui.c @@ -409,7 +409,7 @@ static void MakeSortedTownList(void) uint n = 0; /* Create array for sorting */ - _town_sort = realloc((void*)_town_sort, GetTownArraySize() * sizeof(_town_sort[0])); + _town_sort = realloc((void*)_town_sort, (GetMaxTownIndex() + 1) * sizeof(_town_sort[0])); if (_town_sort == NULL) error("Could not allocate memory for the town-sorting-list"); diff --git a/vehicle.c b/vehicle.c index 8af9b1960..9c657483c 100644 --- a/vehicle.c +++ b/vehicle.c @@ -2248,7 +2248,7 @@ static int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs) /* Extend the list size for BuildDepotVehicleList() */ static inline void ExtendVehicleListSize(const Vehicle ***engine_list, uint16 *engine_list_length, uint16 step_size) { - *engine_list_length = min(*engine_list_length + step_size, GetVehicleArraySize()); + *engine_list_length = min(*engine_list_length + step_size, GetMaxVehicleIndex() + 1); *engine_list = realloc((void*)*engine_list, (*engine_list_length) * sizeof((*engine_list)[0])); } @@ -2391,7 +2391,7 @@ uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array (type == VEH_Train && IsFrontEngine(v)) || (type != VEH_Train && v->subtype <= subtype))) { /* TODO find a better estimate on the total number of vehicles for current player */ - if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetVehicleArraySize()/4); + if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles()/4); (*sort_list)[n++] = v; } } diff --git a/vehicle.h b/vehicle.h index 9f842b155..1aa24ee84 100644 --- a/vehicle.h +++ b/vehicle.h @@ -370,13 +370,18 @@ Direction GetDirectionTowards(const Vehicle* v, int x, int y); DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) -static inline VehicleID GetVehicleArraySize(void) +static inline VehicleID GetMaxVehicleIndex(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 + * _really_ returns the highest index. Now it just returns * the next safe value we are sure about everything is below. */ + return GetVehiclePoolSize() - 1; +} + +static inline uint GetNumVehicles(void) +{ return GetVehiclePoolSize(); } -- cgit v1.2.3-54-g00ecf