OpenTTD Source: src/map.cpp Source File

OpenTTD Source 15.0-RC2

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
10#include "stdafx.h"
11#include "debug.h"
12#include "water_map.h"
13#include "error_func.h"
14#include "string_func.h"
15#include "pathfinder/water_regions.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. */
41 if (!IsInsideMM(size_x, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
42 !IsInsideMM(size_y, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
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
50 Map::log_x = FindFirstBit(size_x);
51 Map::log_y = FindFirstBit(size_y);
52 Map::size_x = size_x;
53 Map::size_y = size_y;
54 Map::size = size_x * size_y;
55 Map::tile_mask = Map::size - 1;
56
57 Tile::base_tiles = std::make_unique<Tile::TileBase[]>(Map::size);
58 Tile::extended_tiles = std::make_unique<Tile::TileExtended[]>(Map::size);
59
60 AllocateWaterRegions();
61}
62
63/* static */ void Map::CountLandTiles()
64{
65 /* Count number of tiles that are land. */
66 Map::initial_land_count = 0;
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%. */
73 Map::initial_land_count += Map::initial_land_count / 12;
74 Map::initial_land_count = std::min(Map::initial_land_count, Map::size);
75}
76
77
78#ifdef _DEBUG
79 TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
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
109 TileIndex 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
158uint DistanceManhattan(TileIndex t0, TileIndex t1)
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
175uint DistanceSquare(TileIndex t0, TileIndex t1)
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
190uint DistanceMax(TileIndex t0, TileIndex t1)
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
206uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
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
218uint DistanceFromEdge(TileIndex tile)
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
235uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
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}
FindFirstBit
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Definition bitmath_func.hpp:189
Tile::extended_tiles
static std::unique_ptr< TileExtended[]> extended_tiles
Pointer to the extended tile-array.
Definition map_func.h:55
Tile::base_tiles
static std::unique_ptr< TileBase[]> base_tiles
Pointer to the tile-array.
Definition map_func.h:54
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition direction_type.h:80
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition direction_type.h:83
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition direction_type.h:81
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition direction_type.h:84
DIAGDIR_BEGIN
@ DIAGDIR_BEGIN
Used for iterations.
Definition direction_type.h:79
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition direction_type.h:82
error_func.h
Error reporting related functions.
DistanceSquare
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:175
_tileoffs_by_axis
const TileIndexDiffC _tileoffs_by_axis[]
'Lookup table' for tile offsets given an Axis
DistanceFromEdge
uint DistanceFromEdge(TileIndex tile)
Param the minimum distance to an edge.
Definition map.cpp:218
DistanceMax
uint DistanceMax(TileIndex t0, TileIndex t1)
Gets the biggest distance component (x or y) between the two given tiles.
Definition map.cpp:190
DistanceFromEdgeDir
uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
Gets the distance to the edge of the map in given direction.
Definition map.cpp:235
DistanceMaxPlusManhattan
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
_tileoffs_by_dir
const TileIndexDiffC _tileoffs_by_dir[]
'Lookup table' for tile offsets given a Direction
TileAddWrap
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
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:158
GetClosestWaterDistance
uint GetClosestWaterDistance(TileIndex tile, bool water)
Finds the distance for the closest tile with water/land given a tile.
Definition map.cpp:252
_tileoffs_by_diagdir
const TileIndexDiffC _tileoffs_by_diagdir[]
'Lookup table' for tile offsets given a DiagDirection
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
TileAdd
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition map_func.h:469
MAX_MAP_SIZE
static const uint MAX_MAP_SIZE
Maximal map size = 4096.
Definition map_type.h:40
MIN_MAP_SIZE
static const uint MIN_MAP_SIZE
Minimal map size = 64.
Definition map_type.h:39
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
IsInsideMM
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition math_func.hpp:267
Delta
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition math_func.hpp:234
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition settings_type.h:423
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition settings_type.h:635
Map
Size related data of the map.
Definition map_func.h:206
Map::SizeX
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:272
Map::WrapToMap
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
Definition map_func.h:329
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:281
Map::Iterate
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:375
Map::size
static uint size
The number of tiles on the map.
Definition map_func.h:239
Map::size_x
static uint size_x
Size of the map along the X.
Definition map_func.h:237
Map::tile_mask
static uint tile_mask
_map_size - 1 (to mask the mapsize)
Definition map_func.h:240
Map::size_y
static uint size_y
Size of the map along the Y.
Definition map_func.h:238
Map::initial_land_count
static uint initial_land_count
Initial number of land tiles on the map.
Definition map_func.h:242
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
Map::MaxX
static uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
Map::log_y
static uint log_y
2^_map_log_y == _map_size_y
Definition map_func.h:236
Map::log_x
static uint log_x
2^_map_log_x == _map_size_x
Definition map_func.h:235
Map::Allocate
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:37
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TileIndexDiffC
A pair-construct of a TileIndexDiff.
Definition map_type.h:31
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
MP_WATER
@ MP_WATER
Water tile.
Definition tile_type.h:54
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition tile_type.h:55
water_map.h
Map accessors for water tiles.
HasTileWaterGround
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:352
IsWaterTile
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192
AllocateWaterRegions
void AllocateWaterRegions()
Allocates the appropriate amount of water regions for the current map size.
Definition water_regions.cpp:414
water_regions.h
Handles dividing the water in the map into regions to assist pathfinding.