Changeset - r7390:444ff4a56f72
[Not reviewed]
master
0 6 0
rubidium - 17 years ago 2007-08-02 23:21:52
rubidium@openttd.org
(svn r10759) -Codechange: make the industry struct use the pool item class as super class.
6 files changed with 43 insertions and 84 deletions:
0 comments (0 inline, 0 general)
src/ai/default/default.cpp
Show inline comments
 
@@ -459,13 +459,13 @@ static Town *AiFindRandomTown()
 
	return GetRandomTown();
 
}
 

	
 
static Industry *AiFindRandomIndustry()
 
{
 
	int num = RandomRange(GetMaxIndustryIndex());
 
	if (IsValidIndustry(GetIndustry(num))) return GetIndustry(num);
 
	if (IsValidIndustryID(num)) return GetIndustry(num);
 

	
 
	return NULL;
 
}
 

	
 
static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
 
{
src/industry.h
Show inline comments
 
@@ -83,16 +83,19 @@ enum IndustyBehaviour {
 
	INDUSTRYBEH_CAN_SUBSIDENCE        = 1 << 13, ///< can cause a subsidence (coal mine, shaft that collapses)
 
};
 

	
 

	
 
DECLARE_ENUM_AS_BIT_SET(IndustyBehaviour);
 

	
 
struct Industry;
 
DECLARE_OLD_POOL(Industry, Industry, 3, 8000)
 

	
 
/**
 
 * Defines the internal data of a functionnal industry
 
 */
 
struct Industry {
 
struct Industry : PoolItem<Industry, IndustryID, &_Industry_pool> {
 
	TileIndex xy;                       ///< coordinates of the primary tile the industry is built one
 
	byte width;
 
	byte height;
 
	const Town *town;                   ///< Nearest town
 
	uint16 produced_cargo_waiting[2];   ///< amount of cargo produced per cargo
 
	uint16 incoming_cargo_waiting[3];   ///< incoming cargo waiting to be processed
 
@@ -108,18 +111,21 @@ struct Industry {
 
	IndustryType type;                  ///< type of industry.
 
	OwnerByte owner;                    ///< owner of the industry.  Which SHOULD always be (imho) OWNER_NONE
 
	byte random_color;                  ///< randomized colour of the industry, for display purpose
 
	Year last_prod_year;                ///< last year of production
 
	byte was_cargo_delivered;           ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
 

	
 
	IndustryID index;                   ///< index of the industry in the pool of industries
 

	
 
	OwnerByte founder;                  ///< Founder of the industry
 
	Date construction_date;             ///< Date of the construction of the industry
 
	uint8 construction_type;            ///< Way the industry was constructed (@see IndustryConstructionType)
 
	Date last_cargo_accepted_at;        ///< Last day cargo was accepted by this industry
 

	
 
	Industry(TileIndex tile = 0) : xy(tile) {}
 
	~Industry();
 

	
 
	bool IsValid() const { return this->xy != 0; }
 
};
 

	
 
struct IndustryTileTable {
 
	TileIndexDiffC ti;
 
	IndustryGfx gfx;
 
};
 
@@ -213,32 +219,20 @@ void PlantRandomFarmField(const Industry
 
extern IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
 
extern IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
 

	
 
/* smallmap_gui.cpp */
 
void BuildIndustriesLegend();
 

	
 
DECLARE_OLD_POOL(Industry, Industry, 3, 8000)
 

	
 
/**
 
 * Check if an Industry really exists.
 
 * @param industry to check
 
 * @return true if position is a valid one
 
 */
 
static inline bool IsValidIndustry(const Industry *industry)
 
{
 
	return industry->xy != 0;
 
}
 

	
 
/**
 
 * Check if an Industry exists whithin the pool of industries
 
 * @param index of the desired industry
 
 * @return true if it is inside the pool
 
 */
 
static inline bool IsValidIndustryID(IndustryID index)
 
{
 
	return index < GetIndustryPoolSize() && IsValidIndustry(GetIndustry(index));
 
	return index < GetIndustryPoolSize() && GetIndustry(index)->IsValid();
 
}
 

	
 

	
 
static inline IndustryID GetMaxIndustryIndex()
 
{
 
	/* TODO - This isn't the real content of the function, but
 
@@ -315,21 +309,13 @@ static inline Industry *GetRandomIndustr
 
		}
 
	}
 

	
 
	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_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1U < GetIndustryPoolSize()) ? GetIndustry(i->index + 1U) : NULL) if (i->IsValid())
 
#define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0)
 

	
 
extern const Industry **_industry_sort;
 
extern bool _industry_sort_dirty;
 

	
 
static const uint8 IT_INVALID = 255;
src/industry_cmd.cpp
Show inline comments
 
@@ -32,12 +32,13 @@
 
#include "tree_map.h"
 
#include "cargotype.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_industries.h"
 
#include "newgrf_industrytiles.h"
 
#include "newgrf_callbacks.h"
 
#include "misc/autoptr.hpp"
 

	
 
void ShowIndustryViewWindow(int industry);
 
void BuildOilRig(TileIndex tile);
 

	
 
static byte _industry_sound_ctr;
 
static TileIndex _industry_sound_tile;
 
@@ -72,25 +73,13 @@ void ResetIndustries()
 
void ResetIndustryCreationProbility(IndustryType type)
 
{
 
	assert(type < INVALID_INDUSTRYTYPE);
 
	_industry_specs[type].appear_creation[_opt.landscape] = 0;
 
}
 

	
 
/**
 
 * Called if a new block is added to the industry-pool
 
 */
 
static void IndustryPoolNewBlock(uint start_item)
 
{
 
	Industry *i;
 

	
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 * TODO - This is just a temporary stage, this will be removed. */
 
	for (i = GetIndustry(start_item); i != NULL; i = (i->index + 1U < GetIndustryPoolSize()) ? GetIndustry(i->index + 1U) : NULL) i->index = start_item++;
 
}
 

	
 
DEFINE_OLD_POOL(Industry, Industry, IndustryPoolNewBlock, NULL)
 
DEFINE_OLD_POOL_GENERIC(Industry, Industry)
 

	
 
/**
 
 * Retrieve the type for this industry.  Although it is accessed by a tile,
 
 * it will return the general type of industry, and not the sprite index
 
 * as would do GetIndustryGfx.
 
 * @param tile that is queried
 
@@ -99,13 +88,13 @@ DEFINE_OLD_POOL(Industry, Industry, Indu
 
 **/
 
IndustryType GetIndustryType(TileIndex tile)
 
{
 
	assert(IsTileType(tile, MP_INDUSTRY));
 

	
 
	const Industry *ind = GetIndustryByTile(tile);
 
	return IsValidIndustry(ind) ? ind->type : (IndustryType)IT_INVALID;
 
	return ind->IsValid() ? ind->type : (IndustryType)IT_INVALID;
 
}
 

	
 
/**
 
 * Accessor for array _industry_specs.
 
 * This will ensure at once : proper access and
 
 * not allowing modifications of it.
 
@@ -136,41 +125,49 @@ const IndustryTileSpec *GetIndustryTileS
 
	if (full_check && its->grf_prop.override != INVALID_INDUSTRYTILE) {
 
		its = &_industry_tile_specs[its->grf_prop.override];
 
	}
 
	return its;
 
}
 

	
 
void DestroyIndustry(Industry *i)
 
Industry::~Industry()
 
{
 
	BEGIN_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
 
	/* Industry can also be destroyed when not fully initialized.
 
	 * This means that we do not have to clear tiles either. */
 
	if (this->width == 0) {
 
		this->xy = 0;
 
		return;
 
	}
 

	
 
	BEGIN_TILE_LOOP(tile_cur, this->width, this->height, this->xy);
 
		if (IsTileType(tile_cur, MP_INDUSTRY)) {
 
			if (GetIndustryIndex(tile_cur) == i->index) {
 
			if (GetIndustryIndex(tile_cur) == this->index) {
 
				DoClearSquare(tile_cur);
 
			}
 
		} else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
 
			DeleteOilRig(tile_cur);
 
		}
 
	END_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
 
	END_TILE_LOOP(tile_cur, this->width, this->height, this->xy);
 

	
 
	if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
 
	if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
 
		/* Remove the farmland and convert it to regular tiles over time. */
 
		BEGIN_TILE_LOOP(tile_cur, 42, 42, i->xy - TileDiffXY(21, 21)) {
 
		BEGIN_TILE_LOOP(tile_cur, 42, 42, this->xy - TileDiffXY(21, 21)) {
 
			tile_cur = TILE_MASK(tile_cur);
 
			if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
 
					GetIndustryIndexOfField(tile_cur) == i->index) {
 
					GetIndustryIndexOfField(tile_cur) == this->index) {
 
				SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
 
			}
 
		} END_TILE_LOOP(tile_cur, 42, 42, i->xy - TileDiff(21, 21))
 
		} END_TILE_LOOP(tile_cur, 42, 42, this->xy - TileDiff(21, 21))
 
	}
 

	
 
	_industry_sort_dirty = true;
 
	DecIndustryTypeCount(i->type);
 
	DecIndustryTypeCount(this->type);
 

	
 
	DeleteSubsidyWithIndustry(i->index);
 
	DeleteWindowById(WC_INDUSTRY_VIEW, i->index);
 
	DeleteSubsidyWithIndustry(this->index);
 
	DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
 
	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
 
	this->xy = 0;
 
}
 

	
 
static void IndustryDrawSugarMine(const TileInfo *ti)
 
{
 
	const DrawIndustryAnimationStruct *d;
 

	
 
@@ -390,13 +387,13 @@ static CommandCost ClearTile_Industry(Ti
 
			!_cheats.magic_bulldozer.value) ||
 
			(_current_player == OWNER_WATER && (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER))) {
 
		SetDParam(0, indspec->name);
 
		return_cmd_error(STR_4800_IN_THE_WAY);
 
	}
 

	
 
	if (flags & DC_EXEC) DeleteIndustry(i);
 
	if (flags & DC_EXEC) delete i;
 
	return CommandCost();
 
}
 

	
 
static void TransportIndustryGoods(TileIndex tile)
 
{
 
	Industry *i = GetIndustryByTile(tile);
 
@@ -1394,33 +1391,12 @@ static bool CheckIfTooCloseToIndustry(Ti
 
			return false;
 
		}
 
	}
 
	return true;
 
}
 

	
 
