# train_cmd.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "bridge_map.h"`
`#include "debug.h"`
`#include "tile_cmd.h"`
`#include "landscape.h"`
`#include "gui.h"`
`#include "station_map.h"`
`#include "tunnel_map.h"`
`#include "timetable.h"`
`#include "articulated_vehicles.h"`
`#include "command_func.h"`
`#include "pathfind.h"`
`#include "npf.h"`
`#include "station.h"`
`#include "news.h"`
`#include "engine.h"`
`#include "player_func.h"`
`#include "player_base.h"`
`#include "depot.h"`
`#include "waypoint.h"`
`#include "vehicle_gui.h"`
`#include "train.h"`
`#include "bridge.h"`
`#include "newgrf_callbacks.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "newgrf_text.h"`
`#include "direction_func.h"`
`#include "yapf/yapf.h"`
`#include "cargotype.h"`
`#include "group.h"`
`#include "table/sprites.h"`
`#include "tunnelbridge_map.h"`
`#include "strings_func.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "signal_func.h"`
`#include "variables.h"`
`#include "autoreplace_gui.h"`
`#include "gfx_func.h"`
`#include "settings_type.h"`
`#include "network/network.h"`
`#include "table/strings.h"`
`#include "table/train_cmd.h"`

Go to the source code of this file.

