#21193: Move gCash to GameState_t, refactor uses

This commit is contained in:
ζeh Matt 2024-01-20 15:46:35 +02:00
parent 2cb4eb950b
commit 3518a638bb
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0
15 changed files with 175 additions and 153 deletions

View File

@ -28,6 +28,8 @@
#include <openrct2/sprites.h>
#include <openrct2/world/Park.h>
using namespace OpenRCT2;
enum
{
WINDOW_FINANCES_PAGE_SUMMARY,
@ -571,8 +573,8 @@ public:
// Current cash
auto ft = Formatter();
ft.Add<money64>(gCash);
StringId stringId = gCash >= 0 ? STR_CASH_LABEL : STR_CASH_NEGATIVE_LABEL;
ft.Add<money64>(GetGameState().Cash);
StringId stringId = GetGameState().Cash >= 0 ? STR_CASH_LABEL : STR_CASH_NEGATIVE_LABEL;
DrawTextBasic(dpi, windowPos + ScreenCoordsXY{ 8, 294 }, stringId, ft);
// Objective related financial information
@ -612,7 +614,7 @@ public:
auto graphBottomRight = windowPos + ScreenCoordsXY{ pageWidget->right - 4, pageWidget->bottom - 4 };
// Cash (less loan)
auto cashLessLoan = gCash - gBankLoan;
auto cashLessLoan = GetGameState().Cash - gBankLoan;
auto ft = Formatter();
ft.Add<money64>(cashLessLoan);

View File

@ -14,6 +14,7 @@
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
#include <openrct2/Game.h>
#include <openrct2/GameState.h>
#include <openrct2/Input.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/config/Config.h>
@ -29,6 +30,8 @@
#include <openrct2/world/Climate.h>
#include <openrct2/world/Park.h>
using namespace OpenRCT2;
// clang-format off
enum WindowGameBottomToolbarWidgetIdx
{
@ -98,9 +101,9 @@ private:
= (gHoverWidget.window_classification == WindowClass::BottomToolbar && gHoverWidget.widget_index == WIDX_MONEY
? COLOUR_WHITE
: NOT_TRANSLUCENT(colours[0]));
StringId stringId = gCash < 0 ? STR_BOTTOM_TOOLBAR_CASH_NEGATIVE : STR_BOTTOM_TOOLBAR_CASH;
StringId stringId = GetGameState().Cash < 0 ? STR_BOTTOM_TOOLBAR_CASH_NEGATIVE : STR_BOTTOM_TOOLBAR_CASH;
auto ft = Formatter();
ft.Add<money64>(gCash);
ft.Add<money64>(GetGameState().Cash);
DrawTextBasic(dpi, screenCoords, stringId, ft, { colour, TextAlignment::CENTRE });
}

View File

@ -24,6 +24,7 @@ namespace OpenRCT2
struct GameState_t
{
uint32_t CurrentTicks{};
money64 Cash;
};
GameState_t& GetGameState();

View File

@ -39,6 +39,8 @@
#include "ParkSetLoanAction.h"
#include "ParkSetParameterAction.h"
using namespace OpenRCT2;
using ParametersRange = std::pair<std::pair<int64_t, int64_t>, std::pair<int64_t, int64_t>>;
CheatSetAction::CheatSetAction(CheatType cheatType, int64_t param1, int64_t param2)
@ -565,7 +567,7 @@ void CheatSetAction::SetScenarioNoMoney(bool enabled) const
void CheatSetAction::SetMoney(money64 amount) const
{
gCash = amount;
GetGameState().Cash = amount;
WindowInvalidateByClass(WindowClass::Finances);
WindowInvalidateByClass(WindowClass::BottomToolbar);
@ -573,7 +575,7 @@ void CheatSetAction::SetMoney(money64 amount) const
void CheatSetAction::AddMoney(money64 amount) const
{
gCash = AddClamp_money64(gCash, amount);
GetGameState().Cash = AddClamp_money64(GetGameState().Cash, amount);
WindowInvalidateByClass(WindowClass::Finances);
WindowInvalidateByClass(WindowClass::BottomToolbar);

View File

@ -10,6 +10,7 @@
#include "ParkSetLoanAction.h"
#include "../Context.h"
#include "../GameState.h"
#include "../core/MemoryStream.h"
#include "../localisation/StringIds.h"
#include "../management/Finance.h"
@ -17,6 +18,8 @@
#include "../ui/WindowManager.h"
#include "../windows/Intent.h"
using namespace OpenRCT2;
ParkSetLoanAction::ParkSetLoanAction(money64 value)
: _value(value)
{
@ -52,7 +55,7 @@ GameActions::Result ParkSetLoanAction::Query() const
// The “isPayingBack” check is needed to allow increasing the loan when the player is in debt.
const auto isPayingBack = gBankLoan > _value;
const auto amountToPayBack = gBankLoan - _value;
if (isPayingBack && amountToPayBack > gCash)
if (isPayingBack && amountToPayBack > GetGameState().Cash)
{
return GameActions::Result(
GameActions::Status::InsufficientFunds, STR_CANT_PAY_BACK_LOAN, STR_NOT_ENOUGH_CASH_AVAILABLE);
@ -62,7 +65,7 @@ GameActions::Result ParkSetLoanAction::Query() const
GameActions::Result ParkSetLoanAction::Execute() const
{
gCash -= (gBankLoan - _value);
GetGameState().Cash -= (gBankLoan - _value);
gBankLoan = _value;
auto windowManager = OpenRCT2::GetContext()->GetUiContext()->GetWindowManager();

View File

@ -9,6 +9,7 @@
#include "ScenarioSetSettingAction.h"
#include "../GameState.h"
#include "../OpenRCT2.h"
#include "../entity/Peep.h"
#include "../interface/Window.h"
@ -19,6 +20,8 @@
#include <algorithm>
using namespace OpenRCT2;
void ScenarioSetSettingAction::Serialise(DataSerialiser& stream)
{
GameAction::Serialise(stream);
@ -80,7 +83,7 @@ GameActions::Result ScenarioSetSettingAction::Execute() const
break;
case ScenarioSetSetting::InitialCash:
gInitialCash = std::clamp<money64>(_value, 0.00_GBP, 1000000.00_GBP);
gCash = gInitialCash;
GetGameState().Cash = gInitialCash;
WindowInvalidateByClass(WindowClass::Finances);
WindowInvalidateByClass(WindowClass::BottomToolbar);
break;

View File

@ -13,6 +13,7 @@
#include "../Date.h"
#include "../EditorObjectSelectionSession.h"
#include "../Game.h"
#include "../GameState.h"
#include "../OpenRCT2.h"
#include "../PlatformEnvironment.h"
#include "../ReplayManager.h"
@ -78,6 +79,8 @@
# include "../drawing/TTF.h"
#endif
using namespace OpenRCT2;
using arguments_t = std::vector<std::string>;
using OpenRCT2::Date;
@ -564,7 +567,7 @@ static int32_t ConsoleCommandGet(InteractiveConsole& console, const arguments_t&
}
else if (argv[0] == "money")
{
console.WriteFormatLine("money %d.%d0", gCash / 10, gCash % 10);
console.WriteFormatLine("money %d.%d0", GetGameState().Cash / 10, GetGameState().Cash % 10);
}
else if (argv[0] == "scenario_initial_cash")
{
@ -767,7 +770,7 @@ static int32_t ConsoleCommandSet(InteractiveConsole& console, const arguments_t&
if (argv[0] == "money" && InvalidArguments(&invalidArgs, double_valid[0]))
{
money64 money = ToMoney64FromGBP(double_val[0]);
if (gCash != money)
if (GetGameState().Cash != money)
{
auto cheatSetAction = CheatSetAction(CheatType::SetMoney, money);
cheatSetAction.SetCallback([&console](const GameAction*, const GameActions::Result* res) {

View File

@ -11,6 +11,7 @@
#include "../Context.h"
#include "../Game.h"
#include "../GameState.h"
#include "../OpenRCT2.h"
#include "../entity/Peep.h"
#include "../entity/Staff.h"
@ -24,6 +25,8 @@
#include "../windows/Intent.h"
#include "../world/Park.h"
using namespace OpenRCT2;
// Monthly research funding costs
const money64 research_cost_table[RESEARCH_FUNDING_COUNT] = {
0.00_GBP, // No funding
@ -37,7 +40,6 @@ static constexpr int32_t dword_988E60[static_cast<int32_t>(ExpenditureType::Coun
};
money64 gInitialCash;
money64 gCash;
money64 gBankLoan;
uint8_t gBankLoanInterestRate;
money64 gMaxBankLoan;
@ -75,7 +77,7 @@ bool FinanceCheckMoneyRequired(uint32_t flags)
*/
bool FinanceCheckAffordability(money64 cost, uint32_t flags)
{
return !FinanceCheckMoneyRequired(flags) || cost <= 0 || cost <= gCash;
return !FinanceCheckMoneyRequired(flags) || cost <= 0 || cost <= GetGameState().Cash;
}
/**
@ -87,7 +89,7 @@ bool FinanceCheckAffordability(money64 cost, uint32_t flags)
void FinancePayment(money64 amount, ExpenditureType type)
{
// overflow check
gCash = AddClamp_money64(gCash, -amount);
GetGameState().Cash = AddClamp_money64(GetGameState().Cash, -amount);
gExpenditureTable[0][static_cast<int32_t>(type)] -= amount;
if (dword_988E60[static_cast<int32_t>(type)] & 1)
@ -226,7 +228,7 @@ void FinanceInit()
gInitialCash = 10000.00_GBP; // Cheat detection
gCash = 10000.00_GBP;
GetGameState().Cash = 10000.00_GBP;
gBankLoan = 10000.00_GBP;
gMaxBankLoan = 20000.00_GBP;
@ -309,7 +311,7 @@ money64 FinanceGetMaximumLoan()
money64 FinanceGetCurrentCash()
{
return gCash;
return GetGameState().Cash;
}
/**
@ -354,7 +356,7 @@ void FinanceShiftExpenditureTable()
*/
void FinanceResetCashToInitial()
{
gCash = gInitialCash;
GetGameState().Cash = gInitialCash;
}
/**

View File

@ -39,7 +39,6 @@ constexpr uint8_t MaxBankLoanInterestRate = 255;
extern const money64 research_cost_table[RESEARCH_FUNDING_COUNT];
extern money64 gInitialCash;
extern money64 gCash;
extern money64 gBankLoan;
extern uint8_t gBankLoanInterestRate;
extern money64 gMaxBankLoan;

View File

@ -11,6 +11,7 @@
# include "NetworkServerAdvertiser.h"
# include "../GameState.h"
# include "../config/Config.h"
# include "../core/Console.hpp"
# include "../core/Guard.hpp"
@ -33,6 +34,8 @@
# include <random>
# include <string>
using namespace OpenRCT2;
enum class MasterServerStatus
{
Ok = 200,
@ -306,7 +309,7 @@ private:
};
if (!(gParkFlags & PARK_FLAGS_NO_MONEY))
{
gameInfo["cash"] = gCash;
gameInfo["cash"] = GetGameState().Cash;
}
root["gameInfo"] = gameInfo;

View File

@ -166,7 +166,7 @@ namespace OpenRCT2
}
// Initial cash will eventually be removed
gInitialCash = gCash;
gInitialCash = gameState.Cash;
}
void Save(GameState_t& gameState, IStream& stream)
@ -797,148 +797,149 @@ namespace OpenRCT2
void ReadWriteParkChunk(GameState_t& gameState, OrcaStream& os)
{
os.ReadWriteChunk(ParkFileChunkType::PARK, [version = os.GetHeader().TargetVersion](OrcaStream::ChunkStream& cs) {
// TODO: Use the passed gameState instead of the global one.
auto& park = GetContext()->GetGameState()->GetPark();
cs.ReadWrite(park.Name);
cs.ReadWrite(gCash);
cs.ReadWrite(gBankLoan);
cs.ReadWrite(gMaxBankLoan);
cs.ReadWrite(gBankLoanInterestRate);
cs.ReadWrite(gParkFlags);
if (version <= 18)
{
money16 tempParkEntranceFee{};
cs.ReadWrite(tempParkEntranceFee);
gParkEntranceFee = ToMoney64(tempParkEntranceFee);
}
else
{
cs.ReadWrite(gParkEntranceFee);
}
cs.ReadWrite(gStaffHandymanColour);
cs.ReadWrite(gStaffMechanicColour);
cs.ReadWrite(gStaffSecurityColour);
cs.ReadWrite(gSamePriceThroughoutPark);
// Finances
if (cs.GetMode() == OrcaStream::Mode::READING)
{
auto numMonths = std::min<uint32_t>(EXPENDITURE_TABLE_MONTH_COUNT, cs.Read<uint32_t>());
auto numTypes = std::min<uint32_t>(static_cast<uint32_t>(ExpenditureType::Count), cs.Read<uint32_t>());
for (uint32_t i = 0; i < numMonths; i++)
os.ReadWriteChunk(
ParkFileChunkType::PARK, [version = os.GetHeader().TargetVersion, &gameState](OrcaStream::ChunkStream& cs) {
// TODO: Use the passed gameState instead of the global one.
auto& park = GetContext()->GetGameState()->GetPark();
cs.ReadWrite(park.Name);
cs.ReadWrite(gameState.Cash);
cs.ReadWrite(gBankLoan);
cs.ReadWrite(gMaxBankLoan);
cs.ReadWrite(gBankLoanInterestRate);
cs.ReadWrite(gParkFlags);
if (version <= 18)
{
for (uint32_t j = 0; j < numTypes; j++)
money16 tempParkEntranceFee{};
cs.ReadWrite(tempParkEntranceFee);
gParkEntranceFee = ToMoney64(tempParkEntranceFee);
}
else
{
cs.ReadWrite(gParkEntranceFee);
}
cs.ReadWrite(gStaffHandymanColour);
cs.ReadWrite(gStaffMechanicColour);
cs.ReadWrite(gStaffSecurityColour);
cs.ReadWrite(gSamePriceThroughoutPark);
// Finances
if (cs.GetMode() == OrcaStream::Mode::READING)
{
auto numMonths = std::min<uint32_t>(EXPENDITURE_TABLE_MONTH_COUNT, cs.Read<uint32_t>());
auto numTypes = std::min<uint32_t>(static_cast<uint32_t>(ExpenditureType::Count), cs.Read<uint32_t>());
for (uint32_t i = 0; i < numMonths; i++)
{
gExpenditureTable[i][j] = cs.Read<money64>();
for (uint32_t j = 0; j < numTypes; j++)
{
gExpenditureTable[i][j] = cs.Read<money64>();
}
}
}
}
else
{
auto numMonths = static_cast<uint32_t>(EXPENDITURE_TABLE_MONTH_COUNT);
auto numTypes = static_cast<uint32_t>(ExpenditureType::Count);
cs.Write(numMonths);
cs.Write(numTypes);
for (uint32_t i = 0; i < numMonths; i++)
else
{
for (uint32_t j = 0; j < numTypes; j++)
auto numMonths = static_cast<uint32_t>(EXPENDITURE_TABLE_MONTH_COUNT);
auto numTypes = static_cast<uint32_t>(ExpenditureType::Count);
cs.Write(numMonths);
cs.Write(numTypes);
for (uint32_t i = 0; i < numMonths; i++)
{
cs.Write(gExpenditureTable[i][j]);
for (uint32_t j = 0; j < numTypes; j++)
{
cs.Write(gExpenditureTable[i][j]);
}
}
}
}
cs.ReadWrite(gHistoricalProfit);
cs.ReadWrite(gHistoricalProfit);
// Marketing
cs.ReadWriteVector(gMarketingCampaigns, [&cs](MarketingCampaign& campaign) {
cs.ReadWrite(campaign.Type);
cs.ReadWrite(campaign.WeeksLeft);
cs.ReadWrite(campaign.Flags);
cs.ReadWrite(campaign.RideId);
});
// Marketing
cs.ReadWriteVector(gMarketingCampaigns, [&cs](MarketingCampaign& campaign) {
cs.ReadWrite(campaign.Type);
cs.ReadWrite(campaign.WeeksLeft);
cs.ReadWrite(campaign.Flags);
cs.ReadWrite(campaign.RideId);
});
// Awards
if (version <= 6)
{
Award awards[RCT2::Limits::MaxAwards]{};
cs.ReadWriteArray(awards, [&cs](Award& award) {
if (award.Time != 0)
{
// Awards
if (version <= 6)
{
Award awards[RCT2::Limits::MaxAwards]{};
cs.ReadWriteArray(awards, [&cs](Award& award) {
if (award.Time != 0)
{
cs.ReadWrite(award.Time);
cs.ReadWrite(award.Type);
GetAwards().push_back(award);
return true;
}
return false;
});
}
else
{
cs.ReadWriteVector(GetAwards(), [&cs](Award& award) {
cs.ReadWrite(award.Time);
cs.ReadWrite(award.Type);
GetAwards().push_back(award);
return true;
}
});
}
cs.ReadWrite(gParkValue);
cs.ReadWrite(gCompanyValue);
cs.ReadWrite(gParkSize);
cs.ReadWrite(gNumGuestsInPark);
cs.ReadWrite(gNumGuestsHeadingForPark);
cs.ReadWrite(gParkRating);
cs.ReadWrite(gParkRatingCasualtyPenalty);
cs.ReadWrite(gCurrentExpenditure);
cs.ReadWrite(gCurrentProfit);
cs.ReadWrite(gWeeklyProfitAverageDividend);
cs.ReadWrite(gWeeklyProfitAverageDivisor);
cs.ReadWrite(gTotalAdmissions);
cs.ReadWrite(gTotalIncomeFromAdmissions);
if (version <= 16)
{
money16 legacyTotalRideValueForMoney = 0;
cs.ReadWrite(legacyTotalRideValueForMoney);
gTotalRideValueForMoney = legacyTotalRideValueForMoney;
}
else
{
cs.ReadWrite(gTotalRideValueForMoney);
}
cs.ReadWrite(gNumGuestsInParkLastWeek);
cs.ReadWrite(gGuestChangeModifier);
cs.ReadWrite(_guestGenerationProbability);
cs.ReadWrite(_suggestedGuestMaximum);
return false;
cs.ReadWriteArray(gPeepWarningThrottle, [&cs](uint8_t& value) {
cs.ReadWrite(value);
return true;
});
}
else
{
cs.ReadWriteVector(GetAwards(), [&cs](Award& award) {
cs.ReadWrite(award.Time);
cs.ReadWrite(award.Type);
cs.ReadWriteArray(gParkRatingHistory, [&cs](uint8_t& value) {
cs.ReadWrite(value);
return true;
});
}
cs.ReadWrite(gParkValue);
cs.ReadWrite(gCompanyValue);
cs.ReadWrite(gParkSize);
cs.ReadWrite(gNumGuestsInPark);
cs.ReadWrite(gNumGuestsHeadingForPark);
cs.ReadWrite(gParkRating);
cs.ReadWrite(gParkRatingCasualtyPenalty);
cs.ReadWrite(gCurrentExpenditure);
cs.ReadWrite(gCurrentProfit);
cs.ReadWrite(gWeeklyProfitAverageDividend);
cs.ReadWrite(gWeeklyProfitAverageDivisor);
cs.ReadWrite(gTotalAdmissions);
cs.ReadWrite(gTotalIncomeFromAdmissions);
if (version <= 16)
{
money16 legacyTotalRideValueForMoney = 0;
cs.ReadWrite(legacyTotalRideValueForMoney);
gTotalRideValueForMoney = legacyTotalRideValueForMoney;
}
else
{
cs.ReadWrite(gTotalRideValueForMoney);
}
cs.ReadWrite(gNumGuestsInParkLastWeek);
cs.ReadWrite(gGuestChangeModifier);
cs.ReadWrite(_guestGenerationProbability);
cs.ReadWrite(_suggestedGuestMaximum);
cs.ReadWriteArray(gPeepWarningThrottle, [&cs](uint8_t& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gGuestsInParkHistory, [&cs](uint32_t& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gParkRatingHistory, [&cs](uint8_t& value) {
cs.ReadWrite(value);
return true;
cs.ReadWriteArray(gCashHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gWeeklyProfitHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gParkValueHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
});
cs.ReadWriteArray(gGuestsInParkHistory, [&cs](uint32_t& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gCashHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gWeeklyProfitHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
cs.ReadWriteArray(gParkValueHistory, [&cs](money64& value) {
cs.ReadWrite(value);
return true;
});
});
}
void ReadWriteResearchChunk(GameState_t& gameState, OrcaStream& os)

View File

@ -180,7 +180,7 @@ namespace RCT1
ImportSprites();
ImportTileElements();
ImportPeepSpawns();
ImportFinance();
ImportFinance(gameState);
ImportResearch();
ImportParkName();
ImportParkFlags(gameState);
@ -1389,13 +1389,13 @@ namespace RCT1
}
}
void ImportFinance()
void ImportFinance(GameState_t& gameState)
{
gParkEntranceFee = _s4.ParkEntranceFee;
gLandPrice = ToMoney64(_s4.LandPrice);
gConstructionRightsPrice = ToMoney64(_s4.ConstructionRightsPrice);
gCash = ToMoney64(_s4.Cash);
gameState.Cash = ToMoney64(_s4.Cash);
gBankLoan = ToMoney64(_s4.Loan);
gMaxBankLoan = ToMoney64(_s4.MaxLoan);
// It's more like 1.33%, but we can only use integers. Can be fixed once we have our own save format.

View File

@ -397,7 +397,7 @@ namespace RCT2
gHistoricalProfit = ToMoney64(_s6.HistoricalProfit);
// Pad013587D4
gScenarioCompletedBy = std::string_view(_s6.ScenarioCompletedName, sizeof(_s6.ScenarioCompletedName));
gCash = ToMoney64(DECRYPT_MONEY(_s6.Cash));
gameState.Cash = ToMoney64(DECRYPT_MONEY(_s6.Cash));
// Pad013587FC
gParkRatingCasualtyPenalty = _s6.ParkRatingCasualtyPenalty;
gMapSize = { _s6.MapSize, _s6.MapSize };

View File

@ -115,7 +115,7 @@ void ScenarioReset()
gParkValue = park.CalculateParkValue();
gCompanyValue = park.CalculateCompanyValue();
gHistoricalProfit = gInitialCash - gBankLoan;
gCash = gInitialCash;
GetGameState().Cash = gInitialCash;
{
utf8 normalisedName[64];

View File

@ -51,15 +51,15 @@ namespace OpenRCT2::Scripting
money64 ScPark::cash_get() const
{
return gCash;
return GetGameState().Cash;
}
void ScPark::cash_set(money64 value)
{
ThrowIfGameStateNotMutable();
if (gCash != value)
if (GetGameState().Cash != value)
{
gCash = value;
GetGameState().Cash = value;
auto intent = Intent(INTENT_ACTION_UPDATE_CASH);
ContextBroadcastIntent(&intent);
}