summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_tunnel.cpp
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2009-05-06 21:38:59 +0000
committeryexo <yexo@openttd.org>2009-05-06 21:38:59 +0000
commitca435fcece2b98d2d4e6f80df6eb1eaeb2e060dc (patch)
treebdd412bfda6223a9eaf306426d30ec52dd9d9d67 /src/ai/api/ai_tunnel.cpp
parent2664f2a2d95dbc2122ff1f9b96e8569ae401892f (diff)
downloadopenttd-ca435fcece2b98d2d4e6f80df6eb1eaeb2e060dc.tar.xz
(svn r16243) -Fix [FS#2875]: CmdBuildTunnel could be called with invalid parameters from the api code, causing crashes later
Diffstat (limited to 'src/ai/api/ai_tunnel.cpp')
-rw-r--r--src/ai/api/ai_tunnel.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/ai/api/ai_tunnel.cpp b/src/ai/api/ai_tunnel.cpp
index cc7d95606..ea103ce4e 100644
--- a/src/ai/api/ai_tunnel.cpp
+++ b/src/ai/api/ai_tunnel.cpp
@@ -23,8 +23,21 @@
/* If it's a tunnel alread, take the easy way out! */
if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
- ::DoCommand(tile, 0, 0, DC_AUTO, CMD_BUILD_TUNNEL);
- return _build_tunnel_endtile == 0 ? INVALID_TILE : _build_tunnel_endtile;
+ uint start_z;
+ Slope start_tileh = ::GetTileSlope(tile, &start_z);
+ DiagDirection direction = ::GetInclinedSlopeDirection(start_tileh);
+ if (direction == INVALID_DIAGDIR) return INVALID_TILE;
+
+ TileIndexDiff delta = ::TileOffsByDiagDir(direction);
+ uint end_z;
+ do {
+ tile += delta;
+ if (!::IsValidTile(tile)) return INVALID_TILE;
+
+ ::GetTileSlope(tile, &end_z);
+ } while (start_z != end_z);
+
+ return tile;
}
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)