# 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 | TrainCache
| Variables that are cached to improve performance and such. More...
struct | Train
| 'Train' is either a loco or a wagon. More...

## Defines

#define | FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)

## Enumerations

enum | VehicleRailFlags { VRF_REVERSING = 0, VRF_GOINGUP = 1, VRF_GOINGDOWN = 2, VRF_POWEREDWAGON = 3, VRF_REVERSE_DIRECTION = 4, VRF_NO_PATH_TO_DESTINATION = 5, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, VRF_TOGGLE_REVERSE = 7, VRF_TRAIN_STUCK = 8 }

## Functions

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.
void | UpdateTrainAcceleration (Train *v)
void | CheckTrainsLengths ()
| Checks if lengths of all rail vehicles are valid.
void | FreeTrainTrackReservation (const Train *v, TileIndex origin=INVALID_TILE, Trackdir orig_td=INVALID_TRACKDIR)
| Free the reserved path in front of a vehicle.
bool | TryPathReserve (Train *v, bool mark_as_stuck=false, bool first_tile_okay=false)
| Try to reserve a path to a safe position.
void | DeleteVisibleTrain (Train *v)
| Delete a train while it is visible.
int | GetTrainStopLocation (StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length)
| Get the stop location of (the center) of the front vehicle of a train at a platform of a station.
void | TrainConsistChanged (Train *v, bool same_length)
| Recalculates the cached stuff of a train.
void | TrainPowerChanged (Train *v)
| Recalculates the cached total power of a train.
int | GetTrainCurveSpeedLimit (Train *v)
| Computes train speed limit caused by curves.
Money | GetTrainRunningCost (const Train *v)

## Detailed Description

Base for the train class.

Definition in 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 192 of file train_cmd.cpp.

