OpenTTD Source 20260711-master-g3fb3006dff
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
24static constexpr inline bool IsValidCorner(Corner corner)
25{
26 return IsInsideMM(corner, 0, CORNER_END);
27}
28
29
36static constexpr inline bool IsSteepSlope(Slope s)
37{
38 return (s & SLOPE_STEEP) != 0;
39}
40
46static constexpr Slope RemoveSteepSlope(Slope s)
47{
48 return s & ~SLOPE_STEEP;
49}
50
57static constexpr inline bool IsHalftileSlope(Slope s)
58{
59 return (s & SLOPE_HALFTILE) != 0;
60}
61
70static constexpr inline Slope RemoveHalftileSlope(Slope s)
71{
72 return s & ~SLOPE_HALFTILE_MASK;
73}
74
87{
88 assert(!IsSteepSlope(s) && !IsHalftileSlope(s));
89 return s ^ SLOPE_ELEVATED;
90}
91
99{
100 return (s == SLOPE_W) || (s == SLOPE_S) || (s == SLOPE_E) || (s == SLOPE_N);
101}
102
110{
111 assert(IsValidCorner(corner));
112 return (Slope)(1 << corner);
113}
114
124{
125 s = RemoveHalftileSlope(s);
127}
128
137{
138 switch (RemoveHalftileSlope(s)) {
139 case SLOPE_W:
140 case SLOPE_STEEP_W: return CORNER_W;
141 case SLOPE_S:
142 case SLOPE_STEEP_S: return CORNER_S;
143 case SLOPE_E:
144 case SLOPE_STEEP_E: return CORNER_E;
145 case SLOPE_N:
146 case SLOPE_STEEP_N: return CORNER_N;
147 default: NOT_REACHED();
148 }
149}
150
158static constexpr inline Corner GetHalftileSlopeCorner(Slope s)
159{
160 assert(IsHalftileSlope(s));
161 return (Corner)((s >> 6) & 3);
162}
163
170static constexpr inline int GetSlopeMaxZ(Slope s)
171{
172 if (s == SLOPE_FLAT) return 0;
173 if (IsSteepSlope(s)) return 2;
174 return 1;
175}
176
183static constexpr inline int GetSlopeMaxPixelZ(Slope s)
184{
185 return GetSlopeMaxZ(s) * TILE_HEIGHT;
186}
187
195{
196 return (Corner)(corner ^ 2);
197}
198
209
220
227inline Slope SteepSlope(Corner corner)
228{
230}
231
238inline bool IsInclinedSlope(Slope s)
239{
240 return (s == SLOPE_NW) || (s == SLOPE_SW) || (s == SLOPE_SE) || (s == SLOPE_NE);
241}
242
250{
251 switch (s) {
252 case SLOPE_NE: return DiagDirection::NE;
253 case SLOPE_SE: return DiagDirection::SE;
254 case SLOPE_SW: return DiagDirection::SW;
255 case SLOPE_NW: return DiagDirection::NW;
256 default: return DiagDirection::Invalid;
257 }
258}
259
267{
268 switch (dir) {
269 case DiagDirection::NE: return SLOPE_NE;
270 case DiagDirection::SE: return SLOPE_SE;
271 case DiagDirection::SW: return SLOPE_SW;
272 case DiagDirection::NW: return SLOPE_NW;
273 default: NOT_REACHED();
274 }
275}
276
284static constexpr inline Slope HalftileSlope(Slope s, Corner corner)
285{
286 assert(IsValidCorner(corner));
287 return (Slope)(s | SLOPE_HALFTILE | (corner << 6));
288}
289
290
298{
299 return f != Foundation::None;
300}
301
309{
310 return f == Foundation::Leveled;
311}
312
320{
321 return (f == Foundation::InclinedX) || (f == Foundation::InclinedY);
322}
323
334
348
359
371
381
392
400{
401 assert(IsValidCorner(corner));
402 return static_cast<Foundation>(static_cast<uint>(Foundation::HalfTileW) + static_cast<uint>(corner));
403}
404
412{
413 assert(IsValidCorner(corner));
414 return static_cast<Foundation>(static_cast<uint>(Foundation::RailW) + static_cast<uint>(corner));
415}
416
424{
425 extern const uint8_t _slope_to_sprite_offset[32];
426 return _slope_to_sprite_offset[s];
427}
428
429#endif /* SLOPE_FUNC_H */
Different types to 'show' directions.
Axis
Enumeration for the two axis X and Y.
@ X
The X axis.
DiagDirection
Enumeration for diagonal directions.
@ Invalid
Flag for an invalid DiagDirection.
@ SW
Southwest.
@ NW
Northwest.
@ NE
Northeast, upper right on your monitor.
@ SE
Southeast.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
const uint8_t _slope_to_sprite_offset[32]
landscape slope => sprite
Integer math functions.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track.
Definition slope_func.h:355
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:170
Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition slope_func.h:216
Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition slope_func.h:194
static constexpr Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition slope_func.h:158
static constexpr Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition slope_func.h:70
bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition slope_func.h:98
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition slope_func.h:423
bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition slope_func.h:330
Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition slope_func.h:136
Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition slope_func.h:343
bool IsLeveledFoundation(Foundation f)
Tests if the foundation is a leveled foundation.
Definition slope_func.h:308
static constexpr bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition slope_func.h:24
bool IsFoundation(Foundation f)
Tests whether the given foundation is a foundation.
Definition slope_func.h:297
bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition slope_func.h:123
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:205
static constexpr Slope RemoveSteepSlope(Slope s)
Removes a steep flag from a slope.
Definition slope_func.h:46
bool IsInclinedSlope(Slope s)
Tests if a specific slope is an inclined slope.
Definition slope_func.h:238
Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition slope_func.h:377
Slope SteepSlope(Corner corner)
Returns a specific steep slope.
Definition slope_func.h:227
Corner GetRailFoundationCorner(Foundation f)
Returns the track corner of a special rail foundation.
Definition slope_func.h:366
static constexpr bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition slope_func.h:57
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:183
DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition slope_func.h:249
Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition slope_func.h:399
static constexpr Slope HalftileSlope(Slope s, Corner corner)
Adds a halftile slope to a slope.
Definition slope_func.h:284
Foundation InclinedFoundation(Axis axis)
Returns the along a specific axis inclined foundation.
Definition slope_func.h:388
Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition slope_func.h:266
Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition slope_func.h:411
Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition slope_func.h:86
Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition slope_func.h:109
bool IsInclinedFoundation(Foundation f)
Tests if the foundation is an inclined foundation.
Definition slope_func.h:319
Definitions of a slope.
Corner
Enumeration of tile corners.
Definition slope_type.h:21
Slope
Enumeration for the slope-type.
Definition slope_type.h:53
@ SLOPE_W
the west corner of the tile is raised
Definition slope_type.h:55
@ SLOPE_HALFTILE_MASK
three bits used for halftile slopes
Definition slope_type.h:77
@ SLOPE_ELEVATED
bit mask containing all 'simple' slopes
Definition slope_type.h:66
@ SLOPE_E
the east corner of the tile is raised
Definition slope_type.h:57
@ SLOPE_S
the south corner of the tile is raised
Definition slope_type.h:56
@ SLOPE_N
the north corner of the tile is raised
Definition slope_type.h:58
@ SLOPE_SW
south and west corner are raised
Definition slope_type.h:61
@ SLOPE_FLAT
a flat tile
Definition slope_type.h:54
@ SLOPE_STEEP_W
a steep slope falling to east (from west)
Definition slope_type.h:71
@ SLOPE_NE
north and east corner are raised
Definition slope_type.h:63
@ SLOPE_STEEP_E
a steep slope falling to west (from east)
Definition slope_type.h:73
@ SLOPE_SE
south and east corner are raised
Definition slope_type.h:62
@ SLOPE_NW
north and west corner are raised
Definition slope_type.h:60
@ SLOPE_STEEP_N
a steep slope falling to south (from north)
Definition slope_type.h:74
@ SLOPE_STEEP_S
a steep slope falling to north (from south)
Definition slope_type.h:72
@ SLOPE_HALFTILE
one halftile is leveled (non continuous slope)
Definition slope_type.h:76
@ SLOPE_STEEP
indicates the slope is steep
Definition slope_type.h:59
Foundation
Enumeration for Foundations.
Definition slope_type.h:98
@ Leveled
The tile is leveled up to a flat slope.
Definition slope_type.h:100
@ RailW
Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
Definition slope_type.h:114
@ None
The tile has no foundation, the slope remains unchanged.
Definition slope_type.h:99
@ End
End marker.
Definition slope_type.h:118
@ InclinedY
The tile has an along Y-axis inclined foundation.
Definition slope_type.h:102
@ SteepBoth
The tile has a steep slope. The lowest corner is raised by a foundation and the upper halftile is lev...
Definition slope_type.h:106
@ InclinedX
The tile has an along X-axis inclined foundation.
Definition slope_type.h:101
@ HalfTileEnd
End marker for halftile foundations.
Definition slope_type.h:111
@ HalfTileW
Level west halftile non-continuously.
Definition slope_type.h:107
Types related to tiles.
static constexpr uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in ZOOM_BASE.
Definition tile_type.h:18