OpenTTD Source: CommandCost Class Reference

OpenTTD Source 15.0-beta1

Public Member Functions | Private Attributes | Static Private Attributes
CommandCost Class Reference

Common return value for all commands. More...

`#include <command_type.h>`

## Public Member Functions
| CommandCost ()
| Creates a command cost return with no cost and no error.
| CommandCost (StringID msg, StringID extra_msg=INVALID_STRING_ID)
| Creates a command return value with one, or optionally two, error message strings.
| CommandCost (ExpensesType ex_t)
| Creates a command cost with given expense type and start cost of 0.
| CommandCost (ExpensesType ex_t, const Money &cst)
| Creates a command return value with the given start cost and expense type.
void | AddCost (const Money &cost)
| Adds the given cost to the cost of the command.
void | AddCost (const CommandCost &cmd_cost)
| Adds the cost of the given command return value to this cost.
void | MultiplyCost (int factor)
| Multiplies the cost of the command by the given factor.
Money | GetCost () const
| The costs as made up to this moment.
ExpensesType | GetExpensesType () const
| The expense type of the cost.
void | MakeError (StringID message, StringID extra_message=INVALID_STRING_ID)
| Makes this CommandCost behave like an error command.
void | UseTextRefStack (const GRFFile *grffile, uint num_registers)
| Activate usage of the NewGRF TextRefStack for the error message.
const GRFFile * | GetTextRefStackGRF () const
| Returns the NewGRF providing the TextRefStack of the error message.
uint | GetTextRefStackSize () const
| Returns the number of uint32_t values for the TextRefStack of the error message.
const uint32_t * | GetTextRefStack () const
| Returns a pointer to the values for the TextRefStack of the error message.
StringID | GetErrorMessage () const
| Returns the error message of a command.
StringID | GetExtraErrorMessage () const
| Returns the extra error message of a command.
bool | Succeeded () const
| Did this command succeed?
bool | Failed () const
| Did this command fail?

## Private Attributes
Money | cost
| The cost of this action.
StringID | message
| Warning message for when success is unset.
ExpensesType | expense_type
| the type of expence as shown on the finances view
bool | success
| Whether the command went fine up to this moment.
const GRFFile * | textref_stack_grffile = nullptr
| NewGRF providing the TextRefStack content.
uint | textref_stack_size = 0
| Number of uint32_t values to put on the TextRefStack for the error message.
StringID | extra_message = INVALID_STRING_ID
| Additional warning message for when success is unset.

## Static Private Attributes
static uint32_t | textref_stack [16]
| Values to put on the TextRefStack for the error message.

## Detailed Description

Common return value for all commands.

Wraps the cost and a possible error message/state together.

Definition at line 23 of file command_type.h.

## Constructor & Destructor Documentation

## ◆ CommandCost() [1/4]

CommandCost::CommandCost | ( |  | ) |
| inline

Creates a command cost return with no cost and no error.

Definition at line 38 of file command_type.h.

## ◆ CommandCost() [2/4]

CommandCost::CommandCost | ( | StringID | msg,
|  | StringID | extra_msg = `INVALID_STRING_ID`
| ) |  |
| inlineexplicit

Creates a command return value with one, or optionally two, error message strings.

Definition at line 43 of file command_type.h.

## ◆ CommandCost() [3/4]

CommandCost::CommandCost | ( | ExpensesType | ex_t | ) |
| inlineexplicit

Creates a command cost with given expense type and start cost of 0.

Parameters

  ex_t | the expense type

Definition at line 49 of file command_type.h.

## ◆ CommandCost() [4/4]

CommandCost::CommandCost | ( | ExpensesType | ex_t,
|  | const Money & | cst
| ) |  |
| inline

Creates a command return value with the given start cost and expense type.

Parameters

  ex_t | the expense type
  cst | the initial cost of this command

Definition at line 56 of file command_type.h.

## Member Function Documentation

## ◆ AddCost() [1/2]

void CommandCost::AddCost | ( | const CommandCost & | ret | ) |

Adds the cost of the given command return value to this cost.

Also takes a possible error message when it is set.

Parameters

  ret | The command to add the cost of.

Definition at line 401 of file command.cpp.

References AddCost(), cost, message, and success.

## ◆ AddCost() [2/2]

void CommandCost::AddCost | ( | const Money & | cost | ) |
| inline

Adds the given cost to the cost of the command.

Parameters

  cost | the cost to add

