# direction_func.h

Go to the documentation of this file.

    00001 /* $Id: direction_func.h 24900 2013-01-08 22:46:42Z planetmaker $ */
    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   /* Cast to uint so compiler can use bitmask. If the difference is negative
    00039    * and we used int instead of uint, further "+ 8" would have to be added. */
    00040   return (DirDiff)((uint)(d0 - d1) % 8);
    00041 }
    00042
    00054 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
    00055 {
    00056   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00057   return (DirDiff)((uint)(d + delta) % 8);
    00058 }
    00059
    00070 static inline Direction ChangeDir(Direction d, DirDiff delta)
    00071 {
    00072   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00073   return (Direction)((uint)(d + delta) % 8);
    00074 }
    00075
    00076
    00083 static inline DiagDirection ReverseDiagDir(DiagDirection d)
    00084 {
    00085   return (DiagDirection)(2 ^ d);
    00086 }
    00087
    00088
    00099 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
    00100 {
    00101   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00102   return (DiagDirection)((uint)(d + delta) % 4);
    00103 }
    00104
    00115 static inline DiagDirection DirToDiagDir(Direction dir)
    00116 {
    00117   return (DiagDirection)(dir >> 1);
    00118 }
    00119
    00130 static inline Direction DiagDirToDir(DiagDirection dir)
    00131 {
    00132   return (Direction)(dir * 2 + 1);
    00133 }
    00134
    00135
    00144 static inline Axis OtherAxis(Axis a)
    00145 {
    00146   return (Axis)(a ^ 1);
    00147 }
    00148
    00149
    00160 static inline Axis DiagDirToAxis(DiagDirection d)
    00161 {
    00162   return (Axis)(d & 1);
    00163 }
    00164
    00165
    00177 static inline DiagDirection AxisToDiagDir(Axis a)
    00178 {
    00179   return (DiagDirection)(2 - a);
    00180 }
    00181
    00193 static inline Direction AxisToDirection(Axis a)
    00194 {
    00195   return (Direction)(5 - 2 * a);
    00196 }
    00197
    00204 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
    00205 {
    00206   return (DiagDirection)(xy * 3 ^ ns * 2);
    00207 }
    00208
    00215 static inline bool IsValidDiagDirection(DiagDirection d)
    00216 {
    00217   return d < DIAGDIR_END;
    00218 }
    00219
    00226 static inline bool IsValidDirection(Direction d)
    00227 {
    00228   return d < DIR_END;
    00229 }
    00230
    00237 static inline bool IsValidAxis(Axis d)
    00238 {
    00239   return d < AXIS_END;
    00240 }
    00241
    00248 static inline bool IsDiagonalDirection(Direction dir)
    00249 {
    00250   return (dir & 1) != 0;
    00251 }
    00252
    00253 #endif /* DIRECTION_FUNC_H */