# 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
| Get a string 'representation' of the vehicle type.
void | MarkDirty ()
| Marks the vehicles to be redrawn and updates cached variables.
void | UpdateDeltaXY (Direction direction)
| Updates the x and y offsets and the size of the sprite used for this vehicle.
ExpensesType | GetExpenseType (bool income) const
| Sets the expense type associated to this vehicle type.
void | PlayLeaveStationSound () const
| Play the sound associated with leaving the station.
bool | IsPrimaryVehicle () const
| Whether this is the primary vehicle in the chain.
SpriteID | GetImage (Direction direction) const
| Gets the sprite to show for the given direction.
int | GetDisplaySpeed () const
| Gets the speed in km-ish/h that can be sent into SetDParam for string processing.
int | GetDisplayMaxSpeed () const
| Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Money | GetRunningCost () const
| Gets the running cost of a vehicle.
int | GetDisplayImageWidth (Point *offset=NULL) const
| Get the width of a train vehicle image in the GUI.
bool | IsInDepot () const
| Check whether the vehicle is in the depot.
bool | IsStoppedInDepot () const
| Check whether the vehicle is in the depot *and* stopped.
bool | Tick ()
| Calls the tick handler of the vehicle.
void | OnNewDay ()
| Calls the new day handler of the vehicle.
uint | Crash (bool flooded=false)
| Crash the (whole) vehicle chain.
Trackdir | GetVehicleTrackdir () const
| Returns the Trackdir on which the vehicle is currently located.
TileIndex | GetOrderStationLocation (StationID station)
| Determine the location for the station where the vehicle goes to next.
bool | FindClosestDepot (TileIndex *location, DestinationID *destination, bool *reverse)
| Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should 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.
void | RailtypeChanged ()
| Update visual effect, power and acceleration caches.
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
void | UpdateVisualEffect (bool allow_power_change)
| Update the cached visual effect.
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.
FORCEINLINE uint32 | GetSlopeSteepness () const
| Returns the slope steepness used by this vehicle.

## Detailed Description

'Train' is either a loco or a wagon.

Definition at line 114 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 178 of file train.h.

## Constructor & Destructor Documentation

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

We want to 'destruct' the right class.

Definition at line 134 of file train.h.

References Vehicle::PreDestructor().

## Member Function Documentation

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

References AccelerationCache::cached_weight, SpecializedVehicle< Train, VEH_TRAIN >::First(), and PowerChanged().

