# Train Struct Reference

'Train' is either a loco or a wagon. More...

`#include <train.h>`

Inheritance diagram for Train:

## Public Types
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...
## Public Member Functions
| Train ()
| We don't want GCC to zero our struct! It already is zeroed and has an index!
virtual | ~Train ()
| We want to 'destruct' the right class.
const char * | GetTypeString () const
void | MarkDirty ()
void | UpdateDeltaXY (Direction direction)
ExpensesType | GetExpenseType (bool income) const
void | PlayLeaveStationSound () const
bool | IsPrimaryVehicle () const
SpriteID | GetImage (Direction direction) const
int | GetDisplaySpeed () const
int | GetDisplayMaxSpeed () const
Money | GetRunningCost () const
int | GetDisplayImageWidth (Point *offset=NULL) const
| Get the width of a train vehicle image in the GUI.
bool | IsInDepot () const
bool | IsStoppedInDepot () const
bool | Tick ()
void | OnNewDay ()
uint | Crash (bool flooded=false)
Trackdir | GetVehicleTrackdir () const
TileIndex | GetOrderStationLocation (StationID station)
bool | FindClosestDepot (TileIndex *location, DestinationID *destination, bool *reverse)
void | ReserveTrackUnderConsist () const
| Tries to reserve track under whole train consist.
int | GetCurveSpeedLimit () const
| Computes train speed limit caused by curves.
void | ConsistChanged (bool same_length)
| Recalculates the cached stuff of a train.
void | CargoChanged ()
| Recalculates the cached weight of a train and its vehicles.
void | PowerChanged ()
| Recalculates the cached total power of a train.
int | UpdateSpeed ()
| This function looks at the vehicle and updates it's speed (cur_speed and subspeed) variables.
void | UpdateAcceleration ()
int | GetCurrentMaxSpeed () const
| Calculates the maximum speed of the vehicle under its current conditions.
int | GetAcceleration () const
| Calculates the acceleration of the vehicle under its current conditions.
FORCEINLINE void | SetFrontEngine ()
| Set front engine state.
FORCEINLINE void | ClearFrontEngine ()
| Remove the front engine state.
FORCEINLINE void | SetArticulatedPart ()
| Set a vehicle to be an articulated part.
FORCEINLINE void | ClearArticulatedPart ()
| Clear a vehicle from being an articulated part.
FORCEINLINE void | SetWagon ()
| Set a vehicle to be a wagon.
FORCEINLINE void | ClearWagon ()
| Clear wagon property.
FORCEINLINE void | SetEngine ()
| Set engine status.
FORCEINLINE void | ClearEngine ()
| Clear engine status.
FORCEINLINE void | SetFreeWagon ()
| Set if a vehicle is a free wagon.
FORCEINLINE void | ClearFreeWagon ()
| Clear a vehicle from being a free wagon.
FORCEINLINE void | SetMultiheaded ()
| Set if a vehicle is a multiheaded engine.
FORCEINLINE void | ClearMultiheaded ()
| Clear multiheaded engine property.
FORCEINLINE bool | IsFrontEngine () const
| Check if train is a front engine.
FORCEINLINE bool | IsFreeWagon () const
| Check if train is a free wagon (got no engine in front of it).
FORCEINLINE bool | IsEngine () const
| Check if a vehicle is an engine (can be first in a train).
FORCEINLINE bool | IsWagon () const
| Check if a train is a wagon.
FORCEINLINE bool | IsMultiheaded () const
| Check if train is a multiheaded engine.
FORCEINLINE bool | IsRearDualheaded () const
| Tell if we are dealing with the rear end of a multiheaded engine.
FORCEINLINE bool | IsArticulatedPart () const
| Check if train is an articulated part of an engine.
FORCEINLINE bool | HasArticulatedPart () const
| Check if an engine has an articulated part.
FORCEINLINE Train * | GetNextArticPart () const
| Get the next part of a multi-part engine.
FORCEINLINE Train * | GetFirstEnginePart ()
| Get the first part of a multi-part engine.
FORCEINLINE const Train * | GetFirstEnginePart () const
| Get the first part of a multi-part engine.
FORCEINLINE Train * | GetLastEnginePart ()
| Get the last part of a multi-part engine.
FORCEINLINE Train * | GetNextVehicle () const
| Get the next real (non-articulated part) vehicle in the consist.
FORCEINLINE Train * | GetPrevVehicle () const
| Get the previous real (non-articulated part) vehicle in the consist.
FORCEINLINE Train * | GetNextUnit () const
| Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
FORCEINLINE Train * | GetPrevUnit ()
| Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
## Data Fields
TrainCache | tcache
Train * | other_multiheaded_part
uint16 | crash_anim_pos
uint16 | flags
TrackBitsByte | track
byte | force_proceed
RailTypeByte | railtype
RailTypes | compatible_railtypes
uint16 | wait_counter
| Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through signals.
## Protected Member Functions
FORCEINLINE uint16 | GetPower () const
| Allows to know the power value that this vehicle will use.
FORCEINLINE uint16 | GetPoweredPartPower (const Train *head) const
| Returns a value if this articulated part is powered.
FORCEINLINE uint16 | GetWeight () const
| Allows to know the weight value that this vehicle will use.
FORCEINLINE byte | GetTractiveEffort () const
| Allows to know the tractive effort value that this vehicle will use.
FORCEINLINE AccelStatus | GetAccelerationStatus () const
| Checks the current acceleration status of this vehicle.
FORCEINLINE uint16 | GetCurrentSpeed () const
| Calculates the current speed of this vehicle.
FORCEINLINE uint32 | GetRollingFriction () const
| Returns the rolling friction coefficient of this vehicle.
FORCEINLINE int32 | GetSlopeResistance () const
| Calculates the total slope resistance for this vehicle.
FORCEINLINE int | GetAccelerationType () const
| Allows to know the acceleration type of a vehicle.

