OpenTTD Source: src/train.h File Reference

OpenTTD Source 15.0-RC2

Data Structures | Typedefs | Enumerations | Functions | Variables
train.h File Reference

Base for the train class. More...

`#include "core/enum_type.hpp"`
`#include "newgrf_engine.h"`
`#include "cargotype.h"`
`#include "rail.h"`
`#include "engine_base.h"`
`#include "rail_map.h"`
`#include "ground_vehicle.hpp"`

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...

## Typedefs
using | VehicleRailFlags = EnumBitSet< VehicleRailFlag, uint16_t >
using | ConsistChangeFlags = EnumBitSet< ConsistChangeFlag, uint8_t >

## Enumerations
enum | VehicleRailFlag : uint8_t { Reversing = 0 , PoweredWagon = 3 , Flipped = 4 , AllowedOnNormalRail = 6 , Reversed = 7 , Stuck = 8 , LeavingStation = 9 }
| Rail vehicle flags. More...
enum | TrainForceProceeding : uint8_t { TFP_NONE = 0 , TFP_STUCK = 1 , TFP_SIGNAL = 2 }
| Modes for ignoring signals. More...
enum class | ConsistChangeFlag : uint8_t { Length , Capacity }
| Flags for Train::ConsistChanged. More...

## Functions
uint8_t | FreightWagonMult (CargoType cargo)
| Return the cargo weight multiplier to use for a rail vehicle.
void | CheckTrainsLengths ()
| Checks if lengths of all rail vehicles are valid.
void | FreeTrainTrackReservation (const Train *v)
| 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.
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 | GetTrainSpriteSize (EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
| Get the size of the sprite of a train sprite heading west, or both heads (used for lists).
bool | TrainOnCrossing (TileIndex tile)
| Check if a level crossing tile has a train on it.
void | NormalizeTrainVehInDepot (const Train *u)
| Move all free vehicles in the depot to the train.

## Variables
static constexpr ConsistChangeFlags | CCF_TRACK {}
| Valid changes while vehicle is driving, and possibly changing tracks.
static constexpr ConsistChangeFlags | CCF_LOADUNLOAD {}
| Valid changes while vehicle is loading/unloading.
static constexpr ConsistChangeFlags | CCF_AUTOREFIT {ConsistChangeFlag::Capacity}
| Valid changes for autorefitting in stations.
static constexpr ConsistChangeFlags | CCF_REFIT {ConsistChangeFlag::Length, ConsistChangeFlag::Capacity}
| Valid changes for refitting in a depot.
static constexpr ConsistChangeFlags | CCF_ARRANGE {ConsistChangeFlag::Length, ConsistChangeFlag::Capacity}
| Valid changes for arranging the consist in a depot.
static constexpr ConsistChangeFlags | CCF_SAVELOAD {ConsistChangeFlag::Length}
| Valid changes when loading a savegame. (Everything that is not stored in the save.)

## Detailed Description

Base for the train class.

Definition in file train.h.

## Typedef Documentation

## ◆ ConsistChangeFlags

using ConsistChangeFlags = EnumBitSet<ConsistChangeFlag, uint8_t>

Definition at line 50 of file train.h.

## ◆ VehicleRailFlags

using VehicleRailFlags = EnumBitSet<VehicleRailFlag, uint16_t>

Definition at line 35 of file train.h.

## Enumeration Type Documentation

## ◆ ConsistChangeFlag

enum class ConsistChangeFlag : uint8_t
| strong

Flags for Train::ConsistChanged.

Enumerator
Length | Allow vehicles to change length.
Capacity | Allow vehicles to change capacity.

Definition at line 45 of file train.h.

## ◆ TrainForceProceeding

enum TrainForceProceeding : uint8_t

Modes for ignoring signals.

Enumerator
TFP_NONE | Normal operation.
TFP_STUCK | Proceed till next signal, but ignore being stuck till then. This includes force leaving depots.
TFP_SIGNAL | Ignore next signal, after the signal ignore being stuck.

Definition at line 38 of file train.h.

## ◆ VehicleRailFlag

enum VehicleRailFlag : uint8_t

Rail vehicle flags.

Enumerator
Reversing | Train is slowing down to reverse.
PoweredWagon | Wagon is powered.
Flipped | Reverse the visible direction of the vehicle.
AllowedOnNormalRail | Electric train engine is allowed to run on normal rail. *‍/.
Reversed | Used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only).
Stuck | Train can't get a path reservation.
LeavingStation | Train is just leaving a station.

