From 41b1cd01bc266f00bd5786d805fd5a72510e1ad6 Mon Sep 17 00:00:00 2001 From: Richard Fine Date: Sun, 11 Oct 2026 05:26:19 -0400 Subject: [PATCH] Refactor: more use of the `Direction` type and helpers (#27237) Advance adoption of the 'Direction' type for cardinal directions: * Split `Direction` and its helpers out to their own header (`Direction.h`) * Replace loops that iterate all four directions with range-based loops * Replace some math with calls to helper functions. I also introduced a new `DirectionRotate()` function, a generalisation of `DirectionNext()`/`DirectionPrev()`. * Replace `uint8_t`/`int32_t` with `Direction` in some function signatures and field types, if the thing being passed/stored is supposed to be a cardinal direction In the process I also fixed two defects in the code: * The bitmask used to select the direction from a TD6EntranceElement was too wide - it was grabbing the low 4 bits, but only needs the low 2. * `CoordsXYZD.toTileCentre()` was taking the tile corner and adding a `CoordsXYZD` value to it, to add half-tile offsets to X and Y. So, it's more correct to add a `CoordsXY` value instead, and skip the unnecessary Z and D dimensions. --- src/openrct2-ui/windows/RideConstruction.cpp | 4 +- .../actions/park/ParkEntrancePlaceAction.cpp | 20 ++-- .../actions/park/ParkEntranceRemoveAction.cpp | 2 +- .../actions/peep/StaffHireNewAction.cpp | 6 +- .../actions/scenery/WallPlaceAction.cpp | 2 +- .../actions/track/TrackPlaceAction.cpp | 2 +- src/openrct2/entity/Guest.cpp | 2 +- src/openrct2/entity/JumpingFountain.cpp | 2 +- src/openrct2/paint/VirtualFloor.cpp | 4 +- .../paint/tile_element/Paint.Tunnel.cpp | 2 +- .../paint/tile_element/Paint.Tunnel.h | 4 +- src/openrct2/paint/track/TrackPaintGeneric.h | 2 +- src/openrct2/rct2/RCT2.h | 2 +- src/openrct2/ride/TrackDesign.cpp | 2 +- src/openrct2/ride/TrackIteration.cpp | 2 +- src/openrct2/ride/Vehicle.Station.cpp | 2 +- .../scenes/editor/EditorController.cpp | 2 +- .../bindings/world/ScPathNavigator.cpp | 2 +- src/openrct2/world/Direction.hpp | 85 ++++++++++++++ src/openrct2/world/Entrance.cpp | 4 +- src/openrct2/world/Footpath.cpp | 10 +- src/openrct2/world/Footpath.h | 2 +- src/openrct2/world/Location.hpp | 107 ++++-------------- src/openrct2/world/Map.cpp | 6 +- src/openrct2/world/Map.h | 4 +- src/openrct2/world/MapAnimation.cpp | 2 +- src/openrct2/world/QuarterTile.cpp | 2 +- src/openrct2/world/QuarterTile.h | 3 +- src/openrct2/world/TileInspector.cpp | 2 +- src/openrct2/world/Wall.cpp | 2 +- src/openrct2/world/Wall.h | 2 +- .../world/tile_element/BannerElement.cpp | 4 +- .../world/tile_element/BannerElement.h | 6 +- src/openrct2/world/tile_element/PathElement.h | 2 +- .../world/tile_element/TileElementBase.cpp | 2 +- 35 files changed, 163 insertions(+), 146 deletions(-) create mode 100644 src/openrct2/world/Direction.hpp diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index 7d02a74e30..1d6a54ecb7 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -3467,7 +3467,7 @@ namespace OpenRCT2::Ui::Windows PathElement* pathsByDir[kNumOrthogonalDirections]; bool keepOrientation = false; - for (int8_t i = 0; i < kNumOrthogonalDirections; i++) + for (Direction i : kAllDirections) { const auto testLoc = CoordsXYZ{ *mapCoords + CoordsDirectionDelta[i], z }; if (!MapIsLocationOwned(testLoc)) @@ -3509,7 +3509,7 @@ namespace OpenRCT2::Ui::Windows if (!keepOrientation) { - for (int8_t i = 0; i < kNumOrthogonalDirections; i++) + for (Direction i : kAllDirections) { if (pathsByDir[i] != nullptr) { diff --git a/src/openrct2/actions/park/ParkEntrancePlaceAction.cpp b/src/openrct2/actions/park/ParkEntrancePlaceAction.cpp index a3292a3e4f..45f9979cd1 100644 --- a/src/openrct2/actions/park/ParkEntrancePlaceAction.cpp +++ b/src/openrct2/actions/park/ParkEntrancePlaceAction.cpp @@ -94,12 +94,12 @@ namespace OpenRCT2::GameActions { if (index == 1) { - entranceLoc += CoordsDirectionDelta[(_loc.direction - 1) & 0x3]; + entranceLoc += CoordsDirectionDelta[DirectionPrev(_loc.direction)]; } else if (index == 2) { - entranceLoc.x += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].x * 2; - entranceLoc.y += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].y * 2; + entranceLoc.x += CoordsDirectionDelta[DirectionNext(_loc.direction)].x * 2; + entranceLoc.y += CoordsDirectionDelta[DirectionNext(_loc.direction)].y * 2; } if (auto res2 = MapCanConstructAt({ entranceLoc, zLow, zHigh }, { 0b1111, 0 }); res2.error != Status::ok) @@ -143,13 +143,13 @@ namespace OpenRCT2::GameActions { if (index == ParkEntranceSequence::left) { - entranceLoc.x += CoordsDirectionDelta[(_loc.direction - 1) & 0x3].x; - entranceLoc.y += CoordsDirectionDelta[(_loc.direction - 1) & 0x3].y; + entranceLoc.x += CoordsDirectionDelta[DirectionPrev(_loc.direction)].x; + entranceLoc.y += CoordsDirectionDelta[DirectionPrev(_loc.direction)].y; } else if (index == ParkEntranceSequence::right) { - entranceLoc.x += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].x * 2; - entranceLoc.y += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].y * 2; + entranceLoc.x += CoordsDirectionDelta[DirectionNext(_loc.direction)].x * 2; + entranceLoc.y += CoordsDirectionDelta[DirectionNext(_loc.direction)].y * 2; } if (!flags.has(CommandFlag::ghost)) @@ -208,12 +208,12 @@ namespace OpenRCT2::GameActions { if (index == 1) { - entranceLoc += CoordsDirectionDelta[(_loc.direction - 1) & 0x3]; + entranceLoc += CoordsDirectionDelta[DirectionPrev(_loc.direction)]; } else if (index == 2) { - entranceLoc.x += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].x * 2; - entranceLoc.y += CoordsDirectionDelta[(_loc.direction + 1) & 0x3].y * 2; + entranceLoc.x += CoordsDirectionDelta[DirectionNext(_loc.direction)].x * 2; + entranceLoc.y += CoordsDirectionDelta[DirectionNext(_loc.direction)].y * 2; } if (!MapCheckCapacityAndReorganise(entranceLoc, numTiles)) { diff --git a/src/openrct2/actions/park/ParkEntranceRemoveAction.cpp b/src/openrct2/actions/park/ParkEntranceRemoveAction.cpp index bb6ffcf2ae..fc26b7d9f7 100644 --- a/src/openrct2/actions/park/ParkEntranceRemoveAction.cpp +++ b/src/openrct2/actions/park/ParkEntranceRemoveAction.cpp @@ -80,7 +80,7 @@ namespace OpenRCT2::GameActions return Result(Status::invalidParameters, STR_CANT_REMOVE_THIS, kStringIdNone); } - auto direction = (park.entrances[entranceIndex].direction - 1) & 3; + auto direction = DirectionPrev(park.entrances[entranceIndex].direction); // Centre (sign) ParkEntranceRemoveSegment(_loc); diff --git a/src/openrct2/actions/peep/StaffHireNewAction.cpp b/src/openrct2/actions/peep/StaffHireNewAction.cpp index 820f7de0a5..b601188b9a 100644 --- a/src/openrct2/actions/peep/StaffHireNewAction.cpp +++ b/src/openrct2/actions/peep/StaffHireNewAction.cpp @@ -279,11 +279,7 @@ namespace OpenRCT2::GameActions { auto rand = ScenarioRandMax(static_cast(park.entrances.size())); const auto& entrance = park.entrances[rand]; - auto dir = entrance.direction; - newLocation = entrance; - // TODO: Replace with CoordsDirectionDelta - newLocation.x += 16 + ((dir & 1) == 0 ? ((dir & 2) ? 32 : -32) : 0); - newLocation.y += 16 + ((dir & 1) == 1 ? ((dir & 2) ? -32 : 32) : 0); + newLocation = entrance + CoordsDirectionDelta[entrance.direction] + CoordsXY{ 16, 16 }; } else { diff --git a/src/openrct2/actions/scenery/WallPlaceAction.cpp b/src/openrct2/actions/scenery/WallPlaceAction.cpp index b115245cf3..6500438972 100644 --- a/src/openrct2/actions/scenery/WallPlaceAction.cpp +++ b/src/openrct2/actions/scenery/WallPlaceAction.cpp @@ -513,7 +513,7 @@ namespace OpenRCT2::GameActions continue; if (elementType == TileElementType::wall) { - int32_t direction = tileElement->getDirection(); + Direction direction = tileElement->getDirection(); if (_edge == direction) { auto res = Result(Status::noClearance, STR_CANT_BUILD_THIS_HERE, kStringIdNone); diff --git a/src/openrct2/actions/track/TrackPlaceAction.cpp b/src/openrct2/actions/track/TrackPlaceAction.cpp index 74762378d5..923da9531e 100644 --- a/src/openrct2/actions/track/TrackPlaceAction.cpp +++ b/src/openrct2/actions/track/TrackPlaceAction.cpp @@ -513,7 +513,7 @@ namespace OpenRCT2::GameActions uint8_t intersectingDirections = ted.sequenceData.sequences[blockIndex].allowedWallEdges; intersectingDirections ^= 0x0F; intersectingDirections = rol4(intersectingDirections, _origin.direction); - for (int32_t i = 0; i < kNumOrthogonalDirections; i++) + for (Direction i : kAllDirections) { if (intersectingDirections & (1 << i)) { diff --git a/src/openrct2/entity/Guest.cpp b/src/openrct2/entity/Guest.cpp index 295d8a5e15..fd89d06d3c 100644 --- a/src/openrct2/entity/Guest.cpp +++ b/src/openrct2/entity/Guest.cpp @@ -4908,7 +4908,7 @@ namespace OpenRCT2 uint8_t hedges[4]{ 0xFF, 0xFF, 0xFF, 0xFF }; uint8_t openCount = 0; uint8_t mazeReverseLastEdge = DirectionReverse(mazeLastEdge); - for (uint8_t i = 0; i < kNumOrthogonalDirections; ++i) + for (Direction i : kAllDirections) { if (!(mazeEntry & (1 << kMazeCurrentDirectionToOpenHedge[var37 / 4][i])) && i != mazeReverseLastEdge) { diff --git a/src/openrct2/entity/JumpingFountain.cpp b/src/openrct2/entity/JumpingFountain.cpp index 43fbf85cd2..580ef0b77d 100644 --- a/src/openrct2/entity/JumpingFountain.cpp +++ b/src/openrct2/entity/JumpingFountain.cpp @@ -98,7 +98,7 @@ namespace OpenRCT2 { case Pattern::cyclicSquares: // 0, 1, 2, 3 - for (int32_t i = 0; i < kNumOrthogonalDirections; i++) + for (Direction i : kAllDirections) { create( newType, { newLoc + _fountainDirectionsPositive[i], newZ }, _fountainDirections[i], diff --git a/src/openrct2/paint/VirtualFloor.cpp b/src/openrct2/paint/VirtualFloor.cpp index d796f9dee4..dfc0d42eed 100644 --- a/src/openrct2/paint/VirtualFloor.cpp +++ b/src/openrct2/paint/VirtualFloor.cpp @@ -316,9 +316,9 @@ void VirtualFloorPaint(PaintSession& session) // Try the four tiles next to us for the same parameters as above, // if our parameters differ we set an edge towards that tile - for (uint8_t i = 0; i < kNumOrthogonalDirections; i++) + for (Direction i : kAllDirections) { - uint8_t effectiveRotation = (kNumOrthogonalDirections + i - direction) % kNumOrthogonalDirections; + Direction effectiveRotation = (kNumOrthogonalDirections + i - direction) % kNumOrthogonalDirections; CoordsXY theirLocation = session.MapPosition + scenery_half_tile_offsets[effectiveRotation]; bool theyAreOccupied; diff --git a/src/openrct2/paint/tile_element/Paint.Tunnel.cpp b/src/openrct2/paint/tile_element/Paint.Tunnel.cpp index 38e12f95d8..2352e0e62a 100644 --- a/src/openrct2/paint/tile_element/Paint.Tunnel.cpp +++ b/src/openrct2/paint/tile_element/Paint.Tunnel.cpp @@ -39,7 +39,7 @@ void PaintUtilSetVerticalTunnel(PaintSession& session, uint16_t height) session.VerticalTunnelHeight = height / 16; } -void PaintUtilPushTunnelRotated(PaintSession& session, uint8_t direction, uint16_t height, TunnelType type) +void PaintUtilPushTunnelRotated(PaintSession& session, Direction direction, uint16_t height, TunnelType type) { if (direction & 1) { diff --git a/src/openrct2/paint/tile_element/Paint.Tunnel.h b/src/openrct2/paint/tile_element/Paint.Tunnel.h index 3d869f8635..5a73d9746d 100644 --- a/src/openrct2/paint/tile_element/Paint.Tunnel.h +++ b/src/openrct2/paint/tile_element/Paint.Tunnel.h @@ -83,7 +83,7 @@ TunnelType GetTunnelType(TunnelGroup tunnelGroup, TunnelSubType tunnelSubType); void PaintUtilPushTunnelLeft(PaintSession& session, uint16_t height, TunnelType type); void PaintUtilPushTunnelRight(PaintSession& session, uint16_t height, TunnelType type); void PaintUtilSetVerticalTunnel(PaintSession& session, uint16_t height); -void PaintUtilPushTunnelRotated(PaintSession& session, uint8_t direction, uint16_t height, TunnelType type); +void PaintUtilPushTunnelRotated(PaintSession& session, Direction direction, uint16_t height, TunnelType type); inline void PaintUtilPushTunnelLeft( PaintSession& session, uint16_t height, TunnelGroup tunnelGroup, TunnelSubType tunnelSubType) @@ -98,7 +98,7 @@ inline void PaintUtilPushTunnelRight( } inline void PaintUtilPushTunnelRotated( - PaintSession& session, uint8_t direction, uint16_t height, TunnelGroup tunnelGroup, TunnelSubType tunnelSubType) + PaintSession& session, Direction direction, uint16_t height, TunnelGroup tunnelGroup, TunnelSubType tunnelSubType) { PaintUtilPushTunnelRotated(session, direction, height, GetTunnelType(tunnelGroup, tunnelSubType)); } diff --git a/src/openrct2/paint/track/TrackPaintGeneric.h b/src/openrct2/paint/track/TrackPaintGeneric.h index 5ee1a888e5..a57e31e028 100644 --- a/src/openrct2/paint/track/TrackPaintGeneric.h +++ b/src/openrct2/paint/track/TrackPaintGeneric.h @@ -27,7 +27,7 @@ namespace OpenRCT2 { const size_t directionOffset = sequenceCount * spriteCount; uint64_t spriteMap = 0; - for (size_t direction = 0; direction < kNumOrthogonalDirections; direction++) + for (Direction direction : kAllDirections) { for (size_t sequence = 0; sequence < sequenceCount; sequence++) { diff --git a/src/openrct2/rct2/RCT2.h b/src/openrct2/rct2/RCT2.h index 5d1a5db7c2..294b22e839 100644 --- a/src/openrct2/rct2/RCT2.h +++ b/src/openrct2/rct2/RCT2.h @@ -324,7 +324,7 @@ namespace OpenRCT2::RCT2 constexpr Direction GetDirection() const { - return (direction & 0b00001111); + return direction & 0b00000011; } constexpr bool IsExit() const diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index 1c5eccfb78..1f8d15b2ff 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -2162,7 +2162,7 @@ void TrackDesignDrawPreview(TrackDesign& td, TrackDesignPreviewBuffer& pixels, b drawingEngine->BeginDraw(); const ScreenCoordsXY offset = { size_x / 2, size_y / 2 }; - for (Direction direction = 0; direction < kNumOrthogonalDirections; direction++) + for (Direction direction : kAllDirections) { view.viewPos = Translate3DTo2DWithZ(direction, centre) - offset; view.rotation = direction; diff --git a/src/openrct2/ride/TrackIteration.cpp b/src/openrct2/ride/TrackIteration.cpp index cff59f5c88..4a27facff2 100644 --- a/src/openrct2/ride/TrackIteration.cpp +++ b/src/openrct2/ride/TrackIteration.cpp @@ -284,7 +284,7 @@ namespace OpenRCT2 int32_t y = input->y; int32_t OriginZ = inputElement->getBaseZ(); - uint8_t rotation = inputElement->getDirection(); + Direction rotation = inputElement->getDirection(); CoordsXY coords = { x, y }; CoordsXY trackCoordOffset = { trackCoordinate.x, trackCoordinate.y }; diff --git a/src/openrct2/ride/Vehicle.Station.cpp b/src/openrct2/ride/Vehicle.Station.cpp index abe93d2084..09a8a51a3d 100644 --- a/src/openrct2/ride/Vehicle.Station.cpp +++ b/src/openrct2/ride/Vehicle.Station.cpp @@ -165,7 +165,7 @@ static bool ride_station_can_depart_synchronised(const Ride& ride, StationIndex * is found we allow for space between that and the next. */ - int32_t direction = tileElement->getDirectionWithOffset(1); + Direction direction = tileElement->getDirectionWithOffset(1); constexpr uint8_t maxCheckDistance = kRideAdjacencyCheckDistance; uint8_t spaceBetween = maxCheckDistance; diff --git a/src/openrct2/scenes/editor/EditorController.cpp b/src/openrct2/scenes/editor/EditorController.cpp index 3e601e451c..8d02f9314c 100644 --- a/src/openrct2/scenes/editor/EditorController.cpp +++ b/src/openrct2/scenes/editor/EditorController.cpp @@ -183,7 +183,7 @@ namespace OpenRCT2::Editor for (const auto& parkEntrance : gameState.park.entrances) { - int32_t direction = DirectionReverse(parkEntrance.direction); + Direction direction = DirectionReverse(parkEntrance.direction); switch (FootpathIsConnectedToMapEdge(parkEntrance, direction, 0)) { diff --git a/src/openrct2/scripting/bindings/world/ScPathNavigator.cpp b/src/openrct2/scripting/bindings/world/ScPathNavigator.cpp index 6de0cdd8a5..40ee40cd58 100644 --- a/src/openrct2/scripting/bindings/world/ScPathNavigator.cpp +++ b/src/openrct2/scripting/bindings/world/ScPathNavigator.cpp @@ -208,7 +208,7 @@ namespace OpenRCT2::Scripting JSValue result = JS_NewArray(ctx); int32_t resultIndex = 0; - for (int32_t direction = 0; direction < 4; direction++) + for (Direction direction : kAllDirections) { if (!(edges & (1 << direction))) continue; diff --git a/src/openrct2/world/Direction.hpp b/src/openrct2/world/Direction.hpp new file mode 100644 index 0000000000..a99257b2aa --- /dev/null +++ b/src/openrct2/world/Direction.hpp @@ -0,0 +1,85 @@ +/***************************************************************************** + * Copyright (c) 2014-2026 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include + +constexpr auto kNumOrthogonalDirections = 4; + +/** + * Cardinal directions are represented by the Direction type. It has four + * possible values: + * 0 is X-decreasing (West) + * 1 is Y-increasing (North) + * 2 is X-increasing (East) + * 3 is Y-decreasing (South) + * Direction is not used to model up/down, or diagonal directions. + */ +using Direction = uint8_t; + +const Direction kInvalidDirection = 0xFF; + +/** + * Array of all valid cardinal directions, to make it easy to write range-based for loops like: + * for (Direction d : kAllDirections) + */ +constexpr Direction kAllDirections[] = { + 0, + 1, + 2, + 3, +}; + +/** + * Given a direction, return the direction that points the other way, + * on the same axis. + */ +inline constexpr Direction DirectionReverse(Direction dir) +{ + return dir ^ 2; +} + +inline constexpr bool DirectionValid(Direction dir) +{ + return dir < kNumOrthogonalDirections; +} + +/** + * Rotate a direction by the given number of 90-degree increments. Positive values + * rotate clockwise, negative values rotate counterclockwise. + */ +inline constexpr Direction DirectionRotate(Direction dir, int rotations) +{ + return (dir + rotations) & 0x03; +} + +/** + * Given a direction, return the next clockwise cardinal direction, wrapping around if necessary. + */ +inline constexpr Direction DirectionNext(Direction dir) +{ + return DirectionRotate(dir, 1); +} + +/** + * Given a direction, return the next counter-clockwise cardinal direction, wrapping around if necessary. + */ +inline constexpr Direction DirectionPrev(Direction dir) +{ + return DirectionRotate(dir, -1); +} + +/* + * Flips the X axis so 1 and 3 are swapped 0 and 2 will stay the same. + */ +inline constexpr Direction DirectionFlipXAxis(Direction direction) +{ + return (direction * 3) % 4; +} diff --git a/src/openrct2/world/Entrance.cpp b/src/openrct2/world/Entrance.cpp index e5aa5b2d31..fd52ac078e 100644 --- a/src/openrct2/world/Entrance.cpp +++ b/src/openrct2/world/Entrance.cpp @@ -123,7 +123,7 @@ money64 OpenRCT2::RideEntranceExitPlaceGhost( */ void MazeEntranceHedgeReplacement(const CoordsXYE& entrance) { - int32_t direction = entrance.element->getDirection(); + Direction direction = entrance.element->getDirection(); auto hedgePos = entrance + CoordsDirectionDelta[direction]; int32_t z = entrance.element->getBaseZ(); RideId rideIndex = entrance.element->asEntrance()->getRideIndex(); @@ -160,7 +160,7 @@ void MazeEntranceHedgeReplacement(const CoordsXYE& entrance) */ void MazeEntranceHedgeRemoval(const CoordsXYE& entrance) { - int32_t direction = entrance.element->getDirection(); + Direction direction = entrance.element->getDirection(); auto hedgePos = entrance + CoordsDirectionDelta[direction]; int32_t z = entrance.element->getBaseZ(); RideId rideIndex = entrance.element->asEntrance()->getRideIndex(); diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index e80953be6c..cc43899da0 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -219,9 +219,9 @@ namespace OpenRCT2 std::get<0>(tileElements) = { initialTileElement, footpathPos }; int32_t z = initialTileElement->getBaseZ(); - for (int32_t initialDirection = 0; initialDirection < kNumOrthogonalDirections; initialDirection++) + for (Direction initialDirection : kAllDirections) { - int32_t direction = initialDirection; + Direction direction = initialDirection; auto currentPos = footpathPos + CoordsDirectionDelta[direction]; std::get<1>(tileElements) = { @@ -1552,7 +1552,7 @@ namespace OpenRCT2 if (tileElement->asPath()->isQueue()) { FootpathQueueChainPush(tileElement->asPath()->getRideIndex()); - for (int32_t direction = 0; direction < kNumOrthogonalDirections; direction++) + for (Direction direction : kAllDirections) { if (tileElement->asPath()->getEdges() & (1 << direction)) { @@ -1764,7 +1764,7 @@ namespace OpenRCT2 FootpathUpdateQueueEntranceBanner(footpathPos, tileElement); - for (uint8_t direction = 0; direction < kNumOrthogonalDirections; direction++) + for (Direction direction : kAllDirections) { int32_t z1 = tileElement->baseHeight; if (tileElement->getType() == TileElementType::path) @@ -1890,7 +1890,7 @@ namespace OpenRCT2 return ride->getRideTypeDescriptor().flags.has(RtdFlag::supportsLevelCrossings); } - bool FootpathIsZAndDirectionValid(const PathElement& pathElement, int32_t currentZ, int32_t currentDirection) + bool FootpathIsZAndDirectionValid(const PathElement& pathElement, int32_t currentZ, Direction currentDirection) { if (pathElement.isSloped()) { diff --git a/src/openrct2/world/Footpath.h b/src/openrct2/world/Footpath.h index a1bde622ab..3e0d21fb76 100644 --- a/src/openrct2/world/Footpath.h +++ b/src/openrct2/world/Footpath.h @@ -200,7 +200,7 @@ namespace OpenRCT2 void FootpathQueueChainReset(); void FootpathQueueChainPush(RideId rideIndex); int32_t FootpathQueueCountConnections(const CoordsXY& position, const PathElement& pathElement); - bool FootpathIsZAndDirectionValid(const PathElement& tileElement, int32_t currentZ, int32_t currentDirection); + bool FootpathIsZAndDirectionValid(const PathElement& tileElement, int32_t currentZ, Direction currentDirection); FootpathPlacementResult FootpathGetOnTerrainPlacement(const TileCoordsXY& location); FootpathPlacementResult FootpathGetOnTerrainPlacement(const SurfaceElement& surfaceElement); diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index ff86e54e27..7f366f7a9d 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -11,6 +11,7 @@ #include "../core/CoordsRange.hpp" #include "../core/Numerics.hpp" +#include "../world/Direction.hpp" #include "../world/MapLimits.h" #include @@ -18,8 +19,6 @@ constexpr int16_t kLocationNull = -32768; constexpr int32_t kCoordsNull = 0xFFFF8000; -constexpr auto kNumOrthogonalDirections = 4; - /** * Tile coordinates use 1 x/y increment per tile and 1 z increment per step. * Regular ('big', 'sprite') coordinates use 32 x/y increments per tile and 8 z increments per step. @@ -453,89 +452,6 @@ struct TileCoordsXYRangedZ : public TileCoordsXY } }; -/** - * Cardinal directions are represented by the Direction type. It has four - * possible values: - * 0 is X-decreasing - * 1 is Y-increasing - * 2 is X-increasing - * 3 is Y-decreasing - * Direction is not used to model up/down, or diagonal directions. - */ -using Direction = uint8_t; - -const Direction kInvalidDirection = 0xFF; - -/** - * Array of all valid cardinal directions, to make it easy to write range-based for loops like: - * for (Direction d : kAllDirections) - */ -constexpr Direction kAllDirections[] = { - 0, - 1, - 2, - 3, -}; - -/** - * Given a direction, return the direction that points the other way, - * on the same axis. - */ -inline constexpr Direction DirectionReverse(Direction dir) -{ - return dir ^ 2; -} - -inline constexpr bool DirectionValid(Direction dir) -{ - return dir < kNumOrthogonalDirections; -} - -/** - * Given a direction, return the next cardinal direction, wrapping around if necessary. - * (TODO: Figure out if this is CW or CCW) - */ -inline constexpr Direction DirectionNext(Direction dir) -{ - return (dir + 1) & 0x03; -} - -/** - * Given a direction, return the previous cardinal direction, wrapping around if necessary. - * (TODO: Figure out if this is CW or CCW) - */ -inline constexpr Direction DirectionPrev(Direction dir) -{ - return (dir - 1) & 0x03; -} - -/** - * Given two positions, return the cardinal direction which is closest to the direction from 'from' to 'to'. - */ -inline constexpr Direction DirectionFromTo(const CoordsXY& from, const CoordsXY& to) -{ - int16_t x_diff = to.x - from.x; - int16_t y_diff = to.y - from.y; - - int16_t abs_x = x_diff < 0 ? -x_diff : x_diff; - int16_t abs_y = y_diff < 0 ? -y_diff : y_diff; - - if (abs_x <= abs_y) - { - return y_diff < 0 ? 3 : 1; - } - - return x_diff < 0 ? 0 : 2; -} - -/* - * Flips the X axis so 1 and 3 are swapped 0 and 2 will stay the same. - */ -inline constexpr Direction DirectionFlipXAxis(Direction direction) -{ - return (direction * 3) % 4; -} - struct CoordsXYZD : public CoordsXYZ { Direction direction{}; @@ -605,7 +521,7 @@ struct CoordsXYZD : public CoordsXYZ constexpr CoordsXYZD toTileCentre() const { - return toTileStart() + CoordsXYZD{ kCoordsXYHalfTile, kCoordsXYHalfTile, 0, 0 }; + return toTileStart() + CoordsXY{ kCoordsXYHalfTile, kCoordsXYHalfTile }; } }; @@ -668,6 +584,25 @@ struct TileCoordsXYZD : public TileCoordsXYZ } }; +/** + * Given two positions, return the cardinal direction which is closest to the direction from 'from' to 'to'. + */ +inline constexpr Direction DirectionFromTo(const CoordsXY& from, const CoordsXY& to) +{ + int16_t x_diff = to.x - from.x; + int16_t y_diff = to.y - from.y; + + int16_t abs_x = x_diff < 0 ? -x_diff : x_diff; + int16_t abs_y = y_diff < 0 ? -y_diff : y_diff; + + if (abs_x <= abs_y) + { + return y_diff < 0 ? 3 : 1; + } + + return x_diff < 0 ? 0 : 2; +} + /** * Represents a rectangular range of the map using regular coordinates (32 per tile). */ diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index df3676501b..ee4f5b2275 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -418,7 +418,7 @@ namespace OpenRCT2 return nullptr; } - BannerElement* MapGetBannerElementAt(const CoordsXYZ& bannerPos, uint8_t position) + BannerElement* MapGetBannerElementAt(const CoordsXYZ& bannerPos, Direction position) { const auto bannerTilePos = TileCoordsXYZ{ bannerPos }; for (auto* element : TileElementsView(bannerPos)) @@ -688,7 +688,7 @@ namespace OpenRCT2 * Checks if the tile at coordinate at height counts as connected. * @return 1 if connected, 0 otherwise */ - bool MapCoordIsConnected(const TileCoordsXYZ& loc, uint8_t faceDirection) + bool MapCoordIsConnected(const TileCoordsXYZ& loc, Direction faceDirection) { TileElement* tileElement = MapGetFirstElementAt(loc); @@ -700,7 +700,7 @@ namespace OpenRCT2 if (tileElement->getType() != TileElementType::path) continue; - uint8_t slopeDirection = tileElement->asPath()->getSlopeDirection(); + Direction slopeDirection = tileElement->asPath()->getSlopeDirection(); if (tileElement->asPath()->isSloped()) { diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h index 95782aca19..1585a6eda1 100644 --- a/src/openrct2/world/Map.h +++ b/src/openrct2/world/Map.h @@ -74,7 +74,7 @@ namespace OpenRCT2 TileElement* MapGetFirstTileElementWithBaseHeightBetween(const TileCoordsXYRangedZ& loc, TileElementType type); void MapSetTileElement(const TileCoordsXY& tilePos, TileElement* elements); int32_t MapHeightFromSlope(const CoordsXY& coords, int32_t slopeDirection, bool isSloped); - BannerElement* MapGetBannerElementAt(const CoordsXYZ& bannerPos, uint8_t position); + BannerElement* MapGetBannerElementAt(const CoordsXYZ& bannerPos, Direction position); SurfaceElement* MapGetSurfaceElementAt(const TileCoordsXY& coords); SurfaceElement* MapGetSurfaceElementAt(const CoordsXY& coords); PathElement* MapGetPathElementAt(const TileCoordsXYZ& loc); @@ -86,7 +86,7 @@ namespace OpenRCT2 EntranceElement* MapGetRideExitElementAt(const CoordsXYZ& exitCoords, bool ghost); uint8_t MapGetHighestLandHeight(const MapRange& range); uint8_t MapGetLowestLandHeight(const MapRange& range); - bool MapCoordIsConnected(const TileCoordsXYZ& loc, uint8_t faceDirection); + bool MapCoordIsConnected(const TileCoordsXYZ& loc, Direction faceDirection); void MapUpdatePathWideFlags(); bool MapIsLocationValid(const CoordsXY& coords); bool MapIsEdge(const CoordsXY& coords); diff --git a/src/openrct2/world/MapAnimation.cpp b/src/openrct2/world/MapAnimation.cpp index 42716a01fc..5561a17ba5 100644 --- a/src/openrct2/world/MapAnimation.cpp +++ b/src/openrct2/world/MapAnimation.cpp @@ -166,7 +166,7 @@ static std::optional UpdateSmallSceneryAnimation( // Don't apply anything to peeps when the scenery is a ghost. if (!scenery.isGhost() && !(getGameState().currentTicks & 0x3FF)) { - const int32_t direction = scenery.getDirection(); + const Direction direction = scenery.getDirection(); auto quad = EntityTileList(CoordsXY{ loc.x, loc.y } - CoordsDirectionDelta[direction]); for (auto peep : quad) { diff --git a/src/openrct2/world/QuarterTile.cpp b/src/openrct2/world/QuarterTile.cpp index d2fcf9ea98..5efc785d62 100644 --- a/src/openrct2/world/QuarterTile.cpp +++ b/src/openrct2/world/QuarterTile.cpp @@ -13,7 +13,7 @@ #include "Location.hpp" // Rotate both of the values amount -const QuarterTile QuarterTile::Rotate(uint8_t amount) const +const QuarterTile QuarterTile::Rotate(Direction amount) const { switch (amount) { diff --git a/src/openrct2/world/QuarterTile.h b/src/openrct2/world/QuarterTile.h index 7f05aa39ae..eb041e29a3 100644 --- a/src/openrct2/world/QuarterTile.h +++ b/src/openrct2/world/QuarterTile.h @@ -11,6 +11,7 @@ #include +using Direction = uint8_t; struct TileCornersZ; class QuarterTile @@ -30,7 +31,7 @@ public: } // Rotate both of the values amount. Returns new RValue QuarterTile - const QuarterTile Rotate(uint8_t amount) const; + const QuarterTile Rotate(Direction amount) const; uint8_t GetBaseQuarterOccupied() const; uint8_t GetZQuarterOccupied() const; TileCornersZ GetQuarterHeights(int32_t height) const; diff --git a/src/openrct2/world/TileInspector.cpp b/src/openrct2/world/TileInspector.cpp index 05a4ce84eb..75079b2ece 100644 --- a/src/openrct2/world/TileInspector.cpp +++ b/src/openrct2/world/TileInspector.cpp @@ -219,7 +219,7 @@ namespace OpenRCT2::TileInspector case TileElementType::path: if (tileElement->asPath()->isSloped()) { - newRotation = (tileElement->asPath()->getSlopeDirection() + 1) & kTileElementDirectionMask; + newRotation = DirectionNext(tileElement->asPath()->getSlopeDirection()); tileElement->asPath()->setSlopeDirection(newRotation); } pathEdges = tileElement->asPath()->getEdges(); diff --git a/src/openrct2/world/Wall.cpp b/src/openrct2/world/Wall.cpp index 87b3a9c6f4..fe6a1282a6 100644 --- a/src/openrct2/world/Wall.cpp +++ b/src/openrct2/world/Wall.cpp @@ -121,7 +121,7 @@ uint8_t GetWallSlopeFromEdgeSlope(uint8_t Slope, uint8_t Edge) * * rct2: 0x006E59DC */ -bool WallInTheWay(const CoordsXYRangedZ& fencePos, int32_t direction) +bool WallInTheWay(const CoordsXYRangedZ& fencePos, Direction direction) { TileElement* tileElement = MapGetFirstElementAt(fencePos); if (tileElement == nullptr) diff --git a/src/openrct2/world/Wall.h b/src/openrct2/world/Wall.h index 2c07af2046..df93a56bad 100644 --- a/src/openrct2/world/Wall.h +++ b/src/openrct2/world/Wall.h @@ -31,4 +31,4 @@ void WallRemoveIntersectingWalls(const CoordsXYRangedZ& wallPos, Direction direc uint8_t GetWallSlopeFromEdgeSlope(uint8_t Slope, uint8_t Edge); -bool WallInTheWay(const CoordsXYRangedZ& fencePos, int32_t direction); +bool WallInTheWay(const CoordsXYRangedZ& fencePos, Direction direction); diff --git a/src/openrct2/world/tile_element/BannerElement.cpp b/src/openrct2/world/tile_element/BannerElement.cpp index 04be2cd5c2..f79e0c4343 100644 --- a/src/openrct2/world/tile_element/BannerElement.cpp +++ b/src/openrct2/world/tile_element/BannerElement.cpp @@ -31,12 +31,12 @@ namespace OpenRCT2 index = newIndex; } - uint8_t BannerElement::getPosition() const + Direction BannerElement::getPosition() const { return position; } - void BannerElement::setPosition(uint8_t newPosition) + void BannerElement::setPosition(Direction newPosition) { position = newPosition; } diff --git a/src/openrct2/world/tile_element/BannerElement.h b/src/openrct2/world/tile_element/BannerElement.h index 24c71c1d48..c312cf548e 100644 --- a/src/openrct2/world/tile_element/BannerElement.h +++ b/src/openrct2/world/tile_element/BannerElement.h @@ -23,7 +23,7 @@ namespace OpenRCT2 private: BannerIndex index; // 5 - uint8_t position; // 7 + Direction position; // 7 uint8_t allowedEdges; // 8 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-private-field" @@ -36,8 +36,8 @@ namespace OpenRCT2 BannerIndex getIndex() const; void setIndex(BannerIndex newIndex); - uint8_t getPosition() const; - void setPosition(uint8_t newPosition); + Direction getPosition() const; + void setPosition(Direction newPosition); uint8_t getAllowedEdges() const; void setAllowedEdges(uint8_t newEdges); diff --git a/src/openrct2/world/tile_element/PathElement.h b/src/openrct2/world/tile_element/PathElement.h index 33455651f4..58bd4711c9 100644 --- a/src/openrct2/world/tile_element/PathElement.h +++ b/src/openrct2/world/tile_element/PathElement.h @@ -62,7 +62,7 @@ namespace OpenRCT2 uint8_t additions; // 9 (0 means no addition) uint8_t edgesAndCorners; // 10 (edges in lower 4 bits, corners in upper 4) FootpathElementFlags flags2; // 11 - uint8_t slopeDirection; // 12 + Direction slopeDirection; // 12 union { uint8_t additionStatus; // 13, only used for litter bins diff --git a/src/openrct2/world/tile_element/TileElementBase.cpp b/src/openrct2/world/tile_element/TileElementBase.cpp index de8ac7d27a..1c791a12db 100644 --- a/src/openrct2/world/tile_element/TileElementBase.cpp +++ b/src/openrct2/world/tile_element/TileElementBase.cpp @@ -47,7 +47,7 @@ namespace OpenRCT2 Direction TileElementBase::getDirectionWithOffset(uint8_t offset) const { - return ((this->type & kTileElementDirectionMask) + offset) & kTileElementDirectionMask; + return DirectionRotate(getDirection(), offset); } bool TileElementBase::isLastForTile() const