Changeset - r12242:f11b2e8973ab
[Not reviewed]
master
0 5 0
smatz - 15 years ago 2009-06-26 15:08:54
smatz@openttd.org
(svn r16667) -Codechange: replace GetRandomTown() and GetRandomIndustry() by Town::GetRandom() and Industry::GetRandom()
5 files changed with 60 insertions and 52 deletions:
0 comments (0 inline, 0 general)
src/industry.h
Show inline comments
 
@@ -135,6 +135,8 @@ struct Industry : IndustryPool::PoolItem
 

	
 
	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)
 

	
src/industry_cmd.cpp
Show inline comments
 
@@ -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);
 
		}
 
	}
src/subsidy.cpp
Show inline comments
 
@@ -126,10 +126,10 @@ static void FindSubsidyPassengerRoute(Fo
 

	
 
	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(FoundR
 

	
 
	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(FoundR
 

	
 
	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(FoundR
 
		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 ||
src/town.h
Show inline comments
 
@@ -140,6 +140,8 @@ struct Town : TownPool::PoolItem<&_town_
 
	{
 
		return Town::Get(GetTownIndex(tile));
 
	}
 

	
 
	static Town *GetRandom();
 
};
 

	
 
uint32 GetWorldPopulation();
 
@@ -179,28 +181,6 @@ bool CheckforTownRating(DoCommandFlag fl
 

	
 
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)
src/town_cmd.cpp
Show inline comments
 
@@ -119,6 +119,30 @@ void Town::InitializeLayout(TownLayout l
 
	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;
0 comments (0 inline, 0 general)