script_bridge.cpp

Go to the documentation of this file.
00001 /* $Id: script_bridge.cpp 23633 2011-12-19 21:05:36Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "script_bridge.hpp"
00014 #include "script_rail.hpp"
00015 #include "../script_instance.hpp"
00016 #include "../../bridge_map.h"
00017 #include "../../strings_func.h"
00018 #include "../../economy_func.h"
00019 #include "../../date_func.h"
00020 
00021 /* static */ bool ScriptBridge::IsValidBridge(BridgeID bridge_id)
00022 {
00023   return bridge_id < MAX_BRIDGES && ::GetBridgeSpec(bridge_id)->avail_year <= _cur_year;
00024 }
00025 
00026 /* static */ bool ScriptBridge::IsBridgeTile(TileIndex tile)
00027 {
00028   if (!::IsValidTile(tile)) return false;
00029   return ::IsBridgeTile(tile);
00030 }
00031 
00032 /* static */ BridgeID ScriptBridge::GetBridgeID(TileIndex tile)
00033 {
00034   if (!IsBridgeTile(tile)) return (BridgeID)-1;
00035   return (BridgeID)::GetBridgeType(tile);
00036 }
00037 
00042 static void _DoCommandReturnBuildBridge2(class ScriptInstance *instance)
00043 {
00044   if (!ScriptBridge::_BuildBridgeRoad2()) {
00045     ScriptInstance::DoCommandReturn(instance);
00046     return;
00047   }
00048 
00049   /* This can never happen, as in test-mode this callback is never executed,
00050    *  and in execute-mode, the other callback is called. */
00051   NOT_REACHED();
00052 }
00053 
00058 static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
00059 {
00060   if (!ScriptBridge::_BuildBridgeRoad1()) {
00061     ScriptInstance::DoCommandReturn(instance);
00062     return;
00063   }
00064 
00065   /* This can never happen, as in test-mode this callback is never executed,
00066    *  and in execute-mode, the other callback is called. */
00067   NOT_REACHED();
00068 }
00069 
00070 /* static */ bool ScriptBridge::BuildBridge(ScriptVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
00071 {
00072   EnforcePrecondition(false, start != end);
00073   EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
00074   EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
00075   EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
00076   EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
00077   EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY || vehicle_type == ScriptVehicle::VT_ROAD);
00078 
00079   uint type = 0;
00080   switch (vehicle_type) {
00081     case ScriptVehicle::VT_ROAD:
00082       type |= (TRANSPORT_ROAD << 15);
00083       type |= (::RoadTypeToRoadTypes((::RoadType)ScriptObject::GetRoadType()) << 8);
00084       break;
00085     case ScriptVehicle::VT_RAIL:
00086       type |= (TRANSPORT_RAIL << 15);
00087       type |= (ScriptRail::GetCurrentRailType() << 8);
00088       break;
00089     case ScriptVehicle::VT_WATER:
00090       type |= (TRANSPORT_WATER << 15);
00091       break;
00092     default: NOT_REACHED();
00093   }
00094 
00095   /* For rail and water we do nothing special */
00096   if (vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER) {
00097     return ScriptObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE);
00098   }
00099 
00100   ScriptObject::SetCallbackVariable(0, start);
00101   ScriptObject::SetCallbackVariable(1, end);
00102   return ScriptObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &::_DoCommandReturnBuildBridge1);
00103 }
00104 
00105 /* static */ bool ScriptBridge::_BuildBridgeRoad1()
00106 {
00107   /* Build the piece of road on the 'start' side of the bridge */
00108   TileIndex end = ScriptObject::GetCallbackVariable(0);
00109   TileIndex start = ScriptObject::GetCallbackVariable(1);
00110 
00111   DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
00112   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
00113 
00114   return ScriptObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &::_DoCommandReturnBuildBridge2);
00115 }
00116 
00117 /* static */ bool ScriptBridge::_BuildBridgeRoad2()
00118 {
00119   /* Build the piece of road on the 'end' side of the bridge */
00120   TileIndex end = ScriptObject::GetCallbackVariable(0);
00121   TileIndex start = ScriptObject::GetCallbackVariable(1);
00122 
00123   DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
00124   DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
00125 
00126   return ScriptObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
00127 }
00128 
00129 /* static */ bool ScriptBridge::RemoveBridge(TileIndex tile)
00130 {
00131   EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
00132   EnforcePrecondition(false, IsBridgeTile(tile));
00133   return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
00134 }
00135 
00136 /* static */ char *ScriptBridge::GetName(BridgeID bridge_id)
00137 {
00138   if (!IsValidBridge(bridge_id)) return NULL;
00139 
00140   static const int len = 64;
00141   char *bridge_name = MallocT<char>(len);
00142 
00143   ::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]);
00144   return bridge_name;
00145 }
00146 
00147 /* static */ int32 ScriptBridge::GetMaxSpeed(BridgeID bridge_id)
00148 {
00149   if (!IsValidBridge(bridge_id)) return -1;
00150 
00151   return ::GetBridgeSpec(bridge_id)->speed; // km-ish/h
00152 }
00153 
00154 /* static */ Money ScriptBridge::GetPrice(BridgeID bridge_id, uint length)
00155 {
00156   if (!IsValidBridge(bridge_id)) return -1;
00157 
00158   return ::CalcBridgeLenCostFactor(length) * _price[PR_BUILD_BRIDGE] * ::GetBridgeSpec(bridge_id)->price >> 8;
00159 }
00160 
00161 /* static */ int32 ScriptBridge::GetMaxLength(BridgeID bridge_id)
00162 {
00163   if (!IsValidBridge(bridge_id)) return -1;
00164 
00165   return min(::GetBridgeSpec(bridge_id)->max_length, _settings_game.construction.max_bridge_length) + 2;
00166 }
00167 
00168 /* static */ int32 ScriptBridge::GetMinLength(BridgeID bridge_id)
00169 {
00170   if (!IsValidBridge(bridge_id)) return -1;
00171 
00172   return ::GetBridgeSpec(bridge_id)->min_length + 2;
00173 }
00174 
00175 /* static */ TileIndex ScriptBridge::GetOtherBridgeEnd(TileIndex tile)
00176 {
00177   if (!::IsValidTile(tile)) return INVALID_TILE;
00178   if (!IsBridgeTile(tile)) return INVALID_TILE;
00179 
00180   return ::GetOtherBridgeEnd(tile);
00181 }