OpenTTD Source  20241108-master-g80f628063a
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 */
Different types to 'show' directions.
Axis
Allow incrementing of DiagDirDiff variables.
@ AXIS_X
The X axis.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_NW
Northwest.
@ DIAGDIR_SE
Southeast.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
@ DIAGDIR_SW
Southwest.
const uint8_t _slope_to_sprite_offset[32]
landscape slope => sprite
Integer math functions.
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
bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:345
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 SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:206
Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:184
static constexpr Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:148
static constexpr Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:88
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition: slope_func.h:320
Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:126
Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition: slope_func.h:333
bool IsLeveledFoundation(Foundation f)
Tests if the foundation is a leveled foundation.
Definition: slope_func.h:298
static constexpr bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:24
bool IsFoundation(Foundation f)
Tests for FOUNDATION_NONE.
Definition: slope_func.h:287
bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition: slope_func.h:113
static constexpr bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
bool IsSlopeWithThreeCornersRaised(Slope s)
Tests if a specific slope has exactly three corners raised.
Definition: slope_func.h:195
bool IsInclinedSlope(Slope s)
Tests if a specific slope is an inclined slope.
Definition: slope_func.h:228
Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
Slope SteepSlope(Corner corner)
Returns a specific steep slope.
Definition: slope_func.h:217
Corner GetRailFoundationCorner(Foundation f)
Returns the track corner of a special rail foundation.
Definition: slope_func.h:356
static constexpr bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:47
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
DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:239
Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition: slope_func.h:391
static constexpr Slope HalftileSlope(Slope s, Corner corner)
Adds a halftile slope to a slope.
Definition: slope_func.h:274
Foundation InclinedFoundation(Axis axis)
Returns the along a specific axis inclined foundation.
Definition: slope_func.h:380
Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition: slope_func.h:256
Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:403
Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:99
bool IsInclinedFoundation(Foundation f)
Tests if the foundation is an inclined foundation.
Definition: slope_func.h:309
Definitions of a slope.
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
@ SLOPE_HALFTILE_MASK
three bits used for halftile slopes
Definition: slope_type.h:72
@ SLOPE_ELEVATED
bit mask containing all 'simple' slopes
Definition: slope_type.h:61
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
@ SLOPE_SW
south and west corner are raised
Definition: slope_type.h:56
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
@ SLOPE_STEEP_W
a steep slope falling to east (from west)
Definition: slope_type.h:66
@ SLOPE_NE
north and east corner are raised
Definition: slope_type.h:58
@ SLOPE_STEEP_E
a steep slope falling to west (from east)
Definition: slope_type.h:68
@ SLOPE_SE
south and east corner are raised
Definition: slope_type.h:57
@ SLOPE_NW
north and west corner are raised
Definition: slope_type.h:55
@ SLOPE_STEEP_N
a steep slope falling to south (from north)
Definition: slope_type.h:69
@ SLOPE_STEEP_S
a steep slope falling to north (from south)
Definition: slope_type.h:67
@ SLOPE_HALFTILE
one halftile is leveled (non continuous slope)
Definition: slope_type.h:71
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
@ FOUNDATION_RAIL_W
Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
Definition: slope_type.h:108
@ FOUNDATION_RAIL_N
Foundation for TRACK_BIT_UPPER, but not a leveled foundation.
Definition: slope_type.h:111
@ FOUNDATION_LEVELED
The tile is leveled up to a flat slope.
Definition: slope_type.h:95
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
@ FOUNDATION_INCLINED_X
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:96
@ 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
@ FOUNDATION_INCLINED_Y
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:97
@ FOUNDATION_HALFTILE_W
Level west halftile non-continuously.
Definition: slope_type.h:102
@ FOUNDATION_HALFTILE_N
Level north halftile non-continuously.
Definition: slope_type.h:105
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
Types related to tiles.
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18