(svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.

This commit is contained in:
truelight
2006-08-22 21:14:45 +00:00
parent 3cdabcbbac
commit ceb523c29f
7 changed files with 63 additions and 20 deletions

View File

@@ -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)