OpenTTD
Public Types | Public Member Functions | Data Fields | Protected Member Functions
GroundVehicle< T, Type > Struct Template Reference

Base class for all vehicles that move through ground. More...

#include <ground_vehicle.hpp>

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

Public Types

typedef GroundVehicle< T, Type > GroundVehicleBase
 Our type.
- Public Types inherited from SpecializedVehicle< T, Type >
typedef SpecializedVehicle< T,
Type > 
SpecializedVehicleBase
 Our type.

Public Member Functions

 GroundVehicle ()
 The constructor at SpecializedVehicle must be called.
void PowerChanged ()
 Recalculates the cached total power of a vehicle.
void CargoChanged ()
 Recalculates the cached weight of a vehicle and its parts.
int GetAcceleration () const
 Calculates the acceleration of the vehicle under its current conditions.
bool IsChainInDepot () const
 Check whether the whole vehicle chain is in the depot.
uint Crash (bool flooded)
 Common code executed for crashed ground vehicles.
int64 GetSlopeResistance () const
 Calculates the total slope resistance for this vehicle.
void UpdateZPositionAndInclination ()
 Updates vehicle's Z position and inclination.
void UpdateZPosition ()
 Updates vehicle's Z position.
int UpdateInclination (bool new_tile, bool update_delta)
 Checks if the vehicle is in a slope and sets the required flags in that case.
void SetFrontEngine ()
 Set front engine state.
void ClearFrontEngine ()
 Remove the front engine state.
void SetArticulatedPart ()
 Set a vehicle to be an articulated part.
void ClearArticulatedPart ()
 Clear a vehicle from being an articulated part.
void SetWagon ()
 Set a vehicle to be a wagon.
void ClearWagon ()
 Clear wagon property.
void SetEngine ()
 Set engine status.
void ClearEngine ()
 Clear engine status.
void SetFreeWagon ()
 Set a vehicle as a free wagon.
void ClearFreeWagon ()
 Clear a vehicle from being a free wagon.
void SetMultiheaded ()
 Set a vehicle as a multiheaded engine.
void ClearMultiheaded ()
 Clear multiheaded engine property.
bool IsFreeWagon () const
 Check if the vehicle is a free wagon (got no engine in front of it).
bool IsEngine () const
 Check if a vehicle is an engine (can be first in a consist).
bool IsWagon () const
 Check if a vehicle is a wagon.
bool IsMultiheaded () const
 Check if the vehicle is a multiheaded engine.
bool IsRearDualheaded () const
 Tell if we are dealing with the rear end of a multiheaded engine.
void SetLastSpeed ()
 Update the GUI variant of the current speed of the vehicle.
- Public Member Functions inherited from SpecializedVehicle< T, Type >
 SpecializedVehicle ()
 Set vehicle type correctly.
T * First () const
 Get the first vehicle in the chain.
T * Last ()
 Get the last vehicle in the chain.
const T * Last () const
 Get the last vehicle in the chain.
T * Next () const
 Get next vehicle in the chain.
T * Previous () const
 Get previous vehicle in the chain.
T * GetNextArticulatedPart ()
 Get the next part of an articulated engine.
T * GetNextArticulatedPart () const
 Get the next part of an articulated engine.
T * GetFirstEnginePart ()
 Get the first part of an articulated engine.
const T * GetFirstEnginePart () const
 Get the first part of an articulated engine.
T * GetLastEnginePart ()
 Get the last part of an articulated engine.
T * GetNextVehicle () const
 Get the next real (non-articulated part) vehicle in the consist.
T * GetPrevVehicle () const
 Get the previous real (non-articulated part) vehicle in the consist.
void UpdateViewport (bool force_update, bool update_delta)
 Update vehicle sprite- and position caches.
- Public Member Functions inherited from Vehicle
 Vehicle (VehicleType type=VEH_INVALID)
 Vehicle constructor.
void PreDestructor ()
 Destroy all stuff that (still) needs the virtual functions to work properly.
virtual ~Vehicle ()
 We want to 'destruct' the right class.
void BeginLoading ()
 Prepare everything to begin the loading when arriving at a station.
void CancelReservation (StationID next, Station *st)
 Return all reserved cargo packets to the station and reset all packets staged for transfer.
void LeaveStation ()
 Perform all actions when leaving a station.
GroundVehicleCacheGetGroundVehicleCache ()
 Access the ground vehicle cache of the vehicle.
