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 road.h Road specific functions. */
9 
10 #ifndef ROAD_H
11 #define ROAD_H
12 
13 #include "road_type.h"
14 #include "gfx_type.h"
15 #include "core/bitmath_func.hpp"
16 #include "strings_type.h"
17 #include "date_type.h"
18 #include "core/enum_type.hpp"
19 #include "newgrf.h"
20 #include "economy_func.h"
21 
22 #include <vector>
23 
24 enum RoadTramType : bool {
25 	RTT_ROAD,
26 	RTT_TRAM,
27 };
28 
29 enum RoadTramTypes : uint8 {
30 	RTTB_ROAD = 1 << RTT_ROAD,
31 	RTTB_TRAM = 1 << RTT_TRAM,
32 };
33 DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
34 
35 static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
36 
37 /** Roadtype flags. Starts with RO instead of R because R is used for rails */
38 enum RoadTypeFlags {
39 	ROTF_CATENARY = 0,                                     ///< Bit number for adding catenary
40 	ROTF_NO_LEVEL_CROSSING,                                ///< Bit number for disabling level crossing
41 	ROTF_NO_HOUSES,                                        ///< Bit number for setting this roadtype as not house friendly
42 	ROTF_HIDDEN,                                           ///< Bit number for hidden from construction.
43 	ROTF_TOWN_BUILD,                                       ///< Bit number for allowing towns to build this roadtype.
44 
45 	ROTFB_NONE = 0,                                        ///< All flags cleared.
46 	ROTFB_CATENARY          = 1 << ROTF_CATENARY,          ///< Value for drawing a catenary.
47 	ROTFB_NO_LEVEL_CROSSING = 1 << ROTF_NO_LEVEL_CROSSING, ///< Value for disabling a level crossing.
48 	ROTFB_NO_HOUSES         = 1 << ROTF_NO_HOUSES,         ///< Value for for setting this roadtype as not house friendly.
49 	ROTFB_HIDDEN            = 1 << ROTF_HIDDEN,            ///< Value for hidden from construction.
50 	ROTFB_TOWN_BUILD        = 1 << ROTF_TOWN_BUILD,        ///< Value for allowing towns to build this roadtype.
51 };
52 DECLARE_ENUM_AS_BIT_SET(RoadTypeFlags)
53 
54 struct SpriteGroup;
55 
56 /** Sprite groups for a roadtype. */
57 enum RoadTypeSpriteGroup {
58 	ROTSG_CURSORS,        ///< Optional: Cursor and toolbar icon images
59 	ROTSG_OVERLAY,        ///< Optional: Images for overlaying track
60 	ROTSG_GROUND,         ///< Required: Main group of ground images
61 	ROTSG_reserved1,      ///<           Placeholder, if we need specific tunnel sprites.
62 	ROTSG_CATENARY_FRONT, ///< Optional: Catenary front
63 	ROTSG_CATENARY_BACK,  ///< Optional: Catenary back
64 	ROTSG_BRIDGE,         ///< Required: Bridge surface images
65 	ROTSG_reserved2,      ///<           Placeholder, if we need specific level crossing sprites.
66 	ROTSG_DEPOT,          ///< Optional: Depot images
67 	ROTSG_reserved3,      ///<           Placeholder, if we add road fences (for highways).
68 	ROTSG_ROADSTOP,       ///< Required: Drive-in stop surface
69 	ROTSG_END,
70 };
71 
72 /** List of road type labels. */
73 typedef std::vector<RoadTypeLabel> RoadTypeLabelList;
74 
75 class RoadTypeInfo {
76 public:
77 	/**
78 	 * struct containing the sprites for the road GUI. @note only sprites referred to
79 	 * directly in the code are listed
80 	 */
81 	struct {
82 		SpriteID build_x_road;        ///< button for building single rail in X direction
83 		SpriteID build_y_road;        ///< button for building single rail in Y direction
84 		SpriteID auto_road;           ///< button for the autoroad construction
85 		SpriteID build_depot;         ///< button for building depots
86 		SpriteID build_tunnel;        ///< button for building a tunnel
87 		SpriteID convert_road;        ///< button for converting road types
88 	} gui_sprites;
89 
90 	struct {
91 		CursorID road_swne;     ///< Cursor for building rail in X direction
92 		CursorID road_nwse;     ///< Cursor for building rail in Y direction
93 		CursorID autoroad;      ///< Cursor for autorail tool
94 		CursorID depot;         ///< Cursor for building a depot
95 		CursorID tunnel;        ///< Cursor for building a tunnel
96 		SpriteID convert_road;  ///< Cursor for converting road types
97 	} cursor;                       ///< Cursors associated with the road type.
98 
99 	struct {
100 		StringID name;            ///< Name of this rail type.
101 		StringID toolbar_caption; ///< Caption in the construction toolbar GUI for this rail type.
102 		StringID menu_text;       ///< Name of this rail type in the main toolbar dropdown.
103 		StringID build_caption;   ///< Caption of the build vehicle GUI for this rail type.
104 		StringID replace_text;    ///< Text used in the autoreplace GUI.
105 		StringID new_engine;      ///< Name of an engine for this type of road in the engine preview GUI.
106 
107 		StringID err_build_road;        ///< Building a normal piece of road
108 		StringID err_remove_road;       ///< Removing a normal piece of road
109 		StringID err_depot;             ///< Building a depot
110 		StringID err_build_station[2];  ///< Building a bus or truck station
111 		StringID err_remove_station[2]; ///< Removing of a bus or truck station
112 		StringID err_convert_road;      ///< Converting a road type
113 
114 		StringID picker_title[2];       ///< Title for the station picker for bus or truck stations
115 		StringID picker_tooltip[2];     ///< Tooltip for the station picker for bus or truck stations
116 	} strings;                        ///< Strings associated with the rail type.
117 
118 	/** bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power */
119 	RoadTypes powered_roadtypes;
120 
121 	/**
122 	 * Bit mask of road type flags
123 	 */
124 	RoadTypeFlags flags;
125 
126 	/**
127 	 * Cost multiplier for building this road type
128 	 */
129 	uint16 cost_multiplier;
130 
131 	/**
132 	 * Cost multiplier for maintenance of this road type
133 	 */
134 	uint16 maintenance_multiplier;
135 
136 	/**
137 	 * Maximum speed for vehicles travelling on this road type
138 	 */
139 	uint16 max_speed;
140 
141 	/**
142 	 * Unique 32 bit road type identifier
143 	 */
144 	RoadTypeLabel label;
145 
146 	/**
147 	 * Road type labels this type provides in addition to the main label.
148 	 */
149 	RoadTypeLabelList alternate_labels;
150 
151 	/**
152 	 * Colour on mini-map
153 	 */
154 	byte map_colour;
155 
156 	/**
157 	 * Introduction date.
158 	 * When #INVALID_DATE or a vehicle using this roadtype gets introduced earlier,
159 	 * the vehicle's introduction date will be used instead for this roadtype.
160 	 * The introduction at this date is furthermore limited by the
161 	 * #introduction_required_types.
162 	 */
163 	Date introduction_date;
164 
165 	/**
166 	 * Bitmask of roadtypes that are required for this roadtype to be introduced
167 	 * at a given #introduction_date.
168 	 */
169 	RoadTypes introduction_required_roadtypes;
170 
171 	/**
172 	 * Bitmask of which other roadtypes are introduced when this roadtype is introduced.
173 	 */
174 	RoadTypes introduces_roadtypes;
175 
176 	/**
177 	 * The sorting order of this roadtype for the toolbar dropdown.
178 	 */
179 	byte sorting_order;
180 
181 	/**
182 	 * NewGRF providing the Action3 for the roadtype. nullptr if not available.
183 	 */
184 	const GRFFile *grffile[ROTSG_END];
185 
186 	/**
187 	 * Sprite groups for resolving sprites
188 	 */
189 	const SpriteGroup *group[ROTSG_END];
190 
UsesOverlay()191 	inline bool UsesOverlay() const
192 	{
193 		return this->group[ROTSG_GROUND] != nullptr;
194 	}
195 };
196 
197 extern RoadTypes _roadtypes_type;
198 
RoadTypeIsRoad(RoadType roadtype)199 static inline bool RoadTypeIsRoad(RoadType roadtype)
200 {
201 	return !HasBit(_roadtypes_type, roadtype);
202 }
203 
RoadTypeIsTram(RoadType roadtype)204 static inline bool RoadTypeIsTram(RoadType roadtype)
205 {
206 	return HasBit(_roadtypes_type, roadtype);
207 }
208 
GetRoadTramType(RoadType roadtype)209 static inline RoadTramType GetRoadTramType(RoadType roadtype)
210 {
211 	return RoadTypeIsTram(roadtype) ? RTT_TRAM : RTT_ROAD;
212 }
213 
OtherRoadTramType(RoadTramType rtt)214 static inline RoadTramType OtherRoadTramType(RoadTramType rtt)
215 {
216 	return rtt == RTT_ROAD ? RTT_TRAM : RTT_ROAD;
217 }
218 
219 /**
220  * Returns a pointer to the Roadtype information for a given roadtype
221  * @param roadtype the road type which the information is requested for
222  * @return The pointer to the RoadTypeInfo
223  */
GetRoadTypeInfo(RoadType roadtype)224 static inline const RoadTypeInfo *GetRoadTypeInfo(RoadType roadtype)
225 {
226 	extern RoadTypeInfo _roadtypes[ROADTYPE_END];
227 	assert(roadtype < ROADTYPE_END);
228 	return &_roadtypes[roadtype];
229 }
230 
231 /**
232  * Checks if an engine of the given RoadType got power on a tile with a given
233  * RoadType. This would normally just be an equality check, but for electrified
234  * roads (which also support non-electric vehicles).
235  * @return Whether the engine got power on this tile.
236  * @param  enginetype The RoadType of the engine we are considering.
237  * @param  tiletype   The RoadType of the tile we are considering.
238  */
HasPowerOnRoad(RoadType enginetype,RoadType tiletype)239 static inline bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
240 {
241 	return HasBit(GetRoadTypeInfo(enginetype)->powered_roadtypes, tiletype);
242 }
243 
244 /**
245  * Returns the cost of building the specified roadtype.
246  * @param roadtype The roadtype being built.
247  * @return The cost multiplier.
248  */
RoadBuildCost(RoadType roadtype)249 static inline Money RoadBuildCost(RoadType roadtype)
250 {
251 	assert(roadtype < ROADTYPE_END);
252 	return (_price[PR_BUILD_ROAD] * GetRoadTypeInfo(roadtype)->cost_multiplier) >> 3;
253 }
254 
255 /**
256  * Returns the cost of clearing the specified roadtype.
257  * @param roadtype The roadtype being removed.
258  * @return The cost.
259  */
RoadClearCost(RoadType roadtype)260 static inline Money RoadClearCost(RoadType roadtype)
261 {
262 	assert(roadtype < ROADTYPE_END);
263 
264 	/* Flat fee for removing road. */
265 	if (RoadTypeIsRoad(roadtype)) return _price[PR_CLEAR_ROAD];
266 
267 	/* Clearing tram earns a little money, but also incurs the standard clear road cost,
268 	 * so no profit can be made. */
269 	return _price[PR_CLEAR_ROAD] - RoadBuildCost(roadtype) * 3 / 4;
270 }
271 
272 /**
273  * Calculates the cost of road conversion
274  * @param from The roadtype we are converting from
275  * @param to   The roadtype we are converting to
276  * @return Cost per RoadBit
277  */
RoadConvertCost(RoadType from,RoadType to)278 static inline Money RoadConvertCost(RoadType from, RoadType to)
279 {
280 	/* Don't apply convert costs when converting to the same roadtype (ex. building a roadstop over existing road) */
281 	if (from == to) return (Money)0;
282 
283 	/* Same cost as removing and then building. */
284 	return RoadBuildCost(to) + RoadClearCost(from);
285 }
286 
287 /**
288  * Test if road disallows level crossings
289  * @param roadtype The roadtype we are testing
290  * @return True iff the roadtype disallows level crossings
291  */
RoadNoLevelCrossing(RoadType roadtype)292 static inline bool RoadNoLevelCrossing(RoadType roadtype)
293 {
294 	assert(roadtype < ROADTYPE_END);
295 	return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_NO_LEVEL_CROSSING);
296 }
297 
298 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels = true);
299 
300 void ResetRoadTypes();
301 void InitRoadTypes();
302 RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt);
303 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt);
304 
305 extern std::vector<RoadType> _sorted_roadtypes;
306 extern RoadTypes _roadtypes_hidden_mask;
307 
308 #endif /* ROAD_H */
309