# command.cpp File Reference

Handling of commands. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "landscape.h"`
`#include "gui.h"`
`#include "command_func.h"`
`#include "network/network.h"`
`#include "genworld.h"`
`#include "newgrf_storage.h"`
`#include "strings_func.h"`
`#include "gfx_func.h"`
`#include "functions.h"`
`#include "town.h"`
`#include "date_func.h"`
`#include "debug.h"`
`#include "company_func.h"`
`#include "company_base.h"`
`#include "signal_func.h"`
`#include "table/strings.h"`

Go to the source code of this file.

## Defines
#define | DEF_CMD(proc, flags) {proc, #proc, flags}
#define | return_dcpi(cmd, clear) { _docommand_recursive = 0; ClearStorageChanges(clear); return cmd; }
| Helper to deduplicate the code for returning.
## Functions
bool | IsValidCommand (uint32 cmd)
| Checks if a integer value belongs to a command.
byte | GetCommandFlags (uint32 cmd)
| Returns the flags from a given command.
const char * | GetCommandName (uint32 cmd)
| Returns the name of a given command.
CommandCost | DoCommand (const CommandContainer *container, DoCommandFlag flags)
| Shorthand for calling the long DoCommand with a container.
CommandCost | DoCommand (TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
| Execute a command.
Money | GetAvailableMoneyForCommand ()
| Returns the current money available which can be used for a command.
bool | DoCommandP (const CommandContainer *container, bool my_cmd)
| Shortcut for the long DoCommandP when having a container with the data.
bool | DoCommandP (TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
| Execute a network safe DoCommand function.
CommandCost | DoCommandPInternal (TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
| Internal helper function for DoCommandP.
## Variables
StringID | _error_message
CommandProc | CmdBuildRailroadTrack
CommandProc | CmdRemoveRailroadTrack
CommandProc | CmdBuildSingleRail
CommandProc | CmdRemoveSingleRail
CommandProc | CmdLandscapeClear
CommandProc | CmdBuildBridge
CommandProc | CmdBuildRailStation
CommandProc | CmdRemoveFromRailStation
CommandProc | CmdConvertRail
CommandProc | CmdBuildSingleSignal
CommandProc | CmdRemoveSingleSignal
CommandProc | CmdTerraformLand
CommandProc | CmdPurchaseLandArea
CommandProc | CmdSellLandArea
CommandProc | CmdBuildTunnel
CommandProc | CmdBuildTrainDepot
CommandProc | CmdBuildRailWaypoint
CommandProc | CmdRenameWaypoint
CommandProc | CmdRemoveFromRailWaypoint
CommandProc | CmdBuildRoadStop
CommandProc | CmdRemoveRoadStop
CommandProc | CmdBuildLongRoad
CommandProc | CmdRemoveLongRoad
CommandProc | CmdBuildRoad
CommandProc | CmdBuildRoadDepot
CommandProc | CmdBuildAirport
CommandProc | CmdBuildDock
CommandProc | CmdBuildShipDepot
CommandProc | CmdBuildBuoy
CommandProc | CmdPlantTree
CommandProc | CmdBuildRailVehicle
CommandProc | CmdMoveRailVehicle
CommandProc | CmdSellRailWagon
CommandProc | CmdSendTrainToDepot
CommandProc | CmdForceTrainProceed
CommandProc | CmdReverseTrainDirection
CommandProc | CmdModifyOrder
CommandProc | CmdSkipToOrder
CommandProc | CmdDeleteOrder
CommandProc | CmdInsertOrder
CommandProc | CmdChangeServiceInt
CommandProc | CmdRestoreOrderIndex
CommandProc | CmdBuildIndustry
CommandProc | CmdBuildCompanyHQ
CommandProc | CmdSetCompanyManagerFace
CommandProc | CmdSetCompanyColour
CommandProc | CmdIncreaseLoan
CommandProc | CmdDecreaseLoan
CommandProc | CmdWantEnginePreview
CommandProc | CmdRenameVehicle
CommandProc | CmdRenameEngine
CommandProc | CmdRenameCompany
CommandProc | CmdRenamePresident
CommandProc | CmdRenameStation
CommandProc | CmdSellAircraft
CommandProc | CmdBuildAircraft
CommandProc | CmdSendAircraftToHangar
CommandProc | CmdRefitAircraft
CommandProc | CmdPlaceSign
CommandProc | CmdRenameSign
CommandProc | CmdBuildRoadVeh
CommandProc | CmdSellRoadVeh
CommandProc | CmdSendRoadVehToDepot
CommandProc | CmdTurnRoadVeh
CommandProc | CmdRefitRoadVeh
CommandProc | CmdPause
CommandProc | CmdBuyShareInCompany
CommandProc | CmdSellShareInCompany
CommandProc | CmdBuyCompany
CommandProc | CmdFoundTown
CommandProc | CmdRenameTown
CommandProc | CmdDoTownAction
CommandProc | CmdChangeSetting
CommandProc | CmdChangeCompanySetting
CommandProc | CmdSellShip
CommandProc | CmdBuildShip
CommandProc | CmdSendShipToDepot
CommandProc | CmdRefitShip
CommandProc | CmdOrderRefit
CommandProc | CmdCloneOrder
CommandProc | CmdClearArea
CommandProc | CmdGiveMoney
CommandProc | CmdMoneyCheat
CommandProc | CmdBuildCanal
CommandProc | CmdBuildLock
CommandProc | CmdCompanyCtrl
CommandProc | CmdLevelLand
CommandProc | CmdRefitRailVehicle
CommandProc | CmdBuildSignalTrack
CommandProc | CmdRemoveSignalTrack
CommandProc | CmdSetAutoReplace
CommandProc | CmdCloneVehicle
CommandProc | CmdStartStopVehicle
CommandProc | CmdMassStartStopVehicle
CommandProc | CmdAutoreplaceVehicle
CommandProc | CmdDepotSellAllVehicles
CommandProc | CmdDepotMassAutoReplace
CommandProc | CmdCreateGroup
CommandProc | CmdRenameGroup
CommandProc | CmdDeleteGroup
CommandProc | CmdAddVehicleGroup
CommandProc | CmdAddSharedVehicleGroup
CommandProc | CmdRemoveAllVehiclesGroup
CommandProc | CmdSetGroupReplaceProtection
CommandProc | CmdMoveOrder
CommandProc | CmdChangeTimetable
CommandProc | CmdSetVehicleOnTime
CommandProc | CmdAutofillTimetable
CommandProc | CmdSetTimetableStart
static const Command | _command_proc_table []
| The master command table.
static int | _docommand_recursive = 0

## Detailed Description

Handling of commands.

Definition in file command.cpp.

## Define Documentation

#define return_dcpi | ( | cmd,
|  | clear |  | ) | { _docommand_recursive = 0; ClearStorageChanges(clear); return cmd; }

Helper to deduplicate the code for returning.

Parameters:

  | cmd | the command cost to return.
  | clear | whether to keep the storage changes or not.

Definition at line 556 of file command.cpp.

Referenced by DoCommandPInternal().

## Function Documentation

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

Execute a command.

This function executes a given command with the parameters from the CommandProc parameter list. Depending on the flags parameter it execute or test a command.

Parameters:

  | tile | The tile to apply the command on (for the CommandProc)
  | p1 | Additional data for the command (for the CommandProc)
  | p2 | Additional data for the command (for the CommandProc)
  | flags | Flags for the command and how to execute the command
  | cmd | The command-id to execute (a value of the CMD_* enums)
  | text | The text to pass

See also:
  CommandProc

Returns:
  the cost

Definition at line 409 of file command.cpp.

References CMD_ERROR, CMD_ID_MASK, DC_ALL_TILES, DC_BANKRUPT, DC_EXEC, DC_QUERY_COST, error(), CommandCost::Failed(), INVALID_STRING_ID, IsValidTile(), MapSize(), Command::proc, and CommandCost::SetGlobalErrorMessage().

CommandCost DoCommand | ( | const CommandContainer * | container,
|  | DoCommandFlag | flags |
| ) |  |  |

Shorthand for calling the long DoCommand with a container.

Parameters:

  | container | Container with (almost) all information
  | flags | Flags for the command and how to execute the command

See also:
  CommandProc

Returns:
  the cost

Definition at line 391 of file command.cpp.

References CommandContainer::cmd, CMD_ID_MASK, DoCommand(), CommandContainer::p1, CommandContainer::p2, CommandContainer::text, and CommandContainer::tile.

Referenced by BuildReplacementVehicle(), CanBuildHouseHere(), CanBuildTramTrackOnTile(), TownViewWindow::CanDeleteTown(), CheckFlatLandBelow(), CheckIfCanLevelIndustryPlatform(), ClearMakeHouseTile(), CmdBuildBridge(), CmdBuildCanal(), CmdBuildDock(), CmdBuildLongRoad(), CmdBuildRailWagon(), CmdBuildRoad(), CmdBuildRoadDepot(), CmdBuildShipDepot(), CmdBuildSingleRail(), CmdBuildTrainDepot(), CmdBuildTunnel(), CmdClearArea(), CmdCloneVehicle(), CmdDepotMassAutoReplace(), CmdDepotSellAllVehicles(), CmdLevelLand(), CmdMassStartStopVehicle(), CmdPlantTree(), CmdPurchaseLandArea(), CmdRailTrackHelper(), CmdRemoveLongRoad(), CmdRemoveSingleRail(), CmdRenamePresident(), CmdSignalTrackHelper(), CopyHeadSpecificThings(), DoBuildShiplift(), DoCommand(), DoDryUp(), DoFloodTile(), RefitWindow::DrawWidget(), FloodHalftile(), GrowTown(), GrowTownWithBridge(), GrowTownWithRoad(), IsRoadAllowedHere(), MoveVehicle(), NormalizeTrainVehInDepot(), BuildRoadToolbarWindow::OnPlacePresize(), BuildRailToolbarWindow::OnPlacePresize(), RemoveRailStation(), RemoveRailWaypoint(), RemoveRoad(), ReplaceChain(), ReplaceFreeUnit(), ResetLandscapeConfirmationCallback(), SearchLumberMillTrees(), SearchTileForStatue(), SendAllVehiclesToDepot(), Vehicle::SendToDepot(), ShowBuildBridgeWindow(), ShowNewGrfVehicleError(), StartStopVehicle(), StationJoinerNeeded(), TownActionBuildStatue(), UpdateOrderDest(), and Town::~Town().

bool DoCommandP | ( | TileIndex | tile,
|  | uint32 | p1,
|  | uint32 | p2,
|  | uint32 | cmd,
|  | CommandCallback * | callback,
|  | const char * | text,
|  | bool | my_cmd |
| ) |  |  |

Execute a network safe DoCommand function.

Toplevel network safe docommand function for the current company. Must not be called recursively. The callback is called when the command succeeded or failed. The parameters tile, p1 and p2 are from the CommandProc function. The paramater cmd is the command to execute. The parameter my_cmd is used to indicate if the command is from a company or the server.

Parameters:

  | tile | The tile to perform a command on (see CommandProc)
  | p1 | Additional data for the command (see CommandProc)
  | p2 | Additional data for the command (see CommandProc)
  | cmd | The command to execute (a CMD_* value)
  | callback | A callback function to call after the command is finished
  | text | The text to pass
  | my_cmd | indicator if the command is from a company or server (to display error messages for a user)

Returns:
  true if the command succeeded, else false

Definition at line 503 of file command.cpp.

References _networking, _shift_pressed, CMD_ID_MASK, CMD_NETWORK_COMMAND, CMD_PAUSE, DoCommandPInternal(), CommandCost::Failed(), GB(), CommandCost::GetCost(), IsGeneratingWorld(), CommandCost::SetGlobalErrorMessage(), ShowErrorMessage(), CommandCost::Succeeded(), TILE_SIZE, TileX(), and TileY().

bool DoCommandP | ( | const CommandContainer * | container,
|  | bool | my_cmd |
| ) |  |  |

Shortcut for the long DoCommandP when having a container with the data.

Parameters:

  | container | the container with information.
  | my_cmd | indicator if the command is from a company or server (to display error messages for a user)

Returns:
  true if the command succeeded, else false

Definition at line 483 of file command.cpp.

References CommandContainer::callback, CommandContainer::cmd, DoCommandP(), CommandContainer::p1, CommandContainer::p2, CommandContainer::text, and CommandContainer::tile.

Referenced by AskUnsafeUnpauseCallback(), ChangeTimetableStartCallback(), CheckPauseHelper(), CheckTrainsLengths(), CommonRaiseLowerBigLand(), DepotWindow::DepotClick(), DoCommandP(), GenerateDesertArea(), GenericPlaceSignals(), GUIPlaceProcDragXY(), HandleAutoSignalPlacement(), DepotWindow::HandleCloneVehClick(), NetworkExecuteLocalCommandQueue(), VehicleViewWindow::OnClick(), VehicleDetailsWindow::OnClick(), VehicleListWindow::OnClick(), RefitWindow::OnClick(), TownAuthorityWindow::OnClick(), TimetableWindow::OnClick(), SelectStationWindow< T >::OnClick(), GameDifficultyWindow::OnClick(), OrdersWindow::OnClick(), BuildIndustryWindow::OnClick(), VehicleGroupWindow::OnClick(), EnginePreviewWindow::OnClick(), DepotWindow::OnClick(), BuyCompanyWindow::OnClick(), CompanyWindow::OnClick(), SelectCompanyManagerFaceWindow::OnClick(), SelectCompanyLiveryWindow::OnClick(), CompanyFinancesWindow::OnClick(), BuildVehicleWindow::OnClick(), ReplaceVehicleWindow::OnClick(), AIDebugWindow::OnClick(), OrdersWindow::OnDragDrop(), VehicleGroupWindow::OnDragDrop(), DepotWindow::OnDragDrop(), VehicleListWindow::OnDropdownSelect(), OrdersWindow::OnDropdownSelect(), VehicleGroupWindow::OnDropdownSelect(), SelectCompanyLiveryWindow::OnDropdownSelect(), MainWindow::OnKeyPress(), BuildTreesWindow::OnPlaceMouseUp(), BuildRoadToolbarWindow::OnPlaceMouseUp(), BuildRailToolbarWindow::OnPlaceMouseUp(), BuildDocksToolbarWindow::OnPlaceMouseUp(), OrdersWindow::OnPlaceObject(), BuildIndustryWindow::OnPlaceObject(), CompanyWindow::OnPlaceObject(), OrdersWindow::OnPlaceObjectAbort(), WaypointWindow::OnQueryTextFinished(), VehicleDetailsWindow::OnQueryTextFinished(), TownViewWindow::OnQueryTextFinished(), TimetableWindow::OnQueryTextFinished(), StationViewWindow::OnQueryTextFinished(), OrdersWindow::OnQueryTextFinished(), VehicleGroupWindow::OnQueryTextFinished(), CompanyWindow::OnQueryTextFinished(), BuildVehicleWindow::OnQueryTextFinished(), OrdersWindow::OrderClick_Delete(), OrdersWindow::OrderClick_FullLoad(), OrdersWindow::OrderClick_NearestDepot(), OrdersWindow::OrderClick_Nonstop(), OrdersWindow::OrderClick_Refit(), OrdersWindow::OrderClick_Service(), OrdersWindow::OrderClick_Skip(), OrdersWindow::OrderClick_Unload(), PlaceExtraDepotRail(), PlaceProc_Sign(), RenameSign(), RestoreVehicleOrders(), SetCompanySetting(), SetSettingValue(), ShowBuildBridgeWindow(), and ShowSelectBaseStationIfNeeded().

CommandCost DoCommandPInternal | ( | TileIndex | tile,
|  | uint32 | p1,
|  | uint32 | p2,
|  | uint32 | cmd,
|  | CommandCallback * | callback,
|  | const char * | text,
|  | bool | my_cmd,
|  | bool | estimate_only |
| ) |  |  |

Internal helper function for DoCommandP.

Helper function for the toplevel network safe docommand function for the current company.

Parameters:

  | tile | The tile to perform a command on (see CommandProc)
  | p1 | Additional data for the command (see CommandProc)
  | p2 | Additional data for the command (see CommandProc)
  | cmd | The command to execute (a CMD_* value)
  | callback | A callback function to call after the command is finished
  | text | The text to pass
  | my_cmd | indicator if the command is from a company or server (to display error messages for a user)
  | estimate_only | whether to give only the estimate or also execute the command

Returns:
  the command cost of this function.

Definition at line 571 of file command.cpp.

References _date, _networking, CMD_ALL_TILES, CMD_COMPANY_CTRL, CMD_ERROR, CMD_FLAGS_MASK, CMD_ID_MASK, CMD_NETWORK_COMMAND, CMD_NO_TEST, CMD_NO_TEST_IF_IN_NETWORK, CMD_SERVER, CMD_SPECTATOR, CommandFlagsToDCFlags(), COMPANY_SPECTATOR, DC_EXEC, CommandCost::Failed(), GetCommandFlags(), GetCommandName(), CommandCost::GetCost(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), INVALID_STRING_ID, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), IsValidTile(), lengthof, MapSize(), NetworkSend_Command(), Command::proc, return_dcpi, SetDParam(), and UpdateSignalsInBuffer().

Referenced by AIObject::DoCommand(), and DoCommandP().

Money GetAvailableMoneyForCommand | ( |  | ) |

Returns the current money available which can be used for a command.

This functions returns the money which can be used to execute a command. This is either the money of the current company or INT64_MAX if there is no such a company "at the moment" like the server itself.

Returns:
  The available money of a company or INT64_MAX

Definition at line 470 of file command.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID().

Referenced by CmdClearArea(), CmdFoundTown(), CmdLevelLand(), and CmdRemoveLongRoad().

byte GetCommandFlags | ( | uint32 | cmd | ) |

Returns the flags from a given command.

This function mask the parameter with CMD_ID_MASK and returns the flags which belongs to the given command.

Parameters:

  | cmd | The integer value of the command

Returns:
  The flags for this command

Definition at line 360 of file command.cpp.

References CMD_ID_MASK, Command::flags, and IsValidCommand().

Referenced by DEF_SERVER_RECEIVE_COMMAND(), DoCommandPInternal(), NetworkClientSocket::Recv_Command(), and StationJoinerNeeded().

const char* GetCommandName | ( | uint32 | cmd | ) |

Returns the name of a given command.

This function mask the parameter with CMD_ID_MASK and returns the name which belongs to the given command.

Parameters:

  | cmd | The integer value of the command

Returns:
  The name for this command

Definition at line 374 of file command.cpp.

References CMD_ID_MASK, IsValidCommand(), and Command::name.

Referenced by DoCommandPInternal().

bool IsValidCommand | ( | uint32 | cmd | ) |

Checks if a integer value belongs to a command.

This function range-checks a cmd, and checks if the cmd is not NULL

Parameters:

  | cmd | The integervalue of a command

Returns:
  true if the command is valid (and got a CommandProc function)

Definition at line 344 of file command.cpp.

References CMD_ID_MASK, lengthof, and Command::proc.

Referenced by GetCommandFlags(), GetCommandName(), and NetworkClientSocket::Recv_Command().

## Variable Documentation

const Command _command_proc_table[]`[static]`

The master command table.

This table contains all possible CommandProc functions with the flags which belongs to it. The indizes are the same as the value from the CMD_* enums.

Definition at line 200 of file command.cpp.