OpenTTD Source: src/rail.h File Reference

OpenTTD Source 1.11.0-RC1

Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
rail.h File Reference
`#include "rail_type.h"`
`#include "track_type.h"`
`#include "gfx_type.h"`
`#include "core/bitmath_func.hpp"`
`#include "economy_func.h"`
`#include "slope_type.h"`
`#include "strings_type.h"`
`#include "date_type.h"`
`#include "signal_type.h"`
`#include "settings_type.h"`

Go to the source code of this file.

## Data Structures
class | RailtypeInfo
| This struct contains all the info that is needed to draw and construct tracks. More...

## Macros
#define | FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes.size() && (var = _sorted_railtypes[index], true) ; index++)
| Loop header for iterating over railtypes, sorted by sortorder. More...

## Typedefs
typedef std::vector< RailTypeLabel > | RailTypeLabelList
| List of rail type labels.

## Enumerations
enum | RailTypeFlags { RTF_CATENARY = 0, RTF_NO_LEVEL_CROSSING = 1, RTF_HIDDEN = 2, RTF_NO_SPRITE_COMBINE = 3, RTF_ALLOW_90DEG = 4, RTF_DISALLOW_90DEG = 5, RTFB_NONE = 0, RTFB_CATENARY = 1 << RTF_CATENARY, RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING, RTFB_HIDDEN = 1 << RTF_HIDDEN, RTFB_NO_SPRITE_COMBINE = 1 << RTF_NO_SPRITE_COMBINE, RTFB_ALLOW_90DEG = 1 << RTF_ALLOW_90DEG, RTFB_DISALLOW_90DEG = 1 << RTF_DISALLOW_90DEG }
| Railtype flags. More...
enum | RailTypeSpriteGroup { RTSG_CURSORS, RTSG_OVERLAY, RTSG_GROUND, RTSG_TUNNEL, RTSG_WIRES, RTSG_PYLONS, RTSG_BRIDGE, RTSG_CROSSING, RTSG_DEPOT, RTSG_FENCES, RTSG_TUNNEL_PORTAL, RTSG_SIGNALS, RTSG_GROUND_COMPLETE, RTSG_END }
| Sprite groups for a railtype. More...
enum | RailTrackOffset { RTO_X, RTO_Y, RTO_N, RTO_S, RTO_E, RTO_W, RTO_SLOPE_NE, RTO_SLOPE_SE, RTO_SLOPE_SW, RTO_SLOPE_NW, RTO_CROSSING_XY, RTO_JUNCTION_SW, RTO_JUNCTION_NE, RTO_JUNCTION_SE, RTO_JUNCTION_NW, RTO_JUNCTION_NSEW }
| Offsets for sprites within an overlay/underlay set. More...
enum | RailTrackBridgeOffset { RTBO_X, RTBO_Y, RTBO_SLOPE }
| Offsets for sprites within a bridge surface overlay set. More...
enum | RailFenceOffset { RFO_FLAT_X_NW, RFO_FLAT_Y_NE, RFO_FLAT_LEFT, RFO_FLAT_UPPER, RFO_SLOPE_SW_NW, RFO_SLOPE_SE_NE, RFO_SLOPE_NE_NW, RFO_SLOPE_NW_NE, RFO_FLAT_X_SE, RFO_FLAT_Y_SW, RFO_FLAT_RIGHT, RFO_FLAT_LOWER, RFO_SLOPE_SW_SE, RFO_SLOPE_SE_SW, RFO_SLOPE_NE_SE, RFO_SLOPE_NW_SW }
| Offsets from base sprite for fence sprites. More...

