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/>.
10 /** @file tile_map.cpp Global tile accessors. */
16 * Return the slope of a given tile
17 * @param tile Tile to compute slope of
18 * @param h If not \c NULL, pointer to storage of z height
19 * @return Slope of the tile, except for the HALFTILE part
21 Slope
GetTileSlope(TileIndex tile
, int *h
)
23 assert(tile
< MapSize());
28 if (x
== MapMaxX() || y
== MapMaxY() ||
29 ((x
== 0 || y
== 0) && _settings_game
.construction
.freeform_edges
)) {
30 if (h
!= NULL
) *h
= TileHeight(tile
);
34 int a
= TileHeight(tile
); // Height of the N corner
35 int min
= a
; // Minimal height of all corners examined so far
36 int b
= TileHeight(tile
+ TileDiffXY(1, 0)); // Height of the W corner
38 int c
= TileHeight(tile
+ TileDiffXY(0, 1)); // Height of the E corner
40 int d
= TileHeight(tile
+ TileDiffXY(1, 1)); // Height of the S corner
43 /* Due to the fact that tiles must connect with each other without leaving gaps, the
44 * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
46 * Also, there is at most 1 corner with height difference of 2.
49 uint r
= SLOPE_FLAT
; // Computed slope of the tile
51 /* For each corner if not equal to minimum height:
52 * - set the SLOPE_STEEP flag if the difference is 2
53 * - add the corresponding SLOPE_X constant to the computed slope
55 if ((a
-= min
) != 0) r
+= (--a
<< 4) + SLOPE_N
;
56 if ((c
-= min
) != 0) r
+= (--c
<< 4) + SLOPE_E
;
57 if ((d
-= min
) != 0) r
+= (--d
<< 4) + SLOPE_S
;
58 if ((b
-= min
) != 0) r
+= (--b
<< 4) + SLOPE_W
;
60 if (h
!= NULL
) *h
= min
;
66 * Get bottom height of the tile
67 * @param tile Tile to compute height of
68 * @return Minimum height of the tile
70 int GetTileZ(TileIndex tile
)
72 if (TileX(tile
) == MapMaxX() || TileY(tile
) == MapMaxY()) return 0;
74 int h
= TileHeight(tile
); // N corner
75 h
= min(h
, TileHeight(tile
+ TileDiffXY(1, 0))); // W corner
76 h
= min(h
, TileHeight(tile
+ TileDiffXY(0, 1))); // E corner
77 h
= min(h
, TileHeight(tile
+ TileDiffXY(1, 1))); // S corner
83 * Get top height of the tile
84 * @param t Tile to compute height of
85 * @return Maximum height of the tile
87 int GetTileMaxZ(TileIndex t
)
89 if (TileX(t
) == MapMaxX() || TileY(t
) == MapMaxY()) return 0;
91 int h
= TileHeight(t
); // N corner
92 h
= max
<int>(h
, TileHeight(t
+ TileDiffXY(1, 0))); // W corner
93 h
= max
<int>(h
, TileHeight(t
+ TileDiffXY(0, 1))); // E corner
94 h
= max
<int>(h
, TileHeight(t
+ TileDiffXY(1, 1))); // S corner