Referenced by ConsistChanged(), MarkDirty(), and TrainSlopeSteepnessChanged().

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 256 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, EngineInfo::callback_mask, CargoChanged(), CBID_VEHICLE_LENGTH, CBM_VEHICLE_LENGTH, Clamp(), ClrBit(), EF_RAIL_TILTS, SpecializedVehicle< Train, VEH_TRAIN >::Get(), GetCurveSpeedLimit(), GetRailTypeInfo(), GetVehicleCallback(), GetVehicleCapacity(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::index, Vehicle::InvalidateNewGRFCache(), IsFreeWagon(), IsFrontEngine(), Vehicle::max_speed, min(), RailVehicleInfo::pow_wag_power, RailVehicleInfo::power, RailtypeInfo::powered_railtypes, PROP_TRAIN_SPEED, PROP_TRAIN_USER_DATA, RAILTYPE_RAIL, RAILTYPES_NONE, RAILTYPES_RAIL, RAILVEH_WAGON, RailVehicleLengthChanged(), SetBit(), SetWindowDirty(), RailVehicleInfo::shorten_factor, RailVehicleInfo::user_def_data, UsesWagonOverride(), GameSettings::vehicle, and VehicleSettings::wagon_speed_limits.

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

uint Train::Crash | ( | bool | flooded = `false` | ) | `[virtual]`

Crash the (whole) vehicle chain.

Parameters:

  | flooded | whether the cause of the crash is flooding or not.

Returns:
  the number of lost souls.

Reimplemented from Vehicle.

Definition at line 3141 of file train_cmd.cpp.

References ClearPathReservation(), FreeTrainTrackReservation(), GetOtherTunnelBridgeEnd(), GetVehicleTrackdir(), HasBit(), INVALID_TILE, IsFrontEngine(), IsTileType(), MP_TUNNELBRIDGE, SpecializedVehicle< T, Type >::Next(), SetTunnelBridgeReservation(), Vehicle::tile, TrainApproachingCrossingTile(), and UpdateLevelCrossing().

Referenced by TrainCrashed().

bool Train::FindClosestDepot | ( | TileIndex * | location,
|  | DestinationID * | destination,
|  | bool * | reverse |
| ) |  |  | `[virtual]`

Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should reverse.

Parameters:

  | location | where do we go to?
  | destination | what hangar do we go to?
  | reverse | should the vehicle be reversed?

Returns:
  true if a depot could be found.

Reimplemented from Vehicle.

Definition at line 2123 of file train_cmd.cpp.

References FindDepotData::best_length, FindClosestTrainDepot(), GetDepotIndex(), FindDepotData::reverse, and FindDepotData::tile.

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

Calculates the acceleration of the vehicle under its current conditions.

Returns:
  Current acceleration of the vehicle.

Definition at line 541 of file train_cmd.cpp.

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

Referenced by UpdateSpeed().

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

Checks the current acceleration status of this vehicle.

Returns:
  Acceleration status.

Definition at line 461 of file train.h.

References AS_ACCEL, AS_BRAKE, HasBit(), and Vehicle::vehstatus.

Referenced by GetAcceleration(), and UpdateSpeed().

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

References RailtypeInfo::acceleration_type, and GetRailTypeInfo().

Referenced by GetAcceleration().

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

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

Returns:
  Maximum speed of the vehicle.

Definition at line 496 of file train_cmd.cpp.

References TrainCache::cached_max_curve_speed, TrainCache::cached_max_rail_speed, Vehicle::cur_speed, Vehicle::current_order, GetCurveSpeedLimit(), GetStationIndex(), GetTrainStopLocation(), IsRailStationTile(), max(), Vehicle::max_speed, min(), Order::ShouldStopAtStation(), Vehicle::tile, TILE_SIZE, and TRACK_BIT_DEPOT.

Referenced by UpdateSpeed().

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

Calculates the current speed of this vehicle.

Returns:
  Current speed in mph.

Definition at line 470 of file train.h.

References Vehicle::cur_speed.

Referenced by GetAcceleration().

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

Computes train speed limit caused by curves.

Returns:
  imposed speed limit

Definition at line 427 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(), Vehicle::max_speed, VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

Referenced by ConsistChanged(), and GetCurrentMaxSpeed().

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

References TrainCache::cached_veh_length, SpecializedVehicle< Train, VEH_TRAIN >::Get(), GRFFile::traininfo_vehicle_pitch, and GRFFile::traininfo_vehicle_width.

Referenced by DrawTrainDetails(), DrawTrainImage(), GetLengthOfArticulatedVehicle(), and DepotWindow::OnPaint().

int Train::GetDisplayMaxSpeed | ( |  | ) | const`[inline, virtual]`

Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.

Returns:
  the vehicle's maximum speed

Reimplemented from Vehicle.

Definition at line 144 of file train.h.

References TrainCache::cached_max_speed.

int Train::GetDisplaySpeed | ( |  | ) | const`[inline, virtual]`

Gets the speed in km-ish/h that can be sent into SetDParam for string processing.

Returns:
  the vehicle's speed

Reimplemented from Vehicle.

Definition at line 143 of file train.h.

ExpensesType Train::GetExpenseType | ( | bool | income | ) | const`[inline, virtual]`

Sets the expense type associated to this vehicle type.

Parameters:

  | income | whether this is income or (running) expenses of the vehicle

Reimplemented from Vehicle.

Definition at line 139 of file train.h.

References EXPENSES_TRAIN_INC, and EXPENSES_TRAIN_RUN.

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

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

FORCEINLINE Train* Train::GetFirstEnginePart | ( |  | ) | `[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().

Referenced by CmdMoveRailVehicle(), and CmdSellRailWagon().

SpriteID Train::GetImage | ( | Direction | direction | ) | const`[virtual]`

Gets the sprite to show for the given direction.

Parameters:

  | direction | the direction the vehicle is facing

Returns:
  the sprite for the given vehicle in the given direction

Reimplemented from Vehicle.

Definition at line 635 of file train_cmd.cpp.

References Vehicle::cargo, Vehicle::cargo_cap, CargoList< Tinst >::Count(), SpecializedVehicle< Train, VEH_TRAIN >::Get(), HasBit(), and ReverseDir().

Referenced by DrawTrainDetails().

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

Get the last part of a multi-part engine.

Returns:
  Last part of the engine.

Definition at line 335 of file train.h.

References GetNextArticPart(), and HasArticulatedPart().

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

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

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

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

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

References GetNextVehicle(), and IsRearDualheaded().

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

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

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

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

TileIndex Train::GetOrderStationLocation | ( | StationID | station | ) | `[virtual]`

Determine the location for the station where the vehicle goes to next.

Things done for example are allocating slots in a road stop or exact location of the platform is determined for ships.

Parameters:

  | station | the station to make the next location of the vehicle.

Returns:
  the location (tile) to aim for.

Reimplemented from Vehicle.

Definition at line 2936 of file train_cmd.cpp.

References FACIL_TRAIN, BaseStation::facilities, SpecializedVehicle< Train, VEH_TRAIN >::Get(), Vehicle::IncrementOrderIndex(), and BaseStation::xy.

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

References GetRailType(), HasPowerOnRail(), IsArticulatedPart(), IsMultiheaded(), PROP_TRAIN_POWER, and Vehicle::tile.

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

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

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

References GetPrevVehicle(), and IsRearDualheaded().

Referenced by ReplaceChain().

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

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

Referenced by GetPrevUnit().

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

Referenced by GetAcceleration().

Money Train::GetRunningCost | ( |  | ) | const`[virtual]`

Gets the running cost of a vehicle.

Returns:
  the vehicle's running cost

Reimplemented from Vehicle.

Definition at line 4088 of file train_cmd.cpp.

References SpecializedVehicle< Train, VEH_TRAIN >::Get(), GetNextVehicle(), GetPrice(), IsMultiheaded(), PROP_TRAIN_RUNNING_COST_FACTOR, and RailVehicleInfo::running_cost.

Referenced by OnNewDay().

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

Calculates the total slope resistance for this vehicle.

Returns:
  Slope resistance.

Definition at line 488 of file train.h.

References HasBit().

Referenced by GetAcceleration().

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

Returns the slope steepness used by this vehicle.

Returns:
  Slope steepness used by the vehicle.

Definition at line 516 of file train.h.

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

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

References PROP_TRAIN_TRACTIVE_EFFORT.

const char* Train::GetTypeString | ( |  | ) | const`[inline, virtual]`

Get a string 'representation' of the vehicle type.

Returns:
  the string representation.

Reimplemented from Vehicle.

Definition at line 136 of file train.h.

Trackdir Train::GetVehicleTrackdir | ( |  | ) | const`[virtual]`

Returns the Trackdir on which the vehicle is currently located.

Works for trains and ships. Currently works only sortof for road vehicles, since they have a fuzzy concept of being "on" a trackdir. Dunno really what it returns for a road vehicle that is halfway a tile, never really understood that part. For road vehicles that are at the beginning or end of the tile, should just return the diagonal trackdir on which they are driving. I _think_. For other vehicles types, or vehicles with no clear trackdir (such as those in depots), returns 0xFF.

Returns:
  the trackdir of the vehicle

Reimplemented from Vehicle.

Definition at line 4210 of file train_cmd.cpp.

References DiagDirToDiagTrackdir(), DirToDiagDir(), FindFirstTrack(), GetRailDepotDirection(), INVALID_TRACKDIR, TRACK_BIT_DEPOT, TRACK_BIT_WORMHOLE, TrackDirectionToTrackdir(), and Vehicle::vehstatus.

Referenced by CheckNextTrainTile(), CmdBuildRailStation(), CmdBuildSingleSignal(), Crash(), FollowTrainReservation(), FreeTrainTrackReservation(), NPFTrainCheckReverse(), NPFTrainFindNearestDepot(), RemoveFromRailBaseStation(), YapfTrainCheckReverse(), and YapfTrainFindNearestDepot().

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

References Vehicle::cargo, Vehicle::cargo_type, CargoList< Tinst >::Count(), TrainCache::first_engine, FreightWagonMult(), SpecializedVehicle< Train, VEH_TRAIN >::Get(), HasBit(), IsArticulatedPart(), and PROP_TRAIN_WEIGHT.

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

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

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

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

References HasBit(), Vehicle::subtype, and TS_ARTICULATED_PART.

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

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

References HasBit(), Vehicle::subtype, and TS_ENGINE.

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

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

References HasBit(), Vehicle::subtype, and TS_FREE_WAGON.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), ConsistChanged(), DepotWindow::DrawVehicleInDepot(), NormalizeTrainVehInDepot(), Tick(), and UpdateTrainGroupID().

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

References HasBit(), Vehicle::subtype, and TS_FRONT.

Referenced by AfterLoadVehicles(), CmdAutoreplaceVehicle(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), ConsistChanged(), Crash(), FreeTrainTrackReservation(), DepotWindow::HandleCloneVehClick(), IsPrimaryVehicle(), IsStoppedInDepot(), NormaliseTrainHead(), DepotWindow::OnDragDrop(), OnNewDay(), RailtypeChanged(), SettingsDisableElrail(), SetTrainGroupID(), Tick(), TrainApproachingCrossingEnum(), TrainApproachingCrossingTile(), TrainSlopeSteepnessChanged(), TryPathReserve(), UpdateTrainGroupID(), and WhoCanServiceIndustry().

bool Train::IsInDepot | ( |  | ) | const`[virtual]`

Check whether the vehicle is in the depot.

Returns:
  true if and only if the vehicle is in the depot.

Reimplemented from Vehicle.

Definition at line 965 of file train_cmd.cpp.

References Vehicle::cur_speed, IsRailDepotTile(), SpecializedVehicle< T, Type >::Next(), Vehicle::tile, and TRACK_BIT_DEPOT.

Referenced by CmdForceTrainProceed(), CmdMassStartStopVehicle(), and IsStoppedInDepot().

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

References HasBit(), Vehicle::subtype, and TS_MULTIHEADED.

Referenced by ArrangeTrains(), CmdReverseTrainDirection(), GetPower(), GetRunningCost(), 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 282 of file train.h.

References IsEngine(), and IsMultiheaded().

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

bool Train::IsStoppedInDepot | ( |  | ) | const`[virtual]`

Check whether the vehicle is in the depot *and* stopped.

Returns:
  true if and only if the vehicle is in the depot and stopped.

Reimplemented from Vehicle.

Definition at line 978 of file train_cmd.cpp.

References IsFrontEngine(), IsInDepot(), and Vehicle::vehstatus.

Referenced by CmdMoveRailVehicle(), CmdRefitRailVehicle(), CmdReverseTrainDirection(), and CmdSellRailWagon().

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

Check if a train is a wagon.

Returns:
  Returns true if vehicle is a wagon

Definition at line 270 of file train.h.

References HasBit(), Vehicle::subtype, and TS_WAGON.

Referenced by NormaliseSubtypes(), and UpdateVisualEffect().

void Train::MarkDirty | ( |  | ) | `[virtual]`

Marks the vehicles to be redrawn and updates cached variables.

This method marks the area of the vehicle on the screen as dirty. It can be use to repaint the vehicle.

Reimplemented from Vehicle.

Definition at line 2950 of file train_cmd.cpp.

References CargoChanged(), Vehicle::Next(), and Vehicle::UpdateViewport().

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 AccelerationCache::cached_air_drag, AccelerationCache::cached_axle_resistance, TrainCache::cached_max_rail_speed, TrainCache::cached_max_speed, AccelerationCache::cached_max_te, AccelerationCache::cached_power, SpecializedVehicle< Train, VEH_TRAIN >::First(), GetRailType(), GetRailTypeInfo(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::index, RailtypeInfo::max_speed, min(), SetWindowDirty(), SetWindowWidgetDirty(), and Vehicle::vehstatus.

Referenced by CargoChanged(), and RailtypeChanged().

void Train::RailtypeChanged | ( |  | ) |

Update visual effect, power and acceleration caches.

Called when a vehicle in the consist enters a different railtype.

Definition at line 203 of file train_cmd.cpp.

References IsFrontEngine(), and PowerChanged().

bool Train::Tick | ( |  | ) | `[virtual]`

Calls the tick handler of the vehicle.

Returns:
  is this vehicle still valid?

Reimplemented from Vehicle.

Definition at line 4110 of file train_cmd.cpp.

References Vehicle::cur_speed, Vehicle::current_order_time, IsFreeWagon(), IsFrontEngine(), Vehicle::running_ticks, Vehicle::tick_counter, and Vehicle::vehstatus.

void Train::UpdateDeltaXY | ( | Direction | direction | ) | `[virtual]`

Updates the x and y offsets and the size of the sprite used for this vehicle.

Parameters:

  | direction | the direction the vehicle is facing

Reimplemented from Vehicle.

Definition at line 1584 of file train_cmd.cpp.

References GB().

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

References _settings_game, Vehicle::acceleration, AS_BRAKE, Clamp(), Vehicle::cur_speed, GetAcceleration(), GetAccelerationStatus(), GetCurrentMaxSpeed(), Vehicle::max_speed, Vehicle::subspeed, VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

void Train::UpdateVisualEffect | ( | bool | allow_power_change | ) | `[protected]`

Update the cached visual effect.

Parameters:

  | allow_power_change | true if the wagon-is-powered-state may change.

Definition at line 217 of file train_cmd.cpp.

References TrainCache::cached_vis_effect, CALLBACK_FAILED, EngineInfo::callback_mask, CBID_TRAIN_WAGON_POWER, CBM_TRAIN_WAGON_POWER, RailVehicleInfo::engclass, GB(), GBUG_VEH_POWERED_WAGON, SpecializedVehicle< Train, VEH_TRAIN >::Get(), GetVehicleCallback(), HasBit(), IsArticulatedPart(), IsWagon(), ShowNewGrfVehicleError(), and RailVehicleInfo::visual_effect.

## 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 129 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