const GroundVehicleCacheGetGroundVehicleCache () const
 Access the ground vehicle cache of the vehicle.
uint16 & GetGroundVehicleFlags ()
 Access the ground vehicle flags of the vehicle.
const uint16 & GetGroundVehicleFlags () const
 Access the ground vehicle flags of the vehicle.
void DeleteUnreachedImplicitOrders ()
 Delete all implicit orders which were not reached.
void HandleLoading (bool mode=false)
 Handle the loading of the vehicle; when not it skips through dummy orders and does nothing in all other cases.
void GetConsistFreeCapacities (SmallMap< CargoID, uint > &capacities) const
 Get a map of cargoes and free capacities in the consist.
uint GetConsistTotalCapacity () const
virtual void MarkDirty ()
 Marks the vehicles to be redrawn and updates cached variables.
virtual void UpdateDeltaXY (Direction direction)
 Updates the x and y offsets and the size of the sprite used for this vehicle.
uint GetOldAdvanceSpeed (uint speed)
 Determines the effective direction-specific vehicle movement speed.
uint GetAdvanceDistance ()
 Determines the vehicle "progress" needed for moving a step.
virtual ExpensesType GetExpenseType (bool income) const
 Sets the expense type associated to this vehicle type.
virtual void PlayLeaveStationSound () const
 Play the sound associated with leaving the station.
virtual bool IsPrimaryVehicle () const
 Whether this is the primary vehicle in the chain.
const EngineGetEngine () const
 Retrieves the engine of the vehicle.
virtual void GetImage (Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
 Gets the sprite to show for the given direction.
const GRFFileGetGRF () const
 Retrieve the NewGRF the vehicle is tied to.
uint32 GetGRFID () const
 Retrieve the GRF ID of the NewGRF the vehicle is tied to.
void InvalidateNewGRFCache ()
 Invalidates cached NewGRF variables.
void InvalidateNewGRFCacheOfChain ()
 Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle)
bool IsGroundVehicle () const
 Check if the vehicle is a ground vehicle.
virtual int GetDisplaySpeed () const
 Gets the speed in km-ish/h that can be sent into SetDParam for string processing.
virtual int GetDisplayMaxSpeed () const
 Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
virtual int GetCurrentMaxSpeed () const
 Calculates the maximum speed of the vehicle under its current conditions.
virtual Money GetRunningCost () const
 Gets the running cost of a vehicle.
virtual bool IsInDepot () const
 Check whether the vehicle is in the depot.
bool IsStoppedInDepot () const
 Check whether the vehicle is in the depot and stopped.
virtual bool Tick ()
 Calls the tick handler of the vehicle.
virtual void OnNewDay ()
 Calls the new day handler of the vehicle.
virtual Trackdir GetVehicleTrackdir () const
 Returns the Trackdir on which the vehicle is currently located.
Money GetDisplayRunningCost () const
 Gets the running cost of a vehicle that can be sent into SetDParam for string processing.
Money GetDisplayProfitThisYear () const
 Gets the profit vehicle had this year.
Money GetDisplayProfitLastYear () const
 Gets the profit vehicle had last year.
void SetNext (Vehicle *next)
 Set the next vehicle of this vehicle.
VehicleMove (int n)
 Get the vehicle at offset n of this vehicle chain.
const VehicleMove (int n) const
 Get the vehicle at offset n of this vehicle chain.
OrderGetFirstOrder () const
 Get the first order of the vehicles order list.
void AddToShared (Vehicle *shared_chain)
 Adds this vehicle to a shared vehicle chain.
void RemoveFromShared ()
 Removes the vehicle from the shared order list.
VehicleNextShared () const
 Get the next vehicle of the shared vehicle chain.
VehiclePreviousShared () const
 Get the previous vehicle of the shared vehicle chain.
VehicleFirstShared () const
 Get the first vehicle of this vehicle chain.
bool IsOrderListShared () const
 Check if we share our orders with another vehicle.
VehicleOrderID GetNumOrders () const
 Get the number of orders this vehicle has.
VehicleOrderID GetNumManualOrders () const
 Get the number of manually added orders this vehicle has.
StationIDStack GetNextStoppingStation () const
 Get the next station the vehicle will stop at.
void ResetRefitCaps ()
 Reset all refit_cap in the consist to cargo_cap.
void CopyVehicleConfigAndStatistics (const Vehicle *src)
 Copy certain configurations and statistics of a vehicle after successful autoreplace/renew The function shall copy everything that cannot be copied by a command (like orders / group etc), and that shall not be resetted for the new vehicle.
