1
/* $Id: ai_tunnel.cpp 15299 2009-01-31 20:16:06Z smatz $ */
3
/** @file ai_tunnel.cpp Implementation of AITunnel. */
5
#include "ai_tunnel.hpp"
7
#include "../ai_instance.hpp"
8
#include "../../tunnel_map.h"
9
#include "../../command_func.h"
10
#include "../../tunnelbridge.h"
11
#include "../../road_func.h"
13
/* static */ bool AITunnel::IsTunnelTile(TileIndex tile)
15
if (!::IsValidTile(tile)) return false;
16
return ::IsTunnelTile(tile);
19
/* static */ TileIndex AITunnel::GetOtherTunnelEnd(TileIndex tile)
21
if (!::IsValidTile(tile)) return INVALID_TILE;
23
/* If it's a tunnel alread, take the easy way out! */
24
if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
26
::DoCommand(tile, 0, 0, DC_AUTO, CMD_BUILD_TUNNEL);
27
return _build_tunnel_endtile == 0 ? INVALID_TILE : _build_tunnel_endtile;
30
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
32
if (!AITunnel::_BuildTunnelRoad2()) {
33
AIObject::SetLastCommandRes(false);
34
AIInstance::DoCommandReturn(instance);
38
/* This can never happen, as in test-mode this callback is never executed,
39
* and in execute-mode, the other callback is called. */
43
static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
45
if (!AITunnel::_BuildTunnelRoad1()) {
46
AIObject::SetLastCommandRes(false);
47
AIInstance::DoCommandReturn(instance);
51
/* This can never happen, as in test-mode this callback is never executed,
52
* and in execute-mode, the other callback is called. */
56
/* static */ bool AITunnel::BuildTunnel(AIVehicle::VehicleType vehicle_type, TileIndex start)
58
EnforcePrecondition(false, ::IsValidTile(start));
59
EnforcePrecondition(false, vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_ROAD);
60
EnforcePrecondition(false, vehicle_type != AIVehicle::VT_RAIL || AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType()));
63
if (vehicle_type == AIVehicle::VT_ROAD) {
64
type |= (TRANSPORT_ROAD << 9);
65
type |= RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType());
67
type |= (TRANSPORT_RAIL << 9);
68
type |= AIRail::GetCurrentRailType();
71
/* For rail we do nothing special */
72
if (vehicle_type == AIVehicle::VT_RAIL) {
73
return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
76
AIObject::SetCallbackVariable(0, start);
77
if (!AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &_DoCommandReturnBuildTunnel1)) return false;
79
/* In case of test-mode, test if we can build both road pieces */
80
return _BuildTunnelRoad1();
83
/* static */ bool AITunnel::_BuildTunnelRoad1()
85
/* Build the piece of road on the 'start' side of the tunnel */
86
TileIndex end = AIObject::GetCallbackVariable(0);
87
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
89
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
90
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
92
if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildTunnel2)) return false;
94
/* In case of test-mode, test the other road piece too */
95
return _BuildTunnelRoad2();
98
/* static */ bool AITunnel::_BuildTunnelRoad2()
100
/* Build the piece of road on the 'end' side of the tunnel */
101
TileIndex end = AIObject::GetCallbackVariable(0);
102
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
104
DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
105
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
107
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
110
/* static */ bool AITunnel::RemoveTunnel(TileIndex tile)
112
EnforcePrecondition(false, IsTunnelTile(tile));
114
return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);