From 83221d51aab944bdef9e0fdfb2f90faaf4f00d68 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 5 Feb 2021 17:25:07 +0200 Subject: [PATCH] Use TileElementsView for RideEntranceExitRemoveAction and cleanup --- .../actions/RideEntranceExitRemoveAction.cpp | 106 ++++++------------ 1 file changed, 37 insertions(+), 69 deletions(-) diff --git a/src/openrct2/actions/RideEntranceExitRemoveAction.cpp b/src/openrct2/actions/RideEntranceExitRemoveAction.cpp index a15a1f8ac3..fd46dda522 100644 --- a/src/openrct2/actions/RideEntranceExitRemoveAction.cpp +++ b/src/openrct2/actions/RideEntranceExitRemoveAction.cpp @@ -12,6 +12,9 @@ #include "../ride/Ride.h" #include "../ride/Station.h" #include "../world/Entrance.h" +#include "../world/TileElementsView.h" + +using namespace OpenRCT2; RideEntranceExitRemoveAction::RideEntranceExitRemoveAction( const CoordsXY& loc, ride_id_t rideIndex, StationIndex stationNum, bool isExit) @@ -42,6 +45,28 @@ void RideEntranceExitRemoveAction::Serialise(DataSerialiser& stream) stream << DS_TAG(_loc) << DS_TAG(_rideIndex) << DS_TAG(_stationNum) << DS_TAG(_isExit); } +static TileElement* FindEntranceElement( + const CoordsXY& loc, ride_id_t rideIndex, int32_t stationNum, int32_t entranceType, bool ghost) +{ + for (auto* entranceElement : TileElementsView(loc)) + { + if (entranceElement->IsGhost() != ghost) + continue; + + if (entranceElement->GetRideIndex() != rideIndex) + continue; + + if (entranceElement->GetStationIndex() != stationNum) + continue; + + if (entranceElement->GetEntranceType() != entranceType) + continue; + + return entranceElement->as(); + } + return nullptr; +} + GameActions::Result::Ptr RideEntranceExitRemoveAction::Query() const { auto ride = get_ride(_rideIndex); @@ -66,40 +91,12 @@ GameActions::Result::Ptr RideEntranceExitRemoveAction::Query() const return MakeResult(GameActions::Status::InvalidParameters, STR_LAND_NOT_OWNED_BY_PARK); } - bool found = false; - TileElement* tileElement = map_get_first_element_at(_loc); + const bool isGhost = GetFlags() & GAME_COMMAND_FLAG_GHOST; - do - { - if (tileElement == nullptr) - break; + auto* entranceElement = FindEntranceElement( + _loc, _rideIndex, _stationNum, _isExit ? ENTRANCE_TYPE_RIDE_EXIT : ENTRANCE_TYPE_RIDE_ENTRANCE, isGhost); - if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE) - continue; - - if (tileElement->GetRideIndex() != _rideIndex) - continue; - - if (tileElement->AsEntrance()->GetStationIndex() != _stationNum) - continue; - - if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && !(tileElement->IsGhost())) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_PARK_ENTRANCE) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE && _isExit) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_RIDE_EXIT && !_isExit) - continue; - - found = true; - break; - } while (!(tileElement++)->IsLastForTile()); - - if (!found) + if (entranceElement == nullptr) { log_warning( "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, @@ -119,47 +116,18 @@ GameActions::Result::Ptr RideEntranceExitRemoveAction::Execute() const return std::make_unique(GameActions::Status::InvalidParameters, STR_NONE); } - if (!(GetFlags() & GAME_COMMAND_FLAG_GHOST)) + const bool isGhost = GetFlags() & GAME_COMMAND_FLAG_GHOST; + if (!isGhost) { ride_clear_for_construction(ride); ride_remove_peeps(ride); invalidate_test_results(ride); } - bool found = false; - TileElement* tileElement = map_get_first_element_at(_loc); + auto* entranceElement = FindEntranceElement( + _loc, _rideIndex, _stationNum, _isExit ? ENTRANCE_TYPE_RIDE_EXIT : ENTRANCE_TYPE_RIDE_ENTRANCE, isGhost); - do - { - if (tileElement == nullptr) - break; - - if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE) - continue; - - if (tileElement->GetRideIndex() != _rideIndex) - continue; - - if (tileElement->AsEntrance()->GetStationIndex() != _stationNum) - continue; - - if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && !tileElement->IsGhost()) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_PARK_ENTRANCE) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE && _isExit) - continue; - - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_RIDE_EXIT && !_isExit) - continue; - - found = true; - break; - } while (!(tileElement++)->IsLastForTile()); - - if (!found) + if (entranceElement == nullptr) { log_warning( "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, @@ -173,10 +141,10 @@ GameActions::Result::Ptr RideEntranceExitRemoveAction::Execute() const res->Position.z = tile_element_height(res->Position); footpath_queue_chain_reset(); - maze_entrance_hedge_replacement({ _loc, tileElement }); - footpath_remove_edges_at(_loc, tileElement); + maze_entrance_hedge_replacement({ _loc, entranceElement }); + footpath_remove_edges_at(_loc, entranceElement); - tile_element_remove(tileElement); + tile_element_remove(entranceElement); if (_isExit) {