(svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
authormatthijs
Tue, 05 Dec 2006 13:58:20 +0000
changeset 5247 c3eece01af11
parent 5246 29224c621fd6
child 5248 2aa0c4cc8f23
(svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
ai/default/default.c
ai/trolly/trolly.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.c
vehicle.h
--- a/ai/default/default.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/ai/default/default.c	Tue Dec 05 13:58:20 2006 +0000
@@ -3586,8 +3586,8 @@
 	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;
 	}
--- a/ai/trolly/trolly.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/ai/trolly/trolly.c	Tue Dec 05 13:58:20 2006 +0000
@@ -377,9 +377,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(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 @@
 			//  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 @@
 	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 @@
 	//  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...
--- a/graph_gui.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/graph_gui.c	Tue Dec 05 13:58:20 2006 +0000
@@ -1151,7 +1151,7 @@
 	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");
 	}
--- a/industry.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/industry.h	Tue Dec 05 13:58:20 2006 +0000
@@ -92,13 +92,18 @@
 
 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 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 @@
 		/* Make sure we have a valid industry */
 		while (GetIndustry(index) == NULL) {
 			index++;
-			if (index == GetIndustryArraySize()) index = 0;
+			if (index > GetMaxIndustryIndex()) index = 0;
 		}
 	}
 
--- a/industry_cmd.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/industry_cmd.c	Tue Dec 05 13:58:20 2006 +0000
@@ -1820,7 +1820,7 @@
 	/* 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);
 	}
--- a/industry_gui.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/industry_gui.c	Tue Dec 05 13:58:20 2006 +0000
@@ -559,10 +559,10 @@
 	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");
 
--- a/network_gui.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/network_gui.c	Tue Dec 05 13:58:20 2006 +0000
@@ -1490,7 +1490,9 @@
 	}
 
 	/* 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) {
--- a/order.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/order.h	Tue Dec 05 13:58:20 2006 +0000
@@ -109,13 +109,18 @@
 
 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();
 }
 
--- a/signs.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/signs.h	Tue Dec 05 13:58:20 2006 +0000
@@ -18,13 +18,18 @@
 
 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();
 }
 
--- a/station.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/station.h	Tue Dec 05 13:58:20 2006 +0000
@@ -144,13 +144,18 @@
 
 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();
 }
 
--- a/station_cmd.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/station_cmd.c	Tue Dec 05 13:58:20 2006 +0000
@@ -2548,7 +2548,7 @@
 	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 @@
 	}
 
 	/* 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)
--- a/station_gui.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/station_gui.c	Tue Dec 05 13:58:20 2006 +0000
@@ -182,7 +182,7 @@
 	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");
 
--- a/town.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/town.h	Tue Dec 05 13:58:20 2006 +0000
@@ -164,13 +164,18 @@
 
 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 Town *GetRandomTown(void)
 {
-	uint num = RandomRange(GetTownArraySize());
+	uint num = RandomRange(GetNumTowns());
 	uint index = 0;
 
 	while (num > 0) {
@@ -189,7 +194,7 @@
 		/* Make sure we have a valid industry */
 		while (GetTown(index) == NULL) {
 			index++;
-			if (index == GetTownArraySize()) index = 0;
+			if (index > GetMaxTownIndex()) index = 0;
 		}
 	}
 
--- a/town_cmd.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/town_cmd.c	Tue Dec 05 13:58:20 2006 +0000
@@ -436,12 +436,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 += 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 @@
 
 	// 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 @@
 
 	/* 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;
 }
 
--- a/town_gui.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/town_gui.c	Tue Dec 05 13:58:20 2006 +0000
@@ -409,7 +409,7 @@
 	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");
 
--- a/vehicle.c	Tue Dec 05 12:17:31 2006 +0000
+++ b/vehicle.c	Tue Dec 05 13:58:20 2006 +0000
@@ -2248,7 +2248,7 @@
 /* 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 @@
 					(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;
 				}
 			}
--- a/vehicle.h	Tue Dec 05 12:17:31 2006 +0000
+++ b/vehicle.h	Tue Dec 05 13:58:20 2006 +0000
@@ -370,13 +370,18 @@
 
 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();
 }