# direction_func.h

Go to the documentation of this file.

    00001 /* $Id: direction_func.h 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef DIRECTION_FUNC_H
    00013 #define DIRECTION_FUNC_H
    00014
    00015 #include "direction_type.h"
    00016
    00023 static inline Direction ReverseDir(Direction d)
    00024 {
    00025   return (Direction)(4 ^ d);
    00026 }
    00027
    00028
    00036 static inline DirDiff DirDifference(Direction d0, Direction d1)
    00037 {
    00038   return (DirDiff)((d0 + 8 - d1) % 8);
    00039 }
    00040
    00052 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
    00053 {
    00054   return (DirDiff)((d + delta) % 8);
    00055 }
    00056
    00067 static inline Direction ChangeDir(Direction d, DirDiff delta)
    00068 {
    00069   return (Direction)((d + delta) % 8);
    00070 }
    00071
    00072
    00079 static inline DiagDirection ReverseDiagDir(DiagDirection d)
    00080 {
    00081   return (DiagDirection)(2 ^ d);
    00082 }
    00083
    00084
    00095 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
    00096 {
    00097   return (DiagDirection)((d + delta) % 4);
    00098 }
    00099
    00110 static inline DiagDirection DirToDiagDir(Direction dir)
    00111 {
    00112   return (DiagDirection)(dir >> 1);
    00113 }
    00114
    00125 static inline Direction DiagDirToDir(DiagDirection dir)
    00126 {
    00127   return (Direction)(dir * 2 + 1);
    00128 }
    00129
    00130
    00139 static inline Axis OtherAxis(Axis a)
    00140 {
    00141   return (Axis)(a ^ 1);
    00142 }
    00143
    00144
    00155 static inline Axis DiagDirToAxis(DiagDirection d)
    00156 {
    00157   return (Axis)(d & 1);
    00158 }
    00159
    00160
    00172 static inline DiagDirection AxisToDiagDir(Axis a)
    00173 {
    00174   return (DiagDirection)(2 - a);
    00175 }
    00176
    00188 static inline Direction AxisToDirection(Axis a)
    00189 {
    00190   return (Direction)(5 - 2 * a);
    00191 }
    00192
    00199 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
    00200 {
    00201   return (DiagDirection)(xy * 3 ^ ns * 2);
    00202 }
    00203
    00210 static inline bool IsValidDiagDirection(DiagDirection d)
    00211 {
    00212   return d < DIAGDIR_END;
    00213 }
    00214
    00221 static inline bool IsValidDirection(Direction d)
    00222 {
    00223   return d < DIR_END;
    00224 }
    00225
    00232 static inline bool IsValidAxis(Axis d)
    00233 {
    00234   return d < AXIS_END;
    00235 }
    00236
    00237 #endif /* DIRECTION_H */