OpenTTD
Functions | Variables
articulated_vehicles.cpp File Reference

Implementation of articulated vehicles. More...

#include "stdafx.h"
#include "train.h"
#include "roadveh.h"
#include "vehicle_func.h"
#include "engine_func.h"
#include "company_func.h"
#include "newgrf.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

static EngineID GetNextArticulatedPart (uint index, EngineID front_type, Vehicle *front=NULL, bool *mirrored=NULL)
 Determines the next articulated part to attach. More...
 
bool IsArticulatedEngine (EngineID engine_type)
 Does a NewGRF report that this should be an articulated vehicle? More...
 
uint CountArticulatedParts (EngineID engine_type, bool purchase_window)
 Count the number of articulated parts of an engine. More...
 
static uint16 GetVehicleDefaultCapacity (EngineID engine, CargoID *cargo_type)
 Returns the default (non-refitted) capacity of a specific EngineID. More...
 
static uint32 GetAvailableVehicleCargoTypes (EngineID engine, bool include_initial_cargo_type)
 Returns all cargoes a vehicle can carry. More...
 
CargoArray GetCapacityOfArticulatedParts (EngineID engine)
 Get the capacity of the parts of a given engine. More...
 
void GetArticulatedVehicleCargoesAndRefits (EngineID engine, CargoArray *cargoes, uint32 *refits)
 Get the default cargoes and refits of an articulated vehicle. More...
 
bool IsArticulatedVehicleRefittable (EngineID engine)
 Checks whether any of the articulated parts is refittable. More...
 
void GetArticulatedRefitMasks (EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
 Merges the refit_masks of all articulated parts. More...
 
uint32 GetUnionOfArticulatedRefitMasks (EngineID engine, bool include_initial_cargo_type)
 Ors the refit_masks of all articulated parts. More...
 
uint32 GetIntersectionOfArticulatedRefitMasks (EngineID engine, bool include_initial_cargo_type)
 Ands the refit_masks of all articulated parts. More...
 
bool IsArticulatedVehicleCarryingDifferentCargoes (const Vehicle *v, CargoID *cargo_type)
 Tests if all parts of an articulated vehicle are refitted to the same cargo. More...
 
void CheckConsistencyOfArticulatedVehicle (const Vehicle *v)
 Checks whether the specs of freshly build articulated vehicles are consistent with the information specified in the purchase list. More...
 
void AddArticulatedParts (Vehicle *first)
 Add the remaining articulated parts to the given vehicle. More...
 

Variables

static const uint MAX_ARTICULATED_PARTS = 100
 Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback.
 

Detailed Description

Implementation of articulated vehicles.

Definition in file articulated_vehicles.cpp.

Function Documentation

◆ AddArticulatedParts()

void AddArticulatedParts ( Vehicle first)

Add the remaining articulated parts to the given vehicle.

Parameters
firstThe head of the articulated bit.

Definition at line 355 of file articulated_vehicles.cpp.

References HasBit(), and BaseVehicle::type.

◆ CheckConsistencyOfArticulatedVehicle()

void CheckConsistencyOfArticulatedVehicle ( const Vehicle v)

Checks whether the specs of freshly build articulated vehicles are consistent with the information specified in the purchase list.

Only essential information is checked to leave room for magic tricks/workarounds to grfcoders. It checks: For autoreplace/-renew:

  • Default cargo type (without capacity)
  • intersection and union of refit masks.

Definition at line 313 of file articulated_vehicles.cpp.

References Vehicle::engine_type, GetArticulatedRefitMasks(), GetAvailableVehicleCargoTypes(), GetCapacityOfArticulatedParts(), and Vehicle::GetEngine().

◆ CountArticulatedParts()

uint CountArticulatedParts ( EngineID  engine_type,
bool  purchase_window 
)

Count the number of articulated parts of an engine.

Parameters
engine_typeThe engine to get the number of parts of.
purchase_windowWhether we are in the scope of the purchase window or not, i.e. whether we cannot allocate vehicles.
Returns
The number of parts.

Definition at line 75 of file articulated_vehicles.cpp.

References HasBit().

Referenced by CmdBuildVehicle().

◆ GetArticulatedRefitMasks()

void GetArticulatedRefitMasks ( EngineID  engine,
bool  include_initial_cargo_type,
uint32 *  union_mask,
uint32 *  intersection_mask 
)

Merges the refit_masks of all articulated parts.

Parameters
enginethe first part
include_initial_cargo_typeif true the default cargo type of the vehicle is included; if false only the refit_mask
union_maskreturns bit mask of CargoIDs which are a refit option for at least one articulated part
intersection_maskreturns bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)

Definition at line 231 of file articulated_vehicles.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), and GetAvailableVehicleCargoTypes().

Referenced by CheckConsistencyOfArticulatedVehicle(), GetIntersectionOfArticulatedRefitMasks(), GetNewCargoTypeForReplace(), GetUnionOfArticulatedRefitMasks(), and Vehicle::NeedsServicing().

◆ GetArticulatedVehicleCargoesAndRefits()

void GetArticulatedVehicleCargoesAndRefits ( EngineID  engine,
CargoArray cargoes,
uint32 *  refits 
)

Get the default cargoes and refits of an articulated vehicle.

The refits are linked to a cargo rather than an articulated part to prevent a long list of parts.

