OpenTTD Source 20250924-master-gbec4e71d53
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"
18#include "core/enum_type.hpp"
19#include "newgrf.h"
20#include "newgrf_badge_type.h"
21#include "economy_func.h"
22
23
24enum RoadTramType : bool {
25 RTT_ROAD,
26 RTT_TRAM,
27};
28
29enum RoadTramTypes : uint8_t {
30 RTTB_ROAD = 1 << RTT_ROAD,
31 RTTB_TRAM = 1 << RTT_TRAM,
32};
33DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
34
35static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
36
38enum class RoadTypeFlag : uint8_t {
39 Catenary = 0,
40 NoLevelCrossing = 1,
41 NoHouses = 2,
42 Hidden = 3,
43 TownBuild = 4,
44};
46
47struct SpriteGroup;
48
65
189
195inline RoadTypes GetMaskForRoadTramType(RoadTramType rtt)
196{
199 return rtt == RTT_ROAD ? _roadtypes_road : _roadtypes_tram;
200}
201
202inline bool RoadTypeIsRoad(RoadType roadtype)
203{
204 return GetMaskForRoadTramType(RTT_ROAD).Test(roadtype);
205}
206
207inline bool RoadTypeIsTram(RoadType roadtype)
208{
209 return GetMaskForRoadTramType(RTT_TRAM).Test(roadtype);
210}
211
212inline RoadTramType GetRoadTramType(RoadType roadtype)
213{
214 return RoadTypeIsTram(roadtype) ? RTT_TRAM : RTT_ROAD;
215}
216
217inline RoadTramType OtherRoadTramType(RoadTramType rtt)
218{
219 return rtt == RTT_ROAD ? RTT_TRAM : RTT_ROAD;
220}
221
227inline const RoadTypeInfo *GetRoadTypeInfo(RoadType roadtype)
228{
229 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
230 assert(roadtype < ROADTYPE_END);
231 return &_roadtypes[roadtype];
232}
233
240{
241 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
242 size_t index = rti - _roadtypes;
243 assert(index < ROADTYPE_END && rti == _roadtypes + index);
244 return static_cast<RoadType>(index);
245}
246
255inline bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
256{
257 return GetRoadTypeInfo(enginetype)->powered_roadtypes.Test(tiletype);
258}
259
266{
267 assert(roadtype < ROADTYPE_END);
268 return (_price[PR_BUILD_ROAD] * GetRoadTypeInfo(roadtype)->cost_multiplier) >> 3;
269}
270
277{
278 assert(roadtype < ROADTYPE_END);
279
280 /* Flat fee for removing road. */
281 if (RoadTypeIsRoad(roadtype)) return _price[PR_CLEAR_ROAD];
282
283 /* Clearing tram earns a little money, but also incurs the standard clear road cost,
284 * so no profit can be made. */
285 return _price[PR_CLEAR_ROAD] - RoadBuildCost(roadtype) * 3 / 4;
286}
287
295{
296 /* Don't apply convert costs when converting to the same roadtype (ex. building a roadstop over existing road) */
297 if (from == to) return (Money)0;
298
299 /* Same cost as removing and then building. */
300 return RoadBuildCost(to) + RoadClearCost(from);
301}
302
308inline bool RoadNoLevelCrossing(RoadType roadtype)
309{
310 assert(roadtype < ROADTYPE_END);
312}
313
314RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels = true);
315
316void ResetRoadTypes();
317void InitRoadTypes();
318RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt);
319bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt);
320
321extern std::vector<RoadType> _sorted_roadtypes;
323
324#endif /* ROAD_H */
Functions related to bit mathematics.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition road.h:135
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition road.h:93
StringID replace_text
Text used in the autoreplace GUI.
Definition road.h:95
StringID picker_title[2]
Title for the station picker for bus or truck stations.
Definition road.h:105
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition road.h:110
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition road.h:165
CursorID autoroad
Cursor for autorail tool.
Definition road.h:84
TimerGameCalendar::Date introduction_date
Introduction date.
Definition road.h:154
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition road.h:180
uint8_t sorting_order
The sorting order of this roadtype for the toolbar dropdown.
Definition road.h:170
StringID picker_tooltip[2]
Tooltip for the station picker for bus or truck stations.
Definition road.h:106
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this road type.
Definition road.h:125
RoadTypeFlags flags
Bit mask of road type flags.
Definition road.h:115
StringID err_build_road
Building a normal piece of road.
Definition road.h:98
StringID err_remove_road
Removing a normal piece of road.
Definition road.h:99
CursorID depot
Cursor for building a depot.
Definition road.h:85
std::vector< RoadTypeLabel > alternate_labels
Road type labels this type provides in addition to the main label.
Definition road.h:140
const GRFFile * grffile[ROTSG_END]
NewGRF providing the Action3 for the roadtype.
Definition road.h:175
CursorID road_nwse
Cursor for building rail in Y direction.
Definition road.h:83
uint16_t max_speed
Maximum speed for vehicles travelling on this road type.
Definition road.h:130
struct RoadTypeInfo::@24 gui_sprites
struct containing the sprites for the road GUI.
StringID name
Name of this rail type.
Definition road.h:91
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition road.h:92
struct RoadTypeInfo::@25 cursor
Cursors associated with the road type.
SpriteID build_y_road
button for building single rail in Y direction
Definition road.h:74
CursorID tunnel
Cursor for building a tunnel.
Definition road.h:86
SpriteID auto_road
button for the autoroad construction
Definition road.h:75
SpriteID convert_road
button for converting road types
Definition road.h:78
CursorID road_swne
Cursor for building rail in X direction.
Definition road.h:82
StringID err_convert_road
Converting a road type.
Definition road.h:103
StringID new_engine
Name of an engine for this type of road in the engine preview GUI.
Definition road.h:96
StringID err_depot
Building a depot.
Definition road.h:100
SpriteID build_x_road
button for building single rail in X direction
Definition road.h:73
struct RoadTypeInfo::@26 strings
Strings associated with the rail type.
SpriteID build_depot
button for building depots
Definition road.h:76
RoadTypes introduction_required_roadtypes
Bitmask of roadtypes that are required for this roadtype to be introduced at a given introduction_dat...
Definition road.h:160
SpriteID build_tunnel
button for building a tunnel
Definition road.h:77
StringID err_build_station[2]
Building a bus or truck station.
Definition road.h:101
uint16_t cost_multiplier
Cost multiplier for building this road type.
Definition road.h:120
PixelColour map_colour
Colour on mini-map.
Definition road.h:145
StringID err_remove_station[2]
Removing of a bus or truck station.
Definition road.h:102
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition road.h:94
Functions related to the economy.
Type (helpers) for enums.
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
uint32_t CursorID
The number of the cursor (sprite)
Definition gfx_type.h:19
Base for the NewGRF implementation.
Types related to NewGRF badges.
void ResetRoadTypes()
Reset all road type information to its default values.
Definition road_cmd.cpp:63
void InitRoadTypes()
Resolve sprites of custom road types.
Definition road_cmd.cpp:111
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:255
RoadType GetRoadTypeInfoIndex(const RoadTypeInfo *rti)
Returns the railtype for a Railtype information.
Definition road.h:239
RoadTypeFlag
Roadtype flag bit numbers.
Definition road.h:38
@ Catenary
Bit number for adding catenary.
@ NoHouses
Bit number for setting this roadtype as not house friendly.
@ Hidden
Bit number for hidden from construction.
@ NoLevelCrossing
Bit number for disabling level crossing.
@ TownBuild
Bit number for allowing towns to build this roadtype.
RoadTypes _roadtypes_hidden_mask
Bitset of hidden roadtypes.
Definition road_cmd.cpp:56
Money RoadClearCost(RoadType roadtype)
Returns the cost of clearing the specified roadtype.
Definition road.h:276
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition road.cpp:140
RoadTypes GetMaskForRoadTramType(RoadTramType rtt)
Get the mask for road types of the given RoadTramType.
Definition road.h:195
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:227
Money RoadConvertCost(RoadType from, RoadType to)
Calculates the cost of road conversion.
Definition road.h:294
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels=true)
Get the road type for a given label.
Definition road.cpp:253
std::vector< RoadType > _sorted_roadtypes
Sorted list of road types.
Definition road_cmd.cpp:55
RoadTypeSpriteGroup
Sprite groups for a roadtype.
Definition road.h:50
@ ROTSG_ONEWAY
Optional: One-way indicator images.
Definition road.h:62
@ ROTSG_OVERLAY
Optional: Images for overlaying track.
Definition road.h:52
@ ROTSG_reserved2
Placeholder, if we need specific level crossing sprites.
Definition road.h:58
@ ROTSG_ROADSTOP
Required: Bay stop surface.
Definition road.h:61
@ ROTSG_GROUND
Required: Main group of ground images.
Definition road.h:53
@ ROTSG_DEPOT
Optional: Depot images.
Definition road.h:59
@ ROTSG_CURSORS
Optional: Cursor and toolbar icon images.
Definition road.h:51
@ ROTSG_CATENARY_BACK
Optional: Catenary back.
Definition road.h:56
@ ROTSG_BRIDGE
Required: Bridge surface images.
Definition road.h:57
@ ROTSG_reserved3
Placeholder, if we add road fences (for highways).
Definition road.h:60
@ ROTSG_CATENARY_FRONT
Optional: Catenary front.
Definition road.h:55
@ ROTSG_TUNNEL
Optional: Ground images for tunnels.
Definition road.h:54
bool RoadNoLevelCrossing(RoadType roadtype)
Test if road disallows level crossings.
Definition road.h:308
RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
Allocate a new road type label.
Definition road_cmd.cpp:130
Money RoadBuildCost(RoadType roadtype)
Returns the cost of building the specified roadtype.
Definition road.h:265
RoadTypes _roadtypes_road
Bitset of road roadtypes.
Definition road_cmd.cpp:57
RoadTypes _roadtypes_tram
Bitset of tram roadtypes.
Definition road_cmd.cpp:58
Enums and other types related to roads.
RoadType
The different roadtypes we support.
Definition road_type.h:23
@ ROADTYPE_END
Used for iterations.
Definition road_type.h:27
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
Colour for pixel/line drawing.
Definition gfx_type.h:405
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition of the game-calendar-timer.