# rail_cmd.cpp File Reference

Handling of rail tiles. More...

`#include "stdafx.h"`
`#include "cmd_helper.h"`
`#include "landscape.h"`
`#include "viewport_func.h"`
`#include "command_func.h"`
`#include "engine_base.h"`
`#include "depot_base.h"`
`#include "pathfinder/yapf/yapf_cache.h"`
`#include "newgrf_engine.h"`
`#include "landscape_type.h"`
`#include "newgrf_railtype.h"`
`#include "newgrf_commons.h"`
`#include "train.h"`
`#include "variables.h"`
`#include "autoslope.h"`
`#include "water.h"`
`#include "tunnelbridge_map.h"`
`#include "window_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "tunnelbridge.h"`
`#include "functions.h"`
`#include "elrail_func.h"`
`#include "town.h"`
`#include "pbs.h"`
`#include "company_base.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`
`#include "table/railtypes.h"`
`#include "table/track_land.h"`

Go to the source code of this file.

## Defines
#define | M(x) (1 << (x))
#define | MAYBE_DRAW_SIGNAL(x, y, z, t) if (IsSignalPresent(tile, x)) DrawSingleSignal(tile, t, GetSingleSignalState(tile, x), y, z)
## Enumerations
enum | { SIGNAL_TO_SOUTHWEST = 0, SIGNAL_TO_NORTHEAST = 2, SIGNAL_TO_SOUTHEAST = 4, SIGNAL_TO_NORTHWEST = 6, SIGNAL_TO_EAST = 8, SIGNAL_TO_WEST = 10, SIGNAL_TO_SOUTH = 12, SIGNAL_TO_NORTH = 14 }
| Enums holding the offsets from base signal sprite, according to the side it is representing. More...
## Functions
| assert_compile (sizeof(_original_railtypes)<=sizeof(_railtypes))
void | ResetRailTypes ()
| Initialize rail type information.
void | ResolveRailTypeGUISprites (RailtypeInfo *rti)
void | InitRailTypes ()
| Resolve sprites of custom rail types.
RailType | AllocateRailType (RailTypeLabel label)
| Allocate a new rail type label.
Vehicle * | EnsureNoTrainOnTrackProc (Vehicle *v, void *data)
static bool | EnsureNoTrainOnTrack (TileIndex tile, Track track)
| Tests if a vehicle interacts with the specified track.
static bool | CheckTrackCombination (TileIndex tile, TrackBits to_build, uint flags)
Foundation | GetRailFoundation (Slope tileh, TrackBits bits)
| Checks if a track combination is valid on a specific slope and returns the needed foundation.
static CommandCost | CheckRailSlope (Slope tileh, TrackBits rail_bits, TrackBits existing, TileIndex tile)
| Tests if a track can be build on a tile.
static bool | ValParamTrackOrientation (Track track)
CommandCost | CmdBuildSingleRail (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a single piece of rail.
CommandCost | CmdRemoveSingleRail (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Remove a single piece of track.
bool | FloodHalftile (TileIndex t)
| Called from water_cmd if a non-flat rail-tile gets flooded and should be converted to shore.
static CommandCost | ValidateAutoDrag (Trackdir *trackdir, TileIndex start, TileIndex end)
static CommandCost | CmdRailTrackHelper (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a stretch of railroad tracks.
CommandCost | CmdBuildRailroadTrack (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build rail on a stretch of track.
CommandCost | CmdRemoveRailroadTrack (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build rail on a stretch of track.
CommandCost | CmdBuildTrainDepot (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a train depot.
CommandCost | CmdBuildSingleSignal (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build signals, alternate between double/single, signal/semaphore, pre/exit/combo-signals, and what-else not.
static bool | CheckSignalAutoFill (TileIndex &tile, Trackdir &trackdir, int &signal_ctr, bool remove)
static CommandCost | CmdSignalTrackHelper (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build many signals by dragging; AutoSignals.
CommandCost | CmdBuildSignalTrack (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build signals on a stretch of track.
CommandCost | CmdRemoveSingleSignal (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Remove signals.
CommandCost | CmdRemoveSignalTrack (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Remove signals on a stretch of track.
static Vehicle * | UpdateTrainPowerProc (Vehicle *v, void *data)
| Update power of train under which is the railtype being converted.
CommandCost | CmdConvertRail (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Convert one rail type to the other.
static CommandCost | RemoveTrainDepot (TileIndex tile, DoCommandFlag flags)
static CommandCost | ClearTile_Track (TileIndex tile, DoCommandFlag flags)
static uint | GetSaveSlopeZ (uint x, uint y, Track track)
| Get surface height in point (x,y) On tiles with halftile foundations move (x,y) to a safe point wrt.
static void | DrawSingleSignal (TileIndex tile, Track track, byte condition, uint image, uint pos)
static void | DrawTrackFence_NW (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_SE (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_NW_SE (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_NE (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_SW (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_NE_SW (const TileInfo *ti, SpriteID base_image)
static void | DrawTrackFence_NS_1 (const TileInfo *ti, SpriteID base_image)
| Draw fence at eastern side of track.
static void | DrawTrackFence_NS_2 (const TileInfo *ti, SpriteID base_image)
| Draw fence at western side of track.
static void | DrawTrackFence_WE_1 (const TileInfo *ti, SpriteID base_image)
| Draw fence at southern side of track.
static void | DrawTrackFence_WE_2 (const TileInfo *ti, SpriteID base_image)
| Draw fence at northern side of track.
static void | DrawTrackDetails (const TileInfo *ti, const RailtypeInfo *rti)
static void | DrawTrackSprite (SpriteID sprite, PaletteID pal, const TileInfo *ti, Slope s)
static void | DrawTrackBitsOverlay (TileInfo *ti, TrackBits track, const RailtypeInfo *rti)
static void | DrawTrackBits (TileInfo *ti, TrackBits track)
| Draw ground sprite and track bits.
static void | DrawSignals (TileIndex tile, TrackBits rails)
static void | DrawTile_Track (TileInfo *ti)
void | DrawTrainDepotSprite (int x, int y, int dir, RailType railtype)
static uint | GetSlopeZ_Track (TileIndex tile, uint x, uint y)
static Foundation | GetFoundation_Track (TileIndex tile, Slope tileh)
static void | TileLoop_Track (TileIndex tile)
static TrackStatus | GetTileTrackStatus_Track (TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
static bool | ClickTile_Track (TileIndex tile)
static void | GetTileDesc_Track (TileIndex tile, TileDesc *td)
static void | ChangeTileOwner_Track (TileIndex tile, Owner old_owner, Owner new_owner)
int | TicksToLeaveDepot (const Train *v)
| Compute number of ticks when next wagon will leave a depot.
static VehicleEnterTileStatus | VehicleEnter_Track (Vehicle *u, TileIndex tile, int x, int y)
| Tile callback routine when vehicle enters tile.
static CommandCost | TestAutoslopeOnRailTile (TileIndex tile, uint flags, uint z_old, Slope tileh_old, uint z_new, Slope tileh_new, TrackBits rail_bits)
| Tests if autoslope is allowed.
static CommandCost | TerraformTile_Track (TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
## Variables
RailtypeInfo | _railtypes [RAILTYPE_END]
static const byte | _track_sloped_sprites [14]
static const TrackBits | _valid_tracks_without_foundation [15]
| Valid TrackBits on a specific (non-steep)-slope without foundation.
static const TrackBits | _valid_tracks_on_leveled_foundation [15]
| Valid TrackBits on a specific (non-steep)-slope with leveled foundation.
static const TileIndexDiffC | _trackdelta []
static uint32 | _drawtile_track_palette
static const int | INF = 1000
static const SubSprite | _halftile_sub_sprite [4]
static const byte | _fractcoords_behind [4] = { 0x8F, 0x8, 0x80, 0xF8 }
static const byte | _fractcoords_enter [4] = { 0x8A, 0x48, 0x84, 0xA8 }
static const int8 | _deltacoord_leaveoffset [8]
const TileTypeProcs | _tile_type_rail_procs

## Detailed Description

Handling of rail tiles.

Definition in file rail_cmd.cpp.

## Enumeration Type Documentation

anonymous enum

Enums holding the offsets from base signal sprite, according to the side it is representing.

The addtion of 2 per enum is necessary in order to "jump" over the green state sprite, all signal sprites being in pair, starting with the off-red state

Definition at line 2087 of file rail_cmd.cpp.

## Function Documentation

static CommandCost CheckRailSlope | ( | Slope | tileh,
|  | TrackBits | rail_bits,
|  | TrackBits | existing,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Tests if a track can be build on a tile.

Parameters:

  | tileh | Tile slope.
  | rail_bits | Tracks to build.
  | existing | Tracks already built.
  | tile | Tile (used for water test)

Returns:
  Error message or cost for foundation building.

Definition at line 336 of file rail_cmd.cpp.

References _settings_game, ConstructionSettings::build_on_slopes, GameSettings::construction, EXPENSES_CONSTRUCTION, FOUNDATION_INVALID, FOUNDATION_NONE, GetRailFoundation(), IsSteepSlope(), IsTileType(), MP_RAILWAY, MP_WATER, RAIL_GROUND_WATER, and return_cmd_error.

Referenced by CmdBuildSingleRail(), and TestAutoslopeOnRailTile().

CommandCost CmdBuildRailroadTrack | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build rail on a stretch of track.

Stub for the unified rail builder/remover

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0-3) - railroad type normal/maglev (0 = normal, 1 = mono, 2 = maglev) p2 = (bit 4-6) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 7) - 0 = build, 1 = remove tracks
  | text | unused

Returns:
  the cost of this operation or an error

See also:
  CmdRailTrackHelper

Definition at line 787 of file rail_cmd.cpp.

References ClrBit(), and CmdRailTrackHelper().

CommandCost CmdBuildSignalTrack | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build signals on a stretch of track.

Stub for the unified signal builder/remover

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0- 2) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 3) - 1 = override signal/semaphore, or pre/exit/combo signal (CTRL-toggle) p2 = (bit 4) - 0 = signals, 1 = semaphores p2 = (bit 5) - 0 = build, 1 = remove signals p2 = (bit 6) - 0 = selected stretch, 1 = auto fill p2 = (bit 7- 9) - default signal type p2 = (bit 24-31) - user defined signals_density
  | text | unused

Returns:
  the cost of this operation or an error

See also:
  CmdSignalTrackHelper

Definition at line 1237 of file rail_cmd.cpp.

References CmdSignalTrackHelper().

CommandCost CmdBuildSingleRail | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build a single piece of rail.

Parameters:

  | tile | tile to build on
  | flags | operation to perform
  | p1 | railtype of being built piece (normal, mono, maglev)
  | p2 | rail track to build
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 366 of file rail_cmd.cpp.

References CommandCost::AddCost(), AddTrackToSignalBuffer(), AXIS_X, AXIS_Y, CheckRailSlope(), CheckTileOwnership(), CMD_CONVERT_RAIL, CMD_ERROR, CMD_LANDSCAPE_CLEAR, DC_EXEC, DoCommand(), DRD_NONE, EnsureNoTrainOnTrack(), EXPENSES_CONSTRUCTION, CommandCost::Failed(), GetDisallowedRoadDirections(), GetRailType(), GetTileSlope(), GetTileType(), GetTownIndex(), GetTrackBits(), HasBit(), HasPowerOnRail(), IsCompatibleRail(), IsPlainRail(), IsSlopeWithOneCornerRaised(), IsTileType(), MarkTileDirtyByTile(), MP_RAILWAY, MP_ROAD, MP_WATER, RAIL_GROUND_BARREN, RAIL_GROUND_WATER, RailBuildCost(), return_cmd_error, ROAD_X, ROAD_Y, ROADTYPE_ROAD, ROADTYPE_TRAM, ROADTYPES_ALL, ROADTYPES_ROAD, ROADTYPES_TRAM, SetTrackBits(), SLOPE_ENW, SLOPE_EW, SLOPE_FLAT, SLOPE_NS, SLOPE_NWS, SLOPE_SEN, SLOPE_WSE, TRACK_BIT_NONE, TRACK_X, TRACK_Y, TrackToTrackBits(), UpdateLevelCrossing(), ValParamRailtype(), and YapfNotifyTrackLayoutChange().

CommandCost CmdBuildSingleSignal | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build signals, alternate between double/single, signal/semaphore, pre/exit/combo-signals, and what-else not.

If the rail piece does not have any signals, bit 4 (cycle signal-type) is ignored

Parameters:

  | tile | tile where to build the signals
  | flags | operation to perform
  | p1 | various bitstuffed elements p1 = (bit 0-2) - track-orientation, valid values: 0-5 (Track enum) p1 = (bit 3) - 1 = override signal/semaphore, or pre/exit/combo signal or (for bit 7) toggle variant (CTRL-toggle) p1 = (bit 4) - 0 = signals, 1 = semaphores p1 = (bit 5-7) - type of the signal, for valid values see enum SignalType in rail_map.h p1 = (bit 8) - convert the present signal type and variant p1 = (bit 9-11)- start cycle from this signal type p1 = (bit 12-14)-wrap around after this signal type p1 = (bit 15-16)-cycle the signal direction this many times p1 = (bit 17) - 1 = don't modify an existing signal but don't fail either, 0 = always set new signal type
  | p2 | used for CmdBuildManySignals() to copy direction of first signal
  | text | unused

Returns:
  the cost of this operation or an error

Todo:
  p2 should be replaced by two bits for "along" and "against" the track.

Definition at line 887 of file rail_cmd.cpp.

References _settings_game, AddTrackToSignalBuffer(), CheckTileOwnership(), CMD_ERROR, Vehicle::cur_speed, Vehicle::current_order, DC_EXEC, EnsureNoTrainOnTrack(), EXPENSES_CONSTRUCTION, PathfinderSettings::forbid_90_deg, FreeTrainTrackReservation(), GB(), GetPresentSignals(), GetRailReservationTrackBits(), GetSignalStates(), GetTrackBits(), GetTrainForReservation(), Train::GetVehicleTrackdir(), HasBit(), HasReservedTracks(), HasSignalOnTrack(), HasSignals(), HasTrack(), IsPlainRailTile(), IsSafeWaitingPosition(), Order::IsType(), KillFirstBit(), MarkTileDirtyByTile(), GameSettings::pf, return_cmd_error, SetHasSignals(), SetPresentSignals(), SetSignalStates(), SIG_ELECTRIC, SIG_SEMAPHORE, SignalOnTrack(), Vehicle::tile, TRACK_BIT_HORZ, TRACK_BIT_NONE, TRACK_BIT_VERT, TrackToTrackBits(), TryPathReserve(), Vehicle::vehstatus, and YapfNotifyTrackLayoutChange().

CommandCost CmdBuildTrainDepot | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build a train depot.

Parameters:

  | tile | position of the train depot
  | flags | operation to perform
  | p1 | rail type
  | p2 | bit 0..1 entrance direction (DiagDirection)
  | text | unused

Returns:
  the cost of this operation or an error

Todo:
  When checking for the tile slope, distingush between "Flat land required" and "land sloped in wrong direction"

Definition at line 821 of file rail_cmd.cpp.

References _settings_game, CommandCost::AddCost(), AddSideToSignalBuffer(), ConstructionSettings::build_on_slopes, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_depot_pool >::CanAllocateItem(), CanBuildDepotByTileh(), CMD_ERROR, CMD_LANDSCAPE_CLEAR, GameSettings::construction, DC_EXEC, DiagDirToDiagTrack(), DoCommand(), CommandCost::Failed(), GetTileSlope(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_DIAGDIR, IsBridgeAbove(), IsSteepSlope(), MarkTileDirtyByTile(), MayHaveBridgeAbove(), return_cmd_error, SLOPE_FLAT, ValParamRailtype(), and YapfNotifyTrackLayoutChange().

CommandCost CmdConvertRail | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Convert one rail type to the other.

You can convert normal rail to monorail/maglev easily or vice-versa.

Parameters:

  | tile | end tile of rail conversion drag
  | flags | operation to perform
  | p1 | start tile of drag
  | p2 | new railtype to convert to
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 1352 of file rail_cmd.cpp.

References _settings_game, CommandCost::AddCost(), SmallVector< T, S >::Append(), CheckTileOwnership(), CMD_ERROR, CountBits(), DC_EXEC, DiagDirToDiagTrack(), VehicleSettings::disable_elrails, EXPENSES_CONSTRUCTION, FindVehicleOnPos(), FreeTrainTrackReservation(), CommandCost::GetCost(), GetOtherTunnelBridgeEnd(), GetRailDepotTrack(), GetRailStationTrack(), GetRailTileType(), GetRailType(), GetReservedTrackbits(), GetTileType(), GetTrackBits(), GetTrainForReservation(), GetTunnelBridgeDirection(), GetTunnelBridgeLength(), GetTunnelBridgeTransportType(), HasPowerOnRail(), HasStationRail(), HasTunnelBridgeReservation(), HasVehicleOnTunnelBridge(), INVALID_TRACK, InvalidateWindowData(), IsBridge(), IsCompatibleRail(), SmallVector< T, S >::Length(), MapSize(), MarkTileDirtyByTile(), MP_RAILWAY, MP_ROAD, MP_STATION, MP_TUNNELBRIDGE, RAIL_TILE_DEPOT, RailConvertCost(), RAILTYPE_ELECTRIC, RAILTYPE_RAIL, RemoveFirstTrack(), SetRailType(), Swap(), TileOffsByDiagDir(), TileX(), TileXY(), TileY(), TRACK_BIT_NONE, TRANSPORT_RAIL, TryPathReserve(), UpdateTrainPowerProc(), ValParamRailtype(), GameSettings::vehicle, and YapfNotifyTrackLayoutChange().

static CommandCost CmdRailTrackHelper | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  | `[static]`

Build a stretch of railroad tracks.

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0-3) - railroad type normal/maglev (0 = normal, 1 = mono, 2 = maglev) p2 = (bit 4-6) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 7) - 0 = build, 1 = remove tracks p2 = (bit 8) - 0 = build up to an obstacle, 1 = fail if an obstacle is found (used for AIs).
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 734 of file rail_cmd.cpp.

References CommandCost::AddCost(), CMD_BUILD_SINGLE_RAIL, CMD_ERROR, CMD_REMOVE_SINGLE_RAIL, DC_EXEC, DoCommand(), EXPENSES_CONSTRUCTION, CommandCost::Failed(), GB(), CommandCost::GetCost(), HasBit(), INVALID_STRING_ID, IsDiagonalTrackdir(), MapSize(), ToggleBit(), ToTileIndexDiff(), TrackdirToTrack(), TrackToTrackdir(), and ValParamRailtype().

Referenced by CmdBuildRailroadTrack(), and CmdRemoveRailroadTrack().

CommandCost CmdRemoveRailroadTrack | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build rail on a stretch of track.

Stub for the unified rail builder/remover

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0-3) - railroad type normal/maglev (0 = normal, 1 = mono, 2 = maglev) p2 = (bit 4-6) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 7) - 0 = build, 1 = remove tracks
  | text | unused

Returns:
  the cost of this operation or an error

See also:
  CmdRailTrackHelper

Definition at line 805 of file rail_cmd.cpp.

References CmdRailTrackHelper(), and SetBit().

CommandCost CmdRemoveSignalTrack | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Remove signals on a stretch of track.

Stub for the unified signal builder/remover

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0- 2) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 3) - 1 = override signal/semaphore, or pre/exit/combo signal (CTRL-toggle) p2 = (bit 4) - 0 = signals, 1 = semaphores p2 = (bit 5) - 0 = build, 1 = remove signals p2 = (bit 6) - 0 = selected stretch, 1 = auto fill p2 = (bit 7- 9) - default signal type p2 = (bit 24-31) - user defined signals_density
  | text | unused

Returns:
  the cost of this operation or an error

See also:
  CmdSignalTrackHelper

Definition at line 1322 of file rail_cmd.cpp.

References CmdSignalTrackHelper(), and SetBit().

CommandCost CmdRemoveSingleRail | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Remove a single piece of track.

Parameters:

  | tile | tile to remove track from
  | flags | operation to perform
  | p1 | unused
  | p2 | rail orientation
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 505 of file rail_cmd.cpp.

References CommandCost::AddCost(), AddTrackToSignalBuffer(), CheckTileOwnership(), CMD_ERROR, CMD_REMOVE_SIGNALS, DC_BANKRUPT, DC_EXEC, DoCommand(), EnsureNoTrainOnTrack(), EXPENSES_CONSTRUCTION, FreeTrainTrackReservation(), GetRailReservationTrackBits(), GetTileOwner(), GetTileSlope(), GetTileType(), GetTownIndex(), GetTrackBits(), GetTrainForReservation(), HasReservedTracks(), HasSignalOnTrack(), INVALID_OWNER, IsPlainRail(), IsSlopeWithOneCornerRaised(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MarkTileDirtyByTile(), MP_RAILWAY, MP_ROAD, OWNER_WATER, RAIL_GROUND_WATER, ROADTYPE_ROAD, ROADTYPE_TRAM, SetTrackBits(), SetTrackReservation(), TRACK_BIT_X, TRACK_BIT_Y, TRACK_X, TRACK_Y, TrackToTrackBits(), TryPathReserve(), and YapfNotifyTrackLayoutChange().

CommandCost CmdRemoveSingleSignal | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Remove signals.

Parameters:

  | tile | coordinates where signal is being deleted from
  | flags | operation to perform
  | p1 | various bitstuffed elements, only track information is used (bit 0- 2) - track-orientation, valid values: 0-5 (Track enum) (bit 3) - override signal/semaphore, or pre/exit/combo signal (CTRL-toggle) (bit 4) - 0 = signals, 1 = semaphores
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 1253 of file rail_cmd.cpp.

References AddTrackToSignalBuffer(), CheckTileOwnership(), CMD_ERROR, DC_EXEC, EnsureNoTrainOnTrack(), EXPENSES_CONSTRUCTION, GB(), GetPresentSignals(), GetReservedTrackbits(), GetTileOwner(), GetTrainForReservation(), HasReservedTracks(), HasSignalOnTrack(), HasSignalOnTrackdir(), HasTrack(), INVALID_TRACK, IsPlainRailTile(), MarkTileDirtyByTile(), OWNER_WATER, ReverseTrackdir(), SetHasSignals(), SetPresentSignals(), SetSignalStates(), SIG_ELECTRIC, SignalOnTrack(), TileAddByDiagDir(), TrackBitsToTrack(), TrackdirBitsToTrackBits(), TrackdirReachesTrackdirs(), TrackdirToExitdir(), TrackToTrackBits(), TrackToTrackdir(), TryPathReserve(), and YapfNotifyTrackLayoutChange().

static CommandCost CmdSignalTrackHelper | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  | `[static]`

Build many signals by dragging; AutoSignals.

Parameters:

  | tile | start tile of drag
  | flags | operation to perform
  | p1 | end tile of drag
  | p2 | various bitstuffed elements p2 = (bit 0- 2) - track-orientation, valid values: 0-5 (Track enum) p2 = (bit 3) - 1 = override signal/semaphore, or pre/exit/combo signal (CTRL-toggle) p2 = (bit 4) - 0 = signals, 1 = semaphores p2 = (bit 5) - 0 = build, 1 = remove signals p2 = (bit 6) - 0 = selected stretch, 1 = auto fill p2 = (bit 7- 9) - default signal type p2 = (bit 24-31) - user defined signals_density
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 1110 of file rail_cmd.cpp.

References CommandCost::AddCost(), CMD_BUILD_SIGNALS, CMD_ERROR, CMD_REMOVE_SIGNALS, DoCommand(), EXPENSES_CONSTRUCTION, GB(), GetPresentSignals(), HasBit(), HasSignalOnTrack(), HasTrack(), IsDiagonalTrackdir(), IsPlainRailTile(), MapSize(), SB(), SetBit(), SIG_ELECTRIC, SignalAgainstTrackdir(), SignalAlongTrackdir(), SignalOnTrack(), SIGTYPE_NORMAL, SIGTYPE_PBS, CommandCost::Succeeded(), ToggleBit(), ToTileIndexDiff(), TrackdirToTrack(), and TrackToTrackdir().

Referenced by CmdBuildSignalTrack(), and CmdRemoveSignalTrack().

static void DrawTrackBits | ( | TileInfo * | ti,
|  | TrackBits | track |
| ) |  |  | `[static]`

Draw ground sprite and track bits.

Parameters:

  | ti | TileInfo
  | track | TrackBits to draw

Definition at line 1938 of file rail_cmd.cpp.

References _settings_client, RailtypeInfo::base_sprites, ComplementSlope(), CornerToTrackBits(), DrawFoundation(), DrawGroundSprite(), FOUNDATION_NONE, FOUNDATION_STEEP_BOTH, FOUNDATION_STEEP_LOWER, GetHalftileFoundationCorner(), GetHighestSlopeCorner(), GetRailFoundation(), GetRailReservationTrackBits(), GetRailType(), GetRailTypeInfo(), RailtypeInfo::ground, ClientSettings::gui, HalftileFoundation(), HasReservedTracks(), IsNonContinuousFoundation(), IsSteepSlope(), IsValidCorner(), OppositeCorner(), RAIL_GROUND_BARREN, RAIL_GROUND_HALF_SNOW, RAIL_GROUND_ICE_DESERT, RAIL_GROUND_WATER, GUISettings::show_track_reservation, RailtypeInfo::single_e, RailtypeInfo::single_n, RailtypeInfo::single_s, RailtypeInfo::single_sloped, RailtypeInfo::single_w, RailtypeInfo::single_x, RailtypeInfo::single_y, SLOPE_E, SLOPE_ELEVATED, SLOPE_FLAT, SLOPE_N, SLOPE_S, SLOPE_W, SlopeWithThreeCornersRaised(), RailtypeInfo::snow_offset, TileInfo::tile, TILE_HEIGHT, TileInfo::tileh, TRACK_BIT_CROSS, TRACK_BIT_HORZ, TRACK_BIT_LEFT, TRACK_BIT_VERT, RailtypeInfo::track_ns, and RailtypeInfo::track_y.

static bool EnsureNoTrainOnTrack | ( | TileIndex | tile,
|  | Track | track |
| ) |  |  | `[static]`

Tests if a vehicle interacts with the specified track.

All track bits interact except parallel TRACK_BIT_HORZ or TRACK_BIT_VERT.

Parameters:

  | tile | The tile.
  | track | The track.

Definition at line 168 of file rail_cmd.cpp.

References HasVehicleOnPos(), and TrackToTrackBits().

Referenced by CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdRemoveSingleRail(), and CmdRemoveSingleSignal().

bool FloodHalftile | ( | TileIndex | t | ) |

Called from water_cmd if a non-flat rail-tile gets flooded and should be converted to shore.

The function floods the lower halftile, if the tile has a halftile foundation.

Parameters:

  | t | The tile to flood.

Returns:
  true if something was flooded.

Definition at line 618 of file rail_cmd.cpp.

References ApplyFoundationToSlope(), CMD_REMOVE_SINGLE_RAIL, CornerToTrackBits(), DC_EXEC, DoCommand(), FIND_FIRST_BIT, GetHighestSlopeCorner(), GetRailFoundation(), GetTileSlope(), GetTrackBits(), IsNonContinuousFoundation(), IsPlainRailTile(), IsSlopeWithOneCornerRaised(), IsSlopeWithThreeCornersRaised(), IsSteepSlope(), MarkTileDirtyByTile(), OppositeCorner(), OWNER_WATER, and RAIL_GROUND_WATER.

Referenced by DoFloodTile().

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 256 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().

static uint GetSaveSlopeZ | ( | uint | x,
|  | uint | y,
|  | Track | track |
| ) |  |  | `[static]`

Get surface height in point (x,y) On tiles with halftile foundations move (x,y) to a safe point wrt.

track

Definition at line 1601 of file rail_cmd.cpp.

References TRACK_LEFT, TRACK_LOWER, TRACK_RIGHT, and TRACK_UPPER.

void ResetRailTypes | ( |  | ) |

Initialize rail type information.

Reset all rail type information to its default values.

Definition at line 51 of file rail_cmd.cpp.

References _original_railtypes.

Referenced by ResetNewGRFData().

static CommandCost TestAutoslopeOnRailTile | ( | TileIndex | tile,
|  | uint | flags,
|  | uint | z_old,
|  | Slope | tileh_old,
|  | uint | z_new,
|  | Slope | tileh_new,
|  | TrackBits | rail_bits |
| ) |  |  | `[static]`

Tests if autoslope is allowed.

Parameters:

  | tile | The tile.
  | flags | Terraform command flags.
  | z_old | Old TileZ.
  | tileh_old | Old TileSlope.
  | z_new | New TileZ.
  | tileh_new | New TileSlope.
  | rail_bits | Trackbits.

Definition at line 2700 of file rail_cmd.cpp.

References _settings_game, CommandCost::AddCost(), ApplyFoundationToSlope(), AutoslopeEnabled(), ConstructionSettings::build_on_slopes, CheckRailSlope(), CMD_ERROR, GameSettings::construction, EXPENSES_CONSTRUCTION, GetRailFoundation(), GetSlopeZInCorner(), IsSlopeWithOneCornerRaised(), RAIL_GROUND_BARREN, RAIL_GROUND_WATER, RemoveHalftileSlope(), and TRACK_BIT_NONE.

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 2621 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().

static VehicleEnterTileStatus VehicleEnter_Track | ( | Vehicle * | u,
|  | TileIndex | tile,
|  | int | x,
|  | int | y |
| ) |  |  | `[static]`

Tile callback routine when vehicle enters tile.

See also:
  vehicle_enter_tile_proc

Definition at line 2639 of file rail_cmd.cpp.

References AXIS_X, TrainCache::cached_veh_length, DiagDirToAxis(), DiagDirToDir(), SpecializedVehicle< T, Type >::First(), SpecializedVehicle< Train, VEH_TRAIN >::From(), GetRailDepotDirection(), InvalidateWindowData(), IsRailDepotTile(), SpecializedVehicle< T, Type >::Next(), ReverseDiagDir(), ReverseDir(), Vehicle::tile, TRACK_BIT_DEPOT, BaseVehicle::type, VEH_TRAIN, Vehicle::vehstatus, VETSB_CANNOT_ENTER, VETSB_CONTINUE, and VETSB_ENTERED_WORMHOLE.

## Variable Documentation

const int8 _deltacoord_leaveoffset[8]`[static]`

Initial value:

     {
      -1,  0,  1,  0,
       0,  1,  0, -1
    }

Definition at line 2610 of file rail_cmd.cpp.

const SubSprite _halftile_sub_sprite[4]`[static]`

Initial value:

     {
      { -INF    , -INF  , 32 - 33, INF     },
      { -INF    ,  0 + 7, INF    , INF     },
      { -31 + 33, -INF  , INF    , INF     },
      { -INF    , -INF  , INF    , 30 - 23 }
    }

Definition at line 1778 of file rail_cmd.cpp.

const TileTypeProcs _tile_type_rail_procs

Initial value:

     {
      DrawTile_Track,
      GetSlopeZ_Track,
      ClearTile_Track,
      NULL,
      GetTileDesc_Track,
      GetTileTrackStatus_Track,
      ClickTile_Track,
      NULL,
      TileLoop_Track,
      ChangeTileOwner_Track,
      NULL,
      VehicleEnter_Track,
      GetFoundation_Track,
      TerraformTile_Track,
    }

const byte _track_sloped_sprites[14]`[static]`

Initial value:

     {
      14, 15, 22, 13,
       0, 21, 17, 12,
      23,  0, 18, 20,
      19, 16
    }

Definition at line 108 of file rail_cmd.cpp.

const TileIndexDiffC _trackdelta[]`[static]`

Initial value:

     {
      { -1,  0 }, {  0,  1 }, { -1,  0 }, {  0,  1 }, {  1,  0 }, {  0,  1 },
      {  0,  0 },
      {  0,  0 },
      {  1,  0 }, {  0, -1 }, {  0, -1 }, {  1,  0 }, {  0, -1 }, { -1,  0 },
      {  0,  0 },
      {  0,  0 }
    }

Definition at line 662 of file rail_cmd.cpp.

const TrackBits _valid_tracks_on_leveled_foundation[15]`[static]`

Initial value:

     {
      TRACK_BIT_NONE,
      TRACK_BIT_LEFT,
      TRACK_BIT_LOWER,
      TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_LEFT,

      TRACK_BIT_RIGHT,
      TRACK_BIT_ALL,
      TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,
      TRACK_BIT_ALL,

      TRACK_BIT_UPPER,
      TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_LEFT,
      TRACK_BIT_ALL,
      TRACK_BIT_ALL,

      TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,
      TRACK_BIT_ALL,
      TRACK_BIT_ALL
    }

Valid TrackBits on a specific (non-steep)-slope with leveled foundation.

Definition at line 228 of file rail_cmd.cpp.

const TrackBits _valid_tracks_without_foundation[15]`[static]`

Initial value:

     {
      TRACK_BIT_ALL,
      TRACK_BIT_RIGHT,
      TRACK_BIT_UPPER,
      TRACK_BIT_X,

      TRACK_BIT_LEFT,
      TRACK_BIT_NONE,
      TRACK_BIT_Y,
      TRACK_BIT_LOWER,

      TRACK_BIT_LOWER,
      TRACK_BIT_Y,
      TRACK_BIT_NONE,
      TRACK_BIT_LEFT,

      TRACK_BIT_X,
      TRACK_BIT_UPPER,
      TRACK_BIT_RIGHT,
    }

Valid TrackBits on a specific (non-steep)-slope without foundation.

Definition at line 206 of file rail_cmd.cpp.