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/>.
8 /** @file tile_map.cpp Global tile accessors. */
13 #include "safeguards.h"
16 * Get a tile's slope given the heigh of its four corners.
17 * @param hnorth The height at the northern corner in the same unit as TileHeight.
18 * @param hwest The height at the western corner in the same unit as TileHeight.
19 * @param heast The height at the eastern corner in the same unit as TileHeight.
20 * @param hsouth The height at the southern corner in the same unit as TileHeight.
21 * @return The slope and the lowest height of the four corners.
23 static std::tuple
<Slope
, int> GetTileSlopeGivenHeight(int hnorth
, int hwest
, int heast
, int hsouth
)
25 /* Due to the fact that tiles must connect with each other without leaving gaps, the
26 * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
28 * Also, there is at most 1 corner with height difference of 2.
30 int hminnw
= std::min(hnorth
, hwest
);
31 int hmines
= std::min(heast
, hsouth
);
32 int hmin
= std::min(hminnw
, hmines
);
34 int hmaxnw
= std::max(hnorth
, hwest
);
35 int hmaxes
= std::max(heast
, hsouth
);
36 int hmax
= std::max(hmaxnw
, hmaxes
);
40 if (hnorth
!= hmin
) r
|= SLOPE_N
;
41 if (hwest
!= hmin
) r
|= SLOPE_W
;
42 if (heast
!= hmin
) r
|= SLOPE_E
;
43 if (hsouth
!= hmin
) r
|= SLOPE_S
;
45 if (hmax
- hmin
== 2) r
|= SLOPE_STEEP
;
51 * Return the slope of a given tile inside the map.
52 * @param tile Tile to compute slope of
53 * @return Slope of the tile, except for the HALFTILE part, and the z height
55 std::tuple
<Slope
, int> GetTileSlopeZ(TileIndex tile
)
57 uint x1
= TileX(tile
);
58 uint y1
= TileY(tile
);
59 uint x2
= std::min(x1
+ 1, Map::MaxX());
60 uint y2
= std::min(y1
+ 1, Map::MaxY());
62 int hnorth
= TileHeight(tile
); // Height of the North corner.
63 int hwest
= TileHeight(TileXY(x2
, y1
)); // Height of the West corner.
64 int heast
= TileHeight(TileXY(x1
, y2
)); // Height of the East corner.
65 int hsouth
= TileHeight(TileXY(x2
, y2
)); // Height of the South corner.
67 return GetTileSlopeGivenHeight(hnorth
, hwest
, heast
, hsouth
);
71 * Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
73 * @param x X coordinate of the tile to compute slope of, may be outside the map.
74 * @param y Y coordinate of the tile to compute slope of, may be outside the map.
75 * @param h If not \c nullptr, pointer to storage of z height.
76 * @return Slope of the tile, except for the HALFTILE part, and the z height of the tile.
78 std::tuple
<Slope
, int> GetTilePixelSlopeOutsideMap(int x
, int y
)
80 int hnorth
= TileHeightOutsideMap(x
, y
); // N corner.
81 int hwest
= TileHeightOutsideMap(x
+ 1, y
); // W corner.
82 int heast
= TileHeightOutsideMap(x
, y
+ 1); // E corner.
83 int hsouth
= TileHeightOutsideMap(x
+ 1, y
+ 1); // S corner.
85 auto [slope
, h
] = GetTileSlopeGivenHeight(hnorth
, hwest
, heast
, hsouth
);
86 return {slope
, h
* TILE_HEIGHT
};
90 * Check if a given tile is flat
91 * @param tile Tile to check
92 * @param h If not \c nullptr, pointer to storage of z height (only if tile is flat)
93 * @return Whether the tile is flat
95 bool IsTileFlat(TileIndex tile
, int *h
)
97 uint x1
= TileX(tile
);
98 uint y1
= TileY(tile
);
99 uint x2
= std::min(x1
+ 1, Map::MaxX());
100 uint y2
= std::min(y1
+ 1, Map::MaxY());
102 uint z
= TileHeight(tile
);
103 if (TileHeight(TileXY(x2
, y1
)) != z
) return false;
104 if (TileHeight(TileXY(x1
, y2
)) != z
) return false;
105 if (TileHeight(TileXY(x2
, y2
)) != z
) return false;
107 if (h
!= nullptr) *h
= z
;
112 * Get bottom height of the tile
113 * @param tile Tile to compute height of
114 * @return Minimum height of the tile
116 int GetTileZ(TileIndex tile
)
118 uint x1
= TileX(tile
);
119 uint y1
= TileY(tile
);
120 uint x2
= std::min(x1
+ 1, Map::MaxX());
121 uint y2
= std::min(y1
+ 1, Map::MaxY());
124 TileHeight(tile
), // N corner
125 TileHeight(TileXY(x2
, y1
)), // W corner
126 TileHeight(TileXY(x1
, y2
)), // E corner
127 TileHeight(TileXY(x2
, y2
)), // S corner
132 * Get top height of the tile inside the map.
133 * @param t Tile to compute height of
134 * @return Maximum height of the tile
136 int GetTileMaxZ(TileIndex t
)
140 uint x2
= std::min(x1
+ 1, Map::MaxX());
141 uint y2
= std::min(y1
+ 1, Map::MaxY());
144 TileHeight(t
), // N corner
145 TileHeight(TileXY(x2
, y1
)), // W corner
146 TileHeight(TileXY(x1
, y2
)), // E corner
147 TileHeight(TileXY(x2
, y2
)), // S corner