summaryrefslogtreecommitdiff
path: root/town.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2006-12-09 14:18:08 +0000
committerrubidium <rubidium@openttd.org>2006-12-09 14:18:08 +0000
commitd02a614ebe30542176d5b6bbc3ac2d001e96e4ce (patch)
treeb8771d527495f6a0be40198c740d7da3257764bd /town.h
parent5b5b1d1514080cb4d1659507987681eede0a212b (diff)
downloadopenttd-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 'town.h')
-rw-r--r--town.h24
1 files changed, 14 insertions, 10 deletions
diff --git a/town.h b/town.h
index edf6b38d0..d1edca1c8 100644
--- a/town.h
+++ b/town.h
@@ -6,6 +6,10 @@
#include "oldpool.h"
#include "player.h"
+enum {
+ INVALID_TOWN = 0xFFFF,
+};
+
struct Town {
TileIndex xy;
@@ -162,6 +166,11 @@ static inline bool IsValidTown(const Town* town)
return town->xy != 0;
}
+static inline bool IsValidTownID(TownID index)
+{
+ return index < GetTownPoolSize() && IsValidTown(GetTown(index));
+}
+
VARDEF uint _total_towns;
static inline TownID GetMaxTownIndex(void)
@@ -184,28 +193,23 @@ static inline uint GetNumTowns(void)
*/
static inline Town *GetRandomTown(void)
{
- uint num = RandomRange(GetNumTowns());
- uint index = 0;
+ int num = RandomRange(GetNumTowns());
+ TownID index = INVALID_TOWN;
- while (num > 0) {
+ while (num >= 0) {
num--;
index++;
/* Make sure we have a valid industry */
- while (GetTown(index) == NULL) {
+ while (!IsValidTownID(index)) {
index++;
- if (index > GetMaxTownIndex()) index = 0;
+ assert(index <= GetMaxTownIndex());
}
}
return GetTown(index);
}
-static inline bool IsValidTownID(uint index)
-{
- return index < GetTownPoolSize() && IsValidTown(GetTown(index));
-}
-
void DestroyTown(Town *t);
static inline void DeleteTown(Town *t)