2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file script_bridge.cpp Implementation of ScriptBridge. */
10 #include "../../stdafx.h"
11 #include "script_bridge.hpp"
12 #include "script_rail.hpp"
13 #include "../script_instance.hpp"
14 #include "../../bridge_map.h"
15 #include "../../strings_func.h"
16 #include "../../landscape_cmd.h"
17 #include "../../road_cmd.h"
18 #include "../../tunnelbridge_cmd.h"
19 #include "../../timer/timer_game_calendar.h"
20 #include "table/strings.h"
22 #include "../../safeguards.h"
24 /* static */ bool ScriptBridge::IsValidBridge(BridgeID bridge_id
)
26 return bridge_id
< MAX_BRIDGES
&& ::GetBridgeSpec(bridge_id
)->avail_year
<= TimerGameCalendar::year
;
29 /* static */ bool ScriptBridge::IsBridgeTile(TileIndex tile
)
31 if (!::IsValidTile(tile
)) return false;
32 return ::IsBridgeTile(tile
);
35 /* static */ BridgeID
ScriptBridge::GetBridgeID(TileIndex tile
)
37 if (!IsBridgeTile(tile
)) return (BridgeID
)-1;
38 return (BridgeID
)::GetBridgeType(tile
);
42 * Helper function to connect a just built bridge to nearby roads.
43 * @param instance The script instance we have to built the road for.
45 static void _DoCommandReturnBuildBridge2(class ScriptInstance
*instance
)
47 if (!ScriptBridge::_BuildBridgeRoad2()) {
48 ScriptInstance::DoCommandReturn(instance
);
52 /* This can never happen, as in test-mode this callback is never executed,
53 * and in execute-mode, the other callback is called. */
58 * Helper function to connect a just built bridge to nearby roads.
59 * @param instance The script instance we have to built the road for.
61 static void _DoCommandReturnBuildBridge1(class ScriptInstance
*instance
)
63 if (!ScriptBridge::_BuildBridgeRoad1()) {
64 ScriptInstance::DoCommandReturn(instance
);
68 /* This can never happen, as in test-mode this callback is never executed,
69 * and in execute-mode, the other callback is called. */
73 /* static */ bool ScriptBridge::BuildBridge(ScriptVehicle::VehicleType vehicle_type
, BridgeID bridge_id
, TileIndex start
, TileIndex end
)
75 EnforceDeityOrCompanyModeValid(false);
76 EnforcePrecondition(false, start
!= end
);
77 EnforcePrecondition(false, ::IsValidTile(start
) && ::IsValidTile(end
));
78 EnforcePrecondition(false, TileX(start
) == TileX(end
) || TileY(start
) == TileY(end
));
79 EnforcePrecondition(false, vehicle_type
== ScriptVehicle::VT_ROAD
|| vehicle_type
== ScriptVehicle::VT_RAIL
|| vehicle_type
== ScriptVehicle::VT_WATER
);
80 EnforcePrecondition(false, vehicle_type
!= ScriptVehicle::VT_RAIL
|| ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
81 EnforcePrecondition(false, vehicle_type
!= ScriptVehicle::VT_ROAD
|| ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType()));
82 EnforcePrecondition(false, ScriptCompanyMode::IsValid() || vehicle_type
== ScriptVehicle::VT_ROAD
);
84 switch (vehicle_type
) {
85 case ScriptVehicle::VT_ROAD
:
86 ScriptObject::SetCallbackVariable(0, start
.base());
87 ScriptObject::SetCallbackVariable(1, end
.base());
88 return ScriptObject::Command
<CMD_BUILD_BRIDGE
>::Do(&::_DoCommandReturnBuildBridge1
, end
, start
, TRANSPORT_ROAD
, bridge_id
, ScriptRoad::GetCurrentRoadType());
89 case ScriptVehicle::VT_RAIL
:
90 return ScriptObject::Command
<CMD_BUILD_BRIDGE
>::Do(end
, start
, TRANSPORT_RAIL
, bridge_id
, ScriptRail::GetCurrentRailType());
91 case ScriptVehicle::VT_WATER
:
92 return ScriptObject::Command
<CMD_BUILD_BRIDGE
>::Do(end
, start
, TRANSPORT_WATER
, bridge_id
, 0);
93 default: NOT_REACHED();
97 /* static */ bool ScriptBridge::_BuildBridgeRoad1()
99 EnforceDeityOrCompanyModeValid(false);
101 /* Build the piece of road on the 'start' side of the bridge */
102 TileIndex end
= ScriptObject::GetCallbackVariable(0);
103 TileIndex start
= ScriptObject::GetCallbackVariable(1);
105 DiagDirection dir_1
= ::DiagdirBetweenTiles(end
, start
);
106 DiagDirection dir_2
= ::ReverseDiagDir(dir_1
);
108 return ScriptObject::Command
<CMD_BUILD_ROAD
>::Do(&::_DoCommandReturnBuildBridge2
, start
+ ::TileOffsByDiagDir(dir_1
), ::DiagDirToRoadBits(dir_2
), (::RoadType
)ScriptRoad::GetCurrentRoadType(), DRD_NONE
, 0);
111 /* static */ bool ScriptBridge::_BuildBridgeRoad2()
113 EnforceDeityOrCompanyModeValid(false);
115 /* Build the piece of road on the 'end' side of the bridge */
116 TileIndex end
= ScriptObject::GetCallbackVariable(0);
117 TileIndex start
= ScriptObject::GetCallbackVariable(1);
119 DiagDirection dir_1
= ::DiagdirBetweenTiles(end
, start
);
120 DiagDirection dir_2
= ::ReverseDiagDir(dir_1
);
122 return ScriptObject::Command
<CMD_BUILD_ROAD
>::Do(end
+ ::TileOffsByDiagDir(dir_2
), ::DiagDirToRoadBits(dir_1
), (::RoadType
)ScriptRoad::GetCurrentRoadType(), DRD_NONE
, 0);
125 /* static */ bool ScriptBridge::RemoveBridge(TileIndex tile
)
127 EnforceCompanyModeValid(false);
128 EnforcePrecondition(false, IsBridgeTile(tile
));
129 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
132 /* static */ std::optional
<std::string
> ScriptBridge::GetName(BridgeID bridge_id
, ScriptVehicle::VehicleType vehicle_type
)
134 EnforcePrecondition(std::nullopt
, vehicle_type
== ScriptVehicle::VT_ROAD
|| vehicle_type
== ScriptVehicle::VT_RAIL
|| vehicle_type
== ScriptVehicle::VT_WATER
);
135 if (!IsValidBridge(bridge_id
)) return std::nullopt
;
137 return GetString(vehicle_type
== ScriptVehicle::VT_WATER
? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT
: ::GetBridgeSpec(bridge_id
)->transport_name
[vehicle_type
]);
140 /* static */ SQInteger
ScriptBridge::GetMaxSpeed(BridgeID bridge_id
)
142 if (!IsValidBridge(bridge_id
)) return -1;
144 return ::GetBridgeSpec(bridge_id
)->speed
; // km-ish/h
147 /* static */ Money
ScriptBridge::GetPrice(BridgeID bridge_id
, SQInteger length
)
149 if (!IsValidBridge(bridge_id
)) return -1;
151 length
= Clamp
<SQInteger
>(length
, 0, INT32_MAX
);
153 return ::CalcBridgeLenCostFactor(length
) * _price
[PR_BUILD_BRIDGE
] * ::GetBridgeSpec(bridge_id
)->price
>> 8;
156 /* static */ SQInteger
ScriptBridge::GetMaxLength(BridgeID bridge_id
)
158 if (!IsValidBridge(bridge_id
)) return -1;
160 return std::min
<SQInteger
>(::GetBridgeSpec(bridge_id
)->max_length
, _settings_game
.construction
.max_bridge_length
) + 2;
163 /* static */ SQInteger
ScriptBridge::GetMinLength(BridgeID bridge_id
)
165 if (!IsValidBridge(bridge_id
)) return -1;
167 return static_cast<SQInteger
>(::GetBridgeSpec(bridge_id
)->min_length
) + 2;
170 /* static */ TileIndex
ScriptBridge::GetOtherBridgeEnd(TileIndex tile
)
172 if (!::IsValidTile(tile
)) return INVALID_TILE
;
173 if (!IsBridgeTile(tile
)) return INVALID_TILE
;
175 return ::GetOtherBridgeEnd(tile
);