ai_bridge.cpp

Go to the documentation of this file.
00001 /* $Id: ai_bridge.cpp 15912 2009-04-01 14:24:54Z 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     AIObject::SetLastCommandRes(false);
00036     AIInstance::DoCommandReturn(instance);
00037     return;
00038   }
00039 
00040   /* This can never happen, as in test-mode this callback is never executed,
00041    *  and in execute-mode, the other callback is called. */
00042   NOT_REACHED();
00043 }
00044 
00045 static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
00046 {
00047   if (!AIBridge::_BuildBridgeRoad1()) {
00048     AIObject::SetLastCommandRes(false);
00049     AIInstance::DoCommandReturn(instance);
00050     return;
00051   }
00052 
00053   /* This can never happen, as in test-mode this callback is never executed,
00054    *  and in execute-mode, the other callback is called. */
00055   NOT_REACHED();
00056 }
00057 
00058 /* static */ bool AIBridge::BuildBridge(AIVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
00059 {
00060   EnforcePrecondition(false, start != end);
00061   EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
00062   EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
00063   EnforcePrecondition(false, vehicle_type == AIVehicle::VT_ROAD || vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER);
00064   EnforcePrecondition(false, vehicle_type != AIVehicle::VT_RAIL || AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType()));
00065 
00066   uint type = 0;
00067   switch (vehicle_type) {
00068     case AIVehicle::VT_ROAD:
00069       type |= (TRANSPORT_ROAD << 15);
00070       type |= (RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType()) << 8);
00071       break;
00072     case AIVehicle::VT_RAIL:
00073       type |= (TRANSPORT_RAIL << 15);
00074       type |= (AIRail::GetCurrentRailType() << 8);
00075       break;
00076     case AIVehicle::VT_WATER:
00077       type |= (TRANSPORT_WATER << 15);
00078       break;
00079     default: NOT_REACHED();
00080   }
00081 
00082   /* For rail and water we do nothing special */
00083   if (vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER) {
00084     return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE);
00085   }
00086 
00087   AIObject::SetCallbackVariable(0, start);
00088   AIObject::SetCallbackVariable(1, end);
00089   if (!AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &_DoCommandReturnBuildBridge1)) return false;
00090 
00091   /* In case of test-mode, test if we can build both road pieces */
00092   return _BuildBridgeRoad1();
00093 }
00094 
00095 /* static */ bool AIBridge::_BuildBridgeRoad1()
00096 {
00097   /* Build the piece of road on the 'start' side of the bridge */
00098   TileIndex end = AIObject::GetCallbackVariable(0);
00099   TileIndex start = AIObject::GetCallbackVariable(1);
00100 
00101   DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
00102   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
00103 
00104   if (!AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2)) return false;
00105 
00106   /* In case of test-mode, test the other road piece too */
00107   return _BuildBridgeRoad2();
00108 }
00109 
00110 /* static */ bool AIBridge::_BuildBridgeRoad2()
00111 {
00112   /* Build the piece of road on the 'end' side of the bridge */
00113   TileIndex end = AIObject::GetCallbackVariable(0);
00114   TileIndex start = AIObject::GetCallbackVariable(1);
00115 
00116   DiagDirection dir_1 = (DiagDirection)((::TileX(start) == ::TileX(end)) ? (::TileY(start) < ::TileY(end) ? DIAGDIR_NW : DIAGDIR_SE) : (::TileX(start) < ::TileX(end) ? DIAGDIR_NE : DIAGDIR_SW));
00117   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
00118 
00119   return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
00120 }
00121 
00122 /* static */ bool AIBridge::RemoveBridge(TileIndex tile)
00123 {
00124   EnforcePrecondition(false, IsBridgeTile(tile));
00125   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
00126 }
00127 
00128 /* static */ char *AIBridge::GetName(BridgeID bridge_id)
00129 {
00130   if (!IsValidBridge(bridge_id)) return NULL;
00131 
00132   static const int len = 64;
00133   char *bridge_name = MallocT<char>(len);
00134 
00135   ::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]);
00136   return bridge_name;
00137 }
00138 
00139 /* static */ int32 AIBridge::GetMaxSpeed(BridgeID bridge_id)
00140 {
00141   if (!IsValidBridge(bridge_id)) return -1;
00142 
00143   return ::GetBridgeSpec(bridge_id)->speed; // km-ish/h
00144 }
00145 
00146 /* static */ Money AIBridge::GetPrice(BridgeID bridge_id, uint length)
00147 {
00148   if (!IsValidBridge(bridge_id)) return -1;
00149 
00150   return length * _price.build_bridge * ::GetBridgeSpec(bridge_id)->price >> 8;
00151 }
00152 
00153 /* static */ int32 AIBridge::GetMaxLength(BridgeID bridge_id)
00154 {
00155   if (!IsValidBridge(bridge_id)) return -1;
00156 
00157   uint max = ::GetBridgeSpec(bridge_id)->max_length;
00158   if (max >= 16 && _settings_game.construction.longbridges) max = 100;
00159   return max + 2;
00160 }
00161 
00162 /* static */ int32 AIBridge::GetMinLength(BridgeID bridge_id)
00163 {
00164   if (!IsValidBridge(bridge_id)) return -1;
00165 
00166   return ::GetBridgeSpec(bridge_id)->min_length + 2;
00167 }
00168 
00169 /* static */ TileIndex AIBridge::GetOtherBridgeEnd(TileIndex tile)
00170 {
00171   if (!::IsValidTile(tile)) return INVALID_TILE;
00172   if (!IsBridgeTile(tile)) return INVALID_TILE;
00173 
00174   return ::GetOtherBridgeEnd(tile);
00175 }

Generated on Wed Jun 3 19:05:08 2009 for OpenTTD by  doxygen 1.5.6