OpenTTD Source  12.0-beta2
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 "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 
44 
45  ROTFB_NONE = 0,
51 };
53 
54 struct SpriteGroup;
55 
69  ROTSG_END,
70 };
71 
73 typedef std::vector<RoadTypeLabel> RoadTypeLabelList;
74 
75 class RoadTypeInfo {
76 public:
81  struct {
88  } gui_sprites;
89 
90  struct {
97  } cursor;
98 
99  struct {
106 
113 
116  } strings;
117 
120 
125 
130 
135 
139  uint16 max_speed;
140 
144  RoadTypeLabel label;
145 
150 
155 
164 
170 
175 
180 
184  const GRFFile *grffile[ROTSG_END];
185 
189  const SpriteGroup *group[ROTSG_END];
190 
191  inline bool UsesOverlay() const
192  {
193  return this->group[ROTSG_GROUND] != nullptr;
194  }
195 };
196 
198 
199 static inline bool RoadTypeIsRoad(RoadType roadtype)
200 {
201  return !HasBit(_roadtypes_type, roadtype);
202 }
203 
204 static inline bool RoadTypeIsTram(RoadType roadtype)
205 {
206  return HasBit(_roadtypes_type, roadtype);
207 }
208 
209 static inline RoadTramType GetRoadTramType(RoadType roadtype)
210 {
211  return RoadTypeIsTram(roadtype) ? RTT_TRAM : RTT_ROAD;
212 }
213 
214 static inline RoadTramType OtherRoadTramType(RoadTramType rtt)
215 {
216  return rtt == RTT_ROAD ? RTT_TRAM : RTT_ROAD;
217 }
218 
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 
239 static inline bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
240 {
241  return HasBit(GetRoadTypeInfo(enginetype)->powered_roadtypes, tiletype);
242 }
243 
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 
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 
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 
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 */
RoadTypeInfo::flags
RoadTypeFlags flags
Bit mask of road type flags.
Definition: road.h:124
RoadTypeInfo::new_engine
StringID new_engine
Name of an engine for this type of road in the engine preview GUI.
Definition: road.h:105
ROTF_NO_LEVEL_CROSSING
@ ROTF_NO_LEVEL_CROSSING
Bit number for disabling level crossing.
Definition: road.h:40
RoadTypeInfo::toolbar_caption
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition: road.h:101
RoadTypeInfo
Definition: road.h:75
ROTSG_GROUND
@ ROTSG_GROUND
Required: Main group of ground images.
Definition: road.h:60
RoadTypeInfo::err_build_road
StringID err_build_road
Building a normal piece of road.
Definition: road.h:107
RoadTypeInfo::menu_text
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition: road.h:102
ROTF_CATENARY
@ ROTF_CATENARY
Bit number for adding catenary.
Definition: road.h:39
ROTFB_TOWN_BUILD
@ ROTFB_TOWN_BUILD
Value for allowing towns to build this roadtype.
Definition: road.h:50
RoadTypeInfo::build_y_road
SpriteID build_y_road
button for building single rail in Y direction
Definition: road.h:83
ROTSG_reserved2
@ ROTSG_reserved2
Placeholder, if we need specific level crossing sprites.
Definition: road.h:65
RoadTypeInfo::introduces_roadtypes
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition: road.h:174
ROTF_TOWN_BUILD
@ ROTF_TOWN_BUILD
Bit number for allowing towns to build this roadtype.
Definition: road.h:43
RoadTypeInfo::sorting_order
byte sorting_order
The sorting order of this roadtype for the toolbar dropdown.
Definition: road.h:179
RoadConvertCost
static Money RoadConvertCost(RoadType from, RoadType to)
Calculates the cost of road conversion.
Definition: road.h:278
RoadTypeInfo::build_depot
SpriteID build_depot
button for building depots
Definition: road.h:85
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
RoadTypeInfo::err_remove_station
StringID err_remove_station[2]
Removing of a bus or truck station.
Definition: road.h:111
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:26
economy_func.h
RoadTypeInfo::tunnel
CursorID tunnel
Cursor for building a tunnel.
Definition: road.h:95
RoadTypeInfo::replace_text
StringID replace_text
Text used in the autoreplace GUI.
Definition: road.h:104
strings_type.h
RoadTypeInfo::introduction_date
Date introduction_date
Introduction date.
Definition: road.h:163
RoadTypeInfo::err_remove_road
StringID err_remove_road
Removing a normal piece of road.
Definition: road.h:108
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
RoadTypeInfo::depot
CursorID depot
Cursor for building a depot.
Definition: road.h:94
ROTSG_CURSORS
@ ROTSG_CURSORS
Optional: Cursor and toolbar icon images.
Definition: road.h:58
RoadTypeInfo::road_nwse
CursorID road_nwse
Cursor for building rail in Y direction.
Definition: road.h:92
RoadTypeInfo::road_swne
CursorID road_swne
Cursor for building rail in X direction.
Definition: road.h:91
RoadTypeLabelList
std::vector< RoadTypeLabel > RoadTypeLabelList
List of road type labels.
Definition: road.h:73
AllocateRoadType
RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
Allocate a new road type label.
Definition: road_cmd.cpp:138
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
bitmath_func.hpp
RoadTypeSpriteGroup
RoadTypeSpriteGroup
Sprite groups for a roadtype.
Definition: road.h:57
RoadTypeInfo::powered_roadtypes
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition: road.h:119
_roadtypes_type
RoadTypes _roadtypes_type
Bitmap of road/tram types.
Definition: road_cmd.cpp:57
CursorID
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:19
ROTF_HIDDEN
@ ROTF_HIDDEN
Bit number for hidden from construction.
Definition: road.h:42
HasAnyRoadTypesAvail
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition: road.cpp:132
RoadTypeInfo::auto_road
SpriteID auto_road
button for the autoroad construction
Definition: road.h:84
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
RoadTypeInfo::group
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition: road.h:189
RoadTypeInfo::alternate_labels
RoadTypeLabelList alternate_labels
Road type labels this type provides in addition to the main label.
Definition: road.h:149
ROTSG_ROADSTOP
@ ROTSG_ROADSTOP
Required: Drive-in stop surface.
Definition: road.h:68
ROTF_NO_HOUSES
@ ROTF_NO_HOUSES
Bit number for setting this roadtype as not house friendly.
Definition: road.h:41
ROTSG_BRIDGE
@ ROTSG_BRIDGE
Required: Bridge surface images.
Definition: road.h:64
ROTFB_CATENARY
@ ROTFB_CATENARY
Value for drawing a catenary.
Definition: road.h:46
RoadTypeInfo::name
StringID name
Name of this rail type.
Definition: road.h:100
RoadTypeInfo::build_tunnel
SpriteID build_tunnel
button for building a tunnel
Definition: road.h:86
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:22
date_type.h
RoadTypeInfo::picker_tooltip
StringID picker_tooltip[2]
Tooltip for the station picker for bus or truck stations.
Definition: road.h:115
ROTFB_NO_HOUSES
@ ROTFB_NO_HOUSES
Value for for setting this roadtype as not house friendly.
Definition: road.h:48
RoadTypeInfo::build_x_road
SpriteID build_x_road
button for building single rail in X direction
Definition: road.h:82
RoadTypeInfo::convert_road
SpriteID convert_road
button for converting road types
Definition: road.h:87
RoadTypeInfo::grffile
const GRFFile * grffile[ROTSG_END]
NewGRF providing the Action3 for the roadtype.
Definition: road.h:184
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
RoadTypeInfo::gui_sprites
struct RoadTypeInfo::@40 gui_sprites
struct containing the sprites for the road GUI.
RoadTypeInfo::label
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:144
RoadTypeInfo::err_build_station
StringID err_build_station[2]
Building a bus or truck station.
Definition: road.h:110
RoadTypeInfo::cursor
struct RoadTypeInfo::@41 cursor
Cursors associated with the road type.
ROTSG_OVERLAY
@ ROTSG_OVERLAY
Optional: Images for overlaying track.
Definition: road.h:59
RoadTypeInfo::autoroad
CursorID autoroad
Cursor for autorail tool.
Definition: road.h:93
RoadClearCost
static Money RoadClearCost(RoadType roadtype)
Returns the cost of clearing the specified roadtype.
Definition: road.h:260
RoadTypeInfo::err_convert_road
StringID err_convert_road
Converting a road type.
Definition: road.h:112
newgrf.h
RoadTypeInfo::map_colour
byte map_colour
Colour on mini-map.
Definition: road.h:154
ROTSG_CATENARY_FRONT
@ ROTSG_CATENARY_FRONT
Optional: Catenary front.
Definition: road.h:62
GetRoadTypeInfo
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:224
enum_type.hpp
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::cost_multiplier
uint16 cost_multiplier
Cost multiplier for building this road type.
Definition: road.h:129
ROTSG_DEPOT
@ ROTSG_DEPOT
Optional: Depot images.
Definition: road.h:66
RoadTypeInfo::max_speed
uint16 max_speed
Maximum speed for vehicles travelling on this road type.
Definition: road.h:139
road_type.h
OverflowSafeInt< int64 >
RoadBuildCost
static Money RoadBuildCost(RoadType roadtype)
Returns the cost of building the specified roadtype.
Definition: road.h:249
HasPowerOnRoad
static 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:239
RoadTypeFlags
RoadTypeFlags
Roadtype flags.
Definition: road.h:38
GetRoadTypeByLabel
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels=true)
Get the road type for a given label.
Definition: road.cpp:243
ResetRoadTypes
void ResetRoadTypes()
Reset all road type information to its default values.
Definition: road_cmd.cpp:62
ROTFB_NONE
@ ROTFB_NONE
All flags cleared.
Definition: road.h:45
ROTSG_reserved1
@ ROTSG_reserved1
Placeholder, if we need specific tunnel sprites.
Definition: road.h:61
RoadTypeInfo::err_depot
StringID err_depot
Building a depot.
Definition: road.h:109
RoadNoLevelCrossing
static bool RoadNoLevelCrossing(RoadType roadtype)
Test if road disallows level crossings.
Definition: road.h:292
gfx_type.h
InitRoadTypes
void InitRoadTypes()
Resolve sprites of custom road types.
Definition: road_cmd.cpp:118
RoadTypeInfo::build_caption
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition: road.h:103
ROTSG_CATENARY_BACK
@ ROTSG_CATENARY_BACK
Optional: Catenary back.
Definition: road.h:63
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:169
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:114
ROTSG_reserved3
@ ROTSG_reserved3
Placeholder, if we add road fences (for highways).
Definition: road.h:67
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
RoadTypeInfo::maintenance_multiplier
uint16 maintenance_multiplier
Cost multiplier for maintenance of this road type.
Definition: road.h:134
ROTFB_NO_LEVEL_CROSSING
@ ROTFB_NO_LEVEL_CROSSING
Value for disabling a level crossing.
Definition: road.h:47
RoadTypeInfo::strings
struct RoadTypeInfo::@42 strings
Strings associated with the rail type.
ROTFB_HIDDEN
@ ROTFB_HIDDEN
Value for hidden from construction.
Definition: road.h:49