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
 
@@ -45,131 +45,136 @@ struct Industry {
 
};
 

	
 
typedef struct IndustryTileTable {
 
	TileIndexDiffC ti;
 
	IndustryGfx gfx;
 
} IndustryTileTable;
 

	
 
typedef struct IndustrySpec {
 
	/** Tables with the 'layout' of different composition of GFXes */
 
	const IndustryTileTable *const *table;
 
	/** Number of elements in the table */
 
	byte num_table;
 
	/** Base cost multiplier*/
 
	byte cost_multiplier;
 
	/** Industries this industry cannot be close to */
 
	IndustryType conflicting[3];
 
	/** index to a procedure to check for conflicting circumstances */
 
	byte check_proc;
 

	
 
	CargoID produced_cargo[2];
 
	byte production_rate[2];
 
	/** The minimum amount of cargo transported to the stations; if the
 
	 * waiting cargo is less than this number, no cargo is moved to it*/
 
	byte minimal_cargo;
 
	CargoID accepts_cargo[3];
 

	
 
	IndustryLifeType life_type;  ///< This is also known as Industry production flag, in newgrf specs
 

	
 
	byte climate_availability;  ///< Bitmask, giving landscape enums as bit position
 

	
 
	StringID name;
 
	StringID closure_text;
 
	StringID production_up_text;
 
	StringID production_down_text;
 
} 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;
 
}
 

	
 
#define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1U < GetIndustryPoolSize()) ? GetIndustry(i->index + 1U) : NULL) if (IsValidIndustry(i))
 
#define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0)
 

	
 
VARDEF const Industry** _industry_sort;
 
VARDEF bool _industry_sort_dirty;
 

	
 

	
 
void DeleteIndustry(Industry *is);
 
void PlantRandomFarmField(const Industry *i);
 

	
 
enum {
 
	IT_COAL_MINE           =   0,
 
	IT_POWER_STATION       =   1,
 
	IT_SAWMILL             =   2,
 
	IT_FOREST              =   3,
 
	IT_OIL_REFINERY        =   4,
 
	IT_OIL_RIG             =   5,
 
	IT_FACTORY             =   6,
 
	IT_PRINTING_WORKS      =   7,
 
	IT_STEEL_MILL          =   8,
 
	IT_FARM                =   9,
 
	IT_COPPER_MINE         =  10,
 
	IT_OIL_WELL            =  11,
 
	IT_BANK_TEMP           =  12,
 
	IT_FOOD_PROCESS        =  13,
 
	IT_PAPER_MILL          =  14,
 
	IT_GOLD_MINE           =  15,
 
	IT_BANK_TROPIC_ARCTIC  =  16,
 
	IT_DIAMOND_MINE        =  17,
 
	IT_IRON_MINE           =  18,
 
	IT_FRUIT_PLANTATION    =  19,
 
	IT_RUBBER_PLANTATION   =  20,
 
	IT_WATER_SUPPLY        =  21,
 
	IT_WATER_TOWER         =  22,
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.
 
	ViewportSign sign;
 

	
 
	// Makes sure we don't build certain house types twice.
 
	// bit 0 = Building funds received
 
	// bit 1 = CHURCH
 
	// bit 2 = STADIUM
 
	byte flags12;
 

	
 
	// Which players have a statue?
 
	byte statues;
 

	
 
	// Player ratings as well as a mask that determines which players have a rating.
 
	byte have_ratings;
 
	uint8 unwanted[MAX_PLAYERS]; // how many months companies aren't wanted by towns (bribe)
 
	PlayerID exclusivity;        // which player has exslusivity
 
	uint8 exclusive_counter;     // months till the exclusivity expires
 
	int16 ratings[MAX_PLAYERS];
 

	
 
	// Maximum amount of passengers and mail that can be transported.
 
	uint32 max_pass;
 
	uint32 max_mail;
 
	uint32 new_max_pass;
 
	uint32 new_max_mail;
 
	uint32 act_pass;
 
	uint32 act_mail;
 
	uint32 new_act_pass;
 
	uint32 new_act_mail;
 

	
 
	// Amount of passengers that were transported.
 
	byte pct_pass_transported;
 
	byte pct_mail_transported;
 

	
 
	// Amount of food and paper that was transported. Actually a bit mask would be enough.
 
	uint16 act_food;
 
	uint16 act_water;
 
	uint16 new_act_food;
 
@@ -117,110 +121,110 @@ enum {
 

	
 
	RATING_INDUSTRY_DOWN_STEP = -1500,
 
	RATING_INDUSTRY_MINIMUM   = RATING_MINIMUM,
 

	
 
	RATING_ROAD_DOWN_STEP = -50,
 
	RATING_ROAD_MINIMUM   = -100,
 
	RATING_HOUSE_MINIMUM  = RATING_MINIMUM,
 

	
 
	RATING_BRIBE_UP_STEP = 200,
 
	RATING_BRIBE_MAXIMUM = 800,
 
	RATING_BRIBE_DOWN_TO = -50        // XXX SHOULD BE SOMETHING LOWER?
 
};
 

	
 
enum {
 
/* This is the base "normal" number of towns on the 8x8 map, when
 
 * one town should get grown per tick. The other numbers of towns
 
 * are then scaled based on that. */
 
	TOWN_GROWTH_FREQUENCY = 23,
 
/* Simple value that indicates the house has reached final stage of construction*/
 
	TOWN_HOUSE_COMPLETED  =  3,
 
};
 

	
 
/* This enum is used in conjonction with town->flags12.
 
 * IT simply states what bit is used for.
 
 * It is pretty unrealistic (IMHO) to only have one church/stadium
 
 * per town, NO MATTER the population of it.
 
 * And there are 5 more bits available on flags12...
 
 */
 
enum {
 
	TOWN_IS_FUNDED      = 0,   // Town has received some funds for
 
	TOWN_HAS_CHURCH     = 1,   // There can be only one church by town.
 
	TOWN_HAS_STADIUM    = 2    // There can be only one stadium by town.
 
};
 

	
 
bool CheckforTownRating(uint32 flags, Town *t, byte type);
 

	
 
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;
 
VARDEF byte _town_sort_order;
 

	
 
VARDEF Town *_cleared_town;
 
VARDEF int _cleared_town_rating;
 

	
 
#endif /* TOWN_H */
0 comments (0 inline, 0 general)