mirror of https://github.com/OpenTTD/OpenTTD
Change: Scale land industries separately from water industries.
parent
82fa5e7164
commit
9f9a074d27
|
@ -75,7 +75,8 @@ enum GenWorldProgress : uint8_t {
|
||||||
GWP_RIVER, ///< Create the rivers
|
GWP_RIVER, ///< Create the rivers
|
||||||
GWP_ROUGH_ROCKY, ///< Make rough and rocky areas
|
GWP_ROUGH_ROCKY, ///< Make rough and rocky areas
|
||||||
GWP_TOWN, ///< Generate towns
|
GWP_TOWN, ///< Generate towns
|
||||||
GWP_INDUSTRY, ///< Generate industries
|
GWP_LAND_INDUSTRY, ///< Generate industries
|
||||||
|
GWP_WATER_INDUSTRY, ///< Generate industries
|
||||||
GWP_OBJECT, ///< Generate objects (radio tower, light houses)
|
GWP_OBJECT, ///< Generate objects (radio tower, light houses)
|
||||||
GWP_TREE, ///< Generate trees
|
GWP_TREE, ///< Generate trees
|
||||||
GWP_GAME_INIT, ///< Initialize the game
|
GWP_GAME_INIT, ///< Initialize the game
|
||||||
|
|
|
@ -1348,7 +1348,8 @@ static const StringID _generation_class_table[] = {
|
||||||
STR_GENERATION_RIVER_GENERATION,
|
STR_GENERATION_RIVER_GENERATION,
|
||||||
STR_GENERATION_CLEARING_TILES,
|
STR_GENERATION_CLEARING_TILES,
|
||||||
STR_GENERATION_TOWN_GENERATION,
|
STR_GENERATION_TOWN_GENERATION,
|
||||||
STR_GENERATION_INDUSTRY_GENERATION,
|
STR_GENERATION_LAND_INDUSTRY_GENERATION,
|
||||||
|
STR_GENERATION_WATER_INDUSTRY_GENERATION,
|
||||||
STR_GENERATION_OBJECT_GENERATION,
|
STR_GENERATION_OBJECT_GENERATION,
|
||||||
STR_GENERATION_TREE_GENERATION,
|
STR_GENERATION_TREE_GENERATION,
|
||||||
STR_GENERATION_SETTINGUP_GAME,
|
STR_GENERATION_SETTINGUP_GAME,
|
||||||
|
@ -1458,7 +1459,7 @@ void ShowGenerateWorldProgress()
|
||||||
|
|
||||||
static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
|
static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
|
||||||
{
|
{
|
||||||
static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 95, 99, 100 };
|
static const int percent_table[] = {0, 5, 14, 17, 20, 40, 55, 60, 65, 80, 85, 95, 99, 100 };
|
||||||
static_assert(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
|
static_assert(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
|
||||||
assert(cls < GWP_CLASS_COUNT);
|
assert(cls < GWP_CLASS_COUNT);
|
||||||
|
|
||||||
|
|
|
@ -2289,9 +2289,11 @@ static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAv
|
||||||
* @param[out] force_at_least_one Returns whether at least one instance should be forced on map creation.
|
* @param[out] force_at_least_one Returns whether at least one instance should be forced on map creation.
|
||||||
* @return Relative probability for the industry to appear.
|
* @return Relative probability for the industry to appear.
|
||||||
*/
|
*/
|
||||||
static uint32_t GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
|
static uint32_t GetScaledIndustryGenerationProbability(IndustryType it, std::optional<bool> water, bool *force_at_least_one)
|
||||||
{
|
{
|
||||||
const IndustrySpec *ind_spc = GetIndustrySpec(it);
|
const IndustrySpec *ind_spc = GetIndustrySpec(it);
|
||||||
|
if (water.has_value() && ind_spc->behaviour.Test(IndustryBehaviour::BuiltOnWater) != *water) return 0;
|
||||||
|
|
||||||
uint32_t chance = ind_spc->appear_creation[to_underlying(_settings_game.game_creation.landscape)];
|
uint32_t chance = ind_spc->appear_creation[to_underlying(_settings_game.game_creation.landscape)];
|
||||||
if (!ind_spc->enabled || ind_spc->layouts.empty() ||
|
if (!ind_spc->enabled || ind_spc->layouts.empty() ||
|
||||||
(_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) ||
|
(_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) ||
|
||||||
|
@ -2357,7 +2359,7 @@ static uint GetNumberOfIndustries()
|
||||||
|
|
||||||
if (difficulty == ID_CUSTOM) return std::min<uint>(IndustryPool::MAX_SIZE, _settings_game.game_creation.custom_industry_number);
|
if (difficulty == ID_CUSTOM) return std::min<uint>(IndustryPool::MAX_SIZE, _settings_game.game_creation.custom_industry_number);
|
||||||
|
|
||||||
return std::min<uint>(IndustryPool::MAX_SIZE, Map::ScaleByLandProportion(Map::ScaleBySize(numof_industry_table[difficulty])));
|
return std::min<uint>(IndustryPool::MAX_SIZE, Map::ScaleBySize(numof_industry_table[difficulty]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2383,11 +2385,11 @@ static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType c
|
||||||
* @param type IndustryType of the desired industry
|
* @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)
|
* @param try_hard Try very hard to find a place. (Used to place at least one industry per type)
|
||||||
*/
|
*/
|
||||||
static void PlaceInitialIndustry(IndustryType type, bool try_hard)
|
static void PlaceInitialIndustry(IndustryType type, bool water, bool try_hard)
|
||||||
{
|
{
|
||||||
Backup<CompanyID> cur_company(_current_company, OWNER_NONE);
|
Backup<CompanyID> cur_company(_current_company, OWNER_NONE);
|
||||||
|
|
||||||
IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
|
IncreaseGeneratingWorldProgress(water ? GWP_WATER_INDUSTRY : GWP_LAND_INDUSTRY);
|
||||||
PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
|
PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
|
||||||
|
|
||||||
cur_company.Restore();
|
cur_company.Restore();
|
||||||
|
@ -2441,6 +2443,31 @@ void IndustryBuildData::EconomyMonthlyLoop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct IndustryGenerationProbabilities {
|
||||||
|
std::array<uint32_t, NUM_INDUSTRYTYPES> probs{};
|
||||||
|
std::array<bool, NUM_INDUSTRYTYPES> force_one{};
|
||||||
|
uint64_t total = 0;
|
||||||
|
uint num_forced = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get scaled industry generation probabilities.
|
||||||
|
* @param water Whether to get land or water industry probabilities.
|
||||||
|
* @returns Probability information.
|
||||||
|
*/
|
||||||
|
static IndustryGenerationProbabilities GetScaledProbabilities(bool water)
|
||||||
|
{
|
||||||
|
IndustryGenerationProbabilities p{};
|
||||||
|
|
||||||
|
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
|
||||||
|
p.probs[it] = GetScaledIndustryGenerationProbability(it, water, &p.force_one[it]);
|
||||||
|
p.total += p.probs[it];;
|
||||||
|
if (p.force_one[it]) p.num_forced++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function will create random industries during game creation.
|
* This function will create random industries during game creation.
|
||||||
* It will scale the amount of industries by mapsize and difficulty level.
|
* It will scale the amount of industries by mapsize and difficulty level.
|
||||||
|
@ -2449,46 +2476,54 @@ void GenerateIndustries()
|
||||||
{
|
{
|
||||||
if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
|
if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
|
||||||
|
|
||||||
uint32_t industry_probs[NUM_INDUSTRYTYPES];
|
/* Get the probabilities for all industries. This is done first as we need the total of
|
||||||
bool force_at_least_one[NUM_INDUSTRYTYPES];
|
* both land and water for scaling later. */
|
||||||
uint32_t total_prob = 0;
|
IndustryGenerationProbabilities lprob = GetScaledProbabilities(false);
|
||||||
uint num_forced = 0;
|
IndustryGenerationProbabilities wprob = GetScaledProbabilities(true);
|
||||||
|
|
||||||
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
|
/* Run generation twice, for land and water industries in turn. */
|
||||||
industry_probs[it] = GetScaledIndustryGenerationProbability(it, force_at_least_one + it);
|
for (bool water = false;; water = true) {
|
||||||
total_prob += industry_probs[it];
|
auto &p = water ? wprob : lprob;
|
||||||
if (force_at_least_one[it]) num_forced++;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint total_amount = GetNumberOfIndustries();
|
/* Total number of industries scaled by land/water proportion. */
|
||||||
if (total_prob == 0 || total_amount < num_forced) {
|
uint total_amount = p.total * GetNumberOfIndustries() / (lprob.total + wprob.total);
|
||||||
|
|
||||||
|
/* Scale land-based industries to the land proportion. */
|
||||||
|
if (!water) total_amount = Map::ScaleByLandProportion(total_amount);
|
||||||
|
|
||||||
|
/* Ensure that forced industries are generated even if the scaled amounts are too low. */
|
||||||
|
if (p.total == 0 || total_amount < p.num_forced) {
|
||||||
/* Only place the forced ones */
|
/* Only place the forced ones */
|
||||||
total_amount = num_forced;
|
total_amount = p.num_forced;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
|
SetGeneratingWorldProgress(water ? GWP_WATER_INDUSTRY : GWP_LAND_INDUSTRY, total_amount);
|
||||||
|
|
||||||
/* Try to build one industry per type independent of any probabilities */
|
/* Try to build one industry per type independent of any probabilities */
|
||||||
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
|
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
|
||||||
if (force_at_least_one[it]) {
|
if (p.force_one[it]) {
|
||||||
assert(total_amount > 0);
|
assert(total_amount > 0);
|
||||||
total_amount--;
|
total_amount--;
|
||||||
PlaceInitialIndustry(it, true);
|
PlaceInitialIndustry(it, water, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add the remaining industries according to their probabilities */
|
/* Add the remaining industries according to their probabilities */
|
||||||
for (uint i = 0; i < total_amount; i++) {
|
for (uint i = 0; i < total_amount; i++) {
|
||||||
uint32_t r = RandomRange(total_prob);
|
uint32_t r = RandomRange(p.total);
|
||||||
IndustryType it = 0;
|
IndustryType it = 0;
|
||||||
while (r >= industry_probs[it]) {
|
while (r >= p.probs[it]) {
|
||||||
r -= industry_probs[it];
|
r -= p.probs[it];
|
||||||
it++;
|
it++;
|
||||||
assert(it < NUM_INDUSTRYTYPES);
|
assert(it < NUM_INDUSTRYTYPES);
|
||||||
}
|
}
|
||||||
assert(industry_probs[it] > 0);
|
assert(p.probs[it] > 0);
|
||||||
PlaceInitialIndustry(it, false);
|
PlaceInitialIndustry(it, water, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (water) break;
|
||||||
|
}
|
||||||
|
|
||||||
_industry_builder.Reset();
|
_industry_builder.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3092,7 +3127,7 @@ void CheckIndustries()
|
||||||
if (Industry::GetIndustryTypeCount(it) > 0) continue; // Types of existing industries can be skipped.
|
if (Industry::GetIndustryTypeCount(it) > 0) continue; // Types of existing industries can be skipped.
|
||||||
|
|
||||||
bool force_at_least_one;
|
bool force_at_least_one;
|
||||||
uint32_t chance = GetScaledIndustryGenerationProbability(it, &force_at_least_one);
|
uint32_t chance = GetScaledIndustryGenerationProbability(it, std::nullopt, &force_at_least_one);
|
||||||
if (chance == 0 || !force_at_least_one) continue; // Types that are not available can be skipped.
|
if (chance == 0 || !force_at_least_one) continue; // Types that are not available can be skipped.
|
||||||
|
|
||||||
const IndustrySpec *is = GetIndustrySpec(it);
|
const IndustrySpec *is = GetIndustrySpec(it);
|
||||||
|
|
|
@ -3451,7 +3451,8 @@ STR_GENERATION_LANDSCAPE_GENERATION :{BLACK}Landscap
|
||||||
STR_GENERATION_RIVER_GENERATION :{BLACK}River generation
|
STR_GENERATION_RIVER_GENERATION :{BLACK}River generation
|
||||||
STR_GENERATION_CLEARING_TILES :{BLACK}Rough and rocky area generation
|
STR_GENERATION_CLEARING_TILES :{BLACK}Rough and rocky area generation
|
||||||
STR_GENERATION_TOWN_GENERATION :{BLACK}Town generation
|
STR_GENERATION_TOWN_GENERATION :{BLACK}Town generation
|
||||||
STR_GENERATION_INDUSTRY_GENERATION :{BLACK}Industry generation
|
STR_GENERATION_LAND_INDUSTRY_GENERATION :{BLACK}Land industry generation
|
||||||
|
STR_GENERATION_WATER_INDUSTRY_GENERATION :{BLACK}Water industry generation
|
||||||
STR_GENERATION_OBJECT_GENERATION :{BLACK}Object generation
|
STR_GENERATION_OBJECT_GENERATION :{BLACK}Object generation
|
||||||
STR_GENERATION_TREE_GENERATION :{BLACK}Tree generation
|
STR_GENERATION_TREE_GENERATION :{BLACK}Tree generation
|
||||||
STR_GENERATION_SETTINGUP_GAME :{BLACK}Setting up game
|
STR_GENERATION_SETTINGUP_GAME :{BLACK}Setting up game
|
||||||
|
|
Loading…
Reference in New Issue