(svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.

This commit is contained in:
matthijs 2006-12-05 13:58:20 +00:00
parent 75ec6d9b77
commit e6ade36de1
17 changed files with 74 additions and 42 deletions

View File

@ -3586,8 +3586,8 @@ static void AiStateRemoveStation(Player *p)
p->ai.state = AIS_1; p->ai.state = AIS_1;
// Get a list of all stations that are in use by a vehicle // Get a list of all stations that are in use by a vehicle
in_use = malloc(GetStationArraySize()); in_use = malloc(GetMaxStationIndex() + 1);
memset(in_use, 0, GetStationArraySize()); memset(in_use, 0, GetMaxStationIndex() + 1);
FOR_ALL_ORDERS(ord) { FOR_ALL_ORDERS(ord) {
if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1; if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
} }

View File

@ -377,9 +377,9 @@ static void AiNew_State_LocateRoute(Player *p)
if (p->ainew.temp == -1) { if (p->ainew.temp == -1) {
// First, we pick a random spot to search from // First, we pick a random spot to search from
if (p->ainew.from_type == AI_CITY) { if (p->ainew.from_type == AI_CITY) {
p->ainew.temp = AI_RandomRange(GetTownArraySize()); p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1);
} else { } 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 // to try again
p->ainew.temp++; p->ainew.temp++;
if (p->ainew.from_type == AI_CITY) { 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 { } 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... // 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) { if (p->ainew.temp == -1) {
// First, we pick a random spot to search to // First, we pick a random spot to search to
if (p->ainew.to_type == AI_CITY) { if (p->ainew.to_type == AI_CITY) {
p->ainew.temp = AI_RandomRange(GetTownArraySize()); p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1);
} else { } 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 // to try again
p->ainew.temp++; p->ainew.temp++;
if (p->ainew.to_type == AI_CITY) { 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 { } 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... // Don't do an attempt if we are trying the same id as the last time...

View File

@ -1151,7 +1151,7 @@ static void GlobalSortSignList(void)
uint n = 0; uint n = 0;
/* Create array for sorting */ /* 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) { if (_sign_sort == NULL) {
error("Could not allocate memory for the sign-sorting-list"); error("Could not allocate memory for the sign-sorting-list");
} }

View File

@ -92,13 +92,18 @@ static inline bool IsValidIndustry(const Industry *industry)
VARDEF int _total_industries; 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return _total_industries - 1;
}
static inline uint GetNumIndustries(void)
{
return _total_industries; return _total_industries;
} }
@ -107,10 +112,10 @@ static inline IndustryID GetIndustryArraySize(void)
*/ */
static inline Industry *GetRandomIndustry(void) static inline Industry *GetRandomIndustry(void)
{ {
uint num = RandomRange(GetIndustryArraySize()); uint num = RandomRange(GetNumIndustries());
uint index = 0; uint index = 0;
if (GetIndustryArraySize() == 0) return NULL; if (GetNumIndustries() == 0) return NULL;
while (num > 0) { while (num > 0) {
num--; num--;
@ -119,7 +124,7 @@ static inline Industry *GetRandomIndustry(void)
/* Make sure we have a valid industry */ /* Make sure we have a valid industry */
while (GetIndustry(index) == NULL) { while (GetIndustry(index) == NULL) {
index++; index++;
if (index == GetIndustryArraySize()) index = 0; if (index > GetMaxIndustryIndex()) index = 0;
} }
} }

View File

@ -1820,7 +1820,7 @@ void IndustryMonthlyLoop(void)
/* 3% chance that we start a new industry */ /* 3% chance that we start a new industry */
if (CHANCE16(3, 100)) { if (CHANCE16(3, 100)) {
MaybeNewIndustry(Random()); MaybeNewIndustry(Random());
} else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) { } else if (!_patches.smooth_economy) {
i = GetRandomIndustry(); i = GetRandomIndustry();
if (i != NULL) ChangeIndustryProduction(i); if (i != NULL) ChangeIndustryProduction(i);
} }

View File

@ -559,10 +559,10 @@ static void MakeSortedIndustryList(void)
int n = 0; int n = 0;
/* Don't attempt a sort if there are no industries */ /* Don't attempt a sort if there are no industries */
if (GetIndustryArraySize() == 0) return; if (GetNumIndustries() == 0) return;
/* Create array for sorting */ /* 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) if (_industry_sort == NULL)
error("Could not allocate memory for the industry-sorting-list"); error("Could not allocate memory for the industry-sorting-list");

View File

@ -1490,7 +1490,9 @@ static const char *ChatTabCompletionNextItem(uint *item)
} }
/* Then, try townnames */ /* 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; const Town *t;
FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_INFO) { FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_INFO) {

View File

@ -109,13 +109,18 @@ VARDEF BackuppedOrders _backup_orders_data[1];
DECLARE_OLD_POOL(Order, Order, 6, 1000) 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return GetOrderPoolSize() - 1;
}
static inline VehicleOrderID GetNumOrders(void)
{
return GetOrderPoolSize(); return GetOrderPoolSize();
} }

View File

@ -18,13 +18,18 @@ typedef struct Sign {
DECLARE_OLD_POOL(Sign, Sign, 2, 16000) 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return GetSignPoolSize() - 1;
}
static inline uint GetNumSigns(void)
{
return GetSignPoolSize(); return GetSignPoolSize();
} }

View File

@ -144,13 +144,18 @@ void ResortStationLists(void);
DECLARE_OLD_POOL(Station, Station, 6, 1000) 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return GetStationPoolSize() - 1;
}
static inline uint GetNumStations(void)
{
return GetStationPoolSize(); return GetStationPoolSize();
} }

View File

@ -2548,7 +2548,7 @@ void OnTick_Station(void)
if (_game_mode == GM_EDITOR) return; if (_game_mode == GM_EDITOR) return;
i = _station_tick_ctr; 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)); 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 */ /* 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) static void Save_ROADSTOP(void)

View File

@ -182,7 +182,7 @@ static void BuildStationsList(plstations_d* sl, PlayerID owner, byte facilities,
if (!(sl->flags & SL_REBUILD)) return; if (!(sl->flags & SL_REBUILD)) return;
/* Create array for sorting */ /* 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) if (station_sort == NULL)
error("Could not allocate memory for the station-sorting-list"); error("Could not allocate memory for the station-sorting-list");

13
town.h
View File

@ -164,13 +164,18 @@ static inline bool IsValidTown(const Town* town)
VARDEF uint _total_towns; 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return _total_towns - 1;
}
static inline uint GetNumTowns(void)
{
return _total_towns; return _total_towns;
} }
@ -179,7 +184,7 @@ static inline TownID GetTownArraySize(void)
*/ */
static inline Town *GetRandomTown(void) static inline Town *GetRandomTown(void)
{ {
uint num = RandomRange(GetTownArraySize()); uint num = RandomRange(GetNumTowns());
uint index = 0; uint index = 0;
while (num > 0) { while (num > 0) {
@ -189,7 +194,7 @@ static inline Town *GetRandomTown(void)
/* Make sure we have a valid industry */ /* Make sure we have a valid industry */
while (GetTown(index) == NULL) { while (GetTown(index) == NULL) {
index++; index++;
if (index == GetTownArraySize()) index = 0; if (index > GetMaxTownIndex()) index = 0;
} }
} }

View File

@ -436,12 +436,12 @@ void OnTick_Town(void)
/* Make sure each town's tickhandler invocation frequency is about the /* Make sure each town's tickhandler invocation frequency is about the
* same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */ * 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;
_cur_town_iter -= TOWN_GROWTH_FREQUENCY) { _cur_town_iter -= TOWN_GROWTH_FREQUENCY) {
uint32 i = _cur_town_ctr; uint32 i = _cur_town_ctr;
if (++_cur_town_ctr >= GetTownArraySize()) if (++_cur_town_ctr > GetMaxTownIndex())
_cur_town_ctr = 0; _cur_town_ctr = 0;
if (IsValidTownID(i)) TownTickHandler(GetTown(i)); if (IsValidTownID(i)) TownTickHandler(GetTown(i));
@ -1093,7 +1093,7 @@ bool GenerateTowns(void)
// give it a last try, but now more aggressive // give it a last try, but now more aggressive
if (num == 0 && CreateRandomTown(10000, 0) == NULL) { if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
if (GetTownArraySize() == 0) { if (GetNumTowns() == 0) {
/* XXX - can we handle that more gracefully? */ /* XXX - can we handle that more gracefully? */
if (_game_mode != GM_EDITOR) error("Could not generate any town"); 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 /* This is to ensure all pointers are within the limits of
* the size of the TownPool */ * the size of the TownPool */
if (_cur_town_ctr >= GetTownArraySize()) if (_cur_town_ctr > GetMaxTownIndex())
_cur_town_ctr = 0; _cur_town_ctr = 0;
} }

View File

@ -409,7 +409,7 @@ static void MakeSortedTownList(void)
uint n = 0; uint n = 0;
/* Create array for sorting */ /* 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) if (_town_sort == NULL)
error("Could not allocate memory for the town-sorting-list"); error("Could not allocate memory for the town-sorting-list");

View File

@ -2248,7 +2248,7 @@ static int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs)
/* Extend the list size for BuildDepotVehicleList() */ /* Extend the list size for BuildDepotVehicleList() */
static inline void ExtendVehicleListSize(const Vehicle ***engine_list, uint16 *engine_list_length, uint16 step_size) 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])); *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 && IsFrontEngine(v)) ||
(type != VEH_Train && v->subtype <= subtype))) { (type != VEH_Train && v->subtype <= subtype))) {
/* TODO find a better estimate on the total number of vehicles for current player */ /* 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; (*sort_list)[n++] = v;
} }
} }

View File

@ -370,13 +370,18 @@ Direction GetDirectionTowards(const Vehicle* v, int x, int y);
DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125) 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 /* TODO - This isn't the real content of the function, but
* with the new pool-system this will be replaced with one that * 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. * the next safe value we are sure about everything is below.
*/ */
return GetVehiclePoolSize() - 1;
}
static inline uint GetNumVehicles(void)
{
return GetVehiclePoolSize(); return GetVehiclePoolSize();
} }