## Functions
static const RailtypeInfo * | GetRailTypeInfo (RailType railtype)
| Returns a pointer to the Railtype information for a given railtype. More...
static bool | IsCompatibleRail (RailType enginetype, RailType tiletype)
| Checks if an engine of the given RailType can drive on a tile with a given RailType. More...
static bool | HasPowerOnRail (RailType enginetype, RailType tiletype)
| Checks if an engine of the given RailType got power on a tile with a given RailType. More...
static bool | RailNoLevelCrossings (RailType rt)
| Test if a RailType disallows build of level crossings. More...
static bool | Rail90DegTurnDisallowed (RailType rt1, RailType rt2, bool def=_settings_game.pf.forbid_90_deg)
| Test if 90 degree turns are disallowed between two railtypes. More...
static Money | RailBuildCost (RailType railtype)
| Returns the cost of building the specified railtype. More...
static Money | RailClearCost (RailType railtype)
| Returns the 'cost' of clearing the specified railtype. More...
static Money | RailConvertCost (RailType from, RailType to)
| Calculates the cost of rail conversion. More...
static Money | RailMaintenanceCost (RailType railtype, uint32 num, uint32 total_num)
| Calculates the maintenance cost of a number of track bits. More...
static Money | SignalMaintenanceCost (uint32 num)
| Calculates the maintenance cost of a number of signals. More...
void | DrawTrainDepotSprite (int x, int y, int image, RailType railtype)
int | TicksToLeaveDepot (const Train *v)
| Compute number of ticks when next wagon will leave a depot. More...
Foundation | GetRailFoundation (Slope tileh, TrackBits bits)
| Checks if a track combination is valid on a specific slope and returns the needed foundation. More...
bool | HasRailtypeAvail (const CompanyID company, const RailType railtype)
| Finds out if a company has a certain buildable railtype available. More...
bool | HasAnyRailtypesAvail (const CompanyID company)
| Test if any buildable railtype is available for a company. More...
bool | ValParamRailtype (const RailType rail)
| Validate functions for rail building. More...
RailTypes | AddDateIntroducedRailTypes (RailTypes current, Date date)
| Add the rail types that are to be introduced at the given date. More...
RailTypes | GetCompanyRailtypes (CompanyID company, bool introduces=true)
| Get the rail types the given company can build. More...
RailTypes | GetRailTypes (bool introduces)
| Get list of rail types, regardless of company availability. More...
RailType | GetRailTypeByLabel (RailTypeLabel label, bool allow_alternate_labels=true)
| Get the rail type for a given label. More...
void | ResetRailTypes ()
| Reset all rail type information to its default values.
void | InitRailTypes ()
| Resolve sprites of custom rail types.
RailType | AllocateRailType (RailTypeLabel label)
| Allocate a new rail type label.

## Variables
std::vector< RailType > | _sorted_railtypes
RailTypes | _railtypes_hidden_mask

## Detailed Description

Rail specific functions.

Definition in file rail.h.

## Macro Definition Documentation

## ◆ FOR_ALL_SORTED_RAILTYPES

#define FOR_ALL_SORTED_RAILTYPES | ( |  | var | ) | for (uint8 index = 0; index < _sorted_railtypes.size() && (var = _sorted_railtypes[index], true) ; index++)

Loop header for iterating over railtypes, sorted by sortorder.

Parameters

  var | Railtype.

Definition at line 471 of file rail.h.

## Enumeration Type Documentation

## ◆ RailFenceOffset

enum RailFenceOffset

Offsets from base sprite for fence sprites.

These are in the order of the sprites in the original data files.

Enumerator
RFO_FLAT_X_NW | Slope FLAT, Track X, Fence NW.
RFO_FLAT_Y_NE | Slope FLAT, Track Y, Fence NE.
RFO_FLAT_LEFT | Slope FLAT, Track LEFT, Fence E.
RFO_FLAT_UPPER | Slope FLAT, Track UPPER, Fence S.
RFO_SLOPE_SW_NW | Slope SW, Track X, Fence NW.
RFO_SLOPE_SE_NE | Slope SE, Track Y, Fence NE.
RFO_SLOPE_NE_NW | Slope NE, Track X, Fence NW.
RFO_SLOPE_NW_NE | Slope NW, Track Y, Fence NE.
RFO_FLAT_X_SE | Slope FLAT, Track X, Fence SE.
RFO_FLAT_Y_SW | Slope FLAT, Track Y, Fence SW.
RFO_FLAT_RIGHT | Slope FLAT, Track RIGHT, Fence W.
RFO_FLAT_LOWER | Slope FLAT, Track LOWER, Fence N.
RFO_SLOPE_SW_SE | Slope SW, Track X, Fence SE.
RFO_SLOPE_SE_SW | Slope SE, Track Y, Fence SW.
RFO_SLOPE_NE_SE | Slope NE, Track X, Fence SE.
RFO_SLOPE_NW_SW | Slope NW, Track Y, Fence SW.

