OpenTTD Source: src/articulated_vehicles.cpp File Reference

OpenTTD Source 15.0-RC2

Functions | Variables
articulated_vehicles.cpp File Reference

Implementation of articulated vehicles. More...

`#include "stdafx.h"`
`#include "core/bitmath_func.hpp"`
`#include "core/random_func.hpp"`
`#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=nullptr, bool *mirrored=nullptr)
| Determines the next articulated part to attach.
bool | IsArticulatedEngine (EngineID engine_type)
| Does a NewGRF report that this should be an articulated vehicle?
uint | CountArticulatedParts (EngineID engine_type, bool purchase_window)
| Count the number of articulated parts of an engine.
static std::pair< CargoType, uint16_t > | GetVehicleDefaultCapacity (EngineID engine)
| Returns the default (non-refitted) cargo and capacity of a specific EngineID.
static CargoTypes | GetAvailableVehicleCargoTypes (EngineID engine, bool include_initial_cargo_type)
| Returns all cargoes a vehicle can carry.
CargoArray | GetCapacityOfArticulatedParts (EngineID engine)
| Get the capacity of the parts of a given engine.
CargoTypes | GetCargoTypesOfArticulatedParts (EngineID engine)
| Get the cargo mask of the parts of a given engine.
bool | IsArticulatedVehicleRefittable (EngineID engine)
| Checks whether any of the articulated parts is refittable.
void | GetArticulatedRefitMasks (EngineID engine, bool include_initial_cargo_type, CargoTypes *union_mask, CargoTypes *intersection_mask)
| Merges the refit_masks of all articulated parts.
CargoTypes | GetUnionOfArticulatedRefitMasks (EngineID engine, bool include_initial_cargo_type)
| Ors the refit_masks of all articulated parts.
CargoTypes | GetCargoTypesOfArticulatedVehicle (const Vehicle *v, CargoType *cargo_type)
| Get cargo mask of all cargoes carried by an articulated vehicle.
void | CheckConsistencyOfArticulatedVehicle (const Vehicle *v)
| Checks whether the specs of freshly build articulated vehicles are consistent with the information specified in the purchase list.
void | AddArticulatedParts (Vehicle *first)
| Add the remaining articulated parts to the given vehicle.

## 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

  first | The head of the articulated bit.

Definition at line 336 of file articulated_vehicles.cpp.

References ArticEngine, Vehicle::build_year, GroundVehicleCache::cached_veh_length, EngineInfo::callback_mask, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_vehicle_pool >::CanAllocateItem(), Engine::CanCarryCargo(), Vehicle::cargo_cap, Vehicle::cargo_subtype, Vehicle::cargo_type, RoadVehicle::compatible_roadtypes, Vehicle::date_of_last_service, Vehicle::date_of_last_service_newgrf, Vehicle::direction, Vehicle::engine_type, GroundVehicleCache::first_engine, Flipped, SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), Engine::GetDefaultCargoType(), Vehicle::GetGroundVehicleCache(), GetNextArticulatedPart(), IsValidCargoType(), Vehicle::max_age, MAX_ARTICULATED_PARTS, TimerGameConst< struct Calendar >::MIN_DATE, Vehicle::owner, Random, Vehicle::random_bits, Vehicle::refit_cap, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Reset(), RoadVehicle::roadtype, RVSB_IN_DEPOT, VehicleSpriteSeq::Set(), GroundVehicle< T, Type >::SetArticulatedPart(), Vehicle::SetNext(), Vehicle::sprite_cache, MutableSpriteCache::sprite_seq, Vehicle::spritenum, RoadVehicle::state, Stopped, Vehicle::subtype, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), TestVehicleBuildProbability(), Vehicle::tile, BaseVehicle::type, Vehicle::UpdatePosition(), Vehicle::value, VEH_ROAD, VEH_TRAIN, VEHICLE_LENGTH, Vehicle::vehstatus, Vehicle::x_pos, Vehicle::y_pos, and Vehicle::z_pos.

Referenced by CmdBuildRailVehicle(), CmdBuildRailWagon(), and CmdBuildRoadVehicle().

## ◆ 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 294 of file articulated_vehicles.cpp.

References Vehicle::cargo_cap, Vehicle::cargo_type, Vehicle::engine_type, GetArticulatedRefitMasks(), GetAvailableVehicleCargoTypes(), GetCapacityOfArticulatedParts(), Vehicle::GetEngine(), Vehicle::GetNextArticulatedPart(), Vehicle::HasArticulatedPart(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, NUM_CARGO, SetBit(), ShowNewGrfVehicleError(), and VehRefit.

Referenced by CmdBuildRailVehicle(), CmdBuildRailWagon(), and CmdBuildRoadVehicle().

## ◆ CountArticulatedParts()

uint CountArticulatedParts | ( | EngineID | engine_type,
|  | bool | purchase_window
| ) |  |

Count the number of articulated parts of an engine.

Parameters

  engine_type | The engine to get the number of parts of.
  purchase_window | Whether 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 _current_company, ArticEngine, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_vehicle_pool >::CanAllocateItem(), GetNextArticulatedPart(), and MAX_ARTICULATED_PARTS.

Referenced by CmdBuildVehicle().

## ◆ GetArticulatedRefitMasks()

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

Merges the refit_masks of all articulated parts.

Parameters

  engine | the first part
  include_initial_cargo_type | if true the default cargo type of the vehicle is included; if false only the refit_mask
  union_mask | returns bit mask of CargoTypes which are a refit option for at least one articulated part
  intersection_mask | returns bit mask of CargoTypes which are a refit option for every articulated part (with default capacity > 0)

Definition at line 222 of file articulated_vehicles.cpp.

References ArticEngine, EngineInfo::callback_mask, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), GetAvailableVehicleCargoTypes(), GetNextArticulatedPart(), Engine::IsGroundVehicle(), MAX_ARTICULATED_PARTS, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

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

## ◆ GetAvailableVehicleCargoTypes()

static CargoTypes GetAvailableVehicleCargoTypes | ( | EngineID | engine,
|  | bool | include_initial_cargo_type
| ) |  |
| inlinestatic

Returns all cargoes a vehicle can carry.

Parameters

  engine | the EngineID of interest
  include_initial_cargo_type | if true the default cargo type of the vehicle is included; if false only the refit_mask

Returns
  bit set of CargoTypes

Definition at line 117 of file articulated_vehicles.cpp.

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

Referenced by CheckConsistencyOfArticulatedVehicle(), and GetArticulatedRefitMasks().

## ◆ GetCapacityOfArticulatedParts()

CargoArray GetCapacityOfArticulatedParts | ( | EngineID | engine | ) |

Get the capacity of the parts of a given engine.

Parameters

  engine | The engine to get the capacities from.

Returns
  The cargo capacities.

Definition at line 136 of file articulated_vehicles.cpp.

References ArticEngine, EngineInfo::callback_mask, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), GetNextArticulatedPart(), GetVehicleDefaultCapacity(), Engine::IsGroundVehicle(), IsValidCargoType(), MAX_ARTICULATED_PARTS, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by CheckConsistencyOfArticulatedVehicle(), CmdBuildVehicle(), and GetTotalCapacityOfArticulatedParts().

## ◆ GetCargoTypesOfArticulatedParts()

CargoTypes GetCargoTypesOfArticulatedParts | ( | EngineID | engine | ) |

Get the cargo mask of the parts of a given engine.

Parameters

  engine | The engine to get the capacities from.

Returns
  The cargo mask.

Definition at line 166 of file articulated_vehicles.cpp.

References ArticEngine, EngineInfo::callback_mask, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), GetNextArticulatedPart(), GetVehicleDefaultCapacity(), Engine::IsGroundVehicle(), IsValidCargoType(), MAX_ARTICULATED_PARTS, SetBit(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

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

## ◆ GetCargoTypesOfArticulatedVehicle()

CargoTypes GetCargoTypesOfArticulatedVehicle | ( | const Vehicle * | v,
|  | CargoType * | cargo_type
| ) |  |

Get cargo mask of all cargoes carried by an articulated vehicle.

Note: Vehicles not carrying anything are ignored

Parameters

  v | the first vehicle in the chain
  cargo_type | returns the common CargoType if needed. (INVALID_CARGO if no part is carrying something or they are carrying different things)

Returns
  cargo mask, may be 0 if the no vehicle parts have cargo capacity

Definition at line 262 of file articulated_vehicles.cpp.

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

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

## ◆ GetNextArticulatedPart()

static EngineID GetNextArticulatedPart | ( | uint | index,
|  | EngineID | front_type,
|  | Vehicle * | front = `nullptr`,
|  | bool * | mirrored = `nullptr`
| ) |  |
| static

Determines the next articulated part to attach.

Parameters

  index | Position in chain
  front_type | Front engine type
  front | Front engine
  mirrored | Returns whether the part shall be flipped.

Returns
  engine to add or EngineID::Invalid()

Definition at line 34 of file articulated_vehicles.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_ARTIC_ENGINE, GB(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), Engine::GetGRF(), GetNewEngineID(), GetVehicleCallback(), HasBit(), and Engine::type.

Referenced by AddArticulatedParts(), CountArticulatedParts(), GetArticulatedRefitMasks(), GetCapacityOfArticulatedParts(), GetCargoTypesOfArticulatedParts(), and IsArticulatedVehicleRefittable().

## ◆ GetUnionOfArticulatedRefitMasks()

CargoTypes GetUnionOfArticulatedRefitMasks | ( | EngineID | engine,
|  | bool | include_initial_cargo_type
| ) |  |

Ors the refit_masks of all articulated parts.

Parameters

  engine | the first part
  include_initial_cargo_type | if true the default cargo type of the vehicle is included; if false only the refit_mask

Returns
  bit mask of CargoTypes which are a refit option for at least one articulated part

Definition at line 248 of file articulated_vehicles.cpp.

References GetArticulatedRefitMasks().

Referenced by CargoAndEngineFilter(), EnginesHaveCargoInCommon(), GetIncompatibleRefitOrderIdForAutoreplace(), GetPreviewCompany(), ShowRefitOptionsList(), and VerifyAutoreplaceRefitForOrders().

## ◆ GetVehicleDefaultCapacity()

static std::pair< CargoType, uint16_t > GetVehicleDefaultCapacity | ( | EngineID | engine | ) |
| inlinestatic

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

Parameters

  engine | the EngineID of interest

Returns
  cargo and capacity

Definition at line 104 of file articulated_vehicles.cpp.

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

Referenced by GetCapacityOfArticulatedParts(), and GetCargoTypesOfArticulatedParts().

## ◆ IsArticulatedEngine()

bool IsArticulatedEngine | ( | EngineID | engine_type | ) |

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

Parameters

  engine_type | The engine to check.

Returns
  True iff the articulated engine callback flag is set.

Definition at line 64 of file articulated_vehicles.cpp.

References ArticEngine, EngineInfo::callback_mask, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

## ◆ IsArticulatedVehicleRefittable()

bool IsArticulatedVehicleRefittable | ( | EngineID | engine | ) |

Checks whether any of the articulated parts is refittable.

Parameters

  engine | the first part

Returns
  true if refittable

Definition at line 196 of file articulated_vehicles.cpp.

References ArticEngine, EngineInfo::callback_mask, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_engine_pool >::Get(), GetNextArticulatedPart(), IsEngineRefittable(), Engine::IsGroundVehicle(), MAX_ARTICULATED_PARTS, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by DrawVehiclePurchaseInfo().

## Variable Documentation

## ◆ MAX_ARTICULATED_PARTS

const uint MAX_ARTICULATED_PARTS = 100
| static

Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback.

Definition at line 24 of file articulated_vehicles.cpp.

Referenced by AddArticulatedParts(), CountArticulatedParts(), GetArticulatedRefitMasks(), GetCapacityOfArticulatedParts(), GetCargoTypesOfArticulatedParts(), and IsArticulatedVehicleRefittable().