From 7f586f50e1bd39c57790ccf21d27ac49be13e34f Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Thu, 28 Feb 2019 10:56:25 +0000 Subject: [PATCH] Move all callees to the game action framework. Fix CoordXY bug --- src/openrct2-ui/windows/MazeConstruction.cpp | 51 +++++++++---------- src/openrct2-ui/windows/RideConstruction.cpp | 38 +++++++++++--- .../actions/RideEntranceExitPlaceAction.hpp | 4 +- src/openrct2/core/DataSerialiserTraits.h | 6 +-- src/openrct2/ride/TrackDesign.cpp | 42 ++++++++------- src/openrct2/world/Entrance.cpp | 12 +++-- 6 files changed, 90 insertions(+), 63 deletions(-) diff --git a/src/openrct2-ui/windows/MazeConstruction.cpp b/src/openrct2-ui/windows/MazeConstruction.cpp index 438f6023c0..9f60d6146f 100644 --- a/src/openrct2-ui/windows/MazeConstruction.cpp +++ b/src/openrct2-ui/windows/MazeConstruction.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -364,38 +365,32 @@ static void window_maze_construction_entrance_tooldown(int32_t x, int32_t y, rct return; ride_id_t rideIndex = gRideEntranceExitPlaceRideIndex; - uint8_t entranceExitType = gRideEntranceExitPlaceType; - if (entranceExitType == ENTRANCE_TYPE_RIDE_ENTRANCE) - { - gGameCommandErrorTitle = STR_CANT_BUILD_MOVE_ENTRANCE_FOR_THIS_RIDE_ATTRACTION; - } - else - { - gGameCommandErrorTitle = STR_CANT_BUILD_MOVE_EXIT_FOR_THIS_RIDE_ATTRACTION; - } - money32 cost = game_do_command( - x, GAME_COMMAND_FLAG_APPLY | (direction_reverse(direction) << 8), y, rideIndex | (entranceExitType << 8), - GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, gRideEntranceExitPlaceStationIndex, 0); + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction( + { x, y }, direction_reverse(direction), rideIndex, gRideEntranceExitPlaceStationIndex, + gRideEntranceExitPlaceType == ENTRANCE_TYPE_RIDE_EXIT); - if (cost == MONEY32_UNDEFINED) - return; + rideEntranceExitPlaceAction.SetCallback([=](const GameAction* ga, const GameActionResult* result) { + if (result->Error != GA_ERROR::OK) + return; - audio_play_sound_at_location(SOUND_PLACE_ITEM, gCommandPosition.x, gCommandPosition.y, gCommandPosition.z); + audio_play_sound_at_location(SOUND_PLACE_ITEM, result->Position.x, result->Position.y, result->Position.z); - Ride* ride = get_ride(rideIndex); - if (ride_are_all_possible_entrances_and_exits_built(ride)) - { - tool_cancel(); - if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_NO_TRACK)) - window_close(w); - } - else - { - gRideEntranceExitPlaceType = entranceExitType ^ 1; - window_invalidate_by_class(WC_RIDE_CONSTRUCTION); - gCurrentToolWidget.widget_index = entranceExitType ? WIDX_MAZE_ENTRANCE : WIDX_MAZE_EXIT; - } + Ride* ride = get_ride(rideIndex); + if (ride_are_all_possible_entrances_and_exits_built(ride)) + { + tool_cancel(); + if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_NO_TRACK)) + window_close_by_class(WC_RIDE_CONSTRUCTION); + } + else + { + gRideEntranceExitPlaceType = gRideEntranceExitPlaceType ^ 1; + window_invalidate_by_class(WC_RIDE_CONSTRUCTION); + gCurrentToolWidget.widget_index = gRideEntranceExitPlaceType ? WIDX_MAZE_ENTRANCE : WIDX_MAZE_EXIT; + } + }); + auto res = GameActions::Execute(&rideEntranceExitPlaceAction); } /** diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index 9c1ab3f314..f419e4976c 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -3938,15 +3939,36 @@ static void ride_construction_tooldown_entrance_exit(int32_t screenX, int32_t sc if (gRideEntranceExitPlaceDirection == 255) return; - gGameCommandErrorTitle = (gRideEntranceExitPlaceType == ENTRANCE_TYPE_RIDE_ENTRANCE) - ? STR_CANT_BUILD_MOVE_ENTRANCE_FOR_THIS_RIDE_ATTRACTION - : STR_CANT_BUILD_MOVE_EXIT_FOR_THIS_RIDE_ATTRACTION; + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction( + { _unkF44188.x, _unkF44188.y }, direction_reverse(gRideEntranceExitPlaceDirection), gRideEntranceExitPlaceRideIndex, + gRideEntranceExitPlaceStationIndex, + gRideEntranceExitPlaceType == ENTRANCE_TYPE_RIDE_EXIT); - game_command_callback = game_command_callback_place_ride_entrance_or_exit; - game_do_command( - _unkF44188.x, (GAME_COMMAND_FLAG_APPLY) | (direction_reverse(gRideEntranceExitPlaceDirection) << 8), _unkF44188.y, - gRideEntranceExitPlaceRideIndex | (gRideEntranceExitPlaceType << 8), GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, - gRideEntranceExitPlaceStationIndex, 0); + rideEntranceExitPlaceAction.SetCallback([=](const GameAction* ga, const GameActionResult* result) { + if (result->Error != GA_ERROR::OK) + return; + + audio_play_sound_at_location(SOUND_PLACE_ITEM, result->Position.x, result->Position.y, result->Position.z); + + Ride* ride = get_ride(gRideEntranceExitPlaceRideIndex); + if (ride_are_all_possible_entrances_and_exits_built(ride)) + { + tool_cancel(); + if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_NO_TRACK)) + { + window_close_by_class(WC_RIDE_CONSTRUCTION); + } + } + else + { + gRideEntranceExitPlaceType = gRideEntranceExitPlaceType ^ 1; + window_invalidate_by_class(WC_RIDE_CONSTRUCTION); + gCurrentToolWidget.widget_index = (gRideEntranceExitPlaceType == ENTRANCE_TYPE_RIDE_ENTRANCE) + ? WC_RIDE_CONSTRUCTION__WIDX_ENTRANCE + : WC_RIDE_CONSTRUCTION__WIDX_EXIT; + } + }); + auto res = GameActions::Execute(&rideEntranceExitPlaceAction); } void window_ride_construction_keyboard_shortcut_turn_left() diff --git a/src/openrct2/actions/RideEntranceExitPlaceAction.hpp b/src/openrct2/actions/RideEntranceExitPlaceAction.hpp index 4f0720c885..94dfc87eee 100644 --- a/src/openrct2/actions/RideEntranceExitPlaceAction.hpp +++ b/src/openrct2/actions/RideEntranceExitPlaceAction.hpp @@ -14,6 +14,8 @@ #include "../ride/Station.h" #include "../world/Entrance.h" #include "../world/MapAnimation.h" +#include "../management/Finance.h" +#include "../world/Sprite.h" #include "GameAction.h" DEFINE_GAME_ACTION(RideEntranceExitPlaceAction, GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, GameActionResult) @@ -46,7 +48,7 @@ public: { GameAction::Serialise(stream); - stream << DS_TAG(_loc) << DS_TAG(_rideIndex) << DS_TAG(_stationNum) << DS_TAG(_isExit); + stream << DS_TAG(_loc) << DS_TAG(_direction) << DS_TAG(_rideIndex) << DS_TAG(_stationNum) << DS_TAG(_isExit); } GameActionResult::Ptr Query() const override diff --git a/src/openrct2/core/DataSerialiserTraits.h b/src/openrct2/core/DataSerialiserTraits.h index f776c8b477..da80b1190a 100644 --- a/src/openrct2/core/DataSerialiserTraits.h +++ b/src/openrct2/core/DataSerialiserTraits.h @@ -331,9 +331,9 @@ template<> struct DataSerializerTraits } static void decode(IStream* stream, CoordsXY& coords) { - auto x = ByteSwapBE(stream->ReadValue()); - auto y = ByteSwapBE(stream->ReadValue()); - coords = CoordsXY(x, y); + auto x = ByteSwapBE(stream->ReadValue()); + auto y = ByteSwapBE(stream->ReadValue()); + coords = CoordsXY{x, y}; } static void log(IStream* stream, const CoordsXY& coords) { diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index 005ccb117a..ef69064c0b 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -1232,9 +1232,10 @@ static int32_t track_design_place_maze(rct_track_td6* td6, int16_t x, int16_t y, flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 | GAME_COMMAND_FLAG_GHOST; } - cost = game_do_command( - mapCoord.x, flags | rotation << 8, mapCoord.y, ride->id, GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, - 0, 0); + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(mapCoord, rotation, ride->id, 0, false); + rideEntranceExitPlaceAction.SetFlags(flags); + auto res = GameActions::ExecuteNested(&rideEntranceExitPlaceAction); + cost = res->Error == GA_ERROR::OK ? res->Cost : MONEY32_UNDEFINED; } if (cost != MONEY32_UNDEFINED) { @@ -1265,9 +1266,10 @@ static int32_t track_design_place_maze(rct_track_td6* td6, int16_t x, int16_t y, flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 | GAME_COMMAND_FLAG_GHOST; } - cost = game_do_command( - mapCoord.x, flags | rotation << 8, mapCoord.y, ride->id | (1 << 8), - GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, 0, 0); + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(mapCoord, rotation, ride->id, 0, true); + rideEntranceExitPlaceAction.SetFlags(flags); + auto res = GameActions::ExecuteNested(&rideEntranceExitPlaceAction); + cost = res->Error == GA_ERROR::OK ? res->Cost : MONEY32_UNDEFINED; } if (cost != MONEY32_UNDEFINED) { @@ -1572,10 +1574,10 @@ static bool track_design_place_ride(rct_track_td6* td6, int16_t x, int16_t y, in case PTD_OPERATION_GET_COST: { rotation = (rotation + entrance->direction) & 3; - uint8_t isExit = 0; + bool isExit = false; if (entrance->direction & (1 << 7)) { - isExit = 1; + isExit = true; } if (_trackDesignPlaceOperation != PTD_OPERATION_1) @@ -1600,29 +1602,33 @@ static bool track_design_place_ride(rct_track_td6* td6, int16_t x, int16_t y, in } int32_t stationIndex = tile_element->AsTrack()->GetStationIndex(); - uint8_t bl = 1; + uint8_t flags = GAME_COMMAND_FLAG_APPLY; if (_trackDesignPlaceOperation == PTD_OPERATION_GET_COST) { - bl = 41; + flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5; } if (_trackDesignPlaceOperation == PTD_OPERATION_4) { - bl = 105; + flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 + | GAME_COMMAND_FLAG_GHOST; } if (_trackDesignPlaceOperation == PTD_OPERATION_1) { - bl = 0; + flags = 0; } gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE; - money32 cost = game_do_command( - x, bl | (rotation << 8), y, ride->id | (isExit << 8), GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, - stationIndex, 0); - _trackDesignPlaceCost += cost; + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction( + { x, y }, rotation, ride->id, stationIndex, isExit); + rideEntranceExitPlaceAction.SetFlags(flags); + auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&rideEntranceExitPlaceAction) + : GameActions::QueryNested(&rideEntranceExitPlaceAction); - if (cost == MONEY32_UNDEFINED) + _trackDesignPlaceCost += res->Cost; + + if (res->Error != GA_ERROR::OK) { - _trackDesignPlaceCost = cost; + _trackDesignPlaceCost = MONEY32_UNDEFINED; return false; } _trackDesignPlaceStateEntranceExitPlaced = true; diff --git a/src/openrct2/world/Entrance.cpp b/src/openrct2/world/Entrance.cpp index 1422c26c5d..44888f4c3b 100644 --- a/src/openrct2/world/Entrance.cpp +++ b/src/openrct2/world/Entrance.cpp @@ -12,6 +12,7 @@ #include "../Cheats.h" #include "../Game.h" #include "../OpenRCT2.h" +#include "../actions/RideEntranceExitPlaceAction.hpp" #include "../actions/RideEntranceExitRemoveAction.hpp" #include "../localisation/StringIds.h" #include "../management/Finance.h" @@ -314,11 +315,12 @@ static money32 RideEntranceExitPlace( static money32 RideEntranceExitPlaceGhost( ride_id_t rideIndex, int16_t x, int16_t y, uint8_t direction, uint8_t placeType, uint8_t stationNum) { - return game_do_command( - x, - (GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 | GAME_COMMAND_FLAG_GHOST) - | (direction << 8), - y, rideIndex | (placeType << 8), GAME_COMMAND_PLACE_RIDE_ENTRANCE_OR_EXIT, stationNum, 0); + auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction( + { x, y }, direction, rideIndex, stationNum, placeType == ENTRANCE_TYPE_RIDE_EXIT); + rideEntranceExitPlaceAction.SetFlags(GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_GHOST); + auto res = GameActions::Execute(&rideEntranceExitPlaceAction); + + return res->Error == GA_ERROR::OK ? res->Cost : MONEY32_UNDEFINED; } /**