Definition at line 99 of file rail.h.

## ◆ RailTrackBridgeOffset

enum RailTrackBridgeOffset

Offsets for sprites within a bridge surface overlay set.

Enumerator
RTBO_X | Piece of rail in X direction.
RTBO_Y | Piece of rail in Y direction.
RTBO_SLOPE | Sloped rail pieces, in order NE, SE, SW, NW.

Definition at line 89 of file rail.h.

## ◆ RailTrackOffset

enum RailTrackOffset

Offsets for sprites within an overlay/underlay set.

These are the same for overlay and underlay sprites.

Enumerator
RTO_X | Piece of rail in X direction.
RTO_Y | Piece of rail in Y direction.
RTO_N | Piece of rail in northern corner.
RTO_S | Piece of rail in southern corner.
RTO_E | Piece of rail in eastern corner.
RTO_W | Piece of rail in western corner.
RTO_SLOPE_NE | Piece of rail on slope with north-east raised.
RTO_SLOPE_SE | Piece of rail on slope with south-east raised.
RTO_SLOPE_SW | Piece of rail on slope with south-west raised.
RTO_SLOPE_NW | Piece of rail on slope with north-west raised.
RTO_CROSSING_XY | Crossing of X and Y rail, with ballast.
RTO_JUNCTION_SW | Ballast for junction 'pointing' SW.
RTO_JUNCTION_NE | Ballast for junction 'pointing' NE.
RTO_JUNCTION_SE | Ballast for junction 'pointing' SE.
RTO_JUNCTION_NW | Ballast for junction 'pointing' NW.
RTO_JUNCTION_NSEW | Ballast for full junction.

Definition at line 67 of file rail.h.

## ◆ RailTypeFlags

enum RailTypeFlags

Railtype flags.

Enumerator
RTF_CATENARY | Bit number for drawing a catenary.
RTF_NO_LEVEL_CROSSING | Bit number for disallowing level crossings.
RTF_HIDDEN | Bit number for hiding from selection.
RTF_NO_SPRITE_COMBINE | Bit number for using non-combined junctions.
RTF_ALLOW_90DEG | Bit number for always allowed 90 degree turns, regardless of setting.
RTF_DISALLOW_90DEG | Bit number for never allowed 90 degree turns, regardless of setting.
RTFB_NONE | All flags cleared.
RTFB_CATENARY | Value for drawing a catenary.
RTFB_NO_LEVEL_CROSSING | Value for disallowing level crossings.
RTFB_HIDDEN | Value for hiding from selection.
RTFB_NO_SPRITE_COMBINE | Value for using non-combined junctions.
RTFB_ALLOW_90DEG | Value for always allowed 90 degree turns, regardless of setting.
RTFB_DISALLOW_90DEG | Value for never allowed 90 degree turns, regardless of setting.

Definition at line 25 of file rail.h.

## ◆ RailTypeSpriteGroup

enum RailTypeSpriteGroup

Sprite groups for a railtype.

Enumerator
RTSG_CURSORS | Cursor and toolbar icon images.
RTSG_OVERLAY | Images for overlaying track.
RTSG_GROUND | Main group of ground images.
RTSG_TUNNEL | Main group of ground images for snow or desert.
RTSG_WIRES | Catenary wires.
RTSG_PYLONS | Catenary pylons.
RTSG_BRIDGE | Bridge surface images.
RTSG_CROSSING | Level crossing overlay images.
RTSG_DEPOT | Depot images.
RTSG_FENCES | Fence images.
RTSG_TUNNEL_PORTAL | Tunnel portal overlay.
RTSG_SIGNALS | Signal images.
RTSG_GROUND_COMPLETE | Complete ground images.

Definition at line 46 of file rail.h.

## Function Documentation

## ◆ AddDateIntroducedRailTypes()

RailTypes AddDateIntroducedRailTypes | ( | RailTypes | current,
|  | Date | date
| ) |  |

Add the rail types that are to be introduced at the given date.

Parameters

  current | The currently available railtypes.
  date | The date for the introduction comparisons.

