OpenTTD
slope_func.h
Go to the documentation of this file.
1 /* $Id: slope_func.h 23106 2011-11-04 11:30:37Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef SLOPE_FUNC_H
13 #define SLOPE_FUNC_H
14 
15 #include "core/math_func.hpp"
16 #include "slope_type.h"
17 #include "direction_type.h"
18 #include "tile_type.h"
19 
26 static inline bool IsValidCorner(Corner corner)
27 {
28  return IsInsideMM(corner, 0, CORNER_END);
29 }
30 
31 
38 static inline bool IsSteepSlope(Slope s)
39 {
40  return (s & SLOPE_STEEP) != 0;
41 }
42 
49 static inline bool IsHalftileSlope(Slope s)
50 {
51  return (s & SLOPE_HALFTILE) != 0;
52 }
53 
63 {
64  return s & ~SLOPE_HALFTILE_MASK;
65 }
66 
78 static inline Slope ComplementSlope(Slope s)
79 {
80  assert(!IsSteepSlope(s) && !IsHalftileSlope(s));
81  return s ^ SLOPE_ELEVATED;
82 }
83 
90 static inline bool IsSlopeWithOneCornerRaised(Slope s)
91 {
92  return (s == SLOPE_W) || (s == SLOPE_S) || (s == SLOPE_E) || (s == SLOPE_N);
93 }
94 
101 static inline Slope SlopeWithOneCornerRaised(Corner corner)
102 {
103  assert(IsValidCorner(corner));
104  return (Slope)(1 << corner);
105 }
106 
115 static inline bool HasSlopeHighestCorner(Slope s)
116 {
117  s = RemoveHalftileSlope(s);
119 }
120 
129 {
130  switch (RemoveHalftileSlope(s)) {
131  case SLOPE_W:
132  case SLOPE_STEEP_W: return CORNER_W;
133  case SLOPE_S:
134  case SLOPE_STEEP_S: return CORNER_S;
135  case SLOPE_E:
136  case SLOPE_STEEP_E: return CORNER_E;
137  case SLOPE_N:
138  case SLOPE_STEEP_N: return CORNER_N;
139  default: NOT_REACHED();
140  }
141 }
142 
151 {
152  assert(IsHalftileSlope(s));
153  return (Corner)((s >> 6) & 3);
154 }
155 
162 static inline int GetSlopeMaxZ(Slope s)
163 {
164  if (s == SLOPE_FLAT) return 0;
165  if (IsSteepSlope(s)) return 2;
166  return 1;
167 }
168 
175 static inline int GetSlopeMaxPixelZ(Slope s)
176 {
177  return GetSlopeMaxZ(s) * TILE_HEIGHT;
178 }
179 
186 static inline Corner OppositeCorner(Corner corner)
187 {
188  return (Corner)(corner ^ 2);
189 }
190 
198 {
200 }
201 
209 {
211 }
212 
219 static inline Slope SteepSlope(Corner corner)
220 {
222 }
223 
230 static inline bool IsInclinedSlope(Slope s)
231 {
232  return (s == SLOPE_NW) || (s == SLOPE_SW) || (s == SLOPE_SE) || (s == SLOPE_NE);
233 }
234 
242 {
243  switch (s) {
244  case SLOPE_NE: return DIAGDIR_NE;
245  case SLOPE_SE: return DIAGDIR_SE;
246  case SLOPE_SW: return DIAGDIR_SW;
247  case SLOPE_NW: return DIAGDIR_NW;
248  default: return INVALID_DIAGDIR;
249  }
250 }
251 
259 {
260  switch (dir) {
261  case DIAGDIR_NE: return SLOPE_NE;
262  case DIAGDIR_SE: return SLOPE_SE;
263  case DIAGDIR_SW: return SLOPE_SW;
264  case DIAGDIR_NW: return SLOPE_NW;
265  default: NOT_REACHED();
266  }
267 }
268 
276 static inline Slope HalftileSlope(Slope s, Corner corner)
277 {
278  assert(IsValidCorner(corner));
279  return (Slope)(s | SLOPE_HALFTILE | (corner << 6));
280 }
281 
282 
289 static inline bool IsFoundation(Foundation f)
290 {
291  return f != FOUNDATION_NONE;
292 }
293 
300 static inline bool IsLeveledFoundation(Foundation f)
301 {
302  return f == FOUNDATION_LEVELED;
303 }
304 
311 static inline bool IsInclinedFoundation(Foundation f)
312 {
313  return (f == FOUNDATION_INCLINED_X) || (f == FOUNDATION_INCLINED_Y);
314 }
315 
323 {
325 }
326 
336 {
338  return (Corner)(f - FOUNDATION_HALFTILE_W);
339 }
340 
347 static inline bool IsSpecialRailFoundation(Foundation f)
348 {
350 }
351 
359 {
360  assert(IsSpecialRailFoundation(f));
361  return (Corner)(f - FOUNDATION_RAIL_W);
362 }
363 
372 {
374 }
375 
382 static inline Foundation InclinedFoundation(Axis axis)
383 {
385 }
386 
393 static inline Foundation HalftileFoundation(Corner corner)
394 {
395  assert(IsValidCorner(corner));
396  return (Foundation)(FOUNDATION_HALFTILE_W + corner);
397 }
398 
406 {
407  assert(IsValidCorner(corner));
408  return (Foundation)(FOUNDATION_RAIL_W + corner);
409 }
410 
417 static inline uint SlopeToSpriteOffset(Slope s)
418 {
419  extern const byte _slope_to_sprite_offset[32];
420  return _slope_to_sprite_offset[s];
421 }
422 
423 #endif /* SLOPE_FUNC_H */
static bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition: slope_func.h:115
static bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:49
the north corner of the tile is raised
Definition: slope_type.h:55
Corner
Enumeration of tile corners.
Definition: slope_type.h:24
Different types to &#39;show&#39; directions.
Definitions of a slope.
south and east corner are raised
Definition: slope_type.h:59
the west corner of the tile is raised
Definition: slope_type.h:52
Flag for an invalid DiagDirection.
static Corner GetHalftileFoundationCorner(Foundation f)
Returns the halftile corner of a halftile-foundation.
Definition: slope_func.h:335
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
const byte _slope_to_sprite_offset[32]
landscape slope => sprite
static Foundation HalftileFoundation(Corner corner)
Returns the halftile foundation for single horizontal/vertical track.
Definition: slope_func.h:393
static bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:26
static bool IsFoundation(Foundation f)
Tests for FOUNDATION_NONE.
Definition: slope_func.h:289
static int GetSlopeMaxZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height) ...
Definition: slope_func.h:162
the east corner of the tile is raised
Definition: slope_type.h:54
Northeast, upper right on your monitor.
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:38
static Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:150
south and west corner are raised
Definition: slope_type.h:58
static Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:101
static bool IsInclinedSlope(Slope s)
Tests if a specific slope is an inclined slope.
Definition: slope_func.h:230
a flat tile
Definition: slope_type.h:51
static Corner GetHighestSlopeCorner(Slope s)
Returns the highest corner of a slope (one corner raised or a steep slope).
Definition: slope_func.h:128
north and east corner are raised
Definition: slope_type.h:60
static Foundation SpecialRailFoundation(Corner corner)
Returns the special rail foundation for single horizontal/vertical track.
Definition: slope_func.h:405
static Slope SteepSlope(Corner corner)
Returns a specific steep slope.
Definition: slope_func.h:219
indicates the slope is steep
Definition: slope_type.h:56
static Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:78
Foundation
Enumeration for Foundations.
Definition: slope_type.h:95
Foundation for TRACK_BIT_UPPER, but not a leveled foundation.
Definition: slope_type.h:113
The tile is leveled up to a flat slope.
Definition: slope_type.h:97
Level north halftile non-continuously.
Definition: slope_type.h:107
Foundation for TRACK_BIT_LEFT, but not a leveled foundation.
Definition: slope_type.h:110
Southeast.
Southwest.
one halftile is leveled (non continuous slope)
Definition: slope_type.h:73
The tile has an along Y-axis inclined foundation.
Definition: slope_type.h:99
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
static bool IsSlopeWithOneCornerRaised(Slope s)
Tests if a specific slope has exactly one corner raised.
Definition: slope_func.h:90
static Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:208
Northwest.
DiagDirection
Enumeration for diagonal directions.
bit mask containing all &#39;simple&#39; slopes
Definition: slope_type.h:63
static Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:371
The tile has a steep slope. The lowest corner is raised by a foundation and the upper halftile is lev...
Definition: slope_type.h:103
static DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:241
a steep slope falling to east (from west)
Definition: slope_type.h:68
Integer math functions.
static Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:62
The X axis.
static int GetSlopeMaxPixelZ(Slope s)
Returns the height of the highest corner of a slope relative to TileZ (= minimal height) ...
Definition: slope_func.h:175
static Slope InclinedSlope(DiagDirection dir)
Returns the slope that is inclined in a specific direction.
Definition: slope_func.h:258
static bool IsSlopeWithThreeCornersRaised(Slope s)
Tests if a specific slope has exactly three corners raised.
Definition: slope_func.h:197
static Corner GetRailFoundationCorner(Foundation f)
Returns the track corner of a special rail foundation.
Definition: slope_func.h:358
north and west corner are raised
Definition: slope_type.h:57
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:96
The tile has an along X-axis inclined foundation.
Definition: slope_type.h:98
static bool IsLeveledFoundation(Foundation f)
Tests if the foundation is a leveled foundation.
Definition: slope_func.h:300
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
a steep slope falling to south (from north)
Definition: slope_type.h:71
static uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:417
static Foundation InclinedFoundation(Axis axis)
Returns the along a specific axis inclined foundation.
Definition: slope_func.h:382
static Slope HalftileSlope(Slope s, Corner corner)
Adds a halftile slope to a slope.
Definition: slope_func.h:276
static bool IsInclinedFoundation(Foundation f)
Tests if the foundation is an inclined foundation.
Definition: slope_func.h:311
a steep slope falling to west (from east)
Definition: slope_type.h:70
static bool IsNonContinuousFoundation(Foundation f)
Tests if a foundation is a non-continuous foundation, i.e.
Definition: slope_func.h:322
Types related to tiles.
three bits used for halftile slopes
Definition: slope_type.h:74
the south corner of the tile is raised
Definition: slope_type.h:53
Level west halftile non-continuously.
Definition: slope_type.h:104
static bool IsSpecialRailFoundation(Foundation f)
Tests if a foundation is a special rail foundation for single horizontal/vertical track...
Definition: slope_func.h:347
Axis
Allow incrementing of DiagDirDiff variables.
static Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:186
a steep slope falling to north (from south)
Definition: slope_type.h:69