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 
8 /** @file direction_func.h Different functions related to conversions between directions. */
9 
10 #ifndef DIRECTION_FUNC_H
11 #define DIRECTION_FUNC_H
12 
13 #include "direction_type.h"
14 
15 /**
16  * Checks if an integer value is a valid DiagDirection
17  *
18  * @param d The value to check
19  * @return True if the value belongs to a DiagDirection, else false
20  */
IsValidDiagDirection(DiagDirection d)21 static inline bool IsValidDiagDirection(DiagDirection d)
22 {
23 	return d < DIAGDIR_END;
24 }
25 
26 /**
27  * Checks if an integer value is a valid Direction
28  *
29  * @param d The value to check
30  * @return True if the value belongs to a Direction, else false
31  */
IsValidDirection(Direction d)32 static inline bool IsValidDirection(Direction d)
33 {
34 	return d < DIR_END;
35 }
36 
37 /**
38  * Checks if an integer value is a valid Axis
39  *
40  * @param d The value to check
41  * @return True if the value belongs to an Axis, else false
42  */
IsValidAxis(Axis d)43 static inline bool IsValidAxis(Axis d)
44 {
45 	return d < AXIS_END;
46 }
47 
48 /**
49  * Return the reverse of a direction
50  *
51  * @param d The direction to get the reverse from
52  * @return The reverse Direction
53  */
ReverseDir(Direction d)54 static inline Direction ReverseDir(Direction d)
55 {
56 	assert(IsValidDirection(d));
57 	return (Direction)(4 ^ d);
58 }
59 
60 
61 /**
62  * Calculate the difference between two directions
63  *
64  * @param d0 The first direction as the base
65  * @param d1 The second direction as the offset from the base
66  * @return The difference how the second direction drifts of the first one.
67  */
DirDifference(Direction d0,Direction d1)68 static inline DirDiff DirDifference(Direction d0, Direction d1)
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 
77 /**
78  * Applies two differences together
79  *
80  * This function adds two differences together and returns the resulting
81  * difference. So adding two DIRDIFF_REVERSE together results in the
82  * DIRDIFF_SAME difference.
83  *
84  * @param d The first difference
85  * @param delta The second difference to add on
86  * @return The resulting difference
87  */
ChangeDirDiff(DirDiff d,DirDiff delta)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 
94 /**
95  * Change a direction by a given difference
96  *
97  * This functions returns a new direction of the given direction
98  * which is rotated by the given difference.
99  *
100  * @param d The direction to get a new direction from
101  * @param delta The offset/drift applied to the direction
102  * @return The new direction
103  */
ChangeDir(Direction d,DirDiff delta)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 
112 /**
113  * Returns the reverse direction of the given DiagDirection
114  *
115  * @param d The DiagDirection to get the reverse from
116  * @return The reverse direction
117  */
ReverseDiagDir(DiagDirection d)118 static inline DiagDirection ReverseDiagDir(DiagDirection d)
119 {
120 	assert(IsValidDiagDirection(d));
121 	return (DiagDirection)(2 ^ d);
122 }
123 
124 /**
125  * Calculate the difference between two DiagDirection values
126  *
127  * @param d0 The first direction as the base
128  * @param d1 The second direction as the offset from the base
129  * @return The difference how the second direction drifts of the first one.
130  */
DiagDirDifference(DiagDirection d0,DiagDirection d1)131 static inline DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
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 
139 /**
140  * Applies a difference on a DiagDirection
141  *
142  * This function applies a difference on a DiagDirection and returns
143  * the new DiagDirection.
144  *
145  * @param d The DiagDirection
146  * @param delta The difference to apply on
147  * @return The new direction which was calculated
148  */
ChangeDiagDir(DiagDirection d,DiagDirDiff delta)149 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
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 
156 /**
157  * Convert a Direction to a DiagDirection.
158  *
159  * This function can be used to convert the 8-way Direction to
160  * the 4-way DiagDirection. If the direction cannot be mapped its
161  * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
162  *
163  * @param dir The direction to convert
164  * @return The resulting DiagDirection, maybe "rounded clockwise".
165  */
DirToDiagDir(Direction dir)166 static inline DiagDirection DirToDiagDir(Direction dir)
167 {
168 	assert(IsValidDirection(dir));
169 	return (DiagDirection)(dir >> 1);
170 }
171 
172 /**
173  * Convert a DiagDirection to a Direction.
174  *
175  * This function can be used to convert the 4-way DiagDirection
176  * to the 8-way Direction. As 4-way are less than 8-way not all
177  * possible directions can be calculated.
178  *
179  * @param dir The direction to convert
180  * @return The resulting Direction
181  */
DiagDirToDir(DiagDirection dir)182 static inline Direction DiagDirToDir(DiagDirection dir)
183 {
184 	assert(IsValidDiagDirection(dir));
185 	return (Direction)(dir * 2 + 1);
186 }
187 
188 
189 /**
190  * Select the other axis as provided.
191  *
192  * This is basically the not-operator for the axis.
193  *
194  * @param a The given axis
195  * @return The other axis
196  */
OtherAxis(Axis a)197 static inline Axis OtherAxis(Axis a)
198 {
199 	assert(IsValidAxis(a));
200 	return (Axis)(a ^ 1);
201 }
202 
203 
204 /**
205  * Convert a DiagDirection to the axis.
206  *
207  * This function returns the axis which belongs to the given
208  * DiagDirection. The axis X belongs to the DiagDirection
209  * north-east and south-west.
210  *
211  * @param d The DiagDirection
212  * @return The axis which belongs to the direction
213  */
DiagDirToAxis(DiagDirection d)214 static inline Axis DiagDirToAxis(DiagDirection d)
215 {
216 	assert(IsValidDiagDirection(d));
217 	return (Axis)(d & 1);
218 }
219 
220 
221 /**
222  * Converts an Axis to a DiagDirection
223  *
224  * This function returns the DiagDirection which
225  * belongs to the axis. As 2 directions are mapped to an axis
226  * this function returns the one which points to south,
227  * either south-west (on X axis) or south-east (on Y axis)
228  *
229  * @param a The axis
230  * @return The direction pointed to south
231  */
AxisToDiagDir(Axis a)232 static inline DiagDirection AxisToDiagDir(Axis a)
233 {
234 	assert(IsValidAxis(a));
235 	return (DiagDirection)(2 - a);
236 }
237 
238 /**
239  * Converts an Axis to a Direction
240  *
241  * This function returns the Direction which
242  * belongs to the axis. As 2 directions are mapped to an axis
243  * this function returns the one which points to south,
244  * either south-west (on X axis) or south-east (on Y axis)
245  *
246  * @param a The axis
247  * @return The direction pointed to south
248  */
AxisToDirection(Axis a)249 static inline Direction AxisToDirection(Axis a)
250 {
251 	assert(IsValidAxis(a));
252 	return (Direction)(5 - 2 * a);
253 }
254 
255 /**
256  * Convert an axis and a flag for north/south into a DiagDirection
257  * @param xy axis to convert
258  * @param ns north -> 0, south -> 1
259  * @return the desired DiagDirection
260  */
XYNSToDiagDir(Axis xy,uint ns)261 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
262 {
263 	assert(IsValidAxis(xy));
264 	return (DiagDirection)(xy * 3 ^ ns * 2);
265 }
266 
267 /**
268  * Checks if a given Direction is diagonal.
269  *
270  * @param dir The given direction.
271  * @return True if the direction is diagonal.
272  */
IsDiagonalDirection(Direction dir)273 static inline bool IsDiagonalDirection(Direction dir)
274 {
275 	assert(IsValidDirection(dir));
276 	return (dir & 1) != 0;
277 }
278 
279 #endif /* DIRECTION_FUNC_H */
280