Change: Only resort town directory window on population change if necessary
[openttd-github.git] / src / road.h
blob1082ab2729499e37adcf8f412d2c27da8f1b5a67
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 */
8 /** @file road.h Road specific functions. */
10 #ifndef ROAD_H
11 #define ROAD_H
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"
22 #include <vector>
24 enum RoadTramType : bool {
25 RTT_ROAD,
26 RTT_TRAM,
29 enum RoadTramTypes : uint8 {
30 RTTB_ROAD = 1 << RTT_ROAD,
31 RTTB_TRAM = 1 << RTT_TRAM,
33 DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
35 #define FOR_ALL_ROADTRAMTYPES(x) for (RoadTramType x : { RTT_ROAD, RTT_TRAM })
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.
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.
52 DECLARE_ENUM_AS_BIT_SET(RoadTypeFlags)
54 struct SpriteGroup;
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,
72 /** List of road type labels. */
73 typedef std::vector<RoadTypeLabel> RoadTypeLabelList;
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
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;
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.
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.
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
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.
118 /** bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power */
119 RoadTypes powered_roadtypes;
122 * Bit mask of road type flags
124 RoadTypeFlags flags;
127 * Cost multiplier for building this road type
129 uint16 cost_multiplier;
132 * Cost multiplier for maintenance of this road type
134 uint16 maintenance_multiplier;
137 * Maximum speed for vehicles travelling on this road type
139 uint16 max_speed;
142 * Unique 32 bit road type identifier
144 RoadTypeLabel label;
147 * Road type labels this type provides in addition to the main label.
149 RoadTypeLabelList alternate_labels;
152 * Colour on mini-map
154 byte map_colour;
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.
163 Date introduction_date;
166 * Bitmask of roadtypes that are required for this roadtype to be introduced
167 * at a given #introduction_date.
169 RoadTypes introduction_required_roadtypes;
172 * Bitmask of which other roadtypes are introduced when this roadtype is introduced.
174 RoadTypes introduces_roadtypes;
177 * The sorting order of this roadtype for the toolbar dropdown.
179 byte sorting_order;
182 * NewGRF providing the Action3 for the roadtype. nullptr if not available.
184 const GRFFile *grffile[ROTSG_END];
187 * Sprite groups for resolving sprites
189 const SpriteGroup *group[ROTSG_END];
191 inline bool UsesOverlay() const
193 return this->group[ROTSG_GROUND] != nullptr;
197 extern RoadTypes _roadtypes_type;
199 static inline bool RoadTypeIsRoad(RoadType roadtype)
201 return !HasBit(_roadtypes_type, roadtype);
204 static inline bool RoadTypeIsTram(RoadType roadtype)
206 return HasBit(_roadtypes_type, roadtype);
209 static inline RoadTramType GetRoadTramType(RoadType roadtype)
211 return RoadTypeIsTram(roadtype) ? RTT_TRAM : RTT_ROAD;
214 static inline RoadTramType OtherRoadTramType(RoadTramType rtt)
216 return rtt == RTT_ROAD ? RTT_TRAM : RTT_ROAD;
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
224 static inline const RoadTypeInfo *GetRoadTypeInfo(RoadType roadtype)
226 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
227 assert(roadtype < ROADTYPE_END);
228 return &_roadtypes[roadtype];
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.
239 static inline bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
241 return HasBit(GetRoadTypeInfo(enginetype)->powered_roadtypes, tiletype);
245 * Returns the cost of building the specified roadtype.
246 * @param roadtype The roadtype being built.
247 * @return The cost multiplier.
249 static inline Money RoadBuildCost(RoadType roadtype)
251 assert(roadtype < ROADTYPE_END);
252 return (_price[PR_BUILD_ROAD] * GetRoadTypeInfo(roadtype)->cost_multiplier) >> 3;
256 * Returns the cost of clearing the specified roadtype.
257 * @param roadtype The roadtype being removed.
258 * @return The cost.
260 static inline Money RoadClearCost(RoadType roadtype)
262 assert(roadtype < ROADTYPE_END);
264 /* Flat fee for removing road. */
265 if (RoadTypeIsRoad(roadtype)) return _price[PR_CLEAR_ROAD];
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;
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
278 static inline Money RoadConvertCost(RoadType from, RoadType to)
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;
283 /* Same cost as removing and then building. */
284 return RoadBuildCost(to) + RoadClearCost(from);
288 * Test if road disallows level crossings
289 * @param roadtype The roadtype we are testing
290 * @return True iff the roadtype disallows level crossings
292 static inline bool RoadNoLevelCrossing(RoadType roadtype)
294 assert(roadtype < ROADTYPE_END);
295 return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_NO_LEVEL_CROSSING);
298 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels = true);
300 void ResetRoadTypes();
301 void InitRoadTypes();
302 RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt);
303 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt);
305 extern std::vector<RoadType> _sorted_roadtypes;
306 extern RoadTypes _roadtypes_hidden_mask;
309 * Loop header for iterating over roadtypes, sorted by sortorder.
310 * @param var Roadtype.
312 #define FOR_ALL_SORTED_ROADTYPES(var) for (uint8 index = 0; index < _sorted_roadtypes.size() && (var = _sorted_roadtypes[index], true) ; index++)
314 #endif /* ROAD_H */