OpenTTD Source  20241121-master-g67a0fccfad
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 "train.h"
#include "aircraft.h"
#include "newgrf_text.h"
#include "vehicle_func.h"
#include "string_func.h"
#include "depot_map.h"
#include "vehiclelist.h"
#include "engine_func.h"
#include "articulated_vehicles.h"
#include "autoreplace_gui.h"
#include "group.h"
#include "order_backup.h"
#include "ship.h"
#include "newgrf.h"
#include "company_base.h"
#include "core/random_func.hpp"
#include "vehicle_cmd.h"
#include "aircraft_cmd.h"
#include "autoreplace_cmd.h"
#include "group_cmd.h"
#include "order_cmd.h"
#include "roadveh_cmd.h"
#include "train_cmd.h"
#include "ship_cmd.h"
#include <sstream>
#include <iomanip>
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  RefitResult
 Helper structure for RefitVehicle() More...
 

Functions

std::tuple< CommandCost, VehicleID, uint, uint16_t, CargoArrayCmdBuildVehicle (DoCommandFlag flags, TileIndex tile, EngineID eid, bool use_free_vehicles, CargoID cargo, ClientID client_id)
 Build a vehicle. More...
 
CommandCost CmdSellVehicle (DoCommandFlag flags, VehicleID v_id, bool sell_chain, bool backup_order, ClientID client_id)
 Sell a vehicle. More...
 
static int GetRefitCostFactor (const Vehicle *v, EngineID engine_type, CargoID new_cid, uint8_t new_subtype, bool *auto_refit_allowed)
 Helper to run the refit cost callback. More...
 
static CommandCost GetRefitCost (const Vehicle *v, EngineID engine_type, CargoID new_cid, uint8_t new_subtype, bool *auto_refit_allowed)
 Learn the price of refitting a certain engine. More...
 
static std::tuple< CommandCost, uint, uint16_t, CargoArrayRefitVehicle (Vehicle *v, bool only_this, uint8_t num_vehicles, CargoID new_cid, uint8_t new_subtype, DoCommandFlag flags, bool auto_refit)
 Refits a vehicle (chain). More...
 
std::tuple< CommandCost, uint, uint16_t, CargoArrayCmdRefitVehicle (DoCommandFlag flags, VehicleID veh_id, CargoID new_cid, uint8_t new_subtype, bool auto_refit, bool only_this, uint8_t num_vehicles)
 Refits a vehicle to the specified cargo type. More...
 
CommandCost CmdStartStopVehicle (DoCommandFlag flags, VehicleID veh_id, bool evaluate_startstop_cb)
 Start/Stop a vehicle. More...
 
CommandCost CmdMassStartStopVehicle (DoCommandFlag flags, TileIndex tile, bool do_start, bool vehicle_list_window, const VehicleListIdentifier &vli)
 Starts or stops a lot of vehicles. More...
 
CommandCost CmdDepotSellAllVehicles (DoCommandFlag flags, TileIndex tile, VehicleType vehicle_type)
 Sells all vehicles in a depot. More...
 
CommandCost CmdDepotMassAutoReplace (DoCommandFlag flags, TileIndex tile, VehicleType vehicle_type)
 Autoreplace all vehicles in the depot. More...
 
bool IsUniqueVehicleName (const std::string &name)
 Test if a name is unique among vehicle names. More...
 
static void CloneVehicleName (const Vehicle *src, Vehicle *dst)
 Clone the custom name of a vehicle, adding or incrementing a number. More...
 
std::tuple< CommandCost, VehicleIDCmdCloneVehicle (DoCommandFlag flags, TileIndex tile, VehicleID veh_id, bool share_orders)
 Clone a vehicle. More...
 
static CommandCost SendAllVehiclesToDepot (DoCommandFlag flags, bool service, const VehicleListIdentifier &vli)
 Send all vehicles of type to depots. More...
 
CommandCost CmdSendVehicleToDepot (DoCommandFlag flags, VehicleID veh_id, DepotCommand depot_cmd, const VehicleListIdentifier &vli)
 Send a vehicle to the depot. More...
 
CommandCost CmdRenameVehicle (DoCommandFlag flags, VehicleID veh_id, const std::string &text)
 Give a custom name to your vehicle. More...
 
CommandCost CmdChangeServiceInt (DoCommandFlag flags, VehicleID veh_id, uint16_t serv_int, bool is_custom, bool is_percent)
 Change the service interval of a vehicle. More...
 

Variables

const StringID _veh_build_msg_table []
 
const StringID _veh_sell_msg_table []
 
const StringID _veh_refit_msg_table []
 
const StringID _send_to_depot_msg_table []
 

Detailed Description

Commands for vehicles.

Definition in file vehicle_cmd.cpp.

Function Documentation

◆ CloneVehicleName()

