Fix: CmdSetAutoReplace didn't validate group type and engine type match (#9950)
[openttd-github.git] / src / command.cpp
blob65e5acf10c91f2cb60640729a65aa4128b698024
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.cpp Handling of commands. */
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "error.h"
13 #include "gui.h"
14 #include "command_func.h"
15 #include "network/network_type.h"
16 #include "network/network.h"
17 #include "genworld.h"
18 #include "strings_func.h"
19 #include "texteff.hpp"
20 #include "town.h"
21 #include "date_func.h"
22 #include "company_func.h"
23 #include "company_base.h"
24 #include "signal_func.h"
25 #include "core/backup_type.hpp"
26 #include "object_base.h"
27 #include "autoreplace_cmd.h"
28 #include "company_cmd.h"
29 #include "depot_cmd.h"
30 #include "economy_cmd.h"
31 #include "engine_cmd.h"
32 #include "goal_cmd.h"
33 #include "group_cmd.h"
34 #include "industry_cmd.h"
35 #include "landscape_cmd.h"
36 #include "misc_cmd.h"
37 #include "news_cmd.h"
38 #include "object_cmd.h"
39 #include "order_cmd.h"
40 #include "rail_cmd.h"
41 #include "road_cmd.h"
42 #include "roadveh_cmd.h"
43 #include "settings_cmd.h"
44 #include "signs_cmd.h"
45 #include "station_cmd.h"
46 #include "story_cmd.h"
47 #include "subsidy_cmd.h"
48 #include "terraform_cmd.h"
49 #include "timetable_cmd.h"
50 #include "town_cmd.h"
51 #include "train_cmd.h"
52 #include "tree_cmd.h"
53 #include "tunnelbridge_cmd.h"
54 #include "vehicle_cmd.h"
55 #include "viewport_cmd.h"
56 #include "water_cmd.h"
57 #include "waypoint_cmd.h"
58 #include "misc/endian_buffer.hpp"
59 #include "string_func.h"
61 #include <array>
63 #include "table/strings.h"
65 #include "safeguards.h"
68 int RecursiveCommandCounter::_counter = 0;
71 /**
72 * Define a command with the flags which belongs to it.
74 * This struct connects a command handler function with the flags created with
75 * the #CMD_AUTO, #CMD_OFFLINE and #CMD_SERVER values.
77 struct CommandInfo {
78 const char *name; ///< A human readable name for the procedure
79 CommandFlags flags; ///< The (command) flags to that apply to this command
80 CommandType type; ///< The type of command.
82 /* Helpers to generate the master command table from the command traits. */
83 template <typename T>
84 inline constexpr CommandInfo CommandFromTrait() noexcept { return { T::name, T::flags, T::type }; };
86 template<typename T, T... i>
87 inline constexpr auto MakeCommandsFromTraits(std::integer_sequence<T, i...>) noexcept {
88 return std::array<CommandInfo, sizeof...(i)>{{ CommandFromTrait<CommandTraits<static_cast<Commands>(i)>>()... }};
91 /**
92 * The master command table
94 * This table contains all possible CommandProc functions with
95 * the flags which belongs to it. The indices are the same
96 * as the value from the CMD_* enums.
98 static constexpr auto _command_proc_table = MakeCommandsFromTraits(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{});
102 * This function range-checks a cmd.
104 * @param cmd The integer value of a command
105 * @return true if the command is valid (and got a CommandProc function)
107 bool IsValidCommand(Commands cmd)
109 return cmd < _command_proc_table.size();
113 * This function mask the parameter with CMD_ID_MASK and returns
114 * the flags which belongs to the given command.
116 * @param cmd The integer value of the command
117 * @return The flags for this command
119 CommandFlags GetCommandFlags(Commands cmd)
121 assert(IsValidCommand(cmd));
123 return _command_proc_table[cmd].flags;
127 * This function mask the parameter with CMD_ID_MASK and returns
128 * the name which belongs to the given command.
130 * @param cmd The integer value of the command
131 * @return The name for this command
133 const char *GetCommandName(Commands cmd)
135 assert(IsValidCommand(cmd));
137 return _command_proc_table[cmd].name;
141 * Returns whether the command is allowed while the game is paused.
142 * @param cmd The command to check.
143 * @return True if the command is allowed while paused, false otherwise.
145 bool IsCommandAllowedWhilePaused(Commands cmd)
147 /* Lookup table for the command types that are allowed for a given pause level setting. */
148 static const int command_type_lookup[] = {
149 CMDPL_ALL_ACTIONS, ///< CMDT_LANDSCAPE_CONSTRUCTION
150 CMDPL_NO_LANDSCAPING, ///< CMDT_VEHICLE_CONSTRUCTION
151 CMDPL_NO_LANDSCAPING, ///< CMDT_MONEY_MANAGEMENT
152 CMDPL_NO_CONSTRUCTION, ///< CMDT_VEHICLE_MANAGEMENT
153 CMDPL_NO_CONSTRUCTION, ///< CMDT_ROUTE_MANAGEMENT
154 CMDPL_NO_CONSTRUCTION, ///< CMDT_OTHER_MANAGEMENT
155 CMDPL_NO_CONSTRUCTION, ///< CMDT_COMPANY_SETTING
156 CMDPL_NO_ACTIONS, ///< CMDT_SERVER_SETTING
157 CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
159 static_assert(lengthof(command_type_lookup) == CMDT_END);
161 assert(IsValidCommand(cmd));
162 return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd].type] <= _settings_game.construction.command_pause_level;
166 * This functions returns the money which can be used to execute a command.
167 * This is either the money of the current company or INT64_MAX if there
168 * is no such a company "at the moment" like the server itself.
170 * @return The available money of a company or INT64_MAX
172 Money GetAvailableMoneyForCommand()
174 CompanyID company = _current_company;
175 if (!Company::IsValidID(company)) return INT64_MAX;
176 return Company::Get(company)->money;
181 * Prepare for calling a command proc.
182 * @param top_level Top level of command execution, i.e. command from a command.
183 * @param test Test run of command?
185 void CommandHelperBase::InternalDoBefore(bool top_level, bool test)
187 if (top_level) _cleared_object_areas.clear();
188 if (test) SetTownRatingTestMode(true);
192 * Process result after calling a command proc.
193 * @param[in,out] res Command result, may be modified.
194 * @param flags Command flags.
195 * @param top_level Top level of command execution, i.e. command from a command.
196 * @param test Test run of command?
198 void CommandHelperBase::InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test)
200 if (test) {
201 SetTownRatingTestMode(false);
203 if (res.Succeeded() && top_level && !(flags & DC_QUERY_COST) && !(flags & DC_BANKRUPT)) {
204 CheckCompanyHasMoney(res); // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
206 } else {
207 /* If top-level, subtract the money. */
208 if (res.Succeeded() && top_level && !(flags & DC_BANKRUPT)) {
209 SubtractMoneyFromCompany(res);
215 * Decide what to do with the command depending on current game state.
216 * @param cmd Command to execute.
217 * @param flags Command flags.
218 * @param tile Tile of command execution.
219 * @param err_message Message prefix to show on error.
220 * @param network_command Does this command come from the network?
221 * @return error state + do only cost estimation? + send to network only?
223 std::tuple<bool, bool, bool> CommandHelperBase::InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command)
225 /* Cost estimation is generally only done when the
226 * local user presses shift while doing something.
227 * However, in case of incoming network commands,
228 * map generation or the pause button we do want
229 * to execute. */
230 bool estimate_only = _shift_pressed && IsLocalCompany() && !_generating_world && !network_command && !(flags & CMD_NO_EST);
232 /* We're only sending the command, so don't do
233 * fancy things for 'success'. */
234 bool only_sending = _networking && !network_command;
236 if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd) && !estimate_only) {
237 ShowErrorMessage(err_message, STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
238 return { true, estimate_only, only_sending };
239 } else {
240 return { false, estimate_only, only_sending };
245 * Process result of executing a command, possibly displaying any error to the player.
246 * @param res Command result.
247 * @param tile Tile of command execution.
248 * @param estimate_only Is this just cost estimation?
249 * @param only_sending Was the command only sent to network?
250 * @param err_message Message prefix to show on error.
251 * @param my_cmd Is the command from this client?
253 void CommandHelperBase::InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd)
255 int x = TileX(tile) * TILE_SIZE;
256 int y = TileY(tile) * TILE_SIZE;
258 if (res.Failed()) {
259 /* Only show the error when it's for us. */
260 if (estimate_only || (IsLocalCompany() && err_message != 0 && my_cmd)) {
261 ShowErrorMessage(err_message, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack());
263 } else if (estimate_only) {
264 ShowEstimatedCostOrIncome(res.GetCost(), x, y);
265 } else if (!only_sending && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
266 /* Only show the cost animation when we did actually
267 * execute the command, i.e. we're not sending it to
268 * the server, when it has cost the local company
269 * something. Furthermore in the editor there is no
270 * concept of cost, so don't show it there either. */
271 ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
275 /** Helper to make a desync log for a command. */
276 void CommandHelperBase::LogCommandExecution(Commands cmd, StringID err_message, TileIndex tile, const CommandDataBuffer &args, bool failed)
278 Debug(desync, 1, "{}: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", failed ? "cmdf" : "cmd", _date, _date_fract, (int)_current_company, cmd, err_message, tile, FormatArrayAsHex(args), GetCommandName(cmd));
282 * Prepare for the test run of a command proc call.
283 * @param cmd_flags Command flags.
284 * @param tile Tile of command execution.
285 * @param[in,out] cur_company Backup of current company at start of command execution.
286 * @return True if test run can go ahead, false on error.
288 bool CommandHelperBase::InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup<CompanyID> &cur_company)
290 /* Always execute server and spectator commands as spectator */
291 bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
293 /* If the company isn't valid it may only do server command or start a new company!
294 * The server will ditch any server commands a client sends to it, so effectively
295 * this guards the server from executing functions for an invalid company. */
296 if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
297 return false;
300 if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
302 /* Enter test mode. */
303 _cleared_object_areas.clear();
304 SetTownRatingTestMode(true);
305 BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
306 return true;
310 * Validate result of test run and prepare for real execution.
311 * @param cmd_flags Command flags.
312 * @param[in,out] res Command result of test run, may be modified.
313 * @param estimate_only Is this just cost estimation?
314 * @param network_command Does this command come from the network?
315 * @param[in,out] cur_company Backup of current company at start of command execution.
316 * @return True if test run can go ahead, false on error.
318 std::tuple<bool, bool, bool> CommandHelperBase::InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, Backup<CompanyID> &cur_company)
320 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
321 SetTownRatingTestMode(false);
323 /* Make sure we're not messing things up here. */
324 assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
326 /* If the command fails, we're doing an estimate
327 * or the player does not have enough money
328 * (unless it's a command where the test and
329 * execution phase might return different costs)
330 * we bail out here. */
331 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
332 if (res.Failed() || estimate_only || (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
333 return { true, !_networking || _generating_world || network_command, false };
336 bool send_net = _networking && !_generating_world && !network_command;
338 if (!send_net) {
339 /* Prepare for command execution. */
340 _cleared_object_areas.clear();
341 BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
344 return { false, _debug_desync_level >= 1, send_net };
348 * Process the result of a command test run and execution run.
349 * @param cmd Command that was executed.
350 * @param cmd_flags Command flags.
351 * @param res_test Command result of test run.
352 * @param tes_exec Command result of real run.
353 * @param extra_cash Additional cash required for successful command execution.
354 * @param tile Tile of command execution.
355 * @param[in,out] cur_company Backup of current company at start of command execution.
356 * @return Final command result.
358 CommandCost CommandHelperBase::InternalExecuteProcessResult(Commands cmd, CommandFlags cmd_flags, const CommandCost &res_test, const CommandCost &res_exec, Money extra_cash, TileIndex tile, Backup<CompanyID> &cur_company)
360 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
362 if (cmd == CMD_COMPANY_CTRL) {
363 cur_company.Trash();
364 /* We are a new company -> Switch to new local company.
365 * We were closed down -> Switch to spectator
366 * Some other company opened/closed down -> The outside function will switch back */
367 _current_company = _local_company;
368 } else {
369 /* Make sure nothing bad happened, like changing the current company. */
370 assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
371 cur_company.Restore();
374 /* If the test and execution can differ we have to check the
375 * return of the command. Otherwise we can check whether the
376 * test and execution have yielded the same result,
377 * i.e. cost and error state are the same. */
378 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
379 if (!test_and_exec_can_differ) {
380 assert(res_test.GetCost() == res_exec.GetCost() && res_test.Failed() == res_exec.Failed()); // sanity check
381 } else if (res_exec.Failed()) {
382 return res_exec;
385 /* If we're needing more money and we haven't done
386 * anything yet, ask for the money! */
387 if (extra_cash != 0 && res_exec.GetCost() == 0) {
388 /* It could happen we removed rail, thus gained money, and deleted something else.
389 * So make sure the signal buffer is empty even in this case */
390 UpdateSignalsInBuffer();
391 SetDParam(0, extra_cash);
392 return CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
395 /* update last build coordinate of company. */
396 if (tile != 0) {
397 Company *c = Company::GetIfValid(_current_company);
398 if (c != nullptr) c->last_build_coordinate = tile;
401 SubtractMoneyFromCompany(res_exec);
403 /* update signals if needed */
404 UpdateSignalsInBuffer();
406 return res_exec;
411 * Adds the cost of the given command return value to this cost.
412 * Also takes a possible error message when it is set.
413 * @param ret The command to add the cost of.
415 void CommandCost::AddCost(const CommandCost &ret)
417 this->AddCost(ret.cost);
418 if (this->success && !ret.success) {
419 this->message = ret.message;
420 this->success = false;
425 * Values to put on the #TextRefStack for the error message.
426 * There is only one static instance of the array, just like there is only one
427 * instance of normal DParams.
429 uint32 CommandCost::textref_stack[16];
432 * Activate usage of the NewGRF #TextRefStack for the error message.
433 * @param grffile NewGRF that provides the #TextRefStack
434 * @param num_registers number of entries to copy from the temporary NewGRF registers
436 void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
438 extern TemporaryStorageArray<int32, 0x110> _temp_store;
440 assert(num_registers < lengthof(textref_stack));
441 this->textref_stack_grffile = grffile;
442 this->textref_stack_size = num_registers;
443 for (uint i = 0; i < num_registers; i++) {
444 textref_stack[i] = _temp_store.GetValue(0x100 + i);