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

« back to all changes in this revision

Viewing changes to src/ai/api/ai_bridge.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_bridge.cpp 15912 2009-04-01 14:24:54Z rubidium $ */
 
2
 
 
3
/** @file ai_bridge.cpp Implementation of AIBridge. */
 
4
 
 
5
#include "ai_bridge.hpp"
 
6
#include "ai_rail.hpp"
 
7
#include "../ai_instance.hpp"
 
8
#include "../../bridge_map.h"
 
9
#include "../../strings_func.h"
 
10
#include "../../core/alloc_func.hpp"
 
11
#include "../../economy_func.h"
 
12
#include "../../settings_type.h"
 
13
#include "../../date_func.h"
 
14
 
 
15
/* static */ bool AIBridge::IsValidBridge(BridgeID bridge_id)
 
16
{
 
17
        return bridge_id < MAX_BRIDGES && ::GetBridgeSpec(bridge_id)->avail_year <= _cur_year;
 
18
}
 
19
 
 
20
/* static */ bool AIBridge::IsBridgeTile(TileIndex tile)
 
21
{
 
22
        if (!::IsValidTile(tile)) return false;
 
23
        return ::IsBridgeTile(tile);
 
24
}
 
25
 
 
26
/* static */ BridgeID AIBridge::GetBridgeID(TileIndex tile)
 
27
{
 
28
        if (!IsBridgeTile(tile)) return (BridgeID)-1;
 
29
        return (BridgeID)::GetBridgeType(tile);
 
30
}
 
31
 
 
32
static void _DoCommandReturnBuildBridge2(class AIInstance *instance)
 
33
{
 
34
        if (!AIBridge::_BuildBridgeRoad2()) {
 
35
                AIObject::SetLastCommandRes(false);
 
36
                AIInstance::DoCommandReturn(instance);
 
37
                return;
 
38
        }
 
39
 
 
40
        /* This can never happen, as in test-mode this callback is never executed,
 
41
         *  and in execute-mode, the other callback is called. */
 
42
        NOT_REACHED();
 
43
}
 
44
 
 
45
static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
 
46
{
 
47
        if (!AIBridge::_BuildBridgeRoad1()) {
 
48
                AIObject::SetLastCommandRes(false);
 
49
                AIInstance::DoCommandReturn(instance);
 
50
                return;
 
51
        }
 
52
 
 
53
        /* This can never happen, as in test-mode this callback is never executed,
 
54
         *  and in execute-mode, the other callback is called. */
 
55
        NOT_REACHED();
 
56
}
 
57
 
 
58
/* static */ bool AIBridge::BuildBridge(AIVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
 
59
{
 
60
        EnforcePrecondition(false, start != end);
 
61
        EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
 
62
        EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
 
63
        EnforcePrecondition(false, vehicle_type == AIVehicle::VT_ROAD || vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER);
 
64
        EnforcePrecondition(false, vehicle_type != AIVehicle::VT_RAIL || AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType()));
 
65
 
 
66
        uint type = 0;
 
67
        switch (vehicle_type) {
 
68
                case AIVehicle::VT_ROAD:
 
69
                        type |= (TRANSPORT_ROAD << 15);
 
70
                        type |= (RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType()) << 8);
 
71
                        break;
 
72
                case AIVehicle::VT_RAIL:
 
73
                        type |= (TRANSPORT_RAIL << 15);
 
74
                        type |= (AIRail::GetCurrentRailType() << 8);
 
75
                        break;
 
76
                case AIVehicle::VT_WATER:
 
77
                        type |= (TRANSPORT_WATER << 15);
 
78
                        break;
 
79
                default: NOT_REACHED();
 
80
        }
 
81
 
 
82
        /* For rail and water we do nothing special */
 
83
        if (vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER) {
 
84
                return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE);
 
85
        }
 
86
 
 
87
        AIObject::SetCallbackVariable(0, start);
 
88
        AIObject::SetCallbackVariable(1, end);
 
89
        if (!AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &_DoCommandReturnBuildBridge1)) return false;
 
90
 
 
91
        /* In case of test-mode, test if we can build both road pieces */
 
92
        return _BuildBridgeRoad1();
 
93
}
 
94
 
 
95
/* static */ bool AIBridge::_BuildBridgeRoad1()
 
96
{
 
97
        /* Build the piece of road on the 'start' side of the bridge */
 
98
        TileIndex end = AIObject::GetCallbackVariable(0);
 
99
        TileIndex start = AIObject::GetCallbackVariable(1);
 
100
 
 
101
        DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
 
102
        DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
 
103
 
 
104
        if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2)) return false;
 
105
 
 
106
        /* In case of test-mode, test the other road piece too */
 
107
        return _BuildBridgeRoad2();
 
108
}
 
109
 
 
110
/* static */ bool AIBridge::_BuildBridgeRoad2()
 
111
{
 
112
        /* Build the piece of road on the 'end' side of the bridge */
 
113
        TileIndex end = AIObject::GetCallbackVariable(0);
 
114
        TileIndex start = AIObject::GetCallbackVariable(1);
 
115
 
 
116
        DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
 
117
        DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
 
118
 
 
119
        return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
 
120
}
 
121
 
 
122
/* static */ bool AIBridge::RemoveBridge(TileIndex tile)
 
123
{
 
124
        EnforcePrecondition(false, IsBridgeTile(tile));
 
125
        return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
 
126
}
 
127
 
 
128
/* static */ char *AIBridge::GetName(BridgeID bridge_id)
 
129
{
 
130
        if (!IsValidBridge(bridge_id)) return NULL;
 
131
 
 
132
        static const int len = 64;
 
133
        char *bridge_name = MallocT<char>(len);
 
134
 
 
135
        ::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]);
 
136
        return bridge_name;
 
137
}
 
138
 
 
139
/* static */ int32 AIBridge::GetMaxSpeed(BridgeID bridge_id)
 
140
{
 
141
        if (!IsValidBridge(bridge_id)) return -1;
 
142
 
 
143
        return ::GetBridgeSpec(bridge_id)->speed; // km-ish/h
 
144
}
 
145
 
 
146
/* static */ Money AIBridge::GetPrice(BridgeID bridge_id, uint length)
 
147
{
 
148
        if (!IsValidBridge(bridge_id)) return -1;
 
149
 
 
150
        return length * _price.build_bridge * ::GetBridgeSpec(bridge_id)->price >> 8;
 
151
}
 
152
 
 
153
/* static */ int32 AIBridge::GetMaxLength(BridgeID bridge_id)
 
154
{
 
155
        if (!IsValidBridge(bridge_id)) return -1;
 
156
 
 
157
        uint max = ::GetBridgeSpec(bridge_id)->max_length;
 
158
        if (max >= 16 && _settings_game.construction.longbridges) max = 100;
 
159
        return max + 2;
 
160
}
 
161
 
 
162
/* static */ int32 AIBridge::GetMinLength(BridgeID bridge_id)
 
163
{
 
164
        if (!IsValidBridge(bridge_id)) return -1;
 
165
 
 
166
        return ::GetBridgeSpec(bridge_id)->min_length + 2;
 
167
}
 
168
 
 
169
/* static */ TileIndex AIBridge::GetOtherBridgeEnd(TileIndex tile)
 
170
{
 
171
        if (!::IsValidTile(tile)) return INVALID_TILE;
 
172
        if (!IsBridgeTile(tile)) return INVALID_TILE;
 
173
 
 
174
        return ::GetOtherBridgeEnd(tile);
 
175
}