OpenTTD
tile_map.h
Go to the documentation of this file.
1 /* $Id: tile_map.h 27148 2015-02-14 12:53:07Z 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 #ifndef TILE_MAP_H
13 #define TILE_MAP_H
14 
15 #include "slope_type.h"
16 #include "map_func.h"
17 #include "core/bitmath_func.hpp"
18 #include "settings_type.h"
19 
31 static inline uint TileHeight(TileIndex tile)
32 {
33  assert(tile < MapSize());
34  return _m[tile].height;
35 }
36 
37 uint TileHeightOutsideMap(int x, int y);
38 
49 static inline void SetTileHeight(TileIndex tile, uint height)
50 {
51  assert(tile < MapSize());
52  assert(height <= MAX_TILE_HEIGHT);
53  _m[tile].height = height;
54 }
55 
64 static inline uint TilePixelHeight(TileIndex tile)
65 {
66  return TileHeight(tile) * TILE_HEIGHT;
67 }
68 
77 static inline uint TilePixelHeightOutsideMap(int x, int y)
78 {
79  return TileHeightOutsideMap(x, y) * TILE_HEIGHT;
80 }
81 
89 static inline TileType GetTileType(TileIndex tile)
90 {
91  assert(tile < MapSize());
92  return (TileType)GB(_m[tile].type, 4, 4);
93 }
94 
102 static inline bool IsInnerTile(TileIndex tile)
103 {
104  assert(tile < MapSize());
105 
106  uint x = TileX(tile);
107  uint y = TileY(tile);
108 
109  return x < MapMaxX() && y < MapMaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
110 }
111 
124 static inline void SetTileType(TileIndex tile, TileType type)
125 {
126  assert(tile < MapSize());
127  /* VOID tiles (and no others) are exactly allowed at the lower left and right
128  * edges of the map. If _settings_game.construction.freeform_edges is true,
129  * the upper edges of the map are also VOID tiles. */
130  assert(IsInnerTile(tile) == (type != MP_VOID));
131  SB(_m[tile].type, 4, 4, type);
132 }
133 
143 static inline bool IsTileType(TileIndex tile, TileType type)
144 {
145  return GetTileType(tile) == type;
146 }
147 
154 static inline bool IsValidTile(TileIndex tile)
155 {
156  return tile < MapSize() && !IsTileType(tile, MP_VOID);
157 }
158 
171 static inline Owner GetTileOwner(TileIndex tile)
172 {
173  assert(IsValidTile(tile));
174  assert(!IsTileType(tile, MP_HOUSE));
175  assert(!IsTileType(tile, MP_INDUSTRY));
176 
177  return (Owner)GB(_m[tile].m1, 0, 5);
178 }
179 
191 static inline void SetTileOwner(TileIndex tile, Owner owner)
192 {
193  assert(IsValidTile(tile));
194  assert(!IsTileType(tile, MP_HOUSE));
195  assert(!IsTileType(tile, MP_INDUSTRY));
196 
197  SB(_m[tile].m1, 0, 5, owner);
198 }
199 
207 static inline bool IsTileOwner(TileIndex tile, Owner owner)
208 {
209  return GetTileOwner(tile) == owner;
210 }
211 
218 static inline void SetTropicZone(TileIndex tile, TropicZone type)
219 {
220  assert(tile < MapSize());
221  assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
222  SB(_m[tile].type, 0, 2, type);
223 }
224 
231 static inline TropicZone GetTropicZone(TileIndex tile)
232 {
233  assert(tile < MapSize());
234  return (TropicZone)GB(_m[tile].type, 0, 2);
235 }
236 
243 static inline byte GetAnimationFrame(TileIndex t)
244 {
246  return _me[t].m7;
247 }
248 
255 static inline void SetAnimationFrame(TileIndex t, byte frame)
256 {
258  _me[t].m7 = frame;
259 }
260 
261 Slope GetTileSlope(TileIndex tile, int *h = NULL);
262 int GetTileZ(TileIndex tile);
263 int GetTileMaxZ(TileIndex tile);
264 
265 bool IsTileFlat(TileIndex tile, int *h = NULL);
266 
273 static inline Slope GetTilePixelSlope(TileIndex tile, int *h)
274 {
275  Slope s = GetTileSlope(tile, h);
276  if (h != NULL) *h *= TILE_HEIGHT;
277  return s;
278 }
279 
280 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h);
281 
287 static inline int GetTilePixelZ(TileIndex tile)
288 {
289  return GetTileZ(tile) * TILE_HEIGHT;
290 }
291 
292 int GetTilePixelZOutsideMap(int x, int y);
293 
299 static inline int GetTileMaxPixelZ(TileIndex tile)
300 {
301  return GetTileMaxZ(tile) * TILE_HEIGHT;
302 }
303 
304 int GetTileMaxPixelZOutsideMap(int x, int y);
305 
306 
314 static inline uint TileHash(uint x, uint y)
315 {
316  uint hash = x >> 4;
317  hash ^= x >> 6;
318  hash ^= y >> 4;
319  hash -= y >> 6;
320  return hash;
321 }
322 
332 static inline uint TileHash2Bit(uint x, uint y)
333 {
334  return GB(TileHash(x, y), 0, 2);
335 }
336 
337 #endif /* TILE_MAP_H */