OpenTTD Source 20250205-master-gfd85ab1e2c
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 "core/alloc_func.hpp"
13#include "water_map.h"
14#include "error_func.h"
15#include "string_func.h"
17
18#include "safeguards.h"
19
20/* static */ uint Map::log_x;
21/* static */ uint Map::log_y;
22/* static */ uint Map::size_x;
23/* static */ uint Map::size_y;
24/* static */ uint Map::size;
25/* static */ uint Map::tile_mask;
26
27/* static */ std::unique_ptr<Tile::TileBase[]> Tile::base_tiles;
28/* static */ std::unique_ptr<Tile::TileExtended[]> Tile::extended_tiles;
29
30
36/* static */ void Map::Allocate(uint size_x, uint size_y)
37{
38 /* Make sure that the map size is within the limits and that
39 * size of both axes is a power of 2. */
42 (size_x & (size_x - 1)) != 0 ||
43 (size_y & (size_y - 1)) != 0) {
44 FatalError("Invalid map size");
45 }
46
47 Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y);
48
55
56 Tile::base_tiles = std::make_unique<Tile::TileBase[]>(Map::size);
57 Tile::extended_tiles = std::make_unique<Tile::TileExtended[]>(Map::size);
58
60}
61
62
63#ifdef _DEBUG
65{
66 int dx = offset & Map::MaxX();
67 if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
68 int dy = (offset - dx) / (int)Map::SizeX();
69
70 uint32_t x = TileX(tile) + dx;
71 uint32_t y = TileY(tile) + dy;
72
73 assert(x < Map::SizeX());
74 assert(y < Map::SizeY());
75 assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
76
77 return TileXY(x, y);
78}
79#endif
80
94TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
95{
96 uint x = TileX(tile) + addx;
97 uint y = TileY(tile) + addy;
98
99 /* Disallow void tiles at the north border. */
100 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
101
102 /* Are we about to wrap? */
103 if (x >= Map::MaxX() || y >= Map::MaxY()) return INVALID_TILE;
104
105 return TileXY(x, y);
106}
107
109extern const TileIndexDiffC _tileoffs_by_axis[] = {
110 { 1, 0},
111 { 0, 1},
112};
113
115extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
116 {-1, 0},
117 { 0, 1},
118 { 1, 0},
119 { 0, -1}
120};
121
123extern const TileIndexDiffC _tileoffs_by_dir[] = {
124 {-1, -1},
125 {-1, 0},
126 {-1, 1},
127 { 0, 1},
128 { 1, 1},
129 { 1, 0},
130 { 1, -1},
131 { 0, -1}
132};
133
144{
145 const uint dx = Delta(TileX(t0), TileX(t1));
146 const uint dy = Delta(TileY(t0), TileY(t1));
147 return dx + dy;
148}
149
150
161{
162 const int dx = TileX(t0) - TileX(t1);
163 const int dy = TileY(t0) - TileY(t1);
164 return dx * dx + dy * dy;
165}
166
167
176{
177 const uint dx = Delta(TileX(t0), TileX(t1));
178 const uint dy = Delta(TileY(t0), TileY(t1));
179 return std::max(dx, dy);
180}
181
182
192{
193 const uint dx = Delta(TileX(t0), TileX(t1));
194 const uint dy = Delta(TileY(t0), TileY(t1));
195 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
196}
197
204{
205 const uint xl = TileX(tile);
206 const uint yl = TileY(tile);
207 const uint xh = Map::SizeX() - 1 - xl;
208 const uint yh = Map::SizeY() - 1 - yl;
209 const uint minl = std::min(xl, yl);
210 const uint minh = std::min(xh, yh);
211 return std::min(minl, minh);
212}
213
221{
222 switch (dir) {
223 case DIAGDIR_NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
224 case DIAGDIR_NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
225 case DIAGDIR_SW: return Map::MaxX() - TileX(tile) - 1;
226 case DIAGDIR_SE: return Map::MaxY() - TileY(tile) - 1;
227 default: NOT_REACHED();
228 }
229}
230
244bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
245{
246 assert(proc != nullptr);
247 assert(size > 0);
248
249 if (size % 2 == 1) {
250 /* If the length of the side is uneven, the center has to be checked
251 * separately, as the pattern of uneven sides requires to go around the center */
252 if (proc(*tile, user_data)) return true;
253
254 /* If tile test is not successful, get one tile up,
255 * ready for a test in first circle around center tile */
256 *tile = TileAddByDir(*tile, DIR_N);
257 return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
258 } else {
259 return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
260 }
261}
262
282bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
283{
284 assert(proc != nullptr);
285 assert(radius > 0);
286
287 uint x = TileX(*tile) + w + 1;
288 uint y = TileY(*tile);
289
290 const uint extent[DIAGDIR_END] = { w, h, w, h };
291
292 for (uint n = 0; n < radius; n++) {
293 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
294 /* Is the tile within the map? */
295 for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
296 if (x < Map::SizeX() && y < Map::SizeY()) {
297 TileIndex t = TileXY(x, y);
298 /* Is the callback successful? */
299 if (proc(t, user_data)) {
300 /* Stop the search */
301 *tile = t;
302 return true;
303 }
304 }
305
306 /* Step to the next 'neighbour' in the circular line */
307 x += _tileoffs_by_diagdir[dir].x;
308 y += _tileoffs_by_diagdir[dir].y;
309 }
310 }
311 /* Jump to next circle to test */
314 }
315
316 *tile = INVALID_TILE;
317 return false;
318}
319
326uint GetClosestWaterDistance(TileIndex tile, bool water)
327{
328 if (HasTileWaterGround(tile) == water) return 0;
329
330 uint max_dist = water ? 0x7F : 0x200;
331
332 int x = TileX(tile);
333 int y = TileY(tile);
334
335 uint max_x = Map::MaxX();
336 uint max_y = Map::MaxY();
337 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
338
339 /* go in a 'spiral' with increasing manhattan distance in each iteration */
340 for (uint dist = 1; dist < max_dist; dist++) {
341 /* next 'diameter' */
342 y--;
343
344 /* going counter-clockwise around this square */
345 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
346 static const int8_t ddx[DIAGDIR_END] = { -1, 1, 1, -1};
347 static const int8_t ddy[DIAGDIR_END] = { 1, 1, -1, -1};
348
349 int dx = ddx[dir];
350 int dy = ddy[dir];
351
352 /* each side of this square has length 'dist' */
353 for (uint a = 0; a < dist; a++) {
354 /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
355 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
356 TileIndex t = TileXY(x, y);
357 if (HasTileWaterGround(t) == water) return dist;
358 }
359 x += dx;
360 y += dy;
361 }
362 }
363 }
364
365 if (!water) {
366 /* no land found - is this a water-only map? */
367 for (const auto t : Map::Iterate()) {
368 if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
369 }
370 }
371
372 return max_dist;
373}
Functions related to the allocation of memory.
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,...)
Ouptut 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:244
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:160
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:203
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:175
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:220
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:191
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:94
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:143
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:326
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:599
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:373
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:457
bool TestTileOnSearchProc(TileIndex tile, void *user_data)
A callback function type for searching tiles.
Definition map_func.h:643
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
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:57
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:317
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:279
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:363
static uint size
The number of tiles on the map.
Definition map_func.h:240
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition map_func.h:270
static uint size_x
Size of the map along the X.
Definition map_func.h:238
static uint tile_mask
_map_size - 1 (to mask the mapsize)
Definition map_func.h:241
static uint size_y
Size of the map along the Y.
Definition map_func.h:239
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:306
static uint log_y
2^_map_log_y == _map_size_y
Definition map_func.h:237
static uint log_x
2^_map_log_x == _map_size_x
Definition map_func.h:236
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:36
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:297
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.