OpenTTD Source 20261112-master-gbcbec5e63f
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"
15
16#include "safeguards.h"
17
18/* static */ uint Map::log_x;
19/* static */ uint Map::log_y;
20/* static */ uint Map::size_x;
21/* static */ uint Map::size_y;
22/* static */ uint Map::size;
23/* static */ uint Map::tile_mask;
24
25/* static */ uint Map::initial_land_count;
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(Facility::Map, Severity::Error, "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/* static */ void Map::CountLandTiles()
63{
64 /* Count number of tiles that are land. */
66 for (const auto tile : Map::Iterate()) {
67 Map::initial_land_count += IsWaterTile(tile) ? 0 : 1;
68 }
69
70 /* Compensate for default values being set for (or users are most familiar with) at least
71 * very low sea level. Dividing by 12 adds roughly 8%. */
74}
75
84{
85 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()))};
86}
87
88#ifdef _DEBUG
90{
91 int dx = offset & Map::MaxX();
92 if (dx >= (int)Map::SizeX() / 2) dx -= Map::SizeX();
93 int dy = (offset - dx) / (int)Map::SizeX();
94
95 uint32_t x = TileX(tile) + dx;
96 uint32_t y = TileY(tile) + dy;
97
98 assert(x < Map::SizeX());
99 assert(y < Map::SizeY());
100 assert(TileXY(x, y) == Map::WrapToMap(tile + offset));
101
102 return TileXY(x, y);
103}
104#endif
105
119TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
120{
121 uint x = TileX(tile) + addx;
122 uint y = TileY(tile) + addy;
123
124 /* Disallow void tiles at the north border. */
125 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
126
127 /* Are we about to wrap? */
128 if (x >= Map::MaxX() || y >= Map::MaxY()) return INVALID_TILE;
129
130 return TileXY(x, y);
131}
132
135 {1, 0}, // Axis::X
136 {0, 1}, // Axis::Y
137}}};
138
141 {-1, 0}, // DiagDirection::NE
142 { 0, 1}, // DiagDirection::SE
143 { 1, 0}, // DiagDirection::SW
144 { 0, -1}, // DiagDirection::NW
145}}};
146
149 {-1, -1}, // Direction::N
150 {-1, 0}, // Direction::NE
151 {-1, 1}, // Direction::E
152 { 0, 1}, // Direction::SE
153 { 1, 1}, // Direction::S
154 { 1, 0}, // Direction::SW
155 { 1, -1}, // Direction::W
156 { 0, -1}, // Direction::NW
157}}};
158
169{
170 const uint dx = Delta(TileX(t0), TileX(t1));
171 const uint dy = Delta(TileY(t0), TileY(t1));
172 return dx + dy;
173}
174
175
186{
187 const int dx = TileX(t0) - TileX(t1);
188 const int dy = TileY(t0) - TileY(t1);
189 return dx * dx + dy * dy;
190}
191
192
201{
202 const uint dx = Delta(TileX(t0), TileX(t1));
203 const uint dy = Delta(TileY(t0), TileY(t1));
204 return std::max(dx, dy);
205}
206
207
217{
218 const uint dx = Delta(TileX(t0), TileX(t1));
219 const uint dy = Delta(TileY(t0), TileY(t1));
220 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
221}
222
229{
230 const uint xl = TileX(tile);
231 const uint yl = TileY(tile);
232 const uint xh = Map::SizeX() - 1 - xl;
233 const uint yh = Map::SizeY() - 1 - yl;
234 const uint minl = std::min(xl, yl);
235 const uint minh = std::min(xh, yh);
236 return std::min(minl, minh);
237}
238
246{
247 switch (dir) {
248 case DiagDirection::NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
249 case DiagDirection::NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
250 case DiagDirection::SW: return Map::MaxX() - TileX(tile) - 1;
251 case DiagDirection::SE: return Map::MaxY() - TileY(tile) - 1;
252 default: NOT_REACHED();
253 }
254}
255
262uint GetClosestWaterDistance(TileIndex tile, bool water)
263{
264 if (HasTileWaterGround(tile) == water) return 0;
265
266 uint max_dist = water ? 0x7F : 0x200;
267
268 int x = TileX(tile);
269 int y = TileY(tile);
270
271 uint max_x = Map::MaxX();
272 uint max_y = Map::MaxY();
273 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
274
275 /* go in a 'spiral' with increasing manhattan distance in each iteration */
276 for (uint dist = 1; dist < max_dist; dist++) {
277 /* next 'diameter' */
278 y--;
279
280 /* going counter-clockwise around this square */
282 static constexpr DiagDirectionIndexArray<int8_t> ddx{-1, 1, 1, -1};
283 static constexpr DiagDirectionIndexArray<int8_t> ddy{ 1, 1, -1, -1};
284
285 int dx = ddx[dir];
286 int dy = ddy[dir];
287
288 /* each side of this square has length 'dist' */
289 for (uint a = 0; a < dist; a++) {
290 /* TileType::Void tiles are not checked (interval is [min; max) for IsInsideMM())*/
291 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
292 TileIndex t = TileXY(x, y);
293 if (HasTileWaterGround(t) == water) return dist;
294 }
295 x += dx;
296 y += dy;
297 }
298 }
299 }
300
301 if (!water) {
302 /* no land found - is this a water-only map? */
303 for (const auto t : Map::Iterate()) {
304 if (!IsTileType(t, TileType::Void) && !IsTileType(t, TileType::Water)) return 0x1FF;
305 }
306 }
307
308 return max_dist;
309}
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Iterate a range of enum values.
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(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Map
Map message facility.
Definition debug_type.h:31
@ Error
Error, but we are recovering.
Definition debug_type.h:16
EnumIndexArray< T, Axis, Axis::End > AxisIndexArray
Array with Axis as index.
EnumIndexArray< T, DiagDirection, DiagDirection::End > DiagDirectionIndexArray
Array with DiagDirection as index.
EnumIndexArray< T, Direction, Direction::End > DirectionIndexArray
Array with Direction as index.
DiagDirection
Enumeration for diagonal directions.
@ SW
Southwest.
@ NW
Northwest.
@ End
Used for iterations.
@ NE
Northeast, upper right on your monitor.
@ SE
Southeast.
Error reporting related functions.
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:185
const AxisIndexArray< 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:228
const DiagDirectionIndexArray< TileIndexDiffC > _tileoffs_by_diagdir
'Lookup table' for tile offsets given a DiagDirection
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:200
TileIndex TileVirtXYClampedToMap(int x, int y)
Get a tile from the virtual XY-coordinate.
Definition map.cpp:83
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:245
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:216
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:119
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:168
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:262
const DirectionIndexArray< TileIndexDiffC > _tileoffs_by_dir
'Lookup table' for tile offsets given a Direction
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.
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:36
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:356
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:194
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.