diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index c44c69e0d1..b17c13fc93 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -202,14 +202,15 @@ void InvalidateCompanyWindows(const Company *company) */ Money GetAvailableMoney(CompanyID company) { + if (_settings_game.difficulty.infinite_money) return INT64_MAX; if (!Company::IsValidID(company)) return INT64_MAX; return Company::Get(company)->money; } /** * This functions returns the money which can be used to execute a command. - * This is either the money of the current company or INT64_MAX if there - * is no such a company "at the moment" like the server itself. + * This is either the money of the current company, or INT64_MAX if infinite money + * is enabled or there is no such a company "at the moment" like the server itself. * * @return The available money of the current company or INT64_MAX */ @@ -221,17 +222,19 @@ Money GetAvailableMoneyForCommand() /** * Verify whether the company can pay the bill. * @param[in,out] cost Money to pay, is changed to an error if the company does not have enough money. - * @return Function returns \c true if the company has enough money, else it returns \c false. + * @return Function returns \c true if the company has enough money or infinite money is enabled, + * else it returns \c false. */ bool CheckCompanyHasMoney(CommandCost &cost) { - if (cost.GetCost() > 0) { - const Company *c = Company::GetIfValid(_current_company); - if (c != nullptr && cost.GetCost() > c->money) { - SetDParam(0, cost.GetCost()); - cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); - return false; - } + if (cost.GetCost() <= 0) return true; + if (_settings_game.difficulty.infinite_money) return true; + + const Company *c = Company::GetIfValid(_current_company); + if (c != nullptr && cost.GetCost() > c->money) { + SetDParam(0, cost.GetCost()); + cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); + return false; } return true; } diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp index b1d5a10d5f..482583de3c 100644 --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -179,7 +179,7 @@ company = ResolveCompanyID(company); if (company == COMPANY_INVALID) return -1; - return ::Company::Get(company)->money; + return GetAvailableMoney((::CompanyID)company); } /* static */ Money ScriptCompany::GetLoanAmount() diff --git a/src/vehicle.cpp b/src/vehicle.cpp index fabf14ce13..7365d4fe41 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -220,7 +220,7 @@ bool Vehicle::NeedsServicing() const * There are a lot more reasons for autoreplace to fail than we can test here reasonably. */ bool pending_replace = false; Money needed_money = c->settings.engine_renew_money; - if (needed_money > c->money) return false; + if (needed_money > GetAvailableMoney(c->index)) return false; for (const Vehicle *v = this; v != nullptr; v = (v->type == VEH_TRAIN) ? Train::From(v)->GetNextUnit() : nullptr) { bool replace_when_old = false; @@ -258,7 +258,7 @@ bool Vehicle::NeedsServicing() const * We want 2*(the price of the new vehicle) without looking at the value of the vehicle we are going to sell. */ pending_replace = true; needed_money += 2 * Engine::Get(new_engine)->GetCost(); - if (needed_money > c->money) return false; + if (needed_money > GetAvailableMoney(c->index)) return false; } return pending_replace;