# train.h File Reference

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

## 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 * | GetNextUnit (Vehicle *v)
| Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
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.
void | ConnectMultiheadedTrains ()
uint | CountArticulatedParts (EngineID engine_type)
int | CheckTrainInDepot (const Vehicle *v, bool needs_to_be_stopped)
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 | CheckTrainStoppedInDepot (const Vehicle *v)
void | UpdateTrainAcceleration (Vehicle *v)
void | CheckTrainsLengths ()
| Checks if lengths of all rail vehicles are valid.

## Detailed Description

Definition in file train.h.

## Enumeration Type Documentation

enum TrainSubtype

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

References _networking, abs(), 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 79 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 163 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 51 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 191 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 135 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 107 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 201 of file train.h.

References IsArticulatedPart().

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

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 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 222 of file train.h.

References EngineHasArticPart(), and GetNextArticPart().

Referenced by CmdMoveRailVehicle(), 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 212 of file train.h.

References EngineHasArticPart().

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

static Vehicle* GetNextUnit | ( | 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 256 of file train.h.

References GetNextVehicle(), and IsRearDualheaded().

Referenced by CmdMoveRailVehicle().

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

References EngineHasArticPart(), and GetNextArticPart().

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

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 61 of file train.h.

References HasBit(), and TS_ARTICULATED_PART.

Referenced by CmdMoveRailVehicle(), CmdSellRailWagon(), EngineHasArticPart(), GetVehiclePalette(), IsEngineCountable(), TrainCargoChanged(), TrainConsistChanged(), TrainPowerChanged(), 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 145 of file train.h.

References HasBit(), and TS_FREE_WAGON.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), CmdMoveRailVehicle(), 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 33 of file train.h.

References HasBit(), and TS_FRONT.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), DrawPlayerVehiclesAmount(), GetTrainAcceleration(), HandleCloneVehClick(), SettingsDisableElrail(), SetTrainGroupID(), TrainApproachingCrossingEnum(), TrainApproachingCrossingTile(), TrainConsistChanged(), 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 173 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 233 of file train.h.

References IsMultiheaded(), and IsTrainEngine().

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

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 117 of file train.h.

References HasBit(), and TS_ENGINE.

Referenced by 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 89 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 70 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 154 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 42 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 182 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 126 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 98 of file train.h.

References SetBit(), and TS_WAGON.

Referenced by CmdBuildRailWagon(), and ConvertOldMultiheadToNew().