OpenTTD Source: src/road.h Source File

OpenTTD Source 14.0-beta3

road.h
Go to the documentation of this file.
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
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 "timer/timer_game_calendar.h"
18 #include "core/enum_type.hpp"
19 #include "newgrf.h"
20 #include "economy_func.h"
21
22
23 enum RoadTramType : bool {
24 RTT_ROAD,
25 RTT_TRAM,
26 };
27
28 enum RoadTramTypes : uint8_t {
29 RTTB_ROAD = 1 << RTT_ROAD,
30 RTTB_TRAM = 1 << RTT_TRAM,
31 };
32 DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
33
34 static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
35
37 enum RoadTypeFlag {
38 ROTF_CATENARY = 0,
39 ROTF_NO_LEVEL_CROSSING,
40 ROTF_NO_HOUSES,
41 ROTF_HIDDEN,
42 ROTF_TOWN_BUILD,
43 };
44
46 enum RoadTypeFlags : uint8_t {
47 ROTFB_NONE = 0,
48 ROTFB_CATENARY = 1 << ROTF_CATENARY,
49 ROTFB_NO_LEVEL_CROSSING = 1 << ROTF_NO_LEVEL_CROSSING,
50 ROTFB_NO_HOUSES = 1 << ROTF_NO_HOUSES,
51 ROTFB_HIDDEN = 1 << ROTF_HIDDEN,
52 ROTFB_TOWN_BUILD = 1 << ROTF_TOWN_BUILD,
53 };
54 DECLARE_ENUM_AS_BIT_SET(RoadTypeFlags)
55
56 struct SpriteGroup;
57
59 enum RoadTypeSpriteGroup {
60 ROTSG_CURSORS,
61 ROTSG_OVERLAY,
62 ROTSG_GROUND,
63 ROTSG_TUNNEL,
64 ROTSG_CATENARY_FRONT,
65 ROTSG_CATENARY_BACK,
66 ROTSG_BRIDGE,
67 ROTSG_reserved2,
68 ROTSG_DEPOT,
69 ROTSG_reserved3,
70 ROTSG_ROADSTOP,
71 ROTSG_ONEWAY,
72 ROTSG_END,
73 };
74
76 typedef std::vector<RoadTypeLabel> RoadTypeLabelList;
77
78 class RoadTypeInfo {
79 public:
84 struct {
85 SpriteID build_x_road;
86 SpriteID build_y_road;
87 SpriteID auto_road;
88 SpriteID build_depot;
89 SpriteID build_tunnel;
90 SpriteID convert_road;
91 } gui_sprites;
92
93 struct {
94 CursorID road_swne;
95 CursorID road_nwse;
96 CursorID autoroad;
97 CursorID depot;
98 CursorID tunnel;
99 SpriteID convert_road;
100 } cursor;
101
102 struct {
103 StringID name;
104 StringID toolbar_caption;
105 StringID menu_text;
106 StringID build_caption;
107 StringID replace_text;
108 StringID new_engine;
109
110 StringID err_build_road;
111 StringID err_remove_road;
112 StringID err_depot;
113 StringID err_build_station[2];
114 StringID err_remove_station[2];
115 StringID err_convert_road;
116
117 StringID picker_title[2];
118 StringID picker_tooltip[2];
119 } strings;
120
122 RoadTypes powered_roadtypes;
123
127 RoadTypeFlags flags;
128
132 uint16_t cost_multiplier;
133
137 uint16_t maintenance_multiplier;
138
142 uint16_t max_speed;
143
147 RoadTypeLabel label;
148
152 RoadTypeLabelList alternate_labels;
153
157 byte map_colour;
158
166 TimerGameCalendar::Date introduction_date;
167
172 RoadTypes introduction_required_roadtypes;
173
177 RoadTypes introduces_roadtypes;
178
182 byte sorting_order;
183
187 const GRFFile *grffile[ROTSG_END];
188
192 const SpriteGroup *group[ROTSG_END];
193
194 inline bool UsesOverlay() const
195 {
196 return this->group[ROTSG_GROUND] != nullptr;
197 }
198 };
199
200 extern RoadTypes _roadtypes_type;
201
202 inline bool RoadTypeIsRoad(RoadType roadtype)
203 {
204 return !HasBit(_roadtypes_type, roadtype);
205 }
206
207 inline bool RoadTypeIsTram(RoadType roadtype)
208 {
209 return HasBit(_roadtypes_type, roadtype);
210 }
211
212 inline RoadTramType GetRoadTramType(RoadType roadtype)
213 {
214 return RoadTypeIsTram(roadtype) ? RTT_TRAM : RTT_ROAD;
215 }
216
217 inline RoadTramType OtherRoadTramType(RoadTramType rtt)
218 {
219 return rtt == RTT_ROAD ? RTT_TRAM : RTT_ROAD;
220 }
221
227 inline const RoadTypeInfo *GetRoadTypeInfo(RoadType roadtype)
228 {
229 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
230 assert(roadtype < ROADTYPE_END);
231 return &_roadtypes[roadtype];
232 }
233
242 inline bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
243 {
244 return HasBit(GetRoadTypeInfo(enginetype)->powered_roadtypes, tiletype);
245 }
246
252 inline Money RoadBuildCost(RoadType roadtype)
253 {
254 assert(roadtype < ROADTYPE_END);
255 return (_price[PR_BUILD_ROAD] * GetRoadTypeInfo(roadtype)->cost_multiplier) >> 3;
256 }
257
263 inline Money RoadClearCost(RoadType roadtype)
264 {
265 assert(roadtype < ROADTYPE_END);
266
267 /* Flat fee for removing road. */
268 if (RoadTypeIsRoad(roadtype)) return _price[PR_CLEAR_ROAD];
269
270 /* Clearing tram earns a little money, but also incurs the standard clear road cost,
271 * so no profit can be made. */
272 return _price[PR_CLEAR_ROAD] - RoadBuildCost(roadtype) * 3 / 4;
273 }
274
281 inline Money RoadConvertCost(RoadType from, RoadType to)
282 {
283 /* Don't apply convert costs when converting to the same roadtype (ex. building a roadstop over existing road) */
284 if (from == to) return (Money)0;
285
286 /* Same cost as removing and then building. */
287 return RoadBuildCost(to) + RoadClearCost(from);
288 }
289
295 inline bool RoadNoLevelCrossing(RoadType roadtype)
296 {
297 assert(roadtype < ROADTYPE_END);
298 return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_NO_LEVEL_CROSSING);
299 }
300
301 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels = true);
302
303 void ResetRoadTypes();
304 void InitRoadTypes();
305 RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt);
306 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt);
307
308 extern std::vector<RoadType> _sorted_roadtypes;
309 extern RoadTypes _roadtypes_hidden_mask;
310
311 #endif /* ROAD_H */
RoadTypeInfo::flags
RoadTypeFlags flags
Bit mask of road type flags.
Definition: road.h:127
RoadTypeInfo::new_engine
StringID new_engine
Name of an engine for this type of road in the engine preview GUI.
Definition: road.h:108
RoadTypeInfo::toolbar_caption
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition: road.h:104
ROTSG_ONEWAY
@ ROTSG_ONEWAY
Optional: One-way indicator images.
Definition: road.h:71
RoadTypeInfo
Definition: road.h:78
ROTSG_GROUND
@ ROTSG_GROUND
Required: Main group of ground images.
Definition: road.h:62
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:29
RoadTypeInfo::err_build_road
StringID err_build_road
Building a normal piece of road.
Definition: road.h:110
RoadTypeInfo::menu_text
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition: road.h:105
RoadTypeInfo::build_y_road
SpriteID build_y_road
button for building single rail in Y direction
Definition: road.h:86
ROTSG_reserved2
@ ROTSG_reserved2
Placeholder, if we need specific level crossing sprites.
Definition: road.h:67
RoadTypeInfo::introduction_date
TimerGameCalendar::Date introduction_date
Introduction date.
Definition: road.h:166
ROTFB_HIDDEN
@ ROTFB_HIDDEN
Value for hidden from construction.
Definition: road.h:51
ROTFB_NO_HOUSES
@ ROTFB_NO_HOUSES
Value for for setting this roadtype as not house friendly.
Definition: road.h:50
RoadTypeInfo::introduces_roadtypes
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition: road.h:177
timer_game_calendar.h
RoadTypeInfo::sorting_order
byte sorting_order
The sorting order of this roadtype for the toolbar dropdown.
Definition: road.h:182
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
ROTFB_NONE
@ ROTFB_NONE
All flags cleared.
Definition: road.h:47
ROTF_NO_LEVEL_CROSSING
@ ROTF_NO_LEVEL_CROSSING
Bit number for disabling level crossing.
Definition: road.h:39
RoadTypeInfo::build_depot
SpriteID build_depot
button for building depots
Definition: road.h:88
RoadTypeInfo::err_remove_station
StringID err_remove_station[2]
Removing of a bus or truck station.
Definition: road.h:114
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
economy_func.h
RoadTypeInfo::tunnel
CursorID tunnel
Cursor for building a tunnel.
Definition: road.h:98
RoadTypeInfo::replace_text
StringID replace_text
Text used in the autoreplace GUI.
Definition: road.h:107
strings_type.h
RoadTypeInfo::err_remove_road
StringID err_remove_road
Removing a normal piece of road.
Definition: road.h:111
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
RoadTypeInfo::depot
CursorID depot
Cursor for building a depot.
Definition: road.h:97
ROTSG_CURSORS
@ ROTSG_CURSORS
Optional: Cursor and toolbar icon images.
Definition: road.h:60
RoadTypeInfo::road_nwse
CursorID road_nwse
Cursor for building rail in Y direction.
Definition: road.h:95
RoadTypeInfo::road_swne
CursorID road_swne
Cursor for building rail in X direction.
Definition: road.h:94
RoadTypeLabelList
std::vector< RoadTypeLabel > RoadTypeLabelList
List of road type labels.
Definition: road.h:76
AllocateRoadType
RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
Allocate a new road type label.
Definition: road_cmd.cpp:134
HasPowerOnRoad
bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
Checks if an engine of the given RoadType got power on a tile with a given RoadType.
Definition: road.h:242
ROTF_CATENARY
@ ROTF_CATENARY
Bit number for adding catenary.
Definition: road.h:38
RoadBuildCost
Money RoadBuildCost(RoadType roadtype)
Returns the cost of building the specified roadtype.
Definition: road.h:252
bitmath_func.hpp
RoadTypeSpriteGroup
RoadTypeSpriteGroup
Sprite groups for a roadtype.
Definition: road.h:59
RoadTypeInfo::powered_roadtypes
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition: road.h:122
_roadtypes_type
RoadTypes _roadtypes_type
Bitmap of road/tram types.
Definition: road_cmd.cpp:62
RoadNoLevelCrossing
bool RoadNoLevelCrossing(RoadType roadtype)
Test if road disallows level crossings.
Definition: road.h:295
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:38
RoadTypeInfo::maintenance_multiplier
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this road type.
Definition: road.h:137
ROTFB_TOWN_BUILD
@ ROTFB_TOWN_BUILD
Value for allowing towns to build this roadtype.
Definition: road.h:52
HasAnyRoadTypesAvail
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition: road.cpp:143
RoadTypeInfo::auto_road
SpriteID auto_road
button for the autoroad construction
Definition: road.h:87
ROTFB_CATENARY
@ ROTFB_CATENARY
Value for drawing a catenary.
Definition: road.h:48
RoadTypeInfo::gui_sprites
struct RoadTypeInfo::@27 gui_sprites
struct containing the sprites for the road GUI.
RoadTypeInfo::group
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition: road.h:192
RoadTypeInfo::alternate_labels
RoadTypeLabelList alternate_labels
Road type labels this type provides in addition to the main label.
Definition: road.h:152
ROTSG_ROADSTOP
@ ROTSG_ROADSTOP
Required: Bay stop surface.
Definition: road.h:70
CursorID
uint32_t CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:19
ROTSG_BRIDGE
@ ROTSG_BRIDGE
Required: Bridge surface images.
Definition: road.h:66
RoadTypeInfo::name
StringID name
Name of this rail type.
Definition: road.h:103
RoadTypeInfo::cost_multiplier
uint16_t cost_multiplier
Cost multiplier for building this road type.
Definition: road.h:132
RoadTypeInfo::build_tunnel
SpriteID build_tunnel
button for building a tunnel
Definition: road.h:89
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
RoadClearCost
Money RoadClearCost(RoadType roadtype)
Returns the cost of clearing the specified roadtype.
Definition: road.h:263
RoadTypeInfo::cursor
struct RoadTypeInfo::@28 cursor
Cursors associated with the road type.
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:227
RoadTypeInfo::picker_tooltip
StringID picker_tooltip[2]
Tooltip for the station picker for bus or truck stations.
Definition: road.h:118
RoadTypeInfo::build_x_road
SpriteID build_x_road
button for building single rail in X direction
Definition: road.h:85
RoadTypeInfo::convert_road
SpriteID convert_road
button for converting road types
Definition: road.h:90
RoadTypeInfo::grffile
const GRFFile * grffile[ROTSG_END]
NewGRF providing the Action3 for the roadtype.
Definition: road.h:187
RoadTypeInfo::label
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:147
RoadTypeInfo::err_build_station
StringID err_build_station[2]
Building a bus or truck station.
Definition: road.h:113
ROTSG_OVERLAY
@ ROTSG_OVERLAY
Optional: Images for overlaying track.
Definition: road.h:61
RoadTypeInfo::autoroad
CursorID autoroad
Cursor for autorail tool.
Definition: road.h:96
RoadTypeInfo::err_convert_road
StringID err_convert_road
Converting a road type.
Definition: road.h:115
newgrf.h
RoadTypeInfo::map_colour
byte map_colour
Colour on mini-map.
Definition: road.h:157
ROTSG_CATENARY_FRONT
@ ROTSG_CATENARY_FRONT
Optional: Catenary front.
Definition: road.h:64
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:25
RoadTypeFlags
RoadTypeFlags
Roadtype flags.
Definition: road.h:46
RoadTypeInfo::strings
struct RoadTypeInfo::@29 strings
Strings associated with the rail type.
ROTF_TOWN_BUILD
@ ROTF_TOWN_BUILD
Bit number for allowing towns to build this roadtype.
Definition: road.h:42
enum_type.hpp
ROTSG_TUNNEL
@ ROTSG_TUNNEL
Optional: Ground images for tunnels.
Definition: road.h:63
ROTF_HIDDEN
@ ROTF_HIDDEN
Bit number for hidden from construction.
Definition: road.h:41
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
RoadTypeInfo::max_speed
uint16_t max_speed
Maximum speed for vehicles travelling on this road type.
Definition: road.h:142
ROTSG_DEPOT
@ ROTSG_DEPOT
Optional: Depot images.
Definition: road.h:68
ROTF_NO_HOUSES
@ ROTF_NO_HOUSES
Bit number for setting this roadtype as not house friendly.
Definition: road.h:40
road_type.h
OverflowSafeInt< int64_t >
GetRoadTypeByLabel
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels=true)
Get the road type for a given label.
Definition: road.cpp:254
ResetRoadTypes
void ResetRoadTypes()
Reset all road type information to its default values.
Definition: road_cmd.cpp:67
RoadTypeFlag
RoadTypeFlag
Roadtype flag bit numbers.
Definition: road.h:37
ROTFB_NO_LEVEL_CROSSING
@ ROTFB_NO_LEVEL_CROSSING
Value for disabling a level crossing.
Definition: road.h:49
RoadTypeInfo::err_depot
StringID err_depot
Building a depot.
Definition: road.h:112
gfx_type.h
InitRoadTypes
void InitRoadTypes()
Resolve sprites of custom road types.
Definition: road_cmd.cpp:114
RoadTypeInfo::build_caption
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition: road.h:106
ROTSG_CATENARY_BACK
@ ROTSG_CATENARY_BACK
Optional: Catenary back.
Definition: road.h:65
RoadTypeInfo::introduction_required_roadtypes
RoadTypes introduction_required_roadtypes
Bitmask of roadtypes that are required for this roadtype to be introduced at a given introduction_dat...
Definition: road.h:172
SpriteGroup
Definition: newgrf_spritegroup.h:57
RoadTypeInfo::picker_title
StringID picker_title[2]
Title for the station picker for bus or truck stations.
Definition: road.h:117
ROTSG_reserved3
@ ROTSG_reserved3
Placeholder, if we add road fences (for highways).
Definition: road.h:69
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
RoadConvertCost
Money RoadConvertCost(RoadType from, RoadType to)
Calculates the cost of road conversion.
Definition: road.h:281
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103