OpenTTD Source  1.11.2
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 
60 {
61  uint x1 = TileX(tile);
62  uint y1 = TileY(tile);
63  uint x2 = std::min(x1 + 1, MapMaxX());
64  uint y2 = std::min(y1 + 1, MapMaxY());
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, MapMaxX());
105  uint y2 = std::min(y1 + 1, MapMaxY());
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 
122 {
123  uint x1 = TileX(tile);
124  uint y1 = TileY(tile);
125  uint x2 = std::min(x1 + 1, MapMaxX());
126  uint y2 = std::min(y1 + 1, MapMaxY());
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 
142 {
143  uint x1 = TileX(t);
144  uint y1 = TileY(t);
145  uint x2 = std::min(x1 + 1, MapMaxX());
146  uint y2 = std::min(y1 + 1, MapMaxY());
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 }
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:100
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
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
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
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
tile_map.h
TileHeight
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:29
TileHeightOutsideMap
static 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
safeguards.h
SLOPE_STEEP
@ SLOPE_STEEP
indicates the slope is steep
Definition: slope_type.h:54
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
stdafx.h
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:111
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
SLOPE_S
@ SLOPE_S
the south corner of the tile is raised
Definition: slope_type.h:51
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:102
SLOPE_W
@ SLOPE_W
the west corner of the tile is raised
Definition: slope_type.h:50
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:16
SLOPE_E
@ SLOPE_E
the east corner of the tile is raised
Definition: slope_type.h:52