~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/ai/api/ai_tunnel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: ai_tunnel.cpp 15299 2009-01-31 20:16:06Z smatz $ */
 
2
 
 
3
/** @file ai_tunnel.cpp Implementation of AITunnel. */
 
4
 
 
5
#include "ai_tunnel.hpp"
 
6
#include "ai_rail.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"
 
12
 
 
13
/* static */ bool AITunnel::IsTunnelTile(TileIndex tile)
 
14
{
 
15
        if (!::IsValidTile(tile)) return false;
 
16
        return ::IsTunnelTile(tile);
 
17
}
 
18
 
 
19
/* static */ TileIndex AITunnel::GetOtherTunnelEnd(TileIndex tile)
 
20
{
 
21
        if (!::IsValidTile(tile)) return INVALID_TILE;
 
22
 
 
23
        /* If it's a tunnel alread, take the easy way out! */
 
24
        if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
 
25
 
 
26
        ::DoCommand(tile, 0, 0, DC_AUTO, CMD_BUILD_TUNNEL);
 
27
        return _build_tunnel_endtile == 0 ? INVALID_TILE : _build_tunnel_endtile;
 
28
}
 
29
 
 
30
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
 
31
{
 
32
        if (!AITunnel::_BuildTunnelRoad2()) {
 
33
                AIObject::SetLastCommandRes(false);
 
34
                AIInstance::DoCommandReturn(instance);
 
35
                return;
 
36
        }
 
37
 
 
38
        /* This can never happen, as in test-mode this callback is never executed,
 
39
         *  and in execute-mode, the other callback is called. */
 
40
        NOT_REACHED();
 
41
}
 
42
 
 
43
static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
 
44
{
 
45
        if (!AITunnel::_BuildTunnelRoad1()) {
 
46
                AIObject::SetLastCommandRes(false);
 
47
                AIInstance::DoCommandReturn(instance);
 
48
                return;
 
49
        }
 
50
 
 
51
        /* This can never happen, as in test-mode this callback is never executed,
 
52
         *  and in execute-mode, the other callback is called. */
 
53
        NOT_REACHED();
 
54
}
 
55
 
 
56
/* static */ bool AITunnel::BuildTunnel(AIVehicle::VehicleType vehicle_type, TileIndex start)
 
57
{
 
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()));
 
61
 
 
62
        uint type = 0;
 
63
        if (vehicle_type == AIVehicle::VT_ROAD) {
 
64
                type |= (TRANSPORT_ROAD << 9);
 
65
                type |= RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType());
 
66
        } else {
 
67
                type |= (TRANSPORT_RAIL << 9);
 
68
                type |= AIRail::GetCurrentRailType();
 
69
        }
 
70
 
 
71
        /* For rail we do nothing special */
 
72
        if (vehicle_type == AIVehicle::VT_RAIL) {
 
73
                return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
 
74
        }
 
75
 
 
76
        AIObject::SetCallbackVariable(0, start);
 
77
        if (!AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &_DoCommandReturnBuildTunnel1)) return false;
 
78
 
 
79
        /* In case of test-mode, test if we can build both road pieces */
 
80
        return _BuildTunnelRoad1();
 
81
}
 
82
 
 
83
/* static */ bool AITunnel::_BuildTunnelRoad1()
 
84
{
 
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);
 
88
 
 
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);
 
91
 
 
92
        if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildTunnel2)) return false;
 
93
 
 
94
        /* In case of test-mode, test the other road piece too */
 
95
        return _BuildTunnelRoad2();
 
96
}
 
97
 
 
98
/* static */ bool AITunnel::_BuildTunnelRoad2()
 
99
{
 
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);
 
103
 
 
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);
 
106
 
 
107
        return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
 
108
}
 
109
 
 
110
/* static */ bool AITunnel::RemoveTunnel(TileIndex tile)
 
111
{
 
112
        EnforcePrecondition(false, IsTunnelTile(tile));
 
113
 
 
114
        return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
 
115
}