diff options
author | rubidium <rubidium@openttd.org> | 2006-12-09 14:18:08 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2006-12-09 14:18:08 +0000 |
commit | d02a614ebe30542176d5b6bbc3ac2d001e96e4ce (patch) | |
tree | b8771d527495f6a0be40198c740d7da3257764bd /industry.h | |
parent | 5b5b1d1514080cb4d1659507987681eede0a212b (diff) | |
download | openttd-d02a614ebe30542176d5b6bbc3ac2d001e96e4ce.tar.xz |
(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.
Diffstat (limited to 'industry.h')
-rw-r--r-- | industry.h | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/industry.h b/industry.h index e0620f35b..f8cf63c82 100644 --- a/industry.h +++ b/industry.h @@ -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()); } } |