Parameters
engineModel to investigate.
[out]cargoesTotal amount of units that can be transported, summed by cargo.
[out]refitsWhether a (possibly partial) refit for each cargo is possible.

Definition at line 172 of file articulated_vehicles.cpp.

References EngineInfo::callback_mask, CBM_VEHICLE_ARTIC_ENGINE, CargoArray::Clear(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetNextArticulatedPart(), GetVehicleDefaultCapacity(), HasBit(), INVALID_ENGINE, IsEngineRefittable(), Engine::IsGroundVehicle(), MAX_ARTICULATED_PARTS, and SetBit().

◆ GetAvailableVehicleCargoTypes()

static uint32 GetAvailableVehicleCargoTypes ( EngineID  engine,
bool  include_initial_cargo_type 
)
inlinestatic

Returns all cargoes a vehicle can carry.

Parameters
enginethe EngineID of interest
include_initial_cargo_typeif true the default cargo type of the vehicle is included; if false only the refit_mask
Returns
bit set of CargoIDs

Definition at line 122 of file articulated_vehicles.cpp.

References Engine::CanCarryCargo(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetDefaultCargoType(), and SetBit().

Referenced by CheckConsistencyOfArticulatedVehicle(), and GetArticulatedRefitMasks().

◆ GetCapacityOfArticulatedParts()

CargoArray GetCapacityOfArticulatedParts ( EngineID  engine)

◆ GetIntersectionOfArticulatedRefitMasks()

uint32 GetIntersectionOfArticulatedRefitMasks ( EngineID  engine,
bool  include_initial_cargo_type 
)

Ands the refit_masks of all articulated parts.

Parameters
enginethe first part
include_initial_cargo_typeif true the default cargo type of the vehicle is included; if false only the refit_mask
Returns
bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)

Definition at line 270 of file articulated_vehicles.cpp.

References GetArticulatedRefitMasks().

◆ GetNextArticulatedPart()

static EngineID GetNextArticulatedPart ( uint  index,
EngineID  front_type,
Vehicle front = NULL,
bool *  mirrored = NULL 
)
static

Determines the next articulated part to attach.

Parameters
indexPosition in chain
front_typeFront engine type
frontFront engine
mirroredReturns whether the part shall be flipped.
Returns
engine to add or INVALID_ENGINE

Definition at line 34 of file articulated_vehicles.cpp.

Referenced by GetArticulatedVehicleCargoesAndRefits(), GetCapacityOfArticulatedParts(), and IsArticulatedVehicleRefittable().

◆ GetUnionOfArticulatedRefitMasks()

uint32 GetUnionOfArticulatedRefitMasks ( EngineID  engine,
bool  include_initial_cargo_type 
)

Ors the refit_masks of all articulated parts.

Parameters
enginethe first part
include_initial_cargo_typeif true the default cargo type of the vehicle is included; if false only the refit_mask
Returns
bit mask of CargoIDs which are a refit option for at least one articulated part

Definition at line 257 of file articulated_vehicles.cpp.

References GetArticulatedRefitMasks().

Referenced by CargoFilter(), EnginesHaveCargoInCommon(), GetPreviewCompany(), and VerifyAutoreplaceRefitForOrders().

◆ GetVehicleDefaultCapacity()

static uint16 GetVehicleDefaultCapacity ( EngineID  engine,
CargoID cargo_type 
)
inlinestatic

Returns the default (non-refitted) capacity of a specific EngineID.

Parameters
enginethe EngineID of interest
cargo_typereturns the default cargo type, if needed
Returns
capacity

Definition at line 107 of file articulated_vehicles.cpp.

References Engine::CanCarryCargo(), CT_INVALID, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetDefaultCargoType(), and Engine::GetDisplayDefaultCapacity().

Referenced by GetArticulatedVehicleCargoesAndRefits(), and GetCapacityOfArticulatedParts().

◆ IsArticulatedEngine()

bool IsArticulatedEngine ( EngineID  engine_type)

Does a NewGRF report that this should be an articulated vehicle?

Parameters
engine_typeThe engine to check.
Returns
True iff the articulated engine callback flag is set.

Definition at line 64 of file articulated_vehicles.cpp.

References HasBit().

◆ IsArticulatedVehicleCarryingDifferentCargoes()

bool IsArticulatedVehicleCarryingDifferentCargoes ( const Vehicle v,
CargoID cargo_type 
)

Tests if all parts of an articulated vehicle are refitted to the same cargo.

Note: Vehicles not carrying anything are ignored

Parameters
vthe first vehicle in the chain
cargo_typereturns the common CargoID if needed. (CT_INVALID if no part is carrying something or they are carrying different things)
Returns
true if some parts are carrying different cargoes, false if all parts are carrying the same (nothing is also the same)

Definition at line 285 of file articulated_vehicles.cpp.

References Engine::CanCarryCargo(), Vehicle::cargo_type, CT_INVALID, Vehicle::GetEngine(), Vehicle::GetNextArticulatedPart(), and Vehicle::HasArticulatedPart().

Referenced by GetNewCargoTypeForReplace(), and Vehicle::NeedsServicing().

◆ IsArticulatedVehicleRefittable()

bool IsArticulatedVehicleRefittable ( EngineID  engine)