Returns
  The rail types that should be available when date introduced rail types are taken into account as well.

Definition at line 218 of file rail.cpp.

References AddDateIntroducedRailTypes(), GetRailTypeInfo(), RailtypeInfo::introduces_railtypes, RailtypeInfo::introduction_date, RailtypeInfo::introduction_required_railtypes, IsInsideMM(), RailtypeInfo::label, MAX_DAY, RAILTYPE_BEGIN, and RAILTYPE_END.

Referenced by AddDateIntroducedRailTypes(), EnableEngineForCompany(), EnginesDailyLoop(), GetCompanyRailtypes(), and GetRailTypes().

## ◆ GetCompanyRailtypes()

RailTypes GetCompanyRailtypes | ( | CompanyID | company,
|  | bool | introduces
| ) |  |

Get the rail types the given company can build.

Parameters

  company | the company to get the rail types for.
  introduces | If true, include rail types introduced by other rail types

Returns
  the rail types.

Definition at line 251 of file rail.cpp.

References _date, _settings_game, AddDateIntroducedRailTypes(), EngineInfo::climates, DAYS_IN_YEAR, GameSettings::game_creation, GetRailTypeInfo(), HasBit(), RailtypeInfo::introduces_railtypes, Engine::IterateType(), GameCreationSettings::landscape, RAILTYPE_END, RAILTYPES_NONE, RAILVEH_WAGON, SetBit(), and VEH_TRAIN.

Referenced by GetRailTypeDropDownList(), SettingsDisableElrail(), and StartupEngines().

## ◆ GetRailFoundation()

Foundation GetRailFoundation | ( | Slope | tileh,
|  | TrackBits | bits
| ) |  |

Checks if a track combination is valid on a specific slope and returns the needed foundation.

Parameters

  tileh | Tile slope.
  bits | Trackbits.

Returns
  Needed foundation or FOUNDATION_INVALID if track/slope combination is not allowed.

Definition at line 321 of file rail_cmd.cpp.

References _valid_tracks_on_leveled_foundation, _valid_tracks_without_foundation, CornerToTrackBits(), FOUNDATION_INCLINED_X, FOUNDATION_INCLINED_Y, FOUNDATION_INVALID, FOUNDATION_NONE, FOUNDATION_STEEP_BOTH, FOUNDATION_STEEP_LOWER, GetHighestSlopeCorner(), HalftileFoundation(), IsSteepSlope(), TRACK_BIT_LEFT, TRACK_BIT_NONE, TRACK_BIT_X, TRACK_BIT_Y, and TracksOverlap().

Referenced by CheckRailSlope(), and TestAutoslopeOnRailTile().

## ◆ GetRailTypeByLabel()

RailType GetRailTypeByLabel | ( | RailTypeLabel | label,
|  | bool | allow_alternate_labels
| ) |  |

Get the rail type for a given label.

Parameters

  label | the railtype label.
  allow_alternate_labels | Search in the alternate label lists as well.

Returns
  the railtype.

Definition at line 311 of file rail.cpp.

References RailtypeInfo::alternate_labels, GetRailTypeInfo(), INVALID_RAILTYPE, RailtypeInfo::label, RAILTYPE_BEGIN, and RAILTYPE_END.

Referenced by GetRailTypeTranslation().

## ◆ GetRailTypeInfo()

static const RailtypeInfo* GetRailTypeInfo | ( | RailType | railtype | ) |
| inlinestatic

Returns a pointer to the Railtype information for a given railtype.

Parameters

  railtype | the rail type which the information is requested for

Returns
  The pointer to the RailtypeInfo

Definition at line 304 of file rail.h.

