# HG changeset patch # User truelight # Date 1156281285 0 # Node ID bc52a48e2590b4605054a99c22d8750f149fb94b # Parent 4a9362d78854070a24aac589a205a0828f8ee650 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned. diff -r 4a9362d78854 -r bc52a48e2590 ai/default/default.c --- a/ai/default/default.c Tue Aug 22 20:56:08 2006 +0000 +++ b/ai/default/default.c Tue Aug 22 21:14:45 2006 +0000 @@ -441,14 +441,12 @@ 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 -r 4a9362d78854 -r bc52a48e2590 disaster_cmd.c --- a/disaster_cmd.c Tue Aug 22 20:56:08 2006 +0000 +++ b/disaster_cmd.c Tue Aug 22 21:14:45 2006 +0000 @@ -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 -r 4a9362d78854 -r bc52a48e2590 economy.c --- a/economy.c Tue Aug 22 20:56:08 2006 +0000 +++ b/economy.c Tue Aug 22 21:14:45 2006 +0000 @@ -882,12 +882,11 @@ 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 @@ 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 @@ 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 -r 4a9362d78854 -r bc52a48e2590 industry.h --- a/industry.h Tue Aug 22 20:56:08 2006 +0000 +++ b/industry.h Tue Aug 22 21:14:45 2006 +0000 @@ -107,6 +107,30 @@ 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 -r 4a9362d78854 -r bc52a48e2590 industry_cmd.c --- a/industry_cmd.c Tue Aug 22 20:56:08 2006 +0000 +++ b/industry_cmd.c Tue Aug 22 21:14:45 2006 +0000 @@ -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 @@ 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 -r 4a9362d78854 -r bc52a48e2590 smallmap_gui.c --- a/smallmap_gui.c Tue Aug 22 20:56:08 2006 +0000 +++ b/smallmap_gui.c Tue Aug 22 21:14:45 2006 +0000 @@ -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 -r 4a9362d78854 -r bc52a48e2590 town.h --- a/town.h Tue Aug 22 20:56:08 2006 +0000 +++ b/town.h Tue Aug 22 21:14:45 2006 +0000 @@ -191,6 +191,28 @@ 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));