Definition at line 63 of file command_type.h.

References cost.

Referenced by AddCost(), BuildReplacementVehicle(), CalculateRailStationCost(), CalculateRoadStopCost(), CheckBuildableTile(), CheckFlatLandAirport(), CheckFlatLandRailStation(), CheckFlatLandRoadStop(), ClearTile_Town(), CmdAutoreplaceVehicle(), CmdBuildAirport(), CmdBuildBridge(), CmdBuildBuoy(), CmdBuildCanal(), CmdBuildDock(), CmdBuildLongRoad(), CmdBuildObject(), CmdBuildObjectArea(), CmdBuildRailWaypoint(), CmdBuildRoad(), CmdBuildRoadDepot(), CmdBuildShipDepot(), CmdBuildSingleRail(), CmdBuildTrainDepot(), CmdBuildTunnel(), CmdBuildVehicle(), CmdClearArea(), CmdCloneVehicle(), CmdConvertRail(), CmdConvertRoad(), CmdDepotMassAutoReplace(), CmdDepotSellAllVehicles(), CmdLandscapeClear(), CmdLevelLand(), CmdPlantTree(), CmdRailTrackHelper(), CmdRemoveLongRoad(), CmdRemoveSingleRail(), CmdSellRailWagon(), CmdSignalTrackHelper(), CmdTerraformLand(), CompaniesGenStatistics(), CopyHeadSpecificThings(), DoBuildLock(), RefitVehicle(), RemoveAirport(), RemoveFromRailBaseStation(), RemoveGenericRoadStop(), RemoveRailStation(), RemoveRoad(), ReplaceChain(), ReplaceFreeUnit(), TerraformTileHeight(), and TestAutoslopeOnRailTile().

## ◆ Failed()

bool CommandCost::Failed | ( |  | ) | const
| inline

Did this command fail?

Returns
  true if and only if it failed

Definition at line 171 of file command_type.h.

References success.