References abs(), CMD_PAUSE, DoCommandP(), SpecializedVehicle< T, Type >::First(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_STRING_ID, max(), SpecializedVehicle< T, Type >::Next(), PM_PAUSED_ERROR, SetDParam(), ShowErrorMessage(), TicksToLeaveDepot(), and TRACK_BIT_DEPOT.

Referenced by InitializeWindowsAndCaches(), and ReloadNewGRFData().

void DeleteVisibleTrain | ( | Train * | v | ) |

Delete a train while it is visible.

This happens when a company bankrupts when infrastructure sharing is enabled.

Parameters:

  | v | The train to delete.

Definition at line 4240 of file train_cmd.cpp.

References DiagDirToDiagTrackBits(), FreeTrainTrackReservation(), GetTileOwner(), GetTunnelBridgeDirection(), HasReservedTracks(), INVALID_DIAGDIR, INVALID_TILE, IsRailDepotTile(), IsTileType(), SpecializedVehicle< T, Type >::Last(), MP_TUNNELBRIDGE, SpecializedVehicle< T, Type >::Previous(), SetSignalsOnBothDir(), Train::track, TRACK_BIT_WORMHOLE, TrackBitsToTrack(), TrainApproachingCrossingTile(), UnreserveRailTrack(), UpdateLevelCrossing(), and UpdateSignalsOnSegment().

Referenced by RemoveAndSellVehicle().

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

Free the reserved path in front of a vehicle.

Definition at line 2400 of file train_cmd.cpp.

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

Referenced by CmdBuildRailStation(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), DeleteVisibleTrain(), FixAllReservations(), RemoveFromRailBaseStation(), and RemoveRailStation().

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

References _settings_game, VehicleSettings::freight_trains, CargoSpec::Get(), and GameSettings::vehicle.

Referenced by DrawTrainDetails(), TrainCargoChanged(), TrainDetailsCapacityTab(), and TrainDetailsCargoTab().

int GetTrainCurveSpeedLimit | ( | Train * | v | ) |

Computes train speed limit caused by curves.

Parameters:

  | v | first vehicle of consist

Returns:
  imposed speed limit

Definition at line 418 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(), GetRailTypeInfo(), SpecializedVehicle< T, Type >::Next(), Train::railtype, Train::tcache, VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

Referenced by GetTrainAcceleration(), and TrainConsistChanged().

int GetTrainStopLocation | ( | StationID | station_id,
|  | TileIndex | tile,
|  | const Train * | v,
|  | int * | station_ahead,
|  | int * | station_length |
| ) |  |  |

Get the stop location of (the center) of the front vehicle of a train at a platform of a station.

Parameters:

  | station_id | the ID of the station where we're stopping
  | tile | the tile where the vehicle currently is
  | v | the vehicle to get the stop location of
  | station_ahead | 'return' the amount of 1/16th tiles in front of the train
  | station_length | 'return' the station length in 1/16th tiles

Returns:
  the location, calculated from the begin of the station to stop at.

Definition at line 373 of file train_cmd.cpp.

References TrainCache::cached_total_length, TrainCache::cached_veh_length, DirToDiagDir(), SpecializedStation< Station, false >::Get(), Station::GetPlatformLength(), OSL_PLATFORM_FAR_END, OSL_PLATFORM_MIDDLE, OSL_PLATFORM_NEAR_END, Train::tcache, and TILE_SIZE.

Referenced by GetTrainAcceleration().

void TrainConsistChanged | ( | Train * | 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 222 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, CBID_TRAIN_WAGON_POWER, CBID_VEHICLE_LENGTH, CBM_TRAIN_WAGON_POWER, CBM_VEHICLE_LENGTH, Clamp(), ClrBit(), Train::compatible_railtypes, EF_RAIL_TILTS, GB(), GetRailTypeInfo(), GetTrainCurveSpeedLimit(), GetVehicleCallback(), GetVehicleCapacity(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Train::IsFreeWagon(), Train::IsFrontEngine(), min(), SpecializedVehicle< T, Type >::Next(), RailtypeInfo::powered_railtypes, PROP_TRAIN_SPEED, PROP_TRAIN_USER_DATA, Train::railtype, RAILTYPE_RAIL, RAILTYPES_NONE, RAILTYPES_RAIL, RAILVEH_WAGON, RailVehicleLengthChanged(), SetBit(), SetWindowDirty(), Train::tcache, TrainCargoChanged(), TrainCache::user_def_data, UsesWagonOverride(), GameSettings::vehicle, and VehicleSettings::wagon_speed_limits.

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

void TrainPowerChanged | ( | Train * | 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 94 of file train_cmd.cpp.

References TrainCache::cached_max_te, TrainCache::cached_power, GetRailType(), HasBit(), HasPowerOnRail(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, SpecializedVehicle< T, Type >::Next(), PROP_TRAIN_POWER, PROP_TRAIN_TRACTIVE_EFFORT, Train::railtype, SetWindowDirty(), SetWindowWidgetDirty(), and Train::tcache.

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

bool TryPathReserve | ( | Train * | 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 2845 of file train_cmd.cpp.

References _settings_client, _settings_game, ClrBit(), DiagdirReachesTrackdirs(), DiagdirReachesTracks(), FindFirstTrackdir(), Train::flags, FollowTrainReservation(), PathfinderSettings::forbid_90_deg, GetRailDepotDirection(), GetReservedTrackbits(), GetTileTrackStatus(), ClientSettings::gui, HasBit(), HasDepotReservation(), HasReservedTracks(), HasSignalOnTrackdir(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Train::IsFrontEngine(), IsTileType(), MarkTileDirtyByTile(), MarkTrainAsStuck(), MP_RAILWAY, PBSTileInfo::okay, GameSettings::pf, ReverseTrackdir(), SetDepotReservation(), SetWindowWidgetDirty(), GUISettings::show_track_reservation, PBSTileInfo::tile, TileAddByDiagDir(), Train::track, TRACK_BIT_DEPOT, TrackBitsToTrackdirBits(), TrackCrossesTracks(), PBSTileInfo::trackdir, TrackdirBitsToTrackBits(), TrackdirToExitdir(), TrackdirToTrack(), TrackStatusToTrackdirBits(), TrainExitDir(), and TRANSPORT_RAIL.

Referenced by CmdBuildRailStation(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), FixAllReservations(), RemoveFromRailBaseStation(), and RemoveRailStation().