summaryrefslogtreecommitdiff
path: root/src/tunnel_map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tunnel_map.cpp')
-rw-r--r--src/tunnel_map.cpp75
1 files changed, 37 insertions, 38 deletions
diff --git a/src/tunnel_map.cpp b/src/tunnel_map.cpp
index 9de523580..6234193e1 100644
--- a/src/tunnel_map.cpp
+++ b/src/tunnel_map.cpp
@@ -10,63 +10,62 @@
#include "stdafx.h"
#include "tunnelbridge_map.h"
+#include "core/pool_func.hpp"
+
#include "safeguards.h"
+/** All tunnel portals tucked away in a pool. */
+TunnelPool _tunnel_pool("Tunnel");
+INSTANTIATE_POOL_METHODS(Tunnel)
/**
- * Gets the other end of the tunnel. Where a vehicle would reappear when it
- * enters at the given tile.
- * @param tile the tile to search from.
- * @return the tile of the other end of the tunnel.
+ * Clean up a tunnel tile
*/
-TileIndex GetOtherTunnelEnd(TileIndex tile)
+Tunnel::~Tunnel()
{
- DiagDirection dir = GetTunnelBridgeDirection(tile);
- TileIndexDiff delta = TileOffsByDiagDir(dir);
- int z = GetTileZ(tile);
-
- dir = ReverseDiagDir(dir);
- do {
- tile += delta;
- } while (
- !IsTunnelTile(tile) ||
- GetTunnelBridgeDirection(tile) != dir ||
- GetTileZ(tile) != z
- );
-
- return tile;
+ if (CleaningPool()) return;
}
-
/**
- * Is there a tunnel in the way in the given direction?
+ * Gets the other end of the tunnel. Where a vehicle would reappear when it
+ * enters at the given tile.
* @param tile the tile to search from.
- * @param z the 'z' to search on.
- * @param dir the direction to start searching to.
- * @return true if and only if there is a tunnel.
+ * @return the tile of the other end of the tunnel.
*/
-bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
+TileIndex GetOtherTunnelEnd(TileIndex tile)
{
- TileIndexDiff delta = TileOffsByDiagDir(dir);
- int height;
-
- do {
- tile -= delta;
- if (!IsValidTile(tile)) return false;
- height = GetTileZ(tile);
- } while (z < height);
-
- return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
+ Tunnel *t = Tunnel::GetByTile(tile);
+ return t->tile_n == tile ? t->tile_s : t->tile_n;
}
/**
* Is there a tunnel in the way in any direction?
* @param tile the tile to search from.
* @param z the 'z' to search on.
+ * @param chunnel_allowed True if chunnel mid-parts are allowed, used when terraforming.
* @return true if and only if there is a tunnel.
*/
-bool IsTunnelInWay(TileIndex tile, int z)
+bool IsTunnelInWay(TileIndex tile, int z, bool chunnel_allowed)
{
- return IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
- IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
+ uint x = TileX(tile);
+ uint y = TileY(tile);
+
+ for(Tunnel *t : Tunnel::Iterate()) {
+ if (t->tile_n > tile || tile > t->tile_s) continue;
+
+ if (t->tile_s - t->tile_n > MapMaxX()){
+ if (TileX(t->tile_n) != x || (int)TileHeight(t->tile_n) != z) continue; // dir DIAGDIR_SE
+ } else {
+ if (TileY(t->tile_n) != y || (int)TileHeight(t->tile_n) != z) continue; // dir DIAGDIR_SW
+ }
+
+ if (t->is_chunnel && chunnel_allowed) {
+ /* Only if tunnel was build over water terraforming is allowed between portals. */
+ TileIndexDiff delta = GetTunnelBridgeDirection(t->tile_n) == DIAGDIR_SE ? TileOffsByDiagDir(DIAGDIR_SE) * 4 : 4; // 4 tiles ramp.
+ if (tile < t->tile_n + delta || t->tile_s - delta < tile) return true;
+ continue;
+ }
+ return true;
+ }
+ return false;
}