2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file command.cpp Handling of commands. */
|
2007-02-23 11:50:43 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "error.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "gui.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2010-08-18 17:06:45 +00:00
|
|
|
#include "network/network_type.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#include "genworld.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2011-02-07 22:22:20 +00:00
|
|
|
#include "texteff.hpp"
|
2008-01-09 17:47:05 +00:00
|
|
|
#include "town.h"
|
2023-04-24 15:56:01 +00:00
|
|
|
#include "timer/timer_game_calendar.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
2008-01-16 01:18:15 +00:00
|
|
|
#include "signal_func.h"
|
2010-06-05 13:32:42 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2010-09-03 21:50:51 +00:00
|
|
|
#include "object_base.h"
|
2021-10-05 20:02:27 +00:00
|
|
|
#include "autoreplace_cmd.h"
|
|
|
|
#include "company_cmd.h"
|
|
|
|
#include "depot_cmd.h"
|
|
|
|
#include "economy_cmd.h"
|
|
|
|
#include "engine_cmd.h"
|
|
|
|
#include "goal_cmd.h"
|
|
|
|
#include "group_cmd.h"
|
|
|
|
#include "industry_cmd.h"
|
2022-11-26 17:03:03 +00:00
|
|
|
#include "league_cmd.h"
|
2021-10-05 20:02:27 +00:00
|
|
|
#include "landscape_cmd.h"
|
|
|
|
#include "misc_cmd.h"
|
|
|
|
#include "news_cmd.h"
|
|
|
|
#include "object_cmd.h"
|
|
|
|
#include "order_cmd.h"
|
|
|
|
#include "rail_cmd.h"
|
|
|
|
#include "road_cmd.h"
|
|
|
|
#include "roadveh_cmd.h"
|
|
|
|
#include "settings_cmd.h"
|
|
|
|
#include "signs_cmd.h"
|
|
|
|
#include "station_cmd.h"
|
|
|
|
#include "story_cmd.h"
|
|
|
|
#include "subsidy_cmd.h"
|
|
|
|
#include "terraform_cmd.h"
|
|
|
|
#include "timetable_cmd.h"
|
|
|
|
#include "town_cmd.h"
|
|
|
|
#include "train_cmd.h"
|
|
|
|
#include "tree_cmd.h"
|
|
|
|
#include "tunnelbridge_cmd.h"
|
|
|
|
#include "vehicle_cmd.h"
|
|
|
|
#include "viewport_cmd.h"
|
|
|
|
#include "water_cmd.h"
|
|
|
|
#include "waypoint_cmd.h"
|
2021-10-28 21:48:26 +00:00
|
|
|
#include "misc/endian_buffer.hpp"
|
|
|
|
#include "string_func.h"
|
2021-10-05 20:02:27 +00:00
|
|
|
|
|
|
|
#include <array>
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2021-10-29 12:41:20 +00:00
|
|
|
int RecursiveCommandCounter::_counter = 0;
|
|
|
|
|
|
|
|
|
2021-10-05 20:02:27 +00:00
|
|
|
/**
|
|
|
|
* Define a command with the flags which belongs to it.
|
|
|
|
*
|
|
|
|
* This struct connects a command handler function with the flags created with
|
|
|
|
* the #CMD_AUTO, #CMD_OFFLINE and #CMD_SERVER values.
|
|
|
|
*/
|
|
|
|
struct CommandInfo {
|
|
|
|
const char *name; ///< A human readable name for the procedure
|
|
|
|
CommandFlags flags; ///< The (command) flags to that apply to this command
|
|
|
|
CommandType type; ///< The type of command.
|
|
|
|
};
|
|
|
|
/* Helpers to generate the master command table from the command traits. */
|
|
|
|
template <typename T>
|
2021-11-02 20:34:39 +00:00
|
|
|
inline constexpr CommandInfo CommandFromTrait() noexcept { return { T::name, T::flags, T::type }; };
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2021-10-05 20:02:27 +00:00
|
|
|
template<typename T, T... i>
|
|
|
|
inline constexpr auto MakeCommandsFromTraits(std::integer_sequence<T, i...>) noexcept {
|
|
|
|
return std::array<CommandInfo, sizeof...(i)>{{ CommandFromTrait<CommandTraits<static_cast<Commands>(i)>>()... }};
|
|
|
|
}
|
2010-04-11 10:11:26 +00:00
|
|
|
|
2007-09-10 15:21:14 +00:00
|
|
|
/**
|
|
|
|
* The master command table
|
|
|
|
*
|
|
|
|
* This table contains all possible CommandProc functions with
|
2010-03-14 12:39:24 +00:00
|
|
|
* the flags which belongs to it. The indices are the same
|
2007-09-10 15:21:14 +00:00
|
|
|
* as the value from the CMD_* enums.
|
|
|
|
*/
|
2021-10-05 20:02:27 +00:00
|
|
|
static constexpr auto _command_proc_table = MakeCommandsFromTraits(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{});
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-10 15:21:14 +00:00
|
|
|
/*!
|
2021-11-02 20:34:39 +00:00
|
|
|
* This function range-checks a cmd.
|
2007-09-10 15:21:14 +00:00
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param cmd The integer value of a command
|
2007-09-10 15:21:14 +00:00
|
|
|
* @return true if the command is valid (and got a CommandProc function)
|
|
|
|
*/
|
2021-10-05 20:02:27 +00:00
|
|
|
bool IsValidCommand(Commands cmd)
|
2005-01-05 14:39:48 +00:00
|
|
|
{
|
2021-11-02 20:34:39 +00:00
|
|
|
return cmd < _command_proc_table.size();
|
2005-01-05 14:39:48 +00:00
|
|
|
}
|
|
|
|
|
2007-09-10 15:21:14 +00:00
|
|
|
/*!
|
2009-01-07 13:26:48 +00:00
|
|
|
* This function mask the parameter with CMD_ID_MASK and returns
|
2007-09-10 15:21:14 +00:00
|
|
|
* the flags which belongs to the given command.
|
|
|
|
*
|
|
|
|
* @param cmd The integer value of the command
|
|
|
|
* @return The flags for this command
|
|
|
|
*/
|
2021-10-03 19:13:32 +00:00
|
|
|
CommandFlags GetCommandFlags(Commands cmd)
|
2006-06-10 08:37:41 +00:00
|
|
|
{
|
2008-10-14 18:49:21 +00:00
|
|
|
assert(IsValidCommand(cmd));
|
|
|
|
|
2021-10-03 19:13:32 +00:00
|
|
|
return _command_proc_table[cmd].flags;
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2005-05-14 19:25:18 +00:00
|
|
|
|
2010-04-11 10:11:26 +00:00
|
|
|
/*!
|
|
|
|
* This function mask the parameter with CMD_ID_MASK and returns
|
|
|
|
* the name which belongs to the given command.
|
|
|
|
*
|
|
|
|
* @param cmd The integer value of the command
|
|
|
|
* @return The name for this command
|
|
|
|
*/
|
2021-10-03 19:13:32 +00:00
|
|
|
const char *GetCommandName(Commands cmd)
|
2010-04-11 10:11:26 +00:00
|
|
|
{
|
|
|
|
assert(IsValidCommand(cmd));
|
|
|
|
|
2021-10-03 19:13:32 +00:00
|
|
|
return _command_proc_table[cmd].name;
|
2010-04-11 10:11:26 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:08:35 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the command is allowed while the game is paused.
|
|
|
|
* @param cmd The command to check.
|
|
|
|
* @return True if the command is allowed while paused, false otherwise.
|
|
|
|
*/
|
2021-10-05 20:02:27 +00:00
|
|
|
bool IsCommandAllowedWhilePaused(Commands cmd)
|
2010-12-07 21:08:35 +00:00
|
|
|
{
|
|
|
|
/* Lookup table for the command types that are allowed for a given pause level setting. */
|
|
|
|
static const int command_type_lookup[] = {
|
|
|
|
CMDPL_ALL_ACTIONS, ///< CMDT_LANDSCAPE_CONSTRUCTION
|
|
|
|
CMDPL_NO_LANDSCAPING, ///< CMDT_VEHICLE_CONSTRUCTION
|
|
|
|
CMDPL_NO_LANDSCAPING, ///< CMDT_MONEY_MANAGEMENT
|
|
|
|
CMDPL_NO_CONSTRUCTION, ///< CMDT_VEHICLE_MANAGEMENT
|
|
|
|
CMDPL_NO_CONSTRUCTION, ///< CMDT_ROUTE_MANAGEMENT
|
|
|
|
CMDPL_NO_CONSTRUCTION, ///< CMDT_OTHER_MANAGEMENT
|
|
|
|
CMDPL_NO_CONSTRUCTION, ///< CMDT_COMPANY_SETTING
|
|
|
|
CMDPL_NO_ACTIONS, ///< CMDT_SERVER_SETTING
|
2011-02-07 22:23:37 +00:00
|
|
|
CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
|
2010-12-07 21:08:35 +00:00
|
|
|
};
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(lengthof(command_type_lookup) == CMDT_END);
|
2010-12-07 21:08:35 +00:00
|
|
|
|
|
|
|
assert(IsValidCommand(cmd));
|
2021-10-03 19:13:32 +00:00
|
|
|
return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd].type] <= _settings_game.construction.command_pause_level;
|
2010-12-07 21:08:35 +00:00
|
|
|
}
|
|
|
|
|
2007-09-10 15:21:14 +00:00
|
|
|
/*!
|
|
|
|
* This functions returns the money which can be used to execute a command.
|
2008-09-30 20:39:50 +00:00
|
|
|
* This is either the money of the current company or INT64_MAX if there
|
|
|
|
* is no such a company "at the moment" like the server itself.
|
2007-09-10 15:21:14 +00:00
|
|
|
*
|
2008-09-30 20:39:50 +00:00
|
|
|
* @return The available money of a company or INT64_MAX
|
2007-09-10 15:21:14 +00:00
|
|
|
*/
|
2007-06-21 14:32:27 +00:00
|
|
|
Money GetAvailableMoneyForCommand()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID company = _current_company;
|
2009-05-17 01:00:56 +00:00
|
|
|
if (!Company::IsValidID(company)) return INT64_MAX;
|
2009-05-16 23:34:14 +00:00
|
|
|
return Company::Get(company)->money;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-08 14:55:28 +00:00
|
|
|
|
2021-10-29 23:31:46 +00:00
|
|
|
/**
|
|
|
|
* Prepare for calling a command proc.
|
|
|
|
* @param top_level Top level of command execution, i.e. command from a command.
|
|
|
|
* @param test Test run of command?
|
|
|
|
*/
|
|
|
|
void CommandHelperBase::InternalDoBefore(bool top_level, bool test)
|
|
|
|
{
|
|
|
|
if (top_level) _cleared_object_areas.clear();
|
|
|
|
if (test) SetTownRatingTestMode(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process result after calling a command proc.
|
|
|
|
* @param[in,out] res Command result, may be modified.
|
|
|
|
* @param flags Command flags.
|
|
|
|
* @param top_level Top level of command execution, i.e. command from a command.
|
|
|
|
* @param test Test run of command?
|
|
|
|
*/
|
|
|
|
void CommandHelperBase::InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test)
|
|
|
|
{
|
|
|
|
if (test) {
|
|
|
|
SetTownRatingTestMode(false);
|
|
|
|
|
|
|
|
if (res.Succeeded() && top_level && !(flags & DC_QUERY_COST) && !(flags & DC_BANKRUPT)) {
|
|
|
|
CheckCompanyHasMoney(res); // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If top-level, subtract the money. */
|
|
|
|
if (res.Succeeded() && top_level && !(flags & DC_BANKRUPT)) {
|
|
|
|
SubtractMoneyFromCompany(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 18:39:09 +00:00
|
|
|
/**
|
|
|
|
* Decide what to do with the command depending on current game state.
|
|
|
|
* @param cmd Command to execute.
|
|
|
|
* @param flags Command flags.
|
|
|
|
* @param tile Tile of command execution.
|
|
|
|
* @param err_message Message prefix to show on error.
|
|
|
|
* @param network_command Does this command come from the network?
|
|
|
|
* @return error state + do only cost estimation? + send to network only?
|
2007-09-10 15:21:14 +00:00
|
|
|
*/
|
2021-10-31 18:39:09 +00:00
|
|
|
std::tuple<bool, bool, bool> CommandHelperBase::InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-01-11 20:39:38 +00:00
|
|
|
/* Cost estimation is generally only done when the
|
2019-09-29 20:27:32 +00:00
|
|
|
* local user presses shift while doing something.
|
2010-01-11 20:39:38 +00:00
|
|
|
* However, in case of incoming network commands,
|
2011-08-21 12:46:46 +00:00
|
|
|
* map generation or the pause button we do want
|
2010-01-11 20:39:38 +00:00
|
|
|
* to execute. */
|
2021-10-31 18:39:09 +00:00
|
|
|
bool estimate_only = _shift_pressed && IsLocalCompany() && !_generating_world && !network_command && !(flags & CMD_NO_EST);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-11 20:39:38 +00:00
|
|
|
/* We're only sending the command, so don't do
|
|
|
|
* fancy things for 'success'. */
|
2021-10-03 19:13:32 +00:00
|
|
|
bool only_sending = _networking && !network_command;
|
2009-01-07 13:31:09 +00:00
|
|
|
|
2020-06-14 11:04:26 +00:00
|
|
|
if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd) && !estimate_only) {
|
2021-10-31 18:39:09 +00:00
|
|
|
ShowErrorMessage(err_message, STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
|
|
|
|
return { true, estimate_only, only_sending };
|
|
|
|
} else {
|
|
|
|
return { false, estimate_only, only_sending };
|
2010-12-07 21:09:30 +00:00
|
|
|
}
|
2021-10-31 18:39:09 +00:00
|
|
|
}
|
2010-12-07 21:09:30 +00:00
|
|
|
|
2021-10-31 18:39:09 +00:00
|
|
|
/**
|
|
|
|
* Process result of executing a command, possibly displaying any error to the player.
|
|
|
|
* @param res Command result.
|
|
|
|
* @param tile Tile of command execution.
|
|
|
|
* @param estimate_only Is this just cost estimation?
|
|
|
|
* @param only_sending Was the command only sent to network?
|
|
|
|
* @param err_message Message prefix to show on error.
|
|
|
|
* @param my_cmd Is the command from this client?
|
|
|
|
*/
|
|
|
|
void CommandHelperBase::InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd)
|
|
|
|
{
|
|
|
|
int x = TileX(tile) * TILE_SIZE;
|
|
|
|
int y = TileY(tile) * TILE_SIZE;
|
2010-08-18 17:06:45 +00:00
|
|
|
|
2010-01-18 22:57:21 +00:00
|
|
|
if (res.Failed()) {
|
2010-01-11 20:39:38 +00:00
|
|
|
/* Only show the error when it's for us. */
|
2021-10-03 19:13:32 +00:00
|
|
|
if (estimate_only || (IsLocalCompany() && err_message != 0 && my_cmd)) {
|
2018-11-19 18:59:25 +00:00
|
|
|
ShowErrorMessage(err_message, x, y, res);
|
2010-01-11 20:39:38 +00:00
|
|
|
}
|
|
|
|
} else if (estimate_only) {
|
|
|
|
ShowEstimatedCostOrIncome(res.GetCost(), x, y);
|
2022-05-05 18:51:56 +00:00
|
|
|
} else if (!only_sending && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
|
2010-01-11 20:39:38 +00:00
|
|
|
/* Only show the cost animation when we did actually
|
|
|
|
* execute the command, i.e. we're not sending it to
|
|
|
|
* the server, when it has cost the local company
|
|
|
|
* something. Furthermore in the editor there is no
|
|
|
|
* concept of cost, so don't show it there either. */
|
2011-11-04 10:18:13 +00:00
|
|
|
ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
|
2010-01-11 20:39:38 +00:00
|
|
|
}
|
2021-10-28 21:48:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
/** Helper to make a desync log for a command. */
|
|
|
|
void CommandHelperBase::LogCommandExecution(Commands cmd, StringID err_message, TileIndex tile, const CommandDataBuffer &args, bool failed)
|
2021-10-28 21:48:26 +00:00
|
|
|
{
|
2023-04-24 15:56:01 +00:00
|
|
|
Debug(desync, 1, "{}: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", failed ? "cmdf" : "cmd", TimerGameCalendar::date, TimerGameCalendar::date_fract, (int)_current_company, cmd, err_message, tile, FormatArrayAsHex(args), GetCommandName(cmd));
|
2021-10-28 21:48:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
/**
|
|
|
|
* Prepare for the test run of a command proc call.
|
|
|
|
* @param cmd_flags Command flags.
|
|
|
|
* @param tile Tile of command execution.
|
|
|
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
|
|
|
* @return True if test run can go ahead, false on error.
|
2010-01-11 20:39:38 +00:00
|
|
|
*/
|
2021-11-01 22:07:27 +00:00
|
|
|
bool CommandHelperBase::InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup<CompanyID> &cur_company)
|
2010-01-11 20:39:38 +00:00
|
|
|
{
|
2009-05-26 21:24:11 +00:00
|
|
|
/* Always execute server and spectator commands as spectator */
|
2010-06-23 14:38:17 +00:00
|
|
|
bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
|
2009-05-26 21:55:49 +00:00
|
|
|
|
2009-05-26 19:23:02 +00:00
|
|
|
/* If the company isn't valid it may only do server command or start a new company!
|
|
|
|
* The server will ditch any server commands a client sends to it, so effectively
|
|
|
|
* this guards the server from executing functions for an invalid company. */
|
2011-12-19 20:50:36 +00:00
|
|
|
if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
|
2021-11-01 22:07:27 +00:00
|
|
|
return false;
|
2009-05-26 19:23:02 +00:00
|
|
|
}
|
2009-02-04 16:57:40 +00:00
|
|
|
|
2010-06-05 13:32:42 +00:00
|
|
|
if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
|
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
/* Enter test mode. */
|
2018-09-20 22:44:14 +00:00
|
|
|
_cleared_object_areas.clear();
|
2012-01-09 22:21:45 +00:00
|
|
|
SetTownRatingTestMode(true);
|
2014-02-23 22:03:08 +00:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
|
2021-11-01 22:07:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate result of test run and prepare for real execution.
|
|
|
|
* @param cmd_flags Command flags.
|
|
|
|
* @param[in,out] res Command result of test run, may be modified.
|
|
|
|
* @param estimate_only Is this just cost estimation?
|
|
|
|
* @param network_command Does this command come from the network?
|
|
|
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
|
|
|
* @return True if test run can go ahead, false on error.
|
|
|
|
*/
|
|
|
|
std::tuple<bool, bool, bool> CommandHelperBase::InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, Backup<CompanyID> &cur_company)
|
|
|
|
{
|
2014-02-23 22:03:08 +00:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
|
2012-01-09 22:21:45 +00:00
|
|
|
SetTownRatingTestMode(false);
|
|
|
|
|
|
|
|
/* Make sure we're not messing things up here. */
|
2021-11-01 22:07:27 +00:00
|
|
|
assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
2012-01-09 22:21:45 +00:00
|
|
|
|
|
|
|
/* If the command fails, we're doing an estimate
|
|
|
|
* or the player does not have enough money
|
|
|
|
* (unless it's a command where the test and
|
|
|
|
* execution phase might return different costs)
|
|
|
|
* we bail out here. */
|
2021-11-01 22:07:27 +00:00
|
|
|
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
|
|
|
if (res.Failed() || estimate_only || (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
|
|
|
|
return { true, !_networking || _generating_world || network_command, false };
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
bool send_net = _networking && !_generating_world && !network_command;
|
2010-01-11 20:39:38 +00:00
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
if (!send_net) {
|
|
|
|
/* Prepare for command execution. */
|
|
|
|
_cleared_object_areas.clear();
|
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
return { false, _debug_desync_level >= 1, send_net };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the result of a command test run and execution run.
|
|
|
|
* @param cmd Command that was executed.
|
|
|
|
* @param cmd_flags Command flags.
|
|
|
|
* @param res_test Command result of test run.
|
|
|
|
* @param tes_exec Command result of real run.
|
2021-11-30 23:17:05 +00:00
|
|
|
* @param extra_cash Additional cash required for successful command execution.
|
2021-11-01 22:07:27 +00:00
|
|
|
* @param tile Tile of command execution.
|
|
|
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
|
|
|
* @return Final command result.
|
|
|
|
*/
|
2021-11-30 23:17:05 +00:00
|
|
|
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)
|
2021-11-01 22:07:27 +00:00
|
|
|
{
|
2014-02-23 22:03:08 +00:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2021-10-03 19:13:32 +00:00
|
|
|
if (cmd == CMD_COMPANY_CTRL) {
|
2010-06-05 13:32:42 +00:00
|
|
|
cur_company.Trash();
|
|
|
|
/* We are a new company -> Switch to new local company.
|
|
|
|
* We were closed down -> Switch to spectator
|
|
|
|
* Some other company opened/closed down -> The outside function will switch back */
|
|
|
|
_current_company = _local_company;
|
|
|
|
} else {
|
|
|
|
/* Make sure nothing bad happened, like changing the current company. */
|
2021-11-01 22:07:27 +00:00
|
|
|
assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
2010-06-05 13:32:42 +00:00
|
|
|
cur_company.Restore();
|
|
|
|
}
|
2009-02-07 18:09:30 +00:00
|
|
|
|
2012-01-09 22:21:45 +00:00
|
|
|
/* If the test and execution can differ we have to check the
|
|
|
|
* return of the command. Otherwise we can check whether the
|
|
|
|
* test and execution have yielded the same result,
|
|
|
|
* i.e. cost and error state are the same. */
|
2021-11-01 22:07:27 +00:00
|
|
|
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
2012-01-09 22:21:45 +00:00
|
|
|
if (!test_and_exec_can_differ) {
|
2021-11-01 22:07:27 +00:00
|
|
|
assert(res_test.GetCost() == res_exec.GetCost() && res_test.Failed() == res_exec.Failed()); // sanity check
|
|
|
|
} else if (res_exec.Failed()) {
|
|
|
|
return res_exec;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 20:39:38 +00:00
|
|
|
/* If we're needing more money and we haven't done
|
|
|
|
* anything yet, ask for the money! */
|
2021-11-30 23:17:05 +00:00
|
|
|
if (extra_cash != 0 && res_exec.GetCost() == 0) {
|
2010-04-17 20:21:33 +00:00
|
|
|
/* It could happen we removed rail, thus gained money, and deleted something else.
|
|
|
|
* So make sure the signal buffer is empty even in this case */
|
|
|
|
UpdateSignalsInBuffer();
|
2021-11-30 23:17:05 +00:00
|
|
|
SetDParam(0, extra_cash);
|
2021-10-29 12:41:20 +00:00
|
|
|
return CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 20:39:38 +00:00
|
|
|
/* update last build coordinate of company. */
|
|
|
|
if (tile != 0) {
|
|
|
|
Company *c = Company::GetIfValid(_current_company);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (c != nullptr) c->last_build_coordinate = tile;
|
2006-03-12 10:15:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
SubtractMoneyFromCompany(res_exec);
|
2010-01-11 20:39:38 +00:00
|
|
|
|
2023-04-27 15:21:29 +00:00
|
|
|
/* Record if there was a command issues during pause; ignore pause/other setting related changes. */
|
|
|
|
if (_pause_mode != PM_UNPAUSED && _command_proc_table[cmd].type != CMDT_SERVER_SETTING) _pause_mode |= PM_COMMAND_DURING_PAUSE;
|
|
|
|
|
2010-01-11 20:39:38 +00:00
|
|
|
/* update signals if needed */
|
|
|
|
UpdateSignalsInBuffer();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2021-11-01 22:07:27 +00:00
|
|
|
return res_exec;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-07 20:02:36 +00:00
|
|
|
|
|
|
|
|
2010-02-14 15:30:08 +00:00
|
|
|
/**
|
|
|
|
* Adds the cost of the given command return value to this cost.
|
|
|
|
* Also takes a possible error message when it is set.
|
|
|
|
* @param ret The command to add the cost of.
|
|
|
|
*/
|
2010-02-14 15:44:21 +00:00
|
|
|
void CommandCost::AddCost(const CommandCost &ret)
|
2008-04-07 20:02:36 +00:00
|
|
|
{
|
|
|
|
this->AddCost(ret.cost);
|
|
|
|
if (this->success && !ret.success) {
|
|
|
|
this->message = ret.message;
|
|
|
|
this->success = false;
|
|
|
|
}
|
|
|
|
}
|
2011-07-03 14:32:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Values to put on the #TextRefStack for the error message.
|
|
|
|
* There is only one static instance of the array, just like there is only one
|
|
|
|
* instance of normal DParams.
|
|
|
|
*/
|
|
|
|
uint32 CommandCost::textref_stack[16];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate usage of the NewGRF #TextRefStack for the error message.
|
2014-01-12 18:00:39 +00:00
|
|
|
* @param grffile NewGRF that provides the #TextRefStack
|
|
|
|
* @param num_registers number of entries to copy from the temporary NewGRF registers
|
2011-07-03 14:32:15 +00:00
|
|
|
*/
|
2014-01-12 18:00:39 +00:00
|
|
|
void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
|
2011-07-03 14:32:15 +00:00
|
|
|
{
|
|
|
|
extern TemporaryStorageArray<int32, 0x110> _temp_store;
|
|
|
|
|
|
|
|
assert(num_registers < lengthof(textref_stack));
|
2014-01-12 18:00:39 +00:00
|
|
|
this->textref_stack_grffile = grffile;
|
2011-07-03 14:32:15 +00:00
|
|
|
this->textref_stack_size = num_registers;
|
|
|
|
for (uint i = 0; i < num_registers; i++) {
|
|
|
|
textref_stack[i] = _temp_store.GetValue(0x100 + i);
|
|
|
|
}
|
|
|
|
}
|