# rail.h File Reference

Rail specific functions. More...

`#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"`

Go to the source code of this file.

## Data Structures
struct | RailtypeInfo
| This struct contains all the info that is needed to draw and construct tracks. More...
## Enumerations
enum | RailTypeFlags { RTF_CATENARY = 0, RTF_NO_LEVEL_CROSSING = 1, RTFB_NONE = 0, RTFB_CATENARY = 1 << RTF_CATENARY, RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING }
enum | RailTypeSpriteGroup { RTSG_CURSORS, RTSG_OVERLAY, RTSG_GROUND, RTSG_TUNNEL, RTSG_WIRES, RTSG_PYLONS, RTSG_BRIDGE, RTSG_CROSSING, RTSG_DEPOT, RTSG_FENCES, RTSG_END }
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 spries within a bridge surface overlay set. More...
enum | RailFenceOffset { RFO_FLAT_X, RFO_FLAT_Y, RFO_FLAT_VERT, RFO_FLAT_HORZ, RFO_SLOPE_SW, RFO_SLOPE_SE, RFO_SLOPE_NE, RFO_SLOPE_NW }
| Offsets from base sprite for fence sprites. More...
## Functions
| DECLARE_ENUM_AS_BIT_SET (RailTypeFlags)
static const RailtypeInfo * | GetRailTypeInfo (RailType railtype)
| Returns a pointer to the Railtype information for a given railtype.
static bool | IsCompatibleRail (RailType enginetype, RailType tiletype)
| Checks if an engine of the given RailType can drive on a tile with a given RailType.
static bool | HasPowerOnRail (RailType enginetype, RailType tiletype)
| Checks if an engine of the given RailType got power on a tile with a given RailType.
static bool | RailNoLevelCrossings (RailType rt)
| Test if a RailType disallows build of level crossings.
static Money | RailBuildCost (RailType railtype)
| Returns the cost of building the specified railtype.
static Money | RailClearCost (RailType railtype)
| Returns the 'cost' of clearing the specified railtype.
static Money | RailConvertCost (RailType from, RailType to)
| Calculates the cost of rail conversion.
void | DrawTrainDepotSprite (int x, int y, int image, RailType railtype)
Vehicle * | EnsureNoTrainOnTrackProc (Vehicle *v, void *data)
int | TicksToLeaveDepot (const Train *v)
| Compute number of ticks when next wagon will leave a depot.
Foundation | GetRailFoundation (Slope tileh, TrackBits bits)
| Checks if a track combination is valid on a specific slope and returns the needed foundation.
bool | HasRailtypeAvail (const CompanyID company, const RailType railtype)
| Finds out if a company has a certain railtype available.
bool | ValParamRailtype (const RailType rail)
| Validate functions for rail building.
RailType | GetBestRailtype (const CompanyID company)
| Returns the "best" railtype a company can build.
RailTypes | GetCompanyRailtypes (const CompanyID c)
| Get the rail types the given company can build.
RailType | GetRailTypeByLabel (RailTypeLabel label)
| Get the rail type for a given label.
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.

## Detailed Description

Rail specific functions.

Definition in file rail.h.

## Enumeration Type Documentation

enum RailFenceOffset

Offsets from base sprite for fence sprites.

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

Definition at line 84 of file rail.h.

enum RailTrackBridgeOffset

Offsets for spries 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 75 of file rail.h.

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 53 of file rail.h.

enum RailTypeFlags

Enumerator:

  RTF_CATENARY | Bit number for drawing a catenary.
  RTF_NO_LEVEL_CROSSING | Bit number for disallowing level crossings.
  RTFB_NONE | All flags cleared.
  RTFB_CATENARY | Value for drawing a catenary.
  RTFB_NO_LEVEL_CROSSING | Value for disallowing level crossings.

Definition at line 23 of file rail.h.

enum RailTypeSpriteGroup

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.

Definition at line 35 of file rail.h.

## Function Documentation

RailType GetBestRailtype | ( | const CompanyID | company | ) |

Returns the "best" railtype a company can build.

As the AI doesn't know what the BEST one is, we have our own priority list here. When adding new railtypes, modify this function

Parameters:

  | company | the company "in action"

Returns:
  The "best" railtype a company has available

Definition at line 187 of file rail.cpp.

References HasRailtypeAvail(), RAILTYPE_ELECTRIC, RAILTYPE_MAGLEV, RAILTYPE_MONO, and RAILTYPE_RAIL.

Referenced by SetDefaultRailGui().

RailTypes GetCompanyRailtypes | ( | const CompanyID | c | ) |

Get the rail types the given company can build.

Parameters:

  | c | the company to get the rail types for.

Returns:
  the rail types.

Definition at line 195 of file rail.cpp.

References _date, _settings_game, DAYS_IN_YEAR, GameSettings::game_creation, HasBit(), GameCreationSettings::landscape, RAILTYPE_END, RAILTYPES_NONE, RAILVEH_WAGON, SetBit(), and VEH_TRAIN.

Referenced by DoStartupNewCompany(), and SettingsDisableElrail().

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 263 of file rail_cmd.cpp.

References CornerToTrackBits(), FOUNDATION_INCLINED_X, FOUNDATION_INCLINED_Y, FOUNDATION_INVALID, FOUNDATION_LEVELED, FOUNDATION_NONE, FOUNDATION_STEEP_BOTH, FOUNDATION_STEEP_LOWER, GetHighestSlopeCorner(), HalftileFoundation(), IsSlopeWithOneCornerRaised(), IsSlopeWithThreeCornersRaised(), IsSteepSlope(), OppositeCorner(), SLOPE_E, SLOPE_N, SLOPE_S, SLOPE_W, SlopeWithOneCornerRaised(), SlopeWithThreeCornersRaised(), SpecialRailFoundation(), TRACK_BIT_HORZ, TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_NONE, TRACK_BIT_RIGHT, TRACK_BIT_UPPER, TRACK_BIT_VERT, TRACK_BIT_X, TRACK_BIT_Y, and TracksOverlap().

Referenced by CheckRailSlope(), DrawCatenaryRailway(), DrawTrackBits(), FloodHalftile(), and TestAutoslopeOnRailTile().

RailType GetRailTypeByLabel | ( | RailTypeLabel | label | ) |

Get the rail type for a given label.

Parameters:

  | label | the railtype label.

Returns:
  the railtype.

Definition at line 217 of file rail.cpp.

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

static const RailtypeInfo* GetRailTypeInfo | ( | RailType | railtype | ) | `[inline, static]`

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 228 of file rail.h.

References RAILTYPE_END.

Referenced by BuildRailClick_AutoRail(), BuildRailClick_Convert(), BuildRailClick_Depot(), BuildRailClick_E(), BuildRailClick_N(), BuildRailClick_NE(), BuildRailClick_NW(), BuildRailClick_Tunnel(), CheckAutoreplaceValidity(), Train::ConsistChanged(), DrawStationTile(), DrawTile_Road(), DrawTile_TunnelBridge(), DrawTrackBits(), VehicleDetailsWindow::DrawWidget(), FollowTrainReservation(), FreeTrainTrackReservation(), Train::GetAccelerationType(), Train::GetCurveSpeedLimit(), GetEngineCategoryName(), GetGlobalVariable(), AIRail::GetMaxSpeed(), GetNewEngine(), GetPylonBase(), GetRailTypeByLabel(), GetSmallMapRoutesPixels(), CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::GetSpeedLimit(), GetTrainForReservation(), GetWireBase(), HasCatenary(), HasPowerOnRail(), IsCompatibleRail(), IsEngineBuildable(), IsSafeWaitingPosition(), IsWaitingPositionFree(), NeedRailTypeConversion(), NPFTrainFindNearestSafeTile(), ReplaceVehicleWindow::OnClick(), ReplaceVehicleWindow::OnPaint(), Train::PowerChanged(), RailBuildCost(), RailNoLevelCrossings(), ResetNewGRFData(), BuildRailToolbarWindow::SetStringParameters(), BuildVehicleWindow::SetStringParameters(), BuildRailToolbarWindow::SetupRailToolbar(), and ReplaceVehicleWindow::UpdateWidgetSize().

static bool HasPowerOnRail | ( | RailType | enginetype,
|  | RailType | tiletype |
| ) |  |  | `[inline, static]`

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 256 of file rail.h.

References GetRailTypeInfo(), and HasBit().

Referenced by CheckFlatLandBelow(), CmdBuildRailVehicle(), CmdBuildSingleRail(), CmdConvertRail(), Train::GetPower(), Train::GetPoweredPartPower(), RailConvertCost(), and AIRail::TrainHasPowerOnRail().

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

Finds out if a company has a certain railtype available.

Parameters:

  | company | the company in question
  | railtype | requested RailType

Returns:
  true if company has requested RailType available

Definition at line 177 of file rail.cpp.

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

Referenced by GetBestRailtype(), AIRail::IsRailTypeAvailable(), SetDefaultRailGui(), and ValParamRailtype().

static bool IsCompatibleRail | ( | RailType | enginetype,
|  | RailType | tiletype |
| ) |  |  | `[inline, static]`

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 243 of file rail.h.

References GetRailTypeInfo(), and HasBit().

Referenced by AIEngine::CanRunOnRail(), CmdBuildRailWagon(), CmdBuildSingleRail(), CmdConvertRail(), IsCompatibleTrainStationTile(), and AIRail::TrainCanRunOnRail().

static Money RailBuildCost | ( | RailType | railtype | ) | `[inline, static]`

Returns the cost of building the specified railtype.

Parameters:

  | railtype | The railtype being built.

Returns:
  The cost multiplier.

Definition at line 276 of file rail.h.

References GetRailTypeInfo(), and RAILTYPE_END.

Referenced by CmdBuildBridge(), CmdBuildRailStation(), CmdBuildSingleRail(), CmdBuildTrainDepot(), CmdBuildTunnel(), AIRail::GetBuildCost(), RailClearCost(), RailConvertCost(), and ShowBuildBridgeWindow().

static Money RailClearCost | ( | RailType | railtype | ) | `[inline, static]`

Returns the 'cost' of clearing the specified railtype.

Parameters:

  | railtype | The railtype being removed.

Returns:
  The cost.

Definition at line 287 of file rail.h.

References max(), RailBuildCost(), and RAILTYPE_END.

Referenced by CmdRemoveSingleRail(), and RailConvertCost().

static Money RailConvertCost | ( | RailType | from,
|  | RailType | to |
| ) |  |  | `[inline, static]`

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 304 of file rail.h.

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

Referenced by CmdConvertRail().

static bool RailNoLevelCrossings | ( | RailType | rt | ) | `[inline, static]`

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 266 of file rail.h.

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

Referenced by CmdBuildRoad(), CmdBuildSingleRail(), and CmdConvertRail().

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 2655 of file rail_cmd.cpp.

References TrainCache::cached_veh_length, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, GetRailDepotDirection(), and Vehicle::tile.

Referenced by AdvanceWagonsAfterSwap(), and CheckTrainsLengths().

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 182 of file rail.cpp.

References HasRailtypeAvail(), and RAILTYPE_END.

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