summaryrefslogtreecommitdiff
path: root/town.h
diff options
context:
space:
mode:
authortruelight <truelight@openttd.org>2006-08-22 21:14:45 +0000
committertruelight <truelight@openttd.org>2006-08-22 21:14:45 +0000
commitceb523c29f364f24b5e6b68b13249187ff6ac371 (patch)
tree3b5390c9adf2806b379ecf5be65b211d1a2e8b4d /town.h
parent3cdabcbbac5ae64c132f0162090079d8c5cf3f90 (diff)
downloadopenttd-ceb523c29f364f24b5e6b68b13249187ff6ac371.tar.xz
(svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
Diffstat (limited to 'town.h')
-rw-r--r--town.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/town.h b/town.h
index 122a373dc..1ad4a7f04 100644
--- a/town.h
+++ b/town.h
@@ -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));