OpenTTD Source: src/tile_map.cpp Source File

OpenTTD Source 15.0-RC2

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
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
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
78std::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
116 int GetTileZ(TileIndex tile)
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
136 int GetTileMaxZ(TileIndex t)
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}
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
safeguards.h
A number of safeguards to prevent using unsafe methods.
Slope
Slope
Enumeration for the slope-type.
Definition slope_type.h:48
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition slope_type.h:50
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition slope_type.h:52
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition slope_type.h:51
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition slope_type.h:53
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
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
Map::MaxX
static uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition tile_map.cpp:95
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
GetTileSlopeGivenHeight
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
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition tile_map.cpp:136
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
tile_map.h
Map writing/reading functions for tiles.
TileHeight
static uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
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
TILE_HEIGHT
static constexpr uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in ZOOM_BASE.
Definition tile_type.h:18