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/>.
10 /** @file direction_func.h Different functions related to conversions between directions. */
12 #ifndef DIRECTION_FUNC_H
13 #define DIRECTION_FUNC_H
15 #include "direction_type.h"
18 * Checks if an integer value is a valid DiagDirection
20 * @param d The value to check
21 * @return True if the value belongs to a DiagDirection, else false
23 static inline bool IsValidDiagDirection(DiagDirection d
)
25 return d
< DIAGDIR_END
;
29 * Checks if an integer value is a valid Direction
31 * @param d The value to check
32 * @return True if the value belongs to a Direction, else false
34 static inline bool IsValidDirection(Direction d
)
40 * Checks if an integer value is a valid Axis
42 * @param d The value to check
43 * @return True if the value belongs to an Axis, else false
45 static inline bool IsValidAxis(Axis d
)
51 * Return the reverse of a direction
53 * @param d The direction to get the reverse from
54 * @return The reverse Direction
56 static inline Direction
ReverseDir(Direction d
)
58 assert(IsValidDirection(d
));
59 return (Direction
)(4 ^ d
);
64 * Calculate the difference between two directions
66 * @param d0 The first direction as the base
67 * @param d1 The second direction as the offset from the base
68 * @return The difference how the second direction drifts of the first one.
70 static inline DirDiff
DirDifference(Direction d0
, Direction d1
)
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);
80 * Applies two differences together
82 * This function adds two differences together and returns the resulting
83 * difference. So adding two DIRDIFF_REVERSE together results in the
84 * DIRDIFF_SAME difference.
86 * @param d The first difference
87 * @param delta The second difference to add on
88 * @return The resulting difference
90 static inline DirDiff
ChangeDirDiff(DirDiff d
, DirDiff delta
)
92 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
93 return (DirDiff
)((uint
)(d
+ delta
) % 8);
97 * Change a direction by a given difference
99 * This functions returns a new direction of the given direction
100 * which is rotated by the given difference.
102 * @param d The direction to get a new direction from
103 * @param delta The offset/drift applied to the direction
104 * @return The new direction
106 static inline Direction
ChangeDir(Direction d
, DirDiff delta
)
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);
115 * Returns the reverse direction of the given DiagDirection
117 * @param d The DiagDirection to get the reverse from
118 * @return The reverse direction
120 static inline DiagDirection
ReverseDiagDir(DiagDirection d
)
122 assert(IsValidDiagDirection(d
));
123 return (DiagDirection
)(2 ^ d
);
127 * Calculate the difference between two DiagDirection values
129 * @param d0 The first direction as the base
130 * @param d1 The second direction as the offset from the base
131 * @return The difference how the second direction drifts of the first one.
133 static inline DiagDirDiff
DiagDirDifference(DiagDirection d0
, DiagDirection d1
)
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);
142 * Applies a difference on a DiagDirection
144 * This function applies a difference on a DiagDirection and returns
145 * the new DiagDirection.
147 * @param d The DiagDirection
148 * @param delta The difference to apply on
149 * @return The new direction which was calculated
151 static inline DiagDirection
ChangeDiagDir(DiagDirection d
, DiagDirDiff delta
)
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);
159 * Convert a Direction to a DiagDirection.
161 * This function can be used to convert the 8-way Direction to
162 * the 4-way DiagDirection. If the direction cannot be mapped its
163 * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
165 * @param dir The direction to convert
166 * @return The resulting DiagDirection, maybe "rounded clockwise".
168 static inline DiagDirection
DirToDiagDir(Direction dir
)
170 assert(IsValidDirection(dir
));
171 return (DiagDirection
)(dir
>> 1);
175 * Convert a DiagDirection to a Direction.
177 * This function can be used to convert the 4-way DiagDirection
178 * to the 8-way Direction. As 4-way are less than 8-way not all
179 * possible directions can be calculated.
181 * @param dir The direction to convert
182 * @return The resulting Direction
184 static inline Direction
DiagDirToDir(DiagDirection dir
)
186 assert(IsValidDiagDirection(dir
));
187 return (Direction
)(dir
* 2 + 1);
192 * Select the other axis as provided.
194 * This is basically the not-operator for the axis.
196 * @param a The given axis
197 * @return The other axis
199 static inline Axis
OtherAxis(Axis a
)
201 assert(IsValidAxis(a
));
202 return (Axis
)(a
^ 1);
207 * Convert a DiagDirection to the axis.
209 * This function returns the axis which belongs to the given
210 * DiagDirection. The axis X belongs to the DiagDirection
211 * north-east and south-west.
213 * @param d The DiagDirection
214 * @return The axis which belongs to the direction
216 static inline Axis
DiagDirToAxis(DiagDirection d
)
218 assert(IsValidDiagDirection(d
));
219 return (Axis
)(d
& 1);
224 * Converts an Axis to a DiagDirection
226 * This function returns the DiagDirection which
227 * belongs to the axis. As 2 directions are mapped to an axis
228 * this function returns the one which points to south,
229 * either south-west (on X axis) or south-east (on Y axis)
232 * @return The direction pointed to south
234 static inline DiagDirection
AxisToDiagDir(Axis a
)
236 assert(IsValidAxis(a
));
237 return (DiagDirection
)(2 - a
);
241 * Converts an Axis to a Direction
243 * This function returns the Direction which
244 * belongs to the axis. As 2 directions are mapped to an axis
245 * this function returns the one which points to south,
246 * either south-west (on X axis) or south-east (on Y axis)
249 * @return The direction pointed to south
251 static inline Direction
AxisToDirection(Axis a
)
253 assert(IsValidAxis(a
));
254 return (Direction
)(5 - 2 * a
);
258 * Convert an axis and a flag for north/south into a DiagDirection
259 * @param xy axis to convert
260 * @param ns north -> 0, south -> 1
261 * @return the desired DiagDirection
263 static inline DiagDirection
XYNSToDiagDir(Axis xy
, uint ns
)
265 assert(IsValidAxis(xy
));
266 return (DiagDirection
)(xy
* 3 ^ ns
* 2);
270 * Checks if a given Direction is diagonal.
272 * @param dir The given direction.
273 * @return True if the direction is diagonal.
275 static inline bool IsDiagonalDirection(Direction dir
)
277 assert(IsValidDirection(dir
));
278 return (dir
& 1) != 0;
281 #endif /* DIRECTION_FUNC_H */