bool HandleBreakdown ()
 Handle all of the aspects of a vehicle breakdown This includes adding smoke and sounds, and ending the breakdown when appropriate.
bool NeedsAutorenewing (const Company *c, bool use_renew_setting=true) const
 Function to tell if a vehicle needs to be autorenewed.
bool NeedsServicing () const
 Check if the vehicle needs to go to a depot in near future (if a opportunity presents itself) for service or replacement.
bool NeedsAutomaticServicing () const
 Checks if the current order should be interrupted for a service-in-depot order.
virtual TileIndex GetOrderStationLocation (StationID station)
 Determine the location for the station where the vehicle goes to next.
virtual bool FindClosestDepot (TileIndex *location, DestinationID *destination, bool *reverse)
 Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should reverse.
CommandCost SendToDepot (DoCommandFlag flags, DepotCommand command)
 Send this vehicle to the depot using the given command(s).
void UpdateVisualEffect (bool allow_power_change=true)
 Update the cached visual effect.
void ShowVisualEffect () const
 Draw visual effects (smoke and/or sparks) for a vehicle chain.
void UpdatePosition ()
 Update the position of the vehicle.
void UpdateViewport (bool dirty)
 Update the vehicle on the viewport, updating the right hash and setting the new coordinates.
void UpdatePositionAndViewport ()
 Update the position of the vehicle, and update the viewport.
void MarkAllViewportsDirty () const
 Marks viewports dirty where the vehicle's image is.
uint16 GetServiceInterval () const
void SetServiceInterval (uint16 interval)
bool ServiceIntervalIsCustom () const
bool ServiceIntervalIsPercent () const
void SetServiceIntervalIsCustom (bool on)
void SetServiceIntervalIsPercent (bool on)
void IncrementImplicitOrderIndex ()
 Increments cur_implicit_order_index, keeps care of the wrap-around and invalidates the GUI.
void IncrementRealOrderIndex ()
 Advanced cur_real_order_index to the next real order, keeps care of the wrap-around and invalidates the GUI.
void UpdateRealOrderIndex ()
 Skip implicit orders until cur_real_order_index is a non-implicit order.
OrderGetOrder (int index) const
 Returns order 'index' of a vehicle or NULL when it doesn't exists.
OrderGetLastOrder () const
 Returns the last order of a vehicle, or NULL if it doesn't exists.
bool IsEngineCountable () const
 Check if a vehicle is counted in num_engines in each company struct.
bool HasEngineType () const
 Check whether Vehicle::engine_type has any meaning.
bool HasDepotOrder () const
 Checks if a vehicle has a depot in its order list.
void HandlePathfindingResult (bool path_found)
 Handle the pathfinding result, especially the lost status.
bool IsFrontEngine () const
 Check if the vehicle is a front engine.
bool IsArticulatedPart () const
 Check if the vehicle is an articulated part of an engine.
bool HasArticulatedPart () const
 Check if an engine has an articulated part.
- Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >
void * operator new (size_t size)
 Allocates space for new Titem.
void * operator new (size_t size, size_t index)
 Allocates space for new Titem with given index.
void * operator new (size_t size, void *ptr)
 Allocates space for new Titem at given memory address.
void operator delete (void *p)
 Marks Titem as free.
- Public Member Functions inherited from BaseConsist
void CopyConsistPropertiesFrom (const BaseConsist *src)
 Copy properties of other BaseConsist.

Data Fields

GroundVehicleCache gcache
 Cache of often calculated values.
uint16 gv_flags

Protected Member Functions

uint DoUpdateSpeed (uint accel, int min_speed, int max_speed)
 Update the speed of the vehicle.

Additional Inherited Members

- Static Public Member Functions inherited from SpecializedVehicle< T, Type >
static bool IsValidID (size_t index)
 Tests whether given index is a valid index for vehicle of this type.
static T * Get (size_t index)
 Gets vehicle with given index.
static T * GetIfValid (size_t index)
 Returns vehicle if the index is a valid index for this vehicle type.
static T * From (Vehicle *v)
 Converts a Vehicle to SpecializedVehicle with type checking.
static const T * From (const Vehicle *v)
 Converts a const Vehicle to const SpecializedVehicle with type checking.
- Static Public Attributes inherited from SpecializedVehicle< T, Type >
static const VehicleType EXPECTED_TYPE = Type
 Specialized type.

Detailed Description