## Data Structures
struct | TrainFindDepotData
struct | TrainTrackFollowerData
struct | RailtypeSlowdownParams
struct | TrainCollideChecker
## Defines
#define | MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
#define | MAX_ACCEPTABLE_DEPOT_DIST 16
## Enumerations
enum | AccelType { AM_ACCEL, AM_BRAKE }
## Functions
static bool | TrainCheckIfLineEnds (Vehicle *v)
| Checks for line end.
static void | TrainController (Vehicle *v, Vehicle *nomove, bool update_image)
static TileIndex | TrainApproachingCrossingTile (const Vehicle *v)
| Determines whether train is approaching a rail-road crossing (thus making it barred).
static DiagDirection | TrainExitDir (Direction direction, TrackBits track)
| Determine the side in which the train will leave the tile.
byte | FreightWagonMult (CargoID cargo)
| Return the cargo weight multiplier to use for a rail vehicle.
void | TrainPowerChanged (Vehicle *v)
| Recalculates the cached total power of a train.
static void | TrainCargoChanged (Vehicle *v)
| Recalculates the cached weight of a train and its vehicles.
static void | RailVehicleLengthChanged (const Vehicle *u)
| Logs a bug in GRF and shows a warning message if this is for the first time this happened.
void | CheckTrainsLengths ()
| Checks if lengths of all rail vehicles are valid.
void | TrainConsistChanged (Vehicle *v, bool same_length)
| Recalculates the cached stuff of a train.
static bool | TrainShouldStop (const Vehicle *v, TileIndex tile)
static int | GetTrainAcceleration (Vehicle *v, bool mode)
| new acceleration
void | UpdateTrainAcceleration (Vehicle *v)
void | DrawTrainEngine (int x, int y, EngineID engine, SpriteID pal)
static CommandCost | CmdBuildRailWagon (EngineID engine, TileIndex tile, uint32 flags)
static void | NormalizeTrainVehInDepot (const Vehicle *u)
| Move all free vehicles in the depot to the train.
static CommandCost | EstimateTrainCost (EngineID engine, const RailVehicleInfo *rvi)
static void | AddRearEngineToMultiheadedTrain (Vehicle *v, Vehicle *u, bool building)
CommandCost | CmdBuildRailVehicle (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Build a railroad vehicle.
int | CheckTrainInDepot (const Vehicle *v, bool needs_to_be_stopped)
int | CheckTrainStoppedInDepot (const Vehicle *v)
bool | CheckTrainIsInsideDepot (const Vehicle *v)
static Vehicle * | UnlinkWagon (Vehicle *v, Vehicle *first)
| Unlink a rail wagon from the consist.
static Vehicle * | FindGoodVehiclePos (const Vehicle *src)
static void | AddWagonToConsist (Vehicle *v, Vehicle *dest)
static void | NormaliseTrainConsist (Vehicle *v)
CommandCost | CmdMoveRailVehicle (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Move a rail vehicle around inside the depot.
CommandCost | CmdStartStopTrain (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Start/Stop a train.
CommandCost | CmdSellRailWagon (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Sell a (single) train wagon/engine.
static void | UpdateVarsAfterSwap (Vehicle *v)
static void | SetLastSpeed (Vehicle *v, int spd)
static void | SwapTrainFlags (byte *swap_flag1, byte *swap_flag2)
static void | ReverseTrainSwapVeh (Vehicle *v, int l, int r)
static void * | TrainOnTileEnum (Vehicle *v, void *)
| Check if the vehicle is a train.
static void * | TrainApproachingCrossingEnum (Vehicle *v, void *data)
| Checks if a train is approaching a rail-road crossing.
static bool | TrainApproachingCrossing (TileIndex tile)
| Finds a vehicle approaching rail-road crossing.
void | UpdateLevelCrossing (TileIndex tile, bool sound)
| Sets correct crossing state.
static void | MaybeBarCrossingWithSound (TileIndex tile)
| Bars crossing and plays ding-ding sound if not barred already.
static void | AdvanceWagonsBeforeSwap (Vehicle *v)
| Advances wagons for train reversing, needed for variable length wagons.
static void | AdvanceWagonsAfterSwap (Vehicle *v)
| Advances wagons for train reversing, needed for variable length wagons.
static void | ReverseTrainDirection (Vehicle *v)
CommandCost | CmdReverseTrainDirection (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Reverse train.
CommandCost | CmdForceTrainProceed (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Force a train through a red signal.
CommandCost | CmdRefitRailVehicle (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Refits a train to the specified cargo type.
static bool | NtpCallbFindDepot (TileIndex tile, TrainFindDepotData *tfdd, int track, uint length)
static TrainFindDepotData | FindClosestTrainDepot (Vehicle *v, int max_distance)
| returns the tile of a depot to goto to.
CommandCost | CmdSendTrainToDepot (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Send a train to a depot.
void | OnTick_Train ()
static void | HandleLocomotiveSmokeCloud (const Vehicle *v)
static bool | CheckTrainStayInDepot (Vehicle *v)
static bool | NtpCallbFindStation (TileIndex tile, TrainTrackFollowerData *ttfd, Trackdir track, uint length)
static void | FillWithStationData (TrainTrackFollowerData *fd, const Vehicle *v)
static Track | ChooseTrainTrack (Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
static bool | CheckReverseTrain (Vehicle *v)
static bool | ProcessTrainOrder (Vehicle *v)
static int | UpdateTrainSpeed (Vehicle *v)
static void | TrainEnterStation (Vehicle *v, StationID station)
static byte | AfterSetTrainPos (Vehicle *v, bool new_tile)
static Direction | GetNewVehicleDirectionByTile (TileIndex new_tile, TileIndex old_tile)
static int | GetDirectionToVehicle (const Vehicle *v, int x, int y)
static bool | CheckCompatibleRail (const Vehicle *v, TileIndex tile)
static void | AffectSpeedByDirChange (Vehicle *v, Direction new_dir)
| Modify the speed of the vehicle due to a turn.
static void | AffectSpeedByZChange (Vehicle *v, byte old_z)
| Modify the speed of the vehicle due to a change in altitude.
static void | TrainMovedChangeSignals (TileIndex tile, DiagDirection dir)
static void | SetVehicleCrashed (Vehicle *v)
static uint | CountPassengersInTrain (const Vehicle *v)
static void * | FindTrainCollideEnum (Vehicle *v, void *data)
static void | CheckTrainCollision (Vehicle *v)
| Checks whether the specified train has a collision with another vehicle.
static void * | CheckVehicleAtSignal (Vehicle *v, void *data)
static void | DeleteLastWagon (Vehicle *v)
| Deletes/Clears the last wagon of a crashed train.
static void | ChangeTrainDirRandomly (Vehicle *v)
static void | HandleCrashedTrain (Vehicle *v)
static void | HandleBrokenTrain (Vehicle *v)
static bool | TrainApproachingLineEnd (Vehicle *v, bool signal)
| Train is approaching line end, slow down and possibly reverse.
static bool | TrainCanLeaveTile (const Vehicle *v)
| Determines whether train would like to leave the tile.
static void | TrainLocoHandler (Vehicle *v, bool mode)
static void | CheckIfTrainNeedsService (Vehicle *v)
void | TrainsYearlyLoop ()
void | InitializeTrains ()
void | ConnectMultiheadedTrains ()
void | ConvertOldMultiheadToNew ()
| Converts all trains to the new subtype format introduced in savegame 16.2 It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found.
## Variables
static const byte | _vehicle_initial_x_fract [4] = {10, 8, 4, 8}
static const byte | _vehicle_initial_y_fract [4] = { 8, 4, 8, 10}
static const int8 | _vehicle_smoke_pos [8]
static const byte | _initial_tile_subcoord [6][4][3]
static const uint32 | _reachable_tracks [4]
static const byte | _search_directions [6][4]
static const byte | _pick_track_table [6] = {1, 3, 2, 2, 0, 0}
static const Direction | _new_vehicle_direction_table [11]
static const RailtypeSlowdownParams | _railtype_slowdown []
static const DiagDirection | _otherside_signal_directions []
static const uint16 | _breakdown_speeds [16]
| Maximum speeds for train that is broken down or approaching line end.

## Detailed Description

Definition in file train_cmd.cpp.

## Function Documentation

static void AdvanceWagonsAfterSwap | ( | Vehicle * | v | ) | `[static]`

Advances wagons for train reversing, needed for variable length wagons.

This one is called after the train is reversed.

Parameters:

  | v | First vehicle in chain

Definition at line 1831 of file train_cmd.cpp.

References AxisToTrackBits(), DiagDirToAxis(), GetRailDepotDirection(), TicksToLeaveDepot(), and TRACK_BIT_DEPOT.

Referenced by ReverseTrainDirection().

static void AdvanceWagonsBeforeSwap | ( | Vehicle * | v | ) | `[static]`

Advances wagons for train reversing, needed for variable length wagons.

This one is called before the train is reversed.

Parameters:

  | v | First vehicle in chain

Definition at line 1803 of file train_cmd.cpp.

Referenced by ReverseTrainDirection().

static void CheckTrainCollision | ( | Vehicle * | v | ) | `[static]`

Checks whether the specified train has a collision with another vehicle.

If so, destroys this vehicle, and the other vehicle if its subtype has TS_Front. Reports the incident in a flashy news item, modifies station ratings and plays a sound.

Definition at line 3080 of file train_cmd.cpp.

References AddNewsItem(), FindVehicleOnPos(), FindVehicleOnPosXY(), GetOtherTunnelBridgeEnd(), NEWS_FLAGS, NF_VEHICLE, NF_VIEWPORT, NM_THIN, NT_ACCIDENT, TRACK_BIT_DEPOT, and TRACK_BIT_WORMHOLE.

void CheckTrainsLengths | ( |  | ) |

Checks if lengths of all rail vehicles are valid.

If not, shows an error message.

Definition at line 214 of file train_cmd.cpp.

References _networking, abs(), max(), TicksToLeaveDepot(), and TRACK_BIT_DEPOT.

Referenced by InitializeWindowsAndCaches(), and ReloadNewGRFData().

CommandCost CmdBuildRailVehicle | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Build a railroad vehicle.

Parameters:

  | tile | tile of the depot where rail-vehicle is built
  | flags | type of operation
  | p1 | engine type id
  | p2 | bit 1 prevents any free cars from being added to the train

Definition at line 766 of file train_cmd.cpp.

References CMD_ERROR, CmdBuildRailWagon(), DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DEFAULT_GROUP, DiagDirToDir(), ENGINE_EXCLUSIVE_PREVIEW, CommandCost::GetCost(), GetRailDepotDirection(), GetRailType(), HasBit(), HasPowerOnRail(), InvalidateAutoreplaceWindow(), IsEngineBuildable(), IsTileDepotType(), IsTileOwner(), NormalizeTrainVehInDepot(), RAILVEH_MULTIHEAD, RAILVEH_WAGON, return_cmd_error, SetBit(), SetFrontEngine(), SetMultiheaded(), SetTrainEngine(), TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, Train::Train(), TrainConsistChanged(), UpdateTrainGroupID(), and VehicleRandomBits().

static CommandCost CmdBuildRailWagon | ( | EngineID | engine,
|  | TileIndex | tile,
|  | uint32 | flags |
| ) |  |  | `[static]`

do not connect new wagon with crashed/flooded consists

Definition at line 619 of file train_cmd.cpp.

References DC_EXEC, DC_QUERY_COST, DEFAULT_GROUP, DiagDirToDir(), CommandCost::GetCost(), GetRailDepotDirection(), HASBITS, InvalidateAutoreplaceWindow(), IsFreeWagon(), return_cmd_error, SetFreeWagon(), SetTrainWagon(), TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, Train::Train(), TrainConsistChanged(), UpdateTrainGroupID(), and VehicleRandomBits().

Referenced by CmdBuildRailVehicle().

CommandCost CmdForceTrainProceed | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Force a train through a red signal.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | train to ignore the red signal
  | p2 | unused

Definition at line 1987 of file train_cmd.cpp.

References CMD_ERROR, DC_EXEC, and IsValidVehicleID().

CommandCost CmdMoveRailVehicle | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Move a rail vehicle around inside the depot.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | various bitstuffed elements p1 (bit 0 - 15) source vehicle index p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
  | p2 | (bit 0) move all vehicles following the source vehicle

Definition at line 1023 of file train_cmd.cpp.

References CALLBACK_FAILED, CBID_TRAIN_ALLOW_WAGON_ATTACH, ClearFreeWagon(), ClearFrontEngine(), CMD_ERROR, CmdMoveRailVehicle(), DC_AUTOREPLACE, DC_EXEC, DEFAULT_GROUP, DeleteVehicleOrders(), error, GB(), GetEngineGRFID(), GetGRFStringID(), GetLastEnginePart(), GetNextUnit(), GetNextVehicle(), GetVehicleCallbackParent(), HasBit(), HASBITS, IsArticulatedPart(), IsFreeWagon(), IsFrontEngine(), IsRearDualheaded(), IsTrainEngine(), IsValidVehicleID(), RemoveVehicleFromGroup(), return_cmd_error, SetFreeWagon(), SetFrontEngine(), SetTrainGroupID(), TrainConsistChanged(), UnlinkWagon(), and UpdateTrainGroupID().

Referenced by CmdMoveRailVehicle().

CommandCost CmdRefitRailVehicle | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Refits a train to the specified cargo type.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID of the train to refit param p2 various bitstuffed elements p2 = (bit 0-7) - the new cargo type to refit to p2 = (bit 8-15) - the new cargo subtype to refit to p2 = (bit 16) - refit only this vehicle

Returns:
  cost of refit or error

Definition at line 2010 of file train_cmd.cpp.

References CommandCost::AddCost(), CALLBACK_FAILED, CanRefitTo(), CBID_VEHICLE_REFIT_CAPACITY, CBM_VEHICLE_REFIT_CAPACITY, CMD_ERROR, DC_EXEC, GB(), GetRefitCost(), GetVehicleCallback(), HasBit(), IsValidVehicleID(), return_cmd_error, and TrainConsistChanged().

CommandCost CmdReverseTrainDirection | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Reverse train.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | train to reverse
  | p2 | if true, reverse a unit in a train (needs to be in a depot)

Definition at line 1937 of file train_cmd.cpp.

References CBM_VEHICLE_ARTIC_ENGINE, CMD_ERROR, DC_EXEC, HasBit(), IsMultiheaded(), IsValidVehicleID(), return_cmd_error, ReverseTrainDirection(), and ToggleBit().

CommandCost CmdSellRailWagon | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Sell a (single) train wagon/engine.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | the wagon/engine index
  | p2 | the selling mode p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines) p2 = 1: sell the vehicle and all vehicles following it in the chain if the wagon is dragged, don't delete the possibly belonging rear-engine to some front p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines; all wagons of the same type will go on the same line. Used by the AI currently

Definition at line 1409 of file train_cmd.cpp.

References CommandCost::AddCost(), CMD_ERROR, CMD_MOVE_RAIL_VEHICLE, DC_EXEC, DEFAULT_GROUP, DeleteDepotHighlightOfVehicle(), DoCommand(), GetNextVehicle(), HasBit(), HASBITS, IsArticulatedPart(), IsFrontEngine(), IsMultiheaded(), IsRearDualheaded(), IsTrainEngine(), IsValidVehicleID(), return_cmd_error, SetFrontEngine(), ShowVehicleViewWindow(), TrainConsistChanged(), TS_FRONT, UnlinkWagon(), and UpdateTrainGroupID().

CommandCost CmdSendTrainToDepot | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Send a train to a depot.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | train to send to the depot
  | p2 | various bitmasked elements p2 bit 0-3 - DEPOT_ flags (see vehicle.h) p2 bit 8-10 - VLW flag (for mass goto depot)

Definition at line 2194 of file train_cmd.cpp.

References ClrBit(), CMD_ERROR, CMD_REVERSE_TRAIN_DIRECTION, DC_EXEC, DoCommand(), FindClosestTrainDepot(), GetDepotByTile(), HasBit(), IsTileDepotType(), IsValidVehicleID(), OFB_NON_STOP, return_cmd_error, SendAllVehiclesToDepot(), SetBit(), and ToggleBit().

CommandCost CmdStartStopTrain | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Start/Stop a train.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | train to start/stop
  | p2 | unused

Definition at line 1368 of file train_cmd.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_START_STOP_CHECK, CMD_ERROR, DC_EXEC, DeleteVehicleNews(), error, GB(), GetEngineGRFID(), GetGRFStringID(), GetVehicleCallback(), IsValidVehicleID(), return_cmd_error, and TRACK_BIT_DEPOT.

static void DeleteLastWagon | ( | Vehicle * | v | ) | `[static]`

Deletes/Clears the last wagon of a crashed train.

It takes the engine of the train, then goes to the last wagon and deletes that. Each call to this function will remove the last wagon of a crashed train. If this wagon was on a crossing, or inside a tunnel/bridge, recalculate the signals as they might need updating

Parameters:

  | v | the Vehicle of which last wagon is to be removed

Definition at line 3338 of file train_cmd.cpp.

References FIND_FIRST_BIT, INVALID_DIAGDIR, IsTileDepotType(), IsTileType(), MarkSingleVehicleDirty(), MP_TUNNELBRIDGE, SetSignalsOnBothDir(), TRACK_BIT_DEPOT, TrainConsistChanged(), UpdateLevelCrossing(), and UpdateSignalsOnSegment().

static TrainFindDepotData FindClosestTrainDepot | ( | Vehicle * | v,
|  | int | max_distance |
| ) |  |  | `[static]`

returns the tile of a depot to goto to.

The given vehicle must not be crashed!

Definition at line 2128 of file train_cmd.cpp.

References GetVehicleTrackdir(), INVALID_TRACKDIR, IsTileDepotType(), NewTrainPathfind(), NPF_FLAG_REVERSE, NPF_INFINITE_PENALTY, NPFGetFlag(), ReverseDir(), ReverseTrackdir(), TrainExitDir(), VPF_NPF, VPF_NTP, VPF_YAPF, and YapfFindNearestRailDepotTwoWay().

Referenced by CmdSendTrainToDepot().

byte FreightWagonMult | ( | CargoID | cargo | ) |

Return the cargo weight multiplier to use for a rail vehicle.

Parameters:

  | cargo | Cargo type to get multiplier for

Returns:
  Cargo weight multiplier

Definition at line 89 of file train_cmd.cpp.

Referenced by TrainCargoChanged().

static void MaybeBarCrossingWithSound | ( | TileIndex | tile | ) | `[inline, static]`

Bars crossing and plays ding-ding sound if not barred already.

Parameters:

  | tile | tile with crossing

Precondition:
  tile is a rail-road crossing

Definition at line 1788 of file train_cmd.cpp.

References MarkTileDirtyByTile().

Referenced by ReverseTrainDirection(), and TrainCheckIfLineEnds().

static bool ProcessTrainOrder | ( | Vehicle * | v | ) | `[static]`

Reversing because of order change is allowed only just after leaving a station (and the difficulty setting to allowed, of course) this can be detected because only after OT_LEAVESTATION, current_order will be reset to nothing. (That also happens if no order, but in that case it won't hit the point in code where may_reverse is checked)

Definition at line 2704 of file train_cmd.cpp.

References InvalidateVehicleOrder(), IsTileType(), MP_STATION, OFB_NON_STOP, OFB_PART_OF_ORDERS, and OFB_SERVICE_IF_NEEDED.

static void RailVehicleLengthChanged | ( | const Vehicle * | u | ) | `[static]`

Logs a bug in GRF and shows a warning message if this is for the first time this happened.

Parameters:

  | u | first vehicle of chain

Definition at line 185 of file train_cmd.cpp.

References _networking, GBUG_VEH_LENGTH, HasBit(), SetBit(), and SetDParamStr().

Referenced by TrainConsistChanged().

static void ReverseTrainDirection | ( | Vehicle * | v | ) | `[static]`

< number of vehicles - 1

Definition at line 1885 of file train_cmd.cpp.

References AdvanceWagonsAfterSwap(), AdvanceWagonsBeforeSwap(), ClrBit(), INVALID_TILE, IsTileDepotType(), MaybeBarCrossingWithSound(), ToggleBit(), TrainApproachingCrossingTile(), TrainConsistChanged(), and UpdateLevelCrossing().

Referenced by CmdReverseTrainDirection(), and TrainApproachingLineEnd().

static bool TrainApproachingCrossing | ( | TileIndex | tile | ) | `[static]`

Finds a vehicle approaching rail-road crossing.

Parameters:

  | tile | tile to test

Returns:
  true if a vehicle is approaching the crossing

Precondition:
  tile is a rail-road crossing

Definition at line 1744 of file train_cmd.cpp.

References AxisToDiagDir(), HasVehicleOnPos(), ReverseDiagDir(), TileOffsByDiagDir(), and TrainApproachingCrossingEnum().

Referenced by UpdateLevelCrossing().

static void* TrainApproachingCrossingEnum | ( | Vehicle * | v,
|  | void * | data |
| ) |  |  | `[static]`

Checks if a train is approaching a rail-road crossing.

Parameters:

  | v | vehicle on tile
  | data | tile with crossing we are testing

Returns:
  v if it is approaching a crossing, NULL otherwise

Definition at line 1725 of file train_cmd.cpp.

References IsFrontEngine(), and TrainApproachingCrossingTile().

Referenced by TrainApproachingCrossing().

static TileIndex TrainApproachingCrossingTile | ( | const Vehicle * | v | ) | `[static]`

Determines whether train is approaching a rail-road crossing (thus making it barred).

Parameters:

  | v | front engine of train

Returns:
  TileIndex of crossing the train is approaching, else INVALID_TILE

Precondition:
  v in non-crashed front engine

Definition at line 3557 of file train_cmd.cpp.

References DiagDirToAxis(), INVALID_TILE, IsFrontEngine(), TileOffsByDiagDir(), TrainCanLeaveTile(), and TrainExitDir().

Referenced by ReverseTrainDirection(), and TrainApproachingCrossingEnum().

static bool TrainApproachingLineEnd | ( | Vehicle * | v,
|  | bool | signal |
| ) |  |  | `[static]`

Train is approaching line end, slow down and possibly reverse.

Parameters:

  | v | front train engine
  | signal | not line end, just a red signal

Returns:
  true iff we did NOT have to reverse

Definition at line 3486 of file train_cmd.cpp.

References DIR_E, DIR_N, DIR_NE, DIR_NW, DIR_S, DIR_SE, DIR_W, ReverseTrainDirection(), and TILE_SIZE.

Referenced by TrainCheckIfLineEnds().

static bool TrainCanLeaveTile | ( | const Vehicle * | v | ) | `[static]`

Determines whether train would like to leave the tile.

Parameters:

  | v | train to test

Returns:
  true iff vehicle is NOT entering or inside a depot or tunnel/bridge

Definition at line 3527 of file train_cmd.cpp.

References DiagDirToDir(), GetRailDepotDirection(), GetTunnelBridgeDirection(), IsTileDepotType(), IsTileType(), MP_TUNNELBRIDGE, ReverseDiagDir(), TRACK_BIT_DEPOT, and TRACK_BIT_WORMHOLE.

Referenced by TrainApproachingCrossingTile(), and TrainCheckIfLineEnds().

static void TrainCargoChanged | ( | Vehicle * | v | ) | `[static]`

Recalculates the cached weight of a train and its vehicles.

Should be called each time the cargo on the consist changes.

Parameters:

  | v | First vehicle of the consist.

Definition at line 149 of file train_cmd.cpp.

References FreightWagonMult(), HasBit(), IsArticulatedPart(), and TrainPowerChanged().

Referenced by TrainConsistChanged().

static bool TrainCheckIfLineEnds | ( | Vehicle * | v | ) | `[static]`

Checks for line end.

Also, bars crossing at next tile if needed

Parameters:

  | v | vehicle we are checking

Returns:
  true iff we did NOT have to reverse

Definition at line 3583 of file train_cmd.cpp.

References FindFirstTrack(), GB(), MaybeBarCrossingWithSound(), ReverseDiagDir(), TileOffsByDiagDir(), TRACK_BIT_NONE, TrackCrossesTracks(), TrackdirBitsToTrackBits(), TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), TrainApproachingLineEnd(), TrainCanLeaveTile(), TrainExitDir(), and VPF_NTP.

void TrainConsistChanged | ( | Vehicle * | v,
|  | bool | same_length |
| ) |  |  |

Recalculates the cached stuff of a train.

Should be called each time a vehicle is added to/removed from the chain, and when the game is loaded. Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)

Parameters:

  | v | First vehicle of the chain.
  | same_length | should length of vehicles stay the same?

Definition at line 244 of file train_cmd.cpp.

References CALLBACK_FAILED, CBID_TRAIN_WAGON_POWER, CBID_VEHICLE_LENGTH, CBM_TRAIN_WAGON_POWER, CBM_VEHICLE_LENGTH, Clamp(), ClrBit(), EF_RAIL_TILTS, GB(), GetRailTypeInfo(), GetVehicleCallback(), HasBit(), IsArticulatedPart(), IsFreeWagon(), IsFrontEngine(), IsTrainEngine(), IsTrainWagon(), min(), RailtypeInfo::powered_railtypes, RAILTYPE_RAIL, RAILTYPES_NONE, RAILTYPES_RAIL, RAILVEH_WAGON, RailVehicleLengthChanged(), SetBit(), TrainCargoChanged(), and UsesWagonOverride().

Referenced by AfterLoadVehicles(), CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdMoveRailVehicle(), CmdRefitRailVehicle(), CmdSellRailWagon(), DeleteLastWagon(), and ReverseTrainDirection().

static DiagDirection TrainExitDir | ( | Direction | direction,
|  | TrackBits | track |
| ) |  |  | `[inline, static]`

Determine the side in which the train will leave the tile.

Parameters:

  | direction | vehicle direction
  | track | vehicle track bits

Returns:
  side of tile the train will leave

Definition at line 70 of file train_cmd.cpp.

References ChangeDiagDir(), DIAGDIR_END, DIAGDIRDIFF_90LEFT, DirToDiagDir(), HasBit(), TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_RIGHT, and TRACK_BIT_UPPER.

Referenced by FindClosestTrainDepot(), TrainApproachingCrossingTile(), and TrainCheckIfLineEnds().

static void* TrainOnTileEnum | ( | Vehicle * | v,
|  | void * |  |
| ) |  |  | `[static]`

Check if the vehicle is a train.

Parameters:

  | v | vehicle on tile

Returns:
  v if it is a train, NULL otherwise

Definition at line 1713 of file train_cmd.cpp.

void TrainPowerChanged | ( | Vehicle * | v | ) |

Recalculates the cached total power of a train.

Should be called when the consist is changed

Parameters:

  | v | First vehicle of the consist.

Definition at line 100 of file train_cmd.cpp.

References GetRailType(), HasBit(), HasPowerOnRail(), IsArticulatedPart(), and IsMultiheaded().

Referenced by SettingsDisableElrail(), TrainCargoChanged(), and UpdateTrainPowerProc().

static Vehicle* UnlinkWagon | ( | Vehicle * | v,
|  | Vehicle * | first |
| ) |  |  | `[static]`

Unlink a rail wagon from the consist.

Parameters:

  | v | Vehicle to remove.
  | first | The first vehicle of the consist.

Returns:
  The first vehicle of the consist.

Definition at line 937 of file train_cmd.cpp.

References GetLastEnginePart(), GetNextVehicle(), IsTrainWagon(), and SetFreeWagon().

Referenced by CmdMoveRailVehicle(), and CmdSellRailWagon().

void UpdateLevelCrossing | ( | TileIndex | tile,
|  | bool | sound |
| ) |  |  |

Sets correct crossing state.

Parameters:

  | tile | tile to update
  | sound | should we play sound?

Precondition:
  tile is a rail-road crossing

Definition at line 1766 of file train_cmd.cpp.

References HasVehicleOnPos(), MarkTileDirtyByTile(), TrainApproachingCrossing(), and TrainOnTileEnum().

Referenced by CmdBuildRoad(), CmdBuildSingleRail(), DeleteLastWagon(), and ReverseTrainDirection().

## Variable Documentation

const uint16 _breakdown_speeds[16]`[static]`

Initial value:

     {
      225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
    }

Maximum speeds for train that is broken down or approaching line end.

Definition at line 3474 of file train_cmd.cpp.

const byte _initial_tile_subcoord[6][4][3]`[static]`

Initial value:

     {
    {{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0,  0, 0 }},
    {{  0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
    {{  0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0,  0, 0 }},
    {{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
    {{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
    {{  0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
    }

Definition at line 2456 of file train_cmd.cpp.

const Direction _new_vehicle_direction_table[11]`[static]`

Initial value:

     {
      DIR_N , DIR_NW, DIR_W , INVALID_DIR,
      DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
      DIR_E , DIR_SE, DIR_S
    }

Definition at line 2898 of file train_cmd.cpp.

const DiagDirection _otherside_signal_directions[]`[static]`

Initial value:

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

Definition at line 2985 of file train_cmd.cpp.

const RailtypeSlowdownParams _railtype_slowdown[]`[static]`

Initial value:

     {

      {256 / 4, 256 / 2, 256 / 4, 2},
      {256 / 4, 256 / 2, 256 / 4, 2},
      {256 / 4, 256 / 2, 256 / 4, 2},
      {0,       256 / 2, 256 / 4, 2},
    }

Definition at line 2950 of file train_cmd.cpp.

const uint32 _reachable_tracks[4]`[static]`

Initial value:

     {
      0x10091009,
      0x00160016,
      0x05200520,
      0x2A002A00,
    }

Definition at line 2465 of file train_cmd.cpp.

const byte _search_directions[6][4]`[static]`

Initial value:

     {
      { 0, 9, 2, 9 },
      { 9, 1, 9, 3 },
      { 9, 0, 3, 9 },
      { 1, 9, 9, 2 },
      { 3, 2, 9, 9 },
      { 9, 9, 1, 0 },
    }

Definition at line 2472 of file train_cmd.cpp.

const int8 _vehicle_smoke_pos[8]`[static]`

Initial value:

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

Definition at line 2266 of file train_cmd.cpp.