Translations update
[openttd/fttd.git] / src / direction_func.h
blobfc39fd9f30f9d262bd1a95caeed89a7cacc09a86
1 /* $Id$ */
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 */
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"
17 /**
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);
29 /**
30 * Calculate the difference between to 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 directions 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);
43 /**
44 * Applies two differences together
46 * This function adds two differences together and return 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);
60 /**
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);
77 /**
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 /**
90 * Applies a difference on a DiagDirection
92 * This function applies a difference on a DiagDirection and returns
93 * the new DiagDirection.
95 * @param d The DiagDirection
96 * @param delta The difference to apply on
97 * @return The new direction which was calculated
99 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
101 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
102 return (DiagDirection)((uint)(d + delta) % 4);
106 * Convert a Direction to a DiagDirection.
108 * This function can be used to convert the 8-way Direction to
109 * the 4-way DiagDirection. If the direction cannot be mapped its
110 * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
112 * @param dir The direction to convert
113 * @return The resulting DiagDirection, maybe "rounded clockwise".
115 static inline DiagDirection DirToDiagDir(Direction dir)
117 return (DiagDirection)(dir >> 1);
121 * Convert a DiagDirection to a Direction.
123 * This function can be used to convert the 4-way DiagDirection
124 * to the 8-way Direction. As 4-way are less than 8-way not all
125 * possible directions can be calculated.
127 * @param dir The direction to convert
128 * @return The resulting Direction
130 static inline Direction DiagDirToDir(DiagDirection dir)
132 return (Direction)(dir * 2 + 1);
137 * Select the other axis as provided.
139 * This is basically the not-operator for the axis.
141 * @param a The given axis
142 * @return The other axis
144 static inline Axis OtherAxis(Axis a)
146 return (Axis)(a ^ 1);
151 * Convert a DiagDirection to the axis.
153 * This function returns the axis which belongs to the given
154 * DiagDirection. The axis X belongs to the DiagDirection
155 * north-east and south-west.
157 * @param d The DiagDirection
158 * @return The axis which belongs to the direction
160 static inline Axis DiagDirToAxis(DiagDirection d)
162 return (Axis)(d & 1);
167 * Converts an Axis to a DiagDirection
169 * This function returns the DiagDirection which
170 * belongs to the axis. As 2 directions are mapped to an axis
171 * this function returns the one which points to south,
172 * either south-west (on X axis) or south-east (on Y axis)
174 * @param a The axis
175 * @return The direction pointed to south
177 static inline DiagDirection AxisToDiagDir(Axis a)
179 return (DiagDirection)(2 - a);
183 * Converts an Axis to a Direction
185 * This function returns the Direction which
186 * belongs to the axis. As 2 directions are mapped to an axis
187 * this function returns the one which points to south,
188 * either south-west (on X axis) or south-east (on Y axis)
190 * @param a The axis
191 * @return The direction pointed to south
193 static inline Direction AxisToDirection(Axis a)
195 return (Direction)(5 - 2 * a);
199 * Checks if an integer value is a valid DiagDirection
201 * @param d The value to check
202 * @return True if the value belongs to a DiagDirection, else false
204 static inline bool IsValidDiagDirection(DiagDirection d)
206 return d < DIAGDIR_END;
210 * Checks if an integer value is a valid Direction
212 * @param d The value to check
213 * @return True if the value belongs to a Direction, else false
215 static inline bool IsValidDirection(Direction d)
217 return d < DIR_END;
221 * Checks if an integer value is a valid Axis
223 * @param d The value to check
224 * @return True if the value belongs to an Axis, else false
226 static inline bool IsValidAxis(Axis d)
228 return d < AXIS_END;
232 * Checks if a given Direction is diagonal.
234 * @param dir The given direction.
235 * @return True if the direction is diagonal.
237 static inline bool IsDiagonalDirection(Direction dir)
239 return (dir & 1) != 0;
242 #endif /* DIRECTION_FUNC_H */