Definition at line 25 of file train.h.

## Function Documentation

## ◆ CheckTrainsLengths()

void CheckTrainsLengths | ( |  | ) |

Checks if lengths of all rail vehicles are valid.

If not, shows an error message.

Definition at line 75 of file train_cmd.cpp.

References _networking, abs(), Crashed, Error, GetEncodedString(), SpecializedVehicle< T, Type >::Iterate(), SpecializedVehicle< T, Type >::Next(), ShowErrorMessage(), TicksToLeaveDepot(), TRACK_BIT_DEPOT, and WL_CRITICAL.

Referenced by InitializeWindowsAndCaches(), and ReloadNewGRFData().

## ◆ FreeTrainTrackReservation()

void FreeTrainTrackReservation | ( | const Train * | v | ) |

Free the reserved path in front of a vehicle.

Parameters

  v | Train owning the reserved path.

Definition at line 2416 of file train_cmd.cpp.

References AddSideToSignalBuffer(), ClearPathReservation(), CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::Follow(), GetAllCompatibleRailTypes(), GetRailDepotDirection(), GetReservedTrackbits(), GetSignalStateByTrackdir(), GetStationIndex(), Train::GetVehicleTrackdir(), HasPbsSignalOnTrackdir(), HasSignalOnTrackdir(), CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::is_bridge, CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::is_station, CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::is_tunnel, Vehicle::IsFrontEngine(), IsOnewaySignal(), IsRailDepotTile(), IsRailStationTile(), IsTileType(), IsValidTrackdir(), MarkTileDirtyByTile(), MP_RAILWAY, MP_TUNNELBRIDGE, CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::new_td_bits, CFollowTrackT< Ttr_type_, VehicleType, T90deg_turns_allowed_, Tmask_reserved_tracks >::new_tile, SpecializedVehicle< T, Type >::Next(), Vehicle::owner, RemoveFirstTrackdir(), ReverseTrackdir(), SetSignalStateByTrackdir(), SIGNAL_STATE_RED, Vehicle::tile, TRACK_BIT_DEPOT, TrackBitsToTrackdirBits(), TRACKDIR_BIT_NONE, TrackdirToExitdir(), TrackdirToTrack(), TracksOverlap(), TrackToTrackBits(), UnreserveRailTrack(), and UpdateSignalsInBuffer().

Referenced by CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), Train::Crash(), DoClearBridge(), DoClearTunnel(), FreeTrainReservation(), and ReverseTrainDirection().

## ◆ FreightWagonMult()

