Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / command.cpp
blob1d6a530b8ca29a11ea9d1fbd895e00e94cf4bdff
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 "timer/timer_game_economy.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 "league_cmd.h"
36 #include "landscape_cmd.h"
37 #include "misc_cmd.h"
38 #include "news_cmd.h"
39 #include "object_cmd.h"
40 #include "order_cmd.h"
41 #include "rail_cmd.h"
42 #include "road_cmd.h"
43 #include "roadveh_cmd.h"
44 #include "settings_cmd.h"
45 #include "signs_cmd.h"
46 #include "station_cmd.h"
47 #include "story_cmd.h"
48 #include "subsidy_cmd.h"
49 #include "terraform_cmd.h"
50 #include "timetable_cmd.h"
51 #include "town_cmd.h"
52 #include "train_cmd.h"
53 #include "tree_cmd.h"
54 #include "tunnelbridge_cmd.h"
55 #include "vehicle_cmd.h"
56 #include "viewport_cmd.h"
57 #include "water_cmd.h"
58 #include "waypoint_cmd.h"
59 #include "misc/endian_buffer.hpp"
60 #include "string_func.h"
62 #include "table/strings.h"
64 #include "safeguards.h"
67 int RecursiveCommandCounter::_counter = 0;
70 /**
71 * Define a command with the flags which belongs to it.
73 * This struct connects a command handler function with the flags created with
74 * the #CMD_AUTO, #CMD_OFFLINE and #CMD_SERVER values.
76 struct CommandInfo {
77 const char *name; ///< A human readable name for the procedure
78 CommandFlags flags; ///< The (command) flags to that apply to this command
79 CommandType type; ///< The type of command.
81 /* Helpers to generate the master command table from the command traits. */
82 template <typename T>
83 inline constexpr CommandInfo CommandFromTrait() noexcept { return { T::name, T::flags, T::type }; };
85 template<typename T, T... i>
86 inline constexpr auto MakeCommandsFromTraits(std::integer_sequence<T, i...>) noexcept {
87 return std::array<CommandInfo, sizeof...(i)>{{ CommandFromTrait<CommandTraits<static_cast<Commands>(i)>>()... }};
90 /**
91 * The master command table
93 * This table contains all possible CommandProc functions with
94 * the flags which belongs to it. The indices are the same
95 * as the value from the CMD_* enums.
97 static constexpr auto _command_proc_table = MakeCommandsFromTraits(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{});
101 * This function range-checks a cmd.
103 * @param cmd The integer value of a command
104 * @return true if the command is valid (and got a CommandProc function)
106 bool IsValidCommand(Commands cmd)
108 return cmd < _command_proc_table.size();
112 * This function mask the parameter with CMD_ID_MASK and returns
113 * the flags which belongs to the given command.
115 * @param cmd The integer value of the command
116 * @return The flags for this command
118 CommandFlags GetCommandFlags(Commands cmd)
120 assert(IsValidCommand(cmd));
122 return _command_proc_table[cmd].flags;
126 * This function mask the parameter with CMD_ID_MASK and returns
127 * the name which belongs to the given command.
129 * @param cmd The integer value of the command
130 * @return The name for this command
132 const char *GetCommandName(Commands cmd)
134 assert(IsValidCommand(cmd));
136 return _command_proc_table[cmd].name;
140 * Returns whether the command is allowed while the game is paused.
141 * @param cmd The command to check.
142 * @return True if the command is allowed while paused, false otherwise.
144 bool IsCommandAllowedWhilePaused(Commands cmd)
146 /* Lookup table for the command types that are allowed for a given pause level setting. */
147 static const int command_type_lookup[] = {
148 CMDPL_ALL_ACTIONS, ///< CMDT_LANDSCAPE_CONSTRUCTION
149 CMDPL_NO_LANDSCAPING, ///< CMDT_VEHICLE_CONSTRUCTION
150 CMDPL_NO_LANDSCAPING, ///< CMDT_MONEY_MANAGEMENT
151 CMDPL_NO_CONSTRUCTION, ///< CMDT_VEHICLE_MANAGEMENT
152 CMDPL_NO_CONSTRUCTION, ///< CMDT_ROUTE_MANAGEMENT
153 CMDPL_NO_CONSTRUCTION, ///< CMDT_OTHER_MANAGEMENT
154 CMDPL_NO_CONSTRUCTION, ///< CMDT_COMPANY_SETTING
155 CMDPL_NO_ACTIONS, ///< CMDT_SERVER_SETTING
156 CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
158 static_assert(lengthof(command_type_lookup) == CMDT_END);
160 assert(IsValidCommand(cmd));
161 return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd].type] <= _settings_game.construction.command_pause_level;
165 * Prepare for calling a command proc.
166 * @param top_level Top level of command execution, i.e. command from a command.
167 * @param test Test run of command?
169 void CommandHelperBase::InternalDoBefore(bool top_level, bool test)
171 if (top_level) _cleared_object_areas.clear();
172 if (test) SetTownRatingTestMode(true);
176 * Process result after calling a command proc.
177 * @param[in,out] res Command result, may be modified.
178 * @param flags Command flags.
179 * @param top_level Top level of command execution, i.e. command from a command.
180 * @param test Test run of command?
182 void CommandHelperBase::InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test)
184 if (test) {
185 SetTownRatingTestMode(false);
187 if (res.Succeeded() && top_level && !(flags & DC_QUERY_COST) && !(flags & DC_BANKRUPT)) {
188 CheckCompanyHasMoney(res); // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
190 } else {
191 /* If top-level, subtract the money. */
192 if (res.Succeeded() && top_level && !(flags & DC_BANKRUPT)) {
193 SubtractMoneyFromCompany(res);
199 * Decide what to do with the command depending on current game state.
200 * @param cmd Command to execute.
201 * @param flags Command flags.
202 * @param tile Tile of command execution.
203 * @param err_message Message prefix to show on error.
204 * @param network_command Does this command come from the network?
205 * @return error state + do only cost estimation? + send to network only?
207 std::tuple<bool, bool, bool> CommandHelperBase::InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command)
209 /* Cost estimation is generally only done when the
210 * local user presses shift while doing something.
211 * However, in case of incoming network commands,
212 * map generation or the pause button we do want
213 * to execute. */
214 bool estimate_only = _shift_pressed && IsLocalCompany() && !_generating_world && !network_command && !(flags & CMD_NO_EST);
216 /* We're only sending the command, so don't do
217 * fancy things for 'success'. */
218 bool only_sending = _networking && !network_command;
220 if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd) && !estimate_only) {
221 ShowErrorMessage(err_message, STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
222 return { true, estimate_only, only_sending };
223 } else {
224 return { false, estimate_only, only_sending };
229 * Process result of executing a command, possibly displaying any error to the player.
230 * @param res Command result.
231 * @param tile Tile of command execution.
232 * @param estimate_only Is this just cost estimation?
233 * @param only_sending Was the command only sent to network?
234 * @param err_message Message prefix to show on error.
235 * @param my_cmd Is the command from this client?
237 void CommandHelperBase::InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd)
239 int x = TileX(tile) * TILE_SIZE;
240 int y = TileY(tile) * TILE_SIZE;
242 if (res.Failed()) {
243 /* Only show the error when it's for us. */
244 if (estimate_only || (IsLocalCompany() && err_message != 0 && my_cmd)) {
245 ShowErrorMessage(err_message, x, y, res);
247 } else if (estimate_only) {
248 ShowEstimatedCostOrIncome(res.GetCost(), x, y);
249 } else if (!only_sending && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
250 /* Only show the cost animation when we did actually
251 * execute the command, i.e. we're not sending it to
252 * the server, when it has cost the local company
253 * something. Furthermore in the editor there is no
254 * concept of cost, so don't show it there either. */
255 ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
259 /** Helper to make a desync log for a command. */
260 void CommandHelperBase::LogCommandExecution(Commands cmd, StringID err_message, const CommandDataBuffer &args, bool failed)
262 Debug(desync, 1, "{}: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {} ({})", failed ? "cmdf" : "cmd", (uint32_t)TimerGameEconomy::date.base(), TimerGameEconomy::date_fract, (int)_current_company, cmd, err_message, FormatArrayAsHex(args), GetCommandName(cmd));
266 * Prepare for the test run of a command proc call.
267 * @param cmd_flags Command flags.
268 * @param[in,out] cur_company Backup of current company at start of command execution.
269 * @return True if test run can go ahead, false on error.
271 bool CommandHelperBase::InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex, Backup<CompanyID> &cur_company)
273 /* Always execute server and spectator commands as spectator */
274 bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
276 /* If the company isn't valid it may only do server command or start a new company!
277 * The server will ditch any server commands a client sends to it, so effectively
278 * this guards the server from executing functions for an invalid company. */
279 if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
280 return false;
283 if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
285 /* Enter test mode. */
286 _cleared_object_areas.clear();
287 SetTownRatingTestMode(true);
288 BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
289 return true;
293 * Validate result of test run and prepare for real execution.
294 * @param cmd_flags Command flags.
295 * @param[in,out] res Command result of test run, may be modified.
296 * @param estimate_only Is this just cost estimation?
297 * @param network_command Does this command come from the network?
298 * @param[in,out] cur_company Backup of current company at start of command execution.
299 * @return True if test run can go ahead, false on error.
301 std::tuple<bool, bool, bool> CommandHelperBase::InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, [[maybe_unused]] Backup<CompanyID> &cur_company)
303 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
304 SetTownRatingTestMode(false);
306 /* Make sure we're not messing things up here. */
307 assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
309 /* If the command fails, we're doing an estimate
310 * or the player does not have enough money
311 * (unless it's a command where the test and
312 * execution phase might return different costs)
313 * we bail out here. */
314 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
315 if (res.Failed() || estimate_only || (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
316 return { true, !_networking || _generating_world || network_command, false };
319 bool send_net = _networking && !_generating_world && !network_command;
321 if (!send_net) {
322 /* Prepare for command execution. */
323 _cleared_object_areas.clear();
324 BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
327 return { false, _debug_desync_level >= 1, send_net };
331 * Process the result of a command test run and execution run.
332 * @param cmd Command that was executed.
333 * @param cmd_flags Command flags.
334 * @param res_test Command result of test run.
335 * @param tes_exec Command result of real run.
336 * @param extra_cash Additional cash required for successful command execution.
337 * @param tile Tile of command execution.
338 * @param[in,out] cur_company Backup of current company at start of command execution.
339 * @return Final command result.
341 CommandCost CommandHelperBase::InternalExecuteProcessResult(Commands cmd, CommandFlags cmd_flags, [[maybe_unused]] const CommandCost &res_test, const CommandCost &res_exec, Money extra_cash, TileIndex tile, Backup<CompanyID> &cur_company)
343 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
345 if (cmd == CMD_COMPANY_CTRL) {
346 cur_company.Trash();
347 /* We are a new company -> Switch to new local company.
348 * We were closed down -> Switch to spectator
349 * Some other company opened/closed down -> The outside function will switch back */
350 _current_company = _local_company;
351 } else {
352 /* Make sure nothing bad happened, like changing the current company. */
353 assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
354 cur_company.Restore();
357 /* If the test and execution can differ we have to check the
358 * return of the command. Otherwise we can check whether the
359 * test and execution have yielded the same result,
360 * i.e. cost and error state are the same. */
361 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
362 if (!test_and_exec_can_differ) {
363 assert(res_test.GetCost() == res_exec.GetCost() && res_test.Failed() == res_exec.Failed()); // sanity check
364 } else if (res_exec.Failed()) {
365 return res_exec;
368 /* If we're needing more money and we haven't done
369 * anything yet, ask for the money! */
370 if (extra_cash != 0 && res_exec.GetCost() == 0) {
371 /* It could happen we removed rail, thus gained money, and deleted something else.
372 * So make sure the signal buffer is empty even in this case */
373 UpdateSignalsInBuffer();
374 SetDParam(0, extra_cash);
375 return CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
378 /* update last build coordinate of company. */
379 if (tile != 0) {
380 Company *c = Company::GetIfValid(_current_company);
381 if (c != nullptr) c->last_build_coordinate = tile;
384 SubtractMoneyFromCompany(res_exec);
386 /* Record if there was a command issues during pause; ignore pause/other setting related changes. */
387 if (_pause_mode != PM_UNPAUSED && _command_proc_table[cmd].type != CMDT_SERVER_SETTING) _pause_mode |= PM_COMMAND_DURING_PAUSE;
389 /* update signals if needed */
390 UpdateSignalsInBuffer();
392 return res_exec;
397 * Adds the cost of the given command return value to this cost.
398 * Also takes a possible error message when it is set.
399 * @param ret The command to add the cost of.
401 void CommandCost::AddCost(const CommandCost &ret)
403 this->AddCost(ret.cost);
404 if (this->success && !ret.success) {
405 this->message = ret.message;
406 this->success = false;
411 * Values to put on the #TextRefStack for the error message.
412 * There is only one static instance of the array, just like there is only one
413 * instance of normal DParams.
415 uint32_t CommandCost::textref_stack[16];
418 * Activate usage of the NewGRF #TextRefStack for the error message.
419 * @param grffile NewGRF that provides the #TextRefStack
420 * @param num_registers number of entries to copy from the temporary NewGRF registers
422 void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
424 extern TemporaryStorageArray<int32_t, 0x110> _temp_store;
426 assert(num_registers < lengthof(textref_stack));
427 this->textref_stack_grffile = grffile;
428 this->textref_stack_size = num_registers;
429 for (uint i = 0; i < num_registers; i++) {
430 textref_stack[i] = _temp_store.GetValue(0x100 + i);