OpenTTD
direction_func.h
Go to the documentation of this file.
1 /* $Id: direction_func.h 27422 2015-10-30 16:20:00Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef DIRECTION_FUNC_H
13 #define DIRECTION_FUNC_H
14 
15 #include "direction_type.h"
16 
23 static inline bool IsValidDiagDirection(DiagDirection d)
24 {
25  return d < DIAGDIR_END;
26 }
27 
34 static inline bool IsValidDirection(Direction d)
35 {
36  return d < DIR_END;
37 }
38 
45 static inline bool IsValidAxis(Axis d)
46 {
47  return d < AXIS_END;
48 }
49 
56 static inline Direction ReverseDir(Direction d)
57 {
58  assert(IsValidDirection(d));
59  return (Direction)(4 ^ d);
60 }
61 
62 
71 {
72  assert(IsValidDirection(d0));
73  assert(IsValidDirection(d1));
74  /* Cast to uint so compiler can use bitmask. If the difference is negative
75  * and we used int instead of uint, further "+ 8" would have to be added. */
76  return (DirDiff)((uint)(d0 - d1) % 8);
77 }
78 
90 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
91 {
92  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
93  return (DirDiff)((uint)(d + delta) % 8);
94 }
95 
106 static inline Direction ChangeDir(Direction d, DirDiff delta)
107 {
108  assert(IsValidDirection(d));
109  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
110  return (Direction)((uint)(d + delta) % 8);
111 }
112 
113 
121 {
122  assert(IsValidDiagDirection(d));
123  return (DiagDirection)(2 ^ d);
124 }
125 
134 {
135  assert(IsValidDiagDirection(d0));
136  assert(IsValidDiagDirection(d1));
137  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
138  return (DiagDirDiff)((uint)(d0 - d1) % 4);
139 }
140 
152 {
153  assert(IsValidDiagDirection(d));
154  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
155  return (DiagDirection)((uint)(d + delta) % 4);
156 }
157 
169 {
170  assert(IsValidDirection(dir));
171  return (DiagDirection)(dir >> 1);
172 }
173 
185 {
186  assert(IsValidDiagDirection(dir));
187  return (Direction)(dir * 2 + 1);
188 }
189 
190 
199 static inline Axis OtherAxis(Axis a)
200 {
201  assert(IsValidAxis(a));
202  return (Axis)(a ^ 1);
203 }
204 
205 
217 {
218  assert(IsValidDiagDirection(d));
219  return (Axis)(d & 1);
220 }
221 
222 
235 {
236  assert(IsValidAxis(a));
237  return (DiagDirection)(2 - a);
238 }
239 
252 {
253  assert(IsValidAxis(a));
254  return (Direction)(5 - 2 * a);
255 }
256 
263 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
264 {
265  assert(IsValidAxis(xy));
266  return (DiagDirection)(xy * 3 ^ ns * 2);
267 }
268 
275 static inline bool IsDiagonalDirection(Direction dir)
276 {
277  assert(IsValidDirection(dir));
278  return (dir & 1) != 0;
279 }
280 
281 #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.
static DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
Direction
Defines the 8 directions on the map.
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
DirDiff
Enumeration for the difference between two directions.
Used for iterations.
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.
DiagDirection
Enumeration for diagonal directions.
Used to iterate.
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.
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.