# train.h File Reference

Base for the train class. More...

`#include "vehicle_base.h"`
`#include "newgrf_engine.h"`
`#include "cargotype.h"`
`#include "rail.h"`
`#include "engine_base.h"`
`#include "rail_map.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 }
enum | AccelStatus { AS_ACCEL, AS_BRAKE }
| What is the status of our acceleration? More...
## Functions
byte | FreightWagonMult (CargoID 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, 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.
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.

## Detailed Description

Base for the train class.

Definition in file train.h.

## Enumeration Type Documentation

enum AccelStatus

What is the status of our acceleration?

Enumerator:

  AS_ACCEL | We want to go faster, if possible ofcourse.
  AS_BRAKE | We want to stop.

Definition at line 97 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 169 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 FreeTrainTrackReservation | ( | const Train * | v,
|  | TileIndex | origin,
|  | Trackdir | orig_td |
| ) |  |  |

Free the reserved path in front of a vehicle.

Definition at line 2373 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(), 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 85 of file train_cmd.cpp.

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

Referenced by DrawTrainDetails(), Train::GetWeight(), TrainDetailsCapacityTab(), and TrainDetailsCargoTab().

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 344 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 Train::GetCurrentMaxSpeed().

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

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

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