# vehicle_cmd.cpp File Reference

Commands for vehicles. More...

`#include "stdafx.h"`
`#include "roadveh.h"`
`#include "news_func.h"`
`#include "airport.h"`
`#include "command_func.h"`
`#include "company_func.h"`
`#include "vehicle_gui.h"`
`#include "train.h"`
`#include "aircraft.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_text.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "vehicle_func.h"`
`#include "string_func.h"`
`#include "depot_map.h"`
`#include "vehiclelist.h"`
`#include "engine_base.h"`
`#include "table/strings.h"`

Go to the source code of this file.

## Functions
CommandCost | CmdStartStopVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Start/Stop a vehicle.
CommandCost | CmdMassStartStopVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Starts or stops a lot of vehicles.
CommandCost | CmdDepotSellAllVehicles (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sells all vehicles in a depot.
CommandCost | CmdDepotMassAutoReplace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Autoreplace all vehicles in the depot.
static CommandCost | GetRefitCost (EngineID engine_type)
| Learn the price of refitting a certain engine.
CommandCost | RefitVehicle (Vehicle *v, bool only_this, CargoID new_cid, byte new_subtype, DoCommandFlag flags)
| Refits a vehicle (chain).
static bool | IsUniqueVehicleName (const char *name)
| Test if a name is unique among vehicle names.
static void | CloneVehicleName (const Vehicle *src, Vehicle *dst)
| Clone the custom name of a vehicle, adding or incrementing a number.
CommandCost | CmdCloneVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Clone a vehicle.
CommandCost | SendAllVehiclesToDepot (VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
| Send all vehicles of type to depots.
CommandCost | CmdRenameVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Give a custom name to your vehicle.
CommandCost | CmdChangeServiceInt (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Change the service interval of a vehicle.
## Variables
const uint32 | _veh_build_proc_table []
const uint32 | _veh_sell_proc_table []
const uint32 | _veh_refit_proc_table []
const uint32 | _send_to_depot_proc_table []

## Detailed Description

Commands for vehicles.

Definition in file vehicle_cmd.cpp.

## Function Documentation

static void CloneVehicleName | ( | const Vehicle * | src,
|  | Vehicle * | dst |
| ) |  |  | `[static]`

Clone the custom name of a vehicle, adding or incrementing a number.

Parameters:

  | src | Source vehicle, with a custom name.
  | dst | Destination vehicle.

Definition at line 339 of file vehicle_cmd.cpp.

References IsUniqueVehicleName(), lastof, Vehicle::name, seprintf(), strecat(), and strecpy().

Referenced by CmdCloneVehicle().

CommandCost CmdChangeServiceInt | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Change the service interval of a vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID that is being service-interval-changed
  | p2 | new service interval
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 638 of file vehicle_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), GetServiceIntervalClamped(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, and SetWindowDirty().

CommandCost CmdCloneVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Clone a vehicle.

If it is a train, it will clone all the cars too

Parameters:

  | tile | tile of the depot where the cloned vehicle is build
  | flags | type of operation
  | p1 | the original vehicle's index
  | p2 | 1 = shared orders, else copied orders
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 389 of file vehicle_cmd.cpp.

References CommandCost::AddCost(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::CanAllocateItem(), Engine::CanCarryCargo(), Vehicle::cargo_subtype, Vehicle::cargo_type, CheckOwnership(), CloneVehicleName(), CMD_ADD_VEHICLE_GROUP, CMD_CLONE_ORDER, CMD_ERROR, CMD_MOVE_RAIL_VEHICLE, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DoCommand(), EXPENSES_NEW_VEHICLES, CommandCost::Failed(), SpecializedVehicle< RoadVehicle, VEH_ROAD >::From(), SpecializedVehicle< Train, VEH_TRAIN >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Get(), GetBestFittingSubType(), Engine::GetDefaultCargoType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), Train::GetNextArticPart(), Train::GetNextVehicle(), GetRefitCost(), Vehicle::group_id, RoadVehicle::HasArticulatedPart(), Train::HasArticulatedPart(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Train::IsFrontEngine(), Vehicle::IsPrimaryVehicle(), Train::IsRearDualheaded(), Vehicle::name, Vehicle::Next(), return_cmd_error, SetBit(), CommandCost::Succeeded(), Vehicle::tile, BaseVehicle::type, VEH_ROAD, and VEH_TRAIN.

CommandCost CmdDepotMassAutoReplace | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Autoreplace all vehicles in the depot.

Parameters:

  | tile | Tile of the depot where the vehicles are
  | flags | type of operation
  | p1 | Type of vehicle
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 204 of file vehicle_cmd.cpp.

References CommandCost::AddCost(), BuildDepotVehicleList(), CMD_AUTOREPLACE_VEHICLE, CMD_ERROR, DoCommand(), EXPENSES_NEW_VEHICLES, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, IsDepotTile(), Vehicle::IsInDepot(), IsTileOwner(), SmallVector< T, S >::Length(), and CommandCost::Succeeded().

CommandCost CmdDepotSellAllVehicles | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Sells all vehicles in a depot.

Parameters:

  | tile | Tile of the depot where the depot is
  | flags | type of operation
  | p1 | Vehicle type
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 175 of file vehicle_cmd.cpp.

References CommandCost::AddCost(), BuildDepotVehicleList(), CMD_ERROR, DoCommand(), EXPENSES_NEW_VEHICLES, GB(), CommandCost::GetCost(), SmallVector< T, S >::Length(), and CommandCost::Succeeded().

CommandCost CmdMassStartStopVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Starts or stops a lot of vehicles.

Parameters:

  | tile | Tile of the depot where the vehicles are started/stopped (only used for depots)
  | flags | type of operation
  | p1 | Station/Order/Depot ID (only used for vehicle list windows)
  | p2 | bitmask bit 0-4 Vehicle type bit 5 false = start vehicles, true = stop vehicles bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 130 of file vehicle_cmd.cpp.

References BuildDepotVehicleList(), CMD_START_STOP_VEHICLE, DoCommand(), SpecializedVehicle< Train, VEH_TRAIN >::From(), GB(), GenerateVehicleSortList(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Train::IsInDepot(), SmallVector< T, S >::Length(), VEH_TRAIN, and Vehicle::vehstatus.

CommandCost CmdRenameVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Give a custom name to your vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID to name
  | p2 | unused
  | text | the new name or an empty string when resetting to the default

Returns:
  the cost of this operation or an error

Definition at line 607 of file vehicle_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_AUTOREPLACE, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), InvalidateWindowClassesData(), IsUniqueVehicleName(), MarkWholeScreenDirty(), MAX_LENGTH_VEHICLE_NAME_BYTES, Vehicle::name, return_cmd_error, and StrEmpty().

CommandCost CmdStartStopVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Start/Stop a vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle to start/stop
  | p2 | bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 71 of file vehicle_cmd.cpp.

References AccelerationCache::cached_power, CALLBACK_FAILED, CBID_VEHICLE_START_STOP_CHECK, CheckOwnership(), CMD_ERROR, Vehicle::cur_speed, DC_AUTOREPLACE, DC_EXEC, DeleteVehicleNews(), error(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), SpecializedVehicle< Train, VEH_TRAIN >::From(), GB(), GetEngineGRFID(), GetGRFStringID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), GetVehicleCallback(), GetWindowClassForVehicleType(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Vehicle::IsPrimaryVehicle(), Vehicle::IsStoppedInDepot(), Vehicle::MarkDirty(), return_cmd_error, SetBit(), SetWindowClassesDirty(), SetWindowDirty(), SetWindowWidgetDirty(), Vehicle::tile, BaseVehicle::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, and Vehicle::vehstatus.

static CommandCost GetRefitCost | ( | EngineID | engine_type | ) | `[static]`

Learn the price of refitting a certain engine.

Parameters:

  | engine_type | Which engine to refit

Returns:
  Price for refitting

Definition at line 232 of file vehicle_cmd.cpp.

References EXPENSES_AIRCRAFT_RUN, EXPENSES_ROADVEH_RUN, EXPENSES_SHIP_RUN, EXPENSES_TRAIN_RUN, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetPrice(), RAILVEH_WAGON, Engine::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by CmdCloneVehicle(), and RefitVehicle().

static bool IsUniqueVehicleName | ( | const char * | name | ) | `[static]`

Test if a name is unique among vehicle names.

Parameters:

  | name | Name to test.

Returns:
  True ifffffff the name is unique.

Definition at line 324 of file vehicle_cmd.cpp.

References Vehicle::name.

Referenced by CloneVehicleName(), and CmdRenameVehicle().

CommandCost RefitVehicle | ( | Vehicle * | v,
|  | bool | only_this,
|  | CargoID | new_cid,
|  | byte | new_subtype,
|  | DoCommandFlag | flags |
| ) |  |  |

Refits a vehicle (chain).

This is the vehicle-type independent part of the CmdRefitXXX functions.

Parameters:

  | v | The vehicle to refit.
  | only_this | Whether to only refit this vehicle, or the whole chain.
  | new_cid | Cargotype to refit to
  | new_subtype | Cargo subtype to refit to
  | flags | Command flags

Returns:
  refit cost; or CMD_ERROR if no vehicle was actually refitable to the cargo

Definition at line 275 of file vehicle_cmd.cpp.

References Engine::CanCarryCargo(), Vehicle::cargo, Vehicle::cargo_cap, Vehicle::cargo_subtype, Vehicle::cargo_type, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Vehicle::GetExpenseType(), GetRefitCost(), GetVehicleCapacity(), HasBit(), Vehicle::InvalidateNewGRFCacheOfChain(), Vehicle::Next(), CargoList< Tinst >::Truncate(), BaseVehicle::type, and VEH_AIRCRAFT.

Referenced by CmdRefitAircraft(), CmdRefitRailVehicle(), CmdRefitRoadVeh(), and CmdRefitShip().

CommandCost SendAllVehiclesToDepot | ( | VehicleType | type,
|  | DoCommandFlag | flags,
|  | bool | service,
|  | Owner | owner,
|  | uint16 | vlw_flag,
|  | uint32 | id |
| ) |  |  |

Send all vehicles of type to depots.

Parameters:

  | type | type of vehicle
  | flags | the flags used for DoCommand()
  | service | should the vehicles only get service in the depots
  | owner | owner of the vehicles to send
  | vlw_flag | tells what kind of list requested the goto depot
  | id | general purpose id whoms meaning is given by `vlw_flag`; e.g. StationID for station lists

Returns:
  0 for success and CMD_ERROR if no vehicle is able to go to depot

Definition at line 576 of file vehicle_cmd.cpp.

References CMD_ERROR, DC_EXEC, DEPOT_DONT_CANCEL, DoCommand(), GenerateVehicleSortList(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, SmallVector< T, S >::Length(), CommandCost::Succeeded(), and Vehicle::tile.

Referenced by CmdSendAircraftToHangar(), CmdSendRoadVehToDepot(), CmdSendShipToDepot(), and CmdSendTrainToDepot().

## Variable Documentation

const uint32 _send_to_depot_proc_table[]

Initial value:

     {

      CMD_SEND_TRAIN_TO_DEPOT     | CMD_MSG(STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT) | CMD_NO_TEST_IF_IN_NETWORK,
      CMD_SEND_ROADVEH_TO_DEPOT   | CMD_MSG(STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT),
      CMD_SEND_SHIP_TO_DEPOT      | CMD_MSG(STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT),
      CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR),
    }

Definition at line 55 of file vehicle_cmd.cpp.

const uint32 _veh_build_proc_table[]

Initial value:

     {
      CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
      CMD_BUILD_ROAD_VEH     | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
      CMD_BUILD_SHIP         | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
      CMD_BUILD_AIRCRAFT     | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT),
    }

Definition at line 34 of file vehicle_cmd.cpp.

const uint32 _veh_refit_proc_table[]

Initial value:

     {
      CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
      CMD_REFIT_ROAD_VEH     | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
      CMD_REFIT_SHIP         | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
      CMD_REFIT_AIRCRAFT     | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
    }

Definition at line 48 of file vehicle_cmd.cpp.

const uint32 _veh_sell_proc_table[]

Initial value:

     {
      CMD_SELL_RAIL_WAGON | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
      CMD_SELL_ROAD_VEH   | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
      CMD_SELL_SHIP       | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
      CMD_SELL_AIRCRAFT   | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
    }

Definition at line 41 of file vehicle_cmd.cpp.