OpenTTD Source  20240915-master-g3784a3d3d6
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 
43 inline 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 
197 inline Axis OtherAxis(Axis a)
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 
261 inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
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 */
ReverseDir
Direction ReverseDir(Direction d)
Return the reverse of a direction.
Definition: direction_func.h:54
direction_type.h
DiagDirDiff
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Definition: direction_type.h:95
DIR_END
@ DIR_END
Used to iterate.
Definition: direction_type.h:34
DiagDirToAxis
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
AXIS_END
@ AXIS_END
Used for iterations.
Definition: direction_type.h:119
AxisToDirection
Direction AxisToDirection(Axis a)
Converts an Axis to a Direction.
Definition: direction_func.h:249
ChangeDir
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition: direction_func.h:104
DirDifference
DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Definition: direction_func.h:68
ChangeDiagDir
DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
Definition: direction_func.h:149
IsValidDirection
bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Definition: direction_func.h:32
DiagDirDifference
DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
Definition: direction_func.h:131
ReverseDiagDir
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:118
DiagDirToDir
Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
Definition: direction_func.h:182
AxisToDiagDir
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Definition: direction_func.h:232
DirToDiagDir
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition: direction_func.h:166
DirDiff
DirDiff
Allow incrementing of Direction variables.
Definition: direction_type.h:58
IsValidAxis
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Definition: direction_func.h:43
IsDiagonalDirection
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
Definition: direction_func.h:273
IsValidDiagDirection
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:21
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:79
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
ChangeDirDiff
DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
Definition: direction_func.h:88
OtherAxis
Axis OtherAxis(Axis a)
Select the other axis as provided.
Definition: direction_func.h:197
XYNSToDiagDir
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition: direction_func.h:261