(svn r19049) -Codechange: Deduplicate computing number of initial industries.

This commit is contained in:
frosch 2010-02-06 20:19:59 +00:00
parent ec436a7615
commit 0ab6f60c62
1 changed files with 17 additions and 34 deletions

View File

@ -1788,33 +1788,20 @@ static const byte _numof_industry_table[NB_DIFFICULTY_LEVEL][NB_NUMOFINDUSTRY] =
* of random industries during game creation * of random industries during game creation
* @param type IndustryType of the desired industry * @param type IndustryType of the desired industry
* @param amount of industries that need to be built */ * @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 CompanyID old_company = _current_company;
* _numof_industry_table. newgrf can specify a big amount */ _current_company = OWNER_NONE;
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. */ for (; amount > 0; amount--) {
num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num); IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
if (_settings_game.difficulty.number_industries != 0) { for (uint i = 0; i < 2000; i++) {
CompanyID old_company = _current_company; if (CreateNewIndustry(RandomTile(), type) != NULL) break;
_current_company = OWNER_NONE; }
assert(num > 0);
do {
uint i;
IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
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. /** This function will create ramdon industries during game creation.
@ -1826,6 +1813,9 @@ void GenerateIndustries()
IndustryType it; IndustryType it;
const IndustrySpec *ind_spc; const IndustrySpec *ind_spc;
uint industry_counts[NUM_INDUSTRYTYPES];
memset(industry_counts, 0, sizeof(industry_counts));
/* Find the total amount of industries */ /* Find the total amount of industries */
if (_settings_game.difficulty.number_industries > 0) { if (_settings_game.difficulty.number_industries > 0) {
for (it = 0; it < NUM_INDUSTRYTYPES; it++) { 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 /* 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 difficulty level. The "chance" in question is more an index into
* the _numof_industry_table,in fact */ * 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. */ /* 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); num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
industry_counts[it] = num;
i += num; i += num;
} }
} }
@ -1854,16 +1845,8 @@ void GenerateIndustries()
if (_settings_game.difficulty.number_industries > 0) { if (_settings_game.difficulty.number_industries > 0) {
for (it = 0; it < NUM_INDUSTRYTYPES; it++) { for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
/* Once the number of industries has been determined, let's really create them. /* 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 if (industry_counts[it] > 0) PlaceInitialIndustry(it, industry_counts[it]);
* 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);
}
} }
} }
} }