OpenTTD
tile_map.cpp
Go to the documentation of this file.
1 /* $Id: tile_map.cpp 26868 2014-09-21 07:48:18Z rubidium $ */
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  if (!IsInnerTile(tile)) {
120  if (h != NULL) *h = TileHeight(tile);
121  return SLOPE_FLAT;
122  }
123 
124  int hnorth = TileHeight(tile); // Height of the North corner.
125  int hwest = TileHeight(tile + TileDiffXY(1, 0)); // Height of the West corner.
126  int heast = TileHeight(tile + TileDiffXY(0, 1)); // Height of the East corner.
127  int hsouth = TileHeight(tile + TileDiffXY(1, 1)); // Height of the South corner.
128 
129  return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
130 }
131 
139 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
140 {
141  int hnorth = TileHeightOutsideMap(x, y); // N corner.
142  int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
143  int heast = TileHeightOutsideMap(x, y + 1); // E corner.
144  int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
145 
146  Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
147  if (h != NULL) *h *= TILE_HEIGHT;
148  return s;
149 }
150 
157 bool IsTileFlat(TileIndex tile, int *h)
158 {
159  assert(tile < MapSize());
160 
161  if (!IsInnerTile(tile)) {
162  if (h != NULL) *h = TileHeight(tile);
163  return true;
164  }
165 
166  uint z = TileHeight(tile);
167  if (TileHeight(tile + TileDiffXY(1, 0)) != z) return false;
168  if (TileHeight(tile + TileDiffXY(0, 1)) != z) return false;
169  if (TileHeight(tile + TileDiffXY(1, 1)) != z) return false;
170 
171  if (h != NULL) *h = z;
172  return true;
173 }
174 
181 {
182  if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
183 
184  int h = TileHeight(tile); // N corner
185  h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
186  h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
187  h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
188 
189  return h;
190 }
191 
198 int GetTilePixelZOutsideMap(int x, int y)
199 {
200  uint h = TileHeightOutsideMap(x, y); // N corner.
201  h = min(h, TileHeightOutsideMap(x + 1, y)); // W corner.
202  h = min(h, TileHeightOutsideMap(x, y + 1)); // E corner.
203  h = min(h, TileHeightOutsideMap(x + 1, y + 1)); // S corner
204 
205  return h * TILE_HEIGHT;
206 }
207 
214 {
215  if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return TileHeightOutsideMap(TileX(t), TileY(t));
216 
217  int h = TileHeight(t); // N corner
218  h = max<int>(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
219  h = max<int>(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
220  h = max<int>(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
221 
222  return h;
223 }
224 
233 int GetTileMaxPixelZOutsideMap(int x, int y)
234 {
235  uint h = TileHeightOutsideMap(x, y);
236  h = max(h, TileHeightOutsideMap(x + 1, y));
237  h = max(h, TileHeightOutsideMap(x, y + 1));
238  h = max(h, TileHeightOutsideMap(x + 1, y + 1));
239 
240  return h * TILE_HEIGHT;
241 }