Referenced by BuildReplacementVehicle(), BuildStationPart(), CalculateRailStationCost(), CalculateRoadStopCost(), CanRemoveRoadWithStop(), CcAddVehicleNewGroup(), CcBuildBridge(), CcBuildPrimaryVehicle(), CcBuildWagon(), CcCloneVehicle(), CcCreateGroup(), CcPlaceSign(), CcRoadStop(), CcStartStopVehicle(), CheckAllowRemoveRoad(), CheckBuildableTile(), CheckFlatLandAirport(), CheckFlatLandRailStation(), CheckFlatLandRoadStop(), CheckIfIndustryTilesAreFree(), CheckIfIndustryTileSlopes(), ClearTile_Station(), CmdAutofillTimetable(), CmdAutoreplaceVehicle(), CmdBuildAirport(), CmdBuildBridge(), CmdBuildBuoy(), CmdBuildCanal(), CmdBuildDock(), CmdBuildIndustry(), CmdBuildLongRoad(), CmdBuildObject(), CmdBuildObjectArea(), CmdBuildRailStation(), CmdBuildRailWaypoint(), CmdBuildRoad(), CmdBuildRoadDepot(), CmdBuildRoadStop(), CmdBuildRoadWaypoint(), CmdBuildShipDepot(), CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdBuildTrainDepot(), CmdBuildTunnel(), CmdBulkChangeTimetable(), CmdChangeServiceInt(), CmdChangeTimetable(), CmdClearArea(), CmdCloneOrder(), CmdCloneVehicle(), CmdConvertRail(), CmdConvertRoad(), CmdDeleteOrder(), CmdDeleteTown(), CmdDoTownAction(), CmdForceTrainProceed(), CmdFoundTown(), CmdInsertOrder(), CmdLevelLand(), CmdModifyOrder(), CmdMoveOrder(), CmdMoveRailVehicle(), CmdOpenCloseAirport(), CmdOrderRefit(), CmdPlantTree(), CmdRailTrackHelper(), CmdRefitVehicle(), CmdRemoveFromRailStation(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), CmdRenameDepot(), CmdRenameStation(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdReverseTrainDirection(), CmdSellRailWagon(), CmdSellVehicle(), CmdSetTimetableStart(), CmdSetVehicleOnTime(), CmdSignalTrackHelper(), CmdSkipToOrder(), CmdStartStopVehicle(), CmdTerraformLand(), CmdTurnRoadVeh(), CreateNewIndustry(), CreateNewIndustryHelper(), DoBuildLock(), DoClearBridge(), DoClearTunnel(), ScriptInstance::DoCommandCallback(), ExploreSegment(), FindJoiningBaseStation(), CommandHelperBase::InternalExecuteProcessResult(), CommandHelperBase::InternalExecuteValidateTestAndPrepExec(), CommandHelperBase::InternalPostResult(), IsRoadAllowedHere(), IsValidTileForWaypoint(), RemoveAirport(), RemoveBuoy(), RemoveDock(), RemoveFromRailBaseStation(), RemoveGenericRoadStop(), RemoveLock(), RemoveRailStation(), RemoveRoad(), RemoveRoadStop(), RemoveRoadWaypointStop(), ReplaceChain(), Vehicle::SendToDepot(), ShowBuildBridgeWindow(), ValidateTrains(), and VehicleEnterDepot().

## ◆ GetCost()

Money CommandCost::GetCost | ( |  | ) | const
| inline

The costs as made up to this moment.

Returns
  the costs

Definition at line 83 of file command_type.h.

References cost.

Referenced by CheckCompanyHasMoney(), CmdAutoreplaceVehicle(), CmdBuildBridge(), CmdBuildCanal(), CmdBuildObjectArea(), CmdBuildRoad(), CmdBuildTunnel(), CmdBuildVehicle(), CmdClearArea(), CmdFoundTown(), CmdGiveMoney(), CmdLevelLand(), CmdPlantTree(), CmdRemoveLongRoad(), ScriptInstance::DoCommandCallback(), HandleStationRefit(), CommandHelperBase::InternalExecuteProcessResult(), CommandHelperBase::InternalPostResult(), Aircraft::OnNewEconomyDay(), RoadVehicle::OnNewEconomyDay(), Ship::OnNewEconomyDay(), Train::OnNewEconomyDay(), ReplaceChain(), ShowBuildBridgeWindow(), SubtractMoneyFromAnyCompany(), SubtractMoneyFromCompanyFract(), and VehicleEnterDepot().

## ◆ GetErrorMessage()

StringID CommandCost::GetErrorMessage | ( |  | ) | const
| inline

Returns the error message of a command.

Returns
  the error message, if succeeded INVALID_STRING_ID

Definition at line 142 of file command_type.h.

References INVALID_STRING_ID, and message.

Referenced by CcBuildIndustry(), CmdBuildLongRoad(), CmdLevelLand(), CmdRailTrackHelper(), CmdRemoveLongRoad(), CmdSignalTrackHelper(), ScriptInstance::DoCommandCallback(), ReplaceChain(), ShowBuildBridgeWindow(), and ShowErrorMessage().

## ◆ GetExpensesType()

ExpensesType CommandCost::GetExpensesType | ( |  | ) | const
| inline

The expense type of the cost.

Returns
  the expense type

Definition at line 92 of file command_type.h.

References expense_type.

Referenced by SubtractMoneyFromAnyCompany(), and SubtractMoneyFromCompanyFract().

## ◆ GetExtraErrorMessage()

StringID CommandCost::GetExtraErrorMessage | ( |  | ) | const
| inline

Returns the extra error message of a command.

Returns
  the extra error message, if succeeded INVALID_STRING_ID

Definition at line 152 of file command_type.h.

References extra_message, and INVALID_STRING_ID.

Referenced by ShowErrorMessage().

## ◆ GetTextRefStack()

const uint32_t * CommandCost::GetTextRefStack | ( |  | ) | const
| inline

Returns a pointer to the values for the TextRefStack of the error message.

Returns
  uint32_t values for the TextRefStack

Definition at line 133 of file command_type.h.

References textref_stack.

Referenced by ShowErrorMessage().

## ◆ GetTextRefStackGRF()

const GRFFile * CommandCost::GetTextRefStackGRF | ( |  | ) | const
| inline

Returns the NewGRF providing the TextRefStack of the error message.

Returns
  the NewGRF.

Definition at line 115 of file command_type.h.

References textref_stack_grffile.

Referenced by ShowErrorMessage().

## ◆ GetTextRefStackSize()

uint CommandCost::GetTextRefStackSize | ( |  | ) | const
| inline

Returns the number of uint32_t values for the TextRefStack of the error message.

Returns
  number of uint32_t values.

Definition at line 124 of file command_type.h.

References textref_stack_size.

Referenced by ShowErrorMessage().

## ◆ MakeError()

void CommandCost::MakeError | ( | StringID | message,
|  | StringID | extra_message = `INVALID_STRING_ID`
| ) |  |
| inline

Makes this CommandCost behave like an error command.

Parameters

  message | The error message.

Definition at line 101 of file command_type.h.

References extra_message, INVALID_STRING_ID, and message.

Referenced by CheckCompanyHasMoney(), CmdConvertRail(), and CmdConvertRoad().

## ◆ MultiplyCost()

void CommandCost::MultiplyCost | ( | int | factor | ) |
| inline

Multiplies the cost of the command by the given factor.

Parameters

  factor | factor to multiply the costs with

Definition at line 74 of file command_type.h.

Referenced by CmdFoundTown().

## ◆ Succeeded()

bool CommandCost::Succeeded | ( |  | ) | const
| inline

Did this command succeed?

Returns
  true if and only if it succeeded

Definition at line 162 of file command_type.h.

References success.

Referenced by AddNearbyStation(), BuildReplacementVehicle(), CanBuildTramTrackOnTile(), CcBuildIndustry(), CcBuildRailTunnel(), CcBuildRoadTunnel(), CheckAllowRemoveTunnelBridge(), CheckClearTile(), ClearMakeHouseTile(), CmdAutoreplaceVehicle(), CmdBuildIndustry(), CmdBuildSingleRail(), CmdBuildVehicle(), CmdCloneVehicle(), CmdDepotMassAutoReplace(), CmdDepotSellAllVehicles(), CmdLevelLand(), CmdRemoveLongRoad(), CmdSignalTrackHelper(), CmdTerraformLand(), CopyHeadSpecificThings(), CreateRandomTown(), DoBuildLock(), ScriptInstance::DoCommandCallback(), HandleStationRefit(), CommandHelperBase::InternalDoAfter(), IsValidTileForWaypoint(), RemoveDock(), RemoveLock(), ReplaceChain(), ReplaceFreeUnit(), SendAllVehiclesToDepot(), and ShowBuildBridgeWindow().

## ◆ UseTextRefStack()

void CommandCost::UseTextRefStack | ( | const GRFFile * | grffile,
|  | uint | num_registers
| ) |  |

Activate usage of the NewGRF TextRefStack for the error message.

Parameters

  grffile | NewGRF that provides the TextRefStack
  num_registers | number of entries to copy from the temporary NewGRF registers

Definition at line 422 of file command.cpp.

References lengthof, textref_stack, textref_stack_grffile, and textref_stack_size.

Referenced by GetErrorMessageFromLocationCallbackResult().

## Field Documentation

## ◆ cost

Money CommandCost::cost
| private

The cost of this action.

Definition at line 24 of file command_type.h.

Referenced by AddCost(), AddCost(), and GetCost().

## ◆ expense_type

ExpensesType CommandCost::expense_type
| private

the type of expence as shown on the finances view

Definition at line 26 of file command_type.h.

Referenced by GetExpensesType().

## ◆ extra_message

StringID CommandCost::extra_message = INVALID_STRING_ID
| private

Additional warning message for when success is unset.

Definition at line 30 of file command_type.h.

Referenced by GetExtraErrorMessage(), and MakeError().

## ◆ message

StringID CommandCost::message
| private

Warning message for when success is unset.

Definition at line 25 of file command_type.h.

Referenced by AddCost(), GetErrorMessage(), and MakeError().

## ◆ success

bool CommandCost::success
| private

Whether the command went fine up to this moment.

Definition at line 27 of file command_type.h.

Referenced by AddCost(), Failed(), and Succeeded().

## ◆ textref_stack

uint32_t CommandCost::textref_stack
| staticprivate

Values to put on the TextRefStack for the error message.

There is only one static instance of the array, just like there is only one instance of normal DParams.

Definition at line 32 of file command_type.h.

Referenced by GetTextRefStack(), and UseTextRefStack().

## ◆ textref_stack_grffile

const GRFFile* CommandCost::textref_stack_grffile = nullptr
| private

NewGRF providing the TextRefStack content.

Definition at line 28 of file command_type.h.

Referenced by GetTextRefStackGRF(), and UseTextRefStack().

## ◆ textref_stack_size

uint CommandCost::textref_stack_size = 0
| private

Number of uint32_t values to put on the TextRefStack for the error message.

Definition at line 29 of file command_type.h.

Referenced by GetTextRefStackSize(), and UseTextRefStack().

The documentation for this class was generated from the following files:

- src/command_type.h
- src/command.cpp