OpenTTD Source 20261112-master-gbcbec5e63f
rail.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef RAIL_H
11#define RAIL_H
12
13#include "rail_type.h"
14#include "track_type.h"
15#include "gfx_type.h"
16#include "core/enum_type.hpp"
17#include "core/flatset_type.hpp"
18#include "economy_func.h"
19#include "slope_type.h"
20#include "strings_type.h"
22#include "signal_type.h"
23#include "settings_type.h"
24#include "newgrf_badge_type.h"
25
35
38
39struct SpriteGroup;
40
58
81
88constexpr SpriteID operator+(SpriteID sprite, RailTrackOffset offset) { return sprite + to_underlying(offset); }
89
93enum class RailTrackBridgeOffset : uint8_t {
94 X,
95 Y,
97};
98
105constexpr SpriteID operator+(SpriteID sprite, RailTrackBridgeOffset offset) { return sprite + to_underlying(offset); }
106
130
135public:
140 struct {
155
160 struct {
171
172 struct {
182
183 struct {
191
194
197
200
205
210
214 uint8_t curve_speed;
215
220
225
230
235
239 uint16_t max_speed;
240
244 RailTypeLabel label;
245
250
255
264
270
275
280
285
290
291 std::vector<BadgeID> badges;
292
293 inline bool UsesOverlay() const
294 {
295 return this->group[RailSpriteType::Ground] != nullptr;
296 }
297
306 inline uint GetRailtypeSpriteOffset() const
307 {
308 return 82 * this->fallback_railtype;
309 }
310
311 RailType Index() const;
312};
313
314
320inline const RailTypeInfo *GetRailTypeInfo(RailType railtype)
321{
322 extern RailTypeInfo _railtypes[RAILTYPE_END];
323 assert(railtype < RAILTYPE_END);
324 return &_railtypes[railtype];
325}
326
333{
334 RailTypes compatible{};
335 for (RailType rt : railtypes) compatible.Set(GetRailTypeInfo(rt)->compatible_railtypes);
336 return compatible;
337}
338
345{
346 RailTypes powered{};
347 for (RailType rt : railtypes) powered.Set(GetRailTypeInfo(rt)->powered_railtypes);
348 return powered;
349}
350
357{
358 RailTypes introduces{};
359 for (RailType rt : railtypes) introduces.Set(GetRailTypeInfo(rt)->introduces_railtypes);
360 return introduces;
361}
362
371inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
372{
373 return GetRailTypeInfo(enginetype)->compatible_railtypes.Test(tiletype);
374}
375
383inline bool IsCompatibleRail(RailTypes enginetype, RailType tiletype)
384{
385 return GetAllCompatibleRailTypes(enginetype).Test(tiletype);
386}
387
396inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
397{
398 return GetRailTypeInfo(enginetype)->powered_railtypes.Test(tiletype);
399}
400
408inline bool HasPowerOnRail(RailTypes enginetype, RailType tiletype)
409{
410 return GetAllPoweredRailTypes(enginetype).Test(tiletype);
411}
412
422
430inline bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def = _settings_game.pf.forbid_90_deg)
431{
432 if (rt1 == INVALID_RAILTYPE || rt2 == INVALID_RAILTYPE) return def;
433
434 const RailTypeInfo *rti1 = GetRailTypeInfo(rt1);
435 const RailTypeInfo *rti2 = GetRailTypeInfo(rt2);
436
437 bool rt1_90deg = rti1->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti1->flags.Test(RailTypeFlag::Allow90Deg) && def);
438 bool rt2_90deg = rti2->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti2->flags.Test(RailTypeFlag::Allow90Deg) && def);
439
440 return rt1_90deg || rt2_90deg;
441}
442
448inline Money RailBuildCost(RailType railtype)
449{
450 assert(railtype < RAILTYPE_END);
451 return (_price[Price::BuildRail] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
452}
453
459inline Money RailClearCost(RailType railtype)
460{
461 /* Clearing rail in fact earns money, but if the build cost is set
462 * very low then a loophole exists where money can be made.
463 * In this case we limit the removal earnings to 3/4s of the build
464 * cost.
465 */
466 assert(railtype < RAILTYPE_END);
467 return std::max(_price[Price::ClearRail], -RailBuildCost(railtype) * 3 / 4);
468}
469
476inline Money RailConvertCost(RailType from, RailType to)
477{
478 /* Get the costs for removing and building anew
479 * A conversion can never be more costly */
480 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
481
482 /* Conversion between somewhat compatible railtypes:
483 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
484 * build costs, if the target type is more expensive (material upgrade costs).
485 * Upgrade can never be more expensive than re-building. */
486 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
487 Money upgradecost = RailBuildCost(to) / 8 + std::max(Money{}, RailBuildCost(to) - RailBuildCost(from));
488 return std::min(upgradecost, rebuildcost);
489 }
490
491 /* make the price the same as remove + build new type for rail types
492 * which are not compatible in any way */
493 return rebuildcost;
494}
495
503inline Money RailMaintenanceCost(RailType railtype, uint32_t num, uint32_t total_num)
504{
505 assert(railtype < RAILTYPE_END);
506 return (_price[Price::InfrastructureRail] * GetRailTypeInfo(railtype)->maintenance_multiplier * num * (1 + IntSqrt(total_num))) >> 11; // 4 bits fraction for the multiplier and 7 bits scaling.
507}
508
514inline Money SignalMaintenanceCost(uint32_t num)
515{
516 return (_price[Price::InfrastructureRail] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
517}
518
519void DrawTrainDepotSprite(int x, int y, DiagDirection dir, RailType railtype);
520int TicksToLeaveDepot(const Train *v);
521
523
524
525bool HasRailTypeAvail(const CompanyID company, const RailType railtype);
526bool HasAnyRailTypesAvail(const CompanyID company);
527bool ValParamRailType(const RailType rail);
528
530
531RailTypes GetCompanyRailTypes(CompanyID company, bool introduces = true);
532RailTypes GetRailTypes(bool introduces);
533
534RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
535
536void ResetRailTypes();
537void InitRailTypes();
538RailType AllocateRailType(RailTypeLabel label);
539
540extern std::vector<RailType> _sorted_railtypes;
541extern RailTypes _railtypes_hidden_mask;
542
543#endif /* RAIL_H */
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
Enum-as-bit-set wrapper.
Flat set implementation that uses a sorted vector for storage.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:134
RailType Index() const
Get the RailType for this RailTypeInfo.
Definition rail.cpp:26
SpriteID single_x
single piece of rail in X direction, without ground
Definition rail.h:144
uint16_t max_speed
Maximum speed for vehicles travelling on this rail type.
Definition rail.h:239
SpriteID build_tunnel
button for building a tunnel
Definition rail.h:167
CursorID rail_swne
Cursor for building rail in X direction.
Definition rail.h:174
SpriteID convert_rail
button for converting rail
Definition rail.h:168
struct RailTypeInfo::@157247141350136173143103254227157213063052244122 strings
Strings associated with the rail type.
CursorID convert
Cursor for converting track.
Definition rail.h:180
EnumIndexArray< const SpriteGroup *, RailSpriteType, RailSpriteType::End > group
Sprite groups for resolving sprites.
Definition rail.h:289
CursorID depot
Cursor for building a depot.
Definition rail.h:178
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:263
PixelColour map_colour
Colour on mini-map.
Definition rail.h:254
RailTypes powered_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype generates power
Definition rail.h:196
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:274
SpriteID ground
ground sprite for a 3-way switch
Definition rail.h:143
EnumIndexArray< const GRFFile *, RailSpriteType, RailSpriteType::End > grffile
NewGRF providing the Action3 for the railtype.
Definition rail.h:284
CursorID rail_nwse
Cursor for building rail in Y direction.
Definition rail.h:176
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition rail.h:269
SpriteID build_x_rail
button for building single rail in X direction
Definition rail.h:162
uint8_t sorting_order
The sorting order of this railtype for the toolbar dropdown.
Definition rail.h:279
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:244
SpriteID single_n
single piece of rail in the northern corner
Definition rail.h:146
VehicleAccelerationModel acceleration_type
Acceleration type of this rail type.
Definition rail.h:234
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this rail type.
Definition rail.h:229
SpriteID crossing
level crossing, rail in X direction
Definition rail.h:151
SpriteID bridge_deck
bridge deck sprites base
Definition rail.h:153
CursorID rail_ew
Cursor for building rail in E-W direction.
Definition rail.h:175
SpriteID auto_rail
button for the autorail construction
Definition rail.h:165
CursorID autorail
Cursor for autorail tool.
Definition rail.h:177
SpriteID single_y
single piece of rail in Y direction, without ground
Definition rail.h:145
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition rail.h:186
StringID name
Name of this rail type.
Definition rail.h:184
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel
Definition rail.h:199
uint8_t fallback_railtype
Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
Definition rail.h:209
uint GetRailtypeSpriteOffset() const
Offset between the current railtype and normal rail.
Definition rail.h:306
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition rail.h:185
struct RailTypeInfo::@047376261311064105134233254254216006075341230376 gui_sprites
struct containing the sprites for the rail GUI.
SpriteID single_s
single piece of rail in the southern corner
Definition rail.h:147
RailTypeFlags flags
Bit mask of rail type flags.
Definition rail.h:219
uint8_t curve_speed
Multiplier for curve maximum speed advantage.
Definition rail.h:214
SpriteID build_ew_rail
button for building single rail in E-W direction
Definition rail.h:163
SpriteID build_y_rail
button for building single rail in Y direction
Definition rail.h:164
FlatSet< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:249
uint16_t cost_multiplier
Cost multiplier for building this rail type.
Definition rail.h:224
EnumIndexArray< EnumIndexArray< EnumIndexArray< SpriteID, SignalState, SignalState::End >, SignalVariant, SignalVariant::End >, SignalType, SignalType::End > signals
signal GUI sprites (type, variant, state)
Definition rail.h:169
uint8_t bridge_offset
Bridge offset.
Definition rail.h:204
SpriteID track_ns
two pieces of rail in North and South corner (East-West direction)
Definition rail.h:142
struct RailTypeInfo::@332027037331076264023214171276243307073252216167 base_sprites
Struct containing the main sprites.
StringID replace_text
Text used in the autoreplace GUI.
Definition rail.h:188
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition rail.h:187
SpriteID snow_offset
sprite number difference between a piece of track on a snowy ground and the corresponding one on norm...
Definition rail.h:193
SpriteID track_y
single piece of rail in Y direction, with ground
Definition rail.h:141
SpriteID build_depot
button for building depots
Definition rail.h:166
SpriteID single_w
single piece of rail in the western corner
Definition rail.h:149
SpriteID single_e
single piece of rail in the eastern corner
Definition rail.h:148
SpriteID build_ns_rail
button for building single rail in N-S direction
Definition rail.h:161
CursorID rail_ns
Cursor for building rail in N-S direction.
Definition rail.h:173
StringID new_loco
Name of an engine for this type of rail in the engine preview GUI.
Definition rail.h:189
SpriteID tunnel
tunnel sprites base
Definition rail.h:152
SpriteID single_sloped
single piece of rail for slopes
Definition rail.h:150
struct RailTypeInfo::@057010233226120022371265166156055202241326366156 cursor
Cursors associated with the rail type.
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
DiagDirection
Enumeration for diagonal directions.
Prices _price
Prices and also the fractional part.
Definition economy.cpp:109
Functions related to the economy.
@ BuildRail
Price for building rails.
@ InfrastructureRail
Rails maintenance cost.
@ ClearRail
Price for destroying rails.
VehicleAccelerationModel
Acceleration model of a vehicle.
Definition engine_type.h:46
#define Y
Climate toyland.
Definition engines.h:97
#define S
Climate sub-tropic.
Definition engines.h:95
Type (helpers) for enums.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Flat set container implementation.
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
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
Definition math_func.cpp:42
Types related to NewGRF badges.
Money RailConvertCost(RailType from, RailType to)
Calculates the cost of rail conversion.
Definition rail.h:476
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
void DrawTrainDepotSprite(int x, int y, DiagDirection dir, RailType railtype)
Draw train depot sprite in the UI.
RailTrackBridgeOffset
Offsets for sprites within a bridge surface overlay set.
Definition rail.h:93
RailSpriteType
Sprite types for a railtype.
Definition rail.h:42
@ TunnelPortal
Tunnel portal overlay.
Definition rail.h:53
@ Ground
Main group of ground images.
Definition rail.h:45
@ Pylons
Catenary pylons.
Definition rail.h:48
@ GroundComplete
Complete ground images.
Definition rail.h:55
@ Overlay
Images for overlaying track.
Definition rail.h:44
@ UI
Cursor and toolbar icon images.
Definition rail.h:43
@ End
End marker.
Definition rail.h:56
@ Crossing
Level crossing overlay images.
Definition rail.h:50
@ Wires
Catenary wires.
Definition rail.h:47
@ Signals
Signal images.
Definition rail.h:54
@ Fences
Fence images.
Definition rail.h:52
@ Bridge
Bridge surface images.
Definition rail.h:49
@ Tunnel
Main group of ground images for snow or desert.
Definition rail.h:46
Money RailClearCost(RailType railtype)
Returns the 'cost' of clearing the specified railtype.
Definition rail.h:459
Money RailBuildCost(RailType railtype)
Returns the cost of building the specified railtype.
Definition rail.h:448
RailType AllocateRailType(RailTypeLabel label)
Allocate a new rail type label.
Definition rail_cmd.cpp:149
std::vector< RailType > _sorted_railtypes
Sorted list of rail types.
Definition rail_cmd.cpp:46
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:80
RailFenceOffset
Offsets from base sprite for fence sprites.
Definition rail.h:111
@ SlopeNENW
Slope NE, Track X, Fence NW.
Definition rail.h:118
@ SlopeNESE
Slope NE, Track X, Fence SE.
Definition rail.h:126
@ SlopeSWNW
Slope SW, Track X, Fence NW.
Definition rail.h:116
@ FlatRight
Slope FLAT, Track RIGHT, Fence W.
Definition rail.h:122
@ FlatXSE
Slope FLAT, Track X, Fence SE.
Definition rail.h:120
@ FlatXNW
Slope FLAT, Track X, Fence NW.
Definition rail.h:112
@ SlopeSESW
Slope SE, Track Y, Fence SW.
Definition rail.h:125
@ SlopeNWNE
Slope NW, Track Y, Fence NE.
Definition rail.h:119
@ SlopeSWSE
Slope SW, Track X, Fence SE.
Definition rail.h:124
@ FlatUpper
Slope FLAT, Track UPPER, Fence S.
Definition rail.h:115
@ FlatYNE
Slope FLAT, Track Y, Fence NE.
Definition rail.h:113
@ FlatYSW
Slope FLAT, Track Y, Fence SW.
Definition rail.h:121
@ FlatLeft
Slope FLAT, Track LEFT, Fence E.
Definition rail.h:114
@ SlopeNWSW
Slope NW, Track Y, Fence SW.
Definition rail.h:127
@ SlopeSENE
Slope SE, Track Y, Fence NE.
Definition rail.h:117
@ FlatLower
Slope FLAT, Track LOWER, Fence N.
Definition rail.h:123
RailTypes GetAllIntroducesRailTypes(RailTypes railtypes)
Returns all introduced railtypes for a set of railtypes.
Definition rail.h:356
Money RailMaintenanceCost(RailType railtype, uint32_t num, uint32_t total_num)
Calculates the maintenance cost of a number of track bits.
Definition rail.h:503
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:70
RailTypes GetAllPoweredRailTypes(RailTypes railtypes)
Returns all powered railtypes for a set of railtypes.
Definition rail.h:344
bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition rail.h:371
constexpr SpriteID operator+(SpriteID sprite, RailTrackOffset offset)
Operator overload to allow RailTrackOffset to be added to a SpriteID without the need to use to_under...
Definition rail.h:88
void InitRailTypes()
Resolve sprites of custom rail types.
Definition rail_cmd.cpp:129
bool HasPowerOnRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType got power on a tile with a given RailType.
Definition rail.h:396
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:104
void ResetRailTypes()
Reset all rail type information to its default values.
Definition rail_cmd.cpp:64
Money SignalMaintenanceCost(uint32_t num)
Calculates the maintenance cost of a number of signals.
Definition rail.h:514
bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def=_settings_game.pf.forbid_90_deg)
Test if 90 degree turns are disallowed between two railtypes.
Definition rail.h:430
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:320
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Definition rail.h:332
RailTypeFlag
Railtype flag bit numbers.
Definition rail.h:27
@ Catenary
Bit number for drawing a catenary.
Definition rail.h:28
@ Allow90Deg
Bit number for always allowed 90 degree turns, regardless of setting.
Definition rail.h:32
@ Disallow90Deg
Bit number for never allowed 90 degree turns, regardless of setting.
Definition rail.h:33
@ Hidden
Bit number for hiding from selection.
Definition rail.h:30
@ NoSpriteCombine
Bit number for using non-combined junctions.
Definition rail.h:31
@ NoLevelCrossing
Bit number for disallowing level crossings.
Definition rail.h:29
RailTypes GetCompanyRailTypes(CompanyID company, bool introduces=true)
Get the rail types the given company can build.
Definition rail.cpp:137
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:168
Foundation GetRailFoundation(Slope tileh, TrackBits bits)
Checks if a track combination is valid on a specific slope and returns the needed foundation.
Definition rail_cmd.cpp:311
RailTrackOffset
Offsets for sprites within an overlay/underlay set.
Definition rail.h:63
@ X
Piece of rail in X direction.
Definition rail.h:64
@ JunctionNW
Ballast for junction 'pointing' NW.
Definition rail.h:78
@ JunctionSW
Ballast for junction 'pointing' SW.
Definition rail.h:75
@ JunctionSE
Ballast for junction 'pointing' SE.
Definition rail.h:77
@ SlopeSE
Piece of rail on slope with south-east raised.
Definition rail.h:71
@ SlopeSW
Piece of rail on slope with south-west raised.
Definition rail.h:72
@ SlopeNE
Piece of rail on slope with north-east raised.
Definition rail.h:70
@ JunctionNSEW
Ballast for full junction.
Definition rail.h:79
@ CrossingXY
Crossing of X and Y rail, with ballast.
Definition rail.h:74
@ JunctionNE
Ballast for junction 'pointing' NE.
Definition rail.h:76
@ SlopeNW
Piece of rail on slope with north-west raised.
Definition rail.h:73
bool RailNoLevelCrossings(RailType rt)
Test if a RailType disallows build of level crossings.
Definition rail.h:418
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels=true)
Get the rail type for a given label.
Definition rail.cpp:197
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:92
EnumBitSet< RailTypeFlag, uint8_t > RailTypeFlags
Bitset of RailTypeFlag elements.
Definition rail.h:37
The different types of rail.
EnumBitSet< RailType, uint64_t > RailTypes
Bitset of RailType elements.
Definition rail_type.h:37
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:26
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:32
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:33
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Types related to global configuration settings.
Types and classes related to signals.
SignalType
Type of signal, i.e.
Definition signal_type.h:22
@ End
End marker.
Definition signal_type.h:29
SignalVariant
Variant of the signal, i.e.
Definition signal_type.h:14
@ End
End marker.
Definition signal_type.h:17
Definitions of a slope.
Slope
Enumeration for the slope-type.
Definition slope_type.h:53
Foundation
Enumeration for Foundations.
Definition slope_type.h:119
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Colour for pixel/line drawing.
Definition gfx_type.h:307
Common wrapper for all the different sprite group types.
'Train' is either a loco or a wagon.
Definition train.h:97
Definition of the game-calendar-timer.
All types related to tracks.
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43