uint8_t FreightWagonMult | ( | CargoType | 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 68 of file train_cmd.cpp.

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

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

## ◆ GetTrainSpriteSize()

void GetTrainSpriteSize | ( | EngineID | engine,
|  | uint & | width,
|  | uint & | height,
|  | int & | xoffs,
|  | int & | yoffs,
|  | EngineImageType | image_type
| ) |  |

Get the size of the sprite of a train sprite heading west, or both heads (used for lists).

Parameters

  | engine | The engine to get the sprite from.
  [out] | width | The width of the sprite.
  [out] | height | The height of the sprite.
  [out] | xoffs | Number of pixels to shift the sprite to the right.
  [out] | yoffs | Number of pixels to shift the sprite downwards.
  | image_type | Context the sprite is used in.

Definition at line 579 of file train_cmd.cpp.

References VehicleSpriteSeq::GetBounds(), Rect::Height(), RAILVEH_MULTIHEAD, ScaleSpriteTrad(), UnScaleGUI(), and Rect::Width().

Referenced by EnginePreviewWindow::UpdateWidgetSize().

## ◆ GetTrainStopLocation()

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

References GroundVehicleCache::cached_total_length, GroundVehicleCache::cached_veh_length, Vehicle::current_order, Vehicle::direction, DirToDiagDir(), FarEnd, GroundVehicle< T, Type >::gcache, SpecializedStation< Station, false >::Get(), Order::GetDestination(), Station::GetPlatformLength(), Order::GetStopLocation(), Order::IsType(), Middle, NearEnd, and TILE_SIZE.

Referenced by Train::GetCurrentMaxSpeed().

## ◆ NormalizeTrainVehInDepot()

void NormalizeTrainVehInDepot | ( | const Train * | u | ) |

Move all free vehicles in the depot to the train.

Definition at line 714 of file train_cmd.cpp.

References Execute, GetFreeWagonsInDepot(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, GroundVehicle< T, Type >::IsEngine(), and Vehicle::tile.

Referenced by CmdBuildVehicle().

## ◆ TrainOnCrossing()

bool TrainOnCrossing | ( | TileIndex | tile | ) |

Check if a level crossing tile has a train on it.

Parameters

  tile | tile to test

Returns
  true if a train is on the crossing

Precondition
  tile is a level crossing

Definition at line 1699 of file train_cmd.cpp.

References HasVehicleOnTile(), IsLevelCrossingTile(), and IsTrain().

Referenced by CheckLevelCrossing().

## ◆ TryPathReserve()

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

References _settings_client, DiagdirReachesTrackdirs(), DiagdirReachesTracks(), FollowTrainReservation(), GetRailDepotDirection(), GetTileRailType(), GetTileTrackStatus(), ClientSettings::gui, HasDepotReservation(), HasReservedTracks(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, Vehicle::IsFrontEngine(), MarkTileDirtyByTile(), MarkTrainAsStuck(), PBSTileInfo::okay, Rail90DegTurnDisallowed(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Reset(), SetDepotReservation(), SetWindowWidgetDirty(), GUISettings::show_track_reservation, Stuck, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), PBSTileInfo::tile, Vehicle::tile, TileAddByDiagDir(), TRACK_BIT_DEPOT, PBSTileInfo::trackdir, TrackdirBitsToTrackBits(), TrackdirToExitdir(), TrackdirToTrack(), TrackStatusToTrackdirBits(), TRANSPORT_RAIL, Train::wait_counter, WC_VEHICLE_VIEW, and WID_VV_START_STOP.

Referenced by CheckTrainStayInDepot(), CmdBuildSingleSignal(), CmdConvertRail(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), DoClearBridge(), DoClearTunnel(), RestoreTrainReservation(), ReverseTrainDirection(), and TrainController().

## Variable Documentation

## ◆ CCF_ARRANGE

constexpr ConsistChangeFlags CCF_ARRANGE {ConsistChangeFlag::Length, ConsistChangeFlag::Capacity}
| staticconstexpr

Valid changes for arranging the consist in a depot.

Definition at line 56 of file train.h.

Referenced by CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdReverseTrainDirection(), DeleteLastWagon(), NormaliseTrainHead(), and VehicleEnterDepot().

## ◆ CCF_AUTOREFIT

constexpr ConsistChangeFlags CCF_AUTOREFIT {ConsistChangeFlag::Capacity}
| staticconstexpr

Valid changes for autorefitting in stations.

Definition at line 54 of file train.h.

Referenced by CmdRefitVehicle().

## ◆ CCF_LOADUNLOAD

constexpr ConsistChangeFlags CCF_LOADUNLOAD {}
| staticconstexpr

Valid changes while vehicle is loading/unloading.

Definition at line 53 of file train.h.

Referenced by TransferCargo().

## ◆ CCF_REFIT

constexpr ConsistChangeFlags CCF_REFIT {ConsistChangeFlag::Length, ConsistChangeFlag::Capacity}
| staticconstexpr

Valid changes for refitting in a depot.

Definition at line 55 of file train.h.

Referenced by CmdRefitVehicle().

## ◆ CCF_SAVELOAD

constexpr ConsistChangeFlags CCF_SAVELOAD {ConsistChangeFlag::Length}
| staticconstexpr

Valid changes when loading a savegame. (Everything that is not stored in the save.)

Definition at line 57 of file train.h.

Referenced by AfterLoadVehiclesPhase2().

## ◆ CCF_TRACK

constexpr ConsistChangeFlags CCF_TRACK {}
| staticconstexpr

Valid changes while vehicle is driving, and possibly changing tracks.

Definition at line 52 of file train.h.

Referenced by CheckCaches(), CmdConvertRail(), FixupTrainLengths(), ReverseTrainDirection(), and TrainController().