Train Struct Reference

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

#include <train.h>

Inheritance diagram for Train:

SpecializedVehicle< Train, VEH_TRAIN > Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >

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
void MarkDirty ()
void UpdateDeltaXY (Direction direction)
ExpensesType GetExpenseType (bool income) const
void PlayLeaveStationSound () const
bool IsPrimaryVehicle () const
SpriteID GetImage (Direction direction) const
int GetDisplaySpeed () const
int GetDisplayMaxSpeed () const
Money GetRunningCost () const
int GetDisplayImageWidth (Point *offset=NULL) const
 Get the width of a train vehicle image in the GUI.
bool IsInDepot () const
bool IsStoppedInDepot () const
bool Tick ()
void OnNewDay ()
uint Crash (bool flooded=false)
Trackdir GetVehicleTrackdir () const
TileIndex GetOrderStationLocation (StationID station)
bool FindClosestDepot (TileIndex *location, DestinationID *destination, bool *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.
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 TrainGetNextArticPart () const
 Get the next part of a multi-part engine.
FORCEINLINE TrainGetFirstEnginePart ()
 Get the first part of a multi-part engine.
FORCEINLINE const TrainGetFirstEnginePart () const
 Get the first part of a multi-part engine.
FORCEINLINE TrainGetLastEnginePart ()
 Get the last part of a multi-part engine.
FORCEINLINE TrainGetNextVehicle () const
 Get the next real (non-articulated part) vehicle in the consist.
FORCEINLINE TrainGetPrevVehicle () const
 Get the previous real (non-articulated part) vehicle in the consist.
FORCEINLINE TrainGetNextUnit () const
 Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
FORCEINLINE TrainGetPrevUnit ()
 Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.

Data Fields

TrainCache tcache
Trainother_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

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.

Detailed Description

'Train' is either a loco or a wagon.

Definition at line 105 of file train.h.


Member Enumeration Documentation

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


Constructor & Destructor Documentation

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

We want to 'destruct' the right class.

Definition at line 125 of file train.h.


Member Function Documentation

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

References TrainCache::cached_veh_length, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::Get(), Point::x, and Point::y.

Referenced by DrawTrainDetails(), DrawTrainImage(), and GetLengthOfArticulatedVehicle().

int Train::GetCurveSpeedLimit (  )  const

void Train::ConsistChanged ( bool  same_length  ) 

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

References _settings_game, TrainCache::cached_weight, SpecializedVehicle< Train, VEH_TRAIN >::First(), PowerChanged(), VehicleSettings::train_slope_steepness, and GameSettings::vehicle.

Referenced by ConsistChanged(), and TrainSlopeSteepnessChanged().

void Train::PowerChanged (  ) 

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

References _settings_game, AS_BRAKE, Clamp(), GetAcceleration(), GetAccelerationStatus(), GetCurrentMaxSpeed(), VehicleSettings::train_acceleration_model, and GameSettings::vehicle.

int Train::GetCurrentMaxSpeed (  )  const

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

Returns:
Maximum speed of the vehicle.

Definition at line 457 of file train_cmd.cpp.

References TrainCache::cached_max_curve_speed, TrainCache::cached_max_speed, GetCurveSpeedLimit(), GetStationIndex(), GetTrainStopLocation(), IsRailStationTile(), max(), min(), TILE_SIZE, and TRACK_BIT_DEPOT.

Referenced by UpdateSpeed().

int Train::GetAcceleration (  )  const

Calculates the acceleration of the vehicle under its current conditions.

Returns:
Current acceleration of the vehicle.

Definition at line 502 of file train_cmd.cpp.

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

Referenced by UpdateSpeed().

FORCEINLINE bool Train::IsFrontEngine (  )  const [inline]

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

References HasBit(), and TS_FREE_WAGON.

Referenced by AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), ConsistChanged(), NormalizeTrainVehInDepot(), and UpdateTrainGroupID().

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

References HasBit(), and TS_ENGINE.

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

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

Check if a train is a wagon.

Returns:
Returns true if vehicle is a wagon

Definition at line 259 of file train.h.

References HasBit(), and TS_WAGON.

Referenced by NormaliseSubtypes().

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

References HasBit(), and TS_MULTIHEADED.

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

References IsEngine(), and IsMultiheaded().

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

FORCEINLINE bool Train::IsArticulatedPart (  )  const [inline]

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

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

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

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

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

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

FORCEINLINE Train* Train::GetFirstEnginePart (  )  [inline]

Get the first part of a multi-part engine.

Returns:
First part of the engine.

Definition at line 302 of file train.h.

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

Referenced by CmdMoveRailVehicle(), and CmdSellRailWagon().

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

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

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

Get the last part of a multi-part engine.

Returns:
Last part of the engine.

Definition at line 324 of file train.h.

References GetNextArticPart(), and HasArticulatedPart().

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

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

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

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

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

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

Referenced by GetPrevUnit().

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

References GetNextVehicle(), and IsRearDualheaded().

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

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

References GetPrevVehicle(), and IsRearDualheaded().

Referenced by ReplaceChain().

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

References GetRailType(), HasPowerOnRail(), IsArticulatedPart(), IsMultiheaded(), and PROP_TRAIN_POWER.

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

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

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

References TrainCache::first_engine, FreightWagonMult(), CargoSpec::Get(), HasBit(), IsArticulatedPart(), PROP_TRAIN_WEIGHT, and CargoSpec::weight.

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

References PROP_TRAIN_TRACTIVE_EFFORT.

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

Checks the current acceleration status of this vehicle.

Returns:
Acceleration status.

Definition at line 447 of file train.h.

References AS_ACCEL, AS_BRAKE, and HasBit().

Referenced by GetAcceleration(), and UpdateSpeed().

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

Calculates the current speed of this vehicle.

Returns:
Current speed in mph.

Definition at line 456 of file train.h.

Referenced by GetAcceleration().

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

Referenced by GetAcceleration().

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

Calculates the total slope resistance for this vehicle.

Returns:
Slope resistance.

Definition at line 474 of file train.h.

References HasBit().

Referenced by GetAcceleration().

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

References RailtypeInfo::acceleration_type, and GetRailTypeInfo().

Referenced by GetAcceleration().


Field Documentation

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

Definition at line 120 of file train.h.

Referenced by MarkTrainAsStuck(), and TryPathReserve().


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

Generated on Wed Jan 20 23:39:14 2010 for OpenTTD by  doxygen 1.5.6