(svn r7452) -Fix: GetRandom(Industry|Town) must return a valid industry/town and should not need to loop over the pool for a second time.

This commit is contained in:
rubidium
2006-12-09 14:18:08 +00:00
parent af5c73c12e
commit fba426dc06
2 changed files with 26 additions and 17 deletions

View File

@@ -90,6 +90,11 @@ static inline bool IsValidIndustry(const Industry *industry)
return industry->xy != 0;
}
static inline bool IsValidIndustryID(IndustryID index)
{
return index < GetIndustryPoolSize() && IsValidIndustry(GetIndustry(index));
}
VARDEF int _total_industries;
static inline IndustryID GetMaxIndustryIndex(void)
@@ -108,23 +113,23 @@ static inline uint GetNumIndustries(void)
}
/**
* Return a random valid town.
* Return a random valid industry.
*/
static inline Industry *GetRandomIndustry(void)
{
uint num = RandomRange(GetNumIndustries());
uint index = 0;
int num = RandomRange(GetNumIndustries());
IndustryID index = INVALID_INDUSTRY;
if (GetNumIndustries() == 0) return NULL;
while (num > 0) {
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid industry */
while (GetIndustry(index) == NULL) {
while (!IsValidIndustryID(index)) {
index++;
if (index > GetMaxIndustryIndex()) index = 0;
assert(index <= GetMaxIndustryIndex());
}
}