Codechange: Use null pointer literal instead of the NULL macro
[openttd-github.git] / src / tile_map.cpp
blob5d85ef9668ecc22be48cc1250bd7713eedf6205e
1 /* $Id$ */
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 */
10 /** @file tile_map.cpp Global tile accessors. */
12 #include "stdafx.h"
13 #include "tile_map.h"
15 #include "safeguards.h"
17 /**
18 * Get a tile's slope given the heigh of its four corners.
19 * @param hnorth The height at the northern corner in the same unit as TileHeight.
20 * @param hwest The height at the western corner in the same unit as TileHeight.
21 * @param heast The height at the eastern corner in the same unit as TileHeight.
22 * @param hsouth The height at the southern corner in the same unit as TileHeight.
23 * @param[out] h The lowest height of the four corners.
24 * @return The slope.
26 static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
28 /* Due to the fact that tiles must connect with each other without leaving gaps, the
29 * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
31 * Also, there is at most 1 corner with height difference of 2.
33 int hminnw = min(hnorth, hwest);
34 int hmines = min(heast, hsouth);
35 int hmin = min(hminnw, hmines);
37 if (h != nullptr) *h = hmin;
39 int hmaxnw = max(hnorth, hwest);
40 int hmaxes = max(heast, hsouth);
41 int hmax = max(hmaxnw, hmaxes);
43 Slope r = SLOPE_FLAT;
45 if (hnorth != hmin) r |= SLOPE_N;
46 if (hwest != hmin) r |= SLOPE_W;
47 if (heast != hmin) r |= SLOPE_E;
48 if (hsouth != hmin) r |= SLOPE_S;
50 if (hmax - hmin == 2) r |= SLOPE_STEEP;
52 return r;
55 /**
56 * Return the slope of a given tile inside the map.
57 * @param tile Tile to compute slope of
58 * @param h If not \c nullptr, pointer to storage of z height
59 * @return Slope of the tile, except for the HALFTILE part
61 Slope GetTileSlope(TileIndex tile, int *h)
63 uint x1 = TileX(tile);
64 uint y1 = TileY(tile);
65 uint x2 = min(x1 + 1, MapMaxX());
66 uint y2 = min(y1 + 1, MapMaxY());
68 int hnorth = TileHeight(tile); // Height of the North corner.
69 int hwest = TileHeight(TileXY(x2, y1)); // Height of the West corner.
70 int heast = TileHeight(TileXY(x1, y2)); // Height of the East corner.
71 int hsouth = TileHeight(TileXY(x2, y2)); // Height of the South corner.
73 return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
76 /**
77 * Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
79 * @param x X coordinate of the tile to compute slope of, may be ouside the map.
80 * @param y Y coordinate of the tile to compute slope of, may be ouside the map.
81 * @param h If not \c nullptr, pointer to storage of z height.
82 * @return Slope of the tile, except for the HALFTILE part.
84 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
86 int hnorth = TileHeightOutsideMap(x, y); // N corner.
87 int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
88 int heast = TileHeightOutsideMap(x, y + 1); // E corner.
89 int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
91 Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
92 if (h != nullptr) *h *= TILE_HEIGHT;
93 return s;
96 /**
97 * Check if a given tile is flat
98 * @param tile Tile to check
99 * @param h If not \c nullptr, pointer to storage of z height (only if tile is flat)
100 * @return Whether the tile is flat
102 bool IsTileFlat(TileIndex tile, int *h)
104 uint x1 = TileX(tile);
105 uint y1 = TileY(tile);
106 uint x2 = min(x1 + 1, MapMaxX());
107 uint y2 = min(y1 + 1, MapMaxY());
109 uint z = TileHeight(tile);
110 if (TileHeight(TileXY(x2, y1)) != z) return false;
111 if (TileHeight(TileXY(x1, y2)) != z) return false;
112 if (TileHeight(TileXY(x2, y2)) != z) return false;
114 if (h != nullptr) *h = z;
115 return true;
119 * Get bottom height of the tile
120 * @param tile Tile to compute height of
121 * @return Minimum height of the tile
123 int GetTileZ(TileIndex tile)
125 uint x1 = TileX(tile);
126 uint y1 = TileY(tile);
127 uint x2 = min(x1 + 1, MapMaxX());
128 uint y2 = min(y1 + 1, MapMaxY());
130 int h = TileHeight(tile); // N corner
131 h = min(h, TileHeight(TileXY(x2, y1))); // W corner
132 h = min(h, TileHeight(TileXY(x1, y2))); // E corner
133 h = min(h, TileHeight(TileXY(x2, y2))); // S corner
135 return h;
139 * Get top height of the tile inside the map.
140 * @param t Tile to compute height of
141 * @return Maximum height of the tile
143 int GetTileMaxZ(TileIndex t)
145 uint x1 = TileX(t);
146 uint y1 = TileY(t);
147 uint x2 = min(x1 + 1, MapMaxX());
148 uint y2 = min(y1 + 1, MapMaxY());
150 int h = TileHeight(t); // N corner
151 h = max<int>(h, TileHeight(TileXY(x2, y1))); // W corner
152 h = max<int>(h, TileHeight(TileXY(x1, y2))); // E corner
153 h = max<int>(h, TileHeight(TileXY(x2, y2))); // S corner
155 return h;