OpenTTD Source: src/tile_map.h Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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[[debug_inline]] inline static uint TileHeight(Tile tile)
30{
31 assert(tile < Map::Size());
32 return tile.height();
33}
34
42 inline uint TileHeightOutsideMap(int x, int y)
43{
44 return TileHeight(TileXY(Clamp(x, 0, Map::MaxX()), Clamp(y, 0, Map::MaxY())));
45}
46
57 inline void SetTileHeight(Tile tile, uint height)
58{
59 assert(tile < Map::Size());
60 assert(height <= MAX_TILE_HEIGHT);
61 tile.height() = height;
62}
63
72 inline uint TilePixelHeight(Tile tile)
73{
74 return TileHeight(tile) * TILE_HEIGHT;
75}
76
84 inline uint TilePixelHeightOutsideMap(int x, int y)
85{
86 return TileHeightOutsideMap(x, y) * TILE_HEIGHT;
87}
88
96[[debug_inline]] inline static TileType GetTileType(Tile tile)
97{
98 assert(tile < Map::Size());
99 return (TileType)GB(tile.type(), 4, 4);
100}
101
109 inline bool IsInnerTile(Tile tile)
110{
111 assert(tile < Map::Size());
112
113 uint x = TileX(tile);
114 uint y = TileY(tile);
115
116 return x < Map::MaxX() && y < Map::MaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
117}
118
131 inline void SetTileType(Tile tile, TileType type)
132{
133 assert(tile < Map::Size());
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(tile.type(), 4, 4, type);
139}
140
150[[debug_inline]] inline static bool IsTileType(Tile tile, TileType type)
151{
152 return GetTileType(tile) == type;
153}
154
161 inline bool IsValidTile(Tile tile)
162{
163 return tile < Map::Size() && !IsTileType(tile, MP_VOID);
164}
165
178 inline Owner GetTileOwner(Tile tile)
179{
180 assert(IsValidTile(tile));
181 assert(!IsTileType(tile, MP_HOUSE));
182 assert(!IsTileType(tile, MP_INDUSTRY));
183
184 return (Owner)GB(tile.m1(), 0, 5);
185}
186
198 inline void SetTileOwner(Tile tile, Owner owner)
199{
200 assert(IsValidTile(tile));
201 assert(!IsTileType(tile, MP_HOUSE));
202 assert(!IsTileType(tile, MP_INDUSTRY));
203
204 SB(tile.m1(), 0, 5, owner.base());
205}
206
214 inline bool IsTileOwner(Tile tile, Owner owner)
215{
216 return GetTileOwner(tile) == owner;
217}
218
225 inline void SetTropicZone(Tile tile, TropicZone type)
226{
227 assert(tile < Map::Size());
228 assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
229 SB(tile.type(), 0, 2, type);
230}
231
238 inline TropicZone GetTropicZone(Tile tile)
239{
240 assert(tile < Map::Size());
241 return (TropicZone)GB(tile.type(), 0, 2);
242}
243
250 inline uint8_t GetAnimationFrame(Tile t)
251{
252 assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_STATION));
253 return t.m7();
254}
255
262 inline void SetAnimationFrame(Tile t, uint8_t frame)
263{
264 assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_STATION));
265 t.m7() = frame;
266}
267
268std::tuple<Slope, int> GetTileSlopeZ(TileIndex tile);
269int GetTileZ(TileIndex tile);
270int GetTileMaxZ(TileIndex tile);
271
272bool IsTileFlat(TileIndex tile, int *h = nullptr);
273
279 inline Slope GetTileSlope(TileIndex tile)
280{
281 return std::get<0>(GetTileSlopeZ(tile));
282}
283
289 inline std::tuple<Slope, int> GetTilePixelSlope(TileIndex tile)
290{
291 auto [s, h] = GetTileSlopeZ(tile);
292 return {s, h * TILE_HEIGHT};
293}
294
295std::tuple<Slope, int> GetTilePixelSlopeOutsideMap(int x, int y);
296
302 inline int GetTilePixelZ(TileIndex tile)
303{
304 return GetTileZ(tile) * TILE_HEIGHT;
305}
306
312 inline int GetTileMaxPixelZ(TileIndex tile)
313{
314 return GetTileMaxZ(tile) * TILE_HEIGHT;
315}
316
324 inline uint TileHash(uint x, uint y)
325{
326 uint hash = x >> 4;
327 hash ^= x >> 6;
328 hash ^= y >> 4;
329 hash -= y >> 6;
330 return hash;
331}
332
342 inline uint TileHash2Bit(uint x, uint y)
343{
344 return GB(TileHash(x, y), 0, 2);
345}
346
347#endif /* TILE_MAP_H */
bitmath_func.hpp
Functions related to bit mathematics.
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition bitmath_func.hpp:58
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
Tile::m1
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
Tile::height
uint8_t & height()
The height of the northern corner.
Definition map_func.h:101
Tile::type
uint8_t & type()
The type (bits 4..7), bridges (2..3), rainforest/desert (0..1)
Definition map_func.h:89
Tile::m7
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
map_func.h
Functions related to maps.
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
settings_type.h
Types related to global configuration settings.
slope_type.h
Definitions of a slope.
Slope
Slope
Enumeration for the slope-type.
Definition slope_type.h:48
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition settings_type.h:423
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition settings_type.h:635
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
Map::MaxX
static uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
Map::Size
static uint Size()
Get the size of the map.
Definition map_func.h:290
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
TileHash
uint TileHash(uint x, uint y)
Calculate a hash value from a tile position.
Definition tile_map.h:324
TileHeight
static uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
IsTileOwner
bool IsTileOwner(Tile tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition tile_map.h:214
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
GetTilePixelZ
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.h:302
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
GetTilePixelSlopeOutsideMap
std::tuple< Slope, int > GetTilePixelSlopeOutsideMap(int x, int y)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
Definition tile_map.cpp:78
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
GetTileMaxPixelZ
int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition tile_map.h:312
GetAnimationFrame
uint8_t GetAnimationFrame(Tile t)
Get the current animation frame.
Definition tile_map.h:250
GetTilePixelSlope
std::tuple< Slope, int > GetTilePixelSlope(TileIndex tile)
Return the slope of a given tile.
Definition tile_map.h:289
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
IsInnerTile
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border)
Definition tile_map.h:109
TilePixelHeight
uint TilePixelHeight(Tile tile)
Returns the height of a tile in pixels.
Definition tile_map.h:72
GetTileSlopeZ
std::tuple< Slope, int > GetTileSlopeZ(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.cpp:55
IsTileFlat
bool IsTileFlat(TileIndex tile, int *h=nullptr)
Check if a given tile is flat.
Definition tile_map.cpp:95
TileHash2Bit
uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
Definition tile_map.h:342
GetTropicZone
TropicZone GetTropicZone(Tile tile)
Get the tropic zone.
Definition tile_map.h:238
TilePixelHeightOutsideMap
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
GetTileMaxZ
int GetTileMaxZ(TileIndex tile)
Get top height of the tile inside the map.
Definition tile_map.cpp:136
SetAnimationFrame
void SetAnimationFrame(Tile t, uint8_t frame)
Set a new animation frame.
Definition tile_map.h:262
SetTileHeight
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
Definition tile_map.h:57
GetTileSlope
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.h:279
SetTropicZone
void SetTropicZone(Tile tile, TropicZone type)
Set the tropic zone.
Definition tile_map.h:225
GetTileType
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
TileHeightOutsideMap
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
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
MAX_TILE_HEIGHT
static constexpr uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition tile_type.h:24
TropicZone
TropicZone
Additional infos of a tile on a tropic game.
Definition tile_type.h:76
TROPICZONE_NORMAL
@ TROPICZONE_NORMAL
Normal tropiczone.
Definition tile_type.h:77
TILE_HEIGHT
static constexpr uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in ZOOM_BASE.
Definition tile_type.h:18
TileType
TileType
The different types of tiles.
Definition tile_type.h:47
MP_STATION
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition tile_type.h:51
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition tile_type.h:55
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition tile_type.h:58