Changeset - r21220:5b27a41b688e
[Not reviewed]
master
0 2 0
rubidium - 10 years ago 2014-02-06 20:42:09
rubidium@openttd.org
(svn r26308) -Fix-ish: do not try to build more towns/industries than the pool can hold (MJP)
2 files changed with 2 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -1974,49 +1974,49 @@ static uint16 GetIndustryGamePlayProbabi
 
		return 0;
 
	}
 
	*min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
 
	return chance;
 
}
 

	
 
/**
 
 * Get wanted number of industries on the map.
 
 * @return Wanted number of industries at the map.
 
 */
 
static uint GetNumberOfIndustries()
 
{
 
	/* Number of industries on a 256x256 map. */
 
	static const uint16 numof_industry_table[] = {
 
		0,    // none
 
		0,    // minimal
 
		10,   // very low
 
		25,   // low
 
		55,   // normal
 
		80,   // high
 
	};
 

	
 
	assert(lengthof(numof_industry_table) == ID_END);
 
	uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
 
	return ScaleByMapSize(numof_industry_table[difficulty]);
 
	return min(IndustryPool::MAX_SIZE, ScaleByMapSize(numof_industry_table[difficulty]));
 
}
 

	
 
/**
 
 * Try to place the industry in the game.
 
 * Since there is no feedback why placement fails, there is no other option
 
 * than to try a few times before concluding it does not work.
 
 * @param type     Industry type of the desired industry.
 
 * @param try_hard Try very hard to find a place. (Used to place at least one industry per type.)
 
 * @return Pointer to created industry, or \c NULL if creation failed.
 
 */
 
static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
 
{
 
	uint tries = try_hard ? 10000u : 2000u;
 
	for (; tries > 0; tries--) {
 
		Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
 
		if (ind != NULL) return ind;
 
	}
 
	return NULL;
 
}
 

	
 
/**
 
 * Try to build a industry on the map.
 
 * @param type IndustryType of the desired industry
 
 * @param try_hard Try very hard to find a place. (Used to place at least one industry per type)
src/town_cmd.cpp
Show inline comments
 
@@ -1859,48 +1859,49 @@ static Town *CreateRandomTown(uint attem
 
		/* We already know that we can allocate a single town when
 
		 * entering this function. However, we create and delete
 
		 * a town which "resets" the allocation checks. As such we
 
		 * need to check again when assertions are enabled. */
 
		assert(Town::CanAllocateItem());
 
	} while (--attempts != 0);
 

	
 
	return NULL;
 
}
 

	
 
static const byte _num_initial_towns[4] = {5, 11, 23, 46};  // very low, low, normal, high
 

	
 
/**
 
 * This function will generate a certain amount of towns, with a certain layout
 
 * It can be called from the scenario editor (i.e.: generate Random Towns)
 
 * as well as from world creation.
 
 * @param layout which towns will be set to, when created
 
 * @return true if towns have been successfully created
 
 */
 
bool GenerateTowns(TownLayout layout)
 
{
 
	uint current_number = 0;
 
	uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.number_towns : 0;
 
	uint total = (difficulty == (uint)CUSTOM_TOWN_NUMBER_DIFFICULTY) ? _settings_game.game_creation.custom_town_number : ScaleByMapSize(_num_initial_towns[difficulty] + (Random() & 7));
 
	total = min(TownPool::MAX_SIZE, total);
 
	uint32 townnameparts;
 

	
 
	SetGeneratingWorldProgress(GWP_TOWN, total);
 

	
 
	/* First attempt will be made at creating the suggested number of towns.
 
	 * Note that this is really a suggested value, not a required one.
 
	 * We would not like the system to lock up just because the user wanted 100 cities on a 64*64 map, would we? */
 
	do {
 
		bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns));
 
		IncreaseGeneratingWorldProgress(GWP_TOWN);
 
		/* Get a unique name for the town. */
 
		if (!GenerateTownName(&townnameparts)) continue;
 
		/* try 20 times to create a random-sized town for the first loop. */
 
		if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout) != NULL) current_number++; // If creation was successful, raise a flag.
 
	} while (--total);
 

	
 
	if (current_number != 0) return true;
 

	
 
	/* If current_number is still zero at this point, it means that not a single town has been created.
 
	 * So give it a last try, but now more aggressive */
 
	if (GenerateTownName(&townnameparts) &&
 
			CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout) != NULL) {
 
		return true;
 
	}
0 comments (0 inline, 0 general)