diff options
Diffstat (limited to 'src/map.cpp')
-rw-r--r-- | src/map.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/map.cpp b/src/map.cpp index ba6c82dba..8e7aeffed 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -8,6 +8,7 @@ #include "core/alloc_func.hpp" #include "core/math_func.hpp" #include "map_func.h" +#include "tile_map.h" #if defined(_MSC_VER) /* Why the hell is that not in all MSVC headers?? */ @@ -327,3 +328,59 @@ bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOn *tile = INVALID_TILE; return false; } + +/*! + * Finds the distance for the closest tile with water/land given a tile + * @param tile the tile to find the distance too + * @param water whether to find water or land + * @return distance to nearest water (max 0x7F) / land (max 0x1FF; 0x200 if there is no land) + * @note FAILS when an industry should be seen as water + */ +uint GetClosestWaterDistance(TileIndex tile, bool water) +{ + if (IsTileType(tile, MP_WATER) == water) return 0; + + uint max_dist = water ? 0x7F : 0x200; + + int x = TileX(tile); + int y = TileY(tile); + + uint max_x = MapMaxX(); + uint max_y = MapMaxY(); + uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0; + + /* go in a 'spiral' with increasing manhattan distance in each iteration */ + for (uint dist = 1; dist < max_dist; dist++) { + /* next 'diameter' */ + y--; + + /* going counter-clockwise around this square */ + for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { + static const int8 ddx[DIAGDIR_END] = { -1, 1, 1, -1}; + static const int8 ddy[DIAGDIR_END] = { 1, 1, -1, -1}; + + int dx = ddx[dir]; + int dy = ddy[dir]; + + /* each side of this square has length 'dist' */ + for (uint a = 0; a < dist; a++) { + /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/ + if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) { + TileIndex t = TileXY(x, y); + if (IsTileType(t, MP_WATER) == water) return dist; + } + x += dx; + y += dy; + } + } + } + + if (!water) { + /* no land found - is this a water-only map? */ + for (TileIndex t = 0; t < MapSize(); t++) { + if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF; + } + } + + return max_dist; +} |