# ai_bridge.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_bridge.cpp 16531 2009-06-07 15:26:33Z rubidium $ */
    00002
    00005 #include "ai_bridge.hpp"
    00006 #include "ai_rail.hpp"
    00007 #include "../ai_instance.hpp"
    00008 #include "../../bridge_map.h"
    00009 #include "../../strings_func.h"
    00010 #include "../../core/alloc_func.hpp"
    00011 #include "../../economy_func.h"
    00012 #include "../../settings_type.h"
    00013 #include "../../date_func.h"
    00014
    00015 /* static */ bool AIBridge::IsValidBridge(BridgeID bridge_id)
    00016 {
    00017   return bridge_id < MAX_BRIDGES && ::GetBridgeSpec(bridge_id)->avail_year <= _cur_year;
    00018 }
    00019
    00020 /* static */ bool AIBridge::IsBridgeTile(TileIndex tile)
    00021 {
    00022   if (!::IsValidTile(tile)) return false;
    00023   return ::IsBridgeTile(tile);
    00024 }
    00025
    00026 /* static */ BridgeID AIBridge::GetBridgeID(TileIndex tile)
    00027 {
    00028   if (!IsBridgeTile(tile)) return (BridgeID)-1;
    00029   return (BridgeID)::GetBridgeType(tile);
    00030 }
    00031
    00032 static void _DoCommandReturnBuildBridge2(class AIInstance *instance)
    00033 {
    00034   if (!AIBridge::_BuildBridgeRoad2()) {
    00035     AIInstance::DoCommandReturn(instance);
    00036     return;
    00037   }
    00038
    00039   /* This can never happen, as in test-mode this callback is never executed,
    00040    *  and in execute-mode, the other callback is called. */
    00041   NOT_REACHED();
    00042 }
    00043
    00044 static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
    00045 {
    00046   if (!AIBridge::_BuildBridgeRoad1()) {
    00047     AIInstance::DoCommandReturn(instance);
    00048     return;
    00049   }
    00050
    00051   /* This can never happen, as in test-mode this callback is never executed,
    00052    *  and in execute-mode, the other callback is called. */
    00053   NOT_REACHED();
    00054 }
    00055
    00056 /* static */ bool AIBridge::BuildBridge(AIVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
    00057 {
    00058   EnforcePrecondition(false, start != end);
    00059   EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
    00060   EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
    00061   EnforcePrecondition(false, vehicle_type == AIVehicle::VT_ROAD || vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER);
    00062   EnforcePrecondition(false, vehicle_type != AIVehicle::VT_RAIL || AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType()));
    00063
    00064   uint type = 0;
    00065   switch (vehicle_type) {
    00066     case AIVehicle::VT_ROAD:
    00067       type |= (TRANSPORT_ROAD << 15);
    00068       type |= (::RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType()) << 8);
    00069       break;
    00070     case AIVehicle::VT_RAIL:
    00071       type |= (TRANSPORT_RAIL << 15);
    00072       type |= (AIRail::GetCurrentRailType() << 8);
    00073       break;
    00074     case AIVehicle::VT_WATER:
    00075       type |= (TRANSPORT_WATER << 15);
    00076       break;
    00077     default: NOT_REACHED();
    00078   }
    00079
    00080   /* For rail and water we do nothing special */
    00081   if (vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER) {
    00082     return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE);
    00083   }
    00084
    00085   AIObject::SetCallbackVariable(0, start);
    00086   AIObject::SetCallbackVariable(1, end);
    00087   return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &_DoCommandReturnBuildBridge1);
    00088 }
    00089
    00090 /* static */ bool AIBridge::_BuildBridgeRoad1()
    00091 {
    00092   /* Build the piece of road on the 'start' side of the bridge */
    00093   TileIndex end = AIObject::GetCallbackVariable(0);
    00094   TileIndex start = AIObject::GetCallbackVariable(1);
    00095
    00096   DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
    00097   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
    00098
    00099   return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2);
    00100 }
    00101
    00102 /* static */ bool AIBridge::_BuildBridgeRoad2()
    00103 {
    00104   /* Build the piece of road on the 'end' side of the bridge */
    00105   TileIndex end = AIObject::GetCallbackVariable(0);
    00106   TileIndex start = AIObject::GetCallbackVariable(1);
    00107
    00108   DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
    00109   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
    00110
    00111   return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
    00112 }
    00113
    00114 /* static */ bool AIBridge::RemoveBridge(TileIndex tile)
    00115 {
    00116   EnforcePrecondition(false, IsBridgeTile(tile));
    00117   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00118 }
    00119
    00120 /* static */ char *AIBridge::GetName(BridgeID bridge_id)
    00121 {
    00122   if (!IsValidBridge(bridge_id)) return NULL;
    00123
    00124   static const int len = 64;
    00125   char *bridge_name = MallocT<char>(len);
    00126
    00127   ::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]);
    00128   return bridge_name;
    00129 }
    00130
    00131 /* static */ int32 AIBridge::GetMaxSpeed(BridgeID bridge_id)
    00132 {
    00133   if (!IsValidBridge(bridge_id)) return -1;
    00134
    00135   return ::GetBridgeSpec(bridge_id)->speed; // km-ish/h
    00136 }
    00137
    00138 /* static */ Money AIBridge::GetPrice(BridgeID bridge_id, uint length)
    00139 {
    00140   if (!IsValidBridge(bridge_id)) return -1;
    00141
    00142   return length * _price.build_bridge * ::GetBridgeSpec(bridge_id)->price >> 8;
    00143 }
    00144
    00145 /* static */ int32 AIBridge::GetMaxLength(BridgeID bridge_id)
    00146 {
    00147   if (!IsValidBridge(bridge_id)) return -1;
    00148
    00149   uint max = ::GetBridgeSpec(bridge_id)->max_length;
    00150   if (max >= 16 && _settings_game.construction.longbridges) max = 100;
    00151   return max + 2;
    00152 }
    00153
    00154 /* static */ int32 AIBridge::GetMinLength(BridgeID bridge_id)
    00155 {
    00156   if (!IsValidBridge(bridge_id)) return -1;
    00157
    00158   return ::GetBridgeSpec(bridge_id)->min_length + 2;
    00159 }
    00160
    00161 /* static */ TileIndex AIBridge::GetOtherBridgeEnd(TileIndex tile)
    00162 {
    00163   if (!::IsValidTile(tile)) return INVALID_TILE;
    00164   if (!IsBridgeTile(tile)) return INVALID_TILE;
    00165
    00166   return ::GetOtherBridgeEnd(tile);
    00167 }