OpenTTD Source 20250312-master-gcdcc6b491d
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
243bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
244{
245 assert(proc != nullptr);
246 assert(size > 0);
247
248 if (size % 2 == 1) {
249 /* If the length of the side is uneven, the center has to be checked
250 * separately, as the pattern of uneven sides requires to go around the center */
251 if (proc(*tile, user_data)) return true;
252
253 /* If tile test is not successful, get one tile up,
254 * ready for a test in first circle around center tile */
255 *tile = TileAddByDir(*tile, DIR_N);
256 return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
257 } else {
258 return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
259 }
260}
261
281bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
282{
283 assert(proc != nullptr);
284 assert(radius > 0);
285
286 uint x = TileX(*tile) + w + 1;
287 uint y = TileY(*tile);
288
289 const uint extent[DIAGDIR_END] = { w, h, w, h };
290
291 for (uint n = 0; n < radius; n++) {
292 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
293 /* Is the tile within the map? */
294 for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
295 if (x < Map::SizeX() && y < Map::SizeY()) {
296 TileIndex t = TileXY(x, y);
297 /* Is the callback successful? */
298 if (proc(t, user_data)) {
299 /* Stop the search */
300 *tile = t;
301 return true;
302 }
303 }
304
305 /* Step to the next 'neighbour' in the circular line */
306 x += _tileoffs_by_diagdir[dir].x;
307 y += _tileoffs_by_diagdir[dir].y;
308 }
309 }
310 /* Jump to next circle to test */
313 }
314
315 *tile = INVALID_TILE;
316 return false;
317}
318
325uint GetClosestWaterDistance(TileIndex tile, bool water)
326{
327 if (HasTileWaterGround(tile) == water) return 0;
328
329 uint max_dist = water ? 0x7F : 0x200;
330
331 int x = TileX(tile);
332 int y = TileY(tile);
333
334 uint max_x = Map::MaxX();
335 uint max_y = Map::MaxY();
336 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
337
338 /* go in a 'spiral' with increasing manhattan distance in each iteration */
339 for (uint dist = 1; dist < max_dist; dist++) {
340 /* next 'diameter' */
341 y--;
342
343 /* going counter-clockwise around this square */
344 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
345 static const int8_t ddx[DIAGDIR_END] = { -1, 1, 1, -1};
346 static const int8_t ddy[DIAGDIR_END] = { 1, 1, -1, -1};
347
348 int dx = ddx[dir];
349 int dy = ddy[dir];
350
351 /* each side of this square has length 'dist' */
352 for (uint a = 0; a < dist; a++) {
353 /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
354 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
355 TileIndex t = TileXY(x, y);
356 if (HasTileWaterGround(t) == water) return dist;
357 }
358 x += dx;
359 y += dy;
360 }
361 }
362 }
363
364 if (!water) {
365 /* no land found - is this a water-only map? */
366 for (const auto t : Map::Iterate()) {
367 if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
368 }
369 }
370
371 return max_dist;
372}
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
@ DIR_N
North.
@ DIR_W
West.
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.
bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
Function performing a search around a center tile and going outward, thus in circle.
Definition map.cpp:243
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:325
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition map_func.h:598
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
bool TestTileOnSearchProc(TileIndex tile, void *user_data)
A callback function type for searching tiles.
Definition map_func.h:642
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:58
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
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
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.