2009-08-21 22:21:05 +02: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 17:11:33 +02:00
|
|
|
/** @file command.cpp Handling of commands. */
|
2007-02-23 12:50:43 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2007-04-12 15:07:15 +02:00
|
|
|
#include "landscape.h"
|
2011-12-10 14:54:10 +01:00
|
|
|
#include "error.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "gui.h"
|
2007-12-21 22:50:46 +01:00
|
|
|
#include "command_func.h"
|
2010-08-18 19:06:45 +02:00
|
|
|
#include "network/network_type.h"
|
2007-01-02 18:34:03 +01: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 12:00:30 +02:00
|
|
|
#include "genworld.h"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2011-02-07 23:22:20 +01:00
|
|
|
#include "texteff.hpp"
|
2008-01-09 18:47:05 +01:00
|
|
|
#include "town.h"
|
2008-01-11 01:30:32 +01:00
|
|
|
#include "date_func.h"
|
2008-09-30 22:51:04 +02:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
2008-01-16 02:18:15 +01:00
|
|
|
#include "signal_func.h"
|
2010-06-05 15:32:42 +02:00
|
|
|
#include "core/backup_type.hpp"
|
2010-09-03 23:50:51 +02:00
|
|
|
#include "object_base.h"
|
2021-10-05 22:02:27 +02: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"
|
|
|
|
#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 23:48:26 +02:00
|
|
|
#include "misc/endian_buffer.hpp"
|
|
|
|
#include "string_func.h"
|
2021-10-05 22:02:27 +02:00
|
|
|
|
|
|
|
#include <array>
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2021-10-29 14:41:20 +02:00
|
|
|
int RecursiveCommandCounter::_counter = 0;
|
|
|
|
|
|
|
|
|
2021-10-05 22:02:27 +02: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 {
|
|
|
|
CommandProc *proc; ///< The procedure to actually executing
|
|
|
|
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. */
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2021-10-05 22:02:27 +02:00
|
|
|
template <typename T>
|
|
|
|
inline constexpr CommandInfo CommandFromTrait() noexcept { return { T::proc, T::name, T::flags, T::type }; };
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2021-10-05 22:02:27 +02: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 12:11:26 +02:00
|
|
|
|
2007-09-10 17:21:14 +02:00
|
|
|
/**
|
|
|
|
* The master command table
|
|
|
|
*
|
|
|
|
* This table contains all possible CommandProc functions with
|
2010-03-14 13:39:24 +01:00
|
|
|
* the flags which belongs to it. The indices are the same
|
2007-09-10 17:21:14 +02:00
|
|
|
* as the value from the CMD_* enums.
|
|
|
|
*/
|
2021-10-05 22:02:27 +02:00
|
|
|
static constexpr auto _command_proc_table = MakeCommandsFromTraits(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{});
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-09-10 17:21:14 +02:00
|
|
|
/*!
|
2019-04-10 23:07:06 +02:00
|
|
|
* This function range-checks a cmd, and checks if the cmd is not nullptr
|
2007-09-10 17:21:14 +02:00
|
|
|
*
|
2013-01-08 23:46:42 +01:00
|
|
|
* @param cmd The integer value of a command
|
2007-09-10 17:21:14 +02:00
|
|
|
* @return true if the command is valid (and got a CommandProc function)
|
|
|
|
*/
|
2021-10-05 22:02:27 +02:00
|
|
|
bool IsValidCommand(Commands cmd)
|
2005-01-05 15:39:48 +01:00
|
|
|
{
|
2021-10-05 22:02:27 +02:00
|
|
|
return cmd < _command_proc_table.size() && _command_proc_table[cmd].proc != nullptr;
|
2005-01-05 15:39:48 +01:00
|
|
|
}
|
|
|
|
|
2007-09-10 17:21:14 +02:00
|
|
|
/*!
|
2009-01-07 14:26:48 +01:00
|
|
|
* This function mask the parameter with CMD_ID_MASK and returns
|
2007-09-10 17:21:14 +02: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 21:13:32 +02:00
|
|
|
CommandFlags GetCommandFlags(Commands cmd)
|
2006-06-10 10:37:41 +02:00
|
|
|
{
|
2008-10-14 20:49:21 +02:00
|
|
|
assert(IsValidCommand(cmd));
|
|
|
|
|
2021-10-03 21:13:32 +02:00
|
|
|
return _command_proc_table[cmd].flags;
|
2006-06-10 10:37:41 +02:00
|
|
|
}
|
2005-05-14 21:25:18 +02:00
|
|
|
|
2010-04-11 12:11:26 +02: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 21:13:32 +02:00
|
|
|
const char *GetCommandName(Commands cmd)
|
2010-04-11 12:11:26 +02:00
|
|
|
{
|
|
|
|
assert(IsValidCommand(cmd));
|
|
|
|
|
2021-10-03 21:13:32 +02:00
|
|
|
return _command_proc_table[cmd].name;
|
2010-04-11 12:11:26 +02:00
|
|
|
}
|
|
|
|
|
2010-12-07 22:08:35 +01: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 22:02:27 +02:00
|
|
|
bool IsCommandAllowedWhilePaused(Commands cmd)
|
2010-12-07 22:08:35 +01: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 23:23:37 +01:00
|
|
|
CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
|
2010-12-07 22:08:35 +01:00
|
|
|
};
|
2020-12-27 11:44:22 +01:00
|
|
|
static_assert(lengthof(command_type_lookup) == CMDT_END);
|
2010-12-07 22:08:35 +01:00
|
|
|
|
|
|
|
assert(IsValidCommand(cmd));
|
2021-10-03 21:13:32 +02:00
|
|
|
return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd].type] <= _settings_game.construction.command_pause_level;
|
2010-12-07 22:08:35 +01:00
|
|
|
}
|
|
|
|
|
2007-09-10 17:21:14 +02:00
|
|
|
/*!
|
|
|
|
* This functions returns the money which can be used to execute a command.
|
2008-09-30 22:39:50 +02: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 17:21:14 +02:00
|
|
|
*
|
2008-09-30 22:39:50 +02:00
|
|
|
* @return The available money of a company or INT64_MAX
|
2007-09-10 17:21:14 +02:00
|
|
|
*/
|
2007-06-21 16:32:27 +02:00
|
|
|
Money GetAvailableMoneyForCommand()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2008-09-30 22:39:50 +02:00
|
|
|
CompanyID company = _current_company;
|
2009-05-17 03:00:56 +02:00
|
|
|
if (!Company::IsValidID(company)) return INT64_MAX;
|
2009-05-17 01:34:14 +02:00
|
|
|
return Company::Get(company)->money;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-01-08 15:55:28 +01:00
|
|
|
|
2021-10-30 01:31:46 +02: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 19:39:09 +01: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 17:21:14 +02:00
|
|
|
*/
|
2021-10-31 19:39:09 +01:00
|
|
|
std::tuple<bool, bool, bool> CommandHelperBase::InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2010-01-11 21:39:38 +01:00
|
|
|
/* Cost estimation is generally only done when the
|
2019-09-29 22:27:32 +02:00
|
|
|
* local user presses shift while doing something.
|
2010-01-11 21:39:38 +01:00
|
|
|
* However, in case of incoming network commands,
|
2011-08-21 14:46:46 +02:00
|
|
|
* map generation or the pause button we do want
|
2010-01-11 21:39:38 +01:00
|
|
|
* to execute. */
|
2021-10-31 19:39:09 +01:00
|
|
|
bool estimate_only = _shift_pressed && IsLocalCompany() && !_generating_world && !network_command && !(flags & CMD_NO_EST);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/* We're only sending the command, so don't do
|
|
|
|
* fancy things for 'success'. */
|
2021-10-03 21:13:32 +02:00
|
|
|
bool only_sending = _networking && !network_command;
|
2009-01-07 14:31:09 +01:00
|
|
|
|
2020-06-14 13:04:26 +02:00
|
|
|
if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd) && !estimate_only) {
|
2021-10-31 19:39:09 +01: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 22:09:30 +01:00
|
|
|
}
|
2021-10-31 19:39:09 +01:00
|
|
|
}
|
2010-12-07 22:09:30 +01:00
|
|
|
|
2021-10-31 19:39:09 +01: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 19:06:45 +02:00
|
|
|
|
2010-01-18 23:57:21 +01:00
|
|
|
if (res.Failed()) {
|
2010-01-11 21:39:38 +01:00
|
|
|
/* Only show the error when it's for us. */
|
2021-10-03 21:13:32 +02:00
|
|
|
if (estimate_only || (IsLocalCompany() && err_message != 0 && my_cmd)) {
|
|
|
|
ShowErrorMessage(err_message, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack());
|
2010-01-11 21:39:38 +01:00
|
|
|
}
|
|
|
|
} else if (estimate_only) {
|
|
|
|
ShowEstimatedCostOrIncome(res.GetCost(), x, y);
|
|
|
|
} else if (!only_sending && res.GetCost() != 0 && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
|
|
|
|
/* 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 11:18:13 +01:00
|
|
|
ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
|
2010-01-11 21:39:38 +01:00
|
|
|
}
|
2021-10-28 23:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper to format command parameters into a hex string. */
|
|
|
|
static std::string CommandParametersToHexString(TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
|
|
|
|
{
|
|
|
|
return FormatArrayAsHex(EndianBufferWriter<>::FromValue(std::make_tuple(tile, p1, p2, text)));
|
|
|
|
}
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/*!
|
|
|
|
* Helper function for the toplevel network safe docommand function for the current company.
|
|
|
|
*
|
|
|
|
* @param cmd The command to execute (a CMD_* value)
|
2021-10-03 21:13:32 +02:00
|
|
|
* @param err_message Message prefix to show on error
|
2010-01-11 21:39:38 +01:00
|
|
|
* @param callback A callback function to call after the command is finished
|
|
|
|
* @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
|
|
|
|
* @param estimate_only whether to give only the estimate or also execute the command
|
2021-10-03 17:15:48 +02:00
|
|
|
* @param tile The tile to perform a command on (see #CommandProc)
|
|
|
|
* @param p1 Additional data for the command (see #CommandProc)
|
|
|
|
* @param p2 Additional data for the command (see #CommandProc)
|
|
|
|
* @param text The text to pass
|
2010-01-11 21:39:38 +01:00
|
|
|
* @return the command cost of this function.
|
|
|
|
*/
|
2021-10-03 21:13:32 +02:00
|
|
|
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)
|
2010-01-11 21:39:38 +01:00
|
|
|
{
|
2021-10-29 14:41:20 +02:00
|
|
|
RecursiveCommandCounter counter{};
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/* Prevent recursion; it gives a mess over the network */
|
2021-10-29 14:41:20 +02:00
|
|
|
assert(counter.IsTopLevel());
|
2010-01-11 21:39:38 +01:00
|
|
|
|
|
|
|
/* Reset the state. */
|
2004-08-09 19:04:08 +02:00
|
|
|
_additional_cash_required = 0;
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/* Get pointer to command handler */
|
2021-10-05 22:02:27 +02:00
|
|
|
assert(cmd < _command_proc_table.size());
|
2021-10-03 21:13:32 +02:00
|
|
|
CommandProc *proc = _command_proc_table[cmd].proc;
|
2010-01-11 21:39:38 +01:00
|
|
|
/* Shouldn't happen, but you never know when someone adds
|
|
|
|
* NULLs to the _command_proc_table. */
|
2019-04-10 23:07:06 +02:00
|
|
|
assert(proc != nullptr);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-01-07 14:31:09 +01:00
|
|
|
/* Command flags are used internally */
|
2011-11-14 21:38:56 +01:00
|
|
|
CommandFlags cmd_flags = GetCommandFlags(cmd);
|
2009-01-12 16:27:39 +01:00
|
|
|
/* Flags get send to the DoCommand */
|
2009-02-09 22:20:05 +01:00
|
|
|
DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
|
2009-01-07 14:31:09 +01:00
|
|
|
|
2010-08-18 19:06:45 +02:00
|
|
|
/* Make sure p2 is properly set to a ClientID. */
|
|
|
|
assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
|
|
|
|
|
2009-01-21 03:31:55 +01:00
|
|
|
/* Do not even think about executing out-of-bounds tile-commands */
|
2021-10-29 14:41:20 +02:00
|
|
|
if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return CMD_ERROR;
|
2009-01-21 03:31:55 +01:00
|
|
|
|
2009-05-26 23:24:11 +02:00
|
|
|
/* Always execute server and spectator commands as spectator */
|
2010-06-23 16:38:17 +02:00
|
|
|
bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
|
2009-05-26 23:55:49 +02:00
|
|
|
|
2009-05-26 21:23:02 +02: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 21:50:36 +01:00
|
|
|
if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
|
2021-10-29 14:41:20 +02:00
|
|
|
return CMD_ERROR;
|
2009-05-26 21:23:02 +02:00
|
|
|
}
|
2009-02-04 17:57:40 +01:00
|
|
|
|
2019-04-22 10:10:04 +02:00
|
|
|
Backup<CompanyID> cur_company(_current_company, FILE_LINE);
|
2010-06-05 15:32:42 +02:00
|
|
|
if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
|
|
|
|
2012-01-09 23:21:45 +01:00
|
|
|
/* Test the command. */
|
2018-09-21 00:44:14 +02:00
|
|
|
_cleared_object_areas.clear();
|
2012-01-09 23:21:45 +01:00
|
|
|
SetTownRatingTestMode(true);
|
2014-02-23 23:03:08 +01:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
|
2021-10-10 02:08:52 +02:00
|
|
|
CommandCost res = proc(flags, tile, p1, p2, text);
|
2014-02-23 23:03:08 +01:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
|
2012-01-09 23:21:45 +01:00
|
|
|
SetTownRatingTestMode(false);
|
|
|
|
|
|
|
|
/* Make sure we're not messing things up here. */
|
|
|
|
assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (res.Failed() || estimate_only ||
|
|
|
|
(!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
|
2021-10-03 21:13:32 +02:00
|
|
|
if (!_networking || _generating_world || network_command) {
|
2012-01-14 20:44:25 +01:00
|
|
|
/* Log the failed command as well. Just to be able to be find
|
|
|
|
* causes of desyncs due to bad command test implementations. */
|
2021-10-28 23:48:26 +02:00
|
|
|
Debug(desync, 1, "cmdf: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", _date, _date_fract, (int)_current_company, cmd, err_message, tile, CommandParametersToHexString(tile, p1, p2, text), GetCommandName(cmd));
|
2012-01-14 20:44:25 +01:00
|
|
|
}
|
2012-01-09 23:21:45 +01:00
|
|
|
cur_company.Restore();
|
2021-10-29 14:41:20 +02:00
|
|
|
return res;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2009-05-26 21:23:02 +02:00
|
|
|
/*
|
|
|
|
* If we are in network, and the command is not from the network
|
2005-05-17 22:52:52 +02:00
|
|
|
* send it to the command-queue and abort execution
|
|
|
|
*/
|
2021-10-03 21:13:32 +02:00
|
|
|
if (_networking && !_generating_world && !network_command) {
|
|
|
|
NetworkSendCommand(cmd, err_message, callback, _current_company, tile, p1, p2, text);
|
2010-06-05 15:32:42 +02:00
|
|
|
cur_company.Restore();
|
2010-01-11 21:39:38 +01:00
|
|
|
|
|
|
|
/* Don't return anything special here; no error, no costs.
|
|
|
|
* This way it's not handled by DoCommand and only the
|
|
|
|
* actual execution of the command causes messages. Also
|
|
|
|
* reset the storages as we've not executed the command. */
|
2021-10-29 14:41:20 +02:00
|
|
|
return CommandCost();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2021-10-28 23:48:26 +02:00
|
|
|
Debug(desync, 1, "cmd: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", _date, _date_fract, (int)_current_company, cmd, err_message, tile, CommandParametersToHexString(tile, p1, p2, text), GetCommandName(cmd));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-04 18:11:03 +01:00
|
|
|
/* Actually try and execute the command. If no cost-type is given
|
|
|
|
* use the construction one */
|
2018-09-21 00:44:14 +02:00
|
|
|
_cleared_object_areas.clear();
|
2014-02-23 23:03:08 +01:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
2021-10-10 02:08:52 +02:00
|
|
|
CommandCost res2 = proc(flags | DC_EXEC, tile, p1, p2, text);
|
2014-02-23 23:03:08 +01:00
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2021-10-03 21:13:32 +02:00
|
|
|
if (cmd == CMD_COMPANY_CTRL) {
|
2010-06-05 15:32:42 +02: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. */
|
|
|
|
assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
|
|
|
cur_company.Restore();
|
|
|
|
}
|
2009-02-07 19:09:30 +01:00
|
|
|
|
2012-01-09 23:21:45 +01: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. */
|
|
|
|
if (!test_and_exec_can_differ) {
|
2010-01-18 23:57:21 +01:00
|
|
|
assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
|
|
|
|
} else if (res2.Failed()) {
|
2021-10-29 14:41:20 +02:00
|
|
|
return res2;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/* If we're needing more money and we haven't done
|
|
|
|
* anything yet, ask for the money! */
|
|
|
|
if (_additional_cash_required != 0 && res2.GetCost() == 0) {
|
2010-04-17 22:21:33 +02: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();
|
2010-01-11 21:39:38 +01:00
|
|
|
SetDParam(0, _additional_cash_required);
|
2021-10-29 14:41:20 +02:00
|
|
|
return CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
/* update last build coordinate of company. */
|
|
|
|
if (tile != 0) {
|
|
|
|
Company *c = Company::GetIfValid(_current_company);
|
2019-04-10 23:07:06 +02:00
|
|
|
if (c != nullptr) c->last_build_coordinate = tile;
|
2006-03-12 11:15:36 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2010-01-11 21:39:38 +01:00
|
|
|
SubtractMoneyFromCompany(res2);
|
|
|
|
|
|
|
|
/* update signals if needed */
|
|
|
|
UpdateSignalsInBuffer();
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2021-10-29 14:41:20 +02:00
|
|
|
return res2;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2008-04-07 22:02:36 +02:00
|
|
|
|
|
|
|
|
2010-02-14 16:30:08 +01: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 16:44:21 +01:00
|
|
|
void CommandCost::AddCost(const CommandCost &ret)
|
2008-04-07 22:02:36 +02:00
|
|
|
{
|
|
|
|
this->AddCost(ret.cost);
|
|
|
|
if (this->success && !ret.success) {
|
|
|
|
this->message = ret.message;
|
|
|
|
this->success = false;
|
|
|
|
}
|
|
|
|
}
|
2011-07-03 16:32:15 +02: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 19:00:39 +01:00
|
|
|
* @param grffile NewGRF that provides the #TextRefStack
|
|
|
|
* @param num_registers number of entries to copy from the temporary NewGRF registers
|
2011-07-03 16:32:15 +02:00
|
|
|
*/
|
2014-01-12 19:00:39 +01:00
|
|
|
void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
|
2011-07-03 16:32:15 +02:00
|
|
|
{
|
|
|
|
extern TemporaryStorageArray<int32, 0x110> _temp_store;
|
|
|
|
|
|
|
|
assert(num_registers < lengthof(textref_stack));
|
2014-01-12 19:00:39 +01:00
|
|
|
this->textref_stack_grffile = grffile;
|
2011-07-03 16:32:15 +02:00
|
|
|
this->textref_stack_size = num_registers;
|
|
|
|
for (uint i = 0; i < num_registers; i++) {
|
|
|
|
textref_stack[i] = _temp_store.GetValue(0x100 + i);
|
|
|
|
}
|
|
|
|
}
|