OpenTTD Source 20260218-master-g2123fca5ea
map_func.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
9
10#ifndef MAP_FUNC_H
11#define MAP_FUNC_H
12
13#include "core/math_func.hpp"
14#include "tile_type.h"
15#include "map_type.h"
16#include "direction_func.h"
17
25class Tile {
26private:
27 friend struct Map;
32 struct TileBase {
33 uint8_t type = 0;
34 uint8_t height = 0;
35 uint16_t m2 = 0;
36 uint8_t m1 = 0;
37 uint8_t m3 = 0;
38 uint8_t m4 = 0;
39 uint8_t m5 = 0;
40 };
41
42 static_assert(sizeof(TileBase) == 8);
43
48 struct TileExtended {
49 uint8_t m6 = 0;
50 uint8_t m7 = 0;
51 uint16_t m8 = 0;
52 };
53
54 static std::unique_ptr<TileBase[]> base_tiles;
55 static std::unique_ptr<TileExtended[]> extended_tiles;
56
58
59public:
64 [[debug_inline]] inline Tile(TileIndex tile) : tile(tile) {}
65
70 Tile(uint tile) : tile(tile) {}
71
76 [[debug_inline]] inline constexpr operator TileIndex() const { return this->tile; }
77
82 [[debug_inline]] inline constexpr operator uint() const { return this->tile.base(); }
83
90 [[debug_inline]] inline uint8_t &type()
91 {
92 return base_tiles[this->tile.base()].type;
93 }
94
101 [[debug_inline]] inline uint8_t &height()
102 {
103 return base_tiles[this->tile.base()].height;
104 }
105
112 [[debug_inline]] inline uint8_t &m1()
113 {
114 return base_tiles[this->tile.base()].m1;
115 }
116
123 [[debug_inline]] inline uint16_t &m2()
124 {
125 return base_tiles[this->tile.base()].m2;
126 }
127
134 [[debug_inline]] inline uint8_t &m3()
135 {
136 return base_tiles[this->tile.base()].m3;
137 }
138
145 [[debug_inline]] inline uint8_t &m4()
146 {
147 return base_tiles[this->tile.base()].m4;
148 }
149
156 [[debug_inline]] inline uint8_t &m5()
157 {
158 return base_tiles[this->tile.base()].m5;
159 }
160
167 [[debug_inline]] inline uint8_t &m6()
168 {
169 return extended_tiles[this->tile.base()].m6;
170 }
171
178 [[debug_inline]] inline uint8_t &m7()
179 {
180 return extended_tiles[this->tile.base()].m7;
181 }
182
189 [[debug_inline]] inline uint16_t &m8()
190 {
191 return extended_tiles[this->tile.base()].m8;
192 }
193};
194
198struct Map {
199private:
203 struct Iterator {
204 typedef Tile value_type;
205 typedef Tile *pointer;
206 typedef Tile &reference;
207 typedef size_t difference_type;
208 typedef std::forward_iterator_tag iterator_category;
209
210 explicit Iterator(TileIndex index) : index(index) {}
211 bool operator==(const Iterator &other) const { return this->index == other.index; }
212 Tile operator*() const { return this->index; }
213 Iterator & operator++() { this->index++; return *this; }
214 private:
215 TileIndex index;
216 };
217
218 /*
219 * Iterable ensemble of all Tiles
220 */
222 Iterator begin() { return Iterator(TileIndex{}); }
223 Iterator end() { return Iterator(TileIndex{Map::Size()}); }
224 bool empty() { return false; }
225 };
226
227 static uint log_x;
228 static uint log_y;
229 static uint size_x;
230 static uint size_y;
231 static uint size;
232 static uint tile_mask;
233
234 static uint initial_land_count;
235
236public:
237 static void Allocate(uint size_x, uint size_y);
238 static void CountLandTiles();
239
245 [[debug_inline]] inline static uint LogX()
246 {
247 return Map::log_x;
248 }
249
255 static inline uint LogY()
256 {
257 return Map::log_y;
258 }
259
264 [[debug_inline]] inline static uint SizeX()
265 {
266 return Map::size_x;
267 }
268
273 static inline uint SizeY()
274 {
275 return Map::size_y;
276 }
277
282 [[debug_inline]] inline static uint Size()
283 {
284 return Map::size;
285 }
286
291 [[debug_inline]] inline static uint MaxX()
292 {
293 return Map::SizeX() - 1;
294 }
295
300 static inline uint MaxY()
301 {
302 return Map::SizeY() - 1;
303 }
304
310 static inline uint ScaleByLandProportion(uint n)
311 {
312 /* Use 64-bit arithmetic to avoid overflow. */
313 return static_cast<uint>(static_cast<uint64_t>(n) * Map::initial_land_count / Map::size);
314 }
315
322 static inline TileIndex WrapToMap(TileIndex tile)
323 {
324 return TileIndex{tile.base() & Map::tile_mask};
325 }
326
333 static inline uint ScaleBySize(uint n)
334 {
335 /* Subtract 12 from shift in order to prevent integer overflow
336 * for large values of n. It's safe since the min mapsize is 64x64. */
337 return CeilDiv(n << (Map::LogX() + Map::LogY() - 12), 1 << 4);
338 }
339
346 static inline uint ScaleBySize1D(uint n)
347 {
348 /* Normal circumference for the X+Y is 256+256 = 1<<9
349 * Note, not actually taking the full circumference into account,
350 * just half of it. */
351 return CeilDiv((n << Map::LogX()) + (n << Map::LogY()), 1 << 9);
352 }
353
359 static bool IsInitialized()
360 {
361 return Tile::base_tiles != nullptr;
362 }
363
368 static IterateWrapper Iterate() { return IterateWrapper(); }
369};
370
378[[debug_inline]] inline static TileIndex TileXY(uint x, uint y)
379{
380 return TileIndex{(y << Map::LogX()) + x};
381}
382
394inline TileIndexDiff TileDiffXY(int x, int y)
395{
396 /* Multiplication gives much better optimization on MSVC than shifting.
397 * 0 << shift isn't optimized to 0 properly.
398 * Typically x and y are constants, and then this doesn't result
399 * in any actual multiplication in the assembly code.. */
400 return (y * Map::SizeX()) + x;
401}
402
409[[debug_inline]] inline static TileIndex TileVirtXY(uint x, uint y)
410{
411 return TileIndex{(y >> 4 << Map::LogX()) + (x >> 4)};
412}
413
414
420[[debug_inline]] inline static uint TileX(TileIndex tile)
421{
422 return tile.base() & Map::MaxX();
423}
424
430[[debug_inline]] inline static uint TileY(TileIndex tile)
431{
432 return tile.base() >> Map::LogX();
433}
434
446{
447 return TileDiffXY(tidc.x, tidc.y);
448}
449
450/* Helper functions to provide explicit +=/-= operators for TileIndex and TileIndexDiff. */
451constexpr TileIndex &operator+=(TileIndex &tile, TileIndexDiff offset) { tile = tile + TileIndex(offset); return tile; }
452constexpr TileIndex &operator-=(TileIndex &tile, TileIndexDiff offset) { tile = tile - TileIndex(offset); return tile; }
453
461#ifndef _DEBUG
462 constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset) { return tile + offset; }
463#else
465#endif
466
475inline TileIndex TileAddXY(TileIndex tile, int x, int y)
476{
477 return TileAdd(tile, TileDiffXY(x, y));
478}
479
480TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
481
495
503{
505
506 assert(IsValidDirection(dir));
507 return _tileoffs_by_dir[dir];
508}
509
521{
522 int x = TileX(tile) + diff.x;
523 int y = TileY(tile) + diff.y;
524 /* Negative value will become big positive value after cast */
525 if ((uint)x >= Map::SizeX() || (uint)y >= Map::SizeY()) return INVALID_TILE;
526 return TileXY(x, y);
527}
528
537{
538 TileIndexDiffC difference;
539
540 difference.x = TileX(tile_a) - TileX(tile_b);
541 difference.y = TileY(tile_a) - TileY(tile_b);
542
543 return difference;
544}
545
546/* Functions to calculate distances */
553
561{
562 extern const TileIndexDiffC _tileoffs_by_axis[];
563
564 assert(IsValidAxis(axis));
566}
567
582
590{
592
593 assert(IsValidDirection(dir));
595}
596
605{
606 return TileAdd(tile, TileOffsByDir(dir));
607}
608
617{
618 return TileAdd(tile, TileOffsByDiagDir(dir));
619}
620
629{
630 int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
631 int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
632 if (dx == 0) {
633 if (dy == 0) return INVALID_DIAGDIR;
634 return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
635 } else {
636 if (dy != 0) return INVALID_DIAGDIR;
637 return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
638 }
639}
640
646inline TileIndex RandomTileSeed(uint32_t r)
647{
648 return Map::WrapToMap(TileIndex{r});
649}
650
657#define RandomTile() RandomTileSeed(Random())
658
659uint GetClosestWaterDistance(TileIndex tile, bool water);
660
661#endif /* MAP_FUNC_H */
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
static std::unique_ptr< TileExtended[]> extended_tiles
Pointer to the extended tile-array.
Definition map_func.h:55
Tile(TileIndex tile)
Create the tile wrapper for the given tile.
Definition map_func.h:64
uint8_t & m5()
General purpose.
Definition map_func.h:156
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:112
static std::unique_ptr< TileBase[]> base_tiles
Pointer to the tile-array.
Definition map_func.h:54
uint8_t & height()
The height of the northern corner.
Definition map_func.h:101
uint8_t & m4()
General purpose.
Definition map_func.h:145
Tile(uint tile)
Create the tile wrapper for the given tile.
Definition map_func.h:70
uint8_t & m6()
General purpose.
Definition map_func.h:167
uint8_t & type()
The type (bits 4..7), bridges (2..3), rainforest/desert (0..1).
Definition map_func.h:90
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:178
uint8_t & m3()
General purpose.
Definition map_func.h:134
uint16_t & m8()
General purpose.
Definition map_func.h:189
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:123
TileIndex tile
The tile to access the map data for.
Definition map_func.h:57
Different functions related to conversions between directions.
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Direction
Defines the 8 directions on the map.
@ DIR_END
Used to iterate.
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_NW
Northwest.
@ DIAGDIR_SE
Southeast.
@ DIAGDIR_END
Used for iterations.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
@ DIAGDIR_SW
Southwest.
const TileIndexDiffC _tileoffs_by_axis[]
'Lookup table' for tile offsets given an Axis
const TileIndexDiffC _tileoffs_by_dir[]
'Lookup table' for tile offsets given a Direction
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
Definition map_func.h:628
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:475
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:409
uint DistanceMaxPlusManhattan(TileIndex, TileIndex)
Max + Manhattan.
Definition map.cpp:206
TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
Definition map_func.h:520
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition map_func.h:604
TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between two tiles from a TileIndexDiffC struct.
Definition map_func.h:445
TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition map_func.h:616
uint DistanceMax(TileIndex, TileIndex)
also known as L-Infinity-Norm
Definition map.cpp:190
TileIndex RandomTileSeed(uint32_t r)
Get a random tile out of a given seed.
Definition map_func.h:646
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:394
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:560
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:378
uint DistanceFromEdgeDir(TileIndex, DiagDirection)
distance from the map edge in given direction
Definition map.cpp:235
uint DistanceFromEdge(TileIndex)
shortest distance from any edge of the map
Definition map.cpp:218
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:430
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges.
Definition map.cpp:109
uint DistanceSquare(TileIndex, TileIndex)
Euclidean- or L2-Norm squared.
Definition map.cpp:175
TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
Definition map_func.h:488
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:252
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:420
uint DistanceManhattan(TileIndex, TileIndex)
also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
Definition map.cpp:158
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:462
TileIndexDiffC TileIndexDiffCByDir(Direction dir)
Returns the TileIndexDiffC offset from a Direction.
Definition map_func.h:502
TileIndexDiff TileOffsByDir(Direction dir)
Convert a Direction to a TileIndexDiff.
Definition map_func.h:589
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:575
TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
Returns the diff between two tiles.
Definition map_func.h:536
Types related to maps.
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
Integer math functions.
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Iterator to iterate all Tiles.
Definition map_func.h:203
Size related data of the map.
Definition map_func.h:198
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:264
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
Definition map_func.h:322
static uint ScaleBySize(uint n)
Scales the given value by the map size, where the given value is for a 256 by 256 map.
Definition map_func.h:333
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:273
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:368
static uint size
The number of tiles on the map.
Definition map_func.h:231
static uint LogX()
Logarithm of the map size along the X side.
Definition map_func.h:245
static uint ScaleByLandProportion(uint n)
Scales the given value by the number of water tiles.
Definition map_func.h:310
static uint size_x
Size of the map along the X.
Definition map_func.h:229
static uint tile_mask
_map_size - 1 (to mask the mapsize)
Definition map_func.h:232
static uint size_y
Size of the map along the Y.
Definition map_func.h:230
static uint ScaleBySize1D(uint n)
Scales the given value by the maps circumference, where the given value is for a 256 by 256 map.
Definition map_func.h:346
static uint LogY()
Logarithm of the map size along the y side.
Definition map_func.h:255
static bool IsInitialized()
Check whether the map has been initialized, as to not try to save the map during crashlog when the ma...
Definition map_func.h:359
static uint initial_land_count
Initial number of land tiles on the map.
Definition map_func.h:234
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:300
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:291
static uint log_y
2^_map_log_y == _map_size_y
Definition map_func.h:228
static uint log_x
2^_map_log_x == _map_size_x
Definition map_func.h:227
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:37
static uint Size()
Get the size of the map.
Definition map_func.h:282
A pair-construct of a TileIndexDiff.
Definition map_type.h:31
int16_t x
The x value of the coordinate.
Definition map_type.h:32
int16_t y
The y value of the coordinate.
Definition map_type.h:33
Data that is stored per tile.
Definition map_func.h:32
uint8_t m3
General purpose.
Definition map_func.h:37
uint8_t m4
General purpose.
Definition map_func.h:38
uint8_t type
The type (bits 4..7), bridges (2..3), rainforest/desert (0..1).
Definition map_func.h:33
uint8_t m5
General purpose.
Definition map_func.h:39
uint8_t height
The height of the northern corner.
Definition map_func.h:34
uint8_t m1
Primarily used for ownership information.
Definition map_func.h:36
uint16_t m2
Primarily used for indices to towns, industries and stations.
Definition map_func.h:35
Data that is stored per tile.
Definition map_func.h:48
uint8_t m6
General purpose.
Definition map_func.h:49
uint8_t m7
Primarily used for newgrf support.
Definition map_func.h:50
uint16_t m8
General purpose.
Definition map_func.h:51
Types related to tiles.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100