Changeset - r6839:3df64b5ddb52
[Not reviewed]
master
0 2 0
belugas - 17 years ago 2007-06-10 01:25:21
belugas@openttd.org
(svn r10078) -Codechange: Centralize all industry counts data and access
2 files changed with 15 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/industry.h
Show inline comments
 
@@ -205,47 +205,57 @@ static inline IndustryID GetMaxIndustryI
 
	 *  _really_ returns the highest index. Now it just returns
 
	 *  the next safe value we are sure about everything is below.
 
	 */
 
	return GetIndustryPoolSize() - 1;
 
}
 

	
 
extern int _total_industries;  // general counter
 
extern uint16 _industry_counts[NUM_INDUSTRYTYPES]; // Number of industries per type ingame
 

	
 
static inline uint GetNumIndustries()
 
{
 
	extern int _total_industries;  // general counter
 
	return _total_industries;
 
}
 

	
 
extern uint16 _industry_counts[NUM_INDUSTRYTYPES]; // Number of industries per type ingame
 

	
 
/** Increment the count of industries for this type
 
 * @param type IndustryType to increment
 
 * @pre type < INVALID_INDUSTRYTYPE */
 
static inline void IncIndustryTypeCount(IndustryType type)
 
{
 
	assert(type < INVALID_INDUSTRYTYPE);
 
	_industry_counts[type]++;
 
	_total_industries++;
 
}
 

	
 
/** Decrement the count of industries for this type
 
 * @param type IndustryType to decrement
 
 * @pre type < INVALID_INDUSTRYTYPE */
 
static inline void DecIndustryTypeCount(IndustryType type)
 
{
 
	assert(type < INVALID_INDUSTRYTYPE);
 
	_industry_counts[type]--;
 
	_total_industries--;
 
}
 

	
 
/** get the count of industries for this type
 
 * @param type IndustryType to query
 
 * @pre type < INVALID_INDUSTRYTYPE */
 
static inline uint8 GetIndustryTypeCount(IndustryType type)
 
{
 
	assert(type < INVALID_INDUSTRYTYPE);
 
	return min(_industry_counts[type], 0xFF); // callback expects only a byte, so cut it
 
}
 

	
 
/** Resets both the total_industries and the _industry_counts
 
 * This way, we centralize all counts activities */
 
static inline void ResetIndustryCounts()
 
{
 
	_total_industries = 0;
 
	memset(&_industry_counts, 0, sizeof(_industry_counts));
 
}
 

	
 
/**
 
 * Return a random valid industry.
 
 */
 
static inline Industry *GetRandomIndustry()
 
{
 
	int num = RandomRange(GetNumIndustries());
src/industry_cmd.cpp
Show inline comments
 
@@ -144,13 +144,12 @@ void DestroyIndustry(Industry *i)
 
				SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
 
			}
 
		} END_TILE_LOOP(tile_cur, 42, 42, i->xy - TileDiff(21, 21))
 
	}
 

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

	
 
	DeleteSubsidyWithIndustry(i->index);
 
	DeleteWindowById(WC_INDUSTRY_VIEW, i->index);
 
	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
 
}
 
@@ -1352,13 +1351,12 @@ static Industry *AllocateIndustry()
 
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;
 

	
 
	_total_industries++;
 
	i->xy = tile;
 
	i->width = i->height = 0;
 
	i->type = type;
 
	IncIndustryTypeCount(type);
 

	
 
	i->production_rate[0] = indspec->production_rate[0];
 
@@ -1854,14 +1852,13 @@ void IndustryMonthlyLoop()
 

	
 
void InitializeIndustries()
 
{
 
	CleanPool(&_Industry_pool);
 
	AddBlockToPool(&_Industry_pool);
 

	
 
	_total_industries = 0;
 
	memset(&_industry_counts, 0, sizeof(_industry_counts));
 
	ResetIndustryCounts();
 
	_industry_sort_dirty = true;
 
	_industry_sound_tile = 0;
 
}
 

	
 
extern const TileTypeProcs _tile_type_industry_procs = {
 
	DrawTile_Industry,           /* draw_tile_proc */
 
@@ -1923,25 +1920,23 @@ static void Save_INDY()
 
}
 

	
 
static void Load_INDY()
 
{
 
	int index;
 

	
 
	_total_industries = 0;
 
	ResetIndustryCounts();
 

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

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

	
 
		i = GetIndustry(index);
 
		SlObject(i, _industry_desc);
 
		IncIndustryTypeCount(i->type);
 

	
 
		_total_industries++;
 
	}
 
}
 

	
 
extern const ChunkHandler _industry_chunk_handlers[] = {
 
	{ 'INDY', Save_INDY, Load_INDY, CH_ARRAY | CH_LAST},
 
};
0 comments (0 inline, 0 general)