OpenTTD Source: src/tile_map.cpp Source File

OpenTTD Source 14.0-beta3

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
24 static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
25 {
26 /* Due to the fact that tiles must connect with each other without leaving gaps, the
27 * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
28 *
29 * Also, there is at most 1 corner with height difference of 2.
30 */
31 int hminnw = std::min(hnorth, hwest);
32 int hmines = std::min(heast, hsouth);
33 int hmin = std::min(hminnw, hmines);
34
35 if (h != nullptr) *h = hmin;
36
37 int hmaxnw = std::max(hnorth, hwest);
38 int hmaxes = std::max(heast, hsouth);
39 int hmax = std::max(hmaxnw, hmaxes);
40
41 Slope r = SLOPE_FLAT;
42
43 if (hnorth != hmin) r |= SLOPE_N;
44 if (hwest != hmin) r |= SLOPE_W;
45 if (heast != hmin) r |= SLOPE_E;
46 if (hsouth != hmin) r |= SLOPE_S;
47
48 if (hmax - hmin == 2) r |= SLOPE_STEEP;
49
50 return r;
51 }
52
59 Slope GetTileSlope(TileIndex tile, int *h)
60 {
61 uint x1 = TileX(tile);
62 uint y1 = TileY(tile);
63 uint x2 = std::min(x1 + 1, Map::MaxX());
64 uint y2 = std::min(y1 + 1, Map::MaxY());
65
66 int hnorth = TileHeight(tile); // Height of the North corner.
67 int hwest = TileHeight(TileXY(x2, y1)); // Height of the West corner.
68 int heast = TileHeight(TileXY(x1, y2)); // Height of the East corner.
69 int hsouth = TileHeight(TileXY(x2, y2)); // Height of the South corner.
70
71 return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
72 }
73
82 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
83 {
84 int hnorth = TileHeightOutsideMap(x, y); // N corner.
85 int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
86 int heast = TileHeightOutsideMap(x, y + 1); // E corner.
87 int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
88
89 Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
90 if (h != nullptr) *h *= TILE_HEIGHT;
91 return s;
92 }
93
100 bool IsTileFlat(TileIndex tile, int *h)
101 {
102 uint x1 = TileX(tile);
103 uint y1 = TileY(tile);
104 uint x2 = std::min(x1 + 1, Map::MaxX());
105 uint y2 = std::min(y1 + 1, Map::MaxY());
106
107 uint z = TileHeight(tile);
108 if (TileHeight(TileXY(x2, y1)) != z) return false;
109 if (TileHeight(TileXY(x1, y2)) != z) return false;
110 if (TileHeight(TileXY(x2, y2)) != z) return false;
111
112 if (h != nullptr) *h = z;
113 return true;
114 }
115
121 int GetTileZ(TileIndex tile)
122 {
123 uint x1 = TileX(tile);
124 uint y1 = TileY(tile);
125 uint x2 = std::min(x1 + 1, Map::MaxX());
126 uint y2 = std::min(y1 + 1, Map::MaxY());
127
128 return std::min({
129 TileHeight(tile), // N corner
130 TileHeight(TileXY(x2, y1)), // W corner
131 TileHeight(TileXY(x1, y2)), // E corner
132 TileHeight(TileXY(x2, y2)), // S corner
133 });
134 }
135
141 int GetTileMaxZ(TileIndex t)
142 {
143 uint x1 = TileX(t);
144 uint y1 = TileY(t);
145 uint x2 = std::min(x1 + 1, Map::MaxX());
146 uint y2 = std::min(y1 + 1, Map::MaxY());
147
148 return std::max({
149 TileHeight(t), // N corner
150 TileHeight(TileXY(x2, y1)), // W corner
151 TileHeight(TileXY(x1, y2)), // E corner
152 TileHeight(TileXY(x2, y2)), // S corner
153 });
154 }
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:100
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
GetTilePixelSlopeOutsideMap
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.cpp:82
Map::MaxX
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:297
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
GetTileSlopeGivenHeight
static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
Get a tile's slope given the heigh of its four corners.
Definition: tile_map.cpp:24
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
tile_map.h
safeguards.h
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
stdafx.h
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:306
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
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_LVL_BASE.
Definition: tile_type.h:18
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
SLOPE_STEEP
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54