OpenTTD Source: src/autoreplace_cmd.cpp File Reference

OpenTTD Source 12.0-beta1

Functions
autoreplace_cmd.cpp File Reference
`#include "stdafx.h"`
`#include "company_func.h"`
`#include "train.h"`
`#include "command_func.h"`
`#include "engine_func.h"`
`#include "vehicle_func.h"`
`#include "autoreplace_func.h"`
`#include "autoreplace_gui.h"`
`#include "articulated_vehicles.h"`
`#include "core/random_func.hpp"`
`#include "vehiclelist.h"`
`#include "road.h"`
`#include "ai/ai.hpp"`
`#include "news_func.h"`
`#include "strings_func.h"`
`#include "table/strings.h"`
`#include "safeguards.h"`

Go to the source code of this file.

## Functions
void | ChangeVehicleViewports (VehicleID from_index, VehicleID to_index)
| Switches viewports following vehicles, which get autoreplaced. More...
void | ChangeVehicleNews (VehicleID from_index, VehicleID to_index)
| Report a change in vehicle IDs (due to autoreplace) to affected vehicle news. More...
void | ChangeVehicleViewWindow (VehicleID from_index, VehicleID to_index)
| Report a change in vehicle IDs (due to autoreplace) to affected vehicle windows. More...
static bool | EnginesHaveCargoInCommon (EngineID engine_a, EngineID engine_b)
| Figure out if two engines got at least one type of cargo in common (refitting if needed) More...
bool | CheckAutoreplaceValidity (EngineID from, EngineID to, CompanyID company)
| Checks some basic properties whether autoreplace is allowed. More...
void | CheckCargoCapacity (Vehicle *v)
| Check the capacity of all vehicles in a chain and spread cargo if needed. More...
static void | TransferCargo (Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
| Transfer cargo from a single (articulated )old vehicle to the new vehicle chain. More...
static bool | VerifyAutoreplaceRefitForOrders (const Vehicle *v, EngineID engine_type)
| Tests whether refit orders that applied to v will also apply to the new vehicle type. More...
static int | GetIncompatibleRefitOrderIdForAutoreplace (const Vehicle *v, EngineID engine_type)
| Gets the index of the first refit order that is incompatible with the requested engine type. More...
static CargoID | GetNewCargoTypeForReplace (Vehicle *v, EngineID engine_type, bool part_of_chain)
| Function to find what type of cargo to refit to when autoreplacing. More...
static CommandCost | GetNewEngineType (const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
| Get the EngineID of the replacement for a vehicle. More...
static CommandCost | BuildReplacementVehicle (Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
| Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does) More...
static CommandCost | CmdStartStopVehicle (const Vehicle *v, bool evaluate_callback)
| Issue a start/stop command. More...
static CommandCost | CmdMoveVehicle (const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
| Issue a train vehicle move command. More...
static CommandCost | CopyHeadSpecificThings (Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags)
| Copy head specific things to the new vehicle chain after it was successfully constructed. More...
static CommandCost | ReplaceFreeUnit (Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
| Replace a single unit in a free wagon chain. More...
static CommandCost | ReplaceChain (Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
| Replace a whole vehicle chain. More...
CommandCost | CmdAutoreplaceVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on their own. More...
CommandCost | CmdSetAutoReplace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Change engine renewal parameters. More...

## Detailed Description

Deals with autoreplace execution but not the setup

Definition in file autoreplace_cmd.cpp.

## Function Documentation

## ◆ BuildReplacementVehicle()

static CommandCost BuildReplacementVehicle | ( | Vehicle * | old_veh,
|  | Vehicle ** | new_vehicle,
|  | bool | part_of_chain
| ) |  |
| static

Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does)

Parameters

  old_veh | A single (articulated/multiheaded) vehicle that shall be replaced.
  new_vehicle | Returns the newly build and refitted vehicle
  part_of_chain | The vehicle is part of a train

Returns
  cost or error

Definition at line 309 of file autoreplace_cmd.cpp.

References _current_company, AddVehicleAdviceNewsItem(), Vehicle::cargo_type, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DoCommand(), CommandCost::Failed(), CargoSpec::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), GetIncompatibleRefitOrderIdForAutoreplace(), GetNewCargoTypeForReplace(), GetNewEngineType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_ENGINE, IsLocalCompany(), CargoSpec::name, SetDParam(), and Vehicle::tile.

Referenced by ReplaceChain(), and ReplaceFreeUnit().

## ◆ ChangeVehicleNews()

void ChangeVehicleNews | ( | VehicleID | from_index,
|  | VehicleID | to_index
| ) |  |

Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.

Note
  Viewports of currently displayed news is changed via ChangeVehicleViewports

Parameters

  from_index | the old vehicle ID
  to_index | the new vehicle ID

Definition at line 986 of file news_gui.cpp.

References _oldest_news, NewsItem::flags, NewsItem::next, NF_VEHICLE_PARAM0, NR_VEHICLE, NewsItem::params, NewsItem::ref1, NewsItem::ref2, NewsItem::reftype1, and NewsItem::reftype2.

## ◆ ChangeVehicleViewports()

void ChangeVehicleViewports | ( | VehicleID | from_index,
|  | VehicleID | to_index
| ) |  |

Switches viewports following vehicles, which get autoreplaced.

Parameters

  from_index | the old vehicle ID
  to_index | the new vehicle ID

Definition at line 3427 of file window.cpp.

References ViewportData::follow_vehicle, Window::SetDirty(), and Window::viewport.

## ◆ ChangeVehicleViewWindow()

void ChangeVehicleViewWindow | ( | VehicleID | from_index,
|  | VehicleID | to_index
| ) |  |

Report a change in vehicle IDs (due to autoreplace) to affected vehicle windows.

Parameters

  from_index | the old vehicle ID
  to_index | the new vehicle ID

Definition at line 1358 of file vehicle_gui.cpp.

References ChangeVehicleWindow(), WC_VEHICLE_DETAILS, WC_VEHICLE_ORDERS, WC_VEHICLE_REFIT, WC_VEHICLE_TIMETABLE, and WC_VEHICLE_VIEW.

## ◆ CheckAutoreplaceValidity()

bool CheckAutoreplaceValidity | ( | EngineID | from,
|  | EngineID | to,
|  | CompanyID | company
| ) |  |

Checks some basic properties whether autoreplace is allowed.

Parameters

  from | Origin engine
  to | Destination engine
  company | Company to check for

Returns
  true if autoreplace is allowed

Definition at line 54 of file autoreplace_cmd.cpp.

References AIR_CTOL, RailtypeInfo::compatible_railtypes, EF_ROAD_TRAM, EnginesHaveCargoInCommon(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetRailTypeInfo(), GetRoadTypeInfo(), HasBit(), IsEngineBuildable(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::IsValidID(), EngineInfo::misc_flags, RoadTypeInfo::powered_roadtypes, RAILVEH_WAGON, RoadVehicleInfo::roadtype, ROADTYPES_NONE, AircraftVehicleInfo::subtype, Engine::type, VEH_AIRCRAFT, VEH_ROAD, and VEH_TRAIN.

Referenced by ReplaceVehicleWindow::GenerateReplaceVehList().

## ◆ CheckCargoCapacity()

void CheckCargoCapacity | ( | Vehicle * | v | ) |

Check the capacity of all vehicles in a chain and spread cargo if needed.

Parameters

  v | The vehicle to check.

Precondition
  You can only do this if the consist is not loading or unloading. It must not carry reserved cargo, nor cargo to be unloaded or transferred.

Definition at line 104 of file autoreplace_cmd.cpp.

References Vehicle::First(), CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP, and Vehicle::Next().

## ◆ CmdAutoreplaceVehicle()

CommandCost CmdAutoreplaceVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on their own.

Parameters

  tile | not used
  flags | type of operation
  p1 | Index of vehicle
  p2 | not used
  text | unused

Returns
  the cost of this operation or an error

Definition at line 717 of file autoreplace_cmd.cpp.

References _current_company, CommandCost::AddCost(), CheckOwnership(), CMD_ERROR, CmdStartStopVehicle(), DC_EXEC, EXPENSES_NEW_VEHICLES, CommandCost::Failed(), SpecializedVehicle< T, Type >::First(), Group::flags, SpecializedVehicle< Train, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), CommandCost::GetCost(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), GetNewEngineType(), Train::GetNextUnit(), GF_REPLACE_WAGON_REMOVAL, Vehicle::group_id, HasBit(), INVALID_ENGINE, Vehicle::IsArticulatedPart(), Vehicle::IsChainInDepot(), Vehicle::IsFrontEngine(), Vehicle::IsPrimaryVehicle(), GroundVehicle< T, Type >::IsRearDualheaded(), Vehicle::IsStoppedInDepot(), Vehicle::owner, CompanySettings::renew_keep_length, ReplaceChain(), ReplaceFreeUnit(), RestoreRandomSeeds(), SaveRandomSeeds(), CompanyProperties::settings, CommandCost::Succeeded(), BaseVehicle::type, VEH_TRAIN, Vehicle::vehstatus, VS_CRASHED, and VS_STOPPED.

## ◆ CmdMoveVehicle()

static CommandCost CmdMoveVehicle | ( | const Vehicle * | v,
|  | const Vehicle * | after,
|  | DoCommandFlag | flags,
|  | bool | whole_chain
| ) |  |
| inlinestatic

Issue a train vehicle move command.

Parameters

  v | The vehicle to move
  after | The vehicle to insert 'v' after, or nullptr to start new chain
  flags | the command flags to use
  whole_chain | move all vehicles following 'v' (true), or only 'v' (false)

Returns
  success or error

Definition at line 384 of file autoreplace_cmd.cpp.

References CMD_MOVE_RAIL_VEHICLE, DC_NO_CARGO_CAP_CHECK, DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, and INVALID_VEHICLE.

Referenced by ReplaceChain(), and ReplaceFreeUnit().

## ◆ CmdSetAutoReplace()

CommandCost CmdSetAutoReplace | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Change engine renewal parameters.

Parameters

  tile | unused
  flags | operation to perform
  p1 | packed data bit 0 = replace when engine gets old? bits 16-31 = engine group
  p2 | packed data bits 0-15 = old engine type bits 16-31 = new engine type
  text | unused

Returns
  the cost of this operation or an error

Definition at line 810 of file autoreplace_cmd.cpp.

References _current_company, CMD_ERROR, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), IsAllGroupID(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID().

## ◆ CmdStartStopVehicle()

static CommandCost CmdStartStopVehicle | ( | const Vehicle * | v,
|  | bool | evaluate_callback
| ) |  |
| inlinestatic

Issue a start/stop command.

Parameters

  v | a vehicle
  evaluate_callback | shall the start/stop callback be evaluated?

Returns
  success or error

Definition at line 371 of file autoreplace_cmd.cpp.

References CMD_START_STOP_VEHICLE, DC_AUTOREPLACE, DC_EXEC, DoCommand(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index.

Referenced by CmdAutoreplaceVehicle().

## ◆ CopyHeadSpecificThings()

static CommandCost CopyHeadSpecificThings | ( | Vehicle * | old_head,
|  | Vehicle * | new_head,
|  | DoCommandFlag | flags
| ) |  |
| static

Copy head specific things to the new vehicle chain after it was successfully constructed.

Parameters

  old_head | The old front vehicle (no wagons attached anymore)
  new_head | The new head of the completely replaced vehicle chain
  flags | the command flags to use

Definition at line 395 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, and CommandCost::Succeeded().

## ◆ EnginesHaveCargoInCommon()

static bool EnginesHaveCargoInCommon | ( | EngineID | engine_a,
|  | EngineID | engine_b
| ) |  |
| static

Figure out if two engines got at least one type of cargo in common (refitting if needed)

Parameters

  engine_a | one of the EngineIDs
  engine_b | the other EngineID

Returns
  true if they can both carry the same type of cargo (or at least one of them got no capacity at all)

Definition at line 40 of file autoreplace_cmd.cpp.

References GetUnionOfArticulatedRefitMasks().

Referenced by CheckAutoreplaceValidity().

## ◆ GetIncompatibleRefitOrderIdForAutoreplace()

static int GetIncompatibleRefitOrderIdForAutoreplace | ( | const Vehicle * | v,
|  | EngineID | engine_type
| ) |  |
| static

Gets the index of the first refit order that is incompatible with the requested engine type.

Parameters

  v | The vehicle to be replaced
  engine_type | The type we want to replace with

Returns
  index of the incompatible order or -1 if none were found

Definition at line 202 of file autoreplace_cmd.cpp.

References Vehicle::First(), OrderList::GetNumOrders(), OrderList::GetOrderAt(), Order::GetRefitCargo(), GetUnionOfArticulatedRefitMasks(), HasBit(), Order::IsRefit(), Vehicle::list, Vehicle::orders, BaseVehicle::type, and VEH_TRAIN.

Referenced by BuildReplacementVehicle().

## ◆ GetNewCargoTypeForReplace()

static CargoID GetNewCargoTypeForReplace | ( | Vehicle * | v,
|  | EngineID | engine_type,
|  | bool | part_of_chain
| ) |  |
| static

Function to find what type of cargo to refit to when autoreplacing.

Parameters

  *v | Original vehicle that is being replaced.
  engine_type | The EngineID of the vehicle that is being replaced to
  part_of_chain | The vehicle is part of a train

Returns
  The cargo type to replace to CT_NO_REFIT is returned if no refit is needed CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible

Definition at line 229 of file autoreplace_cmd.cpp.

References Engine::CanCarryCargo(), Vehicle::cargo_type, CT_INVALID, CT_NO_REFIT, Vehicle::First(), GetArticulatedRefitMasks(), Vehicle::GetEngine(), HasBit(), IsArticulatedVehicleCarryingDifferentCargoes(), Vehicle::Next(), BaseVehicle::type, VEH_TRAIN, and VerifyAutoreplaceRefitForOrders().

Referenced by BuildReplacementVehicle().

## ◆ GetNewEngineType()

static CommandCost GetNewEngineType | ( | const Vehicle * | v,
|  | const Company * | c,
|  | bool | always_replace,
|  | EngineID & | e
| ) |  |
| static

Get the EngineID of the replacement for a vehicle.

Parameters

  | v | The vehicle to find a replacement for
  | c | The vehicle's owner (it's faster to forward the pointer than refinding it)
  | always_replace | Always replace, even if not old.
  [out] | e | the EngineID of the replacement. INVALID_ENGINE if no replacement is found

Returns
  Error if the engine to build is not available

Definition at line 271 of file autoreplace_cmd.cpp.

References _current_company, Vehicle::engine_type, EngineReplacementForCompany(), SpecializedVehicle< Train, Type >::From(), Vehicle::group_id, INVALID_ENGINE, Vehicle::IsArticulatedPart(), IsEngineBuildable(), GroundVehicle< T, Type >::IsRearDualheaded(), Vehicle::NeedsAutorenewing(), BaseVehicle::type, and VEH_TRAIN.

Referenced by BuildReplacementVehicle(), and CmdAutoreplaceVehicle().

## ◆ ReplaceChain()

static CommandCost ReplaceChain | ( | Vehicle ** | chain,
|  | DoCommandFlag | flags,
|  | bool | wagon_removal,
|  | bool * | nothing_to_do
| ) |  |
| static

Replace a whole vehicle chain.

Parameters

  chain | vehicle chain to let autoreplace/renew operator on
  flags | command flags
  wagon_removal | remove wagons when the resulting chain occupies more tiles than the old did
  nothing_to_do | is set to 'false' when something was done (only valid when not failed)

Returns
  cost or error

< Number of units in the chain

< Will store vehicles of the old chain in their order

< New vehicles corresponding to old_vehs or nullptr if no replacement

< Costs for buying and refitting the new vehicles

< Shall store the last engine unit after this step

Definition at line 488 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), BuildReplacementVehicle(), CeilDiv(), CmdMoveVehicle(), DC_AUTOREPLACE, DC_EXEC, EXPENSES_NEW_VEHICLES, CommandCost::Failed(), SpecializedVehicle< Train, Type >::From(), CommandCost::GetCost(), Train::GetNextUnit(), Vehicle::IsPrimaryVehicle(), CommandCost::Succeeded(), TILE_SIZE, BaseVehicle::type, and VEH_TRAIN.

Referenced by CmdAutoreplaceVehicle().

## ◆ ReplaceFreeUnit()

static CommandCost ReplaceFreeUnit | ( | Vehicle ** | single_unit,
|  | DoCommandFlag | flags,
|  | bool * | nothing_to_do
| ) |  |
| static

Replace a single unit in a free wagon chain.

Parameters

  single_unit | vehicle to let autoreplace/renew operator on
  flags | command flags
  nothing_to_do | is set to 'false' when something was done (only valid when not failed)

Returns
  cost or error

Definition at line 436 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), BuildReplacementVehicle(), CmdMoveVehicle(), DC_EXEC, DoCommand(), EXPENSES_NEW_VEHICLES, SpecializedVehicle< Train, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Vehicle::IsArticulatedPart(), GroundVehicle< T, Type >::IsRearDualheaded(), AI::NewEvent(), Vehicle::owner, CommandCost::Succeeded(), and TransferCargo().

Referenced by CmdAutoreplaceVehicle().

## ◆ TransferCargo()

static void TransferCargo | ( | Vehicle * | old_veh,
|  | Vehicle * | new_head,
|  | bool | part_of_chain
| ) |  |
| static

Transfer cargo from a single (articulated )old vehicle to the new vehicle chain.

Parameters

  old_veh | Old vehicle that will be sold
  new_head | Head of the completely constructed new vehicle chain
  part_of_chain | The vehicle is part of a train

Precondition
  You can only do this if both consists are not loading or unloading. They must not carry reserved cargo, nor cargo to be unloaded or transferred.

Definition at line 139 of file autoreplace_cmd.cpp.

References Vehicle::cargo, CCF_LOADUNLOAD, Train::ConsistChanged(), SpecializedVehicle< Train, Type >::From(), Vehicle::IsArticulatedPart(), Vehicle::IsPrimaryVehicle(), CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP, Vehicle::Next(), NUM_CARGO, VehicleCargoList::TotalCount(), BaseVehicle::type, and VEH_TRAIN.

Referenced by ReplaceFreeUnit().

## ◆ VerifyAutoreplaceRefitForOrders()

static bool VerifyAutoreplaceRefitForOrders | ( | const Vehicle * | v,
|  | EngineID | engine_type
| ) |  |
| static

Tests whether refit orders that applied to v will also apply to the new vehicle type.

Parameters

  v | The vehicle to be replaced
  engine_type | The type we want to replace with

Returns
  true iff all refit orders stay valid

Definition at line 179 of file autoreplace_cmd.cpp.

References Vehicle::engine_type, Vehicle::First(), GetUnionOfArticulatedRefitMasks(), HasBit(), Vehicle::Orders(), BaseVehicle::type, and VEH_TRAIN.

Referenced by GetNewCargoTypeForReplace().