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.hpp Everything to query and build bridges. */
10 #ifndef SCRIPT_BRIDGE_HPP
11 #define SCRIPT_BRIDGE_HPP
13 #include "script_vehicle.hpp"
16 * Class that handles all bridge related functions.
19 class ScriptBridge
: public ScriptObject
{
22 * All bridge related error messages.
25 /** Base for bridge related errors */
26 ERR_BRIDGE_BASE
= ScriptError::ERR_CAT_BRIDGE
<< ScriptError::ERR_CAT_BIT_SIZE
,
29 * The bridge you want to build is not available yet,
30 * or it is not available for the requested length.
32 ERR_BRIDGE_TYPE_UNAVAILABLE
, // [STR_ERROR_CAN_T_BUILD_BRIDGE_HERE]
34 /** One (or more) of the bridge head(s) ends in water. */
35 ERR_BRIDGE_CANNOT_END_IN_WATER
, // [STR_ERROR_ENDS_OF_BRIDGE_MUST_BOTH]
37 /** The bride heads need to be on the same height */
38 ERR_BRIDGE_HEADS_NOT_ON_SAME_HEIGHT
, // [STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT]
42 * Checks whether the given bridge type is valid.
43 * @param bridge_id The bridge to check.
44 * @return True if and only if the bridge type is valid.
46 static bool IsValidBridge(BridgeID bridge_id
);
49 * Checks whether the given tile is actually a bridge start or end tile.
50 * @param tile The tile to check.
51 * @pre ScriptMap::IsValidTile(tile).
52 * @return True if and only if the tile is the beginning or end of a bridge.
54 static bool IsBridgeTile(TileIndex tile
);
57 * Get the BridgeID of a bridge at a given tile.
58 * @param tile The tile to get the BridgeID from.
59 * @pre IsBridgeTile(tile).
60 * @return The BridgeID from the bridge at tile 'tile'.
62 static BridgeID
GetBridgeID(TileIndex tile
);
65 * Get the name of a bridge.
66 * @param bridge_id The bridge to get the name of.
67 * @param vehicle_type The vehicle-type of bridge to get the name of.
68 * @pre IsValidBridge(bridge_id).
69 * @pre vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER
70 * @return The name the bridge has.
72 static std::optional
<std::string
> GetName(BridgeID bridge_id
, ScriptVehicle::VehicleType vehicle_type
);
75 * Get the maximum speed of a bridge.
76 * @param bridge_id The bridge to get the maximum speed of.
77 * @pre IsValidBridge(bridge_id).
78 * @return The maximum speed the bridge has.
79 * @note The speed is in OpenTTD's internal speed unit.
80 * This is mph / 1.6, which is roughly km/h.
81 * To get km/h multiply this number by 1.00584.
83 static SQInteger
GetMaxSpeed(BridgeID bridge_id
);
86 * Get the new cost of a bridge, excluding the road and/or rail.
87 * @param bridge_id The bridge to get the new cost of.
88 * @param length The length of the bridge.
89 * The value will be clamped to 0 .. MAX(int32_t).
90 * @pre IsValidBridge(bridge_id).
91 * @return The new cost the bridge has.
93 static Money
GetPrice(BridgeID bridge_id
, SQInteger length
);
96 * Get the maximum length of a bridge.
97 * @param bridge_id The bridge to get the maximum length of.
98 * @pre IsValidBridge(bridge_id).
99 * @returns The maximum length the bridge has.
101 static SQInteger
GetMaxLength(BridgeID bridge_id
);
104 * Get the minimum length of a bridge.
105 * @param bridge_id The bridge to get the minimum length of.
106 * @pre IsValidBridge(bridge_id).
107 * @returns The minimum length the bridge has.
109 static SQInteger
GetMinLength(BridgeID bridge_id
);
112 * Internal function to help BuildBridge in case of road.
115 static bool _BuildBridgeRoad1();
118 * Internal function to help BuildBridge in case of road.
121 static bool _BuildBridgeRoad2();
124 * Build a bridge from one tile to the other.
125 * As an extra for road, this functions builds two half-pieces of road on
126 * each end of the bridge, making it easier for you to connect it to your
128 * @param vehicle_type The vehicle-type of bridge to build.
129 * @param bridge_id The bridge-type to build.
130 * @param start Where to start the bridge.
131 * @param end Where to end the bridge.
132 * @pre ScriptMap::IsValidTile(start).
133 * @pre ScriptMap::IsValidTile(end).
134 * @pre 'start' and 'end' are in a straight line, i.e.
135 * ScriptMap::GetTileX(start) == ScriptMap::GetTileX(end) or
136 * ScriptMap::GetTileY(start) == ScriptMap::GetTileY(end).
137 * @pre vehicle_type == ScriptVehicle::VT_WATER ||
138 * (vehicle_type == ScriptVehicle::VT_ROAD && ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType())) ||
139 * (vehicle_type == ScriptVehicle::VT_RAIL && ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType())).
140 * @game @pre ScriptCompanyMode::IsValid() || vehicle_type == ScriptVehicle::VT_ROAD.
141 * @exception ScriptError::ERR_ALREADY_BUILT
142 * @exception ScriptError::ERR_AREA_NOT_CLEAR
143 * @exception ScriptError::ERR_LAND_SLOPED_WRONG
144 * @exception ScriptError::ERR_VEHICLE_IN_THE_WAY
145 * @exception ScriptBridge::ERR_BRIDGE_TYPE_UNAVAILABLE
146 * @exception ScriptBridge::ERR_BRIDGE_CANNOT_END_IN_WATER
147 * @exception ScriptBridge::ERR_BRIDGE_HEADS_NOT_ON_SAME_HEIGHT
148 * @return Whether the bridge has been/can be build or not.
149 * @game @note Building a bridge as deity (ScriptCompanyMode::IsDeity()) results in a bridge owned by towns.
150 * @note No matter if the road pieces were build or not, if building the
151 * bridge succeeded, this function returns true.
153 static bool BuildBridge(ScriptVehicle::VehicleType vehicle_type
, BridgeID bridge_id
, TileIndex start
, TileIndex end
);
156 * Removes a bridge, by executing it on either the start or end tile.
157 * @param tile An end or start tile of the bridge.
158 * @pre ScriptMap::IsValidTile(tile).
159 * @game @pre ScriptCompanyMode::IsValid().
160 * @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
161 * @return Whether the bridge has been/can be removed or not.
163 static bool RemoveBridge(TileIndex tile
);
166 * Get the tile that is on the other end of a bridge starting at tile.
167 * @param tile The tile that is an end of a bridge.
168 * @pre ScriptMap::IsValidTile(tile).
169 * @pre IsBridgeTile(tile).
170 * @return The TileIndex that is the other end of the bridge.
172 static TileIndex
GetOtherBridgeEnd(TileIndex tile
);
175 #endif /* SCRIPT_BRIDGE_HPP */