Referenced by AddDateIntroducedRailTypes(), CheckAutoreplaceValidity(), CompareRailTypes(), DrawBridgeMiddle(), DrawTile_Road(), DrawTile_TunnelBridge(), DrawTrackBits(), EnableEngineForCompany(), FollowTrainReservation(), Train::GetAccelerationType(), GetCompanyRailtypes(), Train::GetMaxTrackSpeed(), GetPylonBase(), GetRailTypeByLabel(), GetRailTypeDropDownList(), GetRailTypes(), GetRailTypeTranslation(), GetReverseRailTypeTranslation(), CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::GetSpeedLimit(), GetTrainForReservation(), GetWireBase(), HasPowerOnRail(), HasRailCatenary(), IsCompatibleRail(), IsEngineBuildable(), NPFTrainFindNearestSafeTile(), BuildRailToolbarWindow::OnClick(), BuildSignalWindow::OnInit(), ReplaceVehicleWindow::OnPaint(), Rail90DegTurnDisallowed(), RailNoLevelCrossings(), BuildRailToolbarWindow::SetStringParameters(), BuildVehicleWindow::SetStringParameters(), BuildRailToolbarWindow::SetupRailToolbar(), ReplaceVehicleWindow::UpdateWidgetSize(), and CompanyInfrastructureWindow::UpdateWidgetSize().

## ◆ GetRailTypes()

RailTypes GetRailTypes | ( | bool | introduces | ) |

Get list of rail types, regardless of company availability.

Parameters

  introduces | If true, include rail types introduced by other rail types

Returns
  the rail types.

Definition at line 282 of file rail.cpp.

References _settings_game, AddDateIntroducedRailTypes(), EngineInfo::climates, GameSettings::game_creation, GetRailTypeInfo(), HasBit(), RailtypeInfo::introduces_railtypes, Engine::IterateType(), GameCreationSettings::landscape, MAX_DAY, RAILTYPE_END, RAILTYPES_NONE, RAILVEH_WAGON, SetBit(), and VEH_TRAIN.

Referenced by GetRailTypeDropDownList().

## ◆ HasAnyRailtypesAvail()

bool HasAnyRailtypesAvail | ( | const CompanyID | company | ) |

Test if any buildable railtype is available for a company.

Parameters

  company | the company in question

Returns
  true if company has any RailTypes available

Definition at line 196 of file rail.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get().

Referenced by CanBuildVehicleInfrastructure().

## ◆ HasPowerOnRail()

static bool HasPowerOnRail | ( | RailType | enginetype,
|  | RailType | tiletype
| ) |  |
| inlinestatic

Checks if an engine of the given RailType got power on a tile with a given RailType.

This would normally just be an equality check, but for electric rails (which also support non-electric engines).

Returns
  Whether the engine got power on this tile.

Parameters

  enginetype | The RailType of the engine we are considering.
  tiletype | The RailType of the tile we are considering.

Definition at line 332 of file rail.h.

References GetRailTypeInfo(), and HasBit().

Referenced by CmdBuildRailVehicle(), CmdConvertRail(), Train::GetPower(), Train::GetPoweredPartPower(), and RailConvertCost().

## ◆ HasRailtypeAvail()

bool HasRailtypeAvail | ( | const CompanyID | company,
|  | const RailType | railtype
| ) |  |

Finds out if a company has a certain buildable railtype available.

Parameters

  company | the company in question
  railtype | requested RailType

Returns
  true if company has requested RailType available

Definition at line 186 of file rail.cpp.

References HasBit().

Referenced by ValParamRailtype().

## ◆ IsCompatibleRail()

static bool IsCompatibleRail | ( | RailType | enginetype,
|  | RailType | tiletype
| ) |  |
| inlinestatic

Checks if an engine of the given RailType can drive on a tile with a given RailType.

This would normally just be an equality check, but for electric rails (which also support non-electric engines).

Returns
  Whether the engine can drive on this tile.

Parameters

  enginetype | The RailType of the engine we are considering.
  tiletype | The RailType of the tile we are considering.

Definition at line 319 of file rail.h.

References GetRailTypeInfo(), and HasBit().

Referenced by CmdBuildRailWagon(), CmdConvertRail(), and IsCompatibleTrainStationTile().

## ◆ Rail90DegTurnDisallowed()

static bool Rail90DegTurnDisallowed | ( | RailType | rt1,
|  | RailType | rt2,
|  | bool | def = `_settings_game.pf.forbid_90_deg`
| ) |  |
| inlinestatic

Test if 90 degree turns are disallowed between two railtypes.

Parameters

  rt1 | First railtype to test for.
  rt2 | Second railtype to test for.
  def | Default value to use if the rail type doesn't specify anything.

