OpenTTD Source: src/direction_func.h Source File

OpenTTD Source 15.0-beta1

direction_func.h
Go to the documentation of this file.
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
10#ifndef DIRECTION_FUNC_H
11#define DIRECTION_FUNC_H
12
13#include "direction_type.h"
14
21 inline bool IsValidDiagDirection(DiagDirection d)
22{
23 return d < DIAGDIR_END;
24}
25
32 inline bool IsValidDirection(Direction d)
33{
34 return d < DIR_END;
35}
36
43 inline bool IsValidAxis(Axis d)
44{
45 return d < AXIS_END;
46}
47
54 inline Direction ReverseDir(Direction d)
55{
56 assert(IsValidDirection(d));
57 return (Direction)(4 ^ d);
58}
59
60
68 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 static_cast<DirDiff>((static_cast<uint>(d0) - static_cast<uint>(d1)) % 8);
75}
76
88 inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
89{
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);
92}
93
104 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 static_cast<Direction>((static_cast<uint>(d) + static_cast<uint>(delta)) % 8);
109}
110
111
118 inline DiagDirection ReverseDiagDir(DiagDirection d)
119{
120 assert(IsValidDiagDirection(d));
121 return (DiagDirection)(2 ^ d);
122}
123
131 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
149 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 static_cast<DiagDirection>((static_cast<uint>(d) + static_cast<uint>(delta)) % 4);
154}
155
166 inline DiagDirection DirToDiagDir(Direction dir)
167{
168 assert(IsValidDirection(dir));
169 return (DiagDirection)(dir >> 1);
170}
171
182 inline Direction DiagDirToDir(DiagDirection dir)
183{
184 assert(IsValidDiagDirection(dir));
185 return (Direction)(dir * 2 + 1);
186}
187
188
197 inline Axis OtherAxis(Axis a)
198{
199 assert(IsValidAxis(a));
200 return (Axis)(a ^ 1);
201}
202
203
214 inline Axis DiagDirToAxis(DiagDirection d)
215{
216 assert(IsValidDiagDirection(d));
217 return (Axis)(d & 1);
218}
219
220
232 inline DiagDirection AxisToDiagDir(Axis a)
233{
234 assert(IsValidAxis(a));
235 return (DiagDirection)(2 - a);
236}
237
249 inline Direction AxisToDirection(Axis a)
250{
251 assert(IsValidAxis(a));
252 return (Direction)(5 - 2 * a);
253}
254
261 inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
262{
263 assert(IsValidAxis(xy));
264 return (DiagDirection)(xy * 3 ^ ns * 2);
265}
266
273 inline bool IsDiagonalDirection(Direction dir)
274{
275 assert(IsValidDirection(dir));
276 return (dir & 1) != 0;
277}
278
279#endif /* DIRECTION_FUNC_H */
DirDifference
DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Definition direction_func.h:68
IsValidAxis
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Definition direction_func.h:43
DiagDirToDir
Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
Definition direction_func.h:182
ReverseDiagDir
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition direction_func.h:118
ReverseDir
Direction ReverseDir(Direction d)
Return the reverse of a direction.
Definition direction_func.h:54
IsValidDiagDirection
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition direction_func.h:21
ChangeDir
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition direction_func.h:104
AxisToDiagDir
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Definition direction_func.h:232
ChangeDiagDir
DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
Definition direction_func.h:149
IsValidDirection
bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Definition direction_func.h:32
AxisToDirection
Direction AxisToDirection(Axis a)
Converts an Axis to a Direction.
Definition direction_func.h:249
OtherAxis
Axis OtherAxis(Axis a)
Select the other axis as provided.
Definition direction_func.h:197
IsDiagonalDirection
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
Definition direction_func.h:273
ChangeDirDiff
DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
Definition direction_func.h:88
DiagDirToAxis
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition direction_func.h:214
DiagDirDifference
DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
Calculate the difference between two DiagDirection values.
Definition direction_func.h:131
XYNSToDiagDir
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition direction_func.h:261
DirToDiagDir
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition direction_func.h:166
direction_type.h
Different types to 'show' directions.
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_END
@ DIR_END
Used to iterate.
Definition direction_type.h:34
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition direction_type.h:116
AXIS_END
@ AXIS_END
Used for iterations.
Definition direction_type.h:119
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:73
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition direction_type.h:79
DiagDirDiff
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Definition direction_type.h:95
DirDiff
DirDiff
Allow incrementing of Direction variables.
Definition direction_type.h:58