Refactor result passing of TrackDesignAction

This commit is contained in:
ζeh Matt 2021-10-16 22:24:38 +03:00
parent e06f9c3ee7
commit 8e06d84a09
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0
3 changed files with 17 additions and 44 deletions

View File

@ -289,10 +289,10 @@ static void window_track_place_toolupdate(rct_window* w, rct_widgetindex widgetI
// Valid location found. Place the ghost at the location. // Valid location found. Place the ghost at the location.
auto tdAction = TrackDesignAction({ trackLoc, _currentTrackPieceDirection }, *_trackDesign); auto tdAction = TrackDesignAction({ trackLoc, _currentTrackPieceDirection }, *_trackDesign);
tdAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND | GAME_COMMAND_FLAG_GHOST); tdAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND | GAME_COMMAND_FLAG_GHOST);
tdAction.SetCallback([trackLoc](const GameAction*, const TrackDesignActionResult* result) { tdAction.SetCallback([trackLoc](const GameAction*, const GameActions::Result* result) {
if (result->Error == GameActions::Status::Ok) if (result->Error == GameActions::Status::Ok)
{ {
_window_track_place_ride_index = result->rideIndex; _window_track_place_ride_index = result->GetData<ride_id_t>();
_windowTrackPlaceLastValid = trackLoc; _windowTrackPlaceLastValid = trackLoc;
_window_track_place_last_was_valid = true; _window_track_place_last_was_valid = true;
} }
@ -336,20 +336,21 @@ static void window_track_place_tooldown(rct_window* w, rct_widgetindex widgetInd
if (res->Error == GameActions::Status::Ok) if (res->Error == GameActions::Status::Ok)
{ {
auto tdAction = TrackDesignAction({ trackLoc, _currentTrackPieceDirection }, *_trackDesign); auto tdAction = TrackDesignAction({ trackLoc, _currentTrackPieceDirection }, *_trackDesign);
tdAction.SetCallback([trackLoc](const GameAction*, const TrackDesignActionResult* result) { tdAction.SetCallback([trackLoc](const GameAction*, const GameActions::Result* result) {
if (result->Error == GameActions::Status::Ok) if (result->Error == GameActions::Status::Ok)
{ {
auto ride = get_ride(result->rideIndex); const auto rideId = result->GetData<ride_id_t>();
auto ride = get_ride(rideId);
if (ride != nullptr) if (ride != nullptr)
{ {
window_close_by_class(WC_ERROR); window_close_by_class(WC_ERROR);
OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, trackLoc); OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::PlaceItem, trackLoc);
_currentRideIndex = result->rideIndex; _currentRideIndex = rideId;
if (track_design_are_entrance_and_exit_placed()) if (track_design_are_entrance_and_exit_placed())
{ {
auto intent = Intent(WC_RIDE); auto intent = Intent(WC_RIDE);
intent.putExtra(INTENT_EXTRA_RIDE_ID, static_cast<int32_t>(result->rideIndex)); intent.putExtra(INTENT_EXTRA_RIDE_ID, static_cast<int32_t>(rideId));
context_open_intent(&intent); context_open_intent(&intent);
auto wnd = window_find_by_class(WC_TRACK_DESIGN_PLACE); auto wnd = window_find_by_class(WC_TRACK_DESIGN_PLACE);
window_close(wnd); window_close(wnd);

View File

@ -27,26 +27,6 @@ static int32_t place_virtual_track(
return place_virtual_track(const_cast<TrackDesign*>(&td6), ptdOperation, placeScenery, ride, loc); return place_virtual_track(const_cast<TrackDesign*>(&td6), ptdOperation, placeScenery, ride, loc);
} }
TrackDesignActionResult::TrackDesignActionResult()
: GameActions::Result(GameActions::Status::Ok, STR_NONE)
{
}
TrackDesignActionResult::TrackDesignActionResult(GameActions::Status error)
: GameActions::Result(error, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, STR_NONE)
{
}
TrackDesignActionResult::TrackDesignActionResult(GameActions::Status error, rct_string_id title, rct_string_id message)
: GameActions::Result(error, title, message)
{
}
TrackDesignActionResult::TrackDesignActionResult(GameActions::Status error, rct_string_id message)
: GameActions::Result(error, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, message)
{
}
TrackDesignAction::TrackDesignAction(const CoordsXYZD& location, const TrackDesign& td) TrackDesignAction::TrackDesignAction(const CoordsXYZD& location, const TrackDesign& td)
: _loc(location) : _loc(location)
, _td(td) , _td(td)
@ -83,7 +63,7 @@ GameActions::Result::Ptr TrackDesignAction::Query() const
if (!LocationValid(_loc)) if (!LocationValid(_loc))
{ {
return MakeResult(GameActions::Status::InvalidParameters); return MakeResult(GameActions::Status::InvalidParameters, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, STR_NONE);
} }
auto& objManager = OpenRCT2::GetContext()->GetObjectManager(); auto& objManager = OpenRCT2::GetContext()->GetObjectManager();
@ -112,7 +92,7 @@ GameActions::Result::Ptr TrackDesignAction::Query() const
if (ride == nullptr) if (ride == nullptr)
{ {
log_warning("Invalid game command for track placement, ride id = %d", rideIndex); log_warning("Invalid game command for track placement, ride id = %d", rideIndex);
return MakeResult(GameActions::Status::Unknown); return MakeResult(GameActions::Status::Unknown, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, STR_NONE);
} }
money32 cost = 0; money32 cost = 0;
@ -132,9 +112,11 @@ GameActions::Result::Ptr TrackDesignAction::Query() const
GameActions::ExecuteNested(&gameAction); GameActions::ExecuteNested(&gameAction);
if (cost == MONEY32_UNDEFINED) if (cost == MONEY32_UNDEFINED)
{ {
return MakeResult(GameActions::Status::Disallowed, error_reason); return MakeResult(GameActions::Status::Disallowed, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, error_reason);
} }
res->Cost = cost; res->Cost = cost;
res->SetData(ride_id_t{ RIDE_ID_NULL });
return res; return res;
} }
@ -172,7 +154,7 @@ GameActions::Result::Ptr TrackDesignAction::Execute() const
if (ride == nullptr) if (ride == nullptr)
{ {
log_warning("Invalid game command for track placement, ride id = %d", rideIndex); log_warning("Invalid game command for track placement, ride id = %d", rideIndex);
return MakeResult(GameActions::Status::Unknown); return MakeResult(GameActions::Status::Unknown, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, STR_NONE);
} }
money32 cost = 0; money32 cost = 0;
@ -210,7 +192,7 @@ GameActions::Result::Ptr TrackDesignAction::Execute() const
gameAction.SetFlags(GetFlags()); gameAction.SetFlags(GetFlags());
GameActions::ExecuteNested(&gameAction); GameActions::ExecuteNested(&gameAction);
return MakeResult(GameActions::Status::Disallowed, error_reason); return MakeResult(GameActions::Status::Disallowed, STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE, error_reason);
} }
if (entryIndex != OBJECT_ENTRY_INDEX_NULL) if (entryIndex != OBJECT_ENTRY_INDEX_NULL)
@ -269,6 +251,7 @@ GameActions::Result::Ptr TrackDesignAction::Execute() const
r = GameActions::ExecuteNested(&gameAction); r = GameActions::ExecuteNested(&gameAction);
} }
res->Cost = cost; res->Cost = cost;
res->rideIndex = ride->id; res->SetData(ride_id_t{ ride->id });
return res; return res;
} }

View File

@ -12,18 +12,7 @@
#include "../ride/TrackDesign.h" #include "../ride/TrackDesign.h"
#include "GameAction.h" #include "GameAction.h"
class TrackDesignActionResult final : public GameActions::Result DEFINE_GAME_ACTION(TrackDesignAction, GameCommand::PlaceTrackDesign, GameActions::Result)
{
public:
TrackDesignActionResult();
TrackDesignActionResult(GameActions::Status error);
TrackDesignActionResult(GameActions::Status error, rct_string_id title, rct_string_id message);
TrackDesignActionResult(GameActions::Status error, rct_string_id message);
ride_id_t rideIndex = RIDE_ID_NULL;
};
DEFINE_GAME_ACTION(TrackDesignAction, GameCommand::PlaceTrackDesign, TrackDesignActionResult)
{ {
private: private:
CoordsXYZD _loc; CoordsXYZD _loc;