OpenTTD Source  20240917-master-g9ab0a47812
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "tile_map.h"
12 
13 #include "safeguards.h"
14 
23 static 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 
55 std::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 
78 std::tuple<Slope, int> GetTilePixelSlopeOutsideMap(int x, int y)
79 {
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.
84 
85  auto [slope, h] = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth);
86  return {slope, h * TILE_HEIGHT};
87 }
88 
95 bool IsTileFlat(TileIndex tile, int *h)
96 {
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());
101 
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;
106 
107  if (h != nullptr) *h = z;
108  return true;
109 }
110 
117 {
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());
122 
123  return std::min({
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
128  });
129 }
130 
137 {
138  uint x1 = TileX(t);
139  uint y1 = TileY(t);
140  uint x2 = std::min(x1 + 1, Map::MaxX());
141  uint y2 = std::min(y1 + 1, Map::MaxY());
142 
143  return std::max({
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
148  });
149 }
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:95
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:136
Map::MaxX
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:297
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:116
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
tile_map.h
safeguards.h
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
stdafx.h
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:306
SLOPE_STEEP
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
TileHeightOutsideMap
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
TileXY
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:385
TileHeight
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition: tile_map.h:29
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18
GetTileSlopeGivenHeight
static std::tuple< Slope, int > GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth)
Get a tile's slope given the heigh of its four corners.
Definition: tile_map.cpp:23
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
GetTilePixelSlopeOutsideMap
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:78
GetTileSlopeZ
std::tuple< Slope, int > GetTileSlopeZ(TileIndex tile)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:55