OpenTTD Source: src/slope_func.h Source File

OpenTTD Source 13.1

slope_func.h
Go to the documentation of this file.
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
10 #ifndef SLOPE_FUNC_H
11 #define SLOPE_FUNC_H
12
13 #include "core/math_func.hpp"
14 #include "slope_type.h"
15 #include "direction_type.h"
16 #include "tile_type.h"
17
24 static inline bool IsValidCorner(Corner corner)
25 {
26 return IsInsideMM(corner, 0, CORNER_END);
27 }
28
29
36 static inline bool IsSteepSlope(Slope s)
37 {
38 return (s & SLOPE_STEEP) != 0;
39 }
40
47 static inline bool IsHalftileSlope(Slope s)
48 {
49 return (s & SLOPE_HALFTILE) != 0;
50 }
51
60 static inline Slope RemoveHalftileSlope(Slope s)
61 {
62 return s & ~SLOPE_HALFTILE_MASK;
63 }
64
76 static inline Slope ComplementSlope(Slope s)
77 {
78 assert(!IsSteepSlope(s) && !IsHalftileSlope(s));
79 return s ^ SLOPE_ELEVATED;
80 }
81
88 static inline bool IsSlopeWithOneCornerRaised(Slope s)
89 {
90 return (s == SLOPE_W) || (s == SLOPE_S) || (s == SLOPE_E) || (s == SLOPE_N);
91 }
92
99 static inline Slope SlopeWithOneCornerRaised(Corner corner)
100 {
101 assert(IsValidCorner(corner));
102 return (Slope)(1 << corner);
103 }
104
113 static inline bool HasSlopeHighestCorner(Slope s)
114 {
115 s = RemoveHalftileSlope(s);
116 return IsSteepSlope(s) || IsSlopeWithOneCornerRaised(s);
117 }
118
126 static inline Corner GetHighestSlopeCorner(Slope s)
127 {
128 switch (RemoveHalftileSlope(s)) {
129 case SLOPE_W:
130 case SLOPE_STEEP_W: return CORNER_W;
131 case SLOPE_S:
132 case SLOPE_STEEP_S: return CORNER_S;
133 case SLOPE_E:
134 case SLOPE_STEEP_E: return CORNER_E;
135 case SLOPE_N:
136 case SLOPE_STEEP_N: return CORNER_N;
137 default: NOT_REACHED();
138 }
139 }
140
148 static inline Corner GetHalftileSlopeCorner(Slope s)
149 {
150 assert(IsHalftileSlope(s));
151 return (Corner)((s >> 6) & 3);
152 }
153
160 static inline int GetSlopeMaxZ(Slope s)
161 {
162 if (s == SLOPE_FLAT) return 0;
163 if (IsSteepSlope(s)) return 2;
164 return 1;
165 }
166
173 static inline int GetSlopeMaxPixelZ(Slope s)
174 {
175 return GetSlopeMaxZ(s) * TILE_HEIGHT;
176 }
177
184 static inline Corner OppositeCorner(Corner corner)
185 {
186 return (Corner)(corner ^ 2);
187 }
188
195 static inline bool IsSlopeWithThreeCornersRaised(Slope s)
196 {
197 return !IsHalftileSlope(s) && !IsSteepSlope(s) && IsSlopeWithOneCornerRaised(ComplementSlope(s));
198 }
199
206 static inline Slope SlopeWithThreeCornersRaised(Corner corner)
207 {
208 return ComplementSlope(SlopeWithOneCornerRaised(corner));
209 }
210
217 static inline Slope SteepSlope(Corner corner)
218 {
219 return SLOPE_STEEP | SlopeWithThreeCornersRaised(OppositeCorner(corner));
220 }
221
228 static inline bool IsInclinedSlope(Slope s)
229 {
230 return (s == SLOPE_NW) || (s == SLOPE_SW) || (s == SLOPE_SE) || (s == SLOPE_NE);
231 }
232
239 static inline DiagDirection GetInclinedSlopeDirection(Slope s)
240 {
241 switch (s) {
242 case SLOPE_NE: return DIAGDIR_NE;
243 case SLOPE_SE: return DIAGDIR_SE;
244 case SLOPE_SW: return DIAGDIR_SW;
245 case SLOPE_NW: return DIAGDIR_NW;
246 default: return INVALID_DIAGDIR;
247 }
248 }
249
256 static inline Slope InclinedSlope(DiagDirection dir)
257 {
258 switch (dir) {
259 case DIAGDIR_NE: return SLOPE_NE;
260 case DIAGDIR_SE: return SLOPE_SE;
261 case DIAGDIR_SW: return SLOPE_SW;
262 case DIAGDIR_NW: return SLOPE_NW;
263 default: NOT_REACHED();
264 }
265 }
266
274 static inline Slope HalftileSlope(Slope s, Corner corner)
275 {
276 assert(IsValidCorner(corner));
277 return (Slope)(s | SLOPE_HALFTILE | (corner << 6));
278 }
279
280
287 static inline bool IsFoundation(Foundation f)
288 {
289 return f != FOUNDATION_NONE;
290 }
291
298 static inline bool IsLeveledFoundation(Foundation f)
299 {
300 return f == FOUNDATION_LEVELED;
301 }
302
309 static inline bool IsInclinedFoundation(Foundation f)
310 {
311 return (f == FOUNDATION_INCLINED_X) || (f == FOUNDATION_INCLINED_Y);
312 }
313
320 static inline bool IsNonContinuousFoundation(Foundation f)
321 {
322 return IsInsideMM(f, FOUNDATION_STEEP_BOTH, FOUNDATION_HALFTILE_N + 1);
323 }
324
333 static inline Corner GetHalftileFoundationCorner(Foundation f)
334 {
335 assert(IsInsideMM(f, FOUNDATION_HALFTILE_W, FOUNDATION_HALFTILE_N + 1));
336 return (Corner)(f - FOUNDATION_HALFTILE_W);
337 }
338
345 static inline bool IsSpecialRailFoundation(Foundation f)
346 {
347 return IsInsideMM(f, FOUNDATION_RAIL_W, FOUNDATION_RAIL_N + 1);
348 }
349
356 static inline Corner GetRailFoundationCorner(Foundation f)
357 {
358 assert(IsSpecialRailFoundation(f));
359 return (Corner)(f - FOUNDATION_RAIL_W);
360 }
361
369 static inline Foundation FlatteningFoundation(Slope s)
370 {
371 return (s == SLOPE_FLAT ? FOUNDATION_NONE : FOUNDATION_LEVELED);
372 }
373
380 static inline Foundation InclinedFoundation(Axis axis)
381 {
382 return (axis == AXIS_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
383 }
384
391 static inline Foundation HalftileFoundation(Corner corner)
392 {
393 assert(IsValidCorner(corner));
394 return (Foundation)(FOUNDATION_HALFTILE_W + corner);
395 }
396
403 static inline Foundation SpecialRailFoundation(Corner corner)
404 {
405 assert(IsValidCorner(corner));
406 return (Foundation)(FOUNDATION_RAIL_W + corner);
407 }
408
415 static inline uint SlopeToSpriteOffset(Slope s)
416 {
417 extern const byte _slope_to_sprite_offset[32];
418 return _slope_to_sprite_offset[s];
419 }
420
421 #endif /* SLOPE_FUNC_H */
OppositeCorner
static Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:184
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:80
SLOPE_SE
@ SLOPE_SE
south and east corner are raised
Definition: slope_type.h:57
IsInsideMM
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:204
FOUNDATION_HALFTILE_N
@ FOUNDATION_HALFTILE_N
Level north halftile non-continuously.
Definition: slope_type.h:105
SLOPE_STEEP_E
@ SLOPE_STEEP_E
a steep slope falling to west (from east)
Definition: slope_type.h:68
direction_type.h
SpecialRailFoundation
static Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:403
SLOPE_STEEP_S
@ SLOPE_STEEP_S
a steep slope falling to north (from south)
Definition: slope_type.h:67
InclinedSlope
static Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition: slope_func.h:256
FOUNDATION_RAIL_W
@ FOUNDATION_RAIL_W
Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
Definition: slope_type.h:108
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:125
SLOPE_NW
@ SLOPE_NW
north and west corner are raised
Definition: slope_type.h:55
math_func.hpp
IsFoundation
static bool IsFoundation(Foundation f)
Tests for FOUNDATION_NONE.
Definition: slope_func.h:287
SLOPE_ELEVATED
@ SLOPE_ELEVATED
bit mask containing all 'simple' slopes
Definition: slope_type.h:61
IsHalftileSlope
static bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:47
RemoveHalftileSlope
static Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
FOUNDATION_HALFTILE_W
@ FOUNDATION_HALFTILE_W
Level west halftile non-continuously.
Definition: slope_type.h:102
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:82
GetHighestSlopeCorner
static Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:126
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
Foundation
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
FlatteningFoundation
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
SlopeToSpriteOffset
static uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
SLOPE_HALFTILE
@ SLOPE_HALFTILE
one halftile is leveled (non continuous slope)
Definition: slope_type.h:71
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:81
GetSlopeMaxZ
static int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:160
FOUNDATION_INCLINED_Y
@ FOUNDATION_INCLINED_Y
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:97
SteepSlope
static Slope SteepSlope(Corner corner)
Returns a specific steep slope.
Definition: slope_func.h:217
IsSteepSlope
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
FOUNDATION_STEEP_BOTH
@ FOUNDATION_STEEP_BOTH
The tile has a steep slope. The lowest corner is raised by a foundation and the upper halftile is lev...
Definition: slope_type.h:101
SLOPE_NE
@ SLOPE_NE
north and east corner are raised
Definition: slope_type.h:58
IsSlopeWithThreeCornersRaised
static bool IsSlopeWithThreeCornersRaised(Slope s)
Tests if a specific slope has exactly three corners raised.
Definition: slope_func.h:195
IsInclinedFoundation
static bool IsInclinedFoundation(Foundation f)
Tests if the foundation is an inclined foundation.
Definition: slope_func.h:309
HasSlopeHighestCorner
static bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition: slope_func.h:113
_slope_to_sprite_offset
const byte _slope_to_sprite_offset[32]
landscape slope => sprite
FOUNDATION_INCLINED_X
@ FOUNDATION_INCLINED_X
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:96
Corner
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
IsNonContinuousFoundation
static bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition: slope_func.h:320
SlopeWithThreeCornersRaised
static Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:206
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:84
GetHalftileSlopeCorner
static Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:148
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
SLOPE_HALFTILE_MASK
@ SLOPE_HALFTILE_MASK
three bits used for halftile slopes
Definition: slope_type.h:72
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
IsValidCorner
static bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:24
FOUNDATION_RAIL_N
@ FOUNDATION_RAIL_N
Foundation for TRACK_BIT_UPPER, but not a leveled foundation.
Definition: slope_type.h:111
SlopeWithOneCornerRaised
static Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:99
IsSpecialRailFoundation
static bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:345
tile_type.h
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
IsSlopeWithOneCornerRaised
static bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:88
GetSlopeMaxPixelZ
static int GetSlopeMaxPixelZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:173
SLOPE_SW
@ SLOPE_SW
south and west corner are raised
Definition: slope_type.h:56
FOUNDATION_LEVELED
@ FOUNDATION_LEVELED
The tile is leveled up to a flat slope.
Definition: slope_type.h:95
HalftileFoundation
static Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition: slope_func.h:391
GetRailFoundationCorner
static Corner GetRailFoundationCorner(Foundation f)
Returns the track corner of a special rail foundation.
Definition: slope_func.h:356
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:126
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
IsInclinedSlope
static bool IsInclinedSlope(Slope s)
Tests if a specific slope is an inclined slope.
Definition: slope_func.h:228
SLOPE_STEEP_W
@ SLOPE_STEEP_W
a steep slope falling to east (from west)
Definition: slope_type.h:66
InclinedFoundation
static Foundation InclinedFoundation(Axis axis)
Returns the along a specific axis inclined foundation.
Definition: slope_func.h:380
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
HalftileSlope
static Slope HalftileSlope(Slope s, Corner corner)
Adds a halftile slope to a slope.
Definition: slope_func.h:274
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:79
slope_type.h
SLOPE_STEEP
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54
IsLeveledFoundation
static bool IsLeveledFoundation(Foundation f)
Tests if the foundation is a leveled foundation.
Definition: slope_func.h:298
ComplementSlope
static Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
GetHalftileFoundationCorner
static Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition: slope_func.h:333
SLOPE_STEEP_N
@ SLOPE_STEEP_N
a steep slope falling to south (from north)
Definition: slope_type.h:69
GetInclinedSlopeDirection
static DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:239