* Check for bankruptcy of a company. Called every three months. * @param c Company to check. */
| 548 | * @param c Company to check. |
| 549 | */ |
| 550 | static void CompanyCheckBankrupt(Company *c) |
| 551 | { |
| 552 | /* If "Infinite money" setting is on, companies should not go bankrupt. */ |
| 553 | if (_settings_game.difficulty.infinite_money) return; |
| 554 | |
| 555 | /* If the company has money again, it does not go bankrupt */ |
| 556 | if (c->money - c->current_loan >= -c->GetMaxLoan()) { |
| 557 | int previous_months_of_bankruptcy = CeilDiv(c->months_of_bankruptcy, 3); |
| 558 | c->months_of_bankruptcy = 0; |
| 559 | c->bankrupt_asked = CompanyMask{}; |
| 560 | if (previous_months_of_bankruptcy != 0) CompanyAdminUpdate(c); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | c->months_of_bankruptcy++; |
| 565 | |
| 566 | switch (c->months_of_bankruptcy) { |
| 567 | /* All the boring cases (months) with a bad balance where no action is taken */ |
| 568 | case 0: |
| 569 | case 1: |
| 570 | case 2: |
| 571 | case 3: |
| 572 | |
| 573 | case 5: |
| 574 | case 6: |
| 575 | |
| 576 | case 8: |
| 577 | case 9: |
| 578 | break; |
| 579 | |
| 580 | /* Warn about bankruptcy after 3 months */ |
| 581 | case 4: { |
| 582 | auto cni = std::make_unique<CompanyNewsInformation>(STR_NEWS_COMPANY_IN_TROUBLE_TITLE, c); |
| 583 | EncodedString headline = GetEncodedString(STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION, cni->company_name); |
| 584 | AddCompanyNewsItem(std::move(headline), std::move(cni)); |
| 585 | AI::BroadcastNewEvent(new ScriptEventCompanyInTrouble(c->index)); |
| 586 | Game::NewEvent(new ScriptEventCompanyInTrouble(c->index)); |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | /* Offer company for sale after 6 months */ |
| 591 | case 7: { |
| 592 | /* Don't consider the loan */ |
| 593 | Money val = CalculateCompanyValue(c, false); |
| 594 | |
| 595 | c->bankrupt_value = val; |
| 596 | c->bankrupt_asked = CompanyMask{}.Set(c->index); // Don't ask the owner |
| 597 | c->bankrupt_timeout = 0; |
| 598 | |
| 599 | /* The company assets should always have some value */ |
| 600 | assert(c->bankrupt_value > 0); |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | /* Bankrupt company after 6 months (if the company has no value) or latest |
| 605 | * after 9 months (if it still had value after 6 months) */ |
| 606 | default: |
| 607 | case 10: { |
no test coverage detected