Returns
  True if 90 degree turns are disallowed between the two rail types.

Definition at line 354 of file rail.h.

References RailtypeInfo::flags, GetRailTypeInfo(), HasBit(), INVALID_RAILTYPE, RTF_ALLOW_90DEG, and RTF_DISALLOW_90DEG.

Referenced by ExtendTrainReservation(), TrainCheckIfLineEnds(), TrainController(), and TryPathReserve().

## ◆ RailBuildCost()

static Money RailBuildCost | ( | RailType | railtype | ) |
| inlinestatic

Returns the cost of building the specified railtype.

Parameters

  railtype | The railtype being built.

Returns
  The cost multiplier.

Definition at line 372 of file rail.h.

References RAILTYPE_END.

Referenced by RailConvertCost().

## ◆ RailClearCost()

static Money RailClearCost | ( | RailType | railtype | ) |
| inlinestatic

Returns the 'cost' of clearing the specified railtype.

Parameters

  railtype | The railtype being removed.

Returns
  The cost.

Definition at line 383 of file rail.h.

References RAILTYPE_END.

Referenced by RailConvertCost().

## ◆ RailConvertCost()

static Money RailConvertCost | ( | RailType | from,
|  | RailType | to
| ) |  |
| inlinestatic

Calculates the cost of rail conversion.

Parameters

  from | The railtype we are converting from
  to | The railtype we are converting to

Returns
  Cost per TrackBit

Definition at line 400 of file rail.h.

References HasPowerOnRail(), RailBuildCost(), and RailClearCost().

Referenced by CmdConvertRail().

## ◆ RailMaintenanceCost()

static Money RailMaintenanceCost | ( | RailType | railtype,
|  | uint32 | num,
|  | uint32 | total_num
| ) |  |
| inlinestatic

Calculates the maintenance cost of a number of track bits.

Parameters

  railtype | The railtype to get the cost of.
  num | Number of track bits of this railtype.
  total_num | Total number of track bits of all railtypes.

Returns
  Total cost.

Definition at line 427 of file rail.h.

References RAILTYPE_END.

Referenced by CompanyInfrastructureWindow::GetTotalMaintenanceCost().

## ◆ RailNoLevelCrossings()

static bool RailNoLevelCrossings | ( | RailType | rt | ) |
| inlinestatic

Test if a RailType disallows build of level crossings.

Parameters

  rt | The RailType to check.

Returns
  Whether level crossings are not allowed.

Definition at line 342 of file rail.h.

References GetRailTypeInfo(), HasBit(), and RTF_NO_LEVEL_CROSSING.

Referenced by CmdConvertRail().

## ◆ SignalMaintenanceCost()

static Money SignalMaintenanceCost | ( | uint32 | num | ) |
| inlinestatic

Calculates the maintenance cost of a number of signals.

Parameters

  num | Number of signals.

Returns
  Total cost.

Definition at line 438 of file rail.h.

Referenced by CompanyInfrastructureWindow::GetTotalMaintenanceCost().

## ◆ TicksToLeaveDepot()

int TicksToLeaveDepot | ( | const Train * | v | ) |

Compute number of ticks when next wagon will leave a depot.

Negative means next wagon should have left depot n ticks before.

Parameters

  v | vehicle outside (leaving) the depot

Returns
  number of ticks when the next wagon will leave

Definition at line 2943 of file rail_cmd.cpp.

References Train::CalcNextVehicleOffset(), DIAGDIR_NE, GetRailDepotDirection(), Vehicle::tile, and Vehicle::x_pos.

Referenced by AdvanceWagonsAfterSwap(), and CheckTrainsLengths().

## ◆ ValParamRailtype()

bool ValParamRailtype | ( | const RailType | rail | ) |

Validate functions for rail building.

Parameters

  rail | the railtype to check.

Returns
  true if the current company may build the rail.

Definition at line 206 of file rail.cpp.

References _current_company, HasRailtypeAvail(), and RAILTYPE_END.

Referenced by CmdBuildBridge(), CmdBuildRailStation(), CmdBuildSingleRail(), CmdBuildTrainDepot(), CmdBuildTunnel(), CmdConvertRail(), CmdRailTrackHelper(), and ShowBuildRailToolbar().