OpenTTD Source 20251116-master-g21329071df
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 */ 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
77
78#ifdef _DEBUG
80{
81 int dx = offset & Map::MaxX();
82 if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
83 int dy = (offset - dx) / (int)Map::SizeX();
84
85 uint32_t x = TileX(tile) + dx;
86 uint32_t y = TileY(tile) + dy;
87
88 assert(x < Map::SizeX());
89 assert(y < Map::SizeY());
90 assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
91
92 return TileXY(x, y);
93}
94#endif
95
109TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
110{
111 uint x = TileX(tile) + addx;
112 uint y = TileY(tile) + addy;
113
114 /* Disallow void tiles at the north border. */
115 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
116
117 /* Are we about to wrap? */
118 if (x >= Map::MaxX() || y >= Map::MaxY()) return INVALID_TILE;
119
120 return TileXY(x, y);
121}
122
124extern const TileIndexDiffC _tileoffs_by_axis[] = {
125 { 1, 0},
126 { 0, 1},
127};
128
130extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
131 {-1, 0},
132 { 0, 1},
133 { 1, 0},
134 { 0, -1}
135};
136
138extern const TileIndexDiffC _tileoffs_by_dir[] = {
139 {-1, -1},
140 {-1, 0},
141 {-1, 1},
142 { 0, 1},
143 { 1, 1},
144 { 1, 0},
145 { 1, -1},
146 { 0, -1}
147};
148
159{
160 const uint dx = Delta(TileX(t0), TileX(t1));
161 const uint dy = Delta(TileY(t0), TileY(t1));
162 return dx + dy;
163}
164
165
176{
177 const int dx = TileX(t0) - TileX(t1);
178 const int dy = TileY(t0) - TileY(t1);
179 return dx * dx + dy * dy;
180}
181
182
191{
192 const uint dx = Delta(TileX(t0), TileX(t1));
193 const uint dy = Delta(TileY(t0), TileY(t1));
194 return std::max(dx, dy);
195}
196
197
207{
208 const uint dx = Delta(TileX(t0), TileX(t1));
209 const uint dy = Delta(TileY(t0), TileY(t1));
210 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
211}
212
219{
220 const uint xl = TileX(tile);
221 const uint yl = TileY(tile);
222 const uint xh = Map::SizeX() - 1 - xl;
223 const uint yh = Map::SizeY() - 1 - yl;
224 const uint minl = std::min(xl, yl);
225 const uint minh = std::min(xh, yh);
226 return std::min(minl, minh);
227}
228
236{
237 switch (dir) {
238 case DIAGDIR_NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
239 case DIAGDIR_NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
240 case DIAGDIR_SW: return Map::MaxX() - TileX(tile) - 1;
241 case DIAGDIR_SE: return Map::MaxY() - TileY(tile) - 1;
242 default: NOT_REACHED();
243 }
244}
245
252uint GetClosestWaterDistance(TileIndex tile, bool water)
253{
254 if (HasTileWaterGround(tile) == water) return 0;
255
256 uint max_dist = water ? 0x7F : 0x200;
257
258 int x = TileX(tile);
259 int y = TileY(tile);
260
261 uint max_x = Map::MaxX();
262 uint max_y = Map::MaxY();
263 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
264
265 /* go in a 'spiral' with increasing manhattan distance in each iteration */
266 for (uint dist = 1; dist < max_dist; dist++) {
267 /* next 'diameter' */
268 y--;
269
270 /* going counter-clockwise around this square */
271 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
272 static const int8_t ddx[DIAGDIR_END] = { -1, 1, 1, -1};
273 static const int8_t ddy[DIAGDIR_END] = { 1, 1, -1, -1};
274
275 int dx = ddx[dir];
276 int dy = ddy[dir];
277
278 /* each side of this square has length 'dist' */
279 for (uint a = 0; a < dist; a++) {
280 /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
281 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
282 TileIndex t = TileXY(x, y);
283 if (HasTileWaterGround(t) == water) return dist;
284 }
285 x += dx;
286 y += dy;
287 }
288 }
289 }
290
291 if (!water) {
292 /* no land found - is this a water-only map? */
293 for (const auto t : Map::Iterate()) {
294 if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
295 }
296 }
297
298 return max_dist;
299}
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:175
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:218
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:190
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:235
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:206
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:109
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:158
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:252
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:385
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:469
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
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
Size related data of the map.
Definition map_func.h:206
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
Definition map_func.h:329
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:281
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:375
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:272
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 initial_land_count
Initial number of land tiles on the map.
Definition map_func.h:242
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
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:37
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
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:352
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.