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
 
@@ -81,59 +81,64 @@ typedef struct IndustrySpec {
 
const IndustrySpec *GetIndustrySpec(IndustryType thistype);
 

	
 
DECLARE_OLD_POOL(Industry, Industry, 3, 8000)
 

	
 
/**
 
 * Check if an Industry really exists.
 
 */
 
static inline bool IsValidIndustry(const Industry *industry)
 
{
 
	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)
 
{
 
	/* TODO - This isn't the real content of the function, but
 
	 *  with the new pool-system this will be replaced with one that
 
	 *  _really_ returns the highest index. Now it just returns
 
	 *  the next safe value we are sure about everything is below.
 
	 */
 
	return GetIndustryPoolSize() - 1;
 
}
 

	
 
static inline uint GetNumIndustries(void)
 
{
 
	return _total_industries;
 
}
 

	
 
/**
 
 * 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());
 
		}
 
	}
 

	
 
	return GetIndustry(index);
 
}
 

	
 
void DestroyIndustry(Industry *i);
 

	
 
static inline void DeleteIndustry(Industry *i)
 
{
 
	DestroyIndustry(i);
 
	i->xy = 0;
town.h
Show inline comments
 
/* $Id$ */
 

	
 
#ifndef TOWN_H
 
#define TOWN_H
 

	
 
#include "oldpool.h"
 
#include "player.h"
 

	
 
enum {
 
	INVALID_TOWN = 0xFFFF,
 
};
 

	
 
struct Town {
 
	TileIndex xy;
 

	
 
	// Current population of people and amount of houses.
 
	uint16 num_houses;
 
	uint32 population;
 

	
 
	// Town name
 
	uint16 townnametype;
 
	uint32 townnameparts;
 

	
 
	// NOSAVE: Location of name sign, UpdateTownVirtCoord updates this.
 
@@ -153,68 +157,68 @@ bool CheckforTownRating(uint32 flags, To
 
VARDEF const Town** _town_sort;
 

	
 
DECLARE_OLD_POOL(Town, Town, 3, 8000)
 

	
 
/**
 
 * Check if a Town really exists.
 
 */
 
static inline bool IsValidTown(const Town* town)
 
{
 
	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)
 
{
 
	/* TODO - This isn't the real content of the function, but
 
	 *  with the new pool-system this will be replaced with one that
 
	 *  _really_ returns the highest index. Now it just returns
 
	 *  the next safe value we are sure about everything is below.
 
	 */
 
	return GetTownPoolSize() - 1;
 
}
 

	
 
static inline uint GetNumTowns(void)
 
{
 
	return _total_towns;
 
}
 

	
 
/**
 
 * Return a random valid town.
 
 */
 
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)
 
{
 
	DestroyTown(t);
 
	t->xy = 0;
 
}
 

	
 
#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (IsValidTown(t))
 
#define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0)
 

	
 
VARDEF bool _town_sort_dirty;
0 comments (0 inline, 0 general)