## Detailed Description

'Train' is either a loco or a wagon.

Definition at line 105 of file train.h.

## Member Enumeration Documentation

enum Train::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 engine, but might be placed behind another engine.
  TS_FREE_WAGON | First in a wagon chain (in depot).
  TS_MULTIHEADED | Engine is multiheaded.

Definition at line 167 of file train.h.

## Constructor & Destructor Documentation

virtual Train::~Train | ( |  | ) | `[inline, virtual]`

We want to 'destruct' the right class.

Definition at line 125 of file train.h.

## Member Function Documentation

int Train::GetDisplayImageWidth | ( | Point * | offset = `NULL` | ) | const

Get the width of a train vehicle image in the GUI.

Parameters:

  | offset | Additional offset for positioning the sprite; set to NULL if not needed

Returns:
  Width in pixels

Definition at line 573 of file train_cmd.cpp.

References TrainCache::cached_veh_length, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::Get(), Point::x, and Point::y.

Referenced by DrawTrainDetails(), DrawTrainImage(), and GetLengthOfArticulatedVehicle().

int Train::GetCurveSpeedLimit | ( |  | ) | const

Computes train speed limit caused by curves.

Returns:
  imposed speed limit

Definition at line 388 of file train_cmd.cpp.

References _settings_game, TrainCache::cached_tilt, Clamp(), RailtypeInfo::curve_speed, DIRDIFF_45LEFT, DIRDIFF_45RIGHT, DIRDIFF_90LEFT, DIRDIFF_90RIGHT, DIRDIFF_SAME, DirDifference(), SpecializedVehicle< Train, VEH_TRAIN >::First(), GetRailTypeInfo(), VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

Referenced by ConsistChanged(), and GetCurrentMaxSpeed().

