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  */
7 
8 /** @file command_func.h Functions related to commands. */
9 
10 #ifndef COMMAND_FUNC_H
11 #define COMMAND_FUNC_H
12 
13 #include "command_type.h"
14 #include "company_type.h"
15 
16 /**
17  * Define a default return value for a failed command.
18  *
19  * This variable contains a CommandCost object with is declared as "failed".
20  * Other functions just need to return this error if there is an error,
21  * which doesn't need to specific by a StringID.
22  */
23 static const CommandCost CMD_ERROR = CommandCost(INVALID_STRING_ID);
24 
25 /**
26  * Returns from a function with a specific StringID as error.
27  *
28  * This macro is used to return from a function. The parameter contains the
29  * StringID which will be returned.
30  *
31  * @param errcode The StringID to return
32  */
33 #define return_cmd_error(errcode) return CommandCost(errcode);
34 
35 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const std::string &text = {});
36 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags);
37 
38 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback = nullptr, const std::string &text = {}, bool my_cmd = true);
39 bool DoCommandP(const CommandContainer *container, bool my_cmd = true);
40 
41 CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, bool my_cmd, bool estimate_only);
42 
43 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company);
44 
45 extern Money _additional_cash_required;
46 
47 bool IsValidCommand(uint32 cmd);
48 CommandFlags GetCommandFlags(uint32 cmd);
49 const char *GetCommandName(uint32 cmd);
50 Money GetAvailableMoneyForCommand();
51 bool IsCommandAllowedWhilePaused(uint32 cmd);
52 
53 /**
54  * Extracts the DC flags needed for DoCommand from the flags returned by GetCommandFlags
55  * @param cmd_flags Flags from GetCommandFlags
56  * @return flags for DoCommand
57  */
CommandFlagsToDCFlags(CommandFlags cmd_flags)58 static inline DoCommandFlag CommandFlagsToDCFlags(CommandFlags cmd_flags)
59 {
60 	DoCommandFlag flags = DC_NONE;
61 	if (cmd_flags & CMD_NO_WATER) flags |= DC_NO_WATER;
62 	if (cmd_flags & CMD_AUTO) flags |= DC_AUTO;
63 	if (cmd_flags & CMD_ALL_TILES) flags |= DC_ALL_TILES;
64 	return flags;
65 }
66 
67 /*** All command callbacks that exist ***/
68 
69 /* ai/ai_instance.cpp */
70 CommandCallback CcAI;
71 
72 /* airport_gui.cpp */
73 CommandCallback CcBuildAirport;
74 
75 /* bridge_gui.cpp */
76 CommandCallback CcBuildBridge;
77 
78 /* dock_gui.cpp */
79 CommandCallback CcBuildDocks;
80 CommandCallback CcPlaySound_CONSTRUCTION_WATER;
81 
82 /* depot_gui.cpp */
83 CommandCallback CcCloneVehicle;
84 
85 /* game/game_instance.cpp */
86 CommandCallback CcGame;
87 
88 /* group_gui.cpp */
89 CommandCallback CcCreateGroup;
90 CommandCallback CcAddVehicleNewGroup;
91 
92 /* industry_gui.cpp */
93 CommandCallback CcBuildIndustry;
94 
95 /* main_gui.cpp */
96 CommandCallback CcPlaySound_EXPLOSION;
97 CommandCallback CcPlaceSign;
98 CommandCallback CcTerraform;
99 
100 /* rail_gui.cpp */
101 CommandCallback CcPlaySound_CONSTRUCTION_RAIL;
102 CommandCallback CcRailDepot;
103 CommandCallback CcStation;
104 CommandCallback CcBuildRailTunnel;
105 
106 /* road_gui.cpp */
107 CommandCallback CcPlaySound_CONSTRUCTION_OTHER;
108 CommandCallback CcBuildRoadTunnel;
109 CommandCallback CcRoadDepot;
110 CommandCallback CcRoadStop;
111 
112 /* train_gui.cpp */
113 CommandCallback CcBuildWagon;
114 
115 /* town_gui.cpp */
116 CommandCallback CcFoundTown;
117 CommandCallback CcFoundRandomTown;
118 
119 /* vehicle_gui.cpp */
120 CommandCallback CcBuildPrimaryVehicle;
121 CommandCallback CcStartStopVehicle;
122 
123 #endif /* COMMAND_FUNC_H */
124