# train.h File Reference

Base for the train class. More...

`#include "stdafx.h"`
`#include "core/bitmath_func.hpp"`
`#include "vehicle_base.h"`

Go to the source code of this file.

## Data Structures
struct | Train
| This class 'wraps' Vehicle; you do not actually instantiate this class. More...
## Enumerations
enum | TrainSubtype { TS_FRONT = 0, TS_ARTICULATED_PART = 1, TS_WAGON = 2, TS_ENGINE = 3, TS_FREE_WAGON = 4, TS_MULTIHEADED = 5 }
| enum to handle train subtypes Do not access it directly unless you have to. More...
## Functions
static bool | IsFrontEngine (const Vehicle *v)
| Check if a vehicle is front engine.
static void | SetFrontEngine (Vehicle *v)
| Set front engine state.
static void | ClearFrontEngine (Vehicle *v)
| Remove the front engine state.
static bool | IsArticulatedPart (const Vehicle *v)
| Check if a vehicle is an articulated part of an engine.
static void | SetArticulatedPart (Vehicle *v)
| Set a vehicle to be an articulated part.
static void | ClearArticulatedPart (Vehicle *v)
| Clear a vehicle from being an articulated part.
static bool | IsTrainWagon (const Vehicle *v)
| Check if a vehicle is a wagon.
static void | SetTrainWagon (Vehicle *v)
| Set a vehicle to be a wagon.
static void | ClearTrainWagon (Vehicle *v)
| Clear wagon property.
static bool | IsTrainEngine (const Vehicle *v)
| Check if a vehicle is an engine (can be first in a train).
static void | SetTrainEngine (Vehicle *v)
| Set engine status.
static void | ClearTrainEngine (Vehicle *v)
| Clear engine status.
static bool | IsFreeWagon (const Vehicle *v)
| Check if a vehicle is a free wagon (got no engine in front of it).
static void | SetFreeWagon (Vehicle *v)
| Set if a vehicle is a free wagon.
static void | ClearFreeWagon (Vehicle *v)
| Clear a vehicle from being a free wagon.
static bool | IsMultiheaded (const Vehicle *v)
| Check if a vehicle is a multiheaded engine.
static void | SetMultiheaded (Vehicle *v)
| Set if a vehicle is a multiheaded engine.
static void | ClearMultiheaded (Vehicle *v)
| Clear multiheaded engine property.
static bool | EngineHasArticPart (const Vehicle *v)
| Check if an engine has an articulated part.
static Vehicle * | GetNextArticPart (const Vehicle *v)
| Get the next part of a multi-part engine.
static Vehicle * | GetLastEnginePart (Vehicle *v)
| Get the last part of a multi-part engine.
static bool | IsRearDualheaded (const Vehicle *v)
| Tell if we are dealing with the rear end of a multiheaded engine.
static Vehicle * | GetNextVehicle (const Vehicle *v)
| Get the next real (non-articulated part) vehicle in the consist.
static Vehicle * | GetPrevVehicle (const Vehicle *w)
| Get the previous real (non-articulated part) vehicle in the consist.
static Vehicle * | GetNextUnit (const Vehicle *v)
| Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
static Vehicle * | GetPrevUnit (const Vehicle *v)
| Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
void | CcBuildLoco (bool success, TileIndex tile, uint32 p1, uint32 p2)
void | CcBuildWagon (bool success, TileIndex tile, uint32 p1, uint32 p2)
byte | FreightWagonMult (CargoID cargo)
| Return the cargo weight multiplier to use for a rail vehicle.
int | CheckTrainInDepot (const Vehicle *v, bool needs_to_be_stopped)
int | CheckTrainStoppedInDepot (const Vehicle *v)
void | UpdateTrainAcceleration (Vehicle *v)
void | CheckTrainsLengths ()
| Checks if lengths of all rail vehicles are valid.
void | FreeTrainTrackReservation (const Vehicle *v, TileIndex origin=INVALID_TILE, Trackdir orig_td=INVALID_TRACKDIR)
| Free the reserved path in front of a vehicle.
bool | TryPathReserve (Vehicle *v, bool mark_as_stuck=false, bool first_tile_okay=false)
| Try to reserve a path to a safe position.

## Detailed Description

Base for the train class.

Definition in file train.h.

## Enumeration Type Documentation

enum TrainSubtype

enum to handle train subtypes Do not access it directly unless you have to.

Use the access functions below This is an enum to tell what bit to access as it is a bitmask

