src/road_func.h
branchNewGRF_ports
changeset 6872 1c4a4a609f85
child 10184 fcf5fb2548eb
equal deleted inserted replaced
6871:5a9dc001e1ad 6872:1c4a4a609f85
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file road_func.h Functions related to roads. */
       
     4 
       
     5 #ifndef ROAD_FUNC_H
       
     6 #define ROAD_FUNC_H
       
     7 
       
     8 #include "core/bitmath_func.hpp"
       
     9 #include "road_type.h"
       
    10 #include "direction_func.h"
       
    11 #include "player_type.h"
       
    12 
       
    13 /**
       
    14  * Whether the given roadtype is valid.
       
    15  * @param rt the roadtype to check for validness
       
    16  * @return true if and only if valid
       
    17  */
       
    18 static inline bool IsValidRoadType(RoadType rt)
       
    19 {
       
    20 	return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
       
    21 }
       
    22 
       
    23 /**
       
    24  * Are the given bits pointing to valid roadtypes?
       
    25  * @param rts the roadtypes to check for validness
       
    26  * @return true if and only if valid
       
    27  */
       
    28 static inline bool AreValidRoadTypes(RoadTypes rts)
       
    29 {
       
    30 	return HasBit(rts, ROADTYPE_ROAD) || HasBit(rts, ROADTYPE_TRAM);
       
    31 }
       
    32 
       
    33 /**
       
    34  * Maps a RoadType to the corresponding RoadTypes value
       
    35  *
       
    36  * @param rt the roadtype to get the roadtypes from
       
    37  * @return the roadtypes with the given roadtype
       
    38  */
       
    39 static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
       
    40 {
       
    41 	return (RoadTypes)(1 << rt);
       
    42 }
       
    43 
       
    44 /**
       
    45  * Returns the RoadTypes which are not present in the given RoadTypes
       
    46  *
       
    47  * This function returns the complement of a given RoadTypes.
       
    48  *
       
    49  * @param r The given RoadTypes
       
    50  * @return The complement of the given RoadTypes
       
    51  * @note The unused value ROADTYPES_HWAY will be used, too.
       
    52  */
       
    53 static inline RoadTypes ComplementRoadTypes(RoadTypes r)
       
    54 {
       
    55 	return (RoadTypes)(ROADTYPES_ALL ^ r);
       
    56 }
       
    57 
       
    58 
       
    59 /**
       
    60  * Calculate the complement of a RoadBits value
       
    61  *
       
    62  * Simply flips all bits in the RoadBits value to get the complement
       
    63  * of the RoadBits.
       
    64  *
       
    65  * @param r The given RoadBits value
       
    66  * @return the complement
       
    67  */
       
    68 static inline RoadBits ComplementRoadBits(RoadBits r)
       
    69 {
       
    70 	return (RoadBits)(ROAD_ALL ^ r);
       
    71 }
       
    72 
       
    73 /**
       
    74  * Calculate the mirrored RoadBits
       
    75  *
       
    76  * Simply move the bits to their new position.
       
    77  *
       
    78  * @param r The given RoadBits value
       
    79  * @return the mirrored
       
    80  */
       
    81 static inline RoadBits MirrorRoadBits(RoadBits r)
       
    82 {
       
    83 	return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
       
    84 }
       
    85 
       
    86 /**
       
    87  * Calculate rotated RoadBits
       
    88  *
       
    89  * Move the Roadbits clockwise til they are in their final position.
       
    90  *
       
    91  * @param r The given RoadBits value
       
    92  * @param rot The given Rotation angle
       
    93  * @return the rotated
       
    94  */
       
    95 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
       
    96 {
       
    97 	for (; rot > (DiagDirDiff)0; rot--){
       
    98 		r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
       
    99 	}
       
   100 	return r;
       
   101 }
       
   102 
       
   103 /**
       
   104  * Create the road-part which belongs to the given DiagDirection
       
   105  *
       
   106  * This function returns a RoadBits value which belongs to
       
   107  * the given DiagDirection.
       
   108  *
       
   109  * @param d The DiagDirection
       
   110  * @return The result RoadBits which the selected road-part set
       
   111  */
       
   112 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
       
   113 {
       
   114 	return (RoadBits)(ROAD_NW << (3 ^ d));
       
   115 }
       
   116 
       
   117 /**
       
   118  * Finds out, whether given player has all given RoadTypes available
       
   119  * @param PlayerID ID of player
       
   120  * @param rts RoadTypes to test
       
   121  * @return true if player has all requested RoadTypes available
       
   122  */
       
   123 bool HasRoadTypesAvail(const PlayerID p, const RoadTypes rts);
       
   124 
       
   125 /**
       
   126  * Validate functions for rail building.
       
   127  * @param rt road type to check.
       
   128  * @return true if the current player may build the road.
       
   129  */
       
   130 bool ValParamRoadType(const RoadType rt);
       
   131 
       
   132 /**
       
   133  * Get the road types the given player can build.
       
   134  * @param p the player to get the roadtypes for.
       
   135  * @return the road types.
       
   136  */
       
   137 RoadTypes GetPlayerRoadtypes(const PlayerID p);
       
   138 
       
   139 void UpdateLevelCrossing(TileIndex tile, bool sound = true);
       
   140 
       
   141 #endif /* ROAD_FUNC_H */