diff --git a/src/command.cpp b/src/command.cpp index ce4a9482c9..305a8b2726 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -190,7 +190,7 @@ void CommandHelperBase::InternalDoAfter(CommandCost &res, DoCommandFlags flags, } else { /* If top-level, subtract the money. */ if (res.Succeeded() && top_level && !flags.Test(DoCommandFlag::Bankrupt)) { - SubtractMoneyFromCompany(res); + SubtractMoneyFromCompany(_current_company, res); } } } @@ -381,7 +381,7 @@ CommandCost CommandHelperBase::InternalExecuteProcessResult(Commands cmd, Comman if (c != nullptr) c->last_build_coordinate = tile; } - SubtractMoneyFromCompany(res_exec); + SubtractMoneyFromCompany(_current_company, res_exec); /* Record if there was a command issues during pause; ignore pause/other setting related changes. */ if (_pause_mode.Any() && _command_proc_table[cmd].type != CommandType::ServerSetting) _pause_mode.Set(PauseMode::CommandDuringPause); diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index aac1aa7a83..a049e30548 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -281,7 +281,7 @@ bool CheckCompanyHasMoney(CommandCost &cost) * @param c Company to pay the bill. * @param cost Money to pay. */ -static void SubtractMoneyFromAnyCompany(Company *c, const CommandCost &cost) +static void SubtractMoneyFromCompany(Company *c, const CommandCost &cost) { if (cost.GetCost() == 0) return; assert(cost.GetExpensesType() != INVALID_EXPENSES); @@ -307,13 +307,14 @@ static void SubtractMoneyFromAnyCompany(Company *c, const CommandCost &cost) } /** - * Subtract money from the #_current_company, if the company is valid. + * Subtract money from a company, if the company is valid. + * @param company CompanyID of company. * @param cost Money to pay. */ -void SubtractMoneyFromCompany(const CommandCost &cost) +void SubtractMoneyFromCompany(CompanyID company, const CommandCost &cost) { - Company *c = Company::GetIfValid(_current_company); - if (c != nullptr) SubtractMoneyFromAnyCompany(c, cost); + Company *c = Company::GetIfValid(company); + if (c != nullptr) SubtractMoneyFromCompany(c, cost); } /** @@ -330,7 +331,7 @@ void SubtractMoneyFromCompanyFract(CompanyID company, const CommandCost &cst) c->money_fraction = m - (uint8_t)cost; cost >>= 8; if (c->money_fraction > m) cost++; - if (cost != 0) SubtractMoneyFromAnyCompany(c, CommandCost(cst.GetExpensesType(), cost)); + if (cost != 0) SubtractMoneyFromCompany(c, CommandCost(cst.GetExpensesType(), cost)); } static constexpr void UpdateLandscapingLimit(uint32_t &limit, uint64_t per_64k_frames, uint64_t burst) @@ -1328,9 +1329,7 @@ CommandCost CmdGiveMoney(DoCommandFlags flags, Money money, CompanyID dest_compa if (flags.Test(DoCommandFlag::Execute)) { /* Add money to company */ - Backup cur_company(_current_company, dest_company); - SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost())); - cur_company.Restore(); + SubtractMoneyFromCompany(dest_company, CommandCost(EXPENSES_OTHER, -amount.GetCost())); if (_networking) { std::string dest_company_name = GetString(STR_COMPANY_NAME, dest_company); diff --git a/src/company_func.h b/src/company_func.h index e0e13f8613..25c6823738 100644 --- a/src/company_func.h +++ b/src/company_func.h @@ -28,7 +28,7 @@ void UpdateCompanyLiveries(Company *c); Money GetAvailableMoney(CompanyID company); Money GetAvailableMoneyForCommand(); bool CheckCompanyHasMoney(CommandCost &cost); -void SubtractMoneyFromCompany(const CommandCost &cost); +void SubtractMoneyFromCompany(CompanyID company, const CommandCost &cost); void SubtractMoneyFromCompanyFract(CompanyID company, const CommandCost &cost); CommandCost CheckOwnership(Owner owner, TileIndex tile = {}); CommandCost CheckTileOwnership(TileIndex tile); diff --git a/src/economy.cpp b/src/economy.cpp index a82ccc1468..c4f029cddd 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -645,14 +645,10 @@ static void CompaniesGenStatistics() CompanyCheckBankrupt(c); } - Backup cur_company(_current_company); - /* Pay Infrastructure Maintenance, if enabled */ if (_settings_game.economy.infrastructure_maintenance) { /* Improved monthly infrastructure costs. */ for (const Company *c : Company::Iterate()) { - cur_company.Change(c->index); - CommandCost cost(EXPENSES_PROPERTY); uint32_t rail_total = c->infrastructure.GetRailTotal(); for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) { @@ -668,10 +664,9 @@ static void CompaniesGenStatistics() cost.AddCost(StationMaintenanceCost(c->infrastructure.station)); cost.AddCost(AirportMaintenanceCost(c->index)); - SubtractMoneyFromCompany(cost); + SubtractMoneyFromCompany(c->index, cost); } } - cur_company.Restore(); /* Only run the economic statistics and update company stats every 3rd economy month (1st of quarter). */ if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, TimerGameEconomy::month)) return; @@ -808,10 +803,7 @@ void RecomputePrices() /** Let all companies pay the monthly interest on their loan. */ static void CompaniesPayInterest() { - Backup cur_company(_current_company); for (const Company *c : Company::Iterate()) { - cur_company.Change(c->index); - /* Over a year the paid interest should be "loan * interest percentage", * but... as that number is likely not dividable by 12 (pay each month), * one needs to account for that in the monthly fee calculations. @@ -834,11 +826,10 @@ static void CompaniesPayInterest() Money up_to_previous_month = yearly_fee * TimerGameEconomy::month / 12; Money up_to_this_month = yearly_fee * (TimerGameEconomy::month + 1) / 12; - SubtractMoneyFromCompany(CommandCost(EXPENSES_LOAN_INTEREST, up_to_this_month - up_to_previous_month)); + SubtractMoneyFromCompany(c->index, CommandCost(EXPENSES_LOAN_INTEREST, up_to_this_month - up_to_previous_month)); - SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, _price[PR_STATION_VALUE] >> 2)); + SubtractMoneyFromCompany(c->index, CommandCost(EXPENSES_OTHER, _price[PR_STATION_VALUE] >> 2)); } - cur_company.Restore(); } static void HandleEconomyFluctuations() @@ -1189,7 +1180,7 @@ CargoPayment::~CargoPayment() Backup cur_company(_current_company, this->front->owner); - SubtractMoneyFromCompany(CommandCost(this->front->GetExpenseType(true), -this->route_profit)); + SubtractMoneyFromCompany(_current_company, CommandCost(this->front->GetExpenseType(true), -this->route_profit)); this->front->profit_this_year += (this->visual_profit + this->visual_transfer) << 8; if (this->route_profit != 0 && IsLocalCompany() && !PlayVehicleSound(this->front, VSE_LOAD_UNLOAD)) { diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp index ba2f1ba5a0..343f657a50 100644 --- a/src/misc_cmd.cpp +++ b/src/misc_cmd.cpp @@ -244,9 +244,7 @@ CommandCost CmdChangeBankBalance(DoCommandFlags flags, TileIndex tile, Money del if (flags.Test(DoCommandFlag::Execute)) { /* Change company bank balance of company. */ - Backup cur_company(_current_company, company); - SubtractMoneyFromCompany(CommandCost(expenses_type, -delta)); - cur_company.Restore(); + SubtractMoneyFromCompany(company, CommandCost(expenses_type, -delta)); if (tile != 0) { ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 91588a6129..0feef76047 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1056,9 +1056,9 @@ void CallVehicleTicks() int z = v->z_pos; const Company *c = Company::Get(_current_company); - SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, (Money)c->settings.engine_renew_money)); + SubtractMoneyFromCompany(_current_company, CommandCost(EXPENSES_NEW_VEHICLES, (Money)c->settings.engine_renew_money)); CommandCost res = Command::Do(DoCommandFlag::Execute, v->index); - SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, -(Money)c->settings.engine_renew_money)); + SubtractMoneyFromCompany(_current_company, CommandCost(EXPENSES_NEW_VEHICLES, -(Money)c->settings.engine_renew_money)); if (!IsLocalCompany()) continue;