OpenTTD
tile_map.cpp
Go to the documentation of this file.
1 /* $Id: tile_map.cpp 27161 2015-02-22 14:42:34Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "tile_map.h"
14 
15 #include "safeguards.h"
16 
25 uint TileHeightOutsideMap(int x, int y)
26 {
27  /* In all cases: Descend to heightlevel 0 as fast as possible.
28  * So: If we are at the 0-side of the map (x<0 or y<0), we must
29  * subtract the distance to coordinate 0 from the heightlevel at
30  * coordinate 0.
31  * In other words: Subtract e.g. -x. If we are at the MapMax
32  * side of the map, we also need to subtract the distance to
33  * the edge of map, e.g. MapMaxX - x.
34  *
35  * NOTE: Assuming constant heightlevel outside map would be
36  * simpler here. However, then we run into painting problems,
37  * since whenever a heightlevel change at the map border occurs,
38  * we would need to repaint anything outside map.
39  * In contrast, by doing it this way, we can localize this change,
40  * which means we may assume constant heightlevel for all tiles
41  * at more than <heightlevel at map border> distance from the
42  * map border.
43  */
44  if (x < 0) {
45  if (y < 0) {
46  return max((int)TileHeight(TileXY(0, 0)) - (-x) - (-y), 0);
47  } else if (y < (int)MapMaxY()) {
48  return max((int)TileHeight(TileXY(0, y)) - (-x), 0);
49  } else {
50  return max((int)TileHeight(TileXY(0, (int)MapMaxY())) - (-x) - (y - (int)MapMaxY()), 0);
51  }
52  } else if (x < (int)MapMaxX()) {
53  if (y < 0) {
54  return max((int)TileHeight(TileXY(x, 0)) - (-y), 0);
55  } else if (y < (int)MapMaxY()) {
56  return TileHeight(TileXY(x, y));
57  } else {
58  return max((int)TileHeight(TileXY(x, (int)MapMaxY())) - (y - (int)MapMaxY()), 0);
59  }
60  } else {
61  if (y < 0) {
62  return max((int)TileHeight(TileXY((int)MapMaxX(), 0)) - (x - (int)MapMaxX()) - (-y), 0);
63  } else if (y < (int)MapMaxY()) {
64  return max((int)TileHeight(TileXY((int)MapMaxX(), y)) - (x - (int)MapMaxX()), 0);
65  } else {
66  return max((int)TileHeight(TileXY((int)MapMaxX(), (int)MapMaxY())) - (x - (int)MapMaxX()) - (y - (int)MapMaxY()), 0);
67  }
68  }
69 }
70 
80 static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
81 {
82  /* Due to the fact that tiles must connect with each other without leaving gaps, the
83  * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
84  *
85  * Also, there is at most 1 corner with height difference of 2.
86  */
87  int hminnw = min(hnorth, hwest);
88  int hmines = min(heast, hsouth);
89  int hmin = min(hminnw, hmines);
90 
91  if (h != NULL) *h = hmin;
92 
93  int hmaxnw = max(hnorth, hwest);
94  int hmaxes = max(heast, hsouth);
95  int hmax = max(hmaxnw, hmaxes);
96 
97  Slope r = SLOPE_FLAT;
98 
99  if (hnorth != hmin) r |= SLOPE_N;
100  if (hwest != hmin) r |= SLOPE_W;
101  if (heast != hmin) r |= SLOPE_E;
102  if (hsouth != hmin) r |= SLOPE_S;
103 
104  if (hmax - hmin == 2) r |= SLOPE_STEEP;
105 
106  return r;
107 }
108 
116 {
117  assert(tile < MapSize());
118 
119  uint x = TileX(tile);
120  uint y = TileY(tile);
121  if (x == MapMaxX() || y == MapMaxY()) {
122  if (h != NULL) *h = TileHeight(tile);
123  return SLOPE_FLAT;
124  }
125 
126  int hnorth = TileHeight(tile); // Height of the North corner.
127  int hwest = TileHeight(tile + TileDiffXY(1, 0)); // Height of the West corner.
128  int heast = TileHeight(tile + TileDiffXY(0, 1)); // Height of the East corner.
129  int hsouth = TileHeight(tile + TileDiffXY(1, 1)); // Height of the South corner.
130 
131  return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
132 }
133 
141 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
142 {
143  int hnorth = TileHeightOutsideMap(x, y); // N corner.
144  int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
145  int heast = TileHeightOutsideMap(x, y + 1); // E corner.
146  int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
147 
148  Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
149  if (h != NULL) *h *= TILE_HEIGHT;
150  return s;
151 }
152 
159 bool IsTileFlat(TileIndex tile, int *h)
160 {
161  assert(tile < MapSize());
162 
163  if (!IsInnerTile(tile)) {
164  if (h != NULL) *h = TileHeight(tile);
165  return true;
166  }
167 
168  uint z = TileHeight(tile);
169  if (TileHeight(tile + TileDiffXY(1, 0)) != z) return false;
170  if (TileHeight(tile + TileDiffXY(0, 1)) != z) return false;
171  if (TileHeight(tile + TileDiffXY(1, 1)) != z) return false;
172 
173  if (h != NULL) *h = z;
174  return true;
175 }
176 
183 {
184  if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
185 
186  int h = TileHeight(tile); // N corner
187  h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
188  h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
189  h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
190 
191  return h;
192 }
193 
200 int GetTilePixelZOutsideMap(int x, int y)
201 {
202  uint h = TileHeightOutsideMap(x, y); // N corner.
203  h = min(h, TileHeightOutsideMap(x + 1, y)); // W corner.
204  h = min(h, TileHeightOutsideMap(x, y + 1)); // E corner.
205  h = min(h, TileHeightOutsideMap(x + 1, y + 1)); // S corner
206 
207  return h * TILE_HEIGHT;
208 }
209 
216 {
217  if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return TileHeightOutsideMap(TileX(t), TileY(t));
218 
219  int h = TileHeight(t); // N corner
220  h = max<int>(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
221  h = max<int>(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
222  h = max<int>(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
223 
224  return h;
225 }
226 
235 int GetTileMaxPixelZOutsideMap(int x, int y)
236 {
237  uint h = TileHeightOutsideMap(x, y);
238  h = max(h, TileHeightOutsideMap(x + 1, y));
239  h = max(h, TileHeightOutsideMap(x, y + 1));
240  h = max(h, TileHeightOutsideMap(x + 1, y + 1));
241 
242  return h * TILE_HEIGHT;
243 }
the north corner of the tile is raised
Definition: slope_type.h:55
the west corner of the tile is raised
Definition: slope_type.h:52
int GetTilePixelZOutsideMap(int x, int y)
Get bottom height of the tile outside map.
Definition: tile_map.cpp:200
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
Return the slope of a given tile outside the map.
Definition: tile_map.cpp:141
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
the east corner of the tile is raised
Definition: slope_type.h:54
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:159
a flat tile
Definition: slope_type.h:51
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:182
static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
Get a tile&#39;s slope given the heigh of its four corners.
Definition: tile_map.cpp:80
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:115
indicates the slope is steep
Definition: slope_type.h:56
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
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
int GetTileMaxPixelZOutsideMap(int x, int y)
Get top height of the tile outside the map.
Definition: tile_map.cpp:235
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:215
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
uint TileHeightOutsideMap(int x, int y)
Returns the tile height for a coordinate outside map.
Definition: tile_map.cpp:25
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:181
static bool IsInnerTile(TileIndex tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:102
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
Map writing/reading functions for tiles.
the south corner of the tile is raised
Definition: slope_type.h:53
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165