template<class T, VehicleType Type>
struct GroundVehicle< T, Type >

Base class for all vehicles that move through ground.

Child classes must define all of the following functions. These functions are not defined as pure virtual functions at this class to improve performance.

virtual uint16 GetPower() const = 0; virtual uint16 GetPoweredPartPower(const T *head) const = 0; virtual uint16 GetWeight() const = 0; virtual byte GetTractiveEffort() const = 0; virtual byte GetAirDrag() const = 0; virtual byte GetAirDragArea() const = 0; virtual AccelStatus GetAccelerationStatus() const = 0; virtual uint16 GetCurrentSpeed() const = 0; virtual uint32 GetRollingFriction() const = 0; virtual int GetAccelerationType() const = 0; virtual int32 GetSlopeSteepness() const = 0; virtual int GetDisplayMaxSpeed() const = 0; virtual uint16 GetMaxTrackSpeed() const = 0; virtual bool TileMayHaveSlopedTrack() const = 0;

Definition at line 81 of file ground_vehicle.hpp.

Member Function Documentation

template<class T , VehicleType Type>
void GroundVehicle< T, Type >::CargoChanged ( )

Recalculates the cached weight of a vehicle and its parts.

Should be called each time the cargo on the consist changes.

Definition at line 81 of file ground_vehicle.cpp.

Referenced by AfterLoadVehicles(), CmdBuildRoadVehicle(), CmdRefitVehicle(), RoadVehAccelerationModelChanged(), RoadVehSlopeSteepnessChanged(), and TrainSlopeSteepnessChanged().

template<class T, VehicleType Type>
uint GroundVehicle< T, Type >::Crash ( bool  flooded)
inlinevirtual

Common code executed for crashed ground vehicles.

Parameters
floodedwas this vehicle flooded?
Returns
number of victims

Reimplemented from Vehicle.

Reimplemented in Train, and RoadVehicle.

Definition at line 102 of file ground_vehicle.hpp.

Referenced by GroundVehicle< RoadVehicle, VEH_ROAD >::Crash(), RoadVehicle::Crash(), and Train::Crash().

template<class T, VehicleType Type>
uint GroundVehicle< T, Type >::DoUpdateSpeed ( uint  accel,
int  min_speed,
int  max_speed 
)
inlineprotected

Update the speed of the vehicle.

It updates the cur_speed and subspeed variables depending on the state of the vehicle; in this case the current acceleration, minimum and maximum speeds of the vehicle. It returns the distance that that the vehicle can drive this tick. Vehicle::GetAdvanceDistance() determines the distance to drive before moving a step on the map.

Parameters
accelThe acceleration we would like to give this vehicle.
min_speedThe minimum speed here, in vehicle specific units.
max_speedThe maximum speed here, in vehicle specific units.
Returns
Distance to drive.

Definition at line 365 of file ground_vehicle.hpp.

template<class T , VehicleType Type>
int GroundVehicle< T, Type >::GetAcceleration ( ) const

Calculates the acceleration of the vehicle under its current conditions.

Returns
Current acceleration of the vehicle.

Definition at line 107 of file ground_vehicle.cpp.

References AS_ACCEL, ClampToI32(), max(), and min().

template<class T, VehicleType Type>
int64 GroundVehicle< T, Type >::GetSlopeResistance ( ) const
inline

Calculates the total slope resistance for this vehicle.

Returns
Slope resistance.

Definition at line 116 of file ground_vehicle.hpp.

template<class T , VehicleType Type>
bool GroundVehicle< T, Type >::IsChainInDepot ( ) const
virtual

Check whether the whole vehicle chain is in the depot.

Returns
true if and only if the whole chain is in the depot.

Reimplemented from Vehicle.

Definition at line 192 of file ground_vehicle.cpp.

References IsDepotTypeTile(), TRANSPORT_RAIL, TRANSPORT_ROAD, VEH_ROAD, and VEH_TRAIN.

Referenced by CheckIfTrainNeedsService(), and CmdForceTrainProceed().

template<class T, VehicleType Type>
bool GroundVehicle< T, Type >::IsEngine ( ) const
inline

Check if a vehicle is an engine (can be first in a consist).

Returns
Returns true if vehicle is an engine.

Definition at line 318 of file ground_vehicle.hpp.

