2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file command_type.h Types related to commands. */
10 #ifndef COMMAND_TYPE_H
11 #define COMMAND_TYPE_H
13 #include "economy_type.h"
14 #include "strings_type.h"
15 #include "tile_type.h"
20 * Common return value for all commands. Wraps the cost and
21 * a possible error message/state together.
24 ExpensesType expense_type
; ///< the type of expence as shown on the finances view
25 Money cost
; ///< The cost of this action
26 StringID message
; ///< Warning message for when success is unset
27 bool success
; ///< Whether the command went fine up to this moment
28 const GRFFile
*textref_stack_grffile
; ///< NewGRF providing the #TextRefStack content.
29 uint textref_stack_size
; ///< Number of uint32_t values to put on the #TextRefStack for the error message.
30 StringID extra_message
= INVALID_STRING_ID
; ///< Additional warning message for when success is unset
32 static uint32_t textref_stack
[16];
36 * Creates a command cost return with no cost and no error
38 CommandCost() : expense_type(INVALID_EXPENSES
), cost(0), message(INVALID_STRING_ID
), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
41 * Creates a command return value with one, or optionally two, error message strings.
43 explicit CommandCost(StringID msg
, StringID extra_msg
= INVALID_STRING_ID
) : expense_type(INVALID_EXPENSES
), cost(0), message(msg
), success(false), textref_stack_grffile(nullptr), textref_stack_size(0), extra_message(extra_msg
) {}
46 * Creates a command cost with given expense type and start cost of 0
47 * @param ex_t the expense type
49 explicit CommandCost(ExpensesType ex_t
) : expense_type(ex_t
), cost(0), message(INVALID_STRING_ID
), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
52 * Creates a command return value with the given start cost and expense type
53 * @param ex_t the expense type
54 * @param cst the initial cost of this command
56 CommandCost(ExpensesType ex_t
, const Money
&cst
) : expense_type(ex_t
), cost(cst
), message(INVALID_STRING_ID
), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
60 * Adds the given cost to the cost of the command.
61 * @param cost the cost to add
63 inline void AddCost(const Money
&cost
)
68 void AddCost(const CommandCost
&cmd_cost
);
71 * Multiplies the cost of the command by the given factor.
72 * @param factor factor to multiply the costs with
74 inline void MultiplyCost(int factor
)
80 * The costs as made up to this moment
83 inline Money
GetCost() const
89 * The expense type of the cost
90 * @return the expense type
92 inline ExpensesType
GetExpensesType() const
94 return this->expense_type
;
98 * Makes this #CommandCost behave like an error command.
99 * @param message The error message.
101 void MakeError(StringID message
, StringID extra_message
= INVALID_STRING_ID
)
103 assert(message
!= INVALID_STRING_ID
);
104 this->success
= false;
105 this->message
= message
;
106 this->extra_message
= extra_message
;
109 void UseTextRefStack(const GRFFile
*grffile
, uint num_registers
);
112 * Returns the NewGRF providing the #TextRefStack of the error message.
113 * @return the NewGRF.
115 const GRFFile
*GetTextRefStackGRF() const
117 return this->textref_stack_grffile
;
121 * Returns the number of uint32_t values for the #TextRefStack of the error message.
122 * @return number of uint32_t values.
124 uint
GetTextRefStackSize() const
126 return this->textref_stack_size
;
130 * Returns a pointer to the values for the #TextRefStack of the error message.
131 * @return uint32_t values for the #TextRefStack
133 const uint32_t *GetTextRefStack() const
135 return textref_stack
;
139 * Returns the error message of a command
140 * @return the error message, if succeeded #INVALID_STRING_ID
142 StringID
GetErrorMessage() const
144 if (this->success
) return INVALID_STRING_ID
;
145 return this->message
;
149 * Returns the extra error message of a command
150 * @return the extra error message, if succeeded #INVALID_STRING_ID
152 StringID
GetExtraErrorMessage() const
154 if (this->success
) return INVALID_STRING_ID
;
155 return this->extra_message
;
159 * Did this command succeed?
160 * @return true if and only if it succeeded
162 inline bool Succeeded() const
164 return this->success
;
168 * Did this command fail?
169 * @return true if and only if it failed
171 inline bool Failed() const
173 return !this->success
;
180 * This enum defines all possible commands which can be executed to the game
181 * engine. Observing the game like the query-tool or checking the profit of a
182 * vehicle don't result in a command which should be executed in the engine
183 * nor send to the server in a network game.
185 * @see _command_proc_table
187 enum Commands
: uint16_t {
188 CMD_BUILD_RAILROAD_TRACK
, ///< build a rail track
189 CMD_REMOVE_RAILROAD_TRACK
, ///< remove a rail track
190 CMD_BUILD_SINGLE_RAIL
, ///< build a single rail track
191 CMD_REMOVE_SINGLE_RAIL
, ///< remove a single rail track
192 CMD_LANDSCAPE_CLEAR
, ///< demolish a tile
193 CMD_BUILD_BRIDGE
, ///< build a bridge
194 CMD_BUILD_RAIL_STATION
, ///< build a rail station
195 CMD_BUILD_TRAIN_DEPOT
, ///< build a train depot
196 CMD_BUILD_SINGLE_SIGNAL
, ///< build a signal
197 CMD_REMOVE_SINGLE_SIGNAL
, ///< remove a signal
198 CMD_TERRAFORM_LAND
, ///< terraform a tile
199 CMD_BUILD_OBJECT
, ///< build an object
200 CMD_BUILD_OBJECT_AREA
, ///< build an area of objects
201 CMD_BUILD_TUNNEL
, ///< build a tunnel
203 CMD_REMOVE_FROM_RAIL_STATION
, ///< remove a (rectangle of) tiles from a rail station
204 CMD_CONVERT_RAIL
, ///< convert a rail type
206 CMD_BUILD_RAIL_WAYPOINT
, ///< build a waypoint
207 CMD_RENAME_WAYPOINT
, ///< rename a waypoint
208 CMD_REMOVE_FROM_RAIL_WAYPOINT
, ///< remove a (rectangle of) tiles from a rail waypoint
210 CMD_BUILD_ROAD_WAYPOINT
, ///< build a road waypoint
211 CMD_REMOVE_FROM_ROAD_WAYPOINT
, ///< remove a (rectangle of) tiles from a road waypoint
213 CMD_BUILD_ROAD_STOP
, ///< build a road stop
214 CMD_REMOVE_ROAD_STOP
, ///< remove a road stop
215 CMD_BUILD_LONG_ROAD
, ///< build a complete road (not a "half" one)
216 CMD_REMOVE_LONG_ROAD
, ///< remove a complete road (not a "half" one)
217 CMD_BUILD_ROAD
, ///< build a "half" road
218 CMD_BUILD_ROAD_DEPOT
, ///< build a road depot
219 CMD_CONVERT_ROAD
, ///< convert a road type
221 CMD_BUILD_AIRPORT
, ///< build an airport
223 CMD_BUILD_DOCK
, ///< build a dock
225 CMD_BUILD_SHIP_DEPOT
, ///< build a ship depot
226 CMD_BUILD_BUOY
, ///< build a buoy
228 CMD_PLANT_TREE
, ///< plant a tree
230 CMD_BUILD_VEHICLE
, ///< build a vehicle
231 CMD_SELL_VEHICLE
, ///< sell a vehicle
232 CMD_REFIT_VEHICLE
, ///< refit the cargo space of a vehicle
233 CMD_SEND_VEHICLE_TO_DEPOT
, ///< send a vehicle to a depot
234 CMD_SET_VEHICLE_VISIBILITY
, ///< hide or unhide a vehicle in the build vehicle and autoreplace GUIs
236 CMD_MOVE_RAIL_VEHICLE
, ///< move a rail vehicle (in the depot)
237 CMD_FORCE_TRAIN_PROCEED
, ///< proceed a train to pass a red signal
238 CMD_REVERSE_TRAIN_DIRECTION
, ///< turn a train around
240 CMD_CLEAR_ORDER_BACKUP
, ///< clear the order backup of a given user/tile
241 CMD_MODIFY_ORDER
, ///< modify an order (like set full-load)
242 CMD_SKIP_TO_ORDER
, ///< skip an order to the next of specific one
243 CMD_DELETE_ORDER
, ///< delete an order
244 CMD_INSERT_ORDER
, ///< insert a new order
246 CMD_CHANGE_SERVICE_INT
, ///< change the server interval of a vehicle
248 CMD_BUILD_INDUSTRY
, ///< build a new industry
249 CMD_INDUSTRY_SET_FLAGS
, ///< change industry control flags
250 CMD_INDUSTRY_SET_EXCLUSIVITY
, ///< change industry exclusive consumer/supplier
251 CMD_INDUSTRY_SET_TEXT
, ///< change additional text for the industry
252 CMD_INDUSTRY_SET_PRODUCTION
, ///< change industry production
254 CMD_SET_COMPANY_MANAGER_FACE
, ///< set the manager's face of the company
255 CMD_SET_COMPANY_COLOUR
, ///< set the colour of the company
257 CMD_INCREASE_LOAN
, ///< increase the loan from the bank
258 CMD_DECREASE_LOAN
, ///< decrease the loan from the bank
259 CMD_SET_COMPANY_MAX_LOAN
, ///< sets the max loan for the company
261 CMD_WANT_ENGINE_PREVIEW
, ///< confirm the preview of an engine
262 CMD_ENGINE_CTRL
, ///< control availability of the engine for companies
264 CMD_RENAME_VEHICLE
, ///< rename a whole vehicle
265 CMD_RENAME_ENGINE
, ///< rename a engine (in the engine list)
266 CMD_RENAME_COMPANY
, ///< change the company name
267 CMD_RENAME_PRESIDENT
, ///< change the president name
268 CMD_RENAME_STATION
, ///< rename a station
269 CMD_RENAME_DEPOT
, ///< rename a depot
271 CMD_PLACE_SIGN
, ///< place a sign
272 CMD_RENAME_SIGN
, ///< rename a sign
274 CMD_TURN_ROADVEH
, ///< turn a road vehicle around
276 CMD_PAUSE
, ///< pause the game
278 CMD_BUY_COMPANY
, ///< buy a company which is bankrupt
280 CMD_FOUND_TOWN
, ///< found a town
281 CMD_RENAME_TOWN
, ///< rename a town
282 CMD_DO_TOWN_ACTION
, ///< do a action from the town detail window (like advertises or bribe)
283 CMD_TOWN_CARGO_GOAL
, ///< set the goal of a cargo for a town
284 CMD_TOWN_GROWTH_RATE
, ///< set the town growth rate
285 CMD_TOWN_RATING
, ///< set rating of a company in a town
286 CMD_TOWN_SET_TEXT
, ///< set the custom text of a town
287 CMD_EXPAND_TOWN
, ///< expand a town
288 CMD_DELETE_TOWN
, ///< delete a town
289 CMD_PLACE_HOUSE
, ///< place a house
291 CMD_ORDER_REFIT
, ///< change the refit information of an order (for "goto depot" )
292 CMD_CLONE_ORDER
, ///< clone (and share) an order
293 CMD_CLEAR_AREA
, ///< clear an area
295 CMD_MONEY_CHEAT
, ///< do the money cheat
296 CMD_CHANGE_BANK_BALANCE
, ///< change bank balance to charge costs or give money from a GS
297 CMD_BUILD_CANAL
, ///< build a canal
299 CMD_CREATE_SUBSIDY
, ///< create a new subsidy
300 CMD_COMPANY_CTRL
, ///< used in multiplayer to create a new companies etc.
301 CMD_COMPANY_ALLOW_LIST_CTRL
, ///< Used in multiplayer to add/remove a client's public key to/from the company's allow list.
302 CMD_CUSTOM_NEWS_ITEM
, ///< create a custom news message
303 CMD_CREATE_GOAL
, ///< create a new goal
304 CMD_REMOVE_GOAL
, ///< remove a goal
305 CMD_SET_GOAL_DESTINATION
, ///< update goal destination of a goal
306 CMD_SET_GOAL_TEXT
, ///< update goal text of a goal
307 CMD_SET_GOAL_PROGRESS
, ///< update goal progress text of a goal
308 CMD_SET_GOAL_COMPLETED
, ///< update goal completed status of a goal
309 CMD_GOAL_QUESTION
, ///< ask a goal related question
310 CMD_GOAL_QUESTION_ANSWER
, ///< answer(s) to CMD_GOAL_QUESTION
311 CMD_CREATE_STORY_PAGE
, ///< create a new story page
312 CMD_CREATE_STORY_PAGE_ELEMENT
, ///< create a new story page element
313 CMD_UPDATE_STORY_PAGE_ELEMENT
, ///< update a story page element
314 CMD_SET_STORY_PAGE_TITLE
, ///< update title of a story page
315 CMD_SET_STORY_PAGE_DATE
, ///< update date of a story page
316 CMD_SHOW_STORY_PAGE
, ///< show a story page
317 CMD_REMOVE_STORY_PAGE
, ///< remove a story page
318 CMD_REMOVE_STORY_PAGE_ELEMENT
, ///< remove a story page element
319 CMD_SCROLL_VIEWPORT
, ///< scroll main viewport of players
320 CMD_STORY_PAGE_BUTTON
, ///< selection via story page button
322 CMD_LEVEL_LAND
, ///< level land
324 CMD_BUILD_LOCK
, ///< build a lock
326 CMD_BUILD_SIGNAL_TRACK
, ///< add signals along a track (by dragging)
327 CMD_REMOVE_SIGNAL_TRACK
, ///< remove signals along a track (by dragging)
329 CMD_GIVE_MONEY
, ///< give money to another company
330 CMD_CHANGE_SETTING
, ///< change a setting
331 CMD_CHANGE_COMPANY_SETTING
, ///< change a company setting
333 CMD_SET_AUTOREPLACE
, ///< set an autoreplace entry
335 CMD_CLONE_VEHICLE
, ///< clone a vehicle
336 CMD_START_STOP_VEHICLE
, ///< start or stop a vehicle
337 CMD_MASS_START_STOP
, ///< start/stop all vehicles (in a depot)
338 CMD_AUTOREPLACE_VEHICLE
, ///< replace/renew a vehicle while it is in a depot
339 CMD_DEPOT_SELL_ALL_VEHICLES
, ///< sell all vehicles which are in a given depot
340 CMD_DEPOT_MASS_AUTOREPLACE
, ///< force the autoreplace to take action in a given depot
342 CMD_CREATE_GROUP
, ///< create a new group
343 CMD_DELETE_GROUP
, ///< delete a group
344 CMD_ALTER_GROUP
, ///< alter a group
345 CMD_ADD_VEHICLE_GROUP
, ///< add a vehicle to a group
346 CMD_ADD_SHARED_VEHICLE_GROUP
, ///< add all other shared vehicles to a group which are missing
347 CMD_REMOVE_ALL_VEHICLES_GROUP
, ///< remove all vehicles from a group
348 CMD_SET_GROUP_FLAG
, ///< set/clear a flag for a group
349 CMD_SET_GROUP_LIVERY
, ///< set the livery for a group
351 CMD_MOVE_ORDER
, ///< move an order
352 CMD_CHANGE_TIMETABLE
, ///< change the timetable for a vehicle
353 CMD_BULK_CHANGE_TIMETABLE
, ///< change the timetable for all orders of a vehicle
354 CMD_SET_VEHICLE_ON_TIME
, ///< set the vehicle on time feature (timetable)
355 CMD_AUTOFILL_TIMETABLE
, ///< autofill the timetable
356 CMD_SET_TIMETABLE_START
, ///< set the date that a timetable should start
358 CMD_OPEN_CLOSE_AIRPORT
, ///< open/close an airport to incoming aircraft
360 CMD_CREATE_LEAGUE_TABLE
, ///< create a new league table
361 CMD_CREATE_LEAGUE_TABLE_ELEMENT
, ///< create a new element in a league table
362 CMD_UPDATE_LEAGUE_TABLE_ELEMENT_DATA
, ///< update the data fields of a league table element
363 CMD_UPDATE_LEAGUE_TABLE_ELEMENT_SCORE
, ///< update the score of a league table element
364 CMD_REMOVE_LEAGUE_TABLE_ELEMENT
, ///< remove a league table element
366 CMD_END
, ///< Must ALWAYS be on the end of this list!! (period)
370 * List of flags for a command.
372 * This enums defines some flags which can be used for the commands.
375 DC_NONE
= 0x000, ///< no flag is set
376 DC_EXEC
= 0x001, ///< execute the given command
377 DC_AUTO
= 0x002, ///< don't allow building on structures
378 DC_QUERY_COST
= 0x004, ///< query cost only, don't build.
379 DC_NO_WATER
= 0x008, ///< don't allow building on water
381 DC_NO_TEST_TOWN_RATING
= 0x020, ///< town rating does not disallow you from building
382 DC_BANKRUPT
= 0x040, ///< company bankrupts, skip money check, skip vehicle on tile check in some cases
383 DC_AUTOREPLACE
= 0x080, ///< autoreplace/autorenew is in progress, this shall disable vehicle limits when building, and ignore certain restrictions when undoing things (like vehicle attach callback)
384 DC_NO_CARGO_CAP_CHECK
= 0x100, ///< when autoreplace/autorenew is in progress, this shall prevent truncating the amount of cargo in the vehicle to prevent testing the command to remove cargo
385 DC_ALL_TILES
= 0x200, ///< allow this command also on MP_VOID tiles
386 DC_NO_MODIFY_TOWN_RATING
= 0x400, ///< do not change town rating
387 DC_FORCE_CLEAR_TILE
= 0x800, ///< do not only remove the object on the tile, but also clear any water left on it
389 DECLARE_ENUM_AS_BIT_SET(DoCommandFlag
)
392 * Command flags for the command table _command_proc_table.
394 * This enumeration defines flags for the _command_proc_table.
397 CMD_SERVER
= 0x001, ///< the command can only be initiated by the server
398 CMD_SPECTATOR
= 0x002, ///< the command may be initiated by a spectator
399 CMD_OFFLINE
= 0x004, ///< the command cannot be executed in a multiplayer game; single-player only
400 CMD_AUTO
= 0x008, ///< set the DC_AUTO flag on this command
401 CMD_ALL_TILES
= 0x010, ///< allow this command also on MP_VOID tiles
402 CMD_NO_TEST
= 0x020, ///< the command's output may differ between test and execute due to town rating changes etc.
403 CMD_NO_WATER
= 0x040, ///< set the DC_NO_WATER flag on this command
404 CMD_CLIENT_ID
= 0x080, ///< set p2 with the ClientID of the sending client.
405 CMD_DEITY
= 0x100, ///< the command may be executed by COMPANY_DEITY
406 CMD_STR_CTRL
= 0x200, ///< the command's string may contain control strings
407 CMD_NO_EST
= 0x400, ///< the command is never estimated.
408 CMD_LOCATION
= 0x800, ///< the command has implicit location argument.
410 DECLARE_ENUM_AS_BIT_SET(CommandFlags
)
412 /** Types of commands we have. */
414 CMDT_LANDSCAPE_CONSTRUCTION
, ///< Construction and destruction of objects on the map.
415 CMDT_VEHICLE_CONSTRUCTION
, ///< Construction, modification (incl. refit) and destruction of vehicles.
416 CMDT_MONEY_MANAGEMENT
, ///< Management of money, i.e. loans.
417 CMDT_VEHICLE_MANAGEMENT
, ///< Stopping, starting, sending to depot, turning around, replace orders etc.
418 CMDT_ROUTE_MANAGEMENT
, ///< Modifications to route management (orders, groups, etc).
419 CMDT_OTHER_MANAGEMENT
, ///< Renaming stuff, changing company colours, placing signs, etc.
420 CMDT_COMPANY_SETTING
, ///< Changing settings related to a company.
421 CMDT_SERVER_SETTING
, ///< Pausing/removing companies/server settings.
422 CMDT_CHEAT
, ///< A cheat of some sorts.
424 CMDT_END
, ///< Magic end marker.
427 /** Different command pause levels. */
428 enum CommandPauseLevel
{
429 CMDPL_NO_ACTIONS
, ///< No user actions may be executed.
430 CMDPL_NO_CONSTRUCTION
, ///< No construction actions may be executed.
431 CMDPL_NO_LANDSCAPING
, ///< No landscaping actions may be executed.
432 CMDPL_ALL_ACTIONS
, ///< All actions may be executed.
436 template <typename T
> struct CommandFunctionTraitHelper
;
437 template <typename
... Targs
>
438 struct CommandFunctionTraitHelper
<CommandCost(*)(DoCommandFlag
, Targs
...)> {
439 using Args
= std::tuple
<std::decay_t
<Targs
>...>;
440 using RetTypes
= void;
442 using CbProcType
= void(*)(Commands
, const CommandCost
&);
444 template <template <typename
...> typename Tret
, typename
... Tretargs
, typename
... Targs
>
445 struct CommandFunctionTraitHelper
<Tret
<CommandCost
, Tretargs
...>(*)(DoCommandFlag
, Targs
...)> {
446 using Args
= std::tuple
<std::decay_t
<Targs
>...>;
447 using RetTypes
= std::tuple
<std::decay_t
<Tretargs
>...>;
448 using CbArgs
= std::tuple
<std::decay_t
<Tretargs
>..., std::decay_t
<Targs
>...>;
449 using CbProcType
= void(*)(Commands
, const CommandCost
&, Tretargs
...);
452 /** Defines the traits of a command. */
453 template <Commands Tcmd
> struct CommandTraits
;
455 #define DEF_CMD_TRAIT(cmd_, proc_, flags_, type_) \
456 template<> struct CommandTraits<cmd_> { \
457 using ProcType = decltype(&proc_); \
458 using Args = typename CommandFunctionTraitHelper<ProcType>::Args; \
459 using RetTypes = typename CommandFunctionTraitHelper<ProcType>::RetTypes; \
460 using CbArgs = typename CommandFunctionTraitHelper<ProcType>::CbArgs; \
461 using RetCallbackProc = typename CommandFunctionTraitHelper<ProcType>::CbProcType; \
462 static constexpr Commands cmd = cmd_; \
463 static constexpr auto &proc = proc_; \
464 static constexpr CommandFlags flags = (CommandFlags)(flags_); \
465 static constexpr CommandType type = type_; \
466 static inline constexpr const char *name = #proc_; \
469 /** Storage buffer for serialized command data. */
470 typedef std::vector
<uint8_t> CommandDataBuffer
;
473 * Define a callback function for the client, after the command is finished.
475 * Functions of this type are called after the command is finished. The parameters
476 * are from the #CommandProc callback type. The boolean parameter indicates if the
477 * command succeeded or failed.
479 * @param cmd The command that was executed
480 * @param result The result of the executed command
481 * @param tile The tile of the command action
484 typedef void CommandCallback(Commands cmd
, const CommandCost
&result
, TileIndex tile
);
487 * Define a callback function for the client, after the command is finished.
489 * Functions of this type are called after the command is finished. The parameters
490 * are from the #CommandProc callback type. The boolean parameter indicates if the
491 * command succeeded or failed.
493 * @param cmd The command that was executed
494 * @param result The result of the executed command
495 * @param tile The tile of the command action
496 * @param data Additional data of the command
497 * @param result_data Additional returned data from the command
500 typedef void CommandCallbackData(Commands cmd
, const CommandCost
&result
, const CommandDataBuffer
&data
, CommandDataBuffer result_data
);
502 #endif /* COMMAND_TYPE_H */