OpenTTD Source  20240917-master-g9ab0a47812
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 constexpr inline bool IsValidCorner(Corner corner)
25 {
26  return IsInsideMM(corner, 0, CORNER_END);
27 }
28 
29 
36 static constexpr inline bool IsSteepSlope(Slope s)
37 {
38  return (s & SLOPE_STEEP) != 0;
39 }
40 
47 static constexpr inline bool IsHalftileSlope(Slope s)
48 {
49  return (s & SLOPE_HALFTILE) != 0;
50 }
51 
60 static constexpr inline Slope RemoveHalftileSlope(Slope s)
61 {
62  return s & ~SLOPE_HALFTILE_MASK;
63 }
64 
77 {
78  assert(!IsSteepSlope(s) && !IsHalftileSlope(s));
79  return s ^ SLOPE_ELEVATED;
80 }
81 
89 {
90  return (s == SLOPE_W) || (s == SLOPE_S) || (s == SLOPE_E) || (s == SLOPE_N);
91 }
92 
100 {
101  assert(IsValidCorner(corner));
102  return (Slope)(1 << corner);
103 }
104 
114 {
115  s = RemoveHalftileSlope(s);
117 }
118 
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 constexpr inline Corner GetHalftileSlopeCorner(Slope s)
149 {
150  assert(IsHalftileSlope(s));
151  return (Corner)((s >> 6) & 3);
152 }
153 
160 static constexpr 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 constexpr inline int GetSlopeMaxPixelZ(Slope s)
174 {
175  return GetSlopeMaxZ(s) * TILE_HEIGHT;
176 }
177 
185 {
186  return (Corner)(corner ^ 2);
187 }
188 
196 {
198 }
199 
207 {
209 }
210 
217 inline Slope SteepSlope(Corner corner)
218 {
220 }
221 
228 inline bool IsInclinedSlope(Slope s)
229 {
230  return (s == SLOPE_NW) || (s == SLOPE_SW) || (s == SLOPE_SE) || (s == SLOPE_NE);
231 }
232 
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 
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 constexpr inline Slope HalftileSlope(Slope s, Corner corner)
275 {
276  assert(IsValidCorner(corner));
277  return (Slope)(s | SLOPE_HALFTILE | (corner << 6));
278 }
279 
280 
287 inline bool IsFoundation(Foundation f)
288 {
289  return f != FOUNDATION_NONE;
290 }
291 
299 {
300  return f == FOUNDATION_LEVELED;
301 }
302 
310 {
311  return (f == FOUNDATION_INCLINED_X) || (f == FOUNDATION_INCLINED_Y);
312 }
313 
321 {
323 }
324 
334 {
336  return (Corner)(f - FOUNDATION_HALFTILE_W);
337 }
338 
346 {
348 }
349 
357 {
358  assert(IsSpecialRailFoundation(f));
359  return (Corner)(f - FOUNDATION_RAIL_W);
360 }
361 
370 {
372 }
373 
381 {
383 }
384 
392 {
393  assert(IsValidCorner(corner));
394  return static_cast<Foundation>(static_cast<uint>(FOUNDATION_HALFTILE_W) + static_cast<uint>(corner));
395 }
396 
404 {
405  assert(IsValidCorner(corner));
406  return static_cast<Foundation>(static_cast<uint>(FOUNDATION_RAIL_W) + static_cast<uint>(corner));
407 }
408 
416 {
417  extern const uint8_t _slope_to_sprite_offset[32];
418  return _slope_to_sprite_offset[s];
419 }
420 
421 #endif /* SLOPE_FUNC_H */
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
IsFoundation
bool IsFoundation(Foundation f)
Tests for FOUNDATION_NONE.
Definition: slope_func.h:287
SLOPE_SE
@ SLOPE_SE
south and east corner are raised
Definition: slope_type.h:57
HalftileSlope
static constexpr 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:75
FOUNDATION_HALFTILE_N
@ FOUNDATION_HALFTILE_N
Level north halftile non-continuously.
Definition: slope_type.h:105
IsInclinedSlope
bool IsInclinedSlope(Slope s)
Tests if a specific slope is an inclined slope.
Definition: slope_func.h:228
direction_type.h
IsHalftileSlope
static constexpr bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:47
IsInsideMM
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:268
FOUNDATION_RAIL_W
@ FOUNDATION_RAIL_W
Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
Definition: slope_type.h:108
FlatteningFoundation
Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
SLOPE_NE
@ SLOPE_NE
north and east corner are raised
Definition: slope_type.h:58
RemoveHalftileSlope
static constexpr Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
InclinedFoundation
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
math_func.hpp
HalftileFoundation
Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition: slope_func.h:391
IsSteepSlope
static constexpr bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
SLOPE_NW
@ SLOPE_NW
north and west corner are raised
Definition: slope_type.h:55
FOUNDATION_HALFTILE_W
@ FOUNDATION_HALFTILE_W
Level west halftile non-continuously.
Definition: slope_type.h:102
SLOPE_STEEP_E
@ SLOPE_STEEP_E
a steep slope falling to west (from east)
Definition: slope_type.h:68
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
GetHalftileSlopeCorner
static constexpr Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:148
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
Foundation
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
SLOPE_STEEP_S
@ SLOPE_STEEP_S
a steep slope falling to north (from south)
Definition: slope_type.h:67
SLOPE_HALFTILE
@ SLOPE_HALFTILE
one halftile is leveled (non continuous slope)
Definition: slope_type.h:71
SlopeWithThreeCornersRaised
Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:206
ComplementSlope
Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:78
FOUNDATION_INCLINED_Y
@ FOUNDATION_INCLINED_Y
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:97
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:76
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
GetSlopeMaxPixelZ
static constexpr int GetSlopeMaxPixelZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:173
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:80
GetSlopeMaxZ
static constexpr int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
Definition: slope_func.h:160
SLOPE_HALFTILE_MASK
@ SLOPE_HALFTILE_MASK
three bits used for halftile slopes
Definition: slope_type.h:72
FOUNDATION_INCLINED_X
@ FOUNDATION_INCLINED_X
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:96
IsValidCorner
static constexpr bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:24
Corner
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
IsNonContinuousFoundation
bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition: slope_func.h:320
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:77
HasSlopeHighestCorner
bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition: slope_func.h:113
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
GetHalftileFoundationCorner
Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition: slope_func.h:333
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:117
SLOPE_STEEP_W
@ SLOPE_STEEP_W
a steep slope falling to east (from west)
Definition: slope_type.h:66
FOUNDATION_RAIL_N
@ FOUNDATION_RAIL_N
Foundation for TRACK_BIT_UPPER, but not a leveled foundation.
Definition: slope_type.h:111
SLOPE_STEEP
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
tile_type.h
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
IsSlopeWithThreeCornersRaised
bool IsSlopeWithThreeCornersRaised(Slope s)
Tests if a specific slope has exactly three corners raised.
Definition: slope_func.h:195
GetHighestSlopeCorner
Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:126
IsLeveledFoundation
bool IsLeveledFoundation(Foundation f)
Tests if the foundation is a leveled foundation.
Definition: slope_func.h:298
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
GetRailFoundationCorner
Corner GetRailFoundationCorner(Foundation f)
Returns the track corner of a special rail foundation.
Definition: slope_func.h:356
SLOPE_ELEVATED
@ SLOPE_ELEVATED
bit mask containing all 'simple' slopes
Definition: slope_type.h:61
IsInclinedFoundation
bool IsInclinedFoundation(Foundation f)
Tests if the foundation is an inclined foundation.
Definition: slope_func.h:309
_slope_to_sprite_offset
const uint8_t _slope_to_sprite_offset[32]
landscape slope => sprite
SpecialRailFoundation
Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:403
IsSpecialRailFoundation
bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:345
SteepSlope
Slope SteepSlope(Corner corner)
Returns a specific steep slope.
Definition: slope_func.h:217
InclinedSlope
Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition: slope_func.h:256
SlopeToSpriteOffset
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18
SlopeWithOneCornerRaised
Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:99
slope_type.h
IsSlopeWithOneCornerRaised
bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:88
GetInclinedSlopeDirection
DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:239
SLOPE_STEEP_N
@ SLOPE_STEEP_N
a steep slope falling to south (from north)
Definition: slope_type.h:69
OppositeCorner
Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:184