OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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 */ uint Map::initial_land_count;
27
28/* static */ std::unique_ptr<Tile::TileBase[]> Tile::base_tiles;
29/* static */ std::unique_ptr<Tile::TileExtended[]> Tile::extended_tiles;
30
31
37/* static */ void Map::Allocate(uint size_x, uint size_y)
38{
39 /* Make sure that the map size is within the limits and that
40 * size of both axes is a power of 2. */
43 (size_x & (size_x - 1)) != 0 ||
44 (size_y & (size_y - 1)) != 0) {
45 FatalError("Invalid map size");
46 }
47
48 Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y);
49
56
57 Tile::base_tiles = std::make_unique<Tile::TileBase[]>(Map::size);
58 Tile::extended_tiles = std::make_unique<Tile::TileExtended[]>(Map::size);
59
61}
62
63/* static */ void Map::CountLandTiles()
64{
65 /* Count number of tiles that are land. */
67 for (const auto tile : Map::Iterate()) {
68 Map::initial_land_count += IsWaterTile(tile) ? 0 : 1;
69 }
70
71 /* Compensate for default values being set for (or users are most familiar with) at least
72 * very low sea level. Dividing by 12 adds roughly 8%. */
75}
76
85{
86 return TileIndex{(static_cast<uint>(Clamp<int>(y / static_cast<int>(TILE_SIZE), 0, Map::MaxY())) << Map::LogX()) + static_cast<uint>(Clamp<int>(x / static_cast<int>(TILE_SIZE), 0, Map::MaxX()))};
87}
88
89#ifdef _DEBUG
91{
92 int dx = offset & Map::MaxX();
93 if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
94 int dy = (offset - dx) / (int)Map::SizeX();
95
96 uint32_t x = TileX(tile) + dx;
97 uint32_t y = TileY(tile) + dy;
98
99 assert(x < Map::SizeX());
100 assert(y < Map::SizeY());
101 assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
102
103 return TileXY(x, y);
104}
105#endif
106
120TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
121{
122 uint x = TileX(tile) + addx;
123 uint y = TileY(tile) + addy;
124
125 /* Disallow void tiles at the north border. */
126 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
127
128 /* Are we about to wrap? */
129 if (x >= Map::MaxX() || y >= Map::MaxY()) return INVALID_TILE;
130
131 return TileXY(x, y);
132}
133
135extern const TileIndexDiffC _tileoffs_by_axis[] = {
136 { 1, 0},
137 { 0, 1},
138};
139
141extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
142 {-1, 0},
143 { 0, 1},
144 { 1, 0},
145 { 0, -1}
146};
147
149extern const TileIndexDiffC _tileoffs_by_dir[] = {
150 {-1, -1},
151 {-1, 0},
152 {-1, 1},
153 { 0, 1},
154 { 1, 1},
155 { 1, 0},
156 { 1, -1},
157 { 0, -1}
158};
159
170{
171 const uint dx = Delta(TileX(t0), TileX(t1));
172 const uint dy = Delta(TileY(t0), TileY(t1));
173 return dx + dy;
174}
175
176
187{
188 const int dx = TileX(t0) - TileX(t1);
189 const int dy = TileY(t0) - TileY(t1);
190 return dx * dx + dy * dy;
191}
192
193
202{
203 const uint dx = Delta(TileX(t0), TileX(t1));
204 const uint dy = Delta(TileY(t0), TileY(t1));
205 return std::max(dx, dy);
206}
207
208
218{
219 const uint dx = Delta(TileX(t0), TileX(t1));
220 const uint dy = Delta(TileY(t0), TileY(t1));
221 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
222}
223
230{
231 const uint xl = TileX(tile);
232 const uint yl = TileY(tile);
233 const uint xh = Map::SizeX() - 1 - xl;
234 const uint yh = Map::SizeY() - 1 - yl;
235 const uint minl = std::min(xl, yl);
236 const uint minh = std::min(xh, yh);
237 return std::min(minl, minh);
238}
239
247{
248 switch (dir) {
249 case DIAGDIR_NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
250 case DIAGDIR_NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
251 case DIAGDIR_SW: return Map::MaxX() - TileX(tile) - 1;
252 case DIAGDIR_SE: return Map::MaxY() - TileY(tile) - 1;
253 default: NOT_REACHED();
254 }
255}
256
263uint GetClosestWaterDistance(TileIndex tile, bool water)
264{
265 if (HasTileWaterGround(tile) == water) return 0;
266
267 uint max_dist = water ? 0x7F : 0x200;
268
269 int x = TileX(tile);
270 int y = TileY(tile);
271
272 uint max_x = Map::MaxX();
273 uint max_y = Map::MaxY();
274 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
275
276 /* go in a 'spiral' with increasing manhattan distance in each iteration */
277 for (uint dist = 1; dist < max_dist; dist++) {
278 /* next 'diameter' */
279 y--;
280
281 /* going counter-clockwise around this square */
282 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
283 static const int8_t ddx[DIAGDIR_END] = { -1, 1, 1, -1};
284 static const int8_t ddy[DIAGDIR_END] = { 1, 1, -1, -1};
285
286 int dx = ddx[dir];
287 int dy = ddy[dir];
288
289 /* each side of this square has length 'dist' */
290 for (uint a = 0; a < dist; a++) {
291 /* TileType::Void tiles are not checked (interval is [min; max) for IsInsideMM())*/
292 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
293 TileIndex t = TileXY(x, y);
294 if (HasTileWaterGround(t) == water) return dist;
295 }
296 x += dx;
297 y += dy;
298 }
299 }
300 }
301
302 if (!water) {
303 /* no land found - is this a water-only map? */
304 for (const auto t : Map::Iterate()) {
305 if (!IsTileType(t, TileType::Void) && !IsTileType(t, TileType::Water)) return 0x1FF;
306 }
307 }
308
309 return max_dist;
310}
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:186
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:229
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:201
TileIndex TileVirtXYClampedToMap(int x, int y)
Get a tile from the virtual XY-coordinate.
Definition map.cpp:84
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:246
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:217
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:120
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:169
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:263
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:461
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.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
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.
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:262
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
Definition map_func.h:320
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:271
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:366
static uint size
The number of tiles on the map.
Definition map_func.h:229
static uint LogX()
Logarithm of the map size along the X side.
Definition map_func.h:243
static uint size_x
Size of the map along the X.
Definition map_func.h:227
static uint tile_mask
_map_size - 1 (to mask the mapsize)
Definition map_func.h:230
static uint size_y
Size of the map along the Y.
Definition map_func.h:228
static uint initial_land_count
Initial number of land tiles on the map.
Definition map_func.h:232
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:298
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:289
static uint log_y
2^_map_log_y == _map_size_y
Definition map_func.h:226
static uint log_x
2^_map_log_x == _map_size_x
Definition map_func.h:225
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:37
A pair-construct of a TileIndexDiff.
Definition map_type.h:31
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
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
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
@ Water
Water tile.
Definition tile_type.h:55
@ Void
Invisible tiles at the SW and SE border.
Definition tile_type.h:56
Map accessors for water tiles.
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:353
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192
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.