(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.
This commit is contained in:
truelight 2006-08-22 20:41:26 +00:00
parent 2e0d16026b
commit 5fd9aeb12b
18 changed files with 94 additions and 34 deletions

View File

@ -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;
}

View File

@ -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...

View File

@ -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) ||

View File

@ -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");
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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");

View File

@ -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) {

10
order.h
View File

@ -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.
*/

10
signs.h
View File

@ -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.
*/

View File

@ -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.
*/

View File

@ -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)

View File

@ -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");

14
town.h
View File

@ -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;

View File

@ -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;
}

View File

@ -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");

View File

@ -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.
*/

View File

@ -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");
}