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 */
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:89
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:231
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:255
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:191
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:287
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:243
int GetTileMaxZ(TileIndex tile)
Get top height of the tile inside the map.
Definition: tile_map.cpp:215
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:49
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:154
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:207
static uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
Definition: tile_map.h:332
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
int GetTileMaxPixelZOutsideMap(int x, int y)
Get top height of the tile outside the map.
Definition: tile_map.cpp:235
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:171
Types related to global configuration settings.
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
int GetTilePixelZOutsideMap(int x, int y)
Get bottom height of the tile outside map.
Definition: tile_map.cpp:200
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:273
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:159
static uint TileHash(uint x, uint y)
Calculate a hash value from a tile position.
Definition: tile_map.h:314
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
static uint TilePixelHeightOutsideMap(int x, int y)
Returns the tile height for a coordinate outside map.
Definition: tile_map.h:77
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:124
A tile of a station.
Definition: tile_type.h:48
uint TileHeightOutsideMap(int x, int y)
Returns the tile height for a coordinate outside map.
Definition: tile_map.cpp:25
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 outside the map.
Definition: tile_map.cpp:141
ConstructionSettings construction
construction of things in-game
static bool IsInnerTile(TileIndex tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:102
A house by a town.
Definition: tile_type.h:46
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:64
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:182
static void SetTropicZone(TileIndex tile, TropicZone type)
Set the tropic zone.
Definition: tile_map.h:218
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:299
Slope GetTileSlope(TileIndex tile, int *h=NULL)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:115