From bc149ca95cf576b09c20f3f205f36540e7c89913 Mon Sep 17 00:00:00 2001 From: Jan Kelemen Date: Sun, 3 Mar 2024 17:05:48 +0100 Subject: [PATCH] Move gHistoricalProfit and gGuestsInParkHistory to GameState_t (#21513) * Move gGuestsInParkHistory to GameState_t * Move gHistoricalProfit to GameState_t --- src/openrct2-ui/windows/Park.cpp | 6 ++++-- src/openrct2/GameState.h | 3 +++ src/openrct2/management/Finance.cpp | 17 ++++++++--------- src/openrct2/management/Finance.h | 6 ------ src/openrct2/park/ParkFile.cpp | 4 ++-- src/openrct2/rct1/S4Importer.cpp | 8 +++++--- src/openrct2/rct2/S6Importer.cpp | 4 ++-- src/openrct2/scenario/Scenario.cpp | 2 +- src/openrct2/world/Park.cpp | 5 ++--- src/openrct2/world/Park.h | 1 - 10 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/openrct2-ui/windows/Park.cpp b/src/openrct2-ui/windows/Park.cpp index 4cbaeda968..6bdbd577ad 100644 --- a/src/openrct2-ui/windows/Park.cpp +++ b/src/openrct2-ui/windows/Park.cpp @@ -770,9 +770,11 @@ private: auto screenPos = windowPos; Widget* widget = &widgets[WIDX_PAGE_BACKGROUND]; + const auto& gameState = OpenRCT2::GetGameState(); + // Current value auto ft = Formatter(); - ft.Add(OpenRCT2::GetGameState().NumGuestsInPark); + ft.Add(gameState.NumGuestsInPark); DrawTextBasic(dpi, screenPos + ScreenCoordsXY{ widget->left + 3, widget->top + 2 }, STR_GUESTS_IN_PARK_LABEL, ft); // Graph border @@ -803,7 +805,7 @@ private: uint8_t cappedHistory[32]; for (size_t i = 0; i < std::size(cappedHistory); i++) { - auto value = gGuestsInParkHistory[i]; + auto value = gameState.GuestsInParkHistory[i]; if (value != std::numeric_limits::max()) { cappedHistory[i] = static_cast(std::min(value, 5000) / 20); diff --git a/src/openrct2/GameState.h b/src/openrct2/GameState.h index 0701676609..21a6ecdc5b 100644 --- a/src/openrct2/GameState.h +++ b/src/openrct2/GameState.h @@ -42,10 +42,13 @@ namespace OpenRCT2 money64 ParkValue; money64 ParkValueHistory[FINANCE_GRAPH_SIZE]; money64 CompanyValue; + // The total profit for the entire scenario that precedes the current financial table. + money64 HistoricalProfit; money64 ConstructionRightsPrice; money64 CurrentExpenditure; money64 CurrentProfit; uint8_t ParkRatingHistory[32]; + uint32_t GuestsInParkHistory[32]; ClimateType Climate; ClimateState ClimateCurrent; ClimateState ClimateNext; diff --git a/src/openrct2/management/Finance.cpp b/src/openrct2/management/Finance.cpp index 6e7e4fdd20..9a99a987f0 100644 --- a/src/openrct2/management/Finance.cpp +++ b/src/openrct2/management/Finance.cpp @@ -25,6 +25,8 @@ #include "../windows/Intent.h" #include "../world/Park.h" +#include + using namespace OpenRCT2; // Monthly research funding costs @@ -39,7 +41,6 @@ static constexpr int32_t dword_988E60[static_cast(ExpenditureType::Coun 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, }; -money64 gHistoricalProfit; money64 gExpenditureTable[EXPENDITURE_TABLE_MONTH_COUNT][static_cast(ExpenditureType::Count)]; /** @@ -229,11 +230,10 @@ void FinanceInit() gameState.BankLoan = 10000.00_GBP; gameState.MaxBankLoan = 20000.00_GBP; - gHistoricalProfit = 0; - gameState.BankLoanInterestRate = 10; gameState.ParkValue = 0; gameState.CompanyValue = 0; + gameState.HistoricalProfit = 0; gameState.ScenarioCompletedCompanyValue = kMoney64Undefined; gameState.TotalAdmissions = 0; gameState.TotalIncomeFromAdmissions = 0; @@ -322,12 +322,11 @@ void FinanceShiftExpenditureTable() // If EXPENDITURE_TABLE_MONTH_COUNT months have passed then is full, sum the oldest month if (GetDate().GetMonthsElapsed() >= EXPENDITURE_TABLE_MONTH_COUNT) { - money64 sum = 0; - for (uint32_t i = 0; i < static_cast(ExpenditureType::Count); i++) - { - sum += gExpenditureTable[EXPENDITURE_TABLE_MONTH_COUNT - 1][i]; - } - gHistoricalProfit += sum; + const money64 sum = std::accumulate( + std::cbegin(gExpenditureTable[EXPENDITURE_TABLE_MONTH_COUNT - 1]), + std::cend(gExpenditureTable[EXPENDITURE_TABLE_MONTH_COUNT - 1]), money64{}); + + GetGameState().HistoricalProfit += sum; } // Shift the table diff --git a/src/openrct2/management/Finance.h b/src/openrct2/management/Finance.h index 6b3536937f..7666f3e818 100644 --- a/src/openrct2/management/Finance.h +++ b/src/openrct2/management/Finance.h @@ -38,12 +38,6 @@ constexpr uint8_t MaxBankLoanInterestRate = 255; extern const money64 research_cost_table[RESEARCH_FUNDING_COUNT]; -/** - * The total profit for the entire scenario that precedes - * the current financial table. - */ -extern money64 gHistoricalProfit; - extern money64 gExpenditureTable[EXPENDITURE_TABLE_MONTH_COUNT][static_cast(ExpenditureType::Count)]; bool FinanceCheckMoneyRequired(uint32_t flags); diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 4f37d842aa..26c4dea608 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -851,7 +851,7 @@ namespace OpenRCT2 } } } - cs.ReadWrite(gHistoricalProfit); + cs.ReadWrite(gameState.HistoricalProfit); // Marketing cs.ReadWriteVector(gMarketingCampaigns, [&cs](MarketingCampaign& campaign) { @@ -922,7 +922,7 @@ namespace OpenRCT2 return true; }); - cs.ReadWriteArray(gGuestsInParkHistory, [&cs](uint32_t& value) { + cs.ReadWriteArray(gameState.GuestsInParkHistory, [&cs](uint32_t& value) { cs.ReadWrite(value); return true; }); diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 6d714a69f0..fc326b7f94 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -2156,7 +2156,7 @@ namespace RCT1 { if (_s4.GuestsInParkHistory[i] != RCT12ParkHistoryUndefined) { - gGuestsInParkHistory[i] = _s4.GuestsInParkHistory[i] * RCT12GuestsInParkHistoryFactor; + gameState.GuestsInParkHistory[i] = _s4.GuestsInParkHistory[i] * RCT12GuestsInParkHistoryFactor; } } @@ -2171,12 +2171,14 @@ namespace RCT1 } // Number of guests history - std::fill(std::begin(gGuestsInParkHistory), std::end(gGuestsInParkHistory), std::numeric_limits::max()); + std::fill( + std::begin(gameState.GuestsInParkHistory), std::end(gameState.GuestsInParkHistory), + std::numeric_limits::max()); for (size_t i = 0; i < std::size(_s4.GuestsInParkHistory); i++) { if (_s4.GuestsInParkHistory[i] != std::numeric_limits::max()) { - gGuestsInParkHistory[i] = _s4.GuestsInParkHistory[i] * 20; + gameState.GuestsInParkHistory[i] = _s4.GuestsInParkHistory[i] * 20; } } diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index c52066d415..82ebc8e647 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -314,7 +314,7 @@ namespace RCT2 { if (_s6.GuestsInParkHistory[i] != RCT12ParkHistoryUndefined) { - gGuestsInParkHistory[i] = _s6.GuestsInParkHistory[i] * RCT12GuestsInParkHistoryFactor; + gameState.GuestsInParkHistory[i] = _s6.GuestsInParkHistory[i] * RCT12GuestsInParkHistoryFactor; } } @@ -396,7 +396,7 @@ namespace RCT2 gameState.ScenarioCompanyValueRecord = _s6.CompletedCompanyValueRecord; // _s6.LoanHash; // Pad013587CA - gHistoricalProfit = ToMoney64(_s6.HistoricalProfit); + gameState.HistoricalProfit = ToMoney64(_s6.HistoricalProfit); // Pad013587D4 gameState.ScenarioCompletedBy = std::string_view(_s6.ScenarioCompletedName, sizeof(_s6.ScenarioCompletedName)); gameState.Cash = ToMoney64(DECRYPT_MONEY(_s6.Cash)); diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 1c141a8bbb..d6c055c9f4 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -104,7 +104,7 @@ void ScenarioReset(GameState_t& gameState) gameState.ParkRating = park.CalculateParkRating(); gameState.ParkValue = park.CalculateParkValue(); gameState.CompanyValue = park.CalculateCompanyValue(); - gHistoricalProfit = gameState.InitialCash - gameState.BankLoan; + gameState.HistoricalProfit = gameState.InitialCash - gameState.BankLoan; gameState.Cash = gameState.InitialCash; { diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index 5c7a8c8ece..43d325f8b2 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -50,7 +50,6 @@ using namespace OpenRCT2; money64 gLandPrice; int16_t gParkRatingCasualtyPenalty; -uint32_t gGuestsInParkHistory[32]; // If this value is more than or equal to 0, the park rating is forced to this value. Used for cheat static int32_t _forcedParkRating = -1; @@ -729,7 +728,7 @@ void Park::ResetHistories() { auto& gameState = GetGameState(); std::fill(std::begin(gameState.ParkRatingHistory), std::end(gameState.ParkRatingHistory), ParkRatingHistoryUndefined); - std::fill(std::begin(gGuestsInParkHistory), std::end(gGuestsInParkHistory), GuestsInParkHistoryUndefined); + std::fill(std::begin(gameState.GuestsInParkHistory), std::end(gameState.GuestsInParkHistory), GuestsInParkHistoryUndefined); } void Park::UpdateHistories() @@ -751,7 +750,7 @@ void Park::UpdateHistories() // Update park rating, guests in park and current cash history HistoryPushRecord(gameState.ParkRatingHistory, gameState.ParkRating / 4); - HistoryPushRecord(gGuestsInParkHistory, gameState.NumGuestsInPark); + HistoryPushRecord(gameState.GuestsInParkHistory, gameState.NumGuestsInPark); constexpr auto cashHistorySize = std::extent_v; HistoryPushRecord(gameState.CashHistory, FinanceGetCurrentCash() - gameState.BankLoan); diff --git a/src/openrct2/world/Park.h b/src/openrct2/world/Park.h index c0858ff0fe..404bcdb78c 100644 --- a/src/openrct2/world/Park.h +++ b/src/openrct2/world/Park.h @@ -95,7 +95,6 @@ namespace OpenRCT2 extern money64 gLandPrice; extern int16_t gParkRatingCasualtyPenalty; -extern uint32_t gGuestsInParkHistory[32]; void ParkSetForcedRating(int32_t rating); int32_t ParkGetForcedRating();