# autoreplace_cmd.cpp File Reference

Deals with autoreplace execution but not the setup. More...

`#include "stdafx.h"`
`#include "company_func.h"`
`#include "train.h"`
`#include "rail.h"`
`#include "command_func.h"`
`#include "engine_base.h"`
`#include "engine_func.h"`
`#include "vehicle_func.h"`
`#include "functions.h"`
`#include "autoreplace_func.h"`
`#include "articulated_vehicles.h"`
`#include "core/random_func.hpp"`
`#include "table/strings.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.
void | ChangeVehicleNews (VehicleID from_index, VehicleID to_index)
| Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
void | ChangeVehicleViewWindow (VehicleID from_index, VehicleID to_index)
| Report a change in vehicle IDs (due to autoreplace) to affected vehicle windows.
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).
bool | CheckAutoreplaceValidity (EngineID from, EngineID to, CompanyID company)
| Checks some basic properties whether autoreplace is allowed.
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.
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.
static CargoID | GetNewCargoTypeForReplace (Vehicle *v, EngineID engine_type, bool part_of_chain)
| Function to find what type of cargo to refit to when autoreplacing.
static EngineID | GetNewEngineType (const Vehicle *v, const Company *c)
| Get the EngineID of the replacement for a vehicle.
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).
static CommandCost | StartStopVehicle (const Vehicle *v, bool evaluate_callback)
| Issue a start/stop command.
static CommandCost | MoveVehicle (const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
| Issue a train vehicle move command.
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.
static CommandCost | ReplaceFreeUnit (Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
| Replace a single unit in a free wagon chain.
static CommandCost | ReplaceChain (Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
| Replace a whole vehicle chain.
CommandCost | CmdAutoreplaceVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on their own.

## Detailed Description

Deals with autoreplace execution but not the setup.

Definition in file autoreplace_cmd.cpp.

## Function Documentation

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 refittet vehicle
  | part_of_chain | The vehicle is part of a train

Returns:
  cost or error

Definition at line 252 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), CMD_REVERSE_TRAIN_DIRECTION, CT_INVALID, CT_NO_REFIT, DC_AUTOREPLACE, DC_EXEC, DoCommand(), CommandCost::Failed(), SpecializedVehicle< Train, VEH_TRAIN >::From(), GetBestFittingSubType(), GetNewCargoTypeForReplace(), GetNewEngineType(), HasBit(), CommandCost::Succeeded(), and VEH_TRAIN.

Referenced by ReplaceChain(), and ReplaceFreeUnit().

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 797 of file news_gui.cpp.

References _oldest_news, NR_VEHICLE, and NS_ADVICE.

Referenced by CopyHeadSpecificThings().

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 2514 of file window.cpp.

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

Referenced by CopyHeadSpecificThings().

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 741 of file vehicle_gui.cpp.

References ChangeVehicleWindow().

Referenced by CopyHeadSpecificThings().

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 51 of file autoreplace_cmd.cpp.

References AIR_CTOL, RailtypeInfo::compatible_railtypes, EF_ROAD_TRAM, EnginesHaveCargoInCommon(), GetRailTypeInfo(), HasBit(), IsEngineBuildable(), RAILVEH_WAGON, VEH_AIRCRAFT, VEH_ROAD, and VEH_TRAIN.

Referenced by CmdSetAutoReplace(), and ReplaceVehicleWindow::GenerateReplaceVehList().

CommandCost CmdAutoreplaceVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | 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 623 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), CheckOwnership(), CMD_ERROR, DC_EXEC, CommandCost::Failed(), SpecializedVehicle< T, Type >::First(), SpecializedVehicle< Train, VEH_TRAIN >::From(), CommandCost::GetCost(), GetNewEngineType(), Train::GetNextUnit(), Train::IsArticulatedPart(), Train::IsFrontEngine(), Train::IsRearDualheaded(), ReplaceChain(), ReplaceFreeUnit(), RestoreRandomSeeds(), SaveRandomSeeds(), StartStopVehicle(), CommandCost::Succeeded(), and VEH_TRAIN.

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 318 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), ChangeVehicleNews(), ChangeVehicleViewports(), ChangeVehicleViewWindow(), CMD_ADD_VEHICLE_GROUP, CMD_CLONE_ORDER, CMD_RENAME_VEHICLE, DC_AUTOREPLACE, DC_EXEC, DoCommand(), StartStopVehicle(), and CommandCost::Succeeded().

Referenced by ReplaceChain().

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
  | type | the type of the engines

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 37 of file autoreplace_cmd.cpp.

References GetUnionOfArticulatedRefitMasks().

Referenced by CheckAutoreplaceValidity().

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 169 of file autoreplace_cmd.cpp.

References CT_INVALID, CT_NO_REFIT, GetArticulatedRefitMasks(), HasBit(), IsArticulatedVehicleCarryingDifferentCargos(), NUM_CARGO, VEH_TRAIN, and VerifyAutoreplaceRefitForOrders().

Referenced by BuildReplacementVehicle().

static EngineID GetNewEngineType | ( | const Vehicle * | v,
|  | const Company * | c |
| ) |  |  | `[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)

