mirror of https://github.com/OpenTTD/OpenTTD
(svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
parent
3cdabcbbac
commit
ceb523c29f
|
@ -441,14 +441,12 @@ typedef struct FoundRoute {
|
||||||
|
|
||||||
static Town *AiFindRandomTown(void)
|
static Town *AiFindRandomTown(void)
|
||||||
{
|
{
|
||||||
Town *t = GetTown(RandomRange(GetTownArraySize()));
|
return GetRandomTown();
|
||||||
return IsValidTown(t) ? t : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Industry *AiFindRandomIndustry(void)
|
static Industry *AiFindRandomIndustry(void)
|
||||||
{
|
{
|
||||||
Industry *i = GetIndustry(RandomRange(GetIndustryArraySize()));
|
return GetRandomIndustry();
|
||||||
return IsValidIndustry(i) ? i : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
|
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "openttd.h"
|
#include "openttd.h"
|
||||||
|
#include "functions.h"
|
||||||
#include "industry_map.h"
|
#include "industry_map.h"
|
||||||
#include "station_map.h"
|
#include "station_map.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "functions.h"
|
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
#include "vehicle.h"
|
#include "vehicle.h"
|
||||||
|
|
21
economy.c
21
economy.c
|
@ -882,12 +882,11 @@ static void FindSubsidyPassengerRoute(FoundRoute *fr)
|
||||||
|
|
||||||
fr->distance = (uint)-1;
|
fr->distance = (uint)-1;
|
||||||
|
|
||||||
fr->from = from = GetTown(RandomRange(GetTownArraySize()));
|
fr->from = from = GetRandomTown();
|
||||||
if (!IsValidTown(from) || from->population < 400)
|
if (from == NULL || from->population < 400) return;
|
||||||
return;
|
|
||||||
|
|
||||||
fr->to = to = GetTown(RandomRange(GetTownArraySize()));
|
fr->to = to = GetRandomTown();
|
||||||
if (from == to || !IsValidTown(to) || to->population < 400 || to->pct_pass_transported > 42)
|
if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fr->distance = DistanceManhattan(from->xy, to->xy);
|
fr->distance = DistanceManhattan(from->xy, to->xy);
|
||||||
|
@ -901,8 +900,8 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
|
||||||
|
|
||||||
fr->distance = (uint)-1;
|
fr->distance = (uint)-1;
|
||||||
|
|
||||||
fr->from = i = GetIndustry(RandomRange(GetIndustryArraySize()));
|
fr->from = i = GetRandomIndustry();
|
||||||
if (!IsValidIndustry(i)) return;
|
if (i == NULL) return;
|
||||||
|
|
||||||
// Randomize cargo type
|
// Randomize cargo type
|
||||||
if (Random()&1 && i->produced_cargo[1] != CT_INVALID) {
|
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) {
|
if (cargo == CT_GOODS || cargo == CT_FOOD) {
|
||||||
// The destination is a town
|
// The destination is a town
|
||||||
Town *t = GetTown(RandomRange(GetTownArraySize()));
|
Town *t = GetRandomTown();
|
||||||
|
|
||||||
// Only want big towns
|
// 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->distance = DistanceManhattan(i->xy, t->xy);
|
||||||
fr->to = t;
|
fr->to = t;
|
||||||
} else {
|
} else {
|
||||||
// The destination is an industry
|
// The destination is an industry
|
||||||
Industry *i2 = GetIndustry(RandomRange(GetIndustryArraySize()));
|
Industry *i2 = GetRandomIndustry();
|
||||||
|
|
||||||
// The industry must accept the cargo
|
// 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[0] &&
|
||||||
cargo != i2->accepts_cargo[1] &&
|
cargo != i2->accepts_cargo[1] &&
|
||||||
cargo != i2->accepts_cargo[2]))
|
cargo != i2->accepts_cargo[2]))
|
||||||
|
|
24
industry.h
24
industry.h
|
@ -107,6 +107,30 @@ static inline IndustryID GetIndustryArraySize(void)
|
||||||
return _total_industries + 1;
|
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_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)
|
#define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0)
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "openttd.h"
|
#include "openttd.h"
|
||||||
#include "clear_map.h"
|
#include "clear_map.h"
|
||||||
|
#include "functions.h"
|
||||||
#include "industry_map.h"
|
#include "industry_map.h"
|
||||||
#include "station_map.h"
|
#include "station_map.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "table/sprites.h"
|
#include "table/sprites.h"
|
||||||
#include "functions.h"
|
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
#include "viewport.h"
|
#include "viewport.h"
|
||||||
|
@ -1879,8 +1879,8 @@ void IndustryMonthlyLoop(void)
|
||||||
if (CHANCE16(3, 100)) {
|
if (CHANCE16(3, 100)) {
|
||||||
MaybeNewIndustry(Random());
|
MaybeNewIndustry(Random());
|
||||||
} else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) {
|
} else if (!_patches.smooth_economy && GetIndustryArraySize() > 0) {
|
||||||
i = GetIndustry(RandomRange(GetIndustryArraySize()));
|
i = GetRandomIndustry();
|
||||||
if (IsValidIndustry(i)) ChangeIndustryProduction(i);
|
if (i != NULL) ChangeIndustryProduction(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
_current_player = old_player;
|
_current_player = old_player;
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "openttd.h"
|
#include "openttd.h"
|
||||||
|
#include "functions.h"
|
||||||
#include "bridge_map.h"
|
#include "bridge_map.h"
|
||||||
#include "clear_map.h"
|
#include "clear_map.h"
|
||||||
#include "industry_map.h"
|
#include "industry_map.h"
|
||||||
#include "functions.h"
|
|
||||||
#include "spritecache.h"
|
#include "spritecache.h"
|
||||||
#include "station_map.h"
|
#include "station_map.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
|
22
town.h
22
town.h
|
@ -191,6 +191,28 @@ static inline TownID GetTownArraySize(void)
|
||||||
return _total_towns + 1;
|
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)
|
static inline bool IsValidTownID(uint index)
|
||||||
{
|
{
|
||||||
return index < GetTownPoolSize() && IsValidTown(GetTown(index));
|
return index < GetTownPoolSize() && IsValidTown(GetTown(index));
|
||||||
|
|
Loading…
Reference in New Issue