(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()

This commit is contained in:
smatz 2009-06-26 15:08:54 +00:00
parent b668c24d46
commit ff33ed94ce
5 changed files with 60 additions and 52 deletions

View File

@ -135,6 +135,8 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
Industry(TileIndex tile = INVALID_TILE) : xy(tile) {}
~Industry();
static Industry *GetRandom();
};
struct IndustryTileTable {
@ -300,30 +302,6 @@ static inline void ResetIndustryCounts()
memset(&_industry_counts, 0, sizeof(_industry_counts));
}
/**
* Return a random valid industry.
*/
static inline Industry *GetRandomIndustry()
{
if (Industry::GetNumItems() == 0) return NULL;
int num = RandomRange((uint16)Industry::GetNumItems());
IndustryID index = INVALID_INDUSTRY;
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid industry */
while (!Industry::IsValidID(index)) {
index++;
assert(index < Industry::GetPoolSize());
}
}
return Industry::Get(index);
}
#define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
#define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)

View File

@ -171,6 +171,32 @@ Industry::~Industry()
Station::RecomputeIndustriesNearForAll();
}
/**
* Return a random valid industry.
* @return random industry, NULL if there are no industries
*/
/* static */ Industry *Industry::GetRandom()
{
if (Industry::GetNumItems() == 0) return NULL;
int num = RandomRange((uint16)Industry::GetNumItems());
size_t index = MAX_UVALUE(size_t);
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid industry */
while (!Industry::IsValidID(index)) {
index++;
assert(index < Industry::GetPoolSize());
}
}
return Industry::Get(index);
}
static void IndustryDrawSugarMine(const TileInfo *ti)
{
const DrawIndustryAnimationStruct *d;
@ -2289,7 +2315,7 @@ void IndustryDailyLoop()
if (Chance16(3, 100)) {
MaybeNewIndustry();
} else {
Industry *i = GetRandomIndustry();
Industry *i = Industry::GetRandom();
if (i != NULL) ChangeIndustryProduction(i, false);
}
}

View File

@ -126,10 +126,10 @@ static void FindSubsidyPassengerRoute(FoundRoute *fr)
fr->distance = UINT_MAX;
fr->from = from = GetRandomTown();
fr->from = from = Town::GetRandom();
if (from == NULL || from->population < 400) return;
fr->to = to = GetRandomTown();
fr->to = to = Town::GetRandom();
if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
return;
@ -144,7 +144,7 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
fr->distance = UINT_MAX;
fr->from = i = GetRandomIndustry();
fr->from = i = Industry::GetRandom();
if (i == NULL) return;
/* Randomize cargo type */
@ -170,7 +170,7 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
/* The destination is a town */
Town *t = GetRandomTown();
Town *t = Town::GetRandom();
/* Only want big towns */
if (t == NULL || t->population < 900) return;
@ -179,7 +179,7 @@ static void FindSubsidyCargoRoute(FoundRoute *fr)
fr->to = t;
} else {
/* The destination is an industry */
Industry *i2 = GetRandomIndustry();
Industry *i2 = Industry::GetRandom();
/* The industry must accept the cargo */
if (i2 == NULL || i == i2 ||

View File

@ -140,6 +140,8 @@ struct Town : TownPool::PoolItem<&_town_pool> {
{
return Town::Get(GetTownIndex(tile));
}
static Town *GetRandom();
};
uint32 GetWorldPopulation();
@ -179,28 +181,6 @@ bool CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
TileIndexDiff GetHouseNorthPart(HouseID &house);
/**
* Return a random valid town.
*/
static inline Town *GetRandomTown()
{
int num = RandomRange((uint16)Town::GetNumItems());
TownID index = INVALID_TOWN;
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid town */
while (!Town::IsValidID(index)) {
index++;
assert(index < Town::GetPoolSize());
}
}
return Town::Get(index);
}
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX, const Town *ignore = NULL);
#define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)

View File

@ -119,6 +119,30 @@ void Town::InitializeLayout(TownLayout layout)
this->layout = TileHash(TileX(this->xy), TileY(this->xy)) % (NUM_TLS - 1);
}
/**
* Return a random valid town.
* @return random town, NULL if there are no towns
*/
/* static */ Town *Town::GetRandom()
{
if (Town::GetNumItems() == 0) return NULL;
int num = RandomRange((uint16)Town::GetNumItems());
size_t index = MAX_UVALUE(size_t);
while (num >= 0) {
num--;
index++;
/* Make sure we have a valid town */
while (!Town::IsValidID(index)) {
index++;
assert(index < Town::GetPoolSize());
}
}
return Town::Get(index);
}
Money HouseSpec::GetRemovalCost() const
{
return (_price.remove_house * this->removal_cost) >> 8;