OpenTTD: src/tile_map.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
115 Slope GetTileSlope(TileIndex tile, int *h)
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
182 int GetTileZ(TileIndex tile)
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
215 int GetTileMaxZ(TileIndex t)
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 }