Changeset - r6493:d74339463d3a
[Not reviewed]
master
0 2 0
belugas - 17 years ago 2007-04-19 14:50:01
belugas@openttd.org
(svn r9676) -Codechange: introduce NUM_INDUSTRYTYPES instead of IT_END.
This makes the code clearer.
2 files changed with 9 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/industry.h
Show inline comments
 
/* $Id$ */
 

	
 
/** @file industry.h */
 

	
 
#ifndef INDUSTRY_H
 
#define INDUSTRY_H
 

	
 
#include "oldpool.h"
 
#include "helpers.hpp"
 

	
 
typedef byte IndustryGfx;
 
typedef uint8 IndustryType;
 

	
 
enum {
 
	INVALID_INDUSTRY = 0xFFFF,
 
	NUM_INDUSTRYTYPES = 37,
 
	INDUTILE_NOANIM = 0xFF,        ///< flag to mark industry tiles as having no animation
 
};
 

	
 
enum IndustryLifeType {
 
	INDUSTRYLIFE_NOT_CLOSABLE,     ///< Industry can never close
 
	INDUSTRYLIFE_PRODUCTION,       ///< Industry can close and change of production
 
	INDUSTRYLIFE_CLOSABLE,         ///< Industry can only close (no production change)
 
};
 

	
 
/* Procedures that can be run to check whether an industry may
 
 * build at location the given to the procedure */
 
enum CheckProc {
 
	CHECK_NOTHING    = 0,
 
	CHECK_FOREST     = 1,
 
	CHECK_REFINERY   = 2,
 
	CHECK_FARM       = 3,
 
	CHECK_PLANTATION = 4,
 
	CHECK_WATER      = 5,
 
	CHECK_LUMBERMILL = 6,
 
	CHECK_BUBBLEGEN  = 7,
 
	CHECK_OIL_RIG    = 8,
 
	CHECK_END,
 
};
 

	
src/industry_cmd.cpp
Show inline comments
 
