summaryrefslogtreecommitdiff
path: root/src/map.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-04-24 22:27:21 +0000
committerrubidium <rubidium@openttd.org>2009-04-24 22:27:21 +0000
commit00ed8c6f2706d099be84c080759e1e038e90fcb8 (patch)
tree4bba7d7253dd255194164b00e7f9a39bb1f1fa1a /src/map.cpp
parentf2f476a5701cb00b11367ff5afb6e9d5114e0056 (diff)
downloadopenttd-00ed8c6f2706d099be84c080759e1e038e90fcb8.tar.xz
(svn r16138) -Codechange: move GetClosestWaterDistance to map*
Diffstat (limited to 'src/map.cpp')
-rw-r--r--src/map.cpp57
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;
+}