1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-19 02:12:37 +01:00

Codechange: Pass company ID to SubtractMoneyFromCompany. (#15018)

This avoids needing to backup and restore `_current_company` just for SubtractMoneyFromCompany to use it.
This commit is contained in:
Peter Nelson
2026-01-03 09:33:37 +00:00
committed by GitHub
parent 1464d5fd06
commit ebbb7cf3e9
6 changed files with 18 additions and 30 deletions

View File

@@ -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);

View File

@@ -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<CompanyID> 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);

View File

@@ -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);

View File

@@ -645,14 +645,10 @@ static void CompaniesGenStatistics()
CompanyCheckBankrupt(c);
}
Backup<CompanyID> 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<CompanyID> 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<CompanyID> 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)) {

View File

@@ -244,9 +244,7 @@ CommandCost CmdChangeBankBalance(DoCommandFlags flags, TileIndex tile, Money del
if (flags.Test(DoCommandFlag::Execute)) {
/* Change company bank balance of company. */
Backup<CompanyID> 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);

View File

@@ -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<CMD_AUTOREPLACE_VEHICLE>::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;