static void CloneVehicleName ( const Vehicle src,
Vehicle dst 
)
static

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

Parameters
srcSource vehicle, with a custom name.
dstDestination vehicle.

Definition at line 767 of file vehicle_cmd.cpp.

References IsUniqueVehicleName(), and BaseConsist::name.

◆ CmdBuildVehicle()

std::tuple<CommandCost, VehicleID, uint, uint16_t, CargoArray> CmdBuildVehicle ( DoCommandFlag  flags,
TileIndex  tile,
EngineID  eid,
bool  use_free_vehicles,
CargoID  cargo,
ClientID  client_id 
)

Build a vehicle.

Parameters
flagsfor command
tiletile of depot where the vehicle is built
eidvehicle type being built.
use_free_vehiclesuse free vehicles when building the vehicle.
cargorefit cargo type.
client_idUser
Returns
the cost of this operation + the new vehicle ID + the refitted capacity + the refitted mail capacity (aircraft) or an error

Definition at line 87 of file vehicle_cmd.cpp.

References _current_company, AIR_CTOL, CMD_ERROR, CountArticulatedParts(), EXPENSES_NEW_VEHICLES, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetCost(), Engine::GetDefaultCargoType(), GetDepotVehicleType(), INVALID_VEHICLE, IsDepotTile(), IsEngineBuildable(), IsTileOwner(), IsValidCargoID(), NUM_CARGO, RAILVEH_MULTIHEAD, AircraftVehicleInfo::subtype, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

◆ CmdChangeServiceInt()

CommandCost CmdChangeServiceInt ( DoCommandFlag  flags,
VehicleID  veh_id,
uint16_t  serv_int,
bool  is_custom,
bool  is_percent 
)

Change the service interval of a vehicle.

Parameters
flagstype of operation
veh_idvehicle ID that is being service-interval-changed
serv_intnew service interval
is_customservice interval is custom flag
is_percentservice interval is percentage flag
Returns
the cost of this operation or an error

Definition at line 1107 of file vehicle_cmd.cpp.

◆ CmdCloneVehicle()

std::tuple<CommandCost, VehicleID> CmdCloneVehicle ( DoCommandFlag  flags,
TileIndex  tile,
VehicleID  veh_id,
bool  share_orders 
)

Clone a vehicle.

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

Parameters
flagstype of operation
tiletile of the depot where the cloned vehicle is build
veh_idthe original vehicle's index
share_ordersshared orders, else copied orders
Returns
the cost of this operation + the new vehicle ID or an error

Definition at line 826 of file vehicle_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::CanAllocateItem(), CheckOwnership(), CMD_ERROR, DC_EXEC, EXPENSES_NEW_VEHICLES, CommandCost::Failed(), SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), INVALID_VEHICLE, Vehicle::IsFrontEngine(), Vehicle::IsPrimaryVehicle(), Vehicle::Next(), Vehicle::owner, BaseVehicle::type, and VEH_TRAIN.

◆ CmdDepotMassAutoReplace()

CommandCost CmdDepotMassAutoReplace ( DoCommandFlag  flags,
TileIndex  tile,
VehicleType  vehicle_type 
)

Autoreplace all vehicles in the depot.

Parameters
flagstype of operation
tileTile of the depot where the vehicles are
vehicle_typeType of vehicle
Returns
the cost of this operation or an error

Definition at line 726 of file vehicle_cmd.cpp.

References _current_company, CommandCost::AddCost(), BuildDepotVehicleList(), CMD_ERROR, EXPENSES_NEW_VEHICLES, IsCompanyBuildableVehicleType(), IsDepotTile(), IsTileOwner(), and CommandCost::Succeeded().

◆ CmdDepotSellAllVehicles()

CommandCost CmdDepotSellAllVehicles ( DoCommandFlag  flags,
TileIndex  tile,
VehicleType  vehicle_type 
)

Sells all vehicles in a depot.

Parameters
flagstype of operation
tileTile of the depot where the depot is
vehicle_typeVehicle type
Returns
the cost of this operation or an error

Definition at line 692 of file vehicle_cmd.cpp.

References _current_company, CommandCost::AddCost(), BuildDepotVehicleList(), CMD_ERROR, EXPENSES_NEW_VEHICLES, INVALID_CLIENT_ID, IsCompanyBuildableVehicleType(), IsDepotTile(), IsTileOwner(), and CommandCost::Succeeded().

◆ CmdMassStartStopVehicle()

CommandCost CmdMassStartStopVehicle ( DoCommandFlag  flags,
TileIndex  tile,
bool  do_start,
bool  vehicle_list_window,
const VehicleListIdentifier vli 
)

Starts or stops a lot of vehicles.

