From 107faaf821f9b39f2d23c22e22889738aa9e4abf Mon Sep 17 00:00:00 2001 From: Hielke Morsink Date: Wed, 7 Nov 2018 23:12:05 +0100 Subject: [PATCH] Add and use game action for setting the climate Also reordered the registered game actions the same as headers, so they are easier to find. --- data/language/en-GB.txt | 1 + .../windows/EditorScenarioOptions.cpp | 9 +-- src/openrct2/Game.h | 3 +- src/openrct2/actions/ClimateSetAction.hpp | 59 +++++++++++++++++++ .../actions/GameActionRegistration.cpp | 22 +++---- src/openrct2/interface/InteractiveConsole.cpp | 31 +++++++--- src/openrct2/localisation/StringIds.h | 2 + src/openrct2/network/Network.cpp | 2 +- src/openrct2/world/Climate.h | 3 +- 9 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 src/openrct2/actions/ClimateSetAction.hpp diff --git a/data/language/en-GB.txt b/data/language/en-GB.txt index 7f0a7a50a1..5af63f13f9 100644 --- a/data/language/en-GB.txt +++ b/data/language/en-GB.txt @@ -3729,6 +3729,7 @@ STR_6265 :{SMALLFONT}{BLACK}When enabled, your operating system's file browse STR_6266 :Open custom content folder STR_6267 :Open tile inspector STR_6268 :Advance to next tick +STR_6269 :Invalid climate ID ############# # Scenarios # diff --git a/src/openrct2-ui/windows/EditorScenarioOptions.cpp b/src/openrct2-ui/windows/EditorScenarioOptions.cpp index 034b9fc326..dfdcf13897 100644 --- a/src/openrct2-ui/windows/EditorScenarioOptions.cpp +++ b/src/openrct2-ui/windows/EditorScenarioOptions.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -538,14 +539,14 @@ static void window_editor_scenario_options_show_climate_dropdown(rct_window* w) dropdownWidget = &w->widgets[WIDX_CLIMATE]; - for (i = 0; i < 4; i++) + for (i = 0; i < CLIMATE_COUNT; i++) { gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL; gDropdownItemsArgs[i] = ClimateNames[i]; } window_dropdown_show_text_custom_width( w->x + dropdownWidget->left, w->y + dropdownWidget->top, dropdownWidget->bottom - dropdownWidget->top + 1, - w->colours[1], 0, DROPDOWN_FLAG_STAY_OPEN, 4, dropdownWidget->right - dropdownWidget->left - 3); + w->colours[1], 0, DROPDOWN_FLAG_STAY_OPEN, CLIMATE_COUNT, dropdownWidget->right - dropdownWidget->left - 3); dropdown_set_checked(gClimate, true); } @@ -1276,8 +1277,8 @@ static void window_editor_scenario_options_park_dropdown(rct_window* w, rct_widg case WIDX_CLIMATE_DROPDOWN: if (gClimate != (uint8_t)dropdownIndex) { - gClimate = (uint8_t)dropdownIndex; - window_invalidate(w); + auto gameAction = ClimateSetAction(dropdownIndex); + GameActions::Execute(&gameAction); } break; } diff --git a/src/openrct2/Game.h b/src/openrct2/Game.h index 729b3e84b9..c26b00aefe 100644 --- a/src/openrct2/Game.h +++ b/src/openrct2/Game.h @@ -89,7 +89,8 @@ enum GAME_COMMAND GAME_COMMAND_MODIFY_TILE, GAME_COMMAND_EDIT_SCENARIO_OPTIONS, GAME_COMMAND_PLACE_PEEP_SPAWN, // GA, TODO: refactor to separate array for just game actions - GAME_COMMAND_COUNT + GAME_COMMAND_SET_CLIMATE, // GA + GAME_COMMAND_COUNT, }; enum : uint32_t diff --git a/src/openrct2/actions/ClimateSetAction.hpp b/src/openrct2/actions/ClimateSetAction.hpp new file mode 100644 index 0000000000..023862afe9 --- /dev/null +++ b/src/openrct2/actions/ClimateSetAction.hpp @@ -0,0 +1,59 @@ +/***************************************************************************** + * Copyright (c) 2014-2018 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "../world/Climate.h" +#include "GameAction.h" + +struct ClimateSetAction : public GameActionBase +{ + using climate_t = decltype(gClimate); + +private: + climate_t _climate; + +public: + ClimateSetAction() = default; + ClimateSetAction(climate_t climate) + : _climate(climate) + { + } + + uint16_t GetActionFlags() const override + { + return GameAction::GetActionFlags() | GA_FLAGS::ALLOW_WHILE_PAUSED; + } + + void Serialise(DataSerialiser& stream) override + { + GameAction::Serialise(stream); + + stream << _climate; + } + + GameActionResult::Ptr Query() const override + { + if (_climate >= CLIMATE_COUNT) + { + return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_CLIMATE_ID, STR_NONE); + } + + return std::make_unique(); + } + + GameActionResult::Ptr Execute() const override + { + gClimate = _climate; + + gfx_invalidate_screen(); + + return std::make_unique(); + } +}; diff --git a/src/openrct2/actions/GameActionRegistration.cpp b/src/openrct2/actions/GameActionRegistration.cpp index 2d1b830149..6ae3a6ce5d 100644 --- a/src/openrct2/actions/GameActionRegistration.cpp +++ b/src/openrct2/actions/GameActionRegistration.cpp @@ -8,6 +8,7 @@ *****************************************************************************/ #include "BannerSetNameAction.hpp" +#include "ClimateSetAction.hpp" #include "FootpathRemoveAction.hpp" #include "GameAction.h" #include "GuestSetNameAction.hpp" @@ -32,24 +33,25 @@ namespace GameActions { void Register() { - Register(); + Register(); + Register(); + Register(); + Register(); + Register(); Register(); Register(); + Register(); Register(); Register(); + Register(); Register(); - Register(); - Register(); Register(); - Register(); + Register(); + Register(); + Register(); + Register(); Register(); Register(); - Register(); - Register(); - Register(); - Register(); - Register(); Register(); - Register(); } } // namespace GameActions diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index b8ca7fd8cc..1ca141490b 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -14,6 +14,7 @@ #include "../Game.h" #include "../OpenRCT2.h" #include "../Version.h" +#include "../actions/ClimateSetAction.hpp" #include "../config/Config.h" #include "../core/Guard.hpp" #include "../core/String.hpp" @@ -49,9 +50,15 @@ #ifndef NO_TTF # include "../drawing/TTF.h" - #endif +static constexpr const utf8* ClimateNames[] = { + "cool_and_wet", + "warm", + "hot_and_dry", + "cold", +}; + static void console_write_all_commands(InteractiveConsole& console); static int32_t console_parse_int(const utf8* src, bool* valid); static double console_parse_double(const utf8* src, bool* valid); @@ -558,8 +565,7 @@ static int32_t cc_get(InteractiveConsole& console, const utf8** argv, int32_t ar } else if (strcmp(argv[0], "climate") == 0) { - const utf8* climate_names[] = { "cool_and_wet", "warm", "hot_and_dry", "cold" }; - console.WriteFormatLine("climate %s (%d)", climate_names[gClimate], gClimate); + console.WriteFormatLine("climate %s (%d)", ClimateNames[gClimate], gClimate); } else if (strcmp(argv[0], "game_speed") == 0) { @@ -804,21 +810,30 @@ static int32_t cc_set(InteractiveConsole& console, const utf8** argv, int32_t ar { if (int_valid[0]) { - gClimate = std::clamp(int_val[0], 0, 3); + const auto newClimate = int_val[0]; + if (newClimate < 0 || newClimate >= CLIMATE_COUNT) + { + console.WriteLine(language_get_string(STR_INVALID_CLIMATE_ID)); + } + else + { + auto gameAction = ClimateSetAction(newClimate); + GameActions::Execute(&gameAction); + } } else { - const utf8* climate_names[] = { "cool_and_wet", "warm", "hot_and_dry", "cold" }; - for (i = 0; i < 4; i++) + for (i = 0; i < CLIMATE_COUNT; i++) { - if (strcmp(argv[1], climate_names[i]) == 0) + if (strcmp(argv[1], ClimateNames[i]) == 0) { gClimate = i; break; } } } - if (i == 4) + + if (i == CLIMATE_COUNT) invalidArgs = true; else console.Execute("get climate"); diff --git a/src/openrct2/localisation/StringIds.h b/src/openrct2/localisation/StringIds.h index 5311d509f9..d910c2488a 100644 --- a/src/openrct2/localisation/StringIds.h +++ b/src/openrct2/localisation/StringIds.h @@ -3897,6 +3897,8 @@ enum STR_ADVANCE_TO_NEXT_TICK = 6268, + STR_INVALID_CLIMATE_ID = 6269, + // Have to include resource strings (from scenarios and objects) for the time being now that language is partially working STR_COUNT = 32768 }; diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index 444574b1c3..d28d2c9964 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -28,7 +28,7 @@ // This string specifies which version of network stream current build uses. // It is used for making sure only compatible builds get connected, even within // single OpenRCT2 version. -#define NETWORK_STREAM_VERSION "8" +#define NETWORK_STREAM_VERSION "9" #define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION static rct_peep* _pickup_peep = nullptr; diff --git a/src/openrct2/world/Climate.h b/src/openrct2/world/Climate.h index e8c0e16549..b53a80a1d7 100644 --- a/src/openrct2/world/Climate.h +++ b/src/openrct2/world/Climate.h @@ -17,7 +17,8 @@ enum CLIMATE CLIMATE_COOL_AND_WET, CLIMATE_WARM, CLIMATE_HOT_AND_DRY, - CLIMATE_COLD + CLIMATE_COLD, + CLIMATE_COUNT, }; enum WEATHER