Codechange: Add support for additional command result values.

This commit is contained in:
Michael Lutz 2021-11-30 00:52:23 +01:00
parent 8503854655
commit 3e85e833a7
10 changed files with 136 additions and 41 deletions

View File

@ -18,6 +18,7 @@
#include "ai.hpp" #include "ai.hpp"
#include "../script/script_storage.hpp" #include "../script/script_storage.hpp"
#include "../script/script_cmd.h"
#include "ai_info.hpp" #include "ai_info.hpp"
#include "ai_instance.hpp" #include "ai_instance.hpp"
@ -96,8 +97,9 @@ ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
* @param result The result of the command. * @param result The result of the command.
* @param tile The tile on which the command was executed. * @param tile The tile on which the command was executed.
* @param data Command data as given to Command<>::Post. * @param data Command data as given to Command<>::Post.
* @param result_data Additional returned data from the command.
*/ */
void CcAI(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data) void CcAI(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data)
{ {
/* /*
* The company might not exist anymore. Check for this. * The company might not exist anymore. Check for this.
@ -108,7 +110,7 @@ void CcAI(Commands cmd, const CommandCost &result, TileIndex tile, const Command
const Company *c = Company::GetIfValid(_current_company); const Company *c = Company::GetIfValid(_current_company);
if (c == nullptr || c->ai_instance == nullptr) return; if (c == nullptr || c->ai_instance == nullptr) return;
if (c->ai_instance->DoCommandCallback(result, tile, data, cmd)) { if (c->ai_instance->DoCommandCallback(result, tile, data, std::move(result_data), cmd)) {
c->ai_instance->Continue(); c->ai_instance->Continue();
} }
} }

View File

@ -111,10 +111,30 @@ protected:
* Templated wrapper that exposes the command parameter arguments * Templated wrapper that exposes the command parameter arguments
* for the various Command::Do/Post calls. * for the various Command::Do/Post calls.
* @tparam Tcmd The command-id to execute. * @tparam Tcmd The command-id to execute.
* @tparam Tret Return type of the command.
* @tparam Targs The command parameter types. * @tparam Targs The command parameter types.
*/ */
template <Commands Tcmd, typename... Targs> template <Commands Tcmd, typename Tret, typename... Targs>
struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true> : protected CommandHelperBase { struct CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true> : protected CommandHelperBase {
private:
/** Extract the \c CommandCost from a command proc result. */
static inline CommandCost &ExtractCommandCost(Tret &ret)
{
if constexpr (std::is_same_v<Tret, CommandCost>) {
return ret;
} else {
return std::get<0>(ret);
}
}
/** Make a command proc result from a \c CommandCost. */
static inline Tret MakeResult(const CommandCost &cost)
{
Tret ret{};
ExtractCommandCost(ret) = cost;
return ret;
}
public: public:
/** /**
* This function executes a given command with the parameters from the #CommandProc parameter list. * This function executes a given command with the parameters from the #CommandProc parameter list.
@ -129,12 +149,12 @@ public:
* @see CommandProc * @see CommandProc
* @return the cost * @return the cost
*/ */
static CommandCost Do(DoCommandFlag flags, Targs... args) static Tret Do(DoCommandFlag flags, Targs... args)
{ {
if constexpr (std::is_same_v<TileIndex, std::tuple_element_t<0, std::tuple<Targs...>>>) { if constexpr (std::is_same_v<TileIndex, std::tuple_element_t<0, std::tuple<Targs...>>>) {
/* Do not even think about executing out-of-bounds tile-commands. */ /* Do not even think about executing out-of-bounds tile-commands. */
TileIndex tile = std::get<0>(std::make_tuple(args...)); TileIndex tile = std::get<0>(std::make_tuple(args...));
if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR; if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return MakeResult(CMD_ERROR);
} }
RecursiveCommandCounter counter{}; RecursiveCommandCounter counter{};
@ -142,17 +162,17 @@ public:
/* Only execute the test call if it's toplevel, or we're not execing. */ /* Only execute the test call if it's toplevel, or we're not execing. */
if (counter.IsTopLevel() || !(flags & DC_EXEC)) { if (counter.IsTopLevel() || !(flags & DC_EXEC)) {
InternalDoBefore(counter.IsTopLevel(), true); InternalDoBefore(counter.IsTopLevel(), true);
CommandCost res = CommandTraits<Tcmd>::proc(flags & ~DC_EXEC, args...); Tret res = CommandTraits<Tcmd>::proc(flags & ~DC_EXEC, args...);
InternalDoAfter(res, flags, counter.IsTopLevel(), true); // Can modify res. InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(), true); // Can modify res.
if (res.Failed() || !(flags & DC_EXEC)) return res; if (ExtractCommandCost(res).Failed() || !(flags & DC_EXEC)) return res;
} }
/* Execute the command here. All cost-relevant functions set the expenses type /* Execute the command here. All cost-relevant functions set the expenses type
* themselves to the cost object at some point. */ * themselves to the cost object at some point. */
InternalDoBefore(counter.IsTopLevel(), false); InternalDoBefore(counter.IsTopLevel(), false);
CommandCost res = CommandTraits<Tcmd>::proc(flags, args...); Tret res = CommandTraits<Tcmd>::proc(flags, args...);
InternalDoAfter(res, flags, counter.IsTopLevel(), false); InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(), false);
return res; return res;
} }
@ -237,7 +257,7 @@ public:
* @return the command cost of this function. * @return the command cost of this function.
*/ */
template <typename Tcallback> template <typename Tcallback>
static CommandCost Unsafe(StringID err_message, Tcallback *callback, bool my_cmd, bool estimate_only, TileIndex location, std::tuple<Targs...> args) static Tret Unsafe(StringID err_message, Tcallback *callback, bool my_cmd, bool estimate_only, TileIndex location, std::tuple<Targs...> args)
{ {
return Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, false, location, std::move(args)); return Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, false, location, std::move(args));
} }
@ -259,6 +279,13 @@ protected:
((SetClientIdHelper(std::get<Tindices>(values))), ...); ((SetClientIdHelper(std::get<Tindices>(values))), ...);
} }
/** Remove the first element of a tuple. */
template <template <typename...> typename Tt, typename T1, typename... Ts>
static inline Tt<Ts...> RemoveFirstTupleElement(const Tt<T1, Ts...> &tuple)
{
return std::apply([](auto &&, const auto&... args) { return std::tie(args...); }, tuple);
}
template <typename Tcallback> template <typename Tcallback>
static bool InternalPost(StringID err_message, Tcallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args) static bool InternalPost(StringID err_message, Tcallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args)
{ {
@ -280,23 +307,33 @@ protected:
/* Only set client IDs when the command does not come from the network. */ /* Only set client IDs when the command does not come from the network. */
if (!network_command && GetCommandFlags<Tcmd>() & CMD_CLIENT_ID) SetClientIds(args, std::index_sequence_for<Targs...>{}); if (!network_command && GetCommandFlags<Tcmd>() & CMD_CLIENT_ID) SetClientIds(args, std::index_sequence_for<Targs...>{});
CommandCost res = Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, network_command, tile, args); Tret res = Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, network_command, tile, args);
InternalPostResult(res, tile, estimate_only, only_sending, err_message, my_cmd); InternalPostResult(ExtractCommandCost(res), tile, estimate_only, only_sending, err_message, my_cmd);
if (!estimate_only && !only_sending && callback != nullptr) { if (!estimate_only && !only_sending && callback != nullptr) {
if constexpr (std::is_same_v<Tcallback, CommandCallback>) { if constexpr (std::is_same_v<Tcallback, CommandCallback>) {
/* Callback that doesn't need any command arguments. */ /* Callback that doesn't need any command arguments. */
callback(Tcmd, res, tile); callback(Tcmd, ExtractCommandCost(res), tile);
} else if constexpr (std::is_same_v<Tcallback, CommandCallbackData>) { } else if constexpr (std::is_same_v<Tcallback, CommandCallbackData>) {
/* Generic callback that takes packed arguments as a buffer. */ /* Generic callback that takes packed arguments as a buffer. */
callback(Tcmd, res, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args)); if constexpr (std::is_same_v<Tret, CommandCost>) {
callback(Tcmd, ExtractCommandCost(res), tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), {});
} else {
callback(Tcmd, ExtractCommandCost(res), tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), EndianBufferWriter<CommandDataBuffer>::FromValue(RemoveFirstTupleElement(res)));
}
} else if constexpr (!std::is_same_v<Tret, CommandCost> && std::is_same_v<Tcallback *, typename CommandTraits<Tcmd>::RetCallbackProc>) {
std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res));
} else { } else {
/* Callback with arguments. We assume that the tile is only interesting if it actually is in the command arguments. */ /* Callback with arguments. We assume that the tile is only interesting if it actually is in the command arguments. */
std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd, res), args)); if constexpr (std::is_same_v<Tret, CommandCost>) {
std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd, res), args));
} else {
std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res, args));
}
} }
} }
return res.Succeeded(); return ExtractCommandCost(res).Succeeded();
} }
/** Helper to process a single ClientID argument. */ /** Helper to process a single ClientID argument. */
@ -317,7 +354,7 @@ protected:
return (ClientIdIsSet(std::get<Tindices>(values)) && ...); return (ClientIdIsSet(std::get<Tindices>(values)) && ...);
} }
static CommandCost Execute(StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, std::tuple<Targs...> args) static Tret Execute(StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, std::tuple<Targs...> args)
{ {
/* Prevent recursion; it gives a mess over the network */ /* Prevent recursion; it gives a mess over the network */
RecursiveCommandCounter counter{}; RecursiveCommandCounter counter{};
@ -334,14 +371,14 @@ protected:
Backup<CompanyID> cur_company(_current_company, FILE_LINE); Backup<CompanyID> cur_company(_current_company, FILE_LINE);
if (!InternalExecutePrepTest(cmd_flags, tile, cur_company)) { if (!InternalExecutePrepTest(cmd_flags, tile, cur_company)) {
cur_company.Trash(); cur_company.Trash();
return CMD_ERROR; return MakeResult(CMD_ERROR);
} }
/* Test the command. */ /* Test the command. */
DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags); DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
CommandCost res = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags), args)); Tret res = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags), args));
auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(res, cmd_flags, estimate_only, network_command, cur_company); auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(ExtractCommandCost(res), cmd_flags, estimate_only, network_command, cur_company);
if (exit_test) { if (exit_test) {
if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), true); if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), true);
cur_company.Restore(); cur_company.Restore();
@ -358,15 +395,20 @@ protected:
* This way it's not handled by DoCommand and only the * This way it's not handled by DoCommand and only the
* actual execution of the command causes messages. Also * actual execution of the command causes messages. Also
* reset the storages as we've not executed the command. */ * reset the storages as we've not executed the command. */
return CommandCost(); return {};
} }
if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), false); if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), false);
/* Actually try and execute the command. */ /* Actually try and execute the command. */
CommandCost res2 = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags | DC_EXEC), args)); Tret res2 = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags | DC_EXEC), args));
return InternalExecuteProcessResult(Tcmd, cmd_flags, res, res2, tile, cur_company); if constexpr (std::is_same_v<Tret, CommandCost>) {
return InternalExecuteProcessResult(Tcmd, cmd_flags, res, res2, tile, cur_company);
} else {
std::get<0>(res2) = InternalExecuteProcessResult(Tcmd, cmd_flags, ExtractCommandCost(res), ExtractCommandCost(res2), tile, cur_company);
return res2;
}
} }
}; };
@ -374,13 +416,14 @@ protected:
* Overload for #CommandHelper that exposes additional \c Post variants * Overload for #CommandHelper that exposes additional \c Post variants
* for commands that don't take a TileIndex themselves. * for commands that don't take a TileIndex themselves.
* @tparam Tcmd The command-id to execute. * @tparam Tcmd The command-id to execute.
* @tparam Tret Return type of the command.
* @tparam Targs The command parameter types. * @tparam Targs The command parameter types.
*/ */
template <Commands Tcmd, typename... Targs> template <Commands Tcmd, typename Tret, typename... Targs>
struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), false> : CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true> struct CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), false> : CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true>
{ {
/* Import Post overloads from our base class. */ /* Import Post overloads from our base class. */
using CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>::Post; using CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true>::Post;
/** /**
* Shortcut for Post when not using an error message. * Shortcut for Post when not using an error message.
@ -416,7 +459,7 @@ struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), false> : Com
template <typename Tcallback> template <typename Tcallback>
static inline bool Post(StringID err_message, Tcallback *callback, TileIndex location, Targs... args) static inline bool Post(StringID err_message, Tcallback *callback, TileIndex location, Targs... args)
{ {
return CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...)); return CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...));
} }
}; };

View File

@ -409,6 +409,14 @@ template <typename T> struct CommandFunctionTraitHelper;
template <typename... Targs> template <typename... Targs>
struct CommandFunctionTraitHelper<CommandCost(*)(DoCommandFlag, Targs...)> { struct CommandFunctionTraitHelper<CommandCost(*)(DoCommandFlag, Targs...)> {
using Args = std::tuple<std::decay_t<Targs>...>; using Args = std::tuple<std::decay_t<Targs>...>;
using CbArgs = Args;
using CbProcType = void(*)(Commands, const CommandCost &);
};
template <template <typename...> typename Tret, typename... Tretargs, typename... Targs>
struct CommandFunctionTraitHelper<Tret<CommandCost, Tretargs...>(*)(DoCommandFlag, Targs...)> {
using Args = std::tuple<std::decay_t<Targs>...>;
using CbArgs = std::tuple<std::decay_t<Tretargs>..., std::decay_t<Targs>...>;
using CbProcType = void(*)(Commands, const CommandCost &, Tretargs...);
}; };
/** Defines the traits of a command. */ /** Defines the traits of a command. */
@ -418,6 +426,8 @@ template <Commands Tcmd> struct CommandTraits;
template<> struct CommandTraits<cmd_> { \ template<> struct CommandTraits<cmd_> { \
using ProcType = decltype(&proc_); \ using ProcType = decltype(&proc_); \
using Args = typename CommandFunctionTraitHelper<ProcType>::Args; \ using Args = typename CommandFunctionTraitHelper<ProcType>::Args; \
using CbArgs = typename CommandFunctionTraitHelper<ProcType>::CbArgs; \
using RetCallbackProc = typename CommandFunctionTraitHelper<ProcType>::CbProcType; \
static constexpr Commands cmd = cmd_; \ static constexpr Commands cmd = cmd_; \
static constexpr auto &proc = proc_; \ static constexpr auto &proc = proc_; \
static constexpr CommandFlags flags = (CommandFlags)(flags_); \ static constexpr CommandFlags flags = (CommandFlags)(flags_); \
@ -453,8 +463,9 @@ typedef void CommandCallback(Commands cmd, const CommandCost &result, TileIndex
* @param result The result of the executed command * @param result The result of the executed command
* @param tile The tile of the command action * @param tile The tile of the command action
* @param data Additional data of the command * @param data Additional data of the command
* @param result_data Additional returned data from the command
* @see CommandProc * @see CommandProc
*/ */
typedef void CommandCallbackData(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data); typedef void CommandCallbackData(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data);
#endif /* COMMAND_TYPE_H */ #endif /* COMMAND_TYPE_H */

View File

@ -13,6 +13,7 @@
#include "../script/squirrel_class.hpp" #include "../script/squirrel_class.hpp"
#include "../script/script_storage.hpp" #include "../script/script_storage.hpp"
#include "../script/script_cmd.h"
#include "../ai/ai_gui.hpp" #include "../ai/ai_gui.hpp"
#include "game_config.hpp" #include "game_config.hpp"
#include "game_info.hpp" #include "game_info.hpp"
@ -85,10 +86,11 @@ void GameInstance::Died()
* @param result The result of the command. * @param result The result of the command.
* @param tile The tile on which the command was executed. * @param tile The tile on which the command was executed.
* @param data Command data as given to Command<>::Post. * @param data Command data as given to Command<>::Post.
* @param result_data Additional returned data from the command.
*/ */
void CcGame(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data) void CcGame(Commands cmd, const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data)
{ {
if (Game::GetGameInstance()->DoCommandCallback(result, tile, data, cmd)) { if (Game::GetGameInstance()->DoCommandCallback(result, tile, data, std::move(result_data), cmd)) {
Game::GetGameInstance()->Continue(); Game::GetGameInstance()->Continue();
} }
} }

View File

@ -527,7 +527,7 @@ void UnpackNetworkCommand(const CommandPacket* cp)
/* Check if the callback matches with the command arguments. If not, drop the callback. */ /* Check if the callback matches with the command arguments. If not, drop the callback. */
using Tcallback = std::tuple_element_t<Tcb, decltype(_callback_tuple)>; using Tcallback = std::tuple_element_t<Tcb, decltype(_callback_tuple)>;
if constexpr (std::is_same_v<Tcallback, CommandCallback * const> || std::is_same_v<Tcallback, CommandCallbackData * const> || std::is_same_v<typename CommandTraits<Tcmd>::Args, typename CallbackArgsHelper<Tcallback>::Args>) { if constexpr (std::is_same_v<Tcallback, CommandCallback * const> || std::is_same_v<Tcallback, CommandCallbackData * const> || std::is_same_v<typename CommandTraits<Tcmd>::CbArgs, typename CallbackArgsHelper<Tcallback>::Args>) {
Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, cp->tile, args); Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, cp->tile, args);
} else { } else {
Command<Tcmd>::PostFromNet(cp->err_msg, (CommandCallback *)nullptr, cp->my_cmd, cp->tile, args); Command<Tcmd>::PostFromNet(cp->err_msg, (CommandCallback *)nullptr, cp->my_cmd, cp->tile, args);

View File

@ -173,6 +173,16 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->last_command_res; return GetStorage()->last_command_res;
} }
/* static */ void ScriptObject::SetLastCommandResData(CommandDataBuffer data)
{
GetStorage()->last_cmd_ret = std::move(data);
}
/* static */ const CommandDataBuffer &ScriptObject::GetLastCommandResData()
{
return GetStorage()->last_cmd_ret;
}
/* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id) /* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
{ {
GetStorage()->new_vehicle_id = vehicle_id; GetStorage()->new_vehicle_id = vehicle_id;

View File

@ -61,6 +61,12 @@ public:
*/ */
static void SetLastCommandRes(bool res); static void SetLastCommandRes(bool res);
/**
* Store the extra data return by the last DoCommand.
* @param data Extra data return by the command.
*/
static void SetLastCommandResData(CommandDataBuffer data);
/** /**
* Get the currently active instance. * Get the currently active instance.
* @return The instance. * @return The instance.
@ -74,10 +80,11 @@ protected:
* Templated wrapper that exposes the command parameter arguments * Templated wrapper that exposes the command parameter arguments
* on the various DoCommand calls. * on the various DoCommand calls.
* @tparam Tcmd The command-id to execute. * @tparam Tcmd The command-id to execute.
* @tparam Tret Return type of the command.
* @tparam Targs The command parameter types. * @tparam Targs The command parameter types.
*/ */
template <Commands Tcmd, typename... Targs> template <Commands Tcmd, typename Tret, typename... Targs>
struct ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...)> { struct ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)> {
static bool Do(Script_SuspendCallbackProc *callback, Targs... args) static bool Do(Script_SuspendCallbackProc *callback, Targs... args)
{ {
return Execute(callback, std::forward_as_tuple(args...)); return Execute(callback, std::forward_as_tuple(args...));
@ -180,6 +187,11 @@ protected:
*/ */
static bool GetLastCommandRes(); static bool GetLastCommandRes();
/**
* Get the extra return data from the last DoCommand.
*/
static const CommandDataBuffer &GetLastCommandResData();
/** /**
* Get the latest stored new_vehicle_id. * Get the latest stored new_vehicle_id.
*/ */
@ -363,10 +375,17 @@ namespace ScriptObjectInternal {
{ {
((SetClientIdHelper(std::get<Tindices>(values))), ...); ((SetClientIdHelper(std::get<Tindices>(values))), ...);
} }
/** Remove the first element of a tuple. */
template <template <typename...> typename Tt, typename T1, typename... Ts>
static inline Tt<Ts...> RemoveFirstTupleElement(const Tt<T1, Ts...> &tuple)
{
return std::apply([](auto &&, const auto&... args) { return std::tie(args...); }, tuple);
}
} }
template <Commands Tcmd, typename... Targs> template <Commands Tcmd, typename Tret, typename... Targs>
bool ScriptObject::ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...)>::Execute(Script_SuspendCallbackProc *callback, std::tuple<Targs...> args) bool ScriptObject::ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)>::Execute(Script_SuspendCallbackProc *callback, std::tuple<Targs...> args)
{ {
auto [err, estimate_only, networking] = ScriptObject::DoCommandPrep(); auto [err, estimate_only, networking] = ScriptObject::DoCommandPrep();
if (err) return false; if (err) return false;
@ -387,9 +406,14 @@ bool ScriptObject::ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Tar
if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd); if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
/* Try to perform the command. */ /* Try to perform the command. */
CommandCost res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args); Tret res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args);
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only); if constexpr (std::is_same_v<Tret, CommandCost>) {
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only);
} else {
ScriptObject::SetLastCommandResData(EndianBufferWriter<CommandDataBuffer>::FromValue(ScriptObjectInternal::RemoveFirstTupleElement(res)));
return ScriptObject::DoCommandProcessResult(std::get<0>(res), callback, estimate_only);
}
} }
#endif /* SCRIPT_OBJECT_HPP */ #endif /* SCRIPT_OBJECT_HPP */

