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

Method SetupTargetCount

src/industry_cmd.cpp:2632–2672  ·  view source on GitHub ↗

Decide how many industries of each type are needed. */

Source from the content-addressed store, hash-verified

2630
2631/** Decide how many industries of each type are needed. */
2632void IndustryBuildData::SetupTargetCount()
2633{
2634 bool changed = false;
2635 uint num_planned = 0; // Number of industries planned in the industry build data.
2636 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2637 changed |= this->builddata[it].GetIndustryTypeData(it);
2638 num_planned += this->builddata[it].target_count;
2639 }
2640 uint total_amount = this->wanted_inds >> 16; // Desired total number of industries.
2641 changed |= num_planned != total_amount;
2642 if (!changed) return; // All industries are still the same, no need to re-randomize.
2643
2644 /* Initialize the target counts. */
2645 uint force_build = 0; // Number of industries that should always be available.
2646 uint32_t total_prob = 0; // Sum of probabilities.
2647 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2648 IndustryTypeBuildData *ibd = this->builddata + it;
2649 force_build += ibd->min_number;
2650 ibd->target_count = ibd->min_number;
2651 total_prob += ibd->probability;
2652 }
2653
2654 if (total_prob == 0) return; // No buildable industries.
2655
2656 /* Subtract forced industries from the number of industries available for construction. */
2657 total_amount = (total_amount <= force_build) ? 0 : total_amount - force_build;
2658
2659 /* Assign number of industries that should be aimed for, by using the probability as a weight. */
2660 while (total_amount > 0) {
2661 uint32_t r = RandomRange(total_prob);
2662 IndustryType it = 0;
2663 while (r >= this->builddata[it].probability) {
2664 r -= this->builddata[it].probability;
2665 it++;
2666 assert(it < NUM_INDUSTRYTYPES);
2667 }
2668 assert(this->builddata[it].probability > 0);
2669 this->builddata[it].target_count++;
2670 total_amount--;
2671 }
2672}
2673
2674/**
2675 * Try to create a random industry, during gameplay

Callers 1

TryBuildNewIndustryMethod · 0.95

Calls 2

RandomRangeFunction · 0.85
GetIndustryTypeDataMethod · 0.80

Tested by

no test coverage detected