Parameters
flagstype of operation
tileTile of the depot where the vehicles are started/stopped (only used for depots)
do_startset = start vehicles, unset = stop vehicles
vehicle_list_windowif set, then it's a vehicle list window, not a depot and Tile is ignored in this case
vliVehicleListIdentifier
Returns
the cost of this operation or an error

Definition at line 658 of file vehicle_cmd.cpp.

References _current_company, BuildDepotVehicleList(), CMD_ERROR, do_start, GenerateVehicleSortList(), IsCompanyBuildableVehicleType(), IsDepotTile(), IsTileOwner(), VS_STOPPED, and VehicleListIdentifier::vtype.

◆ CmdRefitVehicle()

std::tuple<CommandCost, uint, uint16_t, CargoArray> CmdRefitVehicle ( DoCommandFlag  flags,
VehicleID  veh_id,
CargoID  new_cid,
uint8_t  new_subtype,
bool  auto_refit,
bool  only_this,
uint8_t  num_vehicles 
)

Refits a vehicle to the specified cargo type.

Parameters
flagstype of operation
veh_idvehicle ID to refit
new_cidNew cargo type to refit to.
new_subtypeNew cargo subtype to refit to. 0xFF means to try keeping the same subtype according to GetBestFittingSubType().
auto_refitAutomatic refitting.
only_thisRefit only this vehicle. Used only for cloning vehicles.
num_vehiclesNumber of vehicles to refit (not counting articulated parts). Zero means all vehicles. Only used if "refit only this vehicle" is false.
Returns
the cost of this operation or an error

Definition at line 490 of file vehicle_cmd.cpp.

References CheckOwnership(), CMD_ERROR, Vehicle::current_order, DC_QUERY_COST, CommandCost::Failed(), Vehicle::First(), SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), IsCompanyBuildableVehicleType(), Order::IsType(), Vehicle::owner, BaseVehicle::type, VEH_AIRCRAFT, VEH_SHIP, and VEH_TRAIN.

◆ CmdRenameVehicle()

CommandCost CmdRenameVehicle ( DoCommandFlag  flags,
VehicleID  veh_id,
const std::string &  text 
)

◆ CmdSellVehicle()

CommandCost CmdSellVehicle ( DoCommandFlag  flags,
VehicleID  v_id,
bool  sell_chain,
bool  backup_order,
ClientID  client_id 
)

◆ CmdSendVehicleToDepot()

CommandCost CmdSendVehicleToDepot ( DoCommandFlag  flags,
VehicleID  veh_id,
DepotCommand  depot_cmd,
const VehicleListIdentifier vli 
)

Send a vehicle to the depot.

Parameters
flagsfor command type
veh_idvehicle ID to send to the depot
depot_cmdDEPOT_ flags (see vehicle_type.h)
vliVehicleListIdentifier.
Returns
the cost of this operation or an error

Definition at line 1047 of file vehicle_cmd.cpp.

References CMD_ERROR, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), HasFlag(), Vehicle::IsPrimaryVehicle(), MassSend, SendAllVehiclesToDepot(), Vehicle::SendToDepot(), and Service.

◆ CmdStartStopVehicle()

CommandCost CmdStartStopVehicle ( DoCommandFlag  flags,
VehicleID  veh_id,
bool  evaluate_startstop_cb 
)

Start/Stop a vehicle.

Parameters
flagstype of operation
veh_idvehicle to start/stop, don't forget to change CcStartStopVehicle if you modify this!
evaluate_startstop_cbShall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
Returns
the cost of this operation or an error

Definition at line 572 of file vehicle_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_AUTOREPLACE, CommandCost::Failed(), Aircraft::flags, SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), HasBit(), Vehicle::IsPrimaryVehicle(), Vehicle::owner, return_cmd_error, STARTTAKEOFF, Aircraft::state, TERM7, BaseVehicle::type, VAF_HELI_DIRECT_DESCENT, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, Vehicle::vehstatus, VS_CRASHED, and VS_STOPPED.

◆ GetRefitCost()

static CommandCost GetRefitCost ( const Vehicle v,
EngineID  engine_type,
CargoID  new_cid,
uint8_t  new_subtype,
bool *  auto_refit_allowed 
)
static

Learn the price of refitting a certain engine.

Parameters
vThe vehicle we are refitting, can be nullptr.
engine_typeWhich engine to refit
new_cidCargo type we are refitting to.
new_subtypeNew cargo subtype.
[out]auto_refit_allowedThe refit is allowed as an auto-refit.
Returns
Price for refitting

Definition at line 302 of file vehicle_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetRefitCostFactor(), Engine::type, and VEH_SHIP.

◆ GetRefitCostFactor()

static int GetRefitCostFactor ( const Vehicle v,
EngineID  engine_type,
CargoID  new_cid,
uint8_t  new_subtype,
bool *  auto_refit_allowed 
)
static

