From 4aa32fd9784f0ab4d7e99be248251371cdb129a3 Mon Sep 17 00:00:00 2001 From: Michael Steenbeek <1478678+Gymnasiast@users.noreply.github.com> Date: Sat, 11 Jul 2026 17:07:54 +0200 Subject: [PATCH] Migrate some tile iteration loops to TileElementsView (#26769) --- src/openrct2-ui/ride/Construction.cpp | 15 +- src/openrct2-ui/windows/Banner.cpp | 16 +- src/openrct2-ui/windows/Footpath.cpp | 70 ++++----- src/openrct2-ui/windows/TileInspector.cpp | 10 +- .../actions/scenery/WallPlaceAction.cpp | 6 +- .../actions/terraform/ClearAction.cpp | 13 +- .../actions/terraform/LandSetHeightAction.cpp | 15 +- .../actions/track/TrackRemoveAction.cpp | 147 +++++++----------- src/openrct2/entity/Balloon.cpp | 9 +- src/openrct2/entity/Guest.cpp | 7 +- src/openrct2/interface/Screenshot.cpp | 19 ++- .../paint/track/transport/Chairlift.cpp | 23 +-- src/openrct2/park/ParkFile.cpp | 13 +- src/openrct2/peep/GuestPathfinding.cpp | 11 +- src/openrct2/rct1/S4Importer.cpp | 49 +++--- src/openrct2/rct12/ScenarioPatcher.cpp | 13 +- src/openrct2/rct2/S6Importer.cpp | 14 +- src/openrct2/world/Banner.cpp | 14 +- 18 files changed, 175 insertions(+), 289 deletions(-) diff --git a/src/openrct2-ui/ride/Construction.cpp b/src/openrct2-ui/ride/Construction.cpp index ea30cca293..41ad29fa8b 100644 --- a/src/openrct2-ui/ride/Construction.cpp +++ b/src/openrct2-ui/ride/Construction.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include using namespace OpenRCT2::TrackMetadata; @@ -450,16 +451,10 @@ namespace OpenRCT2 if (MapIsLocationValid(nextLocation)) { // iterate over every element in the tile until we find what we want - auto* tileElement = MapGetFirstElementAt(nextLocation); - if (tileElement == nullptr) - continue; - do + for (auto* trackElement : TileElementsView(nextLocation)) { - if (tileElement->getType() != TileElementType::track) + if (trackElement->getBaseZ() != stationBaseZ) continue; - if (tileElement->getBaseZ() != stationBaseZ) - continue; - auto* trackElement = tileElement->asTrack(); if (trackElement->GetRideIndex() != gRideEntranceExitPlaceRideIndex) continue; if (trackElement->GetTrackType() == TrackElemType::maze) @@ -475,7 +470,7 @@ namespace OpenRCT2 gRideEntranceExitPlaceStationIndex = trackElement->GetStationIndex(); // get the ride entrance's side relative to the TrackElement - Direction direction = (DirectionReverse(entranceExitCoords.direction) - tileElement->getDirection()) & 3; + Direction direction = (DirectionReverse(entranceExitCoords.direction) - trackElement->getDirection()) & 3; const auto& ted = GetTrackElementDescriptor(trackElement->GetTrackType()); auto connectionSides = ted.sequenceData.sequences[trackElement->GetSequenceIndex()] .getEntranceConnectionSides(); @@ -487,7 +482,7 @@ namespace OpenRCT2 gRideEntranceExitPlaceDirection = entranceExitCoords.direction; return entranceExitCoords; } - } while (!(tileElement++)->isLastForTile()); + } } } gRideEntranceExitPlaceDirection = kInvalidDirection; diff --git a/src/openrct2-ui/windows/Banner.cpp b/src/openrct2-ui/windows/Banner.cpp index 873bd0e931..ccf363727a 100644 --- a/src/openrct2-ui/windows/Banner.cpp +++ b/src/openrct2-ui/windows/Banner.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include namespace OpenRCT2::Ui::Windows @@ -109,24 +110,13 @@ namespace OpenRCT2::Ui::Windows return nullptr; } - TileElement* tileElement = MapGetFirstElementAt(banner->position); - if (tileElement == nullptr) + for (auto* bannerElement : TileElementsView(banner->position)) { - return nullptr; - } - - do - { - auto* bannerElement = tileElement->asBanner(); - if (bannerElement == nullptr) - { - continue; - } if (bannerElement->GetIndex() == GetBannerIndex()) { return bannerElement; } - } while (!(tileElement++)->isLastForTile()); + } return nullptr; } diff --git a/src/openrct2-ui/windows/Footpath.cpp b/src/openrct2-ui/windows/Footpath.cpp index c3e920a9c5..a3c7a9d942 100644 --- a/src/openrct2-ui/windows/Footpath.cpp +++ b/src/openrct2-ui/windows/Footpath.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -1537,12 +1538,12 @@ namespace OpenRCT2::Ui::Windows * * rct2: 0x006A78EF */ - void FootpathRemoveTileElement(TileElement* tileElement) + void FootpathRemoveTileElement(PathElement* pathElement) { - auto z = tileElement->getBaseZ(); - if (tileElement->asPath()->IsSloped()) + auto z = pathElement->getBaseZ(); + if (pathElement->IsSloped()) { - uint8_t slopeDirection = tileElement->asPath()->GetSlopeDirection(); + uint8_t slopeDirection = pathElement->GetSlopeDirection(); slopeDirection = DirectionReverse(slopeDirection); if (slopeDirection == _footpathConstructDirection) { @@ -1552,16 +1553,16 @@ namespace OpenRCT2::Ui::Windows // Find a connected edge int32_t edge = DirectionReverse(_footpathConstructDirection); - if (!(tileElement->asPath()->GetEdges() & (1 << edge))) + if (!(pathElement->GetEdges() & (1 << edge))) { edge = (edge + 1) & 3; - if (!(tileElement->asPath()->GetEdges() & (1 << edge))) + if (!(pathElement->GetEdges() & (1 << edge))) { edge = (edge + 2) & 3; - if (!(tileElement->asPath()->GetEdges() & (1 << edge))) + if (!(pathElement->GetEdges() & (1 << edge))) { edge = (edge - 1) & 3; - if (!(tileElement->asPath()->GetEdges() & (1 << edge))) + if (!(pathElement->GetEdges() & (1 << edge))) { edge = DirectionReverse(edge); } @@ -1569,7 +1570,7 @@ namespace OpenRCT2::Ui::Windows } } - _footpathConstructFromPosition.z = tileElement->getBaseZ(); + _footpathConstructFromPosition.z = pathElement->getBaseZ(); auto action = GameActions::FootpathRemoveAction(_footpathConstructFromPosition); GameActions::Execute(&action, getGameState()); @@ -1586,9 +1587,8 @@ namespace OpenRCT2::Ui::Windows * * rct2: 0x006A7873 */ - TileElement* FootpathGetTileElementToRemove() + PathElement* FootpathGetTileElementToRemove() { - TileElement* tileElement; int32_t z, zLow; if (!MapIsLocationValid(_footpathConstructFromPosition)) @@ -1599,39 +1599,33 @@ namespace OpenRCT2::Ui::Windows z = std::min(255 * kCoordsZStep, _footpathConstructFromPosition.z); zLow = z - kPathHeightStep; - tileElement = MapGetFirstElementAt(_footpathConstructFromPosition); - do + for (auto* pathElement : TileElementsView(_footpathConstructFromPosition)) { - if (tileElement == nullptr) - break; - if (tileElement->getType() == TileElementType::path) + if (pathElement->getBaseZ() == z) { - if (tileElement->getBaseZ() == z) + if (pathElement->IsSloped()) { - if (tileElement->asPath()->IsSloped()) + if (DirectionReverse(pathElement->GetSlopeDirection()) != _footpathConstructDirection) { - if (DirectionReverse(tileElement->asPath()->GetSlopeDirection()) != _footpathConstructDirection) - { - continue; - } + continue; } - - return tileElement; } - if (tileElement->getBaseZ() == zLow) - { - if (!tileElement->asPath()->IsSloped()) - { - if ((tileElement->asPath()->GetSlopeDirection()) == _footpathConstructDirection) - { - continue; - } - } - return tileElement; - } + return pathElement; } - } while (!(tileElement++)->isLastForTile()); + if (pathElement->getBaseZ() == zLow) + { + if (!pathElement->IsSloped()) + { + if ((pathElement->GetSlopeDirection()) == _footpathConstructDirection) + { + continue; + } + } + + return pathElement; + } + } return nullptr; } @@ -1642,12 +1636,10 @@ namespace OpenRCT2::Ui::Windows */ void WindowFootpathRemove() { - TileElement* tileElement; - _windowFootpathCost = kMoney64Undefined; FootpathUpdateProvisional(); - tileElement = FootpathGetTileElementToRemove(); + auto* tileElement = FootpathGetTileElementToRemove(); if (tileElement != nullptr) { FootpathRemoveTileElement(tileElement); diff --git a/src/openrct2-ui/windows/TileInspector.cpp b/src/openrct2-ui/windows/TileInspector.cpp index 0a1ec56ed4..c9c89c57a8 100644 --- a/src/openrct2-ui/windows/TileInspector.cpp +++ b/src/openrct2-ui/windows/TileInspector.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -1615,13 +1616,8 @@ namespace OpenRCT2::Ui::Windows int32_t i = 0; char buffer[256]; - const TileElement* tileElement = MapGetFirstElementAt(_toolMap); - - do + for (const auto* tileElement : TileElementsView(_toolMap)) { - if (tileElement == nullptr) - break; - const bool selectedRow = i == windowTileInspectorSelectedIndex; const bool hoveredRow = i == _highlightedIndex; const char* typeName = ""; @@ -1751,7 +1747,7 @@ namespace OpenRCT2::Ui::Windows screenCoords.y -= kScrollableRowHeight; i++; - } while (!(tileElement++)->isLastForTile()); + } } void ClearClipboard() diff --git a/src/openrct2/actions/scenery/WallPlaceAction.cpp b/src/openrct2/actions/scenery/WallPlaceAction.cpp index d1ca1f8175..3dab732f35 100644 --- a/src/openrct2/actions/scenery/WallPlaceAction.cpp +++ b/src/openrct2/actions/scenery/WallPlaceAction.cpp @@ -25,6 +25,7 @@ #include "../../world/ConstructionClearance.h" #include "../../world/Map.h" #include "../../world/MapAnimation.h" +#include "../../world/TileElementsView.h" #include "../../world/Wall.h" #include "../../world/tile_element/LargeSceneryElement.h" #include "../../world/tile_element/PathElement.h" @@ -504,8 +505,7 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_CANT_BUILD_THIS_HERE, STR_OFF_EDGE_OF_MAP); } - TileElement* tileElement = MapGetFirstElementAt(_loc); - do + for (auto* tileElement : TileElementsView(_loc)) { if (tileElement == nullptr) break; @@ -584,7 +584,7 @@ namespace OpenRCT2::GameActions default: break; } - } while (!(tileElement++)->isLastForTile()); + } return Result(); } diff --git a/src/openrct2/actions/terraform/ClearAction.cpp b/src/openrct2/actions/terraform/ClearAction.cpp index e13ff91d41..08bd4abb07 100644 --- a/src/openrct2/actions/terraform/ClearAction.cpp +++ b/src/openrct2/actions/terraform/ClearAction.cpp @@ -15,6 +15,7 @@ #include "../../management/Finance.h" #include "../../world/Location.hpp" #include "../../world/Map.h" +#include "../../world/TileElementsView.h" #include "../../world/tile_element/LargeSceneryElement.h" #include "../../world/tile_element/SmallSceneryElement.h" #include "../GameActionRunner.h" @@ -239,16 +240,10 @@ namespace OpenRCT2::GameActions { for (int32_t x = 0; x < gameState.mapSize.x; x++) { - auto tileElement = MapGetFirstElementAt(TileCoordsXY{ x, y }); - do + for (auto* sceneryElement : TileElementsView(TileCoordsXY{ x, y })) { - if (tileElement == nullptr) - break; - if (tileElement->getType() == TileElementType::largeScenery) - { - tileElement->asLargeScenery()->SetIsAccounted(false); - } - } while (!(tileElement++)->isLastForTile()); + sceneryElement->SetIsAccounted(false); + } } } } diff --git a/src/openrct2/actions/terraform/LandSetHeightAction.cpp b/src/openrct2/actions/terraform/LandSetHeightAction.cpp index 2ab1bdc9ab..a8f5d4c095 100644 --- a/src/openrct2/actions/terraform/LandSetHeightAction.cpp +++ b/src/openrct2/actions/terraform/LandSetHeightAction.cpp @@ -250,19 +250,14 @@ namespace OpenRCT2::GameActions void LandSetHeightAction::SmallSceneryRemoval() const { - TileElement* tileElement = MapGetFirstElementAt(_coords); - do + for (auto* sceneryElement : TileElementsView(_coords)) { - if (tileElement == nullptr) - break; - if (tileElement->getType() != TileElementType::smallScenery) + if (_height > sceneryElement->clearanceHeight) continue; - if (_height > tileElement->clearanceHeight) + if (_height + 4 < sceneryElement->baseHeight) continue; - if (_height + 4 < tileElement->baseHeight) - continue; - TileElementRemove(tileElement--); - } while (!(tileElement++)->isLastForTile()); + TileElementRemove(reinterpret_cast(sceneryElement)); + } } StringId LandSetHeightAction::CheckRideSupports() const diff --git a/src/openrct2/actions/track/TrackRemoveAction.cpp b/src/openrct2/actions/track/TrackRemoveAction.cpp index f4ec856920..2de85b003c 100644 --- a/src/openrct2/actions/track/TrackRemoveAction.cpp +++ b/src/openrct2/actions/track/TrackRemoveAction.cpp @@ -19,6 +19,7 @@ #include "../../ride/ted/TrackElementDescriptor.h" #include "../../world/Footpath.h" #include "../../world/Map.h" +#include "../../world/TileElementsView.h" #include "../../world/tile_element/SurfaceElement.h" #include "../../world/tile_element/TrackElement.h" #include "../GameActionRunner.h" @@ -78,40 +79,33 @@ namespace OpenRCT2::GameActions auto comparableTrackType = normaliseTrackType(_trackType); - bool found = false; + TrackElement* foundElement = nullptr; bool isGhost = GetFlags().has(CommandFlag::ghost); - TileElement* tileElement = MapGetFirstElementAt(_origin); - do + for (auto* trackElement : TileElementsView(_origin)) { - if (tileElement == nullptr) - break; - - if (tileElement->getBaseZ() != _origin.z) + if (trackElement->getBaseZ() != _origin.z) continue; - if (tileElement->getType() != TileElementType::track) + if ((trackElement->getDirection()) != _origin.direction) continue; - if ((tileElement->getDirection()) != _origin.direction) + if (trackElement->GetSequenceIndex() != _sequence) continue; - if (tileElement->asTrack()->GetSequenceIndex() != _sequence) + if (trackElement->isGhost() != isGhost) continue; - if (tileElement->isGhost() != isGhost) - continue; - - auto tileTrackType = normaliseTrackType(tileElement->asTrack()->GetTrackType()); + auto tileTrackType = normaliseTrackType(trackElement->GetTrackType()); if (tileTrackType != comparableTrackType) continue; - found = true; + foundElement = trackElement; break; - } while (!(tileElement++)->isLastForTile()); + } - if (!found) + if (foundElement == nullptr) { LOG_ERROR( "Track Element not found. x = %d, y = %d, z = %d, d = %d, seq = %d.", _origin.x, _origin.y, _origin.z, @@ -119,14 +113,14 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_TRACK_ELEMENT_NOT_FOUND); } - if (tileElement->asTrack()->IsIndestructible()) + if (foundElement->IsIndestructible()) { return Result( Status::disallowed, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_YOU_ARE_NOT_ALLOWED_TO_REMOVE_THIS_SECTION); } - RideId rideIndex = tileElement->asTrack()->GetRideIndex(); - const auto trackType = tileElement->asTrack()->GetTrackType(); + RideId rideIndex = foundElement->GetRideIndex(); + const auto trackType = foundElement->GetTrackType(); auto ride = GetRide(rideIndex); if (ride == nullptr) @@ -141,7 +135,7 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_VALUE_OUT_OF_RANGE); } const auto& ted = GetTrackElementDescriptor(trackType); - auto sequenceIndex = tileElement->asTrack()->GetSequenceIndex(); + auto sequenceIndex = foundElement->GetSequenceIndex(); if (sequenceIndex >= ted.sequenceData.numSequences) { LOG_ERROR("Track block %d not found for track type %d.", sequenceIndex, trackType); @@ -150,7 +144,7 @@ namespace OpenRCT2::GameActions const auto& currentTrackBlock = ted.sequenceData.sequences[sequenceIndex].clearance; auto startLoc = _origin; - startLoc.direction = tileElement->getDirection(); + startLoc.direction = foundElement->getDirection(); auto rotatedTrack = CoordsXYZ{ CoordsXY{ currentTrackBlock.x, currentTrackBlock.y }.Rotate(startLoc.direction), currentTrackBlock.z }; @@ -175,36 +169,29 @@ namespace OpenRCT2::GameActions } MapInvalidateTileFull(mapLoc); - found = false; - tileElement = MapGetFirstElementAt(mapLoc); - do + foundElement = nullptr; + for (auto* trackElement : TileElementsView(mapLoc)) { - if (tileElement == nullptr) - break; - - if (tileElement->getBaseZ() != mapLoc.z) + if (trackElement->getBaseZ() != mapLoc.z) continue; - if (tileElement->getType() != TileElementType::track) + if (trackElement->getDirection() != _origin.direction) continue; - if (tileElement->getDirection() != _origin.direction) + if (trackElement->GetSequenceIndex() != i) continue; - if (tileElement->asTrack()->GetSequenceIndex() != i) + if (trackElement->GetTrackType() != trackType) continue; - if (tileElement->asTrack()->GetTrackType() != trackType) + if (trackElement->isGhost() != isGhost) continue; - if (tileElement->isGhost() != isGhost) - continue; - - found = true; + foundElement = trackElement; break; - } while (!(tileElement++)->isLastForTile()); + } - if (!found) + if (foundElement == nullptr) { LOG_ERROR( "Track Element not found. x = %d, y = %d, z = %d, d = %d, seq = %d.", mapLoc.x, mapLoc.y, mapLoc.z, @@ -212,8 +199,7 @@ namespace OpenRCT2::GameActions return Result(Status::unknown, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_TRACK_ELEMENT_NOT_FOUND); } - if (ted.sequenceData.sequences[0].flags.has(SequenceFlag::trackOrigin) - && (tileElement->asTrack()->GetSequenceIndex() == 0)) + if (ted.sequenceData.sequences[0].flags.has(SequenceFlag::trackOrigin) && (foundElement->GetSequenceIndex() == 0)) { const auto removeElementResult = TrackRemoveStationElement({ mapLoc, _origin.direction }, rideIndex, {}); if (!removeElementResult.Successful) @@ -229,7 +215,7 @@ namespace OpenRCT2::GameActions return Result(Status::unknown, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); } - int16_t _support_height = tileElement->baseHeight - surfaceElement->baseHeight; + int16_t _support_height = foundElement->baseHeight - surfaceElement->baseHeight; if (_support_height < 0) { _support_height = 10; @@ -262,40 +248,33 @@ namespace OpenRCT2::GameActions auto comparableTrackType = normaliseTrackType(_trackType); - bool found = false; + TrackElement* foundElement = nullptr; bool isGhost = GetFlags().has(CommandFlag::ghost); - TileElement* tileElement = MapGetFirstElementAt(_origin); - do + for (auto* trackElement : TileElementsView(_origin)) { - if (tileElement == nullptr) - break; - - if (tileElement->getBaseZ() != _origin.z) + if (trackElement->getBaseZ() != _origin.z) continue; - if (tileElement->getType() != TileElementType::track) + if ((trackElement->getDirection()) != _origin.direction) continue; - if ((tileElement->getDirection()) != _origin.direction) + if (trackElement->GetSequenceIndex() != _sequence) continue; - if (tileElement->asTrack()->GetSequenceIndex() != _sequence) + if (trackElement->isGhost() != isGhost) continue; - if (tileElement->isGhost() != isGhost) - continue; - - auto tileTrackType = normaliseTrackType(tileElement->asTrack()->GetTrackType()); + auto tileTrackType = normaliseTrackType(trackElement->GetTrackType()); if (tileTrackType != comparableTrackType) continue; - found = true; + foundElement = trackElement; break; - } while (!(tileElement++)->isLastForTile()); + } - if (!found) + if (foundElement == nullptr) { LOG_ERROR( "Track Element not found. x = %d, y = %d, z = %d, d = %d, seq = %d.", _origin.x, _origin.y, _origin.z, @@ -303,9 +282,9 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_TRACK_ELEMENT_NOT_FOUND); } - RideId rideIndex = tileElement->asTrack()->GetRideIndex(); - const auto trackType = tileElement->asTrack()->GetTrackType(); - bool isLiftHill = tileElement->asTrack()->HasChain(); + RideId rideIndex = foundElement->GetRideIndex(); + const auto trackType = foundElement->GetTrackType(); + bool isLiftHill = foundElement->HasChain(); auto ride = GetRide(rideIndex); if (ride == nullptr) @@ -314,7 +293,7 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_RIDE_NOT_FOUND); } const auto& ted = GetTrackElementDescriptor(trackType); - auto sequenceIndex = tileElement->asTrack()->GetSequenceIndex(); + auto sequenceIndex = foundElement->GetSequenceIndex(); if (sequenceIndex >= ted.sequenceData.numSequences) { LOG_ERROR("Track block %d not found for track type %d.", sequenceIndex, trackType); @@ -322,7 +301,7 @@ namespace OpenRCT2::GameActions } auto startLoc = _origin; - startLoc.direction = tileElement->getDirection(); + startLoc.direction = foundElement->getDirection(); const auto& currentTrackBlock = ted.sequenceData.sequences[sequenceIndex].clearance; auto rotatedTrackLoc = CoordsXYZ{ CoordsXY{ currentTrackBlock.x, currentTrackBlock.y }.Rotate(startLoc.direction), @@ -345,36 +324,29 @@ namespace OpenRCT2::GameActions MapInvalidateTileFull(mapLoc); - found = false; - tileElement = MapGetFirstElementAt(mapLoc); - do + foundElement = nullptr; + for (auto* trackElement : TileElementsView(mapLoc)) { - if (tileElement == nullptr) - break; - - if (tileElement->getBaseZ() != mapLoc.z) + if (trackElement->getBaseZ() != mapLoc.z) continue; - if (tileElement->getType() != TileElementType::track) + if (trackElement->getDirection() != _origin.direction) continue; - if (tileElement->getDirection() != _origin.direction) + if (trackElement->GetSequenceIndex() != i) continue; - if (tileElement->asTrack()->GetSequenceIndex() != i) + if (trackElement->GetTrackType() != trackType) continue; - if (tileElement->asTrack()->GetTrackType() != trackType) + if (trackElement->isGhost() != isGhost) continue; - if (tileElement->isGhost() != isGhost) - continue; - - found = true; + foundElement = trackElement; break; - } while (!(tileElement++)->isLastForTile()); + } - if (!found) + if (foundElement == nullptr) { LOG_ERROR( "Track Element not found. x = %d, y = %d, z = %d, d = %d, seq = %d.", mapLoc.x, mapLoc.y, mapLoc.z, @@ -382,8 +354,7 @@ namespace OpenRCT2::GameActions return Result(Status::unknown, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_TRACK_ELEMENT_NOT_FOUND); } - if (ted.sequenceData.sequences[0].flags.has(SequenceFlag::trackOrigin) - && (tileElement->asTrack()->GetSequenceIndex() == 0)) + if (ted.sequenceData.sequences[0].flags.has(SequenceFlag::trackOrigin) && (foundElement->GetSequenceIndex() == 0)) { const auto removeElementResult = TrackRemoveStationElement({ mapLoc, _origin.direction }, rideIndex, {}); if (!removeElementResult.Successful) @@ -399,7 +370,7 @@ namespace OpenRCT2::GameActions return Result(Status::unknown, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, STR_ERR_SURFACE_ELEMENT_NOT_FOUND); } - int16_t _support_height = tileElement->baseHeight - surfaceElement->baseHeight; + int16_t _support_height = foundElement->baseHeight - surfaceElement->baseHeight; if (_support_height < 0) { _support_height = 10; @@ -411,7 +382,7 @@ namespace OpenRCT2::GameActions // Don't do this if the ride is simulating and the tile is a ghost to prevent desyncs. if (ted.sequenceData.sequences[0].flags.has(SequenceFlag::trackOrigin) && (!GetFlags().has(CommandFlag::ghost) || (GetFlags().has(CommandFlag::trackDesign))) - && (tileElement->asTrack()->GetSequenceIndex() == 0)) + && (foundElement->GetSequenceIndex() == 0)) { const auto removeElementResult = TrackRemoveStationElement( { mapLoc, _origin.direction }, rideIndex, { CommandFlag::apply }); @@ -428,11 +399,11 @@ namespace OpenRCT2::GameActions InvalidateTestResults(*ride); FootpathQueueChainReset(); - if (!gameState.cheats.disableClearanceChecks || !(tileElement->isGhost())) + if (!gameState.cheats.disableClearanceChecks || !(foundElement->isGhost())) { - FootpathRemoveEdgesAt(mapLoc, tileElement); + FootpathRemoveEdgesAt(mapLoc, reinterpret_cast(foundElement)); } - TileElementRemove(tileElement); + TileElementRemove(reinterpret_cast(foundElement)); ride->validateStations(); if (!GetFlags().has(CommandFlag::ghost)) { diff --git a/src/openrct2/entity/Balloon.cpp b/src/openrct2/entity/Balloon.cpp index 6a4551a3bc..bc0a3984c5 100644 --- a/src/openrct2/entity/Balloon.cpp +++ b/src/openrct2/entity/Balloon.cpp @@ -18,6 +18,7 @@ #include "../ride/RideData.h" #include "../scenario/Scenario.h" #include "../world/Map.h" +#include "../world/TileElementsView.h" #include "../world/tile_element/TrackElement.h" #include "EntityRegistry.h" @@ -126,10 +127,7 @@ namespace OpenRCT2 bool Balloon::Collides() const { - const TileElement* tileElement = MapGetFirstElementAt(CoordsXY({ x, y })); - if (tileElement == nullptr) - return false; - do + for (auto* tileElement : TileElementsView(CoordsXY(x, y))) { // the balloon has height so we add some padding to prevent it clipping through things. int32_t balloon_top = z + kCoordsZStep * 2; @@ -161,8 +159,7 @@ namespace OpenRCT2 return true; } } - - } while (!(tileElement++)->isLastForTile()); + } return false; } } // namespace OpenRCT2 diff --git a/src/openrct2/entity/Guest.cpp b/src/openrct2/entity/Guest.cpp index 7f58c6b8f1..a7deb076bf 100644 --- a/src/openrct2/entity/Guest.cpp +++ b/src/openrct2/entity/Guest.cpp @@ -5033,10 +5033,7 @@ namespace OpenRCT2 }; maze_type mazeType = maze_type::invalid; - auto tileElement = MapGetFirstElementAt(targetLoc); - if (tileElement == nullptr) - return; - do + for (auto* tileElement : TileElementsView(targetLoc)) { if (stationBaseZ != tileElement->getBaseZ()) continue; @@ -5053,7 +5050,7 @@ namespace OpenRCT2 mazeType = maze_type::entrance_or_exit; break; } - } while (!(tileElement++)->isLastForTile()); + } switch (mazeType) { diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index 755bc215ac..f98ced4277 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -32,6 +32,7 @@ #include "../platform/Platform.h" #include "../world/Map.h" #include "../world/Park.h" +#include "../world/TileElementsView.h" #include "../world/Weather.h" #include "../world/tile_element/SurfaceElement.h" #include "Viewport.h" @@ -194,19 +195,17 @@ std::string ScreenshotDumpPNG(RenderTarget& rt) static int32_t GetHighestBaseClearanceZ(const CoordsXY& location, const bool useViewClipping) { int32_t z = 0; - auto element = MapGetFirstElementAt(location); - if (element != nullptr) + + for (const auto* tileElement : TileElementsView(location)) { - do + if (useViewClipping && (tileElement->getBaseZ() > gClipHeight * kCoordsZStep)) { - if (useViewClipping && (element->getBaseZ() > gClipHeight * kCoordsZStep)) - { - continue; - } - z = std::max(z, element->getBaseZ()); - z = std::max(z, element->getClearanceZ()); - } while (!(element++)->isLastForTile()); + continue; + } + z = std::max(z, tileElement->getBaseZ()); + z = std::max(z, tileElement->getClearanceZ()); } + return z; } diff --git a/src/openrct2/paint/track/transport/Chairlift.cpp b/src/openrct2/paint/track/transport/Chairlift.cpp index 4a8c69d928..e462d542e7 100644 --- a/src/openrct2/paint/track/transport/Chairlift.cpp +++ b/src/openrct2/paint/track/transport/Chairlift.cpp @@ -11,6 +11,7 @@ #include "../../../ride/RideData.h" #include "../../../ride/TrackPaint.h" #include "../../../world/Map.h" +#include "../../../world/TileElementsView.h" #include "../../../world/tile_element/TileElement.h" #include "../../../world/tile_element/TrackElement.h" #include "../../Paint.h" @@ -126,24 +127,16 @@ static void ChairliftPaintUtilDrawSupports(PaintSession& session, int32_t segmen static const TrackElement* ChairliftPaintUtilMapGetTrackElementAtFromRideFuzzy( int32_t x, int32_t y, int32_t z, const Ride& ride) { - const TileElement* tileElement = MapGetFirstElementAt(CoordsXY{ x, y }); - if (tileElement == nullptr) + for (auto* trackElement : TileElementsView(CoordsXY(x, y))) { - return nullptr; + if (trackElement->GetRideIndex() != ride.id) + continue; + if (trackElement->baseHeight != z && trackElement->baseHeight != z - 1) + continue; + + return trackElement; } - do - { - if (tileElement->getType() != TileElementType::track) - continue; - if (tileElement->GetRideIndex() != ride.id) - continue; - if (tileElement->baseHeight != z && tileElement->baseHeight != z - 1) - continue; - - return tileElement->asTrack(); - } while (!(tileElement++)->isLastForTile()); - return nullptr; } diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index a593062fab..74f852cad5 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -58,6 +58,7 @@ #include "../world/Map.h" #include "../world/Park.h" #include "../world/Scenery.h" +#include "../world/TileElementsView.h" #include "../world/Weather.h" #include "../world/tile_element/PathElement.h" #include "../world/tile_element/SmallSceneryElement.h" @@ -1279,22 +1280,14 @@ namespace OpenRCT2 { for (int32_t x = 0; x < gameState.mapSize.x; x++) { - TileElement* tileElement = MapGetFirstElementAt(TileCoordsXY{ x, y }); - if (tileElement == nullptr) - continue; - do + for (auto* trackElement : TileElementsView(TileCoordsXY{ x, y })) { - if (tileElement->getType() != TileElementType::track) - continue; - - auto* trackElement = tileElement->asTrack(); const auto* ride = GetRide(trackElement->GetRideIndex()); if (ride != nullptr) { trackElement->SetRideType(ride->type); } - - } while (!(tileElement++)->isLastForTile()); + } } } } diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index 84c2c395e3..16cd4ac15c 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -21,6 +21,7 @@ #include "../world/Entrance.h" #include "../world/Footpath.h" #include "../world/Map.h" +#include "../world/TileElementsView.h" #include "../world/Wall.h" #include "../world/tile_element/BannerElement.h" #include "../world/tile_element/EntranceElement.h" @@ -398,19 +399,13 @@ namespace OpenRCT2::PathFinding static PathSearchResult FootpathElementDestInDir( bool ignoreBanners, TileCoordsXYZ loc, Direction chosenDirection, RideId* outRideIndex, int32_t level) { - TileElement* tileElement; Direction direction; if (level > 25) return PathSearchResult::LimitReached; loc += TileDirectionDelta[chosenDirection]; - tileElement = MapGetFirstElementAt(loc); - if (tileElement == nullptr) - { - return PathSearchResult::Failed; - } - do + for (auto* tileElement : TileElementsView(loc)) { if (tileElement->isGhost()) continue; @@ -490,7 +485,7 @@ namespace OpenRCT2::PathFinding default: break; } - } while (!(tileElement++)->isLastForTile()); + } return PathSearchResult::Failed; } diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 71f5b9cd89..73fc907885 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -60,6 +60,7 @@ #include "../world/MapAnimation.h" #include "../world/Park.h" #include "../world/Scenery.h" +#include "../world/TileElementsView.h" #include "../world/TilePointerIndex.hpp" #include "../world/Wall.h" #include "../world/Weather.h" @@ -2657,37 +2658,31 @@ namespace OpenRCT2::RCT1 { for (int32_t y = 0; y < Limits::kMaxMapSize; y++) { - TileElement* tileElement = MapGetFirstElementAt(TileCoordsXY{ x, y }); - if (tileElement == nullptr) - continue; - do + for (auto* trackElement : TileElementsView(TileCoordsXY(x, y))) { - if (tileElement->getType() == TileElementType::track) + // Lift hill tops are the only pieces present in RCT1 that can count as a block brake. + if (!trackElement->HasChain()) + continue; + + auto trackType = trackElement->GetTrackType(); + switch (trackType) { - // Lift hill tops are the only pieces present in RCT1 that can count as a block brake. - if (!tileElement->asTrack()->HasChain()) + case TrackElemType::up25ToFlat: + case TrackElemType::up60ToFlat: + case TrackElemType::diagUp25ToFlat: + case TrackElemType::diagUp60ToFlat: + break; + default: continue; - - auto trackType = tileElement->asTrack()->GetTrackType(); - switch (trackType) - { - case TrackElemType::up25ToFlat: - case TrackElemType::up60ToFlat: - case TrackElemType::diagUp25ToFlat: - case TrackElemType::diagUp60ToFlat: - break; - default: - continue; - } - - RideId rideIndex = tileElement->asTrack()->GetRideIndex(); - auto ride = GetRide(rideIndex); - if (ride != nullptr) - { - ride->numBlockBrakes++; - } } - } while (!(tileElement++)->isLastForTile()); + + RideId rideIndex = trackElement->GetRideIndex(); + auto ride = GetRide(rideIndex); + if (ride != nullptr) + { + ride->numBlockBrakes++; + } + } } } } diff --git a/src/openrct2/rct12/ScenarioPatcher.cpp b/src/openrct2/rct12/ScenarioPatcher.cpp index 9d5e9410b6..a3131ca495 100644 --- a/src/openrct2/rct12/ScenarioPatcher.cpp +++ b/src/openrct2/rct12/ScenarioPatcher.cpp @@ -28,6 +28,7 @@ #include "../world/Footpath.h" #include "../world/Location.hpp" #include "../world/Map.h" +#include "../world/TileElementsView.h" #include "../world/tile_element/EntranceElement.h" #include "../world/tile_element/PathElement.h" #include "../world/tile_element/Slope.h" @@ -341,21 +342,13 @@ static void ApplyTrackTypeFixes(const json_t& trackTilesFixes) for (const auto& tile : coordinatesVector) { - auto* tileElement = MapGetFirstElementAt(tile); - if (tileElement == nullptr) - continue; - - do + for (auto* trackElement : TileElementsView(tile)) { - if (tileElement->getType() != TileElementType::track) - continue; - - auto* trackElement = tileElement->asTrack(); if (trackElement->GetTrackType() != fromTrackType) continue; trackElement->SetTrackType(destinationTrackType); - } while (!(tileElement++)->isLastForTile()); + } } } } diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 2d7d8c53ba..2109a99706 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -50,6 +50,7 @@ #include "../world/MapAnimation.h" #include "../world/Park.h" #include "../world/Scenery.h" +#include "../world/TileElementsView.h" #include "../world/TilePointerIndex.hpp" #include "../world/Weather.h" #include "../world/tile_element/BannerElement.h" @@ -1032,22 +1033,17 @@ namespace OpenRCT2::RCT2 { const CoordsXYZ coords{ mapAnimation.x, mapAnimation.y, mapAnimation.BaseZ * kCoordsZStep }; const TileCoordsXYZ tileCoords{ coords }; - TileElement* tileElement = MapGetFirstElementAt(tileCoords); - if (tileElement == nullptr) - { - continue; - } - do + for (auto* wallElement : TileElementsView(tileCoords)) { - if (tileElement->getType() != TileElementType::wall || tileElement->baseHeight != tileCoords.z) + if (wallElement->baseHeight != tileCoords.z) { continue; } - tileElement->asWall()->SetIsAnimating(true); + wallElement->SetIsAnimating(true); MapAnimations::MarkTileForUpdate(tileCoords); - } while (!(tileElement++)->isLastForTile()); + } break; } } diff --git a/src/openrct2/world/Banner.cpp b/src/openrct2/world/Banner.cpp index b9c432b11c..1ded4f50e3 100644 --- a/src/openrct2/world/Banner.cpp +++ b/src/openrct2/world/Banner.cpp @@ -104,25 +104,19 @@ void Banner::formatTextTo(Formatter& ft) const */ static RideId BannerGetRideIndexAt(const CoordsXYZ& bannerCoords) { - TileElement* tileElement = MapGetFirstElementAt(bannerCoords); RideId resultRideIndex = RideId::GetNull(); - if (tileElement == nullptr) - return resultRideIndex; - do + for (auto* trackElement : TileElementsView(bannerCoords)) { - if (tileElement->getType() != TileElementType::track) - continue; - - RideId rideIndex = tileElement->asTrack()->GetRideIndex(); + RideId rideIndex = trackElement->GetRideIndex(); auto ride = GetRide(rideIndex); if (ride == nullptr || ride->getRideTypeDescriptor().flags.has(RtdFlag::isShopOrFacility)) continue; - if ((tileElement->getClearanceZ()) + (4 * kCoordsZStep) <= bannerCoords.z) + if ((trackElement->getClearanceZ()) + (4 * kCoordsZStep) <= bannerCoords.z) continue; resultRideIndex = rideIndex; - } while (!(tileElement++)->isLastForTile()); + } return resultRideIndex; }