OpenTTD
tile_map.h
Go to the documentation of this file.
1 /* $Id$ */
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 
44 static inline uint TileHeightOutsideMap(int x, int y)
45 {
46  return TileHeight(TileXY(Clamp(x, 0, MapMaxX()), Clamp(y, 0, MapMaxY())));
47 }
48 
59 static inline void SetTileHeight(TileIndex tile, uint height)
60 {
61  assert(tile < MapSize());
62  assert(height <= MAX_TILE_HEIGHT);
63  _m[tile].height = height;
64 }
65 
74 static inline uint TilePixelHeight(TileIndex tile)
75 {
76  return TileHeight(tile) * TILE_HEIGHT;
77 }
78 
86 static inline uint TilePixelHeightOutsideMap(int x, int y)
87 {
88  return TileHeightOutsideMap(x, y) * TILE_HEIGHT;
89 }
90 
98 static inline TileType GetTileType(TileIndex tile)
99 {
100  assert(tile < MapSize());
101  return (TileType)GB(_m[tile].type, 4, 4);
102 }
103 
111 static inline bool IsInnerTile(TileIndex tile)
112 {
113  assert(tile < MapSize());
114 
115  uint x = TileX(tile);
116  uint y = TileY(tile);
117 
118  return x < MapMaxX() && y < MapMaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
119 }
120 
133 static inline void SetTileType(TileIndex tile, TileType type)
134 {
135  assert(tile < MapSize());
136  /* VOID tiles (and no others) are exactly allowed at the lower left and right
137  * edges of the map. If _settings_game.construction.freeform_edges is true,
138  * the upper edges of the map are also VOID tiles. */
139  assert(IsInnerTile(tile) == (type != MP_VOID));
140  SB(_m[tile].type, 4, 4, type);
141 }
142 
152 static inline bool IsTileType(TileIndex tile, TileType type)
153 {
154  return GetTileType(tile) == type;
155 }
156 
163 static inline bool IsValidTile(TileIndex tile)
164 {
165  return tile < MapSize() && !IsTileType(tile, MP_VOID);
166 }
167 
180 static inline Owner GetTileOwner(TileIndex tile)
181 {
182  assert(IsValidTile(tile));
183  assert(!IsTileType(tile, MP_HOUSE));
184  assert(!IsTileType(tile, MP_INDUSTRY));
185 
186  return (Owner)GB(_m[tile].m1, 0, 5);
187 }
188 
200 static inline void SetTileOwner(TileIndex tile, Owner owner)
201 {
202  assert(IsValidTile(tile));
203  assert(!IsTileType(tile, MP_HOUSE));
204  assert(!IsTileType(tile, MP_INDUSTRY));
205 
206  SB(_m[tile].m1, 0, 5, owner);
207 }
208 
216 static inline bool IsTileOwner(TileIndex tile, Owner owner)
217 {
218  return GetTileOwner(tile) == owner;
219 }
220 
227 static inline void SetTropicZone(TileIndex tile, TropicZone type)
228 {
229  assert(tile < MapSize());
230  assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
231  SB(_m[tile].type, 0, 2, type);
232 }
233 
240 static inline TropicZone GetTropicZone(TileIndex tile)
241 {
242  assert(tile < MapSize());
243  return (TropicZone)GB(_m[tile].type, 0, 2);
244 }
245 
252 static inline byte GetAnimationFrame(TileIndex t)
253 {
255  return _me[t].m7;
256 }
257 
264 static inline void SetAnimationFrame(TileIndex t, byte frame)
265 {
267  _me[t].m7 = frame;
268 }
269 
270 Slope GetTileSlope(TileIndex tile, int *h = NULL);
271 int GetTileZ(TileIndex tile);
272 int GetTileMaxZ(TileIndex tile);
273 
274 bool IsTileFlat(TileIndex tile, int *h = NULL);
275 
282 static inline Slope GetTilePixelSlope(TileIndex tile, int *h)
283 {
284  Slope s = GetTileSlope(tile, h);
285  if (h != NULL) *h *= TILE_HEIGHT;
286  return s;
287 }
288 
289 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h);
290 
296 static inline int GetTilePixelZ(TileIndex tile)
297 {
298  return GetTileZ(tile) * TILE_HEIGHT;
299 }
300 
306 static inline int GetTileMaxPixelZ(TileIndex tile)
307 {
308  return GetTileMaxZ(tile) * TILE_HEIGHT;
309 }
310 
318 static inline uint TileHash(uint x, uint y)
319 {
320  uint hash = x >> 4;
321  hash ^= x >> 6;
322  hash ^= y >> 4;
323  hash -= y >> 6;
324  return hash;
325 }
326 
336 static inline uint TileHash2Bit(uint x, uint y)
337 {
338  return GB(TileHash(x, y), 0, 2);
339 }
340 
341 #endif /* TILE_MAP_H */
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:240
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:264
Definitions of a slope.
Normal tropiczone.
Definition: tile_type.h:72
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:200
static const uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition: tile_type.h:24
TropicZone
Additional infos of a tile on a tropic game.
Definition: tile_type.h:71
Part of an industry.
Definition: tile_type.h:51
static int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:296
TileType
The different types of tiles.
Definition: tile_type.h:42
byte m7
Primarily used for newgrf support.
Definition: map_type.h:37
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:252
int GetTileMaxZ(TileIndex tile)
Get top height of the tile inside the map.
Definition: tile_map.cpp:143
Tile * _m
Tiles of the map.
Definition: map.cpp:32
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Functions related to bit mathematics.
Contains objects such as transmitters and owned land.
Definition: tile_type.h:53
Functions related to maps.
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
static void SetTileHeight(TileIndex tile, uint height)
Sets the height of a tile.
Definition: tile_map.h:59
bool freeform_edges
allow terraforming the tiles at the map edges
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:163
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:216
static uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
Definition: tile_map.h:336
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:180
Types related to global configuration settings.
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:282
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
bool IsTileFlat(TileIndex tile, int *h=NULL)
Check if a given tile is flat.
Definition: tile_map.cpp:102
static uint TileHash(uint x, uint y)
Calculate a hash value from a tile position.
Definition: tile_map.h:318
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
static uint TilePixelHeightOutsideMap(int x, int y)
Returns the height of a tile in pixels, also for tiles outside the map (virtual "black" tiles)...
Definition: tile_map.h:86
Invisible tiles at the SW and SE border.
Definition: tile_type.h:50
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
byte height
The height of the northern corner.
Definition: map_type.h:21
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:133
A tile of a station.
Definition: tile_type.h:48
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles)...
Definition: tile_map.cpp:84
ConstructionSettings construction
construction of things in-game
static uint TileHeightOutsideMap(int x, int y)
Returns the height of a tile, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.h:44
static bool IsInnerTile(TileIndex tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:111
A house by a town.
Definition: tile_type.h:46
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:74
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:123
static void SetTropicZone(TileIndex tile, TropicZone type)
Set the tropic zone.
Definition: tile_map.h:227
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:306
Slope GetTileSlope(TileIndex tile, int *h=NULL)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:61