Helper to run the refit cost callback.

Parameters
vThe vehicle we are refitting, can be nullptr.
engine_typeWhich engine to refit
new_cidCargo type we are refitting to.
new_subtypeNew cargo subtype.
[out]auto_refit_allowedThe refit is allowed as an auto-refit.
Returns
Price for refitting

Definition at line 270 of file vehicle_cmd.cpp.

References GRFFile::cargo_map, CargoSpec::classes, CargoSpec::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), and Engine::GetGRF().

Referenced by GetRefitCost().

◆ IsUniqueVehicleName()

bool IsUniqueVehicleName ( const std::string &  name)

Test if a name is unique among vehicle names.

Parameters
nameName to test.
Returns
True ifffffff the name is unique.

Definition at line 753 of file vehicle_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate().

Referenced by CloneVehicleName(), and CmdRenameVehicle().

◆ RefitVehicle()

static std::tuple<CommandCost, uint, uint16_t, CargoArray> RefitVehicle ( Vehicle v,
bool  only_this,
uint8_t  num_vehicles,
CargoID  new_cid,
uint8_t  new_subtype,
DoCommandFlag  flags,
bool  auto_refit 
)
static

Refits a vehicle (chain).

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

Parameters
vThe vehicle to refit.
only_thisWhether to only refit this vehicle, or to check the rest of them.
num_vehiclesNumber of vehicles to refit (not counting articulated parts). Zero means the whole chain.
new_cidCargotype to refit to
new_subtypeCargo subtype to refit to. 0xFF means to try keeping the same subtype according to GetBestFittingSubType().
flagsCommand flags
auto_refitRefitting is done as automatic refitting outside a depot.
Returns
Refit cost + refittet capacity + mail capacity (aircraft).

Definition at line 359 of file vehicle_cmd.cpp.

References Engine::CanCarryCargo(), Vehicle::cargo_subtype, Vehicle::cargo_type, Engine::DetermineCapacity(), EF_AUTO_REFIT, Vehicle::First(), GetBestFittingSubType(), Vehicle::GetEngine(), Vehicle::GetExpenseType(), GetVehicleSet(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Vehicle::InvalidateNewGRFCacheOfChain(), Vehicle::IsArticulatedPart(), EngineInfo::misc_flags, Vehicle::Next(), BaseVehicle::type, and VEH_TRAIN.

◆ SendAllVehiclesToDepot()

static CommandCost SendAllVehiclesToDepot ( DoCommandFlag  flags,
bool  service,
const VehicleListIdentifier vli 
)
static

Send all vehicles of type to depots.

Parameters
flagsthe flags used for DoCommand()
serviceshould the vehicles only get service in the depots
vliidentifier of the vehicle list
Returns
0 for success and CMD_ERROR if no vehicle is able to go to depot

Definition at line 1013 of file vehicle_cmd.cpp.

References CMD_ERROR, DC_EXEC, DontCancel, GenerateVehicleSortList(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, None, Service, and CommandCost::Succeeded().

Referenced by CmdSendVehicleToDepot().

Variable Documentation

◆ _send_to_depot_msg_table

const StringID _send_to_depot_msg_table[]
Initial value:
= {
STR_ERROR_CAN_T_SEND_TRAIN_TO_DEPOT,
STR_ERROR_CAN_T_SEND_ROAD_VEHICLE_TO_DEPOT,
STR_ERROR_CAN_T_SEND_SHIP_TO_DEPOT,
STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR,
}

Definition at line 69 of file vehicle_cmd.cpp.

◆ _veh_build_msg_table

const StringID _veh_build_msg_table[]
Initial value:
= {
STR_ERROR_CAN_T_BUY_TRAIN,
STR_ERROR_CAN_T_BUY_ROAD_VEHICLE,
STR_ERROR_CAN_T_BUY_SHIP,
STR_ERROR_CAN_T_BUY_AIRCRAFT,
}

Definition at line 48 of file vehicle_cmd.cpp.

◆ _veh_refit_msg_table

const StringID _veh_refit_msg_table[]
Initial value:
= {
STR_ERROR_CAN_T_REFIT_TRAIN,
STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE,
STR_ERROR_CAN_T_REFIT_SHIP,
STR_ERROR_CAN_T_REFIT_AIRCRAFT,
}

Definition at line 62 of file vehicle_cmd.cpp.

◆ _veh_sell_msg_table

const StringID _veh_sell_msg_table[]
Initial value:
= {
STR_ERROR_CAN_T_SELL_TRAIN,
STR_ERROR_CAN_T_SELL_ROAD_VEHICLE,
STR_ERROR_CAN_T_SELL_SHIP,
STR_ERROR_CAN_T_SELL_AIRCRAFT,
}

Definition at line 55 of file vehicle_cmd.cpp.