Enumerator:

  TS_FRONT | Leading engine of a train.
  TS_ARTICULATED_PART | Articulated part of an engine.
  TS_WAGON | Wagon.
  TS_ENGINE | Engine, that can be front engines, but might be placed behind another engine.
  TS_FREE_WAGON | First in a wagon chain (in depot).
  TS_MULTIHEADED | Engine is a multiheaded.

Definition at line 17 of file train.h.

## Function Documentation

void CheckTrainsLengths | ( |  | ) |

Checks if lengths of all rail vehicles are valid.

If not, shows an error message.

Definition at line 202 of file train_cmd.cpp.

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

Referenced by InitializeWindowsAndCaches(), and ReloadNewGRFData().

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

Clear a vehicle from being an articulated part.

Parameters:

  | v | vehicle to change

Definition at line 77 of file train.h.

References ClrBit(), and TS_ARTICULATED_PART.

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

Clear a vehicle from being a free wagon.

Parameters:

  | v | vehicle to change

Definition at line 161 of file train.h.

References ClrBit(), and TS_FREE_WAGON.

Referenced by CmdMoveRailVehicle().

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

Remove the front engine state.

Parameters:

  | v | vehicle to change

Definition at line 49 of file train.h.

References ClrBit(), and TS_FRONT.

Referenced by CmdMoveRailVehicle().

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

Clear multiheaded engine property.

Parameters:

  | v | vehicle to change

Definition at line 189 of file train.h.

References ClrBit(), and TS_MULTIHEADED.

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

Clear engine status.

Parameters:

  | v | vehicle to change

Definition at line 133 of file train.h.

References ClrBit(), and TS_ENGINE.

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

Clear wagon property.

Parameters:

  | v | vehicle to change

Definition at line 105 of file train.h.

References ClrBit(), and TS_WAGON.

static bool EngineHasArticPart | ( | const Vehicle * | v | ) | `[inline, static]`

Check if an engine has an articulated part.

Parameters:

  | v | Vehicle.

Returns:
  True if the engine has an articulated part.

Definition at line 199 of file train.h.

References IsArticulatedPart().

Referenced by CmdCloneVehicle(), GetLastEnginePart(), GetNextArticPart(), GetNextVehicle(), AIVehicle::IsArticulated(), and IsArticulatedVehicleCarryingDifferentCargos().

void FreeTrainTrackReservation | ( | const Vehicle * | v,
|  | TileIndex | origin,
|  | Trackdir | orig_td |
| ) |  |  |

Free the reserved path in front of a vehicle.

Definition at line 2455 of file train_cmd.cpp.

References ClearPathReservation(), RailtypeInfo::compatible_railtypes, CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::Follow(), GetRailTypeInfo(), GetReservedTrackbits(), GetSignalStateByTrackdir(), GetStationIndex(), GetVehicleTrackdir(), HasPbsSignalOnTrackdir(), HasSignalOnTrackdir(), INVALID_TILE, INVALID_TRACKDIR, IsFrontEngine(), IsOnewaySignal(), IsTileType(), IsValidTrackdir(), CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::m_is_bridge, CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::m_is_station, CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::m_is_tunnel, CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::m_new_td_bits, CFollowTrackT< Ttr_type_, T90deg_turns_allowed_, Tmask_reserved_tracks >::m_new_tile, MarkTileDirtyByTile(), MP_RAILWAY, MP_TUNNELBRIDGE, RemoveFirstTrackdir(), ReverseTrackdir(), SetSignalStateByTrackdir(), SIGNAL_STATE_RED, TrackBitsToTrackdirBits(), TRACKDIR_BIT_NONE, TrackdirToTrack(), TracksOverlap(), TrackToTrackBits(), and UnreserveRailTrack().

