OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
70 static inline DirDiff DirDifference(Direction d0, Direction d1)
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
120 static inline DiagDirection ReverseDiagDir(DiagDirection d)
121 {
122 assert(IsValidDiagDirection(d));
123 return (DiagDirection)(2 ^ d);
124 }
125
133 static inline DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
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
151 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
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
168 static inline DiagDirection DirToDiagDir(Direction dir)
169 {
170 assert(IsValidDirection(dir));
171 return (DiagDirection)(dir >> 1);
172 }
173
184 static inline Direction DiagDirToDir(DiagDirection dir)
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
216 static inline Axis DiagDirToAxis(DiagDirection d)
217 {
218 assert(IsValidDiagDirection(d));
219 return (Axis)(d & 1);
220 }
221
222
234 static inline DiagDirection AxisToDiagDir(Axis a)
235 {
236 assert(IsValidAxis(a));
237 return (DiagDirection)(2 - a);
238 }
239
251 static inline Direction AxisToDirection(Axis a)
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 */