changeset 4354:957b7fc45ad6 draft

(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.
author truelight <truelight@openttd.org>
date Tue, 22 Aug 2006 20:41:26 +0000
parents 29e729966b22
children 668699e4de17
files ai/default/default.c ai/trolly/trolly.c economy.c graph_gui.c industry.h industry_cmd.c industry_gui.c network_gui.c order.h signs.h station.h station_cmd.c station_gui.c town.h town_cmd.c town_gui.c vehicle.h vehicle_gui.c
diffstat 18 files changed, 94 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/ai/default/default.c
+++ b/ai/default/default.c
@@ -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;
 	}
--- a/ai/trolly/trolly.c
+++ b/ai/trolly/trolly.c
@@ -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...
--- a/economy.c
+++ b/economy.c
@@ -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) ||
--- a/graph_gui.c
+++ b/graph_gui.c
@@ -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");
 	}
--- a/industry.h
+++ b/industry.h
@@ -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;
 
--- a/industry_cmd.c
+++ b/industry_cmd.c
@@ -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);
 	}
 
--- a/industry_gui.c
+++ b/industry_gui.c
@@ -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");
 
--- a/network_gui.c
+++ b/network_gui.c
@@ -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) {
--- a/order.h
+++ b/order.h
@@ -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.
  */
--- a/signs.h
+++ b/signs.h
@@ -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.
  */
--- a/station.h
+++ b/station.h
@@ -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.
  */
--- a/station_cmd.c
+++ b/station_cmd.c
@@ -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)
--- a/station_gui.c
+++ b/station_gui.c
@@ -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");
 
--- a/town.h
+++ b/town.h
@@ -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;
 
--- a/town_cmd.c
+++ b/town_cmd.c
@@ -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;
 }
 
--- a/town_gui.c
+++ b/town_gui.c
@@ -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");
 
--- a/vehicle.h
+++ b/vehicle.h
@@ -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.
  */
--- a/vehicle_gui.c
+++ b/vehicle_gui.c
@@ -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");
 	}