Returns:
  the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found

Definition at line 222 of file autoreplace_cmd.cpp.

References EngineReplacementForCompany(), SpecializedVehicle< Train, VEH_TRAIN >::From(), Train::IsArticulatedPart(), IsEngineBuildable(), Train::IsRearDualheaded(), and VEH_TRAIN.

Referenced by BuildReplacementVehicle(), and CmdAutoreplaceVehicle().

static CommandCost MoveVehicle | ( | const Vehicle * | v,
|  | const Vehicle * | after,
|  | DoCommandFlag | flags,
|  | bool | whole_chain |
| ) |  |  | `[inline, static]`

Issue a train vehicle move command.

Parameters:

  | v | The vehicle to move
  | after | The vehicle to insert 'v' after, or NULL 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 308 of file autoreplace_cmd.cpp.

References CMD_MOVE_RAIL_VEHICLE, DoCommand(), and INVALID_VEHICLE.

Referenced by ReplaceChain(), and ReplaceFreeUnit().

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 NULL if no replacement

< Costs for buying and refitting the new vehicles

< Shall store the last engine unit after this step

Definition at line 412 of file autoreplace_cmd.cpp.

References BuildReplacementVehicle(), TrainCache::cached_total_length, CopyHeadSpecificThings(), DC_AUTOREPLACE, DC_EXEC, DoCommand(), SpecializedVehicle< Train, VEH_TRAIN >::From(), CommandCost::GetCost(), Train::GetNextUnit(), Train::GetPrevUnit(), MoveVehicle(), RAILVEH_WAGON, CommandCost::Succeeded(), Train::tcache, TILE_SIZE, TransferCargo(), and VEH_TRAIN.

Referenced by CmdAutoreplaceVehicle().

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 363 of file autoreplace_cmd.cpp.

References BuildReplacementVehicle(), DC_EXEC, DoCommand(), SpecializedVehicle< Train, VEH_TRAIN >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Train::IsArticulatedPart(), Train::IsRearDualheaded(), MoveVehicle(), and TransferCargo().

Referenced by CmdAutoreplaceVehicle().

static CommandCost StartStopVehicle | ( | const Vehicle * | v,
|  | bool | evaluate_callback |
| ) |  |  | `[inline, static]`

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 296 of file autoreplace_cmd.cpp.

References CMD_START_STOP_VEHICLE, DC_AUTOREPLACE, DC_EXEC, and DoCommand().

Referenced by CmdAutoreplaceVehicle(), and CopyHeadSpecificThings().

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

Definition at line 97 of file autoreplace_cmd.cpp.

References Train::ConsistChanged(), SpecializedVehicle< Train, VEH_TRAIN >::From(), Train::GetLastEnginePart(), Train::IsArticulatedPart(), min(), CargoList< VehicleCargoList >::MTA_UNLOAD, NUM_CARGO, Train::other_multiheaded_part, and VEH_TRAIN.

Referenced by ReplaceChain(), and ReplaceFreeUnit().

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 135 of file autoreplace_cmd.cpp.

References GetUnionOfArticulatedRefitMasks(), HasBit(), and VEH_TRAIN.

Referenced by GetNewCargoTypeForReplace().