summaryrefslogtreecommitdiff
path: root/src/town_cmd.cpp
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-06-26 15:08:54 +0000
committersmatz <smatz@openttd.org>2009-06-26 15:08:54 +0000
commitff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6 (patch)
tree7772bd531c7e63794d47fdeecfea6399fe5697a6 /src/town_cmd.cpp
parentb668c24d463ca8059fd9be59efddb39298de9646 (diff)
downloadopenttd-ff33ed94ceef4dabb45b5f8ccbae81a1c4402ba6.tar.xz
(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()
Diffstat (limited to 'src/town_cmd.cpp')
-rw-r--r--src/town_cmd.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index 86698d4f6..dabf20ca3 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -119,6 +119,30 @@ void Town::InitializeLayout(TownLayout layout)
this->layout = TileHash(TileX(this->xy), TileY(this->xy)) % (NUM_TLS - 1);
}
+/**
+ * Return a random valid town.
+ * @return random town, NULL if there are no towns
+ */
+/* static */ Town *Town::GetRandom()
+{
+ if (Town::GetNumItems() == 0) return NULL;
+ int num = RandomRange((uint16)Town::GetNumItems());
+ size_t index = MAX_UVALUE(size_t);
+
+ while (num >= 0) {
+ num--;
+ index++;
+
+ /* Make sure we have a valid town */
+ while (!Town::IsValidID(index)) {
+ index++;
+ assert(index < Town::GetPoolSize());
+ }
+ }
+
+ return Town::Get(index);
+}
+
Money HouseSpec::GetRemovalCost() const
{
return (_price.remove_house * this->removal_cost) >> 8;