diff --git a/src/openrct2/actions/RideDemolishAction.cpp b/src/openrct2/actions/RideDemolishAction.cpp index 0237238879..6f78c70866 100644 --- a/src/openrct2/actions/RideDemolishAction.cpp +++ b/src/openrct2/actions/RideDemolishAction.cpp @@ -25,6 +25,7 @@ #include "../ui/WindowManager.h" #include "../world/Banner.h" #include "../world/Park.h" +#include "../world/TileElementsView.h" #include "MazeSetTrackAction.h" #include "TrackRemoveAction.h" @@ -194,58 +195,54 @@ money32 RideDemolishAction::DemolishTracks() const uint8_t oldpaused = gGamePaused; gGamePaused = 0; - tile_element_iterator it; - - tile_element_iterator_begin(&it); - while (tile_element_iterator_next(&it)) + for (TileCoordsXY tilePos = {}; tilePos.x < gMapSize; ++tilePos.x) { - if (it.element->GetType() != TILE_ELEMENT_TYPE_TRACK) - continue; - - if (it.element->AsTrack()->GetRideIndex() != static_cast(_rideIndex)) - continue; - - auto location = CoordsXYZD(TileCoordsXY(it.x, it.y).ToCoordsXY(), it.element->GetBaseZ(), it.element->GetDirection()); - auto type = it.element->AsTrack()->GetTrackType(); - - if (type != TrackElemType::Maze) + for (tilePos.y = 0; tilePos.y < gMapSize; ++tilePos.y) { - auto trackRemoveAction = TrackRemoveAction(type, it.element->AsTrack()->GetSequenceIndex(), location); - trackRemoveAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND); - - auto removRes = GameActions::ExecuteNested(&trackRemoveAction); - - if (removRes.Error != GameActions::Status::Ok) + const auto tileCoords = tilePos.ToCoordsXY(); + for (auto* trackElement : TileElementsView(tileCoords)) { - tile_element_remove(it.element); + if (trackElement->GetRideIndex() != _rideIndex) + continue; + + const auto location = CoordsXYZD(tileCoords, trackElement->GetBaseZ(), trackElement->GetDirection()); + const auto type = trackElement->GetTrackType(); + + if (type != TrackElemType::Maze) + { + auto trackRemoveAction = TrackRemoveAction(type, trackElement->GetSequenceIndex(), location); + trackRemoveAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND); + + auto removRes = GameActions::ExecuteNested(&trackRemoveAction); + + if (removRes.Error != GameActions::Status::Ok) + { + tile_element_remove(trackElement->as()); + } + else + { + refundPrice += removRes.Cost; + } + continue; + } + + static constexpr const CoordsXY DirOffsets[] = { + { 0, 0 }, + { 0, 16 }, + { 16, 16 }, + { 16, 0 }, + }; + for (Direction dir : ALL_DIRECTIONS) + { + const CoordsXYZ off = { DirOffsets[dir], 0 }; + money32 removePrice = MazeRemoveTrack({ location + off, dir }); + if (removePrice != MONEY32_UNDEFINED) + refundPrice += removePrice; + else + break; + } } - else - { - refundPrice += removRes.Cost; - } - - tile_element_iterator_restart_for_tile(&it); - continue; } - - static constexpr const CoordsXY DirOffsets[] = { - { 0, 0 }, - { 0, 16 }, - { 16, 16 }, - { 16, 0 }, - }; - - for (Direction dir : ALL_DIRECTIONS) - { - const CoordsXYZ off = { DirOffsets[dir], 0 }; - money32 removePrice = MazeRemoveTrack({ location + off, dir }); - if (removePrice != MONEY32_UNDEFINED) - refundPrice += removePrice; - else - break; - } - - tile_element_iterator_restart_for_tile(&it); } gGamePaused = oldpaused; diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 07e34c2217..b1386155df 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -56,6 +56,7 @@ #include "../world/MapAnimation.h" #include "../world/Park.h" #include "../world/Scenery.h" +#include "../world/TileElementsView.h" #include "CableLift.h" #include "RideAudio.h" #include "RideData.h" @@ -5687,17 +5688,21 @@ void determine_ride_entrance_and_exit_locations() void ride_clear_leftover_entrances(Ride* ride) { - tile_element_iterator it; - - tile_element_iterator_begin(&it); - while (tile_element_iterator_next(&it)) + for (TileCoordsXY tilePos = {}; tilePos.x < gMapSize; ++tilePos.x) { - if (it.element->GetType() == TILE_ELEMENT_TYPE_ENTRANCE - && it.element->AsEntrance()->GetEntranceType() != ENTRANCE_TYPE_PARK_ENTRANCE - && it.element->AsEntrance()->GetRideIndex() == ride->id) + for (tilePos.y = 0; tilePos.y < gMapSize; ++tilePos.y) { - tile_element_remove(it.element); - tile_element_iterator_restart_for_tile(&it); + for (auto* entrance : TileElementsView(tilePos.ToCoordsXY())) + { + const bool isRideEntranceExit = entrance->GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE + || entrance->GetEntranceType() == ENTRANCE_TYPE_RIDE_EXIT; + if (!isRideEntranceExit) + continue; + if (entrance->GetRideIndex() != ride->id) + continue; + + tile_element_remove(entrance->as()); + } } } } diff --git a/src/openrct2/ride/RideConstruction.cpp b/src/openrct2/ride/RideConstruction.cpp index 7416f7cd4e..af6015e6f7 100644 --- a/src/openrct2/ride/RideConstruction.cpp +++ b/src/openrct2/ride/RideConstruction.cpp @@ -36,6 +36,7 @@ #include "../world/MapAnimation.h" #include "../world/Park.h" #include "../world/Scenery.h" +#include "../world/TileElementsView.h" #include "Ride.h" #include "RideData.h" #include "Track.h" @@ -343,26 +344,20 @@ void ride_remove_peeps(Ride* ride) void ride_clear_blocked_tiles(Ride* ride) { - for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++) + for (TileCoordsXY tilePos = {}; tilePos.x < gMapSize; ++tilePos.x) { - for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) + for (tilePos.y = 0; tilePos.y < gMapSize; ++tilePos.y) { - auto element = map_get_first_element_at(TileCoordsXY{ x, y }); - if (element != nullptr) + for (auto* trackElement : TileElementsView(tilePos.ToCoordsXY())) { - do - { - if (element->GetType() == TILE_ELEMENT_TYPE_TRACK && element->AsTrack()->GetRideIndex() == ride->id) - { - // Unblock footpath element that is at same position - auto footpathElement = map_get_footpath_element( - TileCoordsXYZ{ x, y, element->base_height }.ToCoordsXYZ()); - if (footpathElement != nullptr) - { - footpathElement->AsPath()->SetIsBlockedByVehicle(false); - } - } - } while (!(element++)->IsLastForTile()); + // Unblock footpath element that is at same position + auto* footpathElement = map_get_footpath_element( + TileCoordsXYZ{ tilePos, trackElement->base_height }.ToCoordsXYZ()); + + if (footpathElement == nullptr) + continue; + + footpathElement->AsPath()->SetIsBlockedByVehicle(false); } } }