Referenced by CmdBuildRailroadStation(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveFromRailroadStation(), CmdRemoveSingleRail(), and RemoveTrainWaypoint().

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 77 of file train_cmd.cpp.

References _settings_game, VehicleSettings::freight_trains, and GameSettings::vehicle.

Referenced by TrainCargoChanged().

static Vehicle* GetLastEnginePart | ( | Vehicle * | v | ) | `[inline, static]`

Get the last part of a multi-part engine.

Parameters:

  | v | Vehicle.

Returns:
  Last part of the engine.

Definition at line 220 of file train.h.

References EngineHasArticPart(), and GetNextArticPart().

Referenced by CmdMoveRailVehicle(), TransferCargo(), and UnlinkWagon().

static Vehicle* GetNextArticPart | ( | const Vehicle * | v | ) | `[inline, static]`

Get the next part of a multi-part engine.

Will only work on a multi-part engine (EngineHasArticPart(v) == true), Result is undefined for normal engine.

Definition at line 210 of file train.h.

References EngineHasArticPart().

Referenced by CmdCloneVehicle(), GetLastEnginePart(), GetNextVehicle(), and IsArticulatedVehicleCarryingDifferentCargos().

static Vehicle* GetNextUnit | ( | const Vehicle * | v | ) | `[inline, static]`

Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.

Parameters:

  | v | Vehicle.

Returns:
  Next vehicle in the consist.

Definition at line 268 of file train.h.

References GetNextVehicle(), and IsRearDualheaded().

Referenced by AIVehicle::_MoveWagonInternal(), AIVehicle::_SellWagonInternal(), CmdAutoreplaceVehicle(), CmdMoveRailVehicle(), AIVehicle::GetNumWagons(), AIVehicle::GetWagonAge(), AIVehicle::GetWagonEngineType(), and ReplaceChain().

static Vehicle* GetNextVehicle | ( | const Vehicle * | v | ) | `[inline, static]`

Get the next real (non-articulated part) vehicle in the consist.

Parameters:

  | v | Vehicle.

Returns:
  Next vehicle in the consist.

Definition at line 241 of file train.h.

References EngineHasArticPart(), and GetNextArticPart().

Referenced by CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), GetNextUnit(), and UnlinkWagon().

static Vehicle* GetPrevUnit | ( | const Vehicle * | v | ) | `[inline, static]`

Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.

Parameters:

  | v | Vehicle.

Returns:
  Previous vehicle in the consist.

Definition at line 281 of file train.h.

References GetPrevVehicle(), and IsRearDualheaded().

Referenced by ReplaceChain().

static Vehicle* GetPrevVehicle | ( | const Vehicle * | w | ) | `[inline, static]`

Get the previous real (non-articulated part) vehicle in the consist.

Parameters:

  | w | Vehicle.

Returns:
  Previous vehicle in the consist.

Definition at line 254 of file train.h.

References IsArticulatedPart().

Referenced by GetPrevUnit().

static bool IsArticulatedPart | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is an articulated part of an engine.

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is an articulated part

Definition at line 59 of file train.h.

References HasBit(), and TS_ARTICULATED_PART.

Referenced by BuildDepotVehicleList(), CmdAutoreplaceVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), EngineHasArticPart(), GetNewEngineType(), GetPrevVehicle(), GetVehiclePalette(), IsEngineCountable(), ReplaceFreeUnit(), TrainCargoChanged(), TrainConsistChanged(), TrainPowerChanged(), TransferCargo(), and UpdateTrainPowerProc().

static bool IsFreeWagon | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is a free wagon (got no engine in front of it).

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is a free wagon

Definition at line 143 of file train.h.

References HasBit(), and TS_FREE_WAGON.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), CmdMoveRailVehicle(), AIVehicle::IsValidVehicle(), NormalizeTrainVehInDepot(), TrainConsistChanged(), and UpdateTrainGroupID().

static bool IsFrontEngine | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is front engine.

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is a front engine

Definition at line 31 of file train.h.

References HasBit(), and TS_FRONT.

Referenced by AfterLoadVehicles(), CmdAutoreplaceVehicle(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), DrawCompanyVehiclesAmount(), FreeTrainTrackReservation(), GetTrainAcceleration(), SettingsDisableElrail(), SetTrainGroupID(), TrainApproachingCrossingEnum(), TrainApproachingCrossingTile(), TrainConsistChanged(), TryPathReserve(), UpdateTrainGroupID(), and WhoCanServiceIndustry().

static bool IsMultiheaded | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is a multiheaded engine.

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is a multiheaded engine

Definition at line 171 of file train.h.

References HasBit(), and TS_MULTIHEADED.

Referenced by CmdReverseTrainDirection(), CmdSellRailWagon(), IsRearDualheaded(), and TrainPowerChanged().

static bool IsRearDualheaded | ( | const Vehicle * | v | ) | `[inline, static]`

Tell if we are dealing with the rear end of a multiheaded engine.

Parameters:

  | v | Vehicle.

Returns:
  True if the engine is the rear part of a dualheaded engine.

