OpenTTD: src/slope_func.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
62 static inline Slope RemoveHalftileSlope(Slope s)
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);
118 return IsSteepSlope(s) || IsSlopeWithOneCornerRaised(s);
119 }
120
128 static inline Corner GetHighestSlopeCorner(Slope s)
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
150 static inline Corner GetHalftileSlopeCorner(Slope s)
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
197 static inline bool IsSlopeWithThreeCornersRaised(Slope s)
198 {
199 return !IsHalftileSlope(s) && !IsSteepSlope(s) && IsSlopeWithOneCornerRaised(ComplementSlope(s));
200 }
201
208 static inline Slope SlopeWithThreeCornersRaised(Corner corner)
209 {
210 return ComplementSlope(SlopeWithOneCornerRaised(corner));
211 }
212
219 static inline Slope SteepSlope(Corner corner)
220 {
221 return SLOPE_STEEP | SlopeWithThreeCornersRaised(OppositeCorner(corner));
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
241 static inline DiagDirection GetInclinedSlopeDirection(Slope s)
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
258 static inline Slope InclinedSlope(DiagDirection dir)
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
322 static inline bool IsNonContinuousFoundation(Foundation f)
323 {
324 return IsInsideMM(f, FOUNDATION_STEEP_BOTH, FOUNDATION_HALFTILE_N + 1);
325 }
326
335 static inline Corner GetHalftileFoundationCorner(Foundation f)
336 {
337 assert(IsInsideMM(f, FOUNDATION_HALFTILE_W, FOUNDATION_HALFTILE_N + 1));
338 return (Corner)(f - FOUNDATION_HALFTILE_W);
339 }
340
347 static inline bool IsSpecialRailFoundation(Foundation f)
348 {
349 return IsInsideMM(f, FOUNDATION_RAIL_W, FOUNDATION_RAIL_N + 1);
350 }
351
358 static inline Corner GetRailFoundationCorner(Foundation f)
359 {
360 assert(IsSpecialRailFoundation(f));
361 return (Corner)(f - FOUNDATION_RAIL_W);
362 }
363
371 static inline Foundation FlatteningFoundation(Slope s)
372 {
373 return (s == SLOPE_FLAT ? FOUNDATION_NONE : FOUNDATION_LEVELED);
374 }
375
382 static inline Foundation InclinedFoundation(Axis axis)
383 {
384 return (axis == AXIS_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
385 }
386
393 static inline Foundation HalftileFoundation(Corner corner)
394 {
395 assert(IsValidCorner(corner));
396 return (Foundation)(FOUNDATION_HALFTILE_W + corner);
397 }
398
405 static inline Foundation SpecialRailFoundation(Corner corner)
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 */