Changeset - r14470:e912f95f010b
[Not reviewed]
master
0 1 0
frosch - 14 years ago 2010-02-06 20:19:59
frosch@openttd.org
(svn r19049) -Codechange: Deduplicate computing number of initial industries.
1 file changed with 17 insertions and 34 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -1788,33 +1788,20 @@ static const byte _numof_industry_table[
 
 * of random industries during game creation
 
 * @param type IndustryType of the desired industry
 
 * @param amount of industries that need to be built */
 
static void PlaceInitialIndustry(IndustryType type, int amount)
 
static void PlaceInitialIndustry(IndustryType type, uint amount)
 
{
 
	/* We need to bypass the amount given in parameter if it exceeds the maximum dimension of the
 
	 * _numof_industry_table.  newgrf can specify a big amount */
 
	int num = (amount > NB_NUMOFINDUSTRY) ? amount : _numof_industry_table[_settings_game.difficulty.number_industries][amount];
 
	const IndustrySpec *ind_spc = GetIndustrySpec(type);
 

	
 
	/* These are always placed next to the coastline, so we scale by the perimeter instead. */
 
	num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
 
	CompanyID old_company = _current_company;
 
	_current_company = OWNER_NONE;
 

	
 
	if (_settings_game.difficulty.number_industries != 0) {
 
		CompanyID old_company = _current_company;
 
		_current_company = OWNER_NONE;
 
		assert(num > 0);
 

	
 
		do {
 
			uint i;
 
	for (; amount > 0; amount--) {
 
		IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
 

	
 
			IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
 
		for (uint i = 0; i < 2000; i++) {
 
			if (CreateNewIndustry(RandomTile(), type) != NULL) break;
 
		}
 
	}
 

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

	
 
		_current_company = old_company;
 
	}
 
	_current_company = old_company;
 
}
 

	
 
/** This function will create ramdon industries during game creation.
 
@@ -1826,6 +1813,9 @@ void GenerateIndustries()
 
	IndustryType it;
 
	const IndustrySpec *ind_spc;
 

	
 
	uint industry_counts[NUM_INDUSTRYTYPES];
 
	memset(industry_counts, 0, sizeof(industry_counts));
 

	
 
	/* Find the total amount of industries */
 
	if (_settings_game.difficulty.number_industries > 0) {
 
		for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
 
@@ -1841,10 +1831,11 @@ void GenerateIndustries()
 
				/* 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 */
 
				int num = (chance > NB_NUMOFINDUSTRY) ? chance : _numof_industry_table[_settings_game.difficulty.number_industries][chance];
 
				uint num = (chance > NB_NUMOFINDUSTRY) ? chance : _numof_industry_table[_settings_game.difficulty.number_industries][chance];
 

	
 
				/* These are always placed next to the coastline, so we scale by the perimeter instead. */
 
				num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
 
				industry_counts[it] = num;
 
				i += num;
 
			}
 
		}
 
@@ -1854,16 +1845,8 @@ void GenerateIndustries()
 

	
 
	if (_settings_game.difficulty.number_industries > 0) {
 
		for (it = 0; 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. */
 
			ind_spc = GetIndustrySpec(it);
 
			if (ind_spc->enabled && ind_spc->num_table > 0) {
 
				chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
 
				if (chance > 0) PlaceInitialIndustry(it, chance);
 
			}
 
			/* Once the number of industries has been determined, let's really create them. */
 
			if (industry_counts[it] > 0) PlaceInitialIndustry(it, industry_counts[it]);
 
		}
 
	}
 
}
0 comments (0 inline, 0 general)