void Train::ConsistChanged | ( | 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:

  | same_length | should length of vehicles stay the same?

Definition at line 198 of file train_cmd.cpp.

References _settings_game, TrainCache::cached_max_curve_speed, TrainCache::cached_max_speed, TrainCache::cached_tilt, TrainCache::cached_total_length, CALLBACK_FAILED, CargoChanged(), CBID_TRAIN_WAGON_POWER, CBID_VEHICLE_LENGTH, CBM_TRAIN_WAGON_POWER, CBM_VEHICLE_LENGTH, Clamp(), ClrBit(), EF_RAIL_TILTS, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::Get(), GetCurveSpeedLimit(), GetRailTypeInfo(), GetVehicleCallback(), GetVehicleCapacity(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, IsFreeWagon(), IsFrontEngine(), min(), RailtypeInfo::powered_railtypes, PROP_TRAIN_SPEED, PROP_TRAIN_USER_DATA, RAILTYPE_RAIL, RAILTYPES_NONE, RAILTYPES_RAIL, RAILVEH_WAGON, RailVehicleLengthChanged(), SetBit(), SetWindowDirty(), UsesWagonOverride(), GameSettings::vehicle, and VehicleSettings::wagon_speed_limits.

Referenced by AfterLoadVehicles(), CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdRefitRailVehicle(), DeleteLastWagon(), NormaliseTrainHead(), StateGameLoop(), and TransferCargo().

void Train::CargoChanged | ( |  | ) |

Recalculates the cached weight of a train and its vehicles.

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

Definition at line 134 of file train_cmd.cpp.

References _settings_game, TrainCache::cached_weight, SpecializedVehicle< Train, VEH_TRAIN >::First(), PowerChanged(), VehicleSettings::train_slope_steepness, and GameSettings::vehicle.

Referenced by ConsistChanged(), and TrainSlopeSteepnessChanged().

void Train::PowerChanged | ( |  | ) |

Recalculates the cached total power of a train.

Should be called when the consist is changed

Definition at line 95 of file train_cmd.cpp.

References TrainCache::cached_air_drag, TrainCache::cached_axle_resistance, TrainCache::cached_max_te, TrainCache::cached_power, SpecializedVehicle< Train, VEH_TRAIN >::First(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, SetWindowDirty(), and SetWindowWidgetDirty().

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

int Train::UpdateSpeed | ( |  | ) |

This function looks at the vehicle and updates it's speed (cur_speed and subspeed) variables.

Furthermore, it returns the distance that the train can drive this tick. This distance is expressed as 256 * n, where n is the number of straight (long) tracks the train can traverse. This means that moving along a straight track costs 256 "speed" and a diagonal track costs 192 "speed".

Returns:
  distance to drive.

Definition at line 2938 of file train_cmd.cpp.

References _settings_game, AS_BRAKE, Clamp(), GetAcceleration(), GetAccelerationStatus(), GetCurrentMaxSpeed(), VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

int Train::GetCurrentMaxSpeed | ( |  | ) | const

Calculates the maximum speed of the vehicle under its current conditions.

Returns:
  Maximum speed of the vehicle.

Definition at line 457 of file train_cmd.cpp.

References TrainCache::cached_max_curve_speed, TrainCache::cached_max_speed, GetCurveSpeedLimit(), GetStationIndex(), GetTrainStopLocation(), IsRailStationTile(), max(), min(), TILE_SIZE, and TRACK_BIT_DEPOT.

Referenced by UpdateSpeed().

int Train::GetAcceleration | ( |  | ) | const

Calculates the acceleration of the vehicle under its current conditions.

Returns:
  Current acceleration of the vehicle.

Definition at line 502 of file train_cmd.cpp.

References AS_ACCEL, TrainCache::cached_air_drag, TrainCache::cached_axle_resistance, TrainCache::cached_max_te, TrainCache::cached_power, TrainCache::cached_weight, GetAccelerationStatus(), GetAccelerationType(), GetCurrentSpeed(), GetRollingFriction(), GetSlopeResistance(), max(), and min().

Referenced by UpdateSpeed().

FORCEINLINE bool Train::IsFrontEngine | ( |  | ) | const`[inline]`

Check if train is a front engine.

Returns:
  Returns true if train is a front engine

Definition at line 241 of file train.h.

References HasBit(), and TS_FRONT.

Referenced by AfterLoadVehicles(), CmdAutoreplaceVehicle(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), ConsistChanged(), FreeTrainTrackReservation(), NormaliseTrainHead(), SettingsDisableElrail(), SetTrainGroupID(), TrainApproachingCrossingEnum(), TrainApproachingCrossingTile(), TrainSlopeSteepnessChanged(), TryPathReserve(), UpdateTrainGroupID(), and WhoCanServiceIndustry().

FORCEINLINE bool Train::IsFreeWagon | ( |  | ) | const`[inline]`

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

Returns:
  Returns true if train is a free wagon

Definition at line 247 of file train.h.

References HasBit(), and TS_FREE_WAGON.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), ConsistChanged(), NormalizeTrainVehInDepot(), and UpdateTrainGroupID().

FORCEINLINE bool Train::IsEngine | ( |  | ) | const`[inline]`

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

Returns:
  Returns true if vehicle is an engine

Definition at line 253 of file train.h.

References HasBit(), and TS_ENGINE.

Referenced by AfterLoadVehicles(), CheckNewTrain(), CheckTrainAttachment(), CmdMoveRailVehicle(), CmdSellRailWagon(), IsRearDualheaded(), NormaliseDualHeads(), and NormaliseSubtypes().

FORCEINLINE bool Train::IsWagon | ( |  | ) | const`[inline]`

Check if a train is a wagon.

Returns:
  Returns true if vehicle is a wagon

Definition at line 259 of file train.h.

References HasBit(), and TS_WAGON.

Referenced by NormaliseSubtypes().

FORCEINLINE bool Train::IsMultiheaded | ( |  | ) | const`[inline]`

Check if train is a multiheaded engine.

Returns:
  Returns true if vehicle is a multiheaded engine

Definition at line 265 of file train.h.

References HasBit(), and TS_MULTIHEADED.

Referenced by ArrangeTrains(), CmdReverseTrainDirection(), GetPower(), IsRearDualheaded(), and NormaliseDualHeads().

FORCEINLINE bool Train::IsRearDualheaded | ( |  | ) | const`[inline]`

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

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

Definition at line 271 of file train.h.

References IsEngine(), and IsMultiheaded().

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

FORCEINLINE bool Train::IsArticulatedPart | ( |  | ) | const`[inline]`

Check if train is an articulated part of an engine.

Returns:
  Returns true if train is an articulated part

Definition at line 277 of file train.h.

References HasBit(), and TS_ARTICULATED_PART.

Referenced by BuildDepotVehicleList(), CheckTrainAttachment(), CmdAutoreplaceVehicle(), DrawTrainDetails(), GetCargoSummaryOfArticulatedVehicle(), GetFirstEnginePart(), GetLengthOfArticulatedVehicle(), GetNewEngineType(), GetPower(), GetPrevVehicle(), GetWeight(), HasArticulatedPart(), InsertInConsist(), ReplaceFreeUnit(), TransferCargo(), and UpdateTrainPowerProc().

FORCEINLINE bool Train::HasArticulatedPart | ( |  | ) | const`[inline]`

Check if an engine has an articulated part.

Returns:
  True if the engine has an articulated part.

Definition at line 283 of file train.h.

References IsArticulatedPart(), and SpecializedVehicle< Train, VEH_TRAIN >::Next().

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

FORCEINLINE Train* Train::GetNextArticPart | ( |  | ) | const`[inline]`

Get the next part of a multi-part engine.

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

Returns:
  next part of articulated engine

Definition at line 292 of file train.h.

References HasArticulatedPart(), and SpecializedVehicle< Train, VEH_TRAIN >::Next().

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

FORCEINLINE Train* Train::GetFirstEnginePart | ( |  | ) | `[inline]`

Get the first part of a multi-part engine.

Returns:
  First part of the engine.

Definition at line 302 of file train.h.

References IsArticulatedPart(), and SpecializedVehicle< T, Type >::Previous().

Referenced by CmdMoveRailVehicle(), and CmdSellRailWagon().

FORCEINLINE const Train* Train::GetFirstEnginePart | ( |  | ) | const`[inline]`

Get the first part of a multi-part engine.

Returns:
  First part of the engine.

Definition at line 313 of file train.h.

References IsArticulatedPart(), and SpecializedVehicle< T, Type >::Previous().

FORCEINLINE Train* Train::GetLastEnginePart | ( |  | ) | `[inline]`

Get the last part of a multi-part engine.

Returns:
  Last part of the engine.

Definition at line 324 of file train.h.

References GetNextArticPart(), and HasArticulatedPart().

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

FORCEINLINE Train* Train::GetNextVehicle | ( |  | ) | const`[inline]`

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

Returns:
  Next vehicle in the consist.

Definition at line 335 of file train.h.

References GetNextArticPart(), HasArticulatedPart(), and SpecializedVehicle< T, Type >::Next().

Referenced by ArrangeTrains(), CmdCloneVehicle(), DrawTrainDetails(), GetNextUnit(), GetTrainDetailsWndVScroll(), and NormaliseDualHeads().

FORCEINLINE Train* Train::GetPrevVehicle | ( |  | ) | const`[inline]`

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

Returns:
  Previous vehicle in the consist.

Definition at line 348 of file train.h.

References IsArticulatedPart(), SpecializedVehicle< T, Type >::Previous(), and SpecializedVehicle< Train, VEH_TRAIN >::Previous().

Referenced by GetPrevUnit().

FORCEINLINE Train* Train::GetNextUnit | ( |  | ) | const`[inline]`

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

Returns:
  Next vehicle in the consist.

Definition at line 360 of file train.h.

References GetNextVehicle(), and IsRearDualheaded().

Referenced by AIVehicle::_MoveWagonInternal(), AIVehicle::_SellWagonInternal(), ArrangeTrains(), CmdAutoreplaceVehicle(), AIVehicle::GetNumWagons(), and ReplaceChain().

FORCEINLINE Train* Train::GetPrevUnit | ( |  | ) | `[inline]`

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

Returns:
  Previous vehicle in the consist.

Definition at line 372 of file train.h.

References GetPrevVehicle(), and IsRearDualheaded().

Referenced by ReplaceChain().

FORCEINLINE uint16 Train::GetPower | ( |  | ) | const`[inline, protected]`

Allows to know the power value that this vehicle will use.

Returns:
  Power value from the engine in HP, or zero if the vehicle is not powered.

Definition at line 387 of file train.h.

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

FORCEINLINE uint16 Train::GetPoweredPartPower | ( | const Train * | head | ) | const`[inline, protected]`

Returns a value if this articulated part is powered.

Returns:
  Power value from the articulated part in HP, or zero if it is not powered.

Definition at line 404 of file train.h.

References TrainCache::first_engine, GetRailType(), HasBit(), HasPowerOnRail(), and railtype.

FORCEINLINE uint16 Train::GetWeight | ( |  | ) | const`[inline, protected]`

Allows to know the weight value that this vehicle will use.

Returns:
  Weight value from the engine in tonnes.

Definition at line 417 of file train.h.

References TrainCache::first_engine, FreightWagonMult(), CargoSpec::Get(), HasBit(), IsArticulatedPart(), PROP_TRAIN_WEIGHT, and CargoSpec::weight.

FORCEINLINE byte Train::GetTractiveEffort | ( |  | ) | const`[inline, protected]`

Allows to know the tractive effort value that this vehicle will use.

Returns:
  Tractive effort value from the engine.

Definition at line 438 of file train.h.

References PROP_TRAIN_TRACTIVE_EFFORT.

FORCEINLINE AccelStatus Train::GetAccelerationStatus | ( |  | ) | const`[inline, protected]`

Checks the current acceleration status of this vehicle.

Returns:
  Acceleration status.

Definition at line 447 of file train.h.

References AS_ACCEL, AS_BRAKE, and HasBit().

Referenced by GetAcceleration(), and UpdateSpeed().

FORCEINLINE uint16 Train::GetCurrentSpeed | ( |  | ) | const`[inline, protected]`

Calculates the current speed of this vehicle.

Returns:
  Current speed in mph.

Definition at line 456 of file train.h.

Referenced by GetAcceleration().

FORCEINLINE uint32 Train::GetRollingFriction | ( |  | ) | const`[inline, protected]`

Returns the rolling friction coefficient of this vehicle.

Returns:
  Rolling friction coefficient in [1e-3].

Definition at line 465 of file train.h.

Referenced by GetAcceleration().

FORCEINLINE int32 Train::GetSlopeResistance | ( |  | ) | const`[inline, protected]`

Calculates the total slope resistance for this vehicle.

Returns:
  Slope resistance.

Definition at line 474 of file train.h.

References HasBit().

Referenced by GetAcceleration().

FORCEINLINE int Train::GetAccelerationType | ( |  | ) | const`[inline, protected]`

Allows to know the acceleration type of a vehicle.

Returns:
  Acceleration type of the vehicle.

Definition at line 493 of file train.h.

References RailtypeInfo::acceleration_type, and GetRailTypeInfo().

Referenced by GetAcceleration().

## Field Documentation

uint16 Train::wait_counter

Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through signals.

Definition at line 120 of file train.h.

Referenced by MarkTrainAsStuck(), and TryPathReserve().

The documentation for this struct was generated from the following files:

- src/train.h
- src/train_cmd.cpp