View File

@ -687,7 +687,7 @@ SQInteger ScriptInstance::GetOpsTillSuspend()
return this->engine->GetOpsTillSuspend(); return this->engine->GetOpsTillSuspend();
} }
bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, Commands cmd) bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
{ {
ScriptObject::ActiveInstance active(this); ScriptObject::ActiveInstance active(this);
@ -697,6 +697,7 @@ bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile
} }
ScriptObject::SetLastCommandRes(result.Succeeded()); ScriptObject::SetLastCommandRes(result.Succeeded());
ScriptObject::SetLastCommandResData(std::move(result_data));
if (result.Failed()) { if (result.Failed()) {
ScriptObject::SetLastError(ScriptError::StringToError(result.GetErrorMessage())); ScriptObject::SetLastError(ScriptError::StringToError(result.GetErrorMessage()));

View File

@ -179,10 +179,11 @@ public:
* @param result The result of the command. * @param result The result of the command.
* @param tile The tile on which the command was executed. * @param tile The tile on which the command was executed.
* @param data Command data as given to DoCommandPInternal. * @param data Command data as given to DoCommandPInternal.
* @param result_data Extra data return from the command.
* @param cmd cmd as given to DoCommandPInternal. * @param cmd cmd as given to DoCommandPInternal.
* @return true if we handled result. * @return true if we handled result.
*/ */
bool DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, Commands cmd); bool DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
/** /**
* Insert an event for this script. * Insert an event for this script.

View File

@ -47,6 +47,7 @@ private:
TileIndex last_tile; ///< The last tile passed to a command. TileIndex last_tile; ///< The last tile passed to a command.
CommandDataBuffer last_data; ///< The last data passed to a command. CommandDataBuffer last_data; ///< The last data passed to a command.
Commands last_cmd; ///< The last cmd passed to a command. Commands last_cmd; ///< The last cmd passed to a command.
CommandDataBuffer last_cmd_ret; ///< The extra data returned by the last command.
VehicleID new_vehicle_id; ///< The ID of the new Vehicle. VehicleID new_vehicle_id; ///< The ID of the new Vehicle.
SignID new_sign_id; ///< The ID of the new Sign. SignID new_sign_id; ///< The ID of the new Sign.