static Industry *AllocateIndustry()
 
{
 
	Industry *i;
 

	
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 * TODO - This is just a temporary stage, this will be removed. */
 
	for (i = GetIndustry(0); i != NULL; i = (i->index + 1U < GetIndustryPoolSize()) ? GetIndustry(i->index + 1U) : NULL) {
 
		IndustryID index = i->index;
 

	
 
		if (IsValidIndustry(i)) continue;
 

	
 
		memset(i, 0, sizeof(*i));
 
		i->index = index;
 

	
 
		return i;
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	return AddBlockToPool(&_Industry_pool) ? AllocateIndustry() : NULL;
 
}
 

	
 
static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, const Town *t, Owner owner)
 
{
 
	const IndustrySpec *indspec = GetIndustrySpec(type);
 
	uint32 r;
 
	int j;
 

	
 
@@ -1527,18 +1503,20 @@ static Industry *CreateNewIndustryHelper
 
	const Town *t = CheckMultipleIndustryInTown(tile, type);
 
	if (t == NULL) return NULL;
 

	
 
	if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
 
	if (!CheckSuitableIndustryPos(tile)) return NULL;
 

	
 
	Industry *i = AllocateIndustry();
 
	Industry *i = new Industry(tile);
 
	if (i == NULL) return NULL;
 
	AutoPtrT<Industry> i_auto_delete = i;
 

	
 
	if (flags & DC_EXEC) {
 
		if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
 
		DoCreateNewIndustry(i, tile, type, it, t, OWNER_NONE);
 
		i_auto_delete.Detach();
 
	}
 

	
 
	return i;
 
}
 

	
 
