OpenTTD Source  20240915-master-g3784a3d3d6
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 debug_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 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 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);
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 
239 {
240  assert(tile < Map::Size());
241  return (TropicZone)GB(tile.type(), 0, 2);
242 }
243 
250 inline uint8_t GetAnimationFrame(Tile t)
251 {
253  return t.m7();
254 }
255 
262 inline void SetAnimationFrame(Tile t, uint8_t frame)
263 {
265  t.m7() = frame;
266 }
267 
268 std::tuple<Slope, int> GetTileSlopeZ(TileIndex tile);
269 int GetTileZ(TileIndex tile);
270 int GetTileMaxZ(TileIndex tile);
271 
272 bool IsTileFlat(TileIndex tile, int *h = nullptr);
273 
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 
295 std::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 */
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:51
TileHash2Bit
uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
Definition: tile_map.h:342
Map::MaxX
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:297
GetTileSlope
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition: tile_map.h:279
GB
constexpr static debug_inline 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
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
map_func.h
SetTileHeight
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
Definition: tile_map.h:57
TileHash
uint TileHash(uint x, uint y)
Calculate a hash value from a tile position.
Definition: tile_map.h:324
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition: map_func.h:25
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
MAX_TILE_HEIGHT
static const uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition: tile_type.h:24
Tile::m7
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition: map_func.h:185
GetTileSlopeZ
std::tuple< Slope, int > GetTileSlopeZ(TileIndex tile)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:55
GetTileMaxZ
int GetTileMaxZ(TileIndex tile)
Get top height of the tile inside the map.
Definition: tile_map.cpp:136
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
bitmath_func.hpp
GetTilePixelSlope
std::tuple< Slope, int > GetTilePixelSlope(TileIndex tile)
Return the slope of a given tile.
Definition: tile_map.h:289
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:395
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition: tile_map.h:178
settings_type.h
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition: tile_map.h:161
GetAnimationFrame
uint8_t GetAnimationFrame(Tile t)
Get the current animation frame.
Definition: tile_map.h:250
Tile::height
debug_inline uint8_t & height()
The height of the northern corner.
Definition: map_func.h:101
TilePixelHeight
uint TilePixelHeight(Tile tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:72
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:306
TropicZone
TropicZone
Additional infos of a tile on a tropic game.
Definition: tile_type.h:76
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition: tile_type.h:55
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
TileType
TileType
The different types of tiles.
Definition: tile_type.h:47
Map::Size
static debug_inline uint Size()
Get the size of the map.
Definition: map_func.h:288
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
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
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
TileXY
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:385
GetTileMaxPixelZ
int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:312
TileHeight
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition: tile_map.h:29
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18
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
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:595
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
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
TROPICZONE_NORMAL
@ TROPICZONE_NORMAL
Normal tropiczone.
Definition: tile_type.h:77
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
SetAnimationFrame
void SetAnimationFrame(Tile t, uint8_t 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:95
IsTileOwner
bool IsTileOwner(Tile tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
GetTropicZone
TropicZone GetTropicZone(Tile tile)
Get the tropic zone.
Definition: tile_map.h:238
Tile::type
debug_inline uint8_t & type()
The type (bits 4..7), bridges (2..3), rainforest/desert (0..1)
Definition: map_func.h:89
GetTilePixelZ
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:302
SetTropicZone
void SetTropicZone(Tile tile, TropicZone type)
Set the tropic zone.
Definition: tile_map.h:225
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:116
Tile::m1
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition: map_func.h:113
IsInnerTile
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:109