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

Method TryBuildNewIndustry

src/industry_cmd.cpp:2677–2740  ·  view source on GitHub ↗

* Try to create a random industry, during gameplay */

Source from the content-addressed store, hash-verified

2675 * Try to create a random industry, during gameplay
2676 */
2677void IndustryBuildData::TryBuildNewIndustry()
2678{
2679 this->SetupTargetCount();
2680
2681 int missing = 0; // Number of industries that need to be build.
2682 uint count = 0; // Number of industry types eligible for build.
2683 uint32_t total_prob = 0; // Sum of probabilities.
2684 IndustryType forced_build = NUM_INDUSTRYTYPES; // Industry type that should be forcibly build.
2685 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
2686 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
2687 missing += difference;
2688 if (this->builddata[it].wait_count > 0) continue; // This type may not be built now.
2689 if (difference > 0) {
2690 if (Industry::GetIndustryTypeCount(it) == 0 && this->builddata[it].min_number > 0) {
2691 /* An industry that should exist at least once, is not available. Force it, trying the most needed one first. */
2692 if (forced_build == NUM_INDUSTRYTYPES ||
2693 difference > this->builddata[forced_build].target_count - Industry::GetIndustryTypeCount(forced_build)) {
2694 forced_build = it;
2695 }
2696 }
2697 total_prob += difference;
2698 count++;
2699 }
2700 }
2701
2702 if (EconomyIsInRecession() || (forced_build == NUM_INDUSTRYTYPES && (missing <= 0 || total_prob == 0))) count = 0; // Skip creation of an industry.
2703
2704 if (count >= 1) {
2705 /* If not forced, pick a weighted random industry to build.
2706 * For the case that count == 1, there is no need to draw a random number. */
2707 IndustryType it;
2708 if (forced_build != NUM_INDUSTRYTYPES) {
2709 it = forced_build;
2710 } else {
2711 /* Non-forced, select an industry type to build (weighted random). */
2712 uint32_t r = 0; // Initialized to silence the compiler.
2713 if (count > 1) r = RandomRange(total_prob);
2714 for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
2715 if (this->builddata[it].wait_count > 0) continue; // Type may not be built now.
2716 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
2717 if (difference <= 0) continue; // Too many of this kind.
2718 if (count == 1) break;
2719 if (r < (uint)difference) break;
2720 r -= difference;
2721 }
2722 assert(it < NUM_INDUSTRYTYPES && this->builddata[it].target_count > Industry::GetIndustryTypeCount(it));
2723 }
2724
2725 /* Try to create the industry. */
2726 const Industry *ind = PlaceIndustry(it, IACT_RANDOMCREATION, false);
2727 if (ind == nullptr) {
2728 this->builddata[it].wait_count = this->builddata[it].max_wait + 1; // Compensate for decrementing below.
2729 this->builddata[it].max_wait = std::min(1000, this->builddata[it].max_wait + 2);
2730 } else {
2731 AdvertiseIndustryOpening(ind);
2732 this->builddata[it].max_wait = std::max(this->builddata[it].max_wait / 2, 1); // Reduce waiting time of the industry type.
2733 }
2734 }

Callers 1

industry_cmd.cppFile · 0.80

Calls 5

SetupTargetCountMethod · 0.95
EconomyIsInRecessionFunction · 0.85
RandomRangeFunction · 0.85
PlaceIndustryFunction · 0.85
AdvertiseIndustryOpeningFunction · 0.85

Tested by

no test coverage detected