OpenTTD Source  1.11.2
tile_map.h
Go to the documentation of this file.
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  */
7 
10 #ifndef TILE_MAP_H
11 #define TILE_MAP_H
12 
13 #include "slope_type.h"
14 #include "map_func.h"
15 #include "core/bitmath_func.hpp"
16 #include "settings_type.h"
17 
29 static inline uint TileHeight(TileIndex tile)
30 {
31  assert(tile < MapSize());
32  return _m[tile].height;
33 }
34 
42 static inline uint TileHeightOutsideMap(int x, int y)
43 {
44  return TileHeight(TileXY(Clamp(x, 0, MapMaxX()), Clamp(y, 0, MapMaxY())));
45 }
46 
57 static inline void SetTileHeight(TileIndex tile, uint height)
58 {
59  assert(tile < MapSize());
60  assert(height <= MAX_TILE_HEIGHT);
61  _m[tile].height = height;
62 }
63 
72 static inline uint TilePixelHeight(TileIndex tile)
73 {
74  return TileHeight(tile) * TILE_HEIGHT;
75 }
76 
84 static inline uint TilePixelHeightOutsideMap(int x, int y)
85 {
86  return TileHeightOutsideMap(x, y) * TILE_HEIGHT;
87 }
88 
96 static inline TileType GetTileType(TileIndex tile)
97 {
98  assert(tile < MapSize());
99  return (TileType)GB(_m[tile].type, 4, 4);
100 }
101 
109 static inline bool IsInnerTile(TileIndex tile)
110 {
111  assert(tile < MapSize());
112 
113  uint x = TileX(tile);
114  uint y = TileY(tile);
115 
116  return x < MapMaxX() && y < MapMaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
117 }
118 
131 static inline void SetTileType(TileIndex tile, TileType type)
132 {
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);
139 }
140 
150 static inline bool IsTileType(TileIndex tile, TileType type)
151 {
152  return GetTileType(tile) == type;
153 }
154 
161 static inline bool IsValidTile(TileIndex tile)
162 {
163  return tile < MapSize() && !IsTileType(tile, MP_VOID);
164 }
165 
178 static inline Owner GetTileOwner(TileIndex tile)
179 {
180  assert(IsValidTile(tile));
181  assert(!IsTileType(tile, MP_HOUSE));
182  assert(!IsTileType(tile, MP_INDUSTRY));
183 
184  return (Owner)GB(_m[tile].m1, 0, 5);
185 }
186 
198 static inline void SetTileOwner(TileIndex tile, Owner owner)
199 {
200  assert(IsValidTile(tile));
201  assert(!IsTileType(tile, MP_HOUSE));
202  assert(!IsTileType(tile, MP_INDUSTRY));
203 
204  SB(_m[tile].m1, 0, 5, owner);
205 }
206 
214 static inline bool IsTileOwner(TileIndex tile, Owner owner)
215 {
216  return GetTileOwner(tile) == owner;
217 }
218 
225 static inline void SetTropicZone(TileIndex tile, TropicZone type)
226 {
227  assert(tile < MapSize());
228  assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
229  SB(_m[tile].type, 0, 2, type);
230 }
231 
238 static inline TropicZone GetTropicZone(TileIndex tile)
239 {
240  assert(tile < MapSize());
241  return (TropicZone)GB(_m[tile].type, 0, 2);
242 }
243 
250 static inline byte GetAnimationFrame(TileIndex t)
251 {
253  return _me[t].m7;
254 }
255 
262 static inline void SetAnimationFrame(TileIndex t, byte frame)
263 {
265  _me[t].m7 = frame;
266 }
267 
268 Slope GetTileSlope(TileIndex tile, int *h = nullptr);
269 int GetTileZ(TileIndex tile);
270 int GetTileMaxZ(TileIndex tile);
271 
272 bool IsTileFlat(TileIndex tile, int *h = nullptr);
273 
280 static inline Slope GetTilePixelSlope(TileIndex tile, int *h)
281 {
282  Slope s = GetTileSlope(tile, h);
283  if (h != nullptr) *h *= TILE_HEIGHT;
284  return s;
285 }
286 
287 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h);
288 
294 static inline int GetTilePixelZ(TileIndex tile)
295 {
296  return GetTileZ(tile) * TILE_HEIGHT;
297 }
298 
304 static inline int GetTileMaxPixelZ(TileIndex tile)
305 {
306  return GetTileMaxZ(tile) * TILE_HEIGHT;
307 }
308 
316 static inline uint TileHash(uint x, uint y)
317 {
318  uint hash = x >> 4;
319  hash ^= x >> 6;
320  hash ^= y >> 4;
321  hash -= y >> 6;
322  return hash;
323 }
324 
334 static inline uint TileHash2Bit(uint x, uint y)
335 {
336  return GB(TileHash(x, y), 0, 2);
337 }
338 
339 #endif /* TILE_MAP_H */
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:49
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
SetTileType
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
TilePixelHeight
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:72
map_func.h
_me
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:54
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
MAX_TILE_HEIGHT
static const uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition: tile_type.h:22
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
GetTileMaxZ
int GetTileMaxZ(TileIndex tile)
Get top height of the tile inside the map.
Definition: tile_map.cpp:141
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
bitmath_func.hpp
GetAnimationFrame
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:250
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
SetTropicZone
static void SetTropicZone(TileIndex tile, TropicZone type)
Set the tropic zone.
Definition: tile_map.h:225
TileHeight
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:29
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:56
SB
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.
Definition: bitmath_func.hpp:58
GetTileSlope
Slope GetTileSlope(TileIndex tile, int *h=nullptr)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:59
TilePixelHeightOutsideMap
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:84
TileHeightOutsideMap
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:42
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
GetTropicZone
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:238
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:333
IsTileOwner
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
TileHash
static uint TileHash(uint x, uint y)
Calculate a hash value from a tile position.
Definition: tile_map.h:316
settings_type.h
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
SetTileHeight
static void SetTileHeight(TileIndex tile, uint height)
Sets the height of a tile.
Definition: tile_map.h:57
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
GetTilePixelSlopeOutsideMap
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:82
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
TropicZone
TropicZone
Additional infos of a tile on a tropic game.
Definition: tile_type.h:74
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:111
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition: tile_type.h:53
GetTilePixelZ
static int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:294
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
TileType
TileType
The different types of tiles.
Definition: tile_type.h:45
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:51
TileHash2Bit
static uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
Definition: tile_map.h:334
GetTileMaxPixelZ
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:304
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:102
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:16
TileExtended::m7
byte m7
Primarily used for newgrf support.
Definition: map_type.h:35
SetTileOwner
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:565
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
TROPICZONE_NORMAL
@ TROPICZONE_NORMAL
Normal tropiczone.
Definition: tile_type.h:75
GetTilePixelSlope
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:280
SetAnimationFrame
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:262
slope_type.h
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h=nullptr)
Check if a given tile is flat.
Definition: tile_map.cpp:100
Tile::height
byte height
The height of the northern corner.
Definition: map_type.h:19
IsInnerTile
static bool IsInnerTile(TileIndex tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:109
_m
Tile * _m
Tiles of the map.
Definition: map.cpp:30
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121