Changeset - r5299:1a5e005aa3e9
[Not reviewed]
master
0 2 0
rubidium - 17 years ago 2006-12-09 14:18:08
rubidium@openttd.org
(svn r7452) -Fix: GetRandom(Industry|Town) must return a valid industry/town and should not need to loop over the pool for a second time.
2 files changed with 26 insertions and 17 deletions:
town.h
14
10
0 comments (0 inline, 0 general)
industry.h
Show inline comments
 
@@ -90,6 +90,11 @@ static inline bool IsValidIndustry(const
 
	return industry->xy != 0;
 
}
 

	
 
static inline bool IsValidIndustryID(IndustryID index)
 
{
 
	return index < GetIndustryPoolSize() && IsValidIndustry(GetIndustry(index));
 
}
 

	
 
VARDEF int _total_industries;
 

	
 
static inline IndustryID GetMaxIndustryIndex(void)
 
@@ -108,23 +113,23 @@ static inline uint GetNumIndustries(void
 
}
 

	
 
/**
 
 * Return a random valid town.
 
 * Return a random valid industry.
 
 */
 
static inline Industry *GetRandomIndustry(void)
 
{
 
	uint num = RandomRange(GetNumIndustries());
 
	uint index = 0;
 
	int num = RandomRange(GetNumIndustries());
 
	IndustryID index = INVALID_INDUSTRY;
 

	
 
	if (GetNumIndustries() == 0) return NULL;
 

	
 
	while (num > 0) {
 
	while (num >= 0) {
 
		num--;
 

	
 
		index++;
 

	
 
		/* Make sure we have a valid industry */
 
		while (GetIndustry(index) == NULL) {
 
		while (!IsValidIndustryID(index)) {
 
			index++;
 
			if (index > GetMaxIndustryIndex()) index = 0;
 
			assert(index <= GetMaxIndustryIndex());
 
		}
 
	}
 

	
town.h
Show inline comments
 
@@ -6,6 +6,10 @@
 
#include "oldpool.h"
 
#include "player.h"
 

	
 
enum {
 
	INVALID_TOWN = 0xFFFF,
 
};
 

	
 
struct Town {
 
	TileIndex xy;
 

	
 
@@ -162,6 +166,11 @@ static inline bool IsValidTown(const Tow
 
	return town->xy != 0;
 
}
 

	
 
static inline bool IsValidTownID(TownID index)
 
{
 
	return index < GetTownPoolSize() && IsValidTown(GetTown(index));
 
}
 

	
 
VARDEF uint _total_towns;
 

	
 
static inline TownID GetMaxTownIndex(void)
 
@@ -184,28 +193,23 @@ static inline uint GetNumTowns(void)
 
 */
 
static inline Town *GetRandomTown(void)
 
{
 
	uint num = RandomRange(GetNumTowns());
 
	uint index = 0;
 
	int num = RandomRange(GetNumTowns());
 
	TownID index = INVALID_TOWN;
 

	
 
	while (num > 0) {
 
	while (num >= 0) {
 
		num--;
 

	
 
		index++;
 
		/* Make sure we have a valid industry */
 
		while (GetTown(index) == NULL) {
 
		while (!IsValidTownID(index)) {
 
			index++;
 
			if (index > GetMaxTownIndex()) index = 0;
 
			assert(index <= GetMaxTownIndex());
 
		}
 
	}
 

	
 
	return GetTown(index);
 
}
 

	
 
static inline bool IsValidTownID(uint index)
 
{
 
	return index < GetTownPoolSize() && IsValidTown(GetTown(index));
 
}
 

	
 
void DestroyTown(Town *t);
 

	
 
static inline void DeleteTown(Town *t)
0 comments (0 inline, 0 general)