@@ -46,69 +46,69 @@ static void IndustryPoolNewBlock(uint st
 

	
 
	/* 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)
 

	
 
/**
 
 * 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.
 
 * The same information can be accessed by looking at Industry->type
 
 * @param tile that is queried
 
 * @pre IsTileType(tile, MP_INDUSTRY)
 
 * @return general type for this industry, as defined in industry.h
 
 **/
 
IndustryType GetIndustryType(TileIndex tile)
 
{
 
	IndustryGfx this_type = GetIndustryGfx(tile);
 
	IndustryType iloop;
 

	
 
	assert(IsTileType(tile, MP_INDUSTRY));
 

	
 
	for (iloop = IT_COAL_MINE; iloop < IT_END; iloop += 1) {
 
	for (iloop = IT_COAL_MINE; iloop < NUM_INDUSTRYTYPES; iloop += 1) {
 
		if (IS_BYTE_INSIDE(this_type, industry_gfx_Solver[iloop].MinGfx,
 
				industry_gfx_Solver[iloop].MaxGfx + 1)) {
 
			return iloop;
 
		}
 
	}
 

	
 
	return IT_INVALID;  //we have not found equivalent, whatever the reason
 
}
 

	
 
/**
 
 * Accessor for array _industry_specs.
 
 * This will ensure at once : proper access and
 
 * not allowing modifications of it.
 
 * @param thistype of industry (which is the index in _industry_specs)
 
 * @pre thistype < IT_END
 
 * @pre thistype < NUM_INDUSTRYTYPES
 
 * @return a pointer to the corresponding industry spec
 
 **/
 
const IndustrySpec *GetIndustrySpec(IndustryType thistype)
 
{
 
	assert(thistype < IT_END);
 
	assert(thistype < NUM_INDUSTRYTYPES);
 
	return &_industry_specs[thistype];
 
}
 

	
 
/**
 
 * Accessor for array _industry_tile_specs.
 
 * This will ensure at once : proper access and
 
 * not allowing modifications of it.
 
 * @param gfx of industrytile (which is the index in _industry_specs)
 
 * @pre gfx < NUM_INDUSTRY_GFXES
 
 * @return a pointer to the corresponding industrytile spec
 
 **/
 
const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
 
{
 
	assert(gfx < NUM_INDUSTRY_GFXES);
 
	return &_industry_tile_specs[gfx];
 
}
 

	
 
void DestroyIndustry(Industry *i)
 
{
 
	BEGIN_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
 
		if (IsTileType(tile_cur, MP_INDUSTRY)) {
 
			if (GetIndustryIndex(tile_cur) == i->index) {
 
				DoClearSquare(tile_cur);
 
			}
 
@@ -1569,69 +1569,69 @@ static void PlaceInitialIndustry(Industr
 
		do {
 
			uint i;
 

	
 
			IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
 

	
 
			for (i = 0; i < 2000; i++) {
 
				if (CreateNewIndustry(RandomTile(), type) != NULL) break;
 
			}
 
		} while (--num);
 

	
 
		_current_player = old_player;
 
	}
 
}
 

	
 
/** This function will create ramdon industries during game creation.
 
 * It will scale the amount of industries by map size as well as difficulty level */
 
void GenerateIndustries()
 
{
 
	uint i = 0;
 
	uint8 chance;
 
	IndustryType it;
 
	const IndustrySpec *ind_spc;
 

	
 
	/* Find the total amount of industries */
 
	for (it = IT_COAL_MINE; it < IT_END; it++) {
 
	for (it = IT_COAL_MINE; it < NUM_INDUSTRYTYPES; it++) {
 
		int num;
 

	
 
		ind_spc = GetIndustrySpec(it);
 
		chance = ind_spc->appear_creation[_opt.landscape];
 

	
 
		if (chance > 0) {
 
			/* once the chance of appearance is determind, it have to be scaled by
 
			 * the difficulty level. The "chance" in question is more an index into
 
			 * the _numof_industry_table,in fact */
 
			num = _numof_industry_table[_opt.diff.number_industries][chance];
 

	
 
			/* These are always placed next to the coastline, so we scale by the perimeter instead. */
 
			num = (it == IT_OIL_REFINERY || it == IT_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
 
			i += num;
 
		}
 
	}
 

	
 
	SetGeneratingWorldProgress(GWP_INDUSTRY, i);
 

	
 
	for (it = IT_COAL_MINE; it < IT_END; it++) {
 
	for (it = IT_COAL_MINE; it < NUM_INDUSTRYTYPES; it++) {
 
		/* Once the number of industries has been determined, let's really create them.
 
		 * The test for chance allows us to try create industries that are available only
 
		 * for this landscape.
 
		 * @todo :  Do we really have to pass chance as un-scaled value, since we've already
 
		 *          processed that scaling above? No, don't think so.  Will find a way. */
 
		chance = GetIndustrySpec(it)->appear_creation[_opt.landscape];
 
		if (chance > 0) PlaceInitialIndustry(it, chance);
 
	};
 
}
 

	
 
/* Change industry production or do closure */
 
static void ExtChangeIndustryProduction(Industry *i)
 
{
 
	bool closeit = true;
 
	int j;
 
	const IndustrySpec *indspec = GetIndustrySpec(i->type);
 

	
 
	switch (indspec->life_type) {
 
		case INDUSTRYLIFE_NOT_CLOSABLE:
 
			return;
 

	
 
		case INDUSTRYLIFE_CLOSABLE:
 
			if ((byte)(_cur_year - i->last_prod_year) < 5 || !CHANCE16(1, 180))
 
				closeit = false;
 
@@ -1731,67 +1731,67 @@ static void UpdateIndustryStatistics(Ind
 
		InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
 

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

	
 
/** Simple helper that will collect data for the generation of industries */
 
struct ProbabilityHelper {
 
	uint16 prob;      ///< probability
 
	IndustryType ind; ///< industry id correcponding
 
};
 

	
 
/**
 
 * Try to create a random industry, during gameplay
 
 */
 
static void MaybeNewIndustry(void)
 
{
 
	Industry *ind;               //will receive the industry's creation pointer
 
	IndustryType rndtype, j;     // Loop controlers
 
	const IndustrySpec *ind_spc;
 
	uint num = 0;
 
	ProbabilityHelper cumulative_probs[IT_END]; // probability collector
 
	ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES]; // probability collector
 
	uint16 probability_max = 0;
 

	
 
	/* Generate a list of all possible industries that can be built. */
 
	for (j = 0; j < IT_END; j++) {
 
	for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
 
		byte chance = GetIndustrySpec(j)->appear_ingame[_opt.landscape];
 

	
 
		/* if appearing chance for this landscape is above 0, this industry can be chosen */
 
		if (chance != 0) {
 
			probability_max += chance;
 
			/* adds the result for this industry */
 
			cumulative_probs[num].ind = j;
 
			cumulative_probs[num++].prob = probability_max;
 
		}
 
	}
 

	
 
	/* Find a random type, with maximum being what has been evaluate above*/
 
	rndtype = RandomRange(probability_max);
 
	for (j = 0; j < IT_END; j++) {
 
	for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
 
		/* and choose the index of the industry that matches as close as possible this random type */
 
		if (cumulative_probs[j].prob >= rndtype) break;
 
	}
 

	
 
	ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
 
	/*  Check if it is allowed */
 
	if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
 
	if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
 

	
 
	/* try to create 2000 times this industry */
 
	num = 2000;
 
	for (;;) {
 
		ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
 
		if (ind != NULL) break;
 
		if (--num == 0) return;
 
	}
 

	
 
	SetDParam(0, ind_spc->name);
 
	SetDParam(1, ind->town->index);
 
	AddNewsItem(ind_spc->new_industry_text,
 
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, NT_OPENCLOSE, 0), ind->xy, 0);
 
}
 

	
 
static void ChangeIndustryProduction(Industry *i)
0 comments (0 inline, 0 general)