OpenTTD Source  12.0-beta2
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 
21 static inline bool IsValidDiagDirection(DiagDirection d)
22 {
23  return d < DIAGDIR_END;
24 }
25 
32 static inline bool IsValidDirection(Direction d)
33 {
34  return d < DIR_END;
35 }
36 
43 static inline bool IsValidAxis(Axis d)
44 {
45  return d < AXIS_END;
46 }
47 
54 static inline Direction ReverseDir(Direction d)
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 (DirDiff)((uint)(d0 - d1) % 8);
75 }
76 
88 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
89 {
90  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
91  return (DirDiff)((uint)(d + delta) % 8);
92 }
93 
104 static inline Direction ChangeDir(Direction d, DirDiff delta)
105 {
106  assert(IsValidDirection(d));
107  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
108  return (Direction)((uint)(d + 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 (DiagDirection)((uint)(d + 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 static 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 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
262 {
263  assert(IsValidAxis(xy));
264  return (DiagDirection)(xy * 3 ^ ns * 2);
265 }
266 
273 static inline bool IsDiagonalDirection(Direction dir)
274 {
275  assert(IsValidDirection(dir));
276  return (dir & 1) != 0;
277 }
278 
279 #endif /* DIRECTION_FUNC_H */
AxisToDirection
static Direction AxisToDirection(Axis a)
Converts an Axis to a Direction.
Definition: direction_func.h:249
direction_type.h
IsValidAxis
static bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Definition: direction_func.h:43
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
ChangeDir
static Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition: direction_func.h:104
OtherAxis
static Axis OtherAxis(Axis a)
Select the other axis as provided.
Definition: direction_func.h:197
DiagDirDiff
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Definition: direction_type.h:104
ChangeDirDiff
static DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
Definition: direction_func.h:88
DiagDirToAxis
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
IsDiagonalDirection
static bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
Definition: direction_func.h:273
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:83
IsValidDiagDirection
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:21
DirDifference
static DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Definition: direction_func.h:68
DiagDirDifference
static DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
Definition: direction_func.h:131
DirToDiagDir
static DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition: direction_func.h:166
ChangeDiagDir
static DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
Definition: direction_func.h:149
XYNSToDiagDir
static DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition: direction_func.h:261
DiagDirToDir
static Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
Definition: direction_func.h:182
ReverseDiagDir
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:118
IsValidDirection
static bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Definition: direction_func.h:32
DirDiff
DirDiff
Enumeration for the difference between two directions.
Definition: direction_type.h:62
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
AxisToDiagDir
static DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Definition: direction_func.h:232
ReverseDir
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
Definition: direction_func.h:54
DIR_END
@ DIR_END
Used to iterate.
Definition: direction_type.h:34
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:123
AXIS_END
@ AXIS_END
Used for iterations.
Definition: direction_type.h:126