Public Member Functions | Data Fields | Protected Member Functions | Friends

Train Struct Reference

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

#include <train.h>

Inheritance diagram for Train:
GroundVehicle< Train, VEH_TRAIN > SpecializedVehicle< Train, Type > Vehicle Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool > BaseVehicle

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.
void MarkDirty ()
 Goods at the consist have changed, update the graphics, cargo, and acceleration.
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 a sound for a train leaving the station.
bool IsPrimaryVehicle () const
 Whether this is the primary vehicle in the chain.
SpriteID GetImage (Direction direction, EngineImageType image_type) const
 Get the sprite to display the train.
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
 Get running cost for the train consist.
int GetDisplayImageWidth (Point *offset=NULL) const
 Get the width of a train vehicle image in the GUI.
bool IsInDepot () const
 Is the whole consist the in a depot?
bool IsStoppedInDepot () const
 Is the train stopped in a depot?
bool Tick ()
 Update train vehicle data for a tick.
void OnNewDay ()
 Update day counters of the train vehicle.
uint Crash (bool flooded=false)
 The train vehicle crashed! Update its status and other parts around it.
Trackdir GetVehicleTrackdir () const
 Get the tracks of the train vehicle.
TileIndex GetOrderStationLocation (StationID station)
 Get the location of the next station to visit.
bool FindClosestDepot (TileIndex *location, DestinationID *destination, bool *reverse)
 Locate the closest depot for this consist, and return the information to the caller.
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.
int UpdateSpeed ()
 This function looks at the vehicle and updates its speed (cur_speed and subspeed) variables.
void UpdateAcceleration ()
 Update acceleration of the train from the cached power and weight.
int GetCurrentMaxSpeed () const
 Calculates the maximum speed of the vehicle under its current conditions.
TrainGetNextUnit () const
 Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
TrainGetPrevUnit ()
 Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
int CalcNextVehicleOffset () const
 Calculate the offset from this vehicle's center to the following center taking the vehicle lengths into account.

Data Fields

TrainCache tcache
Trainother_multiheaded_part
uint16 crash_anim_pos
 Crash animation counter.
uint16 flags
TrackBitsByte track
TrainForceProceedingByte 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

uint16 GetPower () const
 Allows to know the power value that this vehicle will use.
uint16 GetPoweredPartPower (const Train *head) const
 Returns a value if this articulated part is powered.
uint16 GetWeight () const
 Allows to know the weight value that this vehicle will use.
byte GetTractiveEffort () const
 Allows to know the tractive effort value that this vehicle will use.
byte GetAirDragArea () const
 Gets the area used for calculating air drag.
byte GetAirDrag () const
 Gets the air drag coefficient of this vehicle.
AccelStatus GetAccelerationStatus () const
 Checks the current acceleration status of this vehicle.
uint16 GetCurrentSpeed () const
 Calculates the current speed of this vehicle.
uint32 GetRollingFriction () const
 Returns the rolling friction coefficient of this vehicle.
int GetAccelerationType () const
 Allows to know the acceleration type of a vehicle.
uint32 GetSlopeSteepness () const
 Returns the slope steepness used by this vehicle.
uint16 GetMaxTrackSpeed () const
 Gets the maximum speed allowed by the track for this vehicle.
bool TileMayHaveSlopedTrack () const
 Checks if the vehicle is at a tile that can be sloped.
bool HasToUseGetSlopePixelZ ()
 Trains can always use the faster algorithm because they have always the same direction as the track under them.

Friends

struct GroundVehicle< Train, VEH_TRAIN >

Detailed Description

'Train' is either a loco or a wagon.

Definition at line 70 of file train.h.


Constructor & Destructor Documentation

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

We want to 'destruct' the right class.

Definition at line 90 of file train.h.

References Vehicle::PreDestructor().


Member Function Documentation

int Train::CalcNextVehicleOffset (  )  const [inline]

Calculate the offset from this vehicle's center to the following center taking the vehicle lengths into account.

Returns:
Offset from center to center.

Definition at line 153 of file train.h.

References GroundVehicleCache::cached_veh_length, GroundVehicle< T, Type >::gcache, GroundVehicle< Train, VEH_TRAIN >::gcache, and SpecializedVehicle< Train, Type >::Next().

