4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file road.h Road specific functions. */
15 #include "road_type.h"
17 #include "core/bitmath_func.hpp"
18 #include "strings_type.h"
19 #include "date_type.h"
20 #include "core/enum_type.hpp"
21 #include "core/smallvec_type.hpp"
23 #include "economy_func.h"
25 /** Roadtype flags. Starts with RO instead of R because R is used for rails */
27 ROTF_CATENARY
= 0, ///< Bit number for adding catenary
28 ROTF_NO_LEVEL_CROSSING
, ///< Bit number for disabling level crossing
29 ROTF_NO_HOUSES
, ///< Bit number for setting this roadtype as not house friendly
31 ROTFB_NONE
= 0, ///< All flags cleared.
32 ROTFB_CATENARY
= 1 << ROTF_CATENARY
, ///< Value for drawing a catenary.
33 ROTFB_NO_LEVEL_CROSSING
= 1 << ROTF_NO_LEVEL_CROSSING
, ///< Value for disabling a level crossing.
34 ROTFB_NO_HOUSES
= 1 << ROTF_NO_HOUSES
, ///< Value for for setting this roadtype as not house friendly.
36 DECLARE_ENUM_AS_BIT_SET(RoadTypeFlags
)
40 /** Sprite groups for a roadtype. */
41 enum RoadTypeSpriteGroup
{
42 ROTSG_CURSORS
, ///< Optional: Cursor and toolbar icon images
43 ROTSG_OVERLAY
, ///< Optional: Images for overlaying track
44 ROTSG_GROUND
, ///< Required: Main group of ground images
45 ROTSG_reserved1
, ///< Placeholder, if we need specific tunnel sprites.
46 ROTSG_CATENARY_FRONT
, ///< Optional: Catenary front
47 ROTSG_CATENARY_BACK
, ///< Optional: Catenary back
48 ROTSG_BRIDGE
, ///< Required: Bridge surface images
49 ROTSG_reserved2
, ///< Placeholder, if we need specific level crossing sprites.
50 ROTSG_DEPOT
, ///< Optional: Depot images
51 ROTSG_reserved3
, ///< Placeholder, if we add road fences (for highways).
52 ROTSG_ROADSTOP
, ///< Required: Drive-in stop surface
56 /** List of road type labels. */
57 typedef SmallVector
<RoadTypeLabel
, 4> RoadTypeLabelList
;
62 * struct containing the sprites for the road GUI. @note only sprites referred to
63 * directly in the code are listed
66 SpriteID build_x_road
; ///< button for building single rail in X direction
67 SpriteID build_y_road
; ///< button for building single rail in Y direction
68 SpriteID auto_road
; ///< button for the autoroad construction
69 SpriteID build_depot
; ///< button for building depots
70 SpriteID build_tunnel
; ///< button for building a tunnel
71 SpriteID convert_road
; ///< button for converting road types
75 CursorID road_swne
; ///< Cursor for building rail in X direction
76 CursorID road_nwse
; ///< Cursor for building rail in Y direction
77 CursorID autoroad
; ///< Cursor for autorail tool
78 CursorID depot
; ///< Cursor for building a depot
79 CursorID tunnel
; ///< Cursor for building a tunnel
80 SpriteID convert_road
; ///< Cursor for converting road types
81 } cursor
; ///< Cursors associated with the road type.
84 StringID name
; ///< Name of this rail type.
85 StringID toolbar_caption
; ///< Caption in the construction toolbar GUI for this rail type.
86 StringID menu_text
; ///< Name of this rail type in the main toolbar dropdown.
87 StringID build_caption
; ///< Caption of the build vehicle GUI for this rail type.
88 StringID replace_text
; ///< Text used in the autoreplace GUI.
89 StringID new_engine
; ///< Name of an engine for this type of road in the engine preview GUI.
91 StringID err_build_road
; ///< Building a normal piece of road
92 StringID err_remove_road
; ///< Removing a normal piece of road
93 StringID err_depot
; ///< Building a depot
94 StringID err_build_station
[2]; ///< Building a bus or truck station
95 StringID err_remove_station
[2]; ///< Removing of a bus or truck station
96 StringID err_convert_road
; ///< Converting a road type
98 StringID picker_title
[2]; ///< Title for the station picker for bus or truck stations
99 StringID picker_tooltip
[2]; ///< Tooltip for the station picker for bus or truck stations
100 } strings
; ///< Strings associated with the rail type.
102 /** bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power */
103 RoadSubTypes powered_roadtypes
;
106 * Bit mask of road type flags
111 * Cost multiplier for building this road type
113 uint16 cost_multiplier
;
116 * Cost multiplier for maintenance of this road type
118 uint16 maintenance_multiplier
;
121 * Maximum speed for vehicles travelling on this road type
126 * Unique 32 bit road type identifier
131 * Road type labels this type provides in addition to the main label.
133 RoadTypeLabelList alternate_labels
;
142 * When #INVALID_DATE or a vehicle using this roadtype gets introduced earlier,
143 * the vehicle's introduction date will be used instead for this roadtype.
144 * The introduction at this date is furthermore limited by the
145 * #introduction_required_types.
147 Date introduction_date
;
150 * Bitmask of roadtypes that are required for this roadtype to be introduced
151 * at a given #introduction_date.
153 RoadSubTypes introduction_required_roadtypes
;
156 * Bitmask of which other roadtypes are introduced when this roadtype is introduced.
158 RoadSubTypes introduces_roadtypes
;
161 * The sorting order of this roadtype for the toolbar dropdown.
166 * NewGRF providing the Action3 for the roadtype. NULL if not available.
168 const GRFFile
*grffile
[ROTSG_END
];
171 * Sprite groups for resolving sprites
173 const SpriteGroup
*group
[ROTSG_END
];
175 inline bool UsesOverlay() const
177 return this->group
[ROTSG_GROUND
] != NULL
;
182 * Returns a pointer to the Roadtype information for a given roadtype
183 * @param roadtype the road type which the information is requested for
184 * @return The pointer to the RoadtypeInfo
186 static inline const RoadtypeInfo
*GetRoadTypeInfo(RoadTypeIdentifier rtid
)
188 extern RoadtypeInfo _roadtypes
[ROADTYPE_END
][ROADSUBTYPE_END
];
189 assert(rtid
.IsValid());
190 return &_roadtypes
[rtid
.basetype
][rtid
.subtype
];
194 * Checks if an engine of the given RoadType got power on a tile with a given
195 * RoadType. This would normally just be an equality check, but for electrified
196 * roads (which also support non-electric vehicles).
197 * @return Whether the engine got power on this tile.
198 * @param engine_rtid The RoadType of the engine we are considering.
199 * @param tile_rtid The RoadType of the tile we are considering.
201 static inline bool HasPowerOnRoad(RoadTypeIdentifier engine_rtid
, RoadTypeIdentifier tile_rtid
)
203 return engine_rtid
.basetype
== tile_rtid
.basetype
&& HasBit(GetRoadTypeInfo(engine_rtid
)->powered_roadtypes
, tile_rtid
.subtype
);
207 * Returns the cost of building the specified roadtype.
208 * @param rti The roadtype being built.
209 * @return The cost multiplier.
211 static inline Money
RoadBuildCost(RoadTypeIdentifier rtid
)
213 assert(rtid
.IsValid());
214 return (_price
[PR_BUILD_ROAD
] * GetRoadTypeInfo(rtid
)->cost_multiplier
) >> 3;
218 * Calculates the cost of road conversion
219 * @param from The roadtype we are converting from
220 * @param to The roadtype we are converting to
221 * @return Cost per RoadBit
223 static inline Money
RoadConvertCost(RoadTypeIdentifier from
, RoadTypeIdentifier to
)
225 /* Don't apply convert costs when converting to the same roadtype (ex. building a roadstop over existing road) */
226 if (from
== to
) return (Money
)0;
228 /* Get the costs for removing and building anew
229 * A conversion can never be more costly */
230 Money rebuildcost
= RoadBuildCost(to
) - _price
[PR_CLEAR_ROAD
];
232 /* Conversion between somewhat compatible roadtypes:
233 * Pay 1/8 of the target road cost (labour costs) and additionally any difference in the
234 * build costs, if the target type is more expensive (material upgrade costs).
235 * Upgrade can never be more expensive than re-building. */
236 if (HasPowerOnRoad(from
, to
) || HasPowerOnRoad(to
, from
)) {
237 Money upgradecost
= RoadBuildCost(to
) / 8 + max((Money
)0, RoadBuildCost(to
) - RoadBuildCost(from
));
238 return min(upgradecost
, rebuildcost
);
241 /* make the price the same as remove + build new type for road types
242 * which are not compatible in any way */
249 static inline bool RoadNoLevelCrossing(RoadTypeIdentifier rtid
)
251 assert(rtid
.IsValid());
252 return HasBit(GetRoadTypeInfo(rtid
)->flags
, ROTF_NO_LEVEL_CROSSING
);
255 RoadTypeIdentifier
GetRoadTypeByLabel(RoadTypeLabel label
, RoadType subtype
, bool allow_alternate_labels
= true);
257 void ResetRoadTypes();
258 void InitRoadTypes();
259 RoadTypeIdentifier
AllocateRoadType(RoadTypeLabel label
, RoadType basetype
);
260 RoadSubTypes
ExistingRoadSubTypesForRoadType(RoadType rt
, CompanyID c
);
261 bool CanBuildRoadTypeInfrastructure(RoadTypeIdentifier rtid
, CompanyID company
);
263 extern RoadTypeIdentifier _sorted_roadtypes
[ROADTYPE_END
][ROADSUBTYPE_END
];
264 extern uint8 _sorted_roadtypes_size
[ROADTYPE_END
];
267 * Loop header for iterating over roadtypes, sorted by sortorder.
268 * @param var Roadtype.
270 #define FOR_ALL_SORTED_ROADTYPES(var, type) for (uint8 index = 0; index < _sorted_roadtypes_size[type] && (var = _sorted_roadtypes[type][index], true) ; index++)