OpenTTD Source 20260531-master-g0e951f3528
direction_func.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef DIRECTION_FUNC_H
11#define DIRECTION_FUNC_H
12
13#include "direction_type.h"
14
22{
23 return d < DiagDirection::End;
24}
25
33{
34 return d < Direction::End;
35}
36
43inline bool IsValidAxis(Axis d)
44{
45 return d < Axis::End;
46}
47
55{
56 assert(IsValidDirection(d));
57 return static_cast<Direction>(4 ^ to_underlying(d));
58}
59
60
69{
70 assert(IsValidDirection(d0));
71 assert(IsValidDirection(d1));
72 /* Cast to uint so compiler can use bitmask. If the difference is negative
73 * and we used int instead of uint, further "+ 8" would have to be added. */
74 return static_cast<DirDiff>((to_underlying(d0) - to_underlying(d1)) % 8);
75}
76
89{
90 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
91 return static_cast<DirDiff>((to_underlying(d) + to_underlying(delta)) % 8);
92}
93
103
115{
116 assert(IsValidDirection(d));
117 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
118 return static_cast<Direction>((to_underlying(d) + to_underlying(delta)) % 8);
119}
120
121
129{
130 assert(IsValidDiagDirection(d));
131 return static_cast<DiagDirection>(2 ^ to_underlying(d));
132}
133
142{
143 assert(IsValidDiagDirection(d0));
144 assert(IsValidDiagDirection(d1));
145 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
146 return static_cast<DiagDirDiff>((to_underlying(d0) - to_underlying(d1)) % 4);
147}
148
160{
161 assert(IsValidDiagDirection(d));
162 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
163 return static_cast<DiagDirection>((to_underlying(d) + to_underlying(delta)) % 4);
164}
165
177{
178 assert(IsValidDirection(dir));
179 return static_cast<DiagDirection>(to_underlying(dir) >> 1);
180}
181
193{
194 assert(IsValidDiagDirection(dir));
195 return static_cast<Direction>(to_underlying(dir) * 2 + 1);
196}
197
198
208{
209 assert(IsValidAxis(a));
210 return static_cast<Axis>(to_underlying(a) ^ 1);
211}
212
213
225{
226 assert(IsValidDiagDirection(d));
227 return static_cast<Axis>(to_underlying(d) & 1);
228}
229
230
243{
244 assert(IsValidAxis(a));
245 return static_cast<DiagDirection>(2 - to_underlying(a));
246}
247
264
277{
278 assert(IsValidAxis(a));
279 return static_cast<Direction>(5 - 2 * to_underlying(a));
280}
281
289{
290 assert(IsValidAxis(xy));
291 return static_cast<DiagDirection>(to_underlying(xy) * 3 ^ ns * 2);
292}
293
301{
302 assert(IsValidDirection(dir));
303 return (to_underlying(dir) & 1) != 0;
304}
305
306#endif /* DIRECTION_FUNC_H */
DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
DiagDirections AxisToDiagDirs(Axis a)
Converts an Axis to DiagDirections.
Direction ReverseDir(Direction d)
Return the reverse of a direction.
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Direction AxisToDirection(Axis a)
Converts an Axis to a Direction.
Axis OtherAxis(Axis a)
Select the other axis as provided.
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
DirDiff LimitDirDiff(DirDiff d)
Limit a direction difference to up to 45 degrees.
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Different types to 'show' directions.
DirDiff
Enumeration for the difference between two directions.
@ Left45
Angle of 45 degrees left.
@ Reverse
One direction is the opposite of the other one.
@ Right45
Angle of 45 degrees right.
Direction
Defines the 8 directions on the map.
@ End
Used to iterate.
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Axis
Enumeration for the two axis X and Y.
@ X
The X axis.
@ End
End marker.
DiagDirection
Enumeration for diagonal directions.
@ SW
Southwest.
@ NW
Northwest.
@ End
Used for iterations.
@ NE
Northeast, upper right on your monitor.
@ SE
Southeast.
EnumBitSet< DiagDirection, uint8_t > DiagDirections
Bitset of DiagDirection elements.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21