/** Build/Fund an industry
 
@@ -1805,13 +1783,13 @@ static void UpdateIndustryStatistics(Ind
 
	}
 

	
 
	if (refresh)
 
		InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
 

	
 
	if (i->prod_level == 0) {
 
		DeleteIndustry(i);
 
		delete i;
 
	} else if (_patches.smooth_economy) {
 
		ExtChangeIndustryProduction(i);
 
	}
 
}
 

	
 
/** Simple helper that will collect data for the generation of industries */
 
@@ -2090,18 +2068,13 @@ static void Load_INDY()
 
{
 
	int index;
 

	
 
	ResetIndustryCounts();
 

	
 
	while ((index = SlIterateArray()) != -1) {
 
		Industry *i;
 

	
 
		if (!AddBlockIfNeeded(&_Industry_pool, index))
 
			error("Industries: failed loading savegame: too many industries");
 

	
 
		i = GetIndustry(index);
 
		Industry *i = new (index) Industry();
 
		SlObject(i, _industry_desc);
 
		IncIndustryTypeCount(i->type);
 
	}
 
}
 

	
 
static void Load_IIDS()
src/oldloader.cpp
Show inline comments
 
@@ -712,13 +712,13 @@ static bool LoadOldIndustry(LoadgameStat
 
	if (!AddBlockIfNeeded(&_Industry_pool, num))
 
		error("Industries: failed loading savegame: too many industries");
 

	
 
	i = GetIndustry(num);
 
	if (!LoadChunk(ls, i, industry_chunk)) return false;
 

	
 
	if (IsValidIndustry(i)) {
 
	if (i->IsValid()) {
 
		i->town = GetTown(REMAP_TOWN_IDX(_old_town_index));
 
	}
 

	
 
	return true;
 
}
 

	
src/strings.cpp
Show inline comments
 
@@ -653,13 +653,13 @@ static char* FormatString(char* buff, co
 

	
 
			case SCC_INDUSTRY_NAME: { /* {INDUSTRY} */
 
				const Industry* i = GetIndustry(GetInt32(&argv));
 
				int64 args[2];
 

	
 
				/* industry not valid anymore? */
 
				if (!IsValidIndustry(i)) break;
 
				if (!i->IsValid()) break;
 

	
 
				/* First print the town name and the industry type name
 
				 * The string STR_INDUSTRY_PATTERN controls the formatting */
 
				args[0] = i->town->index;
 
				args[1] = GetIndustrySpec(i->type)->name;
 
				buff = FormatString(buff, GetStringPtr(STR_INDUSTRY_FORMAT), args, modifier >> 24, last);
src/town_cmd.cpp
Show inline comments
 
@@ -58,13 +58,13 @@ Town::~Town()
 
	 * and remove from list of sorted towns */
 
	DeleteWindowById(WC_TOWN_VIEW, this->index);
 
	_town_sort_dirty = true;
 
	_total_towns--;
 

	
 
	/* Delete all industries belonging to the town */
 
	FOR_ALL_INDUSTRIES(i) if (i->town == this) DeleteIndustry(i);
 
	FOR_ALL_INDUSTRIES(i) if (i->town == this) delete i;
 

	
 
	/* Go through all tiles and delete those belonging to the town */
 
	for (TileIndex tile = 0; tile < MapSize(); ++tile) {
 
		switch (GetTileType(tile)) {
 
			case MP_HOUSE:
 
				if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
0 comments (0 inline, 0 general)