OpenTTD Source 20250528-master-g3aca5d62a8
map.cpp
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#include "stdafx.h"
11#include "debug.h"
12#include "water_map.h"
13#include "error_func.h"
14#include "string_func.h"
16
17#include "safeguards.h"
18
19/* static */ uint Map::log_x;
20/* static */ uint Map::log_y;
21/* static */ uint Map::size_x;
22/* static */ uint Map::size_y;
23/* static */ uint Map::size;
24/* static */ uint Map::tile_mask;
25
26/* static */ std::unique_ptr<Tile::TileBase[]> Tile::base_tiles;
27/* static */ std::unique_ptr<Tile::TileExtended[]> Tile::extended_tiles;
28
29
35/* static */ void Map::Allocate(uint size_x, uint size_y)
36{
37 /* Make sure that the map size is within the limits and that
38 * size of both axes is a power of 2. */
41 (size_x & (size_x - 1)) != 0 ||
42 (size_y & (size_y - 1)) != 0) {
43 FatalError("Invalid map size");
44 }
45
46 Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y);
47
54
55 Tile::base_tiles = std::make_unique<Tile::TileBase[]>(Map::size);
56 Tile::extended_tiles = std::make_unique<Tile::TileExtended[]>(Map::size);
57
59}
60
61
62#ifdef _DEBUG
64{
65 int dx = offset & Map::MaxX();
66 if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
67 int dy = (offset - dx) / (int)Map::SizeX();
68
69 uint32_t x = TileX(tile) + dx;
70 uint32_t y = TileY(tile) + dy;
71
72 assert(x < Map::SizeX());
73 assert(y < Map::SizeY());
74 assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
75
76 return TileXY(x, y);
77}
78#endif
79
93TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
94{
95 uint x = TileX(tile) + addx;
96 uint y = TileY(tile) + addy;
97
98 /* Disallow void tiles at the north border. */
99 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
100
101 /* Are we about to wrap? */
102 if (x >= Map::MaxX() || y >= Map::MaxY()) return INVALID_TILE;
103
104 return TileXY(x, y);
105}
106
108extern const TileIndexDiffC _tileoffs_by_axis[] = {
109 { 1, 0},
110 { 0, 1},
111};
112
114extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
115 {-1, 0},
116 { 0, 1},
117 { 1, 0},
118 { 0, -1}
119};
120
122extern const TileIndexDiffC _tileoffs_by_dir[] = {
123 {-1, -1},
124 {-1, 0},
125 {-1, 1},
126 { 0, 1},
127 { 1, 1},
128 { 1, 0},
129 { 1, -1},
130 { 0, -1}
131};
132
143{
144 const uint dx = Delta(TileX(t0), TileX(t1));
145 const uint dy = Delta(TileY(t0), TileY(t1));
146 return dx + dy;
147}
148
149
160{
161 const int dx = TileX(t0) - TileX(t1);
162 const int dy = TileY(t0) - TileY(t1);
163 return dx * dx + dy * dy;
164}
165
166
175{
176 const uint dx = Delta(TileX(t0), TileX(t1));
177 const uint dy = Delta(TileY(t0), TileY(t1));
178 return std::max(dx, dy);
179}
180
181
191{
192 const uint dx = Delta(TileX(t0), TileX(t1));
193 const uint dy = Delta(TileY(t0), TileY(t1));
194 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
195}
196
203{
204 const uint xl = TileX(tile);
205 const uint yl = TileY(tile);
206 const uint xh = Map::SizeX() - 1 - xl;
207 const uint yh = Map::SizeY() - 1 - yl;
208 const uint minl = std::min(xl, yl);
209 const uint minh = std::min(xh, yh);
210 return std::min(minl, minh);
211}
212
220{
221 switch (dir) {
222 case DIAGDIR_NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
223 case DIAGDIR_NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
224 case DIAGDIR_SW: return Map::MaxX() - TileX(tile) - 1;
225 case DIAGDIR_SE: return Map::MaxY() - TileY(tile) - 1;
226 default: NOT_REACHED();
227 }
228}
229
236uint GetClosestWaterDistance(TileIndex tile, bool water)
237{
238 if (HasTileWaterGround(tile) == water) return 0;
239
240 uint max_dist = water ? 0x7F : 0x200;
241
242 int x = TileX(tile);
243 int y = TileY(tile);
244
245 uint max_x = Map::MaxX();
246 uint max_y = Map::MaxY();
247 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
248
249 /* go in a 'spiral' with increasing manhattan distance in each iteration */
250 for (uint dist = 1; dist < max_dist; dist++) {
251 /* next 'diameter' */
252 y--;
253
254 /* going counter-clockwise around this square */
255 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
256 static const int8_t ddx[DIAGDIR_END] = { -1, 1, 1, -1};
257 static const int8_t ddy[DIAGDIR_END] = { 1, 1, -1, -1};
258
259 int dx = ddx[dir];
260 int dy = ddy[dir];
261
262 /* each side of this square has length 'dist' */
263 for (uint a = 0; a < dist; a++) {
264 /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
265 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
266 TileIndex t = TileXY(x, y);
267 if (HasTileWaterGround(t) == water) return dist;
268 }
269 x += dx;
270 y += dy;
271 }
272 }
273 }
274
275 if (!water) {
276 /* no land found - is this a water-only map? */
277 for (const auto t : Map::Iterate()) {
278 if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
279 }
280 }
281
282 return max_dist;
283}
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
static std::unique_ptr< TileExtended[]> extended_tiles
Pointer to the extended tile-array.
Definition map_func.h:55
static std::unique_ptr< TileBase[]> base_tiles
Pointer to the tile-array.
Definition map_func.h:54
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_NW
Northwest.
@ DIAGDIR_SE
Southeast.
@ DIAGDIR_END
Used for iterations.
@ DIAGDIR_BEGIN
Used for iterations.
@ DIAGDIR_SW
Southwest.
Error reporting related functions.
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:159
const TileIndexDiffC _tileoffs_by_axis[]
'Lookup table' for tile offsets given an Axis
uint DistanceFromEdge(TileIndex tile)
Param the minimum distance to an edge.
Definition map.cpp:202
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:174
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:219
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles plus the Manhattan distance,...
Definition map.cpp:190
const TileIndexDiffC _tileoffs_by_dir[]
'Lookup table' for tile offsets given a Direction
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:93
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:142
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:236
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:372
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:456
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:424
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:414
static const uint MAX_MAP_SIZE
Maximal map size = 4096.
Definition map_type.h:40
static const uint MIN_MAP_SIZE
Minimal map size = 64.
Definition map_type.h:39
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
bool freeform_edges
allow terraforming the tiles at the map edges
ConstructionSettings construction
construction of things in-game
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
Definition map_func.h:316
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:278
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:362
static uint size
The number of tiles on the map.
Definition map_func.h:239
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition map_func.h:269
static uint size_x
Size of the map along the X.
Definition map_func.h:237
static uint tile_mask
_map_size - 1 (to mask the mapsize)
Definition map_func.h:240
static uint size_y
Size of the map along the Y.
Definition map_func.h:238
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:305
static uint log_y
2^_map_log_y == _map_size_y
Definition map_func.h:236
static uint log_x
2^_map_log_x == _map_size_x
Definition map_func.h:235
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:35
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:296
A pair-construct of a TileIndexDiff.
Definition map_type.h:31
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
@ MP_WATER
Water tile.
Definition tile_type.h:54
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition tile_type.h:55
Map accessors for water tiles.
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:350
void AllocateWaterRegions()
Allocates the appropriate amount of water regions for the current map size.
Handles dividing the water in the map into regions to assist pathfinding.