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/>.
8 /** @file direction_func.h Different functions related to conversions between directions. */
10 #ifndef DIRECTION_FUNC_H
11 #define DIRECTION_FUNC_H
13 #include "direction_type.h"
16 * Checks if an integer value is a valid DiagDirection
18 * @param d The value to check
19 * @return True if the value belongs to a DiagDirection, else false
21 inline bool IsValidDiagDirection(DiagDirection d
)
23 return d
< DIAGDIR_END
;
27 * Checks if an integer value is a valid Direction
29 * @param d The value to check
30 * @return True if the value belongs to a Direction, else false
32 inline bool IsValidDirection(Direction d
)
38 * Checks if an integer value is a valid Axis
40 * @param d The value to check
41 * @return True if the value belongs to an Axis, else false
43 inline bool IsValidAxis(Axis d
)
49 * Return the reverse of a direction
51 * @param d The direction to get the reverse from
52 * @return The reverse Direction
54 inline Direction
ReverseDir(Direction d
)
56 assert(IsValidDirection(d
));
57 return (Direction
)(4 ^ d
);
62 * Calculate the difference between two directions
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.
68 inline DirDiff
DirDifference(Direction d0
, Direction d1
)
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 static_cast<DirDiff
>((static_cast<uint
>(d0
) - static_cast<uint
>(d1
)) % 8);
78 * Applies two differences together
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.
84 * @param d The first difference
85 * @param delta The second difference to add on
86 * @return The resulting difference
88 inline DirDiff
ChangeDirDiff(DirDiff d
, DirDiff delta
)
90 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
91 return static_cast<DirDiff
>((static_cast<uint
>(d
) + static_cast<uint
>(delta
)) % 8);
95 * Change a direction by a given difference
97 * This functions returns a new direction of the given direction
98 * which is rotated by the given difference.
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
104 inline Direction
ChangeDir(Direction d
, DirDiff delta
)
106 assert(IsValidDirection(d
));
107 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
108 return static_cast<Direction
>((static_cast<uint
>(d
) + static_cast<uint
>(delta
)) % 8);
113 * Returns the reverse direction of the given DiagDirection
115 * @param d The DiagDirection to get the reverse from
116 * @return The reverse direction
118 inline DiagDirection
ReverseDiagDir(DiagDirection d
)
120 assert(IsValidDiagDirection(d
));
121 return (DiagDirection
)(2 ^ d
);
125 * Calculate the difference between two DiagDirection values
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.
131 inline DiagDirDiff
DiagDirDifference(DiagDirection d0
, DiagDirection d1
)
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);
140 * Applies a difference on a DiagDirection
142 * This function applies a difference on a DiagDirection and returns
143 * the new DiagDirection.
145 * @param d The DiagDirection
146 * @param delta The difference to apply on
147 * @return The new direction which was calculated
149 inline DiagDirection
ChangeDiagDir(DiagDirection d
, DiagDirDiff delta
)
151 assert(IsValidDiagDirection(d
));
152 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
153 return static_cast<DiagDirection
>((static_cast<uint
>(d
) + static_cast<uint
>(delta
)) % 4);
157 * Convert a Direction to a DiagDirection.
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.
163 * @param dir The direction to convert
164 * @return The resulting DiagDirection, maybe "rounded clockwise".
166 inline DiagDirection
DirToDiagDir(Direction dir
)
168 assert(IsValidDirection(dir
));
169 return (DiagDirection
)(dir
>> 1);
173 * Convert a DiagDirection to a Direction.
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.
179 * @param dir The direction to convert
180 * @return The resulting Direction
182 inline Direction
DiagDirToDir(DiagDirection dir
)
184 assert(IsValidDiagDirection(dir
));
185 return (Direction
)(dir
* 2 + 1);
190 * Select the other axis as provided.
192 * This is basically the not-operator for the axis.
194 * @param a The given axis
195 * @return The other axis
197 inline Axis
OtherAxis(Axis a
)
199 assert(IsValidAxis(a
));
200 return (Axis
)(a
^ 1);
205 * Convert a DiagDirection to the axis.
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.
211 * @param d The DiagDirection
212 * @return The axis which belongs to the direction
214 inline Axis
DiagDirToAxis(DiagDirection d
)
216 assert(IsValidDiagDirection(d
));
217 return (Axis
)(d
& 1);
222 * Converts an Axis to a DiagDirection
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)
230 * @return The direction pointed to south
232 inline DiagDirection
AxisToDiagDir(Axis a
)
234 assert(IsValidAxis(a
));
235 return (DiagDirection
)(2 - a
);
239 * Converts an Axis to a Direction
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)
247 * @return The direction pointed to south
249 inline Direction
AxisToDirection(Axis a
)
251 assert(IsValidAxis(a
));
252 return (Direction
)(5 - 2 * a
);
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
261 inline DiagDirection
XYNSToDiagDir(Axis xy
, uint ns
)
263 assert(IsValidAxis(xy
));
264 return (DiagDirection
)(xy
* 3 ^ ns
* 2);
268 * Checks if a given Direction is diagonal.
270 * @param dir The given direction.
271 * @return True if the direction is diagonal.
273 inline bool IsDiagonalDirection(Direction dir
)
275 assert(IsValidDirection(dir
));
276 return (dir
& 1) != 0;
279 #endif /* DIRECTION_FUNC_H */