1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /**
9  * @file slope_type.h Definitions of a slope.
10  * This file defines the enumeration and helper functions for handling
11  * the slope info of a tile.
12  */
13 
14 #ifndef SLOPE_TYPE_H
15 #define SLOPE_TYPE_H
16 
17 #include "core/enum_type.hpp"
18 
19 /**
20  * Enumeration of tile corners
21  */
22 enum Corner {
23 	CORNER_W = 0,
24 	CORNER_S = 1,
25 	CORNER_E = 2,
26 	CORNER_N = 3,
27 	CORNER_END,
28 	CORNER_INVALID = 0xFF
29 };
30 
31 
32 /**
33  * Enumeration for the slope-type.
34  *
35  * This enumeration use the chars N,E,S,W corresponding the
36  * direction north, east, south and west. The top corner of a tile
37  * is the north-part of the tile. The whole slope is encoded with
38  * 5 bits, 4 bits for each corner and 1 bit for a steep-flag.
39  *
40  * For halftile slopes an extra 3 bits are used to represent this
41  * properly; 1 bit for a halftile-flag and 2 bits to encode which
42  * extra side (corner) is leveled when the slope of the first 5
43  * bits is applied. This means that there can only be one leveled
44  * slope for steep slopes, which is logical because two leveled
45  * slopes would mean that it is not a steep slope as halftile
46  * slopes only span one height level.
47  */
48 enum Slope {
49 	SLOPE_FLAT     = 0x00,                                  ///< a flat tile
50 	SLOPE_W        = 0x01,                                  ///< the west corner of the tile is raised
51 	SLOPE_S        = 0x02,                                  ///< the south corner of the tile is raised
52 	SLOPE_E        = 0x04,                                  ///< the east corner of the tile is raised
53 	SLOPE_N        = 0x08,                                  ///< the north corner of the tile is raised
54 	SLOPE_STEEP    = 0x10,                                  ///< indicates the slope is steep
55 	SLOPE_NW       = SLOPE_N | SLOPE_W,                     ///< north and west corner are raised
56 	SLOPE_SW       = SLOPE_S | SLOPE_W,                     ///< south and west corner are raised
57 	SLOPE_SE       = SLOPE_S | SLOPE_E,                     ///< south and east corner are raised
58 	SLOPE_NE       = SLOPE_N | SLOPE_E,                     ///< north and east corner are raised
59 	SLOPE_EW       = SLOPE_E | SLOPE_W,                     ///< east and west corner are raised
60 	SLOPE_NS       = SLOPE_N | SLOPE_S,                     ///< north and south corner are raised
61 	SLOPE_ELEVATED = SLOPE_N | SLOPE_E | SLOPE_S | SLOPE_W, ///< bit mask containing all 'simple' slopes
62 	SLOPE_NWS      = SLOPE_N | SLOPE_W | SLOPE_S,           ///< north, west and south corner are raised
63 	SLOPE_WSE      = SLOPE_W | SLOPE_S | SLOPE_E,           ///< west, south and east corner are raised
64 	SLOPE_SEN      = SLOPE_S | SLOPE_E | SLOPE_N,           ///< south, east and north corner are raised
65 	SLOPE_ENW      = SLOPE_E | SLOPE_N | SLOPE_W,           ///< east, north and west corner are raised
66 	SLOPE_STEEP_W  = SLOPE_STEEP | SLOPE_NWS,               ///< a steep slope falling to east (from west)
67 	SLOPE_STEEP_S  = SLOPE_STEEP | SLOPE_WSE,               ///< a steep slope falling to north (from south)
68 	SLOPE_STEEP_E  = SLOPE_STEEP | SLOPE_SEN,               ///< a steep slope falling to west (from east)
69 	SLOPE_STEEP_N  = SLOPE_STEEP | SLOPE_ENW,               ///< a steep slope falling to south (from north)
70 
71 	SLOPE_HALFTILE = 0x20,                                  ///< one halftile is leveled (non continuous slope)
72 	SLOPE_HALFTILE_MASK = 0xE0,                             ///< three bits used for halftile slopes
73 	SLOPE_HALFTILE_W = SLOPE_HALFTILE | (CORNER_W << 6),    ///< the west halftile is leveled (non continuous slope)
74 	SLOPE_HALFTILE_S = SLOPE_HALFTILE | (CORNER_S << 6),    ///< the south halftile is leveled (non continuous slope)
75 	SLOPE_HALFTILE_E = SLOPE_HALFTILE | (CORNER_E << 6),    ///< the east halftile is leveled (non continuous slope)
76 	SLOPE_HALFTILE_N = SLOPE_HALFTILE | (CORNER_N << 6),    ///< the north halftile is leveled (non continuous slope)
77 };
78 DECLARE_ENUM_AS_BIT_SET(Slope)
79 
80 /**
81  * Helper for creating a bitset of slopes.
82  * @param x The slope to convert into a bitset.
83  */
84 #define M(x) (1 << (x))
85 /** Constant bitset with safe slopes for building a level crossing. */
86 static const uint32 VALID_LEVEL_CROSSING_SLOPES = M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT);
87 #undef M
88 
89 
90 /**
91  * Enumeration for Foundations.
92  */
93 enum Foundation {
94 	FOUNDATION_NONE,             ///< The tile has no foundation, the slope remains unchanged.
95 	FOUNDATION_LEVELED,          ///< The tile is leveled up to a flat slope.
96 	FOUNDATION_INCLINED_X,       ///< The tile has an along X-axis inclined foundation.
97 	FOUNDATION_INCLINED_Y,       ///< The tile has an along Y-axis inclined foundation.
98 	FOUNDATION_STEEP_LOWER,      ///< The tile has a steep slope. The lowest corner is raised by a foundation to allow building railroad on the lower halftile.
99 
100 	/* Halftile foundations */
101 	FOUNDATION_STEEP_BOTH,       ///< The tile has a steep slope. The lowest corner is raised by a foundation and the upper halftile is leveled.
102 	FOUNDATION_HALFTILE_W,       ///< Level west halftile non-continuously.
103 	FOUNDATION_HALFTILE_S,       ///< Level south halftile non-continuously.
104 	FOUNDATION_HALFTILE_E,       ///< Level east halftile non-continuously.
105 	FOUNDATION_HALFTILE_N,       ///< Level north halftile non-continuously.
106 
107 	/* Special anti-zig-zag foundations for single horizontal/vertical track */
108 	FOUNDATION_RAIL_W,           ///< Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
109 	FOUNDATION_RAIL_S,           ///< Foundation for TRACK_BIT_LOWER, but not a leveled foundation.
110 	FOUNDATION_RAIL_E,           ///< Foundation for TRACK_BIT_RIGHT, but not a leveled foundation.
111 	FOUNDATION_RAIL_N,           ///< Foundation for TRACK_BIT_UPPER, but not a leveled foundation.
112 
113 	FOUNDATION_INVALID = 0xFF,   ///< Used inside "rail_cmd.cpp" to indicate invalid slope/track combination.
114 };
115 
116 #endif /* SLOPE_TYPE_H */
117