Files
OpenRCT2-Web/src/openrct2/world/Direction.hpp
T
Richard Fine 41b1cd01bc 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.
2026-10-11 11:26:19 +02:00

86 lines
2.2 KiB
C++

/*****************************************************************************
* 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 <cstdint>
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;
}