changeset 12252:f758517b6645 draft

(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()
author smatz <smatz@openttd.org>
date Fri, 26 Jun 2009 15:08:54 +0000
parents 0047ec1f61d2
children 7e1bd891bbaa
files src/industry.h src/industry_cmd.cpp src/subsidy.cpp src/town.h src/town_cmd.cpp
diffstat 5 files changed, 60 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry.h
+++ b/src/industry.h
@@ -135,6 +135,8 @@
 
 	Industry(TileIndex tile = INVALID_TILE) : xy(tile) {}
 	~Industry();
+
+	static Industry *GetRandom();
 };
 
 struct IndustryTileTable {
@@ -300,30 +302,6 @@
 	memset(&_industry_counts, 0, sizeof(_industry_counts));
 }
 
-/**
- * Return a random valid industry.
- */
-static inline Industry *GetRandomIndustry()
-{
-	if (Industry::GetNumItems() == 0) return NULL;
-
-	int num = RandomRange((uint16)Industry::GetNumItems());
-	IndustryID index = INVALID_INDUSTRY;
-
-	while (num >= 0) {
-		num--;
-		index++;
-
-		/* Make sure we have a valid industry */
-		while (!Industry::IsValidID(index)) {
-			index++;
-			assert(index < Industry::GetPoolSize());
-		}
-	}
-
-	return Industry::Get(index);
-}
-
 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
 
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -171,6 +171,32 @@
 	Station::RecomputeIndustriesNearForAll();
 }
 
+
+/**
+ * Return a random valid industry.
+ * @return random industry, NULL if there are no industries
+ */
+/* static */ Industry *Industry::GetRandom()
+{
+	if (Industry::GetNumItems() == 0) return NULL;
+	int num = RandomRange((uint16)Industry::GetNumItems());
+	size_t index = MAX_UVALUE(size_t);
+
+	while (num >= 0) {
+		num--;
+		index++;
+
+		/* Make sure we have a valid industry */
+		while (!Industry::IsValidID(index)) {
+			index++;
+			assert(index < Industry::GetPoolSize());
+		}
+	}
+
+	return Industry::Get(index);
+}
+
+
 static void IndustryDrawSugarMine(const TileInfo *ti)
 {
 	const DrawIndustryAnimationStruct *d;
@@ -2289,7 +2315,7 @@
 		if (Chance16(3, 100)) {
 			MaybeNewIndustry();
 		} else {
-			Industry *i = GetRandomIndustry();
+			Industry *i = Industry::GetRandom();
 			if (i != NULL) ChangeIndustryProduction(i, false);
 		}
 	}
--- a/src/subsidy.cpp
+++ b/src/subsidy.cpp
@@ -126,10 +126,10 @@
 
 	fr->distance = UINT_MAX;
 
-	fr->from = from = GetRandomTown();
+	fr->from = from = Town::GetRandom();
 	if (from == NULL || from->population < 400) return;
 
-	fr->to = to = GetRandomTown();
+	fr->to = to = Town::GetRandom();
 	if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
 		return;
 
@@ -144,7 +144,7 @@
 
 	fr->distance = UINT_MAX;
 
-	fr->from = i = GetRandomIndustry();
+	fr->from = i = Industry::GetRandom();
 	if (i == NULL) return;
 
 	/* Randomize cargo type */
@@ -170,7 +170,7 @@
 
 	if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
 		/*  The destination is a town */
-		Town *t = GetRandomTown();
+		Town *t = Town::GetRandom();
 
 		/* Only want big towns */
 		if (t == NULL || t->population < 900) return;
@@ -179,7 +179,7 @@
 		fr->to = t;
 	} else {
 		/* The destination is an industry */
-		Industry *i2 = GetRandomIndustry();
+		Industry *i2 = Industry::GetRandom();
 
 		/* The industry must accept the cargo */
 		if (i2 == NULL || i == i2 ||
--- a/src/town.h
+++ b/src/town.h
@@ -140,6 +140,8 @@
 	{
 		return Town::Get(GetTownIndex(tile));
 	}
+
+	static Town *GetRandom();
 };
 
 uint32 GetWorldPopulation();
@@ -179,28 +181,6 @@
 
 TileIndexDiff GetHouseNorthPart(HouseID &house);
 
-/**
- * Return a random valid town.
- */
-static inline Town *GetRandomTown()
-{
-	int num = RandomRange((uint16)Town::GetNumItems());
-	TownID index = INVALID_TOWN;
-
-	while (num >= 0) {
-		num--;
-
-		index++;
-		/* Make sure we have a valid town */
-		while (!Town::IsValidID(index)) {
-			index++;
-			assert(index < Town::GetPoolSize());
-		}
-	}
-
-	return Town::Get(index);
-}
-
 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX, const Town *ignore = NULL);
 
 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -119,6 +119,30 @@
 	this->layout = TileHash(TileX(this->xy), TileY(this->xy)) % (NUM_TLS - 1);
 }
 
+/**
+ * Return a random valid town.
+ * @return random town, NULL if there are no towns
+ */
+/* static */ Town *Town::GetRandom()
+{
+	if (Town::GetNumItems() == 0) return NULL;
+	int num = RandomRange((uint16)Town::GetNumItems());
+	size_t index = MAX_UVALUE(size_t);
+
+	while (num >= 0) {
+		num--;
+		index++;
+
+		/* Make sure we have a valid town */
+		while (!Town::IsValidID(index)) {
+			index++;
+			assert(index < Town::GetPoolSize());
+		}
+	}
+
+	return Town::Get(index);
+}
+
 Money HouseSpec::GetRemovalCost() const
 {
 	return (_price.remove_house * this->removal_cost) >> 8;