From ceb523c29f364f24b5e6b68b13249187ff6ac371 Mon Sep 17 00:00:00 2001 From: truelight Date: Tue, 22 Aug 2006 21:14:45 +0000 Subject: (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned. --- industry.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'industry.h') diff --git a/industry.h b/industry.h index dcd489b95..2b642dece 100644 --- a/industry.h +++ b/industry.h @@ -107,6 +107,30 @@ static inline IndustryID GetIndustryArraySize(void) return _total_industries + 1; } +/** + * Return a random valid town. + */ +static inline Industry *GetRandomIndustry(void) +{ + uint num = RandomRange(GetIndustryArraySize()); + uint index = 0; + + if (GetIndustryArraySize() == 0) return NULL; + + while (num > 0) { + num--; + + index++; + /* Make sure we have a valid industry */ + while (GetIndustry(index) == NULL) { + index++; + if (index == GetIndustryArraySize()) index = 0; + } + } + + return GetIndustry(index); +} + #define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL) if (IsValidIndustry(i)) #define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0) -- cgit v1.2.3-70-g09d2