summaryrefslogtreecommitdiff
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
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.
-rw-r--r--ai/default/default.c6
-rw-r--r--disaster_cmd.c2
-rw-r--r--economy.c21
-rw-r--r--industry.h24
-rw-r--r--industry_cmd.c6
-rw-r--r--smallmap_gui.c2
-rw-r--r--town.h22
7 files changed, 63 insertions, 20 deletions
diff --git a/ai/default/default.c b/ai/default/default.c
index 3de583bf4..08af2b119 100644
--- a/ai/default/default.c
+++ b/ai/default/default.c
@@ -441,14 +441,12 @@ typedef struct FoundRoute {
static Town *AiFindRandomTown(void)
{
- Town *t = GetTown(RandomRange(GetTownArraySize()));
- return IsValidTown(t) ? t : NULL;
+ return GetRandomTown();
}
static Industry *AiFindRandomIndustry(void)
{
- Industry *i = GetIndustry(RandomRange(GetIndustryArraySize()));
- return IsValidIndustry(i) ? i : NULL;
+ return GetRandomIndustry();
}
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
diff --git a/disaster_cmd.c b/disaster_cmd.c
index ea1f3cf97..d44214e17 100644
--- a/disaster_cmd.c
+++ b/disaster_cmd.c
@@ -2,10 +2,10 @@
#include "stdafx.h"
#include "openttd.h"
+#include "functions.h"
#include "industry_map.h"
#include "station_map.h"
#include "table/strings.h"
-#include "functions.h"
#include "map.h"
#include "tile.h"
#include "vehicle.h"
diff --git a/economy.c b/economy.c
index 19fa8952c..1abdda6d7 100644
--- a/economy.c
+++ b/economy.c
@@ -882,12 +882,11 @@ static void FindSubsidyPassengerRoute(FoundRoute *fr)
fr->distance = (uint)-1;
- fr->from = from = GetTown(RandomRange(GetTownArraySize()));
- if (!IsValidTown(from) || from->population < 400)
- return;
+ fr->from = from = GetRandomTown();
+ if (from == NULL || from->population < 400) return;
- fr->to = to = GetTown(RandomRange(GetTownArraySize()));
- if (from == to || !IsValidTown(to) || to->population < 400 || to->pct_pass_transported > 42)
+ fr->to = to = GetRandomTown();
+ if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
return;
fr->distance = DistanceManhattan(from->xy, to->xy);
@@ -901,8 +900,8 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
fr->distance = (uint)-1;
- fr->from = i = GetIndustry(RandomRange(GetIndustryArraySize()));
- if (!IsValidIndustry(i)) return;
+ fr->from = i = GetRandomIndustry();
+ if (i == NULL) return;
// Randomize cargo type
if (Random()&1 && i->produced_cargo[1] != CT_INVALID) {
@@ -925,19 +924,19 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
if (cargo == CT_GOODS || cargo == CT_FOOD) {
// The destination is a town
- Town *t = GetTown(RandomRange(GetTownArraySize()));
+ Town *t = GetRandomTown();
// Only want big towns
- if (!IsValidTown(t) || t->population < 900) return;
+ if (t == NULL || t->population < 900) return;
fr->distance = DistanceManhattan(i->xy, t->xy);
fr->to = t;
} else {
// The destination is an industry
- Industry *i2 = GetIndustry(RandomRange(GetIndustryArraySize()));
+ Industry *i2 = GetRandomIndustry();
// The industry must accept the cargo
- if (i == i2 || !IsValidIndustry(i2) ||
+ if (i == i2 || i == NULL ||
(cargo != i2->accepts_cargo[0] &&
cargo != i2->accepts_cargo[1] &&
cargo != i2->accepts_cargo[2]))
diff --git a/industry.h b/industry.h
index dcd489b95..2b642dece 100644
--- a/industry.h
+++ b/industry.h
@@ -107,6 +107,30 @@ static inline IndustryID GetIndustryArraySize(void)
return _total_industries + 1;
}
+/**
+ * Return a random valid town.
+ */
+static inline Industry *GetRandomIndustry(void)
+{
+ uint num = RandomRange(GetIndustryArraySize());
+ uint index = 0;
+
+ if (GetIndustryArraySize() == 0) return NULL;
+
+ while (num > 0) {
+ num--;
+
+ index++;
+ /* Make sure we have a valid industry */
+ while (GetIndustry(index) == NULL) {
+ index++;
+ if (index == GetIndustryArraySize()) index = 0;
+ }
+ }
+
+ return GetIndustry(index);
+}
+
#define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1 < GetIndustryPoolSize()) ? GetIndustry(i->index + 1) : NULL) if (IsValidIndustry(i))
#define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0)
diff --git a/industry_cmd.c b/industry_cmd.c
index b36f01ff5..215033e13 100644
--- a/industry_cmd.c
+++ b/industry_cmd.c
@@ -3,11 +3,11 @@
#include "stdafx.h"
#include "openttd.h"
#include "clear_map.h"
+#include "functions.h"
#include "industry_map.h"
#include "station_map.h"
#include "table/strings.h"
#include "table/sprites.h"
-#include "functions.h"
#include "map.h"
#include "tile.h"
#include "viewport.h"
@@ -1879,8 +1879,8 @@ void IndustryMonthlyLoop(void)
if (CHANCE16(3, 100)) {
MaybeNewIndustry(Random());
} else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) {
- i = GetIndustry(RandomRange(GetIndustryArraySize()));
- if (IsValidIndustry(i)) ChangeIndustryProduction(i);
+ i = GetRandomIndustry();
+ if (i != NULL) ChangeIndustryProduction(i);
}
_current_player = old_player;
diff --git a/smallmap_gui.c b/smallmap_gui.c
index 337ac4e05..452183c97 100644
--- a/smallmap_gui.c
+++ b/smallmap_gui.c
@@ -2,10 +2,10 @@
#include "stdafx.h"
#include "openttd.h"
+#include "functions.h"
#include "bridge_map.h"
#include "clear_map.h"
#include "industry_map.h"
-#include "functions.h"
#include "spritecache.h"
#include "station_map.h"
#include "table/strings.h"
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));