Codechange: Use wrapper struct to automatically manage command depth tracking.
[openttd-github.git] / src / command_func.h
blob2a6f0c68a715e869a5ed59177fa6c04a5ce4ebee
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_func.h Functions related to commands. */
10 #ifndef COMMAND_FUNC_H
11 #define COMMAND_FUNC_H
13 #include "command_type.h"
14 #include "company_type.h"
15 #include <vector>
17 /**
18 * Define a default return value for a failed command.
20 * This variable contains a CommandCost object with is declared as "failed".
21 * Other functions just need to return this error if there is an error,
22 * which doesn't need to specific by a StringID.
24 static const CommandCost CMD_ERROR = CommandCost(INVALID_STRING_ID);
26 /**
27 * Returns from a function with a specific StringID as error.
29 * This macro is used to return from a function. The parameter contains the
30 * StringID which will be returned.
32 * @param errcode The StringID to return
34 #define return_cmd_error(errcode) return CommandCost(errcode);
36 /** Storage buffer for serialized command data. */
37 typedef std::vector<byte> CommandDataBuffer;
39 CommandCost DoCommand(DoCommandFlag flags, Commands cmd, TileIndex tile, uint32 p1, uint32 p2, const std::string &text = {});
41 bool DoCommandP(Commands cmd, StringID err_message, CommandCallback *callback, TileIndex tile, uint32 p1, uint32 p2, const std::string &text = {});
42 bool DoCommandP(Commands cmd, StringID err_message, TileIndex tile, uint32 p1, uint32 p2, const std::string &text = {});
43 bool DoCommandP(Commands cmd, CommandCallback *callback, TileIndex tile, uint32 p1, uint32 p2, const std::string &text = {});
44 bool DoCommandP(Commands cmd, TileIndex tile, uint32 p1, uint32 p2, const std::string &text = {});
46 bool InjectNetworkCommand(Commands cmd, StringID err_message, CommandCallback *callback, bool my_cmd, TileIndex tile, uint32 p1, uint32 p2, const std::string &text);
48 CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, uint32 p1, uint32 p2, const std::string &text);
50 void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex tile, uint32 p1, uint32 p2, const std::string &text);
51 void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex location, const CommandDataBuffer &cmd_data);
53 extern Money _additional_cash_required;
55 bool IsValidCommand(Commands cmd);
56 CommandFlags GetCommandFlags(Commands cmd);
57 const char *GetCommandName(Commands cmd);
58 Money GetAvailableMoneyForCommand();
59 bool IsCommandAllowedWhilePaused(Commands cmd);
61 /**
62 * Extracts the DC flags needed for DoCommand from the flags returned by GetCommandFlags
63 * @param cmd_flags Flags from GetCommandFlags
64 * @return flags for DoCommand
66 static inline DoCommandFlag CommandFlagsToDCFlags(CommandFlags cmd_flags)
68 DoCommandFlag flags = DC_NONE;
69 if (cmd_flags & CMD_NO_WATER) flags |= DC_NO_WATER;
70 if (cmd_flags & CMD_AUTO) flags |= DC_AUTO;
71 if (cmd_flags & CMD_ALL_TILES) flags |= DC_ALL_TILES;
72 return flags;
75 /** Helper class to keep track of command nesting level. */
76 struct RecursiveCommandCounter {
77 RecursiveCommandCounter() noexcept { _counter++; }
78 ~RecursiveCommandCounter() noexcept { _counter--; }
80 /** Are we in the top-level command execution? */
81 bool IsTopLevel() const { return _counter == 1; }
82 private:
83 static int _counter;
86 #endif /* COMMAND_FUNC_H */