From ff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6 Mon Sep 17 00:00:00 2001 From: smatz Date: Fri, 26 Jun 2009 15:08:54 +0000 Subject: (svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom() --- src/industry_cmd.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/industry_cmd.cpp') 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); } } -- cgit v1.2.3-70-g09d2