OpenTTD Source 20260206-master-g4d4e37dbf1
tile_map.cpp
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#include "stdafx.h"
11#include "tile_map.h"
12
13#include "safeguards.h"
14
23static std::tuple<Slope, int> GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth)
24{
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.
27 *
28 * Also, there is at most 1 corner with height difference of 2.
29 */
30 int hminnw = std::min(hnorth, hwest);
31 int hmines = std::min(heast, hsouth);
32 int hmin = std::min(hminnw, hmines);
33
34 int hmaxnw = std::max(hnorth, hwest);
35 int hmaxes = std::max(heast, hsouth);
36 int hmax = std::max(hmaxnw, hmaxes);
37
38 Slope r = SLOPE_FLAT;
39
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;
44
45 if (hmax - hmin == 2) r |= SLOPE_STEEP;
46
47 return {r, hmin};
48}
49
55std::tuple<Slope, int> GetTileSlopeZ(TileIndex tile)
56{
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());
61
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.
66
67 return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth);
68}
69
77std::tuple<Slope, int> GetTilePixelSlopeOutsideMap(int x, int y)
78{
79 int hnorth = TileHeightOutsideMap(x, y); // N corner.
80 int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
81 int heast = TileHeightOutsideMap(x, y + 1); // E corner.
82 int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
83
84 auto [slope, h] = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth);
85 return {slope, h * TILE_HEIGHT};
86}
87
94bool IsTileFlat(TileIndex tile, int *h)
95{
96 uint x1 = TileX(tile);
97 uint y1 = TileY(tile);
98 uint x2 = std::min(x1 + 1, Map::MaxX());
99 uint y2 = std::min(y1 + 1, Map::MaxY());
100
101 uint z = TileHeight(tile);
102 if (TileHeight(TileXY(x2, y1)) != z) return false;
103 if (TileHeight(TileXY(x1, y2)) != z) return false;
104 if (TileHeight(TileXY(x2, y2)) != z) return false;
105
106 if (h != nullptr) *h = z;
107 return true;
108}
109
116{
117 uint x1 = TileX(tile);
118 uint y1 = TileY(tile);
119 uint x2 = std::min(x1 + 1, Map::MaxX());
120 uint y2 = std::min(y1 + 1, Map::MaxY());
121
122 return std::min({
123 TileHeight(tile), // N corner
124 TileHeight(TileXY(x2, y1)), // W corner
125 TileHeight(TileXY(x1, y2)), // E corner
126 TileHeight(TileXY(x2, y2)), // S corner
127 });
128}
129
136{
137 uint x1 = TileX(t);
138 uint y1 = TileY(t);
139 uint x2 = std::min(x1 + 1, Map::MaxX());
140 uint y2 = std::min(y1 + 1, Map::MaxY());
141
142 return std::max({
143 TileHeight(t), // N corner
144 TileHeight(TileXY(x2, y1)), // W corner
145 TileHeight(TileXY(x1, y2)), // E corner
146 TileHeight(TileXY(x2, y2)), // S corner
147 });
148}
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:375
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:427
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:417
A number of safeguards to prevent using unsafe methods.
Slope
Enumeration for the slope-type.
Definition slope_type.h:47
@ SLOPE_W
the west corner of the tile is raised
Definition slope_type.h:49
@ SLOPE_E
the east corner of the tile is raised
Definition slope_type.h:51
@ SLOPE_S
the south corner of the tile is raised
Definition slope_type.h:50
@ SLOPE_N
the north corner of the tile is raised
Definition slope_type.h:52
@ SLOPE_FLAT
a flat tile
Definition slope_type.h:48
@ SLOPE_STEEP
indicates the slope is steep
Definition slope_type.h:53
Definition of base types and functions in a cross-platform compatible way.
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:298
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:289
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition tile_map.cpp:94
std::tuple< Slope, int > GetTilePixelSlopeOutsideMap(int x, int y)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
Definition tile_map.cpp:77
std::tuple< Slope, int > GetTileSlopeZ(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.cpp:55
static std::tuple< Slope, int > GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth)
Get a tile's slope given the height of its four corners.
Definition tile_map.cpp:23
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition tile_map.cpp:135
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:115
Map writing/reading functions for tiles.
static uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
uint TileHeightOutsideMap(int x, int y)
Returns the height of a tile, also for tiles outside the map (virtual "black" tiles).
Definition tile_map.h:42
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
static constexpr uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in ZOOM_BASE.
Definition tile_type.h:18