Definition at line 231 of file train.h.

References IsMultiheaded(), and IsTrainEngine().

Referenced by BuildDepotVehicleList(), CmdAutoreplaceVehicle(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), GetNewEngineType(), GetNextUnit(), GetPrevUnit(), IsEngineCountable(), and ReplaceFreeUnit().

static bool IsTrainEngine | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is an engine (can be first in a train).

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is an engine

Definition at line 115 of file train.h.

References HasBit(), and TS_ENGINE.

Referenced by AfterLoadVehicles(), CmdMoveRailVehicle(), CmdSellRailWagon(), IsRearDualheaded(), and TrainConsistChanged().

static bool IsTrainWagon | ( | const Vehicle * | v | ) | `[inline, static]`

Check if a vehicle is a wagon.

Parameters:

  | v | vehicle to check

Returns:
  Returns true if vehicle is a wagon

Definition at line 87 of file train.h.

References HasBit(), and TS_WAGON.

Referenced by TrainConsistChanged(), and UnlinkWagon().

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

Set a vehicle to be an articulated part.

Parameters:

  | v | vehicle to change

Definition at line 68 of file train.h.

References SetBit(), and TS_ARTICULATED_PART.

Referenced by ConvertOldMultiheadToNew().

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

Set if a vehicle is a free wagon.

Parameters:

  | v | vehicle to change

Definition at line 152 of file train.h.

References SetBit(), and TS_FREE_WAGON.

Referenced by CmdBuildRailWagon(), CmdMoveRailVehicle(), ConvertOldMultiheadToNew(), and UnlinkWagon().

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

Set front engine state.

Parameters:

  | v | vehicle to change

Definition at line 40 of file train.h.

References SetBit(), and TS_FRONT.

Referenced by CmdBuildRailVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), and ConvertOldMultiheadToNew().

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

Set if a vehicle is a multiheaded engine.

Parameters:

  | v | vehicle to change

Definition at line 180 of file train.h.

References SetBit(), and TS_MULTIHEADED.

Referenced by CmdBuildRailVehicle(), and ConvertOldMultiheadToNew().

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

Set engine status.

Parameters:

  | v | vehicle to change

Definition at line 124 of file train.h.

References SetBit(), and TS_ENGINE.

Referenced by CmdBuildRailVehicle(), and ConvertOldMultiheadToNew().

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

Set a vehicle to be a wagon.

Parameters:

  | v | vehicle to change

Definition at line 96 of file train.h.

References SetBit(), and TS_WAGON.

Referenced by CmdBuildRailWagon(), and ConvertOldMultiheadToNew().

bool TryPathReserve | ( | Vehicle * | v,
|  | bool | mark_as_stuck,
|  | bool | first_tile_okay |
| ) |  |  |

Try to reserve a path to a safe position.

Parameters:

  | v | The vehicle
  | mark_as_stuck | Should the train be marked as stuck on a failed reservation?
  | first_tile_okay | True if no path should be reserved if the current tile is a safe position.

Returns:
  True if a path could be reserved.

Definition at line 3042 of file train_cmd.cpp.

References _settings_client, _settings_game, ClrBit(), DiagdirReachesTrackdirs(), DiagdirReachesTracks(), FindFirstTrackdir(), FollowTrainReservation(), PathfinderSettings::forbid_90_deg, GetDepotWaypointReservation(), GetRailDepotDirection(), GetReservedTrackbits(), GetTileTrackStatus(), ClientSettings::gui, HasBit(), HasReservedTracks(), HasSignalOnTrackdir(), InvalidateWindowWidget(), IsFrontEngine(), IsTileType(), MarkTileDirtyByTile(), MarkTrainAsStuck(), MP_RAILWAY, PBSTileInfo::okay, PathfinderSettings::pathfinder_for_trains, GameSettings::pf, ReverseTrackdir(), SetDepotWaypointReservation(), GUISettings::show_track_reservation, PBSTileInfo::tile, TileAddByDiagDir(), TRACK_BIT_DEPOT, TrackBitsToTrackdirBits(), TrackCrossesTracks(), PBSTileInfo::trackdir, TrackdirBitsToTrackBits(), TrackdirToExitdir(), TrackdirToTrack(), TrackStatusToTrackdirBits(), TrainExitDir(), TRANSPORT_RAIL, and VPF_NTP.

Referenced by CmdBuildRailroadStation(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveFromRailroadStation(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), and RemoveTrainWaypoint().