(svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.

pull/155/head
truelight 18 years ago
parent cea8546a86
commit a21b2750bd

@ -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)

@ -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"

@ -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]))

@ -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)

@ -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;

@ -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"

@ -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));

Loading…
Cancel
Save