Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / tile_map.h
blobb6c715e8a8b067363d5bbd06d528a4840c57da0f
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 */
8 /** @file tile_map.h Map writing/reading functions for tiles. */
10 #ifndef TILE_MAP_H
11 #define TILE_MAP_H
13 #include "slope_type.h"
14 #include "map_func.h"
15 #include "core/bitmath_func.hpp"
16 #include "settings_type.h"
18 /**
19 * Returns the height of a tile
21 * This function returns the height of the northern corner of a tile.
22 * This is saved in the global map-array. It does not take affect by
23 * any slope-data of the tile.
25 * @param tile The tile to get the height from
26 * @return the height of the tile
27 * @pre tile < MapSize()
29 static inline uint TileHeight(TileIndex tile)
31 assert(tile < MapSize());
32 return _m[tile].height;
35 /**
36 * Returns the height of a tile, also for tiles outside the map (virtual "black" tiles).
38 * @param x X coordinate of the tile, may be outside the map.
39 * @param y Y coordinate of the tile, may be outside the map.
40 * @return The height in the same unit as TileHeight.
42 static inline uint TileHeightOutsideMap(int x, int y)
44 return TileHeight(TileXY(Clamp(x, 0, MapMaxX()), Clamp(y, 0, MapMaxY())));
47 /**
48 * Sets the height of a tile.
50 * This function sets the height of the northern corner of a tile.
52 * @param tile The tile to change the height
53 * @param height The new height value of the tile
54 * @pre tile < MapSize()
55 * @pre height <= MAX_TILE_HEIGHT
57 static inline void SetTileHeight(TileIndex tile, uint height)
59 assert(tile < MapSize());
60 assert(height <= MAX_TILE_HEIGHT);
61 _m[tile].height = height;
64 /**
65 * Returns the height of a tile in pixels.
67 * This function returns the height of the northern corner of a tile in pixels.
69 * @param tile The tile to get the height
70 * @return The height of the tile in pixel
72 static inline uint TilePixelHeight(TileIndex tile)
74 return TileHeight(tile) * TILE_HEIGHT;
77 /**
78 * Returns the height of a tile in pixels, also for tiles outside the map (virtual "black" tiles).
80 * @param x X coordinate of the tile, may be outside the map.
81 * @param y Y coordinate of the tile, may be outside the map.
82 * @return The height in pixels in the same unit as TilePixelHeight.
84 static inline uint TilePixelHeightOutsideMap(int x, int y)
86 return TileHeightOutsideMap(x, y) * TILE_HEIGHT;
89 /**
90 * Get the tiletype of a given tile.
92 * @param tile The tile to get the TileType
93 * @return The tiletype of the tile
94 * @pre tile < MapSize()
96 static inline TileType GetTileType(TileIndex tile)
98 assert(tile < MapSize());
99 return (TileType)GB(_m[tile].type, 4, 4);
103 * Check if a tile is within the map (not a border)
105 * @param tile The tile to check
106 * @return Whether the tile is in the interior of the map
107 * @pre tile < MapSize()
109 static inline bool IsInnerTile(TileIndex tile)
111 assert(tile < MapSize());
113 uint x = TileX(tile);
114 uint y = TileY(tile);
116 return x < MapMaxX() && y < MapMaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
120 * Set the type of a tile
122 * This functions sets the type of a tile. If the type
123 * MP_VOID is selected the tile must be at the south-west or
124 * south-east edges of the map and vice versa.
126 * @param tile The tile to save the new type
127 * @param type The type to save
128 * @pre tile < MapSize()
129 * @pre type MP_VOID <=> tile is on the south-east or south-west edge.
131 static inline void SetTileType(TileIndex tile, TileType type)
133 assert(tile < MapSize());
134 /* VOID tiles (and no others) are exactly allowed at the lower left and right
135 * edges of the map. If _settings_game.construction.freeform_edges is true,
136 * the upper edges of the map are also VOID tiles. */
137 assert(IsInnerTile(tile) == (type != MP_VOID));
138 SB(_m[tile].type, 4, 4, type);
142 * Checks if a tile is a given tiletype.
144 * This function checks if a tile has the given tiletype.
146 * @param tile The tile to check
147 * @param type The type to check against
148 * @return true If the type matches against the type of the tile
150 static inline bool IsTileType(TileIndex tile, TileType type)
152 return GetTileType(tile) == type;
156 * Checks if a tile is valid
158 * @param tile The tile to check
159 * @return True if the tile is on the map and not one of MP_VOID.
161 static inline bool IsValidTile(TileIndex tile)
163 return tile < MapSize() && !IsTileType(tile, MP_VOID);
167 * Returns the owner of a tile
169 * This function returns the owner of a tile. This cannot used
170 * for tiles which type is one of MP_HOUSE, MP_VOID and MP_INDUSTRY
171 * as no company owned any of these buildings.
173 * @param tile The tile to check
174 * @return The owner of the tile
175 * @pre IsValidTile(tile)
176 * @pre The type of the tile must not be MP_HOUSE and MP_INDUSTRY
178 static inline Owner GetTileOwner(TileIndex tile)
180 assert(IsValidTile(tile));
181 assert(!IsTileType(tile, MP_HOUSE));
182 assert(!IsTileType(tile, MP_INDUSTRY));
184 return (Owner)GB(_m[tile].m1, 0, 5);
188 * Sets the owner of a tile
190 * This function sets the owner status of a tile. Note that you cannot
191 * set a owner for tiles of type MP_HOUSE, MP_VOID and MP_INDUSTRY.
193 * @param tile The tile to change the owner status.
194 * @param owner The new owner.
195 * @pre IsValidTile(tile)
196 * @pre The type of the tile must not be MP_HOUSE and MP_INDUSTRY
198 static inline void SetTileOwner(TileIndex tile, Owner owner)
200 assert(IsValidTile(tile));
201 assert(!IsTileType(tile, MP_HOUSE));
202 assert(!IsTileType(tile, MP_INDUSTRY));
204 SB(_m[tile].m1, 0, 5, owner);
208 * Checks if a tile belongs to the given owner
210 * @param tile The tile to check
211 * @param owner The owner to check against
212 * @return True if a tile belongs the the given owner
214 static inline bool IsTileOwner(TileIndex tile, Owner owner)
216 return GetTileOwner(tile) == owner;
220 * Set the tropic zone
221 * @param tile the tile to set the zone of
222 * @param type the new type
223 * @pre tile < MapSize()
225 static inline void SetTropicZone(TileIndex tile, TropicZone type)
227 assert(tile < MapSize());
228 assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
229 SB(_m[tile].type, 0, 2, type);
233 * Get the tropic zone
234 * @param tile the tile to get the zone of
235 * @pre tile < MapSize()
236 * @return the zone type
238 static inline TropicZone GetTropicZone(TileIndex tile)
240 assert(tile < MapSize());
241 return (TropicZone)GB(_m[tile].type, 0, 2);
245 * Get the current animation frame
246 * @param t the tile
247 * @pre IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION)
248 * @return frame number
250 static inline byte GetAnimationFrame(TileIndex t)
252 assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION));
253 return _me[t].m7;
257 * Set a new animation frame
258 * @param t the tile
259 * @param frame the new frame number
260 * @pre IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION)
262 static inline void SetAnimationFrame(TileIndex t, byte frame)
264 assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION));
265 _me[t].m7 = frame;
268 Slope GetTileSlope(TileIndex tile, int *h = nullptr);
269 int GetTileZ(TileIndex tile);
270 int GetTileMaxZ(TileIndex tile);
272 bool IsTileFlat(TileIndex tile, int *h = nullptr);
275 * Return the slope of a given tile
276 * @param tile Tile to compute slope of
277 * @param h If not \c nullptr, pointer to storage of z height
278 * @return Slope of the tile, except for the HALFTILE part
280 static inline Slope GetTilePixelSlope(TileIndex tile, int *h)
282 Slope s = GetTileSlope(tile, h);
283 if (h != nullptr) *h *= TILE_HEIGHT;
284 return s;
287 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h);
290 * Get bottom height of the tile
291 * @param tile Tile to compute height of
292 * @return Minimum height of the tile
294 static inline int GetTilePixelZ(TileIndex tile)
296 return GetTileZ(tile) * TILE_HEIGHT;
300 * Get top height of the tile
301 * @param tile Tile to compute height of
302 * @return Maximum height of the tile
304 static inline int GetTileMaxPixelZ(TileIndex tile)
306 return GetTileMaxZ(tile) * TILE_HEIGHT;
310 * Calculate a hash value from a tile position
312 * @param x The X coordinate
313 * @param y The Y coordinate
314 * @return The hash of the tile
316 static inline uint TileHash(uint x, uint y)
318 uint hash = x >> 4;
319 hash ^= x >> 6;
320 hash ^= y >> 4;
321 hash -= y >> 6;
322 return hash;
326 * Get the last two bits of the TileHash
327 * from a tile position.
329 * @see TileHash()
330 * @param x The X coordinate
331 * @param y The Y coordinate
332 * @return The last two bits from hash of the tile
334 static inline uint TileHash2Bit(uint x, uint y)
336 return GB(TileHash(x, y), 0, 2);
339 #endif /* TILE_MAP_H */