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 * Return the reverse of a direction
20 * @param d The direction to get the reverse from
21 * @return The reverse Direction
23 static inline Direction
ReverseDir(Direction d
)
25 return (Direction
)(4 ^ d
);
30 * Calculate the difference between two directions
32 * @param d0 The first direction as the base
33 * @param d1 The second direction as the offset from the base
34 * @return The difference how the second direction drifts of the first one.
36 static inline DirDiff
DirDifference(Direction d0
, Direction d1
)
38 /* Cast to uint so compiler can use bitmask. If the difference is negative
39 * and we used int instead of uint, further "+ 8" would have to be added. */
40 return (DirDiff
)((uint
)(d0
- d1
) % 8);
44 * Applies two differences together
46 * This function adds two differences together and returns the resulting
47 * difference. So adding two DIRDIFF_REVERSE together results in the
48 * DIRDIFF_SAME difference.
50 * @param d The first difference
51 * @param delta The second difference to add on
52 * @return The resulting difference
54 static inline DirDiff
ChangeDirDiff(DirDiff d
, DirDiff delta
)
56 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
57 return (DirDiff
)((uint
)(d
+ delta
) % 8);
61 * Change a direction by a given difference
63 * This functions returns a new direction of the given direction
64 * which is rotated by the given difference.
66 * @param d The direction to get a new direction from
67 * @param delta The offset/drift applied to the direction
68 * @return The new direction
70 static inline Direction
ChangeDir(Direction d
, DirDiff delta
)
72 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
73 return (Direction
)((uint
)(d
+ delta
) % 8);
78 * Returns the reverse direction of the given DiagDirection
80 * @param d The DiagDirection to get the reverse from
81 * @return The reverse direction
83 static inline DiagDirection
ReverseDiagDir(DiagDirection d
)
85 return (DiagDirection
)(2 ^ d
);
89 * Calculate the difference between two DiagDirection values
91 * @param d0 The first direction as the base
92 * @param d1 The second direction as the offset from the base
93 * @return The difference how the second direction drifts of the first one.
95 static inline DiagDirDiff
DiagDirDifference(DiagDirection d0
, DiagDirection d1
)
97 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
98 return (DiagDirDiff
)((uint
)(d0
- d1
) % 4);
102 * Applies a difference on a DiagDirection
104 * This function applies a difference on a DiagDirection and returns
105 * the new DiagDirection.
107 * @param d The DiagDirection
108 * @param delta The difference to apply on
109 * @return The new direction which was calculated
111 static inline DiagDirection
ChangeDiagDir(DiagDirection d
, DiagDirDiff delta
)
113 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
114 return (DiagDirection
)((uint
)(d
+ delta
) % 4);
118 * Convert a Direction to a DiagDirection.
120 * This function can be used to convert the 8-way Direction to
121 * the 4-way DiagDirection. If the direction cannot be mapped its
122 * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
124 * @param dir The direction to convert
125 * @return The resulting DiagDirection, maybe "rounded clockwise".
127 static inline DiagDirection
DirToDiagDir(Direction dir
)
129 return (DiagDirection
)(dir
>> 1);
133 * Convert a DiagDirection to a Direction.
135 * This function can be used to convert the 4-way DiagDirection
136 * to the 8-way Direction. As 4-way are less than 8-way not all
137 * possible directions can be calculated.
139 * @param dir The direction to convert
140 * @return The resulting Direction
142 static inline Direction
DiagDirToDir(DiagDirection dir
)
144 return (Direction
)(dir
* 2 + 1);
149 * Select the other axis as provided.
151 * This is basically the not-operator for the axis.
153 * @param a The given axis
154 * @return The other axis
156 static inline Axis
OtherAxis(Axis a
)
158 return (Axis
)(a
^ 1);
163 * Convert a DiagDirection to the axis.
165 * This function returns the axis which belongs to the given
166 * DiagDirection. The axis X belongs to the DiagDirection
167 * north-east and south-west.
169 * @param d The DiagDirection
170 * @return The axis which belongs to the direction
172 static inline Axis
DiagDirToAxis(DiagDirection d
)
174 return (Axis
)(d
& 1);
179 * Converts an Axis to a DiagDirection
181 * This function returns the DiagDirection which
182 * belongs to the axis. As 2 directions are mapped to an axis
183 * this function returns the one which points to south,
184 * either south-west (on X axis) or south-east (on Y axis)
187 * @return The direction pointed to south
189 static inline DiagDirection
AxisToDiagDir(Axis a
)
191 return (DiagDirection
)(2 - a
);
195 * Checks if an integer value is a valid DiagDirection
197 * @param d The value to check
198 * @return True if the value belongs to a DiagDirection, else false
200 static inline bool IsValidDiagDirection(DiagDirection d
)
202 return d
< DIAGDIR_END
;
206 * Checks if an integer value is a valid Direction
208 * @param d The value to check
209 * @return True if the value belongs to a Direction, else false
211 static inline bool IsValidDirection(Direction d
)
217 * Checks if an integer value is a valid Axis
219 * @param d The value to check
220 * @return True if the value belongs to an Axis, else false
222 static inline bool IsValidAxis(Axis d
)
228 * Checks if a given Direction is diagonal.
230 * @param dir The given direction.
231 * @return True if the direction is diagonal.
233 static inline bool IsDiagonalDirection(Direction dir
)
235 return (dir
& 1) != 0;
238 #endif /* DIRECTION_FUNC_H */