Referenced by AfterLoadVehicles(), AgeVehicle(), CheckNewTrain(), CheckTrainAttachment(), CmdMoveRailVehicle(), CmdSellRailWagon(), ConnectMultiheadedTrains(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsRearDualheaded(), NormaliseDualHeads(), and NormaliseSubtypes().

template<class T, VehicleType Type>
bool GroundVehicle< T, Type >::IsFreeWagon ( ) const
inline

Check if the vehicle is a free wagon (got no engine in front of it).

Returns
Returns true if the vehicle is a free wagon.

Definition at line 312 of file ground_vehicle.hpp.

Referenced by AfterLoadGame(), AfterLoadVehicles(), BuildDepotVehicleList(), CmdBuildRailWagon(), CmdRefitVehicle(), ConnectMultiheadedTrains(), DepotWindow::DrawVehicleInDepot(), NormalizeTrainVehInDepot(), and UpdateTrainGroupID().

template<class T, VehicleType Type>
bool GroundVehicle< T, Type >::IsMultiheaded ( ) const
inline

Check if the vehicle is a multiheaded engine.

Returns
Returns true if the vehicle is a multiheaded engine.

Definition at line 330 of file ground_vehicle.hpp.

Referenced by ArrangeTrains(), CmdReverseTrainDirection(), ConnectMultiheadedTrains(), Train::GetRunningCost(), GetVehicleSet(), GroundVehicle< RoadVehicle, VEH_ROAD >::IsRearDualheaded(), IterateVehicleParts(), and NormaliseDualHeads().

template<class T, VehicleType Type>
bool GroundVehicle< T, Type >::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 336 of file ground_vehicle.hpp.

Referenced by BuildDepotVehicleList(), CheckTrainAttachment(), CmdAutoreplaceVehicle(), CmdCloneVehicle(), CmdMoveRailVehicle(), CmdSellRailWagon(), GetNewEngineType(), Train::GetNextUnit(), Train::GetPrevUnit(), Vehicle::IsEngineCountable(), ReplaceFreeUnit(), and ReserveConsist().

template<class T, VehicleType Type>
bool GroundVehicle< T, Type >::IsWagon ( ) const
inline

Check if a vehicle is a wagon.

Returns
Returns true if vehicle is a wagon.

Definition at line 324 of file ground_vehicle.hpp.

Referenced by NormaliseSubtypes().

template<class T , VehicleType Type>
void GroundVehicle< T, Type >::PowerChanged ( )

Recalculates the cached total power of a vehicle.

Should be called when the consist is changed.

Definition at line 23 of file ground_vehicle.cpp.

References max(), min(), SetWindowDirty(), SetWindowWidgetDirty(), VS_STOPPED, WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW, and WID_VV_START_STOP.

template<class T, VehicleType Type>
void GroundVehicle< T, Type >::SetLastSpeed ( )
inline

Update the GUI variant of the current speed of the vehicle.

Also mark the widget dirty when that is needed, i.e. when the speed of this vehicle has changed.

Definition at line 343 of file ground_vehicle.hpp.

Referenced by CmdReverseTrainDirection(), and MarkTrainAsStuck().

template<class T, VehicleType Type>
int GroundVehicle< T, Type >::UpdateInclination ( bool  new_tile,
bool  update_delta 
)
inline

Checks if the vehicle is in a slope and sets the required flags in that case.

Parameters
new_tileTrue if the vehicle reached a new tile.
update_deltaIndicates to also update the delta.
Returns
Old height of the vehicle.

Definition at line 234 of file ground_vehicle.hpp.

Referenced by ChangeTrainDirRandomly(), and UpdateStatusAfterSwap().

template<class T, VehicleType Type>
void GroundVehicle< T, Type >::UpdateZPosition ( )
inline

Updates vehicle's Z position.

Inclination can't change in the middle of a tile. The faster code is used for trains and road vehicles unless they are reversing on a sloped tile.

Definition at line 162 of file ground_vehicle.hpp.

Referenced by GroundVehicle< RoadVehicle, VEH_ROAD >::UpdateInclination().

template<class T, VehicleType Type>
void GroundVehicle< T, Type >::UpdateZPositionAndInclination ( )
inline

Updates vehicle's Z position and inclination.

Used when the vehicle entered given tile.

Precondition
The vehicle has to be at (or near to) a border of the tile, directed towards tile centre

Definition at line 137 of file ground_vehicle.hpp.

Referenced by GroundVehicle< RoadVehicle, VEH_ROAD >::UpdateInclination().

Field Documentation

template<class T, VehicleType Type>
uint16 GroundVehicle< T, Type >::gv_flags

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