diff --git a/src/tunnel_map.cpp b/src/tunnel_map.cpp index e46d7af947..1771b1d82b 100644 --- a/src/tunnel_map.cpp +++ b/src/tunnel_map.cpp @@ -40,28 +40,6 @@ TileIndex GetOtherTunnelEnd(TileIndex tile) return t->tile_n == tile ? t->tile_s : t->tile_n; } - -/** - * Is there a tunnel in the way in the given direction? - * @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. - */ -bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir) -{ - 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; -} - /** * Is there a tunnel in the way in any direction? * @param tile the tile to search from. @@ -70,6 +48,20 @@ bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir) */ bool IsTunnelInWay(TileIndex tile, int z) { - 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); + + Tunnel *t; + FOR_ALL_TUNNELS(t) { + 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 + } + + return true; + } + return false; } diff --git a/src/tunnel_map.h b/src/tunnel_map.h index c1c1e45e24..3621f54eb6 100644 --- a/src/tunnel_map.h +++ b/src/tunnel_map.h @@ -52,7 +52,6 @@ static inline TunnelID GetTunnelIndex(TileIndex t) TileIndex GetOtherTunnelEnd(TileIndex); bool IsTunnelInWay(TileIndex, int z); -bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir); /** * Makes a road tunnel entrance diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index e5c20dffe2..9091fb2502 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -663,12 +663,6 @@ CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, * position, because of increased-cost-by-length: 'cost += cost >> 3' */ TileIndexDiff delta = TileOffsByDiagDir(direction); - DiagDirection tunnel_in_way_dir; - if (DiagDirToAxis(direction) == AXIS_Y) { - tunnel_in_way_dir = (TileX(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE; - } else { - tunnel_in_way_dir = (TileY(start_tile) < (MapMaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW; - } TileIndex end_tile = start_tile; @@ -688,7 +682,7 @@ CommandCost CmdBuildTunnel(TileIndex start_tile, DoCommandFlag flags, uint32 p1, if (start_z == end_z) break; - if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) { + if (!_cheats.crossing_tunnels.value && IsTunnelInWay(end_tile, start_z)) { return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY); }