summaryrefslogtreecommitdiff
path: root/industry.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 /industry.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 'industry.h')
-rw-r--r--industry.h24
1 files changed, 24 insertions, 0 deletions
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)