OpenTTD
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 */
Different types to &#39;show&#39; directions.
static bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
static bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
static DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Used for iterations.
static DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
Used to iterate.
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Direction
Defines the 8 directions on the map.
DirDiff
Enumeration for the difference between two directions.
Used for iterations.
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
static DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
static DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
DiagDirDiff
Enumeration for the difference between to DiagDirection.
static Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
static DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
static Direction AxisToDirection(Axis a)
Converts an Axis to a Direction.
static bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
static Axis OtherAxis(Axis a)
Select the other axis as provided.
static DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
DiagDirection
Enumeration for diagonal directions.
static DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
static Direction ReverseDir(Direction d)
Return the reverse of a direction.
Axis
Allow incrementing of DiagDirDiff variables.
static Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.