Fix #10117: Decrement object burst limit after build check
[openttd-github.git] / src / command_type.h
blob85f5680aead87bdc53857c8a15a3aaa95a0d88c1
1 /*
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/>.
6 */
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"
16 #include <vector>
18 struct GRFFile;
20 /**
21 * Common return value for all commands. Wraps the cost and
22 * a possible error message/state together.
24 class CommandCost {
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];
34 public:
35 /**
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) {}
40 /**
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) {}
45 /**
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) {}
51 /**
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) {}
59 /**
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)
65 this->cost += cost;
68 void AddCost(const CommandCost &cmd_cost);
70 /**
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)
76 this->cost *= factor;
79 /**
80 * The costs as made up to this moment
81 * @return the costs
83 inline Money GetCost() const
85 return this->cost;
88 /**
89 * The expense type of the cost
90 * @return the expense type
92 inline ExpensesType GetExpensesType() const
94 return this->expense_type;
97 /**
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;
167 * List of commands.
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_OBJECT_AREA, ///< build an area of objects
190 CMD_BUILD_TUNNEL, ///< build a tunnel
192 CMD_REMOVE_FROM_RAIL_STATION, ///< remove a (rectangle of) tiles from a rail station
193 CMD_CONVERT_RAIL, ///< convert a rail type
195 CMD_BUILD_RAIL_WAYPOINT, ///< build a waypoint
196 CMD_RENAME_WAYPOINT, ///< rename a waypoint
197 CMD_REMOVE_FROM_RAIL_WAYPOINT, ///< remove a (rectangle of) tiles from a rail waypoint
199 CMD_BUILD_ROAD_STOP, ///< build a road stop
200 CMD_REMOVE_ROAD_STOP, ///< remove a road stop
201 CMD_BUILD_LONG_ROAD, ///< build a complete road (not a "half" one)
202 CMD_REMOVE_LONG_ROAD, ///< remove a complete road (not a "half" one)
203 CMD_BUILD_ROAD, ///< build a "half" road
204 CMD_BUILD_ROAD_DEPOT, ///< build a road depot
205 CMD_CONVERT_ROAD, ///< convert a road type
207 CMD_BUILD_AIRPORT, ///< build an airport
209 CMD_BUILD_DOCK, ///< build a dock
211 CMD_BUILD_SHIP_DEPOT, ///< build a ship depot
212 CMD_BUILD_BUOY, ///< build a buoy
214 CMD_PLANT_TREE, ///< plant a tree
216 CMD_BUILD_VEHICLE, ///< build a vehicle
217 CMD_SELL_VEHICLE, ///< sell a vehicle
218 CMD_REFIT_VEHICLE, ///< refit the cargo space of a vehicle
219 CMD_SEND_VEHICLE_TO_DEPOT, ///< send a vehicle to a depot
220 CMD_SET_VEHICLE_VISIBILITY, ///< hide or unhide a vehicle in the build vehicle and autoreplace GUIs
222 CMD_MOVE_RAIL_VEHICLE, ///< move a rail vehicle (in the depot)
223 CMD_FORCE_TRAIN_PROCEED, ///< proceed a train to pass a red signal
224 CMD_REVERSE_TRAIN_DIRECTION, ///< turn a train around
226 CMD_CLEAR_ORDER_BACKUP, ///< clear the order backup of a given user/tile
227 CMD_MODIFY_ORDER, ///< modify an order (like set full-load)
228 CMD_SKIP_TO_ORDER, ///< skip an order to the next of specific one
229 CMD_DELETE_ORDER, ///< delete an order
230 CMD_INSERT_ORDER, ///< insert a new order
232 CMD_CHANGE_SERVICE_INT, ///< change the server interval of a vehicle
234 CMD_BUILD_INDUSTRY, ///< build a new industry
235 CMD_INDUSTRY_CTRL, ///< change industry properties
237 CMD_SET_COMPANY_MANAGER_FACE, ///< set the manager's face of the company
238 CMD_SET_COMPANY_COLOUR, ///< set the colour of the company
240 CMD_INCREASE_LOAN, ///< increase the loan from the bank
241 CMD_DECREASE_LOAN, ///< decrease the loan from the bank
243 CMD_WANT_ENGINE_PREVIEW, ///< confirm the preview of an engine
244 CMD_ENGINE_CTRL, ///< control availability of the engine for companies
246 CMD_RENAME_VEHICLE, ///< rename a whole vehicle
247 CMD_RENAME_ENGINE, ///< rename a engine (in the engine list)
248 CMD_RENAME_COMPANY, ///< change the company name
249 CMD_RENAME_PRESIDENT, ///< change the president name
250 CMD_RENAME_STATION, ///< rename a station
251 CMD_RENAME_DEPOT, ///< rename a depot
253 CMD_PLACE_SIGN, ///< place a sign
254 CMD_RENAME_SIGN, ///< rename a sign
256 CMD_TURN_ROADVEH, ///< turn a road vehicle around
258 CMD_PAUSE, ///< pause the game
260 CMD_BUY_SHARE_IN_COMPANY, ///< buy a share from a company
261 CMD_SELL_SHARE_IN_COMPANY, ///< sell a share from a company
262 CMD_BUY_COMPANY, ///< buy a company which is bankrupt
264 CMD_FOUND_TOWN, ///< found a town
265 CMD_RENAME_TOWN, ///< rename a town
266 CMD_DO_TOWN_ACTION, ///< do a action from the town detail window (like advertises or bribe)
267 CMD_TOWN_CARGO_GOAL, ///< set the goal of a cargo for a town
268 CMD_TOWN_GROWTH_RATE, ///< set the town growth rate
269 CMD_TOWN_RATING, ///< set rating of a company in a town
270 CMD_TOWN_SET_TEXT, ///< set the custom text of a town
271 CMD_EXPAND_TOWN, ///< expand a town
272 CMD_DELETE_TOWN, ///< delete a town
274 CMD_ORDER_REFIT, ///< change the refit information of an order (for "goto depot" )
275 CMD_CLONE_ORDER, ///< clone (and share) an order
276 CMD_CLEAR_AREA, ///< clear an area
278 CMD_MONEY_CHEAT, ///< do the money cheat
279 CMD_CHANGE_BANK_BALANCE, ///< change bank balance to charge costs or give money from a GS
280 CMD_BUILD_CANAL, ///< build a canal
282 CMD_CREATE_SUBSIDY, ///< create a new subsidy
283 CMD_COMPANY_CTRL, ///< used in multiplayer to create a new companies etc.
284 CMD_CUSTOM_NEWS_ITEM, ///< create a custom news message
285 CMD_CREATE_GOAL, ///< create a new goal
286 CMD_REMOVE_GOAL, ///< remove a goal
287 CMD_SET_GOAL_TEXT, ///< update goal text of a goal
288 CMD_SET_GOAL_PROGRESS, ///< update goal progress text of a goal
289 CMD_SET_GOAL_COMPLETED, ///< update goal completed status of a goal
290 CMD_GOAL_QUESTION, ///< ask a goal related question
291 CMD_GOAL_QUESTION_ANSWER, ///< answer(s) to CMD_GOAL_QUESTION
292 CMD_CREATE_STORY_PAGE, ///< create a new story page
293 CMD_CREATE_STORY_PAGE_ELEMENT, ///< create a new story page element
294 CMD_UPDATE_STORY_PAGE_ELEMENT, ///< update a story page element
295 CMD_SET_STORY_PAGE_TITLE, ///< update title of a story page
296 CMD_SET_STORY_PAGE_DATE, ///< update date of a story page
297 CMD_SHOW_STORY_PAGE, ///< show a story page
298 CMD_REMOVE_STORY_PAGE, ///< remove a story page
299 CMD_REMOVE_STORY_PAGE_ELEMENT, ///< remove a story page element
300 CMD_SCROLL_VIEWPORT, ///< scroll main viewport of players
301 CMD_STORY_PAGE_BUTTON, ///< selection via story page button
303 CMD_LEVEL_LAND, ///< level land
305 CMD_BUILD_LOCK, ///< build a lock
307 CMD_BUILD_SIGNAL_TRACK, ///< add signals along a track (by dragging)
308 CMD_REMOVE_SIGNAL_TRACK, ///< remove signals along a track (by dragging)
310 CMD_GIVE_MONEY, ///< give money to another company
311 CMD_CHANGE_SETTING, ///< change a setting
312 CMD_CHANGE_COMPANY_SETTING, ///< change a company setting
314 CMD_SET_AUTOREPLACE, ///< set an autoreplace entry
316 CMD_CLONE_VEHICLE, ///< clone a vehicle
317 CMD_START_STOP_VEHICLE, ///< start or stop a vehicle
318 CMD_MASS_START_STOP, ///< start/stop all vehicles (in a depot)
319 CMD_AUTOREPLACE_VEHICLE, ///< replace/renew a vehicle while it is in a depot
320 CMD_DEPOT_SELL_ALL_VEHICLES, ///< sell all vehicles which are in a given depot
321 CMD_DEPOT_MASS_AUTOREPLACE, ///< force the autoreplace to take action in a given depot
323 CMD_CREATE_GROUP, ///< create a new group
324 CMD_DELETE_GROUP, ///< delete a group
325 CMD_ALTER_GROUP, ///< alter a group
326 CMD_ADD_VEHICLE_GROUP, ///< add a vehicle to a group
327 CMD_ADD_SHARED_VEHICLE_GROUP, ///< add all other shared vehicles to a group which are missing
328 CMD_REMOVE_ALL_VEHICLES_GROUP, ///< remove all vehicles from a group
329 CMD_SET_GROUP_FLAG, ///< set/clear a flag for a group
330 CMD_SET_GROUP_LIVERY, ///< set the livery for a group
332 CMD_MOVE_ORDER, ///< move an order
333 CMD_CHANGE_TIMETABLE, ///< change the timetable for a vehicle
334 CMD_SET_VEHICLE_ON_TIME, ///< set the vehicle on time feature (timetable)
335 CMD_AUTOFILL_TIMETABLE, ///< autofill the timetable
336 CMD_SET_TIMETABLE_START, ///< set the date that a timetable should start
338 CMD_OPEN_CLOSE_AIRPORT, ///< open/close an airport to incoming aircraft
340 CMD_END, ///< Must ALWAYS be on the end of this list!! (period)
344 * List of flags for a command.
346 * This enums defines some flags which can be used for the commands.
348 enum DoCommandFlag {
349 DC_NONE = 0x000, ///< no flag is set
350 DC_EXEC = 0x001, ///< execute the given command
351 DC_AUTO = 0x002, ///< don't allow building on structures
352 DC_QUERY_COST = 0x004, ///< query cost only, don't build.
353 DC_NO_WATER = 0x008, ///< don't allow building on water
354 // 0x010 is unused
355 DC_NO_TEST_TOWN_RATING = 0x020, ///< town rating does not disallow you from building
356 DC_BANKRUPT = 0x040, ///< company bankrupts, skip money check, skip vehicle on tile check in some cases
357 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)
358 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
359 DC_ALL_TILES = 0x200, ///< allow this command also on MP_VOID tiles
360 DC_NO_MODIFY_TOWN_RATING = 0x400, ///< do not change town rating
361 DC_FORCE_CLEAR_TILE = 0x800, ///< do not only remove the object on the tile, but also clear any water left on it
363 DECLARE_ENUM_AS_BIT_SET(DoCommandFlag)
366 * Command flags for the command table _command_proc_table.
368 * This enumeration defines flags for the _command_proc_table.
370 enum CommandFlags {
371 CMD_SERVER = 0x001, ///< the command can only be initiated by the server
372 CMD_SPECTATOR = 0x002, ///< the command may be initiated by a spectator
373 CMD_OFFLINE = 0x004, ///< the command cannot be executed in a multiplayer game; single-player only
374 CMD_AUTO = 0x008, ///< set the DC_AUTO flag on this command
375 CMD_ALL_TILES = 0x010, ///< allow this command also on MP_VOID tiles
376 CMD_NO_TEST = 0x020, ///< the command's output may differ between test and execute due to town rating changes etc.
377 CMD_NO_WATER = 0x040, ///< set the DC_NO_WATER flag on this command
378 CMD_CLIENT_ID = 0x080, ///< set p2 with the ClientID of the sending client.
379 CMD_DEITY = 0x100, ///< the command may be executed by COMPANY_DEITY
380 CMD_STR_CTRL = 0x200, ///< the command's string may contain control strings
381 CMD_NO_EST = 0x400, ///< the command is never estimated.
383 DECLARE_ENUM_AS_BIT_SET(CommandFlags)
385 /** Types of commands we have. */
386 enum CommandType {
387 CMDT_LANDSCAPE_CONSTRUCTION, ///< Construction and destruction of objects on the map.
388 CMDT_VEHICLE_CONSTRUCTION, ///< Construction, modification (incl. refit) and destruction of vehicles.
389 CMDT_MONEY_MANAGEMENT, ///< Management of money, i.e. loans and shares.
390 CMDT_VEHICLE_MANAGEMENT, ///< Stopping, starting, sending to depot, turning around, replace orders etc.
391 CMDT_ROUTE_MANAGEMENT, ///< Modifications to route management (orders, groups, etc).
392 CMDT_OTHER_MANAGEMENT, ///< Renaming stuff, changing company colours, placing signs, etc.
393 CMDT_COMPANY_SETTING, ///< Changing settings related to a company.
394 CMDT_SERVER_SETTING, ///< Pausing/removing companies/server settings.
395 CMDT_CHEAT, ///< A cheat of some sorts.
397 CMDT_END, ///< Magic end marker.
400 /** Different command pause levels. */
401 enum CommandPauseLevel {
402 CMDPL_NO_ACTIONS, ///< No user actions may be executed.
403 CMDPL_NO_CONSTRUCTION, ///< No construction actions may be executed.
404 CMDPL_NO_LANDSCAPING, ///< No landscaping actions may be executed.
405 CMDPL_ALL_ACTIONS, ///< All actions may be executed.
409 template <typename T> struct CommandFunctionTraitHelper;
410 template <typename... Targs>
411 struct CommandFunctionTraitHelper<CommandCost(*)(DoCommandFlag, Targs...)> {
412 using Args = std::tuple<std::decay_t<Targs>...>;
413 using RetTypes = void;
414 using CbArgs = Args;
415 using CbProcType = void(*)(Commands, const CommandCost &);
417 template <template <typename...> typename Tret, typename... Tretargs, typename... Targs>
418 struct CommandFunctionTraitHelper<Tret<CommandCost, Tretargs...>(*)(DoCommandFlag, Targs...)> {
419 using Args = std::tuple<std::decay_t<Targs>...>;
420 using RetTypes = std::tuple<std::decay_t<Tretargs>...>;
421 using CbArgs = std::tuple<std::decay_t<Tretargs>..., std::decay_t<Targs>...>;
422 using CbProcType = void(*)(Commands, const CommandCost &, Tretargs...);
425 /** Defines the traits of a command. */
426 template <Commands Tcmd> struct CommandTraits;
428 #define DEF_CMD_TRAIT(cmd_, proc_, flags_, type_) \
429 template<> struct CommandTraits<cmd_> { \
430 using ProcType = decltype(&proc_); \
431 using Args = typename CommandFunctionTraitHelper<ProcType>::Args; \
432 using RetTypes = typename CommandFunctionTraitHelper<ProcType>::RetTypes; \
433 using CbArgs = typename CommandFunctionTraitHelper<ProcType>::CbArgs; \
434 using RetCallbackProc = typename CommandFunctionTraitHelper<ProcType>::CbProcType; \
435 static constexpr Commands cmd = cmd_; \
436 static constexpr auto &proc = proc_; \
437 static constexpr CommandFlags flags = (CommandFlags)(flags_); \
438 static constexpr CommandType type = type_; \
439 static inline constexpr const char *name = #proc_; \
442 /** Storage buffer for serialized command data. */
443 typedef std::vector<byte> CommandDataBuffer;
446 * Define a callback function for the client, after the command is finished.
448 * Functions of this type are called after the command is finished. The parameters
449 * are from the #CommandProc callback type. The boolean parameter indicates if the
450 * command succeeded or failed.
452 * @param cmd The command that was executed
453 * @param result The result of the executed command
454 * @param tile The tile of the command action
455 * @see CommandProc
457 typedef void CommandCallback(Commands cmd, const CommandCost &result, TileIndex tile);
460 * Define a callback function for the client, after the command is finished.
462 * Functions of this type are called after the command is finished. The parameters
463 * are from the #CommandProc callback type. The boolean parameter indicates if the
464 * command succeeded or failed.
466 * @param cmd The command that was executed
467 * @param result The result of the executed command
468 * @param tile The tile of the command action
469 * @param data Additional data of the command
470 * @param result_data Additional returned data from the command
471 * @see CommandProc
473 typedef void CommandCallbackData(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data);
475 #endif /* COMMAND_TYPE_H */