# rail.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "bridge_map.h"`
`#include "rail.h"`
`#include "station_map.h"`
`#include "tunnel_map.h"`
`#include "tunnelbridge_map.h"`
`#include "settings_type.h"`
`#include "date_func.h"`
`#include "player_func.h"`
`#include "player_base.h"`

Go to the source code of this file.

## Functions
RailType | GetTileRailType (TileIndex tile)
| Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
bool | HasRailtypeAvail (const PlayerID p, const RailType railtype)
| Finds out if a Player has a certain railtype available.
bool | ValParamRailtype (const RailType rail)
| Validate functions for rail building.
RailType | GetBestRailtype (const PlayerID p)
| Returns the "best" railtype a player can build.
RailTypes | GetPlayerRailtypes (PlayerID p)
| Get the rail types the given player can build.
## Variables
const byte | _signal_along_trackdir [TRACKDIR_END]
const byte | _signal_against_trackdir [TRACKDIR_END]
const byte | _signal_on_track []
const TrackdirBits | _exitdir_reaches_trackdirs []
const Trackdir | _next_trackdir [TRACKDIR_END]
const TrackdirBits | _track_crosses_trackdirs [TRACKDIR_END]
const TrackBits | _track_crosses_tracks []
const DiagDirection | _trackdir_to_exitdir [TRACKDIR_END]
const Trackdir | _track_exitdir_to_trackdir [][DIAGDIR_END]
const Trackdir | _track_enterdir_to_trackdir [][DIAGDIR_END]
const Trackdir | _track_direction_to_trackdir [][DIR_END]
const Trackdir | _dir_to_diag_trackdir []
const TrackBits | _corner_to_trackbits []
const TrackdirBits | _uphill_trackdirs []
const int | _default_railtype_cost_multiplier [RAILTYPE_END]
int | _railtype_cost_multiplier [RAILTYPE_END]

## Detailed Description

Definition in file rail.cpp.

## Function Documentation

RailType GetBestRailtype | ( | const PlayerID | p | ) |

Returns the "best" railtype a player 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:

  | p | the player "in action"

Returns:
  The "best" railtype a player has available

Definition at line 192 of file rail.cpp.

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

RailTypes GetPlayerRailtypes | ( | const PlayerID | p | ) |

Get the rail types the given player can build.

Parameters:

  | p | the player to get the rail types for.

Returns:
  the rail types.

Definition at line 200 of file rail.cpp.

References EngineInfo::climates, HasBit(), RAILTYPE_END, RAILTYPES_NONE, RAILVEH_WAGON, and SetBit().

Referenced by DoStartupNewPlayer(), and SettingsDisableElrail().

bool HasRailtypeAvail | ( | const PlayerID | p,
|  | const RailType | railtype |
| ) |  |  |

Finds out if a Player has a certain railtype available.

Parameters:

  | p | Player in question
  | railtype | requested RailType

Returns:
  true if player has requested RailType available

Definition at line 182 of file rail.cpp.

References HasBit().

Referenced by GetBestRailtype(), and ValParamRailtype().

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

Validate functions for rail building.

Parameters:

  | rail | the railtype to check.

Returns:
  true if the current player may build the rail.

Definition at line 187 of file rail.cpp.

References HasRailtypeAvail().

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

## Variable Documentation

const TrackBits _corner_to_trackbits[]

Initial value:

     {
      TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_RIGHT, TRACK_BIT_UPPER,
    }

const int _default_railtype_cost_multiplier[RAILTYPE_END]

Initial value:

     {
      8, 12, 16, 24,
    }

Definition at line 152 of file rail.cpp.

const Trackdir _dir_to_diag_trackdir[]

Initial value:

     {
      TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW,
    }

const TrackdirBits _exitdir_reaches_trackdirs[]

Initial value:

     {
      TRACKDIR_BIT_X_NE | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_LEFT_N,
      TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E,
      TRACKDIR_BIT_X_SW | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_RIGHT_S,
      TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W
    }

const Trackdir _next_trackdir[TRACKDIR_END]

Initial value:

     {
      TRACKDIR_X_NE,  TRACKDIR_Y_SE,  TRACKDIR_LOWER_E, TRACKDIR_UPPER_E, TRACKDIR_RIGHT_S, TRACKDIR_LEFT_S, INVALID_TRACKDIR, INVALID_TRACKDIR,
      TRACKDIR_X_SW,  TRACKDIR_Y_NW,  TRACKDIR_LOWER_W, TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N
    }

