From 763e9968cf11aff6f36f9f61404cfe8f674ef85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:24:04 +0300 Subject: [PATCH] Refactor FootpathIsZAndDirectionValid and its uses, more constness --- src/openrct2/entity/Staff.cpp | 8 +++--- src/openrct2/peep/GuestPathfinding.cpp | 37 +++++++++++++------------- src/openrct2/world/Footpath.cpp | 12 ++++----- src/openrct2/world/Footpath.h | 2 +- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/openrct2/entity/Staff.cpp b/src/openrct2/entity/Staff.cpp index 8e959f9ae0..fb88e5f494 100644 --- a/src/openrct2/entity/Staff.cpp +++ b/src/openrct2/entity/Staff.cpp @@ -183,7 +183,8 @@ bool Staff::CanIgnoreWideFlag(const CoordsXYZ& staffPos, TileElement* path) cons } /* test_element is a path */ - if (!FootpathIsZAndDirectionValid(test_element, adjacPos.z / kCoordsZStep, adjac_dir)) + const auto* adjacentPathElement = test_element->AsPath(); + if (!FootpathIsZAndDirectionValid(*adjacentPathElement, adjacPos.z / kCoordsZStep, adjac_dir)) continue; /* test_element is a connected path */ @@ -193,7 +194,7 @@ bool Staff::CanIgnoreWideFlag(const CoordsXYZ& staffPos, TileElement* path) cons pathcount++; } - if (test_element->AsPath()->IsWide()) + if (adjacentPathElement->IsWide()) { if (!widefound) { @@ -714,7 +715,8 @@ Direction Staff::MechanicDirectionPath(uint8_t validDirections, PathElement* pat } const auto goalPos = TileCoordsXYZ{ location }; - Direction pathfindDirection = PathFinding::ChooseDirection(TileCoordsXYZ{ NextLoc }, goalPos, *this, false, RideId::GetNull()); + Direction pathfindDirection = PathFinding::ChooseDirection( + TileCoordsXYZ{ NextLoc }, goalPos, *this, false, RideId::GetNull()); if (pathfindDirection == INVALID_DIRECTION) { /* Heuristic search failed for all directions. diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index 0b2b111f4d..d0cec670cc 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -140,14 +140,14 @@ namespace OpenRCT2::PathFinding } #pragma endregion - static TileElement* GetBannerOnPath(TileElement* pathElement) + static const TileElement* GetBannerOnPath(const TileElement* pathElement) { // This is an improved version of original. // That only checked for one fence in the way. if (pathElement->IsLastForTile()) return nullptr; - TileElement* bannerElement = pathElement + 1; + const TileElement* bannerElement = pathElement + 1; do { // Path on top, so no banners @@ -165,11 +165,11 @@ namespace OpenRCT2::PathFinding return nullptr; } - static int32_t BannerClearPathEdges(bool ignoreBanners, PathElement* pathElement, int32_t edges) + static int32_t BannerClearPathEdges(bool ignoreBanners, const PathElement* pathElement, int32_t edges) { if (ignoreBanners) return edges; - TileElement* bannerElement = GetBannerOnPath(reinterpret_cast(pathElement)); + const TileElement* bannerElement = GetBannerOnPath(reinterpret_cast(pathElement)); if (bannerElement != nullptr) { do @@ -183,7 +183,7 @@ namespace OpenRCT2::PathFinding /** * Gets the connected edges of a path that are permitted (i.e. no 'no entry' signs) */ - static int32_t PathGetPermittedEdges(bool ignoreBanners, PathElement* pathElement) + static int32_t PathGetPermittedEdges(bool ignoreBanners, const PathElement* pathElement) { return BannerClearPathEdges(ignoreBanners, pathElement, pathElement->GetEdgesAndCorners()) & 0x0F; } @@ -358,12 +358,13 @@ namespace OpenRCT2::PathFinding continue; if (nextTileElement->GetType() != TileElementType::Path) continue; - if (!FootpathIsZAndDirectionValid(nextTileElement, loc.z, chosenDirection)) + const auto* nextPathElement = nextTileElement->AsPath(); + if (!FootpathIsZAndDirectionValid(*nextPathElement, loc.z, chosenDirection)) continue; - if (nextTileElement->AsPath()->IsWide()) + if (nextPathElement->IsWide()) return PathSearchResult::Wide; // Only queue tiles that are connected to a ride are returned as ride queues. - if (nextTileElement->AsPath()->IsQueue() && !nextTileElement->AsPath()->GetRideIndex().IsNull()) + if (nextPathElement->IsQueue() && !nextPathElement->GetRideIndex().IsNull()) return PathSearchResult::RideQueue; return PathSearchResult::Other; @@ -452,12 +453,13 @@ namespace OpenRCT2::PathFinding break; case TileElementType::Path: { - if (!FootpathIsZAndDirectionValid(tileElement, loc.z, chosenDirection)) + const auto* pathElement = tileElement->AsPath(); + if (!FootpathIsZAndDirectionValid(*pathElement, loc.z, chosenDirection)) continue; if (tileElement->AsPath()->IsWide()) return PathSearchResult::Wide; - uint8_t edges = PathGetPermittedEdges(ignoreBanners, tileElement->AsPath()); + uint8_t edges = PathGetPermittedEdges(ignoreBanners, pathElement); edges &= ~(1 << DirectionReverse(chosenDirection)); loc.z = tileElement->BaseHeight; @@ -840,17 +842,17 @@ namespace OpenRCT2::PathFinding break; case TileElementType::Path: { + const auto* pathElement = tileElement->AsPath(); /* For peeps heading for a ride with a queue, the goal is the last * queue path. * Otherwise, peeps walk on path tiles to get to the goal. */ - - if (!FootpathIsZAndDirectionValid(tileElement, loc.z, testEdge)) + if (!FootpathIsZAndDirectionValid(*pathElement, loc.z, testEdge)) continue; // Path may be sloped, so set z to path base height. loc.z = tileElement->BaseHeight; - if (tileElement->AsPath()->IsWide()) + if (pathElement->IsWide()) { /* Check if staff can ignore this wide flag. */ if (staff == nullptr || !staff->CanIgnoreWideFlag(loc.ToCoordsXYZ(), tileElement)) @@ -863,7 +865,7 @@ namespace OpenRCT2::PathFinding searchResult = PathSearchResult::Thin; - uint8_t numEdges = std::popcount(tileElement->AsPath()->GetEdges()); + uint8_t numEdges = std::popcount(pathElement->GetEdges()); if (numEdges < 2) { @@ -875,15 +877,14 @@ namespace OpenRCT2::PathFinding } else { // numEdges == 2 - if (tileElement->AsPath()->IsQueue() - && tileElement->AsPath()->GetRideIndex() != state.queueRideIndex) + if (pathElement->IsQueue() && pathElement->GetRideIndex() != state.queueRideIndex) { - if (state.ignoreForeignQueues && !tileElement->AsPath()->GetRideIndex().IsNull()) + if (state.ignoreForeignQueues && !pathElement->GetRideIndex().IsNull()) { // Path is a queue we aren't interested in /* The rideIndex will be useful for * adding transport rides later. */ - rideIndex = tileElement->AsPath()->GetRideIndex(); + rideIndex = pathElement->GetRideIndex(); searchResult = PathSearchResult::RideQueue; } } diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index ab04c066f1..9a74e0d680 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -2329,14 +2329,14 @@ bool PathElement::IsLevelCrossing(const CoordsXY& coords) const return ride->GetRideTypeDescriptor().HasFlag(RtdFlag::supportsLevelCrossings); } -bool FootpathIsZAndDirectionValid(TileElement* tileElement, int32_t currentZ, int32_t currentDirection) +bool FootpathIsZAndDirectionValid(const PathElement& pathElement, int32_t currentZ, int32_t currentDirection) { - if (tileElement->AsPath()->IsSloped()) + if (pathElement.IsSloped()) { - int32_t slopeDirection = tileElement->AsPath()->GetSlopeDirection(); + int32_t slopeDirection = pathElement.GetSlopeDirection(); if (slopeDirection == currentDirection) { - if (currentZ != tileElement->BaseHeight) + if (currentZ != pathElement.BaseHeight) return false; } else @@ -2344,13 +2344,13 @@ bool FootpathIsZAndDirectionValid(TileElement* tileElement, int32_t currentZ, in slopeDirection = DirectionReverse(slopeDirection); if (slopeDirection != currentDirection) return false; - if (currentZ != tileElement->BaseHeight + 2) + if (currentZ != pathElement.BaseHeight + 2) return false; } } else { - if (currentZ != tileElement->BaseHeight) + if (currentZ != pathElement.BaseHeight) return false; } return true; diff --git a/src/openrct2/world/Footpath.h b/src/openrct2/world/Footpath.h index a0430b323e..9db7eedced 100644 --- a/src/openrct2/world/Footpath.h +++ b/src/openrct2/world/Footpath.h @@ -186,4 +186,4 @@ const FootpathRailingsObject* GetPathRailingsEntry(ObjectEntryIndex entryIndex); void FootpathQueueChainReset(); void FootpathQueueChainPush(RideId rideIndex); -bool FootpathIsZAndDirectionValid(TileElement* tileElement, int32_t currentZ, int32_t currentDirection); +bool FootpathIsZAndDirectionValid(const PathElement& tileElement, int32_t currentZ, int32_t currentDirection);