OpenTTD Source 20260206-master-g4d4e37dbf1
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 SetErrorOwner (Owner owner)
 Set the 'owner' (the originator) of this error message.
void SetEncodedMessage (EncodedString &&message)
 Set the encoded message string.
EncodedStringGetEncodedMessage ()
 Get the last encoded error message.
CompanyID GetErrorOwner () const
 Get the originator owner for this error.
void AddCost (const Money &cost)
 Adds the given cost to the cost of the command.
void AddCost (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)
 Makes this CommandCost behave like an error command.
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 expense as shown on the finances view
bool success
 Whether the command went fine up to this moment.
Owner owner = CompanyID::Invalid()
 Originator owner of error.
StringID extra_message = INVALID_STRING_ID
 Additional warning message for when success is unset.
EncodedString encoded_message {}
 Encoded error message, used if the error message includes parameters.

Detailed Description

Common return value for all commands.

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

Definition at line 24 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 37 of file command_type.h.

References cost, expense_type, INVALID_EXPENSES, INVALID_STRING_ID, message, and success.

Referenced by AddCost().

◆ 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.

Parameters
msgThe error message.
extra_msgOptional secondary error message.

Definition at line 44 of file command_type.h.

References cost, expense_type, extra_message, INVALID_EXPENSES, INVALID_STRING_ID, message, and success.

◆ CommandCost() [3/4]

CommandCost::CommandCost ( ExpensesType ex_t)
inlineexplicit

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

Parameters
ex_tthe expense type

Definition at line 50 of file command_type.h.

References cost, expense_type, INVALID_STRING_ID, message, and success.

◆ 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_tthe expense type
cstthe initial cost of this command

Definition at line 57 of file command_type.h.

References cost, expense_type, INVALID_STRING_ID, message, and success.

Member Function Documentation

◆ AddCost() [1/2]

void CommandCost::AddCost ( 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
retThe command to add the cost of.

Definition at line 400 of file command.cpp.

References AddCost(), CommandCost(), encoded_message, message, and success.

◆ AddCost() [2/2]

◆ Failed()

bool CommandCost::Failed ( ) const
inline

Did this command fail?

Returns
true if and only if it failed

Definition at line 181 of file command_type.h.

Referenced by BuildReplacementVehicle(), BuildStationPart(), CalculateRailStationCost(), CalculateRoadStopCost(), CanRemoveRoadWithStop(), CcAddVehicleNewGroup(), CcBuildBridge(), CcBuildPrimaryVehicle(), CcBuildWagon(), CcCloneVehicle(), CcCreateGroup(), CcMoveStationName(), CcMoveWaypointName(), 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(), CmdMoveStationName(), CmdMoveWaypointName(), CmdOpenCloseAirport(), CmdOrderRefit(), CmdPlaceHouseArea(), 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(), 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()

◆ GetEncodedMessage()

EncodedString & CommandCost::GetEncodedMessage ( )
inline

Get the last encoded error message.

Returns
Reference to the encoded message.

Definition at line 84 of file command_type.h.

Referenced by ShowErrorMessage().

◆ 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 152 of file command_type.h.

References INVALID_STRING_ID.

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

◆ GetErrorOwner()

CompanyID CommandCost::GetErrorOwner ( ) const
inline

Get the originator owner for this error.

Returns
The owner.

Definition at line 93 of file command_type.h.

Referenced by ShowErrorMessage().

◆ GetExpensesType()

ExpensesType CommandCost::GetExpensesType ( ) const
inline

The expense type of the cost.

Returns
the expense type

Definition at line 131 of file command_type.h.

Referenced by SubtractMoneyFromCompany(), 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 162 of file command_type.h.

References INVALID_STRING_ID.

Referenced by ShowErrorMessage().

◆ MakeError()

void CommandCost::MakeError ( StringID message)
inline

Makes this CommandCost behave like an error command.

Parameters
messageThe error message.

Definition at line 140 of file command_type.h.

References 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
factorfactor to multiply the costs with

Definition at line 113 of file command_type.h.

Referenced by CmdFoundTown().

◆ SetEncodedMessage()

void CommandCost::SetEncodedMessage ( EncodedString && message)
inline

Set the encoded message string.

If set, this is used by the error message window instead of the error StringID, to allow more information to be displayed to the local player.

Note
Do not set an encoded message if the error is not for the local player, as it will never be seen.
Parameters
messageEncodedString message to set.

Definition at line 75 of file command_type.h.

References message.

Referenced by CheckCompanyHasMoney(), CheckOwnership(), CmdConvertRoad(), CommandCostWithParam(), and GetErrorMessageFromLocationCallbackResult().

◆ SetErrorOwner()

void CommandCost::SetErrorOwner ( Owner owner)
inline

Set the 'owner' (the originator) of this error message.

This is used to show a company owner's face if you attempt an action on something owned by other company.

Parameters
ownerThe owner.

Definition at line 64 of file command_type.h.

References owner.

Referenced by CheckOwnership().

◆ Succeeded()

Field Documentation

◆ cost

Money CommandCost::cost
private

The cost of this action.

Definition at line 25 of file command_type.h.

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

◆ encoded_message

EncodedString CommandCost::encoded_message {}
private

Encoded error message, used if the error message includes parameters.

Definition at line 31 of file command_type.h.

Referenced by AddCost().

◆ expense_type

ExpensesType CommandCost::expense_type
private

the type of expense as shown on the finances view

Definition at line 27 of file command_type.h.

Referenced by CommandCost(), CommandCost(), CommandCost(), and CommandCost().

◆ 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 CommandCost().

◆ message

StringID CommandCost::message
private

Warning message for when success is unset.

Definition at line 26 of file command_type.h.

Referenced by AddCost(), CommandCost(), CommandCost(), CommandCost(), CommandCost(), MakeError(), and SetEncodedMessage().

◆ owner

Owner CommandCost::owner = CompanyID::Invalid()
private

Originator owner of error.

Definition at line 29 of file command_type.h.

Referenced by SetErrorOwner().

◆ success

bool CommandCost::success
private

Whether the command went fine up to this moment.

Definition at line 28 of file command_type.h.

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


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