# autoreplace_cmd.cpp File Reference

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

`#include "stdafx.h"`
`#include "company_func.h"`
`#include "vehicle_gui.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/alloc_func.hpp"`
`#include "table/strings.h"`

Go to the source code of this file.

## Functions
static bool | EnginesGotCargoInCommon (EngineID engine_a, EngineID engine_b, VehicleType type)
| 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 245 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), CMD_REVERSE_TRAIN_DIRECTION, DC_AUTOREPLACE, DC_EXEC, DoCommand(), CommandCost::Failed(), GetNewCargoTypeForReplace(), GetNewEngineType(), HasBit(), and CommandCost::Succeeded().

Referenced by ReplaceChain(), and ReplaceFreeUnit().

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

References AIR_CTOL, RailtypeInfo::compatible_railtypes, EF_ROAD_TRAM, EnginesGotCargoInCommon(), GetRailTypeInfo(), HasBit(), IsEngineBuildable(), and RAILVEH_WAGON.

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

Definition at line 606 of file autoreplace_cmd.cpp.

References CommandCost::AddCost(), CMD_ERROR, DC_EXEC, CommandCost::Failed(), CommandCost::GetCost(), GetNewEngineType(), GetNextUnit(), HASBITS, IsArticulatedPart(), IsFrontEngine(), IsRearDualheaded(), IsValidVehicleID(), ReplaceChain(), ReplaceFreeUnit(), RestoreRandomSeeds(), SaveRandomSeeds(), StartStopVehicle(), and CommandCost::Succeeded().

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

References CommandCost::AddCost(), 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 EnginesGotCargoInCommon | ( | EngineID | engine_a,
|  | EngineID | engine_b,
|  | VehicleType | type |
| ) |  |  | `[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 27 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 162 of file autoreplace_cmd.cpp.

References GetIntersectionOfArticulatedRefitMasks(), GetUnionOfArticulatedRefitMasks(), HasBit(), IsArticulatedVehicleCarryingDifferentCargos(), 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 215 of file autoreplace_cmd.cpp.

References EngineReplacementForCompany(), IsArticulatedPart(), IsEngineBuildable(), and IsRearDualheaded().

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
  | whole_chain | move all vehicles following 'v' (true), or only 'v' (false)

Returns:
  success or error

Definition at line 295 of file autoreplace_cmd.cpp.

References CMD_MOVE_RAIL_VEHICLE, and DoCommand().

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

References BuildReplacementVehicle(), CopyHeadSpecificThings(), DC_AUTOREPLACE, DC_EXEC, DoCommand(), CommandCost::GetCost(), GetNextUnit(), GetPrevUnit(), MoveVehicle(), RAILVEH_WAGON, CommandCost::Succeeded(), TILE_SIZE, and TransferCargo().

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

References CommandCost::AddCost(), BuildReplacementVehicle(), DC_EXEC, DoCommand(), IsArticulatedPart(), IsRearDualheaded(), MoveVehicle(), CommandCost::Succeeded(), 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 284 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 90 of file autoreplace_cmd.cpp.

References GetLastEnginePart(), IsArticulatedPart(), min(), CargoList::MTA_UNLOAD, and TrainConsistChanged().

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

References GetUnionOfArticulatedRefitMasks(), and HasBit().

Referenced by GetNewCargoTypeForReplace().