Referenced by AdvanceWagonsAfterSwap(), AdvanceWagonsBeforeSwap(), TicksToLeaveDepot(), and VehicleEnter_Track().

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

References _settings_game, TrainCache::cached_max_curve_speed, VehicleCache::cached_max_speed, TrainCache::cached_tilt, GroundVehicleCache::cached_total_length, CALLBACK_FAILED, EngineInfo::callback_mask, EngineInfo::cargo_age_period, GroundVehicle< Train, VEH_TRAIN >::CargoChanged(), CBID_VEHICLE_LENGTH, CBM_VEHICLE_LENGTH, Clamp(), ClrBit(), Engine::DetermineCapacity(), EF_RAIL_TILTS, Vehicle::engine_type, ErrorUnknownCallbackResult(), GroundVehicle< Train, VEH_TRAIN >::gcache, GetCurveSpeedLimit(), Engine::GetGRF(), Engine::GetGRFID(), GetRailTypeInfo(), GetVehicleCallback(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::index, INVALID_ENGINE, Vehicle::InvalidateNewGRFCache(), InvalidateWindowData(), GroundVehicle< Train, VEH_TRAIN >::IsFreeWagon(), Vehicle::IsFrontEngine(), RailVehicleInfo::max_speed, min(), EngineInfo::misc_flags, RailVehicleInfo::pow_wag_power, RailVehicleInfo::power, RailtypeInfo::powered_railtypes, PROP_TRAIN_CARGO_AGE_PERIOD, PROP_TRAIN_SHORTEN_FACTOR, PROP_TRAIN_SPEED, PROP_TRAIN_USER_DATA, RAILVEH_WAGON, SetBit(), SetWindowDirty(), RailVehicleInfo::shorten_factor, UpdateAcceleration(), RailVehicleInfo::user_def_data, UsesWagonOverride(), Vehicle::vcache, VE_DISABLE_WAGON_POWER, GameSettings::vehicle, VEHICLE_LENGTH, VehicleLengthChanged(), VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL, VRF_POWEREDWAGON, VehicleSettings::wagon_speed_limits, WC_VEHICLE_DETAILS, and WC_VEHICLE_REFIT.

Referenced by AfterLoadGame(), AfterLoadVehicles(), CheckCaches(), CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdRefitVehicle(), CmdReverseTrainDirection(), DeleteLastWagon(), FixupTrainLengths(), NormaliseTrainHead(), ReverseTrainDirection(), SettingsDisableElrail(), TransferCargo(), and VehicleEnterDepot().

uint Train::Crash ( bool  flooded = false  )  [virtual]
bool Train::FindClosestDepot ( TileIndex location,
DestinationID *  destination,
bool *  reverse 
) [virtual]

Locate the closest depot for this consist, and return the information to the caller.

Parameters:
location [out] If not NULL and a depot is found, store its location in the given address.
destination [out] If not NULL and a depot is found, store its index in the given address.
reverse [out] If not NULL and a depot is found, store reversal information in the given address.
Returns:
A depot has been found.

Reimplemented from Vehicle.

Definition at line 1983 of file train_cmd.cpp.

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

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

Checks the current acceleration status of this vehicle.

Returns:
Acceleration status.

Definition at line 248 of file train.h.

References AS_BRAKE, HasBit(), Vehicle::vehstatus, VRF_TRAIN_STUCK, and VS_STOPPED.

Referenced by UpdateSpeed().

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

Allows to know the acceleration type of a vehicle.

Returns:
Acceleration type of the vehicle.

Definition at line 278 of file train.h.

References RailtypeInfo::acceleration_type, and GetRailTypeInfo().

byte Train::GetAirDrag (  )  const [inline, protected]

Gets the air drag coefficient of this vehicle.

Returns:
Air drag value from the engine.

Definition at line 239 of file train.h.

References Vehicle::engine_type.

byte Train::GetAirDragArea (  )  const [inline, protected]

Gets the area used for calculating air drag.

Returns:
Area of the engine in m^2.

Definition at line 229 of file train.h.

References TRACK_BIT_WORMHOLE, Vehicle::vehstatus, and VS_HIDDEN.

int Train::GetCurrentMaxSpeed (  )  const
uint16 Train::GetCurrentSpeed (  )  const [inline, protected]

Calculates the current speed of this vehicle.

Returns:
Current speed in km/h-ish.

Definition at line 257 of file train.h.

References Vehicle::cur_speed.

Referenced by GetRollingFriction().

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

References GroundVehicleCache::cached_veh_length, GroundVehicle< Train, VEH_TRAIN >::gcache, Vehicle::GetEngine(), Engine::GetGRF(), GRFFile::traininfo_vehicle_pitch, GRFFile::traininfo_vehicle_width, and VEHICLE_LENGTH.

Referenced by DrawTrainDetails(), DrawTrainImage(), GetLengthOfArticulatedVehicle(), GetVehicleWidth(), HighlightDragPosition(), 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 101 of file train.h.

References VehicleCache::cached_max_speed, and Vehicle::vcache.

Referenced by CYapfCostRailT< Types >::PfCalcCost().

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

References GroundVehicle< Train, VEH_TRAIN >::gcache, and GroundVehicleCache::last_speed.

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

References EXPENSES_TRAIN_INC.

SpriteID Train::GetImage ( Direction  direction,
EngineImageType  image_type 
) const [virtual]

Get the sprite to display the train.

Parameters:
direction Direction of view/travel.
image_type Visualisation context.
Returns:
Sprite to display.

Reimplemented from Vehicle.

Definition at line 456 of file train_cmd.cpp.

References Vehicle::cargo, Vehicle::cargo_cap, CargoList< Tinst >::Count(), Vehicle::GetEngine(), HasBit(), Engine::original_image_index, ReverseDir(), and VRF_REVERSE_DIRECTION.

Referenced by ChangeTrainDirRandomly(), and DrawTrainDetails().

uint16 Train::GetMaxTrackSpeed (  )  const [inline, protected]

Gets the maximum speed allowed by the track for this vehicle.

Returns:
Maximum speed allowed.

Definition at line 296 of file train.h.

References GetRailType(), GetRailTypeInfo(), RailtypeInfo::max_speed, and Vehicle::tile.

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

References SpecializedVehicle< T, Type >::GetNextVehicle(), SpecializedVehicle< Train, Type >::GetNextVehicle(), and GroundVehicle< T, Type >::IsRearDualheaded().

Referenced by ArrangeTrains(), CmdAutoreplaceVehicle(), Vehicle::NeedsServicing(), and ReplaceChain().

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

Get the location of the next station to visit.

Parameters:
station Next station to visit.
Returns:
Location of the new station.

Reimplemented from Vehicle.

Definition at line 2724 of file train_cmd.cpp.

References FACIL_TRAIN, BaseStation::facilities, SpecializedVehicle< Train, Type >::Get(), Vehicle::IncrementRealOrderIndex(), Vehicle::last_station_visited, and BaseStation::xy.

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

References Vehicle::engine_type, GetRailType(), HasPowerOnRail(), Vehicle::IsArticulatedPart(), GroundVehicle< Train, VEH_TRAIN >::IsMultiheaded(), PROP_TRAIN_POWER, and Vehicle::tile.

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

References GroundVehicleCache::first_engine, GroundVehicle< Train, VEH_TRAIN >::gcache, GetRailType(), HasBit(), HasPowerOnRail(), Vehicle::tile, and VRF_POWEREDWAGON.

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

References SpecializedVehicle< T, Type >::GetPrevVehicle(), SpecializedVehicle< Train, Type >::GetPrevVehicle(), and GroundVehicle< T, Type >::IsRearDualheaded().

Referenced by ReplaceChain().

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

Returns the rolling friction coefficient of this vehicle.

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

Definition at line 266 of file train.h.

References GetCurrentSpeed().

Money Train::GetRunningCost (  )  const [virtual]

Get running cost for the train consist.

Returns:
Yearly running costs.

Reimplemented from Vehicle.

Definition at line 3848 of file train_cmd.cpp.

References Vehicle::GetEngine(), Engine::GetGRF(), SpecializedVehicle< T, Type >::GetNextVehicle(), GetPrice(), GroundVehicle< T, Type >::IsMultiheaded(), PROP_TRAIN_RUNNING_COST_FACTOR, and RailVehicleInfo::running_cost.

Referenced by OnNewDay().

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

Returns the slope steepness used by this vehicle.

Returns:
Slope steepness used by the vehicle.

Definition at line 287 of file train.h.

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

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

References Vehicle::engine_type, and PROP_TRAIN_TRACTIVE_EFFORT.

uint16 Train::GetWeight (  )  const [inline, protected]
bool Train::HasToUseGetSlopePixelZ (  )  [inline, protected]

Trains can always use the faster algorithm because they have always the same direction as the track under them.

Returns:
false

Definition at line 316 of file train.h.

bool Train::IsInDepot (  )  const [virtual]

Is the whole consist the in a depot?

Returns:
true iff all vehicles of the train are in a depot.

Reimplemented from Vehicle.

Definition at line 737 of file train_cmd.cpp.

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

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

bool Train::IsStoppedInDepot (  )  const [virtual]

Is the train stopped in a depot?

Returns:
True if the train is stopped in a depot, else false.

Reimplemented from Vehicle.

Definition at line 754 of file train_cmd.cpp.

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

Referenced by CmdMoveRailVehicle(), and CmdReverseTrainDirection().

void Train::MarkDirty (  )  [virtual]

Goods at the consist have changed, update the graphics, cargo, and acceleration.

Reimplemented from Vehicle.

Definition at line 2739 of file train_cmd.cpp.

References GroundVehicle< Train, VEH_TRAIN >::CargoChanged(), SpecializedVehicle< T, Type >::Next(), UpdateAcceleration(), and SpecializedVehicle< T, Type >::UpdateViewport().

void Train::PlayLeaveStationSound (  )  const [virtual]

Play a sound for a train leaving the station.

Reimplemented from Vehicle.

Definition at line 1996 of file train_cmd.cpp.

References Vehicle::engine_type, PlayVehicleSound(), and VSE_START.

Referenced by CheckTrainStayInDepot().

void Train::ReserveTrackUnderConsist (  )  const
bool Train::Tick (  )  [virtual]

Update train vehicle data for a tick.

Returns:
True if the vehicle still exists, false if it has ceased to exist (front of consists only).

Reimplemented from Vehicle.

Definition at line 3873 of file train_cmd.cpp.

References crash_anim_pos, Vehicle::cur_speed, Vehicle::current_order_time, GroundVehicle< Train, VEH_TRAIN >::IsFreeWagon(), Vehicle::IsFrontEngine(), Vehicle::running_ticks, Vehicle::tick_counter, Vehicle::vehstatus, VS_CRASHED, and VS_STOPPED.

bool Train::TileMayHaveSlopedTrack (  )  const [inline, protected]

Checks if the vehicle is at a tile that can be sloped.

Returns:
True if the tile can be sloped.

Definition at line 305 of file train.h.

References TRACK_BIT_X.

void Train::UpdateAcceleration (  ) 
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 1388 of file train_cmd.cpp.

References GroundVehicleCache::cached_veh_length, DIR_NE, DIR_NW, DIR_SE, DIR_SW, GroundVehicle< Train, VEH_TRAIN >::gcache, IsDiagonalDirection(), VEHICLE_LENGTH, Vehicle::x_bb_offs, Vehicle::x_extent, Vehicle::x_offs, Vehicle::y_bb_offs, Vehicle::y_extent, Vehicle::y_offs, and Vehicle::z_extent.

Referenced by ChangeTrainDirRandomly().

int Train::UpdateSpeed (  ) 

This function looks at the vehicle and updates its speed (cur_speed and subspeed) variables.

Furthermore, it returns the distance that the train can drive this tick. Vehicle::GetAdvanceDistance() determines the distance to drive before moving a step on the map.

Returns:
distance to drive.

Definition at line 2758 of file train_cmd.cpp.

References _settings_game, Vehicle::acceleration, AS_BRAKE, GroundVehicleCache::cached_max_track_speed, GroundVehicle< Train, VEH_TRAIN >::DoUpdateSpeed(), GroundVehicle< Train, VEH_TRAIN >::gcache, GroundVehicle< Train, VEH_TRAIN >::GetAcceleration(), GetAccelerationStatus(), GetCurrentMaxSpeed(), min(), VehicleSettings::train_acceleration_model, and GameSettings::vehicle.


Field Documentation

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

Definition at line 85 of file train.h.

Referenced by AfterLoadGame(), CheckTrainStayInDepot(), MarkTrainAsStuck(), ReverseTrainDirection(), TryPathReserve(), and VehicleEnterDepot().


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