OpenTTD
direction_func.h
Go to the documentation of this file.
1 /* $Id$ */
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 
126 
138 {
139  assert(IsValidDiagDirection(d));
140  /* Cast to uint so compiler can use bitmask. Result can never be negative. */
141  return (DiagDirection)((uint)(d + delta) % 4);
142 }
143 
155 {
156  assert(IsValidDirection(dir));
157  return (DiagDirection)(dir >> 1);
158 }
159 
171 {
172  assert(IsValidDiagDirection(dir));
173  return (Direction)(dir * 2 + 1);
174 }
175 
176 
185 static inline Axis OtherAxis(Axis a)
186 {
187  assert(IsValidAxis(a));
188  return (Axis)(a ^ 1);
189 }
190 
191 
203 {
204  assert(IsValidDiagDirection(d));
205  return (Axis)(d & 1);
206 }
207 
208 
221 {
222  assert(IsValidAxis(a));
223  return (DiagDirection)(2 - a);
224 }
225 
238 {
239  assert(IsValidAxis(a));
240  return (Direction)(5 - 2 * a);
241 }
242 
249 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
250 {
251  assert(IsValidAxis(xy));
252  return (DiagDirection)(xy * 3 ^ ns * 2);
253 }
254 
261 static inline bool IsDiagonalDirection(Direction dir)
262 {
263  assert(IsValidDirection(dir));
264  return (dir & 1) != 0;
265 }
266 
267 #endif /* DIRECTION_FUNC_H */