src/direction_func.h
branchNewGRF_ports
changeset 6872 1c4a4a609f85
child 10355 ee4b5f7a5bf2
equal deleted inserted replaced
6871:5a9dc001e1ad 6872:1c4a4a609f85
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file direction_func.h Different functions related to conversions between directions. */
       
     4 
       
     5 #ifndef DIRECTION_FUNC_H
       
     6 #define DIRECTION_FUNC_H
       
     7 
       
     8 #include "direction_type.h"
       
     9 
       
    10 /**
       
    11  * Return the reverse of a direction
       
    12  *
       
    13  * @param d The direction to get the reverse from
       
    14  * @return The reverse Direction
       
    15  */
       
    16 static inline Direction ReverseDir(Direction d)
       
    17 {
       
    18 	return (Direction)(4 ^ d);
       
    19 }
       
    20 
       
    21 
       
    22 /**
       
    23  * Calculate the difference between to directions
       
    24  *
       
    25  * @param d0 The first direction as the base
       
    26  * @param d1 The second direction as the offset from the base
       
    27  * @return The difference how the second directions drifts of the first one.
       
    28  */
       
    29 static inline DirDiff DirDifference(Direction d0, Direction d1)
       
    30 {
       
    31 	return (DirDiff)((d0 + 8 - d1) % 8);
       
    32 }
       
    33 
       
    34 /**
       
    35  * Applies two differences together
       
    36  *
       
    37  * This function adds two differences together and return the resulting
       
    38  * difference. So adding two DIRDIFF_REVERSE together results in the
       
    39  * DIRDIFF_SAME difference.
       
    40  *
       
    41  * @param d The first difference
       
    42  * @param delta The second difference to add on
       
    43  * @return The resulting difference
       
    44  */
       
    45 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
       
    46 {
       
    47 	return (DirDiff)((d + delta) % 8);
       
    48 }
       
    49 
       
    50 /**
       
    51  * Change a direction by a given difference
       
    52  *
       
    53  * This functions returns a new direction of the given direction
       
    54  * which is rotated by the given difference.
       
    55  *
       
    56  * @param d The direction to get a new direction from
       
    57  * @param delta The offset/drift applied to the direction
       
    58  * @return The new direction
       
    59  */
       
    60 static inline Direction ChangeDir(Direction d, DirDiff delta)
       
    61 {
       
    62 	return (Direction)((d + delta) % 8);
       
    63 }
       
    64 
       
    65 
       
    66 /**
       
    67  * Returns the reverse direction of the given DiagDirection
       
    68  *
       
    69  * @param d The DiagDirection to get the reverse from
       
    70  * @return The reverse direction
       
    71  */
       
    72 static inline DiagDirection ReverseDiagDir(DiagDirection d)
       
    73 {
       
    74 	return (DiagDirection)(2 ^ d);
       
    75 }
       
    76 
       
    77 
       
    78 /**
       
    79  * Applies a difference on a DiagDirection
       
    80  *
       
    81  * This function applies a difference on a DiagDirection and returns
       
    82  * the new DiagDirection.
       
    83  *
       
    84  * @param d The DiagDirection
       
    85  * @param delta The difference to applie on
       
    86  * @return The new direction which was calculated
       
    87  */
       
    88 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
       
    89 {
       
    90 	return (DiagDirection)((d + delta) % 4);
       
    91 }
       
    92 
       
    93 /**
       
    94  * Convert a Direction to a DiagDirection.
       
    95  *
       
    96  * This function can be used to convert the 8-way Direction to
       
    97  * the 4-way DiagDirection. If the direction cannot be mapped its
       
    98  * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
       
    99  *
       
   100  * @param dir The direction to convert
       
   101  * @return The resulting DiagDirection, maybe "rounded clockwise".
       
   102  */
       
   103 static inline DiagDirection DirToDiagDir(Direction dir)
       
   104 {
       
   105 	return (DiagDirection)(dir >> 1);
       
   106 }
       
   107 
       
   108 /**
       
   109  * Convert a DiagDirection to a Direction.
       
   110  *
       
   111  * This function can be used to convert the 4-way DiagDirection
       
   112  * to the 8-way Direction. As 4-way are less than 8-way not all
       
   113  * possible directions can be calculated.
       
   114  *
       
   115  * @param dir The direction to convert
       
   116  * @return The resulting Direction
       
   117  */
       
   118 static inline Direction DiagDirToDir(DiagDirection dir)
       
   119 {
       
   120 	return (Direction)(dir * 2 + 1);
       
   121 }
       
   122 
       
   123 
       
   124 /**
       
   125  * Select the other axis as provided.
       
   126  *
       
   127  * This is basically the not-operator for the axis.
       
   128  *
       
   129  * @param a The given axis
       
   130  * @return The other axis
       
   131  */
       
   132 static inline Axis OtherAxis(Axis a)
       
   133 {
       
   134 	return (Axis)(a ^ 1);
       
   135 }
       
   136 
       
   137 
       
   138 /**
       
   139  * Convert a DiagDirection to the axis.
       
   140  *
       
   141  * This function returns the axis which belongs to the given
       
   142  * DiagDirection. The axis X belongs to the DiagDirection
       
   143  * north-east and south-west.
       
   144  *
       
   145  * @param d The DiagDirection
       
   146  * @return The axis which belongs to the direction
       
   147  */
       
   148 static inline Axis DiagDirToAxis(DiagDirection d)
       
   149 {
       
   150 	return (Axis)(d & 1);
       
   151 }
       
   152 
       
   153 
       
   154 /**
       
   155  * Converts an Axis to a DiagDirection
       
   156  *
       
   157  * This function returns the DiagDirection which
       
   158  * belongs to the axis. As 2 directions are mapped to an axis
       
   159  * this function returns the one which points to south,
       
   160  * either south-west (on X axis) or south-east (on Y axis)
       
   161  *
       
   162  * @param a The axis
       
   163  * @return The direction pointed to south
       
   164  */
       
   165 static inline DiagDirection AxisToDiagDir(Axis a)
       
   166 {
       
   167 	return (DiagDirection)(2 - a);
       
   168 }
       
   169 
       
   170 /**
       
   171  * Convert an axis and a flag for north/south into a DiagDirection
       
   172  * @param xy axis to convert
       
   173  * @param ns north -> 0, south -> 1
       
   174  * @return the desired DiagDirection
       
   175  */
       
   176 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
       
   177 {
       
   178 	return (DiagDirection)(xy * 3 ^ ns * 2);
       
   179 }
       
   180 
       
   181 /**
       
   182  * Checks if an interger value is a valid DiagDirection
       
   183  *
       
   184  * @param d The value to check
       
   185  * @return True if the value belongs to a DiagDirection, else false
       
   186  */
       
   187 static inline bool IsValidDiagDirection(DiagDirection d)
       
   188 {
       
   189 	return d < DIAGDIR_END;
       
   190 }
       
   191 
       
   192 /**
       
   193  * Checks if an integer value is a valid Direction
       
   194  *
       
   195  * @param d The value to check
       
   196  * @return True if the value belongs to a Direction, else false
       
   197  */
       
   198 static inline bool IsValidDirection(Direction d)
       
   199 {
       
   200 	return d < DIR_END;
       
   201 }
       
   202 
       
   203 /**
       
   204  * Checks if an integer value is a valid Axis
       
   205  *
       
   206  * @param d The value to check
       
   207  * @return True if the value belongs to an Axis, else false
       
   208  */
       
   209 static inline bool IsValidAxis(Axis d)
       
   210 {
       
   211 	return d < AXIS_END;
       
   212 }
       
   213 
       
   214 #endif /* DIRECTION_H */