# direction_func.h

Go to the documentation of this file.

    00001 /* $Id: direction_func.h 26105 2013-11-25 13:16:06Z 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 bool IsValidDiagDirection(DiagDirection d)
    00024 {
    00025   return d < DIAGDIR_END;
    00026 }
    00027
    00034 static inline bool IsValidDirection(Direction d)
    00035 {
    00036   return d < DIR_END;
    00037 }
    00038
    00045 static inline bool IsValidAxis(Axis d)
    00046 {
    00047   return d < AXIS_END;
    00048 }
    00049
    00056 static inline Direction ReverseDir(Direction d)
    00057 {
    00058   assert(IsValidDirection(d));
    00059   return (Direction)(4 ^ d);
    00060 }
    00061
    00062
    00070 static inline DirDiff DirDifference(Direction d0, Direction d1)
    00071 {
    00072   assert(IsValidDirection(d0));
    00073   assert(IsValidDirection(d1));
    00074   /* Cast to uint so compiler can use bitmask. If the difference is negative
    00075    * and we used int instead of uint, further "+ 8" would have to be added. */
    00076   return (DirDiff)((uint)(d0 - d1) % 8);
    00077 }
    00078
    00090 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
    00091 {
    00092   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00093   return (DirDiff)((uint)(d + delta) % 8);
    00094 }
    00095
    00106 static inline Direction ChangeDir(Direction d, DirDiff delta)
    00107 {
    00108   assert(IsValidDirection(d));
    00109   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00110   return (Direction)((uint)(d + delta) % 8);
    00111 }
    00112
    00113
    00120 static inline DiagDirection ReverseDiagDir(DiagDirection d)
    00121 {
    00122   assert(IsValidDiagDirection(d));
    00123   return (DiagDirection)(2 ^ d);
    00124 }
    00125
    00126
    00137 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
    00138 {
    00139   assert(IsValidDiagDirection(d));
    00140   /* Cast to uint so compiler can use bitmask. Result can never be negative. */
    00141   return (DiagDirection)((uint)(d + delta) % 4);
    00142 }
    00143
    00154 static inline DiagDirection DirToDiagDir(Direction dir)
    00155 {
    00156   assert(IsValidDirection(dir));
    00157   return (DiagDirection)(dir >> 1);
    00158 }
    00159
    00170 static inline Direction DiagDirToDir(DiagDirection dir)
    00171 {
    00172   assert(IsValidDiagDirection(dir));
    00173   return (Direction)(dir * 2 + 1);
    00174 }
    00175
    00176
    00185 static inline Axis OtherAxis(Axis a)
    00186 {
    00187   assert(IsValidAxis(a));
    00188   return (Axis)(a ^ 1);
    00189 }
    00190
    00191
    00202 static inline Axis DiagDirToAxis(DiagDirection d)
    00203 {
    00204   assert(IsValidDiagDirection(d));
    00205   return (Axis)(d & 1);
    00206 }
    00207
    00208
    00220 static inline DiagDirection AxisToDiagDir(Axis a)
    00221 {
    00222   assert(IsValidAxis(a));
    00223   return (DiagDirection)(2 - a);
    00224 }
    00225
    00237 static inline Direction AxisToDirection(Axis a)
    00238 {
    00239   assert(IsValidAxis(a));
    00240   return (Direction)(5 - 2 * a);
    00241 }
    00242
    00249 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
    00250 {
    00251   assert(IsValidAxis(xy));
    00252   return (DiagDirection)(xy * 3 ^ ns * 2);
    00253 }
    00254
    00261 static inline bool IsDiagonalDirection(Direction dir)
    00262 {
    00263   assert(IsValidDirection(dir));
    00264   return (dir & 1) != 0;
    00265 }
    00266
    00267 #endif /* DIRECTION_FUNC_H */