Merge pull request #16062 from ZehMatt/refactor/map-iterators

Refactor tile map iterations
This commit is contained in:
ζeh Matt 2021-12-01 06:16:39 -08:00 committed by GitHub
commit ad5e54460f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 73 deletions

View File

@ -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<ride_id_t>(_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<TrackElement>(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<TileElement>());
}
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;

View File

@ -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<EntranceElement>(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<TileElement>());
}
}
}
}

View File

@ -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<TrackElement>(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);
}
}
}