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"
21 * Common return value for all commands. Wraps the cost and
22 * a possible error message/state together.
25 ExpensesType expense_type
; ///< the type of expence as shown on the finances view
26 Money cost
; ///< The cost of this action
27 StringID message
; ///< Warning message for when success is unset
28 bool success
; ///< Whether the comment went fine up to this moment
29 const GRFFile
*textref_stack_grffile
; ///< NewGRF providing the #TextRefStack content.
30 uint textref_stack_size
; ///< Number of uint32 values to put on the #TextRefStack for the error message.
32 static uint32 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 the is failed with the given message
43 explicit CommandCost(StringID msg
) : expense_type(INVALID_EXPENSES
), cost(0), message(msg
), success(false), textref_stack_grffile(nullptr), textref_stack_size(0) {}
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
)
103 assert(message
!= INVALID_STRING_ID
);
104 this->success
= false;
105 this->message
= message
;
108 void UseTextRefStack(const GRFFile
*grffile
, uint num_registers
);
111 * Returns the NewGRF providing the #TextRefStack of the error message.
112 * @return the NewGRF.
114 const GRFFile
*GetTextRefStackGRF() const
116 return this->textref_stack_grffile
;
120 * Returns the number of uint32 values for the #TextRefStack of the error message.
121 * @return number of uint32 values.
123 uint
GetTextRefStackSize() const
125 return this->textref_stack_size
;
129 * Returns a pointer to the values for the #TextRefStack of the error message.
130 * @return uint32 values for the #TextRefStack
132 const uint32
*GetTextRefStack() const
134 return textref_stack
;
138 * Returns the error message of a command
139 * @return the error message, if succeeded #INVALID_STRING_ID
141 StringID
GetErrorMessage() const
143 if (this->success
) return INVALID_STRING_ID
;
144 return this->message
;
148 * Did this command succeed?
149 * @return true if and only if it succeeded
151 inline bool Succeeded() const
153 return this->success
;
157 * Did this command fail?
158 * @return true if and only if it failed
160 inline bool Failed() const
162 return !this->success
;
169 * This enum defines all possible commands which can be executed to the game
170 * engine. Observing the game like the query-tool or checking the profit of a
171 * vehicle don't result in a command which should be executed in the engine
172 * nor send to the server in a network game.
174 * @see _command_proc_table
176 enum Commands
: uint16
{
177 CMD_BUILD_RAILROAD_TRACK
, ///< build a rail track
178 CMD_REMOVE_RAILROAD_TRACK
, ///< remove a rail track
179 CMD_BUILD_SINGLE_RAIL
, ///< build a single rail track
180 CMD_REMOVE_SINGLE_RAIL
, ///< remove a single rail track
181 CMD_LANDSCAPE_CLEAR
, ///< demolish a tile
182 CMD_BUILD_BRIDGE
, ///< build a bridge
183 CMD_BUILD_RAIL_STATION
, ///< build a rail station
184 CMD_BUILD_TRAIN_DEPOT
, ///< build a train depot
185 CMD_BUILD_SIGNALS
, ///< build a signal
186 CMD_REMOVE_SIGNALS
, ///< remove a signal
187 CMD_TERRAFORM_LAND
, ///< terraform a tile
188 CMD_BUILD_OBJECT
, ///< build an object
189 CMD_BUILD_TUNNEL
, ///< build a tunnel
191 CMD_REMOVE_FROM_RAIL_STATION
, ///< remove a (rectangle of) tiles from a rail station
192 CMD_CONVERT_RAIL
, ///< convert a rail type
194 CMD_BUILD_RAIL_WAYPOINT
, ///< build a waypoint
195 CMD_RENAME_WAYPOINT
, ///< rename a waypoint
196 CMD_REMOVE_FROM_RAIL_WAYPOINT
, ///< remove a (rectangle of) tiles from a rail waypoint
198 CMD_BUILD_ROAD_STOP
, ///< build a road stop
199 CMD_REMOVE_ROAD_STOP
, ///< remove a road stop
200 CMD_BUILD_LONG_ROAD
, ///< build a complete road (not a "half" one)
201 CMD_REMOVE_LONG_ROAD
, ///< remove a complete road (not a "half" one)
202 CMD_BUILD_ROAD
, ///< build a "half" road
203 CMD_BUILD_ROAD_DEPOT
, ///< build a road depot
204 CMD_CONVERT_ROAD
, ///< convert a road type
206 CMD_BUILD_AIRPORT
, ///< build an airport
208 CMD_BUILD_DOCK
, ///< build a dock
210 CMD_BUILD_SHIP_DEPOT
, ///< build a ship depot
211 CMD_BUILD_BUOY
, ///< build a buoy
213 CMD_PLANT_TREE
, ///< plant a tree
215 CMD_BUILD_VEHICLE
, ///< build a vehicle
216 CMD_SELL_VEHICLE
, ///< sell a vehicle
217 CMD_REFIT_VEHICLE
, ///< refit the cargo space of a vehicle
218 CMD_SEND_VEHICLE_TO_DEPOT
, ///< send a vehicle to a depot
219 CMD_SET_VEHICLE_VISIBILITY
, ///< hide or unhide a vehicle in the build vehicle and autoreplace GUIs
221 CMD_MOVE_RAIL_VEHICLE
, ///< move a rail vehicle (in the depot)
222 CMD_FORCE_TRAIN_PROCEED
, ///< proceed a train to pass a red signal
223 CMD_REVERSE_TRAIN_DIRECTION
, ///< turn a train around
225 CMD_CLEAR_ORDER_BACKUP
, ///< clear the order backup of a given user/tile
226 CMD_MODIFY_ORDER
, ///< modify an order (like set full-load)
227 CMD_SKIP_TO_ORDER
, ///< skip an order to the next of specific one
228 CMD_DELETE_ORDER
, ///< delete an order
229 CMD_INSERT_ORDER
, ///< insert a new order
231 CMD_CHANGE_SERVICE_INT
, ///< change the server interval of a vehicle
233 CMD_BUILD_INDUSTRY
, ///< build a new industry
234 CMD_INDUSTRY_CTRL
, ///< change industry properties
236 CMD_SET_COMPANY_MANAGER_FACE
, ///< set the manager's face of the company
237 CMD_SET_COMPANY_COLOUR
, ///< set the colour of the company
239 CMD_INCREASE_LOAN
, ///< increase the loan from the bank
240 CMD_DECREASE_LOAN
, ///< decrease the loan from the bank
242 CMD_WANT_ENGINE_PREVIEW
, ///< confirm the preview of an engine
243 CMD_ENGINE_CTRL
, ///< control availability of the engine for companies
245 CMD_RENAME_VEHICLE
, ///< rename a whole vehicle
246 CMD_RENAME_ENGINE
, ///< rename a engine (in the engine list)
247 CMD_RENAME_COMPANY
, ///< change the company name
248 CMD_RENAME_PRESIDENT
, ///< change the president name
249 CMD_RENAME_STATION
, ///< rename a station
250 CMD_RENAME_DEPOT
, ///< rename a depot
252 CMD_PLACE_SIGN
, ///< place a sign
253 CMD_RENAME_SIGN
, ///< rename a sign
255 CMD_TURN_ROADVEH
, ///< turn a road vehicle around
257 CMD_PAUSE
, ///< pause the game
259 CMD_BUY_SHARE_IN_COMPANY
, ///< buy a share from a company
260 CMD_SELL_SHARE_IN_COMPANY
, ///< sell a share from a company
261 CMD_BUY_COMPANY
, ///< buy a company which is bankrupt
263 CMD_FOUND_TOWN
, ///< found a town
264 CMD_RENAME_TOWN
, ///< rename a town
265 CMD_DO_TOWN_ACTION
, ///< do a action from the town detail window (like advertises or bribe)
266 CMD_TOWN_CARGO_GOAL
, ///< set the goal of a cargo for a town
267 CMD_TOWN_GROWTH_RATE
, ///< set the town growth rate
268 CMD_TOWN_RATING
, ///< set rating of a company in a town
269 CMD_TOWN_SET_TEXT
, ///< set the custom text of a town
270 CMD_EXPAND_TOWN
, ///< expand a town
271 CMD_DELETE_TOWN
, ///< delete a town
273 CMD_ORDER_REFIT
, ///< change the refit information of an order (for "goto depot" )
274 CMD_CLONE_ORDER
, ///< clone (and share) an order
275 CMD_CLEAR_AREA
, ///< clear an area
277 CMD_MONEY_CHEAT
, ///< do the money cheat
278 CMD_CHANGE_BANK_BALANCE
, ///< change bank balance to charge costs or give money from a GS
279 CMD_BUILD_CANAL
, ///< build a canal
281 CMD_CREATE_SUBSIDY
, ///< create a new subsidy
282 CMD_COMPANY_CTRL
, ///< used in multiplayer to create a new companies etc.
283 CMD_CUSTOM_NEWS_ITEM
, ///< create a custom news message
284 CMD_CREATE_GOAL
, ///< create a new goal
285 CMD_REMOVE_GOAL
, ///< remove a goal
286 CMD_SET_GOAL_TEXT
, ///< update goal text of a goal
287 CMD_SET_GOAL_PROGRESS
, ///< update goal progress text of a goal
288 CMD_SET_GOAL_COMPLETED
, ///< update goal completed status of a goal
289 CMD_GOAL_QUESTION
, ///< ask a goal related question
290 CMD_GOAL_QUESTION_ANSWER
, ///< answer(s) to CMD_GOAL_QUESTION
291 CMD_CREATE_STORY_PAGE
, ///< create a new story page
292 CMD_CREATE_STORY_PAGE_ELEMENT
, ///< create a new story page element
293 CMD_UPDATE_STORY_PAGE_ELEMENT
, ///< update a story page element
294 CMD_SET_STORY_PAGE_TITLE
, ///< update title of a story page
295 CMD_SET_STORY_PAGE_DATE
, ///< update date of a story page
296 CMD_SHOW_STORY_PAGE
, ///< show a story page
297 CMD_REMOVE_STORY_PAGE
, ///< remove a story page
298 CMD_REMOVE_STORY_PAGE_ELEMENT
, ///< remove a story page element
299 CMD_SCROLL_VIEWPORT
, ///< scroll main viewport of players
300 CMD_STORY_PAGE_BUTTON
, ///< selection via story page button
302 CMD_LEVEL_LAND
, ///< level land
304 CMD_BUILD_LOCK
, ///< build a lock
306 CMD_BUILD_SIGNAL_TRACK
, ///< add signals along a track (by dragging)
307 CMD_REMOVE_SIGNAL_TRACK
, ///< remove signals along a track (by dragging)
309 CMD_GIVE_MONEY
, ///< give money to another company
310 CMD_CHANGE_SETTING
, ///< change a setting
311 CMD_CHANGE_COMPANY_SETTING
, ///< change a company setting
313 CMD_SET_AUTOREPLACE
, ///< set an autoreplace entry
315 CMD_CLONE_VEHICLE
, ///< clone a vehicle
316 CMD_START_STOP_VEHICLE
, ///< start or stop a vehicle
317 CMD_MASS_START_STOP
, ///< start/stop all vehicles (in a depot)
318 CMD_AUTOREPLACE_VEHICLE
, ///< replace/renew a vehicle while it is in a depot
319 CMD_DEPOT_SELL_ALL_VEHICLES
, ///< sell all vehicles which are in a given depot
320 CMD_DEPOT_MASS_AUTOREPLACE
, ///< force the autoreplace to take action in a given depot
322 CMD_CREATE_GROUP
, ///< create a new group
323 CMD_DELETE_GROUP
, ///< delete a group
324 CMD_ALTER_GROUP
, ///< alter a group
325 CMD_ADD_VEHICLE_GROUP
, ///< add a vehicle to a group
326 CMD_ADD_SHARED_VEHICLE_GROUP
, ///< add all other shared vehicles to a group which are missing
327 CMD_REMOVE_ALL_VEHICLES_GROUP
, ///< remove all vehicles from a group
328 CMD_SET_GROUP_FLAG
, ///< set/clear a flag for a group
329 CMD_SET_GROUP_LIVERY
, ///< set the livery for a group
331 CMD_MOVE_ORDER
, ///< move an order
332 CMD_CHANGE_TIMETABLE
, ///< change the timetable for a vehicle
333 CMD_SET_VEHICLE_ON_TIME
, ///< set the vehicle on time feature (timetable)
334 CMD_AUTOFILL_TIMETABLE
, ///< autofill the timetable
335 CMD_SET_TIMETABLE_START
, ///< set the date that a timetable should start
337 CMD_OPEN_CLOSE_AIRPORT
, ///< open/close an airport to incoming aircraft
339 CMD_END
, ///< Must ALWAYS be on the end of this list!! (period)
343 * List of flags for a command.
345 * This enums defines some flags which can be used for the commands.
348 DC_NONE
= 0x000, ///< no flag is set
349 DC_EXEC
= 0x001, ///< execute the given command
350 DC_AUTO
= 0x002, ///< don't allow building on structures
351 DC_QUERY_COST
= 0x004, ///< query cost only, don't build.
352 DC_NO_WATER
= 0x008, ///< don't allow building on water
354 DC_NO_TEST_TOWN_RATING
= 0x020, ///< town rating does not disallow you from building
355 DC_BANKRUPT
= 0x040, ///< company bankrupts, skip money check, skip vehicle on tile check in some cases
356 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)
357 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
358 DC_ALL_TILES
= 0x200, ///< allow this command also on MP_VOID tiles
359 DC_NO_MODIFY_TOWN_RATING
= 0x400, ///< do not change town rating
360 DC_FORCE_CLEAR_TILE
= 0x800, ///< do not only remove the object on the tile, but also clear any water left on it
362 DECLARE_ENUM_AS_BIT_SET(DoCommandFlag
)
365 * Command flags for the command table _command_proc_table.
367 * This enumeration defines flags for the _command_proc_table.
370 CMD_SERVER
= 0x001, ///< the command can only be initiated by the server
371 CMD_SPECTATOR
= 0x002, ///< the command may be initiated by a spectator
372 CMD_OFFLINE
= 0x004, ///< the command cannot be executed in a multiplayer game; single-player only
373 CMD_AUTO
= 0x008, ///< set the DC_AUTO flag on this command
374 CMD_ALL_TILES
= 0x010, ///< allow this command also on MP_VOID tiles
375 CMD_NO_TEST
= 0x020, ///< the command's output may differ between test and execute due to town rating changes etc.
376 CMD_NO_WATER
= 0x040, ///< set the DC_NO_WATER flag on this command
377 CMD_CLIENT_ID
= 0x080, ///< set p2 with the ClientID of the sending client.
378 CMD_DEITY
= 0x100, ///< the command may be executed by COMPANY_DEITY
379 CMD_STR_CTRL
= 0x200, ///< the command's string may contain control strings
380 CMD_NO_EST
= 0x400, ///< the command is never estimated.
382 DECLARE_ENUM_AS_BIT_SET(CommandFlags
)
384 /** Types of commands we have. */
386 CMDT_LANDSCAPE_CONSTRUCTION
, ///< Construction and destruction of objects on the map.
387 CMDT_VEHICLE_CONSTRUCTION
, ///< Construction, modification (incl. refit) and destruction of vehicles.
388 CMDT_MONEY_MANAGEMENT
, ///< Management of money, i.e. loans and shares.
389 CMDT_VEHICLE_MANAGEMENT
, ///< Stopping, starting, sending to depot, turning around, replace orders etc.
390 CMDT_ROUTE_MANAGEMENT
, ///< Modifications to route management (orders, groups, etc).
391 CMDT_OTHER_MANAGEMENT
, ///< Renaming stuff, changing company colours, placing signs, etc.
392 CMDT_COMPANY_SETTING
, ///< Changing settings related to a company.
393 CMDT_SERVER_SETTING
, ///< Pausing/removing companies/server settings.
394 CMDT_CHEAT
, ///< A cheat of some sorts.
396 CMDT_END
, ///< Magic end marker.
399 /** Different command pause levels. */
400 enum CommandPauseLevel
{
401 CMDPL_NO_ACTIONS
, ///< No user actions may be executed.
402 CMDPL_NO_CONSTRUCTION
, ///< No construction actions may be executed.
403 CMDPL_NO_LANDSCAPING
, ///< No landscaping actions may be executed.
404 CMDPL_ALL_ACTIONS
, ///< All actions may be executed.
408 template <typename T
> struct CommandFunctionTraitHelper
;
409 template <typename
... Targs
>
410 struct CommandFunctionTraitHelper
<CommandCost(*)(DoCommandFlag
, Targs
...)> {
411 using Args
= std::tuple
<std::decay_t
<Targs
>...>;
412 using RetTypes
= void;
414 using CbProcType
= void(*)(Commands
, const CommandCost
&);
416 template <template <typename
...> typename Tret
, typename
... Tretargs
, typename
... Targs
>
417 struct CommandFunctionTraitHelper
<Tret
<CommandCost
, Tretargs
...>(*)(DoCommandFlag
, Targs
...)> {
418 using Args
= std::tuple
<std::decay_t
<Targs
>...>;
419 using RetTypes
= std::tuple
<std::decay_t
<Tretargs
>...>;
420 using CbArgs
= std::tuple
<std::decay_t
<Tretargs
>..., std::decay_t
<Targs
>...>;
421 using CbProcType
= void(*)(Commands
, const CommandCost
&, Tretargs
...);
424 /** Defines the traits of a command. */
425 template <Commands Tcmd
> struct CommandTraits
;
427 #define DEF_CMD_TRAIT(cmd_, proc_, flags_, type_) \
428 template<> struct CommandTraits<cmd_> { \
429 using ProcType = decltype(&proc_); \
430 using Args = typename CommandFunctionTraitHelper<ProcType>::Args; \
431 using RetTypes = typename CommandFunctionTraitHelper<ProcType>::RetTypes; \
432 using CbArgs = typename CommandFunctionTraitHelper<ProcType>::CbArgs; \
433 using RetCallbackProc = typename CommandFunctionTraitHelper<ProcType>::CbProcType; \
434 static constexpr Commands cmd = cmd_; \
435 static constexpr auto &proc = proc_; \
436 static constexpr CommandFlags flags = (CommandFlags)(flags_); \
437 static constexpr CommandType type = type_; \
438 static inline constexpr const char *name = #proc_; \
441 /** Storage buffer for serialized command data. */
442 typedef std::vector
<byte
> CommandDataBuffer
;
445 * Define a callback function for the client, after the command is finished.
447 * Functions of this type are called after the command is finished. The parameters
448 * are from the #CommandProc callback type. The boolean parameter indicates if the
449 * command succeeded or failed.
451 * @param cmd The command that was executed
452 * @param result The result of the executed command
453 * @param tile The tile of the command action
456 typedef void CommandCallback(Commands cmd
, const CommandCost
&result
, TileIndex tile
);
459 * Define a callback function for the client, after the command is finished.
461 * Functions of this type are called after the command is finished. The parameters
462 * are from the #CommandProc callback type. The boolean parameter indicates if the
463 * command succeeded or failed.
465 * @param cmd The command that was executed
466 * @param result The result of the executed command
467 * @param tile The tile of the command action
468 * @param data Additional data of the command
469 * @param result_data Additional returned data from the command
472 typedef void CommandCallbackData(Commands cmd
, const CommandCost
&result
, TileIndex tile
, const CommandDataBuffer
&data
, CommandDataBuffer result_data
);
474 #endif /* COMMAND_TYPE_H */