const byte _signal_against_trackdir[TRACKDIR_END]

Initial value:

     {
      0x4, 0x4, 0x4, 0x1, 0x8, 0x2, 0, 0,
      0x8, 0x8, 0x8, 0x2, 0x4, 0x1
    }

const byte _signal_along_trackdir[TRACKDIR_END]

Initial value:

     {
      0x8, 0x8, 0x8, 0x2, 0x4, 0x1, 0, 0,
      0x4, 0x4, 0x4, 0x1, 0x8, 0x2
    }

const byte _signal_on_track[]

Initial value:

     {
      0xC, 0xC, 0xC, 0x3, 0xC, 0x3
    }

const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END]

Initial value:

     {
      TRACKDIR_BIT_Y_SE     | TRACKDIR_BIT_Y_NW,
      TRACKDIR_BIT_X_NE     | TRACKDIR_BIT_X_SW,
      TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,
      TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,
      TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E,
      TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E
    }

const TrackBits _track_crosses_tracks[]

Initial value:

     {
      TRACK_BIT_Y,
      TRACK_BIT_X,
      TRACK_BIT_VERT,
      TRACK_BIT_VERT,
      TRACK_BIT_HORZ,
      TRACK_BIT_HORZ
    }

const Trackdir _track_direction_to_trackdir[][DIR_END]

Initial value:

     {
      {INVALID_TRACKDIR, TRACKDIR_X_NE,     INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_X_SW,     INVALID_TRACKDIR, INVALID_TRACKDIR},
      {INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_Y_SE,     INVALID_TRACKDIR, INVALID_TRACKDIR,  INVALID_TRACKDIR, TRACKDIR_Y_NW},
      {INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_UPPER_E, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_UPPER_W, INVALID_TRACKDIR},
      {INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LOWER_E, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LOWER_W, INVALID_TRACKDIR},
      {TRACKDIR_LEFT_N,  INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_LEFT_S,  INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR},
      {TRACKDIR_RIGHT_N, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR,  TRACKDIR_RIGHT_S, INVALID_TRACKDIR,  INVALID_TRACKDIR, INVALID_TRACKDIR}
    }

const Trackdir _track_enterdir_to_trackdir[][DIAGDIR_END]

Initial value:

     {
      {TRACKDIR_X_NE,     INVALID_TRACKDIR,  TRACKDIR_X_SW,     INVALID_TRACKDIR},
      {INVALID_TRACKDIR,  TRACKDIR_Y_SE,     INVALID_TRACKDIR,  TRACKDIR_Y_NW},
      {INVALID_TRACKDIR,  TRACKDIR_UPPER_E,  TRACKDIR_UPPER_W,  INVALID_TRACKDIR},
      {TRACKDIR_LOWER_E,  INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_LOWER_W},
      {TRACKDIR_LEFT_N,   TRACKDIR_LEFT_S,   INVALID_TRACKDIR,  INVALID_TRACKDIR},
      {INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_RIGHT_S,  TRACKDIR_RIGHT_N}
    }

const Trackdir _track_exitdir_to_trackdir[][DIAGDIR_END]

Initial value:

     {
      {TRACKDIR_X_NE,     INVALID_TRACKDIR,  TRACKDIR_X_SW,     INVALID_TRACKDIR},
      {INVALID_TRACKDIR,  TRACKDIR_Y_SE,     INVALID_TRACKDIR,  TRACKDIR_Y_NW},
      {TRACKDIR_UPPER_E,  INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_UPPER_W},
      {INVALID_TRACKDIR,  TRACKDIR_LOWER_E,  TRACKDIR_LOWER_W,  INVALID_TRACKDIR},
      {INVALID_TRACKDIR,  INVALID_TRACKDIR,  TRACKDIR_LEFT_S,   TRACKDIR_LEFT_N},
      {TRACKDIR_RIGHT_N,  TRACKDIR_RIGHT_S,  INVALID_TRACKDIR,  INVALID_TRACKDIR}
    }

const DiagDirection _trackdir_to_exitdir[TRACKDIR_END]

Initial value:

     {
      DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_NE,
      DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE,
    }