summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-06-26 15:08:54 +0000
committersmatz <smatz@openttd.org>2009-06-26 15:08:54 +0000
commitff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6 (patch)
tree7772bd531c7e63794d47fdeecfea6399fe5697a6 /src
parentb668c24d463ca8059fd9be59efddb39298de9646 (diff)
downloadopenttd-ff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6.tar.xz
(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()
Diffstat (limited to 'src')
-rw-r--r--src/industry.h26
-rw-r--r--src/industry_cmd.cpp28
-rw-r--r--src/subsidy.cpp10
-rw-r--r--src/town.h24
-rw-r--r--src/town_cmd.cpp24
5 files changed, 60 insertions, 52 deletions
diff --git a/src/industry.h b/src/industry.h
index 7bd1ead44..63843ad00 100644
--- a/src/industry.h
+++ b/src/industry.h
@@ -135,6 +135,8 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
Industry(TileIndex tile = INVALID_TILE) : xy(tile) {}
~Industry();
+
+ static Industry *GetRandom();
};
struct IndustryTileTable {
@@ -300,30 +302,6 @@ static inline void ResetIndustryCounts()
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)
diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp
index c97215259..629123686 100644
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -171,6 +171,32 @@ Industry::~Industry()
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 @@ void IndustryDailyLoop()
if (Chance16(3, 100)) {
MaybeNewIndustry();
} else {
- Industry *i = GetRandomIndustry();
+ Industry *i = Industry::GetRandom();
if (i != NULL) ChangeIndustryProduction(i, false);
}
}
diff --git a/src/subsidy.cpp b/src/subsidy.cpp
index c61129099..fbc8c01c6 100644
--- a/src/subsidy.cpp
+++ b/src/subsidy.cpp
@@ -126,10 +126,10 @@ static void FindSubsidyPassengerRoute(FoundRoute *fr)
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 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
fr->distance = UINT_MAX;
- fr->from = i = GetRandomIndustry();
+ fr->from = i = Industry::GetRandom();
if (i == NULL) return;
/* Randomize cargo type */
@@ -170,7 +170,7 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
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 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
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 ||
diff --git a/src/town.h b/src/town.h
index f4cdf3e37..c65569ad1 100644
--- a/src/town.h
+++ b/src/town.h
@@ -140,6 +140,8 @@ struct Town : TownPool::PoolItem<&_town_pool> {
{
return Town::Get(GetTownIndex(tile));
}
+
+ static Town *GetRandom();
};
uint32 GetWorldPopulation();
@@ -179,28 +181,6 @@ bool CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
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)
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index 86698d4f6..dabf20ca3 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -119,6 +119,30 @@ void Town::InitializeLayout(TownLayout layout)
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;