OpenTTD Source 20241224-master-gee860a5c8e
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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef DIRECTION_FUNC_H
11#define DIRECTION_FUNC_H
12
13#include "direction_type.h"
14
22{
23 return d < DIAGDIR_END;
24}
25
33{
34 return d < DIR_END;
35}
36
43inline bool IsValidAxis(Axis d)
44{
45 return d < AXIS_END;
46}
47
55{
56 assert(IsValidDirection(d));
57 return (Direction)(4 ^ 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>((static_cast<uint>(d0) - static_cast<uint>(d1)) % 8);
75}
76
89{
90 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
91 return static_cast<DirDiff>((static_cast<uint>(d) + static_cast<uint>(delta)) % 8);
92}
93
105{
106 assert(IsValidDirection(d));
107 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
108 return static_cast<Direction>((static_cast<uint>(d) + static_cast<uint>(delta)) % 8);
109}
110
111
119{
120 assert(IsValidDiagDirection(d));
121 return (DiagDirection)(2 ^ d);
122}
123
132{
133 assert(IsValidDiagDirection(d0));
134 assert(IsValidDiagDirection(d1));
135 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
136 return (DiagDirDiff)((uint)(d0 - d1) % 4);
137}
138
150{
151 assert(IsValidDiagDirection(d));
152 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
153 return static_cast<DiagDirection>((static_cast<uint>(d) + static_cast<uint>(delta)) % 4);
154}
155
167{
168 assert(IsValidDirection(dir));
169 return (DiagDirection)(dir >> 1);
170}
171
183{
184 assert(IsValidDiagDirection(dir));
185 return (Direction)(dir * 2 + 1);
186}
187
188
198{
199 assert(IsValidAxis(a));
200 return (Axis)(a ^ 1);
201}
202
203
215{
216 assert(IsValidDiagDirection(d));
217 return (Axis)(d & 1);
218}
219
220
233{
234 assert(IsValidAxis(a));
235 return (DiagDirection)(2 - a);
236}
237
250{
251 assert(IsValidAxis(a));
252 return (Direction)(5 - 2 * a);
253}
254
262{
263 assert(IsValidAxis(xy));
264 return (DiagDirection)(xy * 3 ^ ns * 2);
265}
266
274{
275 assert(IsValidDirection(dir));
276 return (dir & 1) != 0;
277}
278
279#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.
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.
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.
Direction
Defines the 8 directions on the map.
@ DIR_END
Used to iterate.
Axis
Allow incrementing of DiagDirDiff variables.
@ AXIS_END
Used for iterations.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_END
Used for iterations.
DiagDirDiff
Enumeration for the difference between to DiagDirection.
DirDiff
Allow incrementing of Direction variables.