summaryrefslogtreecommitdiff
path: root/src/industry_cmd.cpp
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/industry_cmd.cpp
parentb668c24d463ca8059fd9be59efddb39298de9646 (diff)
downloadopenttd-ff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6.tar.xz
(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()
Diffstat (limited to 'src/industry_cmd.cpp')
-rw-r--r--src/industry_cmd.cpp28
1 files changed, 27 insertions, 1 deletions
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);
}
}