diff --git a/src/openrct2-ui/windows/Finances.cpp b/src/openrct2-ui/windows/Finances.cpp index 52e0cf6aaf..56179599bf 100644 --- a/src/openrct2-ui/windows/Finances.cpp +++ b/src/openrct2-ui/windows/Finances.cpp @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include #include #include @@ -594,19 +595,21 @@ static void window_finances_summary_mouseup(rct_window *w, rct_widgetindex widge */ static void window_finances_summary_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget* widget) { - money32 newLoan; - - switch (widgetIndex) { + switch (widgetIndex) + { case WIDX_LOAN_INCREASE: - newLoan = gBankLoan + MONEY(1000, 00); - gGameCommandErrorTitle = STR_CANT_BORROW_ANY_MORE_MONEY; - finance_set_loan(newLoan); - break; + { + auto newLoan = gBankLoan + MONEY(1000, 00); + auto gameAction = ParkSetLoanAction(newLoan); + GameActions::Execute(&gameAction); + break; + } case WIDX_LOAN_DECREASE: - if (gBankLoan > 0) { - newLoan = gBankLoan - MONEY(1000, 00); - gGameCommandErrorTitle = STR_CANT_PAY_BACK_LOAN; - finance_set_loan(newLoan); + if (gBankLoan > 0) + { + auto newLoan = gBankLoan - MONEY(1000, 00); + auto gameAction = ParkSetLoanAction(newLoan); + GameActions::Execute(&gameAction); } break; } diff --git a/src/openrct2/Cheats.cpp b/src/openrct2/Cheats.cpp index 4292bf6a63..471caae21c 100644 --- a/src/openrct2/Cheats.cpp +++ b/src/openrct2/Cheats.cpp @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include "actions/ParkSetLoanAction.hpp" #include "Cheats.h" #include "config/Config.h" #include "localisation/Localisation.h" @@ -259,9 +260,8 @@ static void cheat_clear_loan() cheat_add_money(gBankLoan); // Then pay the loan - money32 newLoan; - newLoan = MONEY(0, 00); - game_do_command(0, GAME_COMMAND_FLAG_APPLY, 0, newLoan, GAME_COMMAND_SET_CURRENT_LOAN, 0, 0); + auto gameAction = ParkSetLoanAction(MONEY(0, 00)); + GameActions::Execute(&gameAction); } static void cheat_generate_guests(sint32 count) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index d9b0ecc251..a71f96b665 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -1733,7 +1733,7 @@ GAME_COMMAND_POINTER * new_game_command_table[GAME_COMMAND_COUNT] = { game_command_remove_wall, game_command_place_large_scenery, game_command_remove_large_scenery, - game_command_set_current_loan, + nullptr, game_command_set_research_funding, game_command_place_track_design, game_command_start_campaign, diff --git a/src/openrct2/Game.h b/src/openrct2/Game.h index 1824f0b823..71e7054ae6 100644 --- a/src/openrct2/Game.h +++ b/src/openrct2/Game.h @@ -72,7 +72,7 @@ enum GAME_COMMAND GAME_COMMAND_REMOVE_WALL, GAME_COMMAND_PLACE_LARGE_SCENERY, GAME_COMMAND_REMOVE_LARGE_SCENERY, - GAME_COMMAND_SET_CURRENT_LOAN, + GAME_COMMAND_SET_CURRENT_LOAN, // GA GAME_COMMAND_SET_RESEARCH_FUNDING, GAME_COMMAND_PLACE_TRACK_DESIGN, GAME_COMMAND_START_MARKETING_CAMPAIGN, diff --git a/src/openrct2/actions/GameActionRegistration.cpp b/src/openrct2/actions/GameActionRegistration.cpp index c10a5b743b..27b0d3620a 100644 --- a/src/openrct2/actions/GameActionRegistration.cpp +++ b/src/openrct2/actions/GameActionRegistration.cpp @@ -16,6 +16,7 @@ #include "GameAction.h" #include "GuestSetNameAction.hpp" +#include "ParkSetLoanAction.hpp" #include "PlaceParkEntranceAction.hpp" #include "SetParkEntranceFeeAction.hpp" #include "StaffSetNameAction.hpp" @@ -31,6 +32,7 @@ namespace GameActions void Register() { Register(); + Register(); Register(); Register(); Register(); diff --git a/src/openrct2/actions/ParkSetLoanAction.hpp b/src/openrct2/actions/ParkSetLoanAction.hpp new file mode 100644 index 0000000000..b9e17aacba --- /dev/null +++ b/src/openrct2/actions/ParkSetLoanAction.hpp @@ -0,0 +1,83 @@ +#pragma region Copyright (c) 2014-2018 OpenRCT2 Developers +/***************************************************************************** +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* OpenRCT2 is the work of many authors, a full list can be found in contributors.md +* For more information, visit https://github.com/OpenRCT2/OpenRCT2 +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* A full copy of the GNU General Public License can be found in licence.txt +*****************************************************************************/ +#pragma endregion + +#pragma once + +#include "../Context.h" +#include "../core/MemoryStream.h" +#include "../localisation/StringIds.h" +#include "../management/Finance.h" +#include "../ui/UiContext.h" +#include "../ui/WindowManager.h" +#include "../windows/Intent.h" +#include "GameAction.h" + +using namespace OpenRCT2; + +struct ParkSetLoanAction : public GameActionBase +{ +private: + money32 _value; + +public: + ParkSetLoanAction() {} + ParkSetLoanAction(money32 value) + : _value(value) + { + } + + uint16 GetActionFlags() const override + { + return GameAction::GetActionFlags() | GA_FLAGS::ALLOW_WHILE_PAUSED; + } + + void Serialise(DataSerialiser& stream) override + { + GameAction::Serialise(stream); + stream << _value; + } + + GameActionResult::Ptr Query() const override + { + auto currentLoan = gBankLoan; + auto loanDifference = currentLoan - _value; + if (_value > currentLoan) + { + if (_value > gMaxBankLoan) + { + return std::make_unique(GA_ERROR::DISALLOWED, STR_CANT_BORROW_ANY_MORE_MONEY, STR_BANK_REFUSES_TO_INCREASE_LOAN); + } + } + else + { + if (loanDifference > gCash) + { + return std::make_unique(GA_ERROR::INSUFFICIENT_FUNDS, STR_CANT_PAY_BACK_LOAN, STR_NOT_ENOUGH_CASH_AVAILABLE); + } + } + return std::make_unique(); + } + + GameActionResult::Ptr Execute() const override + { + gCash -= (gBankLoan - _value); + gBankLoan = _value; + + auto windowManager = GetContext()->GetUiContext()->GetWindowManager(); + windowManager->BroadcastIntent(Intent(INTENT_ACTION_UPDATE_CASH)); + return std::make_unique(); + } +}; diff --git a/src/openrct2/management/Finance.cpp b/src/openrct2/management/Finance.cpp index 105bca1842..0e8db543c5 100644 --- a/src/openrct2/management/Finance.cpp +++ b/src/openrct2/management/Finance.cpp @@ -284,11 +284,6 @@ void finance_update_daily_profit() window_invalidate_by_class(WC_FINANCES); } -void finance_set_loan(money32 loan) -{ - game_do_command(0, GAME_COMMAND_FLAG_APPLY, 0, loan, GAME_COMMAND_SET_CURRENT_LOAN, 0, 0); -} - money32 finance_get_initial_cash() { return gInitialCash; @@ -309,51 +304,6 @@ money32 finance_get_current_cash() return gCash; } -/** - * - * rct2: 0x0069DFB3 - */ -void game_command_set_current_loan(sint32 * eax, sint32 * ebx, sint32 * ecx, sint32 * edx, sint32 * esi, sint32 * edi, sint32 * ebp) -{ - money32 loanDifference, currentLoan; - money32 newLoan = *edx; - - currentLoan = gBankLoan; - loanDifference = currentLoan - newLoan; - - gCommandExpenditureType = RCT_EXPENDITURE_TYPE_INTEREST; - if (newLoan > currentLoan) - { - if (newLoan > gMaxBankLoan) - { - gGameCommandErrorText = STR_BANK_REFUSES_TO_INCREASE_LOAN; - *ebx = MONEY32_UNDEFINED; - return; - } - } - else - { - if (loanDifference > gCash) - { - gGameCommandErrorText = STR_NOT_ENOUGH_CASH_AVAILABLE; - *ebx = MONEY32_UNDEFINED; - return; - } - } - - if (*ebx & GAME_COMMAND_FLAG_APPLY) - { - gCash -= loanDifference; - gBankLoan = newLoan; - gInitialCash = gCash; - - auto intent = Intent(INTENT_ACTION_UPDATE_CASH); - context_broadcast_intent(&intent); - } - - *ebx = 0; -} - /** * Shift the expenditure table history one month to the left * If the table is full, accumulate the sum of the oldest month first diff --git a/src/openrct2/management/Finance.h b/src/openrct2/management/Finance.h index 066b5c06f9..9fd47c7b6b 100644 --- a/src/openrct2/management/Finance.h +++ b/src/openrct2/management/Finance.h @@ -80,11 +80,9 @@ void finance_update_daily_profit(); void finance_shift_expenditure_table(); void finance_reset_cash_to_initial(); -void finance_set_loan(money32 loan); money32 finance_get_initial_cash(); money32 finance_get_current_loan(); money32 finance_get_maximum_loan(); money32 finance_get_current_cash(); -void game_command_set_current_loan(sint32* eax, sint32* ebx, sint32* ecx, sint32* edx, sint32* esi, sint32* edi, sint32* ebp); money32 finance_get_last_month_shop_profit();