1 /*
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/>.
6  */
7 
8 /** @file script_bridge.hpp Everything to query and build bridges. */
9 
10 #ifndef SCRIPT_BRIDGE_HPP
11 #define SCRIPT_BRIDGE_HPP
12 
13 #include "script_vehicle.hpp"
14 
15 /**
16  * Class that handles all bridge related functions.
17  * @api ai game
18  */
19 class ScriptBridge : public ScriptObject {
20 public:
21 	/**
22 	 * All bridge related error messages.
23 	 */
24 	enum ErrorMessages {
25 		/** Base for bridge related errors */
26 		ERR_BRIDGE_BASE = ScriptError::ERR_CAT_BRIDGE << ScriptError::ERR_CAT_BIT_SIZE,
27 
28 		/**
29 		 * The bridge you want to build is not available yet,
30 		 * or it is not available for the requested length.
31 		 */
32 		ERR_BRIDGE_TYPE_UNAVAILABLE,         // [STR_ERROR_CAN_T_BUILD_BRIDGE_HERE]
33 
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]
36 
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]
39 	};
40 
41 	/**
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.
45 	 */
46 	static bool IsValidBridge(BridgeID bridge_id);
47 
48 	/**
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.
53 	 */
54 	static bool IsBridgeTile(TileIndex tile);
55 
56 	/**
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'.
61 	 */
62 	static BridgeID GetBridgeID(TileIndex tile);
63 
64 	/**
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.
71 	 */
72 	static char *GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type);
73 
74 	/**
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.
82 	 */
83 	static int32 GetMaxSpeed(BridgeID bridge_id);
84 
85 	/**
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 	 * @pre IsValidBridge(bridge_id).
90 	 * @return The new cost the bridge has.
91 	 */
92 	static Money GetPrice(BridgeID bridge_id, uint length);
93 
94 	/**
95 	 * Get the maximum length of a bridge.
96 	 * @param bridge_id The bridge to get the maximum length of.
97 	 * @pre IsValidBridge(bridge_id).
98 	 * @returns The maximum length the bridge has.
99 	 */
100 	static int32 GetMaxLength(BridgeID bridge_id);
101 
102 	/**
103 	 * Get the minimum length of a bridge.
104 	 * @param bridge_id The bridge to get the minimum length of.
105 	 * @pre IsValidBridge(bridge_id).
106 	 * @returns The minimum length the bridge has.
107 	 */
108 	static int32 GetMinLength(BridgeID bridge_id);
109 
110 	/**
111 	 * Internal function to help BuildBridge in case of road.
112 	 * @api -all
113 	 */
114 	static bool _BuildBridgeRoad1();
115 
116 	/**
117 	 * Internal function to help BuildBridge in case of road.
118 	 * @api -all
119 	 */
120 	static bool _BuildBridgeRoad2();
121 
122 	/**
123 	 * Build a bridge from one tile to the other.
124 	 * As an extra for road, this functions builds two half-pieces of road on
125 	 *  each end of the bridge, making it easier for you to connect it to your
126 	 *  network.
127 	 * @param vehicle_type The vehicle-type of bridge to build.
128 	 * @param bridge_id The bridge-type to build.
129 	 * @param start Where to start the bridge.
130 	 * @param end Where to end the bridge.
131 	 * @pre ScriptMap::IsValidTile(start).
132 	 * @pre ScriptMap::IsValidTile(end).
133 	 * @pre 'start' and 'end' are in a straight line, i.e.
134 	 *  ScriptMap::GetTileX(start) == ScriptMap::GetTileX(end) or
135 	 *  ScriptMap::GetTileY(start) == ScriptMap::GetTileY(end).
136 	 * @pre vehicle_type == ScriptVehicle::VT_WATER ||
137 	 *   (vehicle_type == ScriptVehicle::VT_ROAD && ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType())) ||
138 	 *   (vehicle_type == ScriptVehicle::VT_RAIL && ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType())).
139 	 * @game @pre Outside CompanyMode: vehicle_type == ScriptVehicle::VT_ROAD.
140 	 * @exception ScriptError::ERR_ALREADY_BUILT
141 	 * @exception ScriptError::ERR_AREA_NOT_CLEAR
142 	 * @exception ScriptError::ERR_LAND_SLOPED_WRONG
143 	 * @exception ScriptError::ERR_VEHICLE_IN_THE_WAY
144 	 * @exception ScriptBridge::ERR_BRIDGE_TYPE_UNAVAILABLE
145 	 * @exception ScriptBridge::ERR_BRIDGE_CANNOT_END_IN_WATER
146 	 * @exception ScriptBridge::ERR_BRIDGE_HEADS_NOT_ON_SAME_HEIGHT
147 	 * @return Whether the bridge has been/can be build or not.
148 	 * @game @note Building a bridge (without CompanyMode) results in a bridge owned by towns.
149 	 * @note No matter if the road pieces were build or not, if building the
150 	 *  bridge succeeded, this function returns true.
151 	 */
152 	static bool BuildBridge(ScriptVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end);
153 
154 	/**
155 	 * Removes a bridge, by executing it on either the start or end tile.
156 	 * @param tile An end or start tile of the bridge.
157 	 * @pre ScriptMap::IsValidTile(tile).
158 	 * @game @pre Valid ScriptCompanyMode active in scope.
159 	 * @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
160 	 * @return Whether the bridge has been/can be removed or not.
161 	 */
162 	static bool RemoveBridge(TileIndex tile);
163 
164 	/**
165 	 * Get the tile that is on the other end of a bridge starting at tile.
166 	 * @param tile The tile that is an end of a bridge.
167 	 * @pre ScriptMap::IsValidTile(tile).
168 	 * @pre IsBridgeTile(tile).
169 	 * @return The TileIndex that is the other end of the bridge.
170 	 */
171 	static TileIndex GetOtherBridgeEnd(TileIndex tile);
172 };
173 
174 #endif /* SCRIPT_BRIDGE_HPP */
175