changeset 14480:9f9f5e54a9d2 draft

(svn r19049) -Codechange: Deduplicate computing number of initial industries.
author frosch <frosch@openttd.org>
date Sat, 06 Feb 2010 20:19:59 +0000
parents 977f9bd7f701
children a6bae4e80e37
files src/industry_cmd.cpp
diffstat 1 files changed, 17 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -1788,33 +1788,20 @@
  * 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 @@
 	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 @@
 				/* 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 @@
 
 	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]);
 		}
 	}
 }