Skip to content

Instantly share code, notes, and snippets.

@rubidium42
Created May 28, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubidium42/6d4ce350095f2fcb4435feb3ac4e581f to your computer and use it in GitHub Desktop.
Save rubidium42/6d4ce350095f2fcb4435feb3ac4e581f to your computer and use it in GitHub Desktop.
Idea to prevent bankrupting yourself with the last share
diff --git a/src/economy.cpp b/src/economy.cpp
index 35fcf57a5..089d8d6d0 100644
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1969,7 +1969,7 @@ void CompaniesMonthlyLoop()
HandleEconomyFluctuations();
}
-static void DoAcquireCompany(Company *c)
+static void DoAcquireCompany(Company *c, bool take_over)
{
CompanyID ci = c->index;
@@ -1977,7 +1977,7 @@ static void DoAcquireCompany(Company *c)
cni->FillData(c, Company::Get(_current_company));
SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE);
- SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
+ SetDParam(1, take_over ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
SetDParamStr(2, cni->company_name);
SetDParamStr(3, cni->other_company_name);
SetDParam(4, c->bankrupt_value);
@@ -1987,14 +1987,6 @@ static void DoAcquireCompany(Company *c)
ChangeOwnershipOfCompanyItems(ci, _current_company);
- if (c->bankrupt_value == 0) {
- Company *owner = Company::Get(_current_company);
- /* Get the bank balance of the company you acquired. */
- owner->money += c->money;
- /* Pay back the loan in full. */
- owner->money -= c->current_loan;
- }
-
if (c->is_ai) AI::Stop(c->index);
DeleteCompanyWindows(ci);
@@ -2033,27 +2025,28 @@ CommandCost CmdBuyShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1,
/* Those lines are here for network-protection (clients can be slow) */
if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 0) return cost;
+ bool acquire_company = GetAmountOwnedBy(c, _current_company) == 3;
if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1) {
if (!c->is_ai) return cost; // We can not buy out a real company (temporarily). TODO: well, enable it obviously.
- if (GetAmountOwnedBy(c, _current_company) == 3 && !MayCompanyTakeOver(_current_company, target_company)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
+ if (acquire_company && !MayCompanyTakeOver(_current_company, target_company)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
}
-
cost.AddCost(CalculateCompanyValue(c) >> 2);
+ if (acquire_company) {
+ /* Get the bank balance of the company you acquired. */
+ cost.AddCost(-c->money);
+ /* Pay back the loan in full. */
+ cost.AddCost(c->current_loan);
+ }
+
if (flags & DC_EXEC) {
Owner *b = c->share_owners;
while (*b != COMPANY_SPECTATOR) b++; // share owners is guaranteed to contain at least one COMPANY_SPECTATOR
*b = _current_company;
- for (int i = 0; c->share_owners[i] == _current_company;) {
- if (++i == 4) {
- c->bankrupt_value = 0;
- DoAcquireCompany(c);
- break;
- }
- }
+ if (acquire_company) DoAcquireCompany(c, true);
InvalidateWindowData(WC_COMPANY, target_company);
CompanyAdminUpdate(c);
}
@@ -2132,7 +2125,7 @@ CommandCost CmdBuyCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
CommandCost cost(EXPENSES_OTHER, c->bankrupt_value);
if (flags & DC_EXEC) {
- DoAcquireCompany(c);
+ DoAcquireCompany(c, false);
}
return cost;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment