(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

22
town.h
View File

@@ -191,6 +191,28 @@ static inline TownID GetTownArraySize(void)
return _total_towns + 1;
}
/**
* Return a random valid town.
*/
static inline Town *GetRandomTown(void)
{
uint num = RandomRange(GetTownArraySize());
uint index = 0;
while (num > 0) {
num--;
index++;
/* Make sure we have a valid industry */
while (GetTown(index) == NULL) {
index++;
if (index == GetTownArraySize()) index = 0;
}
}
return GetTown(index);
}
static inline bool IsValidTownID(uint index)
{
return index < GetTownPoolSize() && IsValidTown(GetTown(index));