src/slope.h
branchnoai
changeset 9694 e72987579514
parent 9574 698395509d12
child 6870 ca3fd1fbe311
child 9703 d2a6acdbd665
equal deleted inserted replaced
9693:31fcaa5375a1 9694:e72987579514
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 /** @file slope.h */
     3 /** @file slope.h Definitions of a slope.
       
     4  * This file defines the enumeration and helper functions for handling
       
     5  * the slope info of a tile.
       
     6  */
     4 
     7 
     5 #ifndef SLOPE_H
     8 #ifndef SLOPE_H
     6 #define SLOPE_H
     9 #define SLOPE_H
     7 
    10 
       
    11 /**
       
    12  * Enumeration for the slope-type.
       
    13  *
       
    14  * This enumeration use the chars N,E,S,W corresponding the
       
    15  * direction north, east, south and west. The top corner of a tile
       
    16  * is the north-part of the tile. The whole slope is encoded with
       
    17  * 5 bits, 4 bits for each corner and 1 bit for a steep-flag.
       
    18  */
     8 enum Slope {
    19 enum Slope {
     9 	SLOPE_FLAT     = 0x00,
    20 	SLOPE_FLAT     = 0x00,                                  ///< a flat tile
    10 	SLOPE_W        = 0x01,
    21 	SLOPE_W        = 0x01,                                  ///< the west corner of the tile is raised
    11 	SLOPE_S        = 0x02,
    22 	SLOPE_S        = 0x02,                                  ///< the south corner of the tile is raised
    12 	SLOPE_E        = 0x04,
    23 	SLOPE_E        = 0x04,                                  ///< the east corner of the tile is raised
    13 	SLOPE_N        = 0x08,
    24 	SLOPE_N        = 0x08,                                  ///< the north corner of the tile is raised
    14 	SLOPE_STEEP    = 0x10,
    25 	SLOPE_STEEP    = 0x10,                                  ///< indicates the slope is steep
    15 	SLOPE_NW       = SLOPE_N | SLOPE_W,
    26 	SLOPE_NW       = SLOPE_N | SLOPE_W,                     ///< north and west corner are raised
    16 	SLOPE_SW       = SLOPE_S | SLOPE_W,
    27 	SLOPE_SW       = SLOPE_S | SLOPE_W,                     ///< south and west corner are raised
    17 	SLOPE_SE       = SLOPE_S | SLOPE_E,
    28 	SLOPE_SE       = SLOPE_S | SLOPE_E,                     ///< south and east corner are raised
    18 	SLOPE_NE       = SLOPE_N | SLOPE_E,
    29 	SLOPE_NE       = SLOPE_N | SLOPE_E,                     ///< north and east corner are raised
    19 	SLOPE_EW       = SLOPE_E | SLOPE_W,
    30 	SLOPE_EW       = SLOPE_E | SLOPE_W,                     ///< east and west corner are raised
    20 	SLOPE_NS       = SLOPE_N | SLOPE_S,
    31 	SLOPE_NS       = SLOPE_N | SLOPE_S,                     ///< north and south corner are raised
    21 	SLOPE_ELEVATED = SLOPE_N | SLOPE_E | SLOPE_S | SLOPE_W,
    32 	SLOPE_ELEVATED = SLOPE_N | SLOPE_E | SLOPE_S | SLOPE_W, ///< all corner are raised, similar to SLOPE_FLAT
    22 	SLOPE_NWS      = SLOPE_N | SLOPE_W | SLOPE_S,
    33 	SLOPE_NWS      = SLOPE_N | SLOPE_W | SLOPE_S,           ///< north, west and south corner are raised
    23 	SLOPE_WSE      = SLOPE_W | SLOPE_S | SLOPE_E,
    34 	SLOPE_WSE      = SLOPE_W | SLOPE_S | SLOPE_E,           ///< west, south and east corner are raised
    24 	SLOPE_SEN      = SLOPE_S | SLOPE_E | SLOPE_N,
    35 	SLOPE_SEN      = SLOPE_S | SLOPE_E | SLOPE_N,           ///< south, east and north corner are raised
    25 	SLOPE_ENW      = SLOPE_E | SLOPE_N | SLOPE_W,
    36 	SLOPE_ENW      = SLOPE_E | SLOPE_N | SLOPE_W,           ///< east, north and west corner are raised
    26 	SLOPE_STEEP_W  = SLOPE_STEEP | SLOPE_NWS,
    37 	SLOPE_STEEP_W  = SLOPE_STEEP | SLOPE_NWS,               ///< a steep slope falling to east (from west)
    27 	SLOPE_STEEP_S  = SLOPE_STEEP | SLOPE_WSE,
    38 	SLOPE_STEEP_S  = SLOPE_STEEP | SLOPE_WSE,               ///< a steep slope falling to north (from south)
    28 	SLOPE_STEEP_E  = SLOPE_STEEP | SLOPE_SEN,
    39 	SLOPE_STEEP_E  = SLOPE_STEEP | SLOPE_SEN,               ///< a steep slope falling to west (from east)
    29 	SLOPE_STEEP_N  = SLOPE_STEEP | SLOPE_ENW
    40 	SLOPE_STEEP_N  = SLOPE_STEEP | SLOPE_ENW                ///< a steep slope falling to south (from north)
    30 };
    41 };
    31 
    42 
       
    43 /**
       
    44  * Checks if a slope is steep.
       
    45  *
       
    46  * @param s The given #Slope.
       
    47  * @return True if the slope is steep, else false.
       
    48  */
    32 static inline bool IsSteepSlope(Slope s)
    49 static inline bool IsSteepSlope(Slope s)
    33 {
    50 {
    34 	return (s & SLOPE_STEEP) != 0;
    51 	return (s & SLOPE_STEEP) != 0;
    35 }
    52 }
    36 
    53 
       
    54 /**
       
    55  * Return the complement of a slope.
       
    56  *
       
    57  * This method returns the complement of a slope. The complement of a
       
    58  * slope is a slope with raised corner which aren't raised in the given
       
    59  * slope.
       
    60  *
       
    61  * @pre The slope must not be steep.
       
    62  * @param s The #Slope to get the complement.
       
    63  * @return a complement Slope of the given slope.
       
    64  */
    37 static inline Slope ComplementSlope(Slope s)
    65 static inline Slope ComplementSlope(Slope s)
    38 {
    66 {
    39 	assert(!IsSteepSlope(s));
    67 	assert(!IsSteepSlope(s));
    40 	return (Slope)(0xF ^ s);
    68 	return (Slope)(0xF ^ s);
    41 }
    69 }
    42 
    70 
       
    71 /**
       
    72  * Returns the highest corner of a slope (one corner raised or a steep slope).
       
    73  *
       
    74  * @pre      The slope must be a slope with one corner raised or a steep slope.
       
    75  * @param s  The #Slope.
       
    76  * @return   Number of the highest corner. (0 west, 1 south, 2 east, 3 north)
       
    77  */
       
    78 static inline byte GetHighestSlopeCorner(Slope s)
       
    79 {
       
    80 	switch (s) {
       
    81 		case SLOPE_W:
       
    82 		case SLOPE_STEEP_W: return 0;
       
    83 		case SLOPE_S:
       
    84 		case SLOPE_STEEP_S: return 1;
       
    85 		case SLOPE_E:
       
    86 		case SLOPE_STEEP_E: return 2;
       
    87 		case SLOPE_N:
       
    88 		case SLOPE_STEEP_N: return 3;
       
    89 		default: NOT_REACHED();
       
    90 	}
       
    91 }
       
    92 
       
    93 
       
    94 /**
       
    95  * Enumeration for Foundations.
       
    96  */
       
    97 enum Foundation {
       
    98 	FOUNDATION_NONE,             ///< The tile has no foundation, the slope remains unchanged.
       
    99 	FOUNDATION_LEVELED,          ///< The tile is leveled up to a flat slope.
       
   100 	FOUNDATION_INCLINED_X,       ///< The tile has an along X-axis inclined foundation.
       
   101 	FOUNDATION_INCLINED_Y,       ///< The tile has an along Y-axis inclined foundation.
       
   102 	FOUNDATION_STEEP_LOWER,      ///< The tile has a steep slope. The lowerst corner is raised by a foundation to allow building railroad on the lower halftile.
       
   103 	FOUNDATION_STEEP_HIGHER,     ///< The tile has a steep slope. Three corners are raised by a foundation to allow building railroad on the higher halftile.
       
   104 };
       
   105 
       
   106 /**
       
   107  * Tests for FOUNDATION_NONE.
       
   108  *
       
   109  * @param f  Maybe a #Foundation.
       
   110  * @return   true iff f is a foundation.
       
   111  */
       
   112 static inline bool IsFoundation(Foundation f)
       
   113 {
       
   114 	return f != FOUNDATION_NONE;
       
   115 }
       
   116 
       
   117 /**
       
   118  * Tests if the foundation is a leveled foundation.
       
   119  *
       
   120  * @param f  The #Foundation.
       
   121  * @return   true iff f is a leveled foundation.
       
   122  */
       
   123 static inline bool IsLeveledFoundation(Foundation f)
       
   124 {
       
   125 	return f == FOUNDATION_LEVELED;
       
   126 }
       
   127 
       
   128 /**
       
   129  * Tests if the foundation is an inclined foundation.
       
   130  *
       
   131  * @param f  The #Foundation.
       
   132  * @return   true iff f is an inclined foundation.
       
   133  */
       
   134 static inline bool IsInclinedFoundation(Foundation f)
       
   135 {
       
   136 	return (f == FOUNDATION_INCLINED_X) || (f == FOUNDATION_INCLINED_Y);
       
   137 }
       
   138 
       
   139 /**
       
   140  * Returns the foundation needed to flatten a slope.
       
   141  * The returned foundation is either FOUNDATION_NONE if the tile was already flat, or FOUNDATION_LEVELED.
       
   142  *
       
   143  * @pre      The slope must not be steep.
       
   144  * @param s  The current #Slope.
       
   145  * @return   The needed #Foundation.
       
   146  */
       
   147 static inline Foundation FlatteningFoundation(Slope s)
       
   148 {
       
   149 	assert(!IsSteepSlope(s));
       
   150 	return (s == SLOPE_FLAT ? FOUNDATION_NONE : FOUNDATION_LEVELED);
       
   151 }
       
   152 
       
   153 /**
       
   154  * Returns the along a specific axis inclined foundation.
       
   155  *
       
   156  * @param axis  The #Axis.
       
   157  * @return      The needed #Foundation.
       
   158  */
       
   159 static inline Foundation InclinedFoundation(Axis axis)
       
   160 {
       
   161 	return (axis == AXIS_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
       
   162 }
       
   163 
    43 #endif /* SLOPE_H */
   164 #endif /* SLOPE_H */