diff --git a/src/openrct2/actions/LargeSceneryRemoveAction.cpp b/src/openrct2/actions/LargeSceneryRemoveAction.cpp index 96b79650f7..9e35be4ed4 100644 --- a/src/openrct2/actions/LargeSceneryRemoveAction.cpp +++ b/src/openrct2/actions/LargeSceneryRemoveAction.cpp @@ -21,6 +21,9 @@ #include "../world/Park.h" #include "../world/SmallScenery.h" #include "../world/Sprite.h" +#include "../world/TileElementsView.h" + +using namespace OpenRCT2; LargeSceneryRemoveAction::LargeSceneryRemoveAction(const CoordsXYZD& location, uint16_t tileIndex) : _loc(location) @@ -59,7 +62,7 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Query() const res->Expenditure = ExpenditureType::Landscaping; res->Cost = 0; - TileElement* tileElement = FindLargeSceneryElement(); + TileElement* tileElement = FindLargeSceneryElement(_loc, _tileIndex); if (tileElement == nullptr) { log_warning("Invalid game command for scenery removal, x = %d, y = %d", _loc.x, _loc.y); @@ -132,7 +135,7 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const res->Expenditure = ExpenditureType::Landscaping; res->Cost = 0; - TileElement* tileElement = FindLargeSceneryElement(); + TileElement* tileElement = FindLargeSceneryElement(_loc, _tileIndex); if (tileElement == nullptr) { log_warning("Invalid game command for scenery removal, x = %d, y = %d", _loc.x, _loc.y); @@ -171,37 +174,13 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const } } - TileElement* sceneryElement = map_get_first_element_at(currentTile); - bool element_found = false; + auto* sceneryElement = FindLargeSceneryElement(currentTile, i); if (sceneryElement != nullptr) { - do - { - if (sceneryElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) - continue; - - if (sceneryElement->GetDirection() != _loc.direction) - continue; - - if (sceneryElement->AsLargeScenery()->GetSequenceIndex() != i) - continue; - - if (sceneryElement->GetBaseZ() != currentTile.z) - continue; - - // If we are removing ghost elements - if ((flags & GAME_COMMAND_FLAG_GHOST) && sceneryElement->IsGhost() == false) - continue; - - map_invalidate_tile_full(currentTile); - tile_element_remove(sceneryElement); - - element_found = true; - break; - } while (!(sceneryElement++)->IsLastForTile()); + map_invalidate_tile_full(currentTile); + tile_element_remove(sceneryElement); } - - if (element_found == false) + else { log_error("Tile not found when trying to remove element!"); } @@ -212,33 +191,25 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const return res; } -TileElement* LargeSceneryRemoveAction::FindLargeSceneryElement() const +TileElement* LargeSceneryRemoveAction::FindLargeSceneryElement(const CoordsXYZ& pos, int32_t sequenceIndex) const { - TileElement* tileElement = map_get_first_element_at(_loc); - if (tileElement == nullptr) - return nullptr; - - do + for (auto* sceneryElement : TileElementsView(pos)) { - if (tileElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY) - continue; - - if (tileElement->GetBaseZ() != _loc.z) - continue; - - if (tileElement->AsLargeScenery()->GetSequenceIndex() != _tileIndex) - continue; - - if (tileElement->GetDirection() != _loc.direction) - continue; - // If we are removing ghost elements - if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && tileElement->IsGhost() == false) + if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && sceneryElement->IsGhost() == false) continue; - return tileElement; + if (sceneryElement->GetDirection() != _loc.direction) + continue; - } while (!(tileElement++)->IsLastForTile()); + if (sceneryElement->GetSequenceIndex() != sequenceIndex) + continue; + + if (sceneryElement->GetBaseZ() != pos.z) + continue; + + return sceneryElement->as(); + } return nullptr; } diff --git a/src/openrct2/actions/LargeSceneryRemoveAction.h b/src/openrct2/actions/LargeSceneryRemoveAction.h index 8c58747296..6eb895a71e 100644 --- a/src/openrct2/actions/LargeSceneryRemoveAction.h +++ b/src/openrct2/actions/LargeSceneryRemoveAction.h @@ -30,5 +30,5 @@ public: GameActions::Result::Ptr Execute() const override; private: - TileElement* FindLargeSceneryElement() const; + TileElement* FindLargeSceneryElement(const CoordsXYZ& pos, int32_t sequenceIndex) const; };