MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GenerateIndustries

Function GenerateIndustries

src/industry_cmd.cpp:2488–2542  ·  view source on GitHub ↗

* This function will create random industries during game creation. * It will scale the amount of industries by mapsize and difficulty level. */

Source from the content-addressed store, hash-verified

2486 * It will scale the amount of industries by mapsize and difficulty level.
2487 */
2488void GenerateIndustries()
2489{
2490 if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
2491
2492 /* Get the probabilities for all industries. This is done first as we need the total of
2493 * both land and water for scaling later. */
2494 IndustryGenerationProbabilities lprob = GetScaledProbabilities(false);
2495 IndustryGenerationProbabilities wprob = GetScaledProbabilities(true);
2496
2497 /* Run generation twice, for land and water industries in turn. */
2498 for (bool water = false;; water = true) {
2499 auto &p = water ? wprob : lprob;
2500
2501 /* Total number of industries scaled by land/water proportion. */
2502 uint total_amount = 0;
2503 if (lprob.total + wprob.total > 0) total_amount = p.total * GetNumberOfIndustries() / (lprob.total + wprob.total);
2504
2505 /* Scale land-based industries to the land proportion, unless the player has set a custom industry count. */
2506 if (!water && _settings_game.difficulty.industry_density != ID_CUSTOM) total_amount = Map::ScaleByLandProportion(total_amount);
2507
2508 /* Ensure that forced industries are generated even if the scaled amounts are too low. */
2509 if (p.total == 0 || total_amount < p.num_forced) {
2510 /* Only place the forced ones */
2511 total_amount = p.num_forced;
2512 }
2513
2514 SetGeneratingWorldProgress(water ? GWP_WATER_INDUSTRY : GWP_LAND_INDUSTRY, total_amount);
2515
2516 /* Try to build one industry per type independent of any probabilities */
2517 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2518 if (p.force_one[it]) {
2519 assert(total_amount > 0);
2520 total_amount--;
2521 PlaceInitialIndustry(it, water, true);
2522 }
2523 }
2524
2525 /* Add the remaining industries according to their probabilities */
2526 for (uint i = 0; i < total_amount; i++) {
2527 uint32_t r = RandomRange(p.total);
2528 IndustryType it = 0;
2529 while (r >= p.probs[it]) {
2530 r -= p.probs[it];
2531 it++;
2532 assert(it < NUM_INDUSTRYTYPES);
2533 }
2534 assert(p.probs[it] > 0);
2535 PlaceInitialIndustry(it, water, false);
2536 }
2537
2538 if (water) break;
2539 }
2540
2541 _industry_builder.Reset();
2542}
2543
2544template <>
2545Industry::ProducedHistory SumHistory(std::span<const Industry::ProducedHistory> history)

Callers 2

_GenerateWorldFunction · 0.85

Calls 6

GetScaledProbabilitiesFunction · 0.85
GetNumberOfIndustriesFunction · 0.85
PlaceInitialIndustryFunction · 0.85
RandomRangeFunction · 0.85
ResetMethod · 0.45

Tested by

no test coverage detected