2017-01-11 22:47:17 +01:00
|
|
|
/*****************************************************************************
|
2019-03-17 08:16:15 +01:00
|
|
|
* Copyright (c) 2014-2019 OpenRCT2 developers
|
2017-01-11 22:47:17 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2017-01-11 22:47:17 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-01-11 22:47:17 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-06-22 22:56:21 +02:00
|
|
|
#include "GameAction.h"
|
|
|
|
|
2017-09-27 23:07:26 +02:00
|
|
|
#include "../Context.h"
|
2018-12-06 06:34:27 +01:00
|
|
|
#include "../ReplayManager.h"
|
2017-01-11 22:47:17 +01:00
|
|
|
#include "../core/Guard.hpp"
|
2017-01-11 23:53:33 +01:00
|
|
|
#include "../core/Memory.hpp"
|
2017-03-23 20:49:19 +01:00
|
|
|
#include "../core/MemoryStream.h"
|
2018-01-25 20:47:57 +01:00
|
|
|
#include "../localisation/Localisation.h"
|
2017-01-11 23:53:33 +01:00
|
|
|
#include "../network/network.h"
|
2017-09-27 23:07:26 +02:00
|
|
|
#include "../platform/platform.h"
|
2018-03-13 13:14:02 +01:00
|
|
|
#include "../scenario/Scenario.h"
|
2017-12-31 21:40:00 +01:00
|
|
|
#include "../world/Park.h"
|
2019-08-20 23:51:12 +02:00
|
|
|
#include "../world/Scenery.h"
|
2018-06-22 22:56:21 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2017-01-11 23:53:33 +01:00
|
|
|
|
2017-01-11 22:47:17 +01:00
|
|
|
GameActionResult::GameActionResult(GA_ERROR error, rct_string_id message)
|
|
|
|
{
|
|
|
|
Error = error;
|
|
|
|
ErrorMessage = message;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:50:49 +02:00
|
|
|
GameActionResult::GameActionResult(GA_ERROR error, rct_string_id title, rct_string_id message)
|
|
|
|
{
|
|
|
|
Error = error;
|
|
|
|
ErrorTitle = title;
|
|
|
|
ErrorMessage = message;
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:56:21 +02:00
|
|
|
GameActionResult::GameActionResult(GA_ERROR error, rct_string_id title, rct_string_id message, uint8_t* args)
|
2017-10-09 16:50:49 +02:00
|
|
|
{
|
|
|
|
Error = error;
|
|
|
|
ErrorTitle = title;
|
|
|
|
ErrorMessage = message;
|
2018-02-01 13:52:39 +01:00
|
|
|
std::copy_n(args, ErrorMessageArgs.size(), ErrorMessageArgs.begin());
|
2017-10-09 16:50:49 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 22:47:17 +01:00
|
|
|
namespace GameActions
|
|
|
|
{
|
2019-08-20 23:51:12 +02:00
|
|
|
struct QueuedGameAction
|
|
|
|
{
|
|
|
|
uint32_t tick;
|
|
|
|
uint32_t uniqueId;
|
|
|
|
GameAction::Ptr action;
|
|
|
|
|
|
|
|
explicit QueuedGameAction(uint32_t t, std::unique_ptr<GameAction>&& ga, uint32_t id)
|
|
|
|
: tick(t)
|
|
|
|
, uniqueId(id)
|
|
|
|
, action(std::move(ga))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const QueuedGameAction& comp) const
|
|
|
|
{
|
|
|
|
// First sort by tick
|
|
|
|
if (tick < comp.tick)
|
|
|
|
return true;
|
|
|
|
if (tick > comp.tick)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the ticks are equal sort by commandIndex
|
|
|
|
return uniqueId < comp.uniqueId;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-11 22:47:17 +01:00
|
|
|
static GameActionFactory _actions[GAME_COMMAND_COUNT];
|
2019-08-20 23:51:12 +02:00
|
|
|
static std::multiset<QueuedGameAction> _actionQueue;
|
|
|
|
static uint32_t _nextUniqueId = 0;
|
2019-10-05 13:09:21 +02:00
|
|
|
static bool _suspended = false;
|
2017-01-11 22:47:17 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
GameActionFactory Register(uint32_t id, GameActionFactory factory)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2018-11-21 23:16:04 +01:00
|
|
|
Guard::Assert(id < std::size(_actions));
|
2017-01-11 22:47:17 +01:00
|
|
|
Guard::ArgumentNotNull(factory);
|
|
|
|
|
|
|
|
_actions[id] = factory;
|
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
2018-12-15 10:11:54 +01:00
|
|
|
bool IsValidId(uint32_t id)
|
|
|
|
{
|
|
|
|
if (id < std::size(_actions))
|
|
|
|
{
|
|
|
|
return _actions[id] != nullptr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-05 13:09:21 +02:00
|
|
|
void SuspendQueue()
|
|
|
|
{
|
|
|
|
_suspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeQueue()
|
|
|
|
{
|
|
|
|
_suspended = false;
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:51:12 +02:00
|
|
|
void Enqueue(const GameAction* ga, uint32_t tick)
|
|
|
|
{
|
|
|
|
auto action = Clone(ga);
|
|
|
|
Enqueue(std::move(action), tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enqueue(GameAction::Ptr&& ga, uint32_t tick)
|
|
|
|
{
|
|
|
|
if (ga->GetPlayer() == -1 && network_get_mode() != NETWORK_MODE_NONE)
|
|
|
|
{
|
|
|
|
// Server can directly invoke actions and will have no player id assigned
|
|
|
|
// as that normally happens when receiving them over network.
|
|
|
|
ga->SetPlayer(network_get_current_player_id());
|
|
|
|
}
|
|
|
|
_actionQueue.emplace(tick, std::move(ga), _nextUniqueId++);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessQueue()
|
|
|
|
{
|
2019-10-05 13:09:21 +02:00
|
|
|
if (_suspended)
|
|
|
|
{
|
|
|
|
// Do nothing if suspended, this is usually the case between connect and map loads.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:51:12 +02:00
|
|
|
const uint32_t currentTick = gCurrentTicks;
|
|
|
|
|
|
|
|
while (_actionQueue.begin() != _actionQueue.end())
|
|
|
|
{
|
|
|
|
// run all the game commands at the current tick
|
|
|
|
const QueuedGameAction& queued = *_actionQueue.begin();
|
|
|
|
|
|
|
|
if (network_get_mode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
|
|
|
if (queued.tick < currentTick)
|
|
|
|
{
|
|
|
|
// This should never happen.
|
|
|
|
Guard::Assert(
|
|
|
|
false,
|
2019-10-31 17:25:18 +01:00
|
|
|
"Discarding game action %s (%u) from tick behind current tick, ID: %08X, Action Tick: %08X, Current "
|
|
|
|
"Tick: "
|
2019-08-20 23:51:12 +02:00
|
|
|
"%08X\n",
|
2019-10-31 17:25:18 +01:00
|
|
|
queued.action->GetName(), queued.action->GetType(), queued.uniqueId, queued.tick, currentTick);
|
2019-08-20 23:51:12 +02:00
|
|
|
}
|
2019-08-23 23:06:27 +02:00
|
|
|
else if (queued.tick > currentTick)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-08-20 23:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove ghost scenery so it doesn't interfere with incoming network command
|
|
|
|
switch (queued.action->GetType())
|
|
|
|
{
|
|
|
|
case GAME_COMMAND_PLACE_WALL:
|
|
|
|
case GAME_COMMAND_PLACE_LARGE_SCENERY:
|
|
|
|
case GAME_COMMAND_PLACE_BANNER:
|
|
|
|
case GAME_COMMAND_PLACE_SCENERY:
|
|
|
|
scenery_remove_ghost_tool_placement();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameAction* action = queued.action.get();
|
|
|
|
action->SetFlags(action->GetFlags() | GAME_COMMAND_FLAG_NETWORKED);
|
|
|
|
|
|
|
|
Guard::Assert(action != nullptr);
|
|
|
|
|
|
|
|
GameActionResult::Ptr result = Execute(action);
|
|
|
|
if (result->Error == GA_ERROR::OK && network_get_mode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
|
|
|
// Relay this action to all other clients.
|
|
|
|
network_send_game_action(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
_actionQueue.erase(_actionQueue.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearQueue()
|
|
|
|
{
|
|
|
|
_actionQueue.clear();
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:27:21 +02:00
|
|
|
void Initialize()
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Register();
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::unique_ptr<GameAction> Create(uint32_t id)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2017-07-17 21:27:21 +02:00
|
|
|
Initialize();
|
|
|
|
|
2018-06-22 22:56:21 +02:00
|
|
|
GameAction* result = nullptr;
|
2018-11-21 23:16:04 +01:00
|
|
|
if (id < std::size(_actions))
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
|
|
|
GameActionFactory factory = _actions[id];
|
|
|
|
if (factory != nullptr)
|
|
|
|
{
|
|
|
|
result = factory();
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 18:23:58 +01:00
|
|
|
#ifdef _DEBUG
|
2018-05-14 12:36:45 +02:00
|
|
|
Guard::ArgumentNotNull(result, "Attempting to create unregistered gameaction: %u", id);
|
2019-02-07 18:23:58 +01:00
|
|
|
#endif
|
2017-07-21 19:54:05 +02:00
|
|
|
return std::unique_ptr<GameAction>(result);
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2018-12-28 20:31:07 +01:00
|
|
|
GameAction::Ptr Clone(const GameAction* action)
|
|
|
|
{
|
|
|
|
std::unique_ptr<GameAction> ga = GameActions::Create(action->GetType());
|
|
|
|
ga->SetCallback(action->GetCallback());
|
|
|
|
|
|
|
|
// Serialise action data into stream.
|
|
|
|
DataSerialiser dsOut(true);
|
|
|
|
action->Serialise(dsOut);
|
|
|
|
|
|
|
|
// Serialise into new action.
|
2019-07-27 15:49:06 +02:00
|
|
|
IStream& stream = dsOut.GetStream();
|
2018-12-28 20:31:07 +01:00
|
|
|
stream.SetPosition(0);
|
|
|
|
|
|
|
|
DataSerialiser dsIn(false, stream);
|
|
|
|
ga->Serialise(dsIn);
|
|
|
|
|
|
|
|
return ga;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
static bool CheckActionInPausedMode(uint32_t actionFlags)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2018-06-22 22:56:21 +02:00
|
|
|
if (gGamePaused == 0)
|
|
|
|
return true;
|
|
|
|
if (gCheatsBuildInPauseMode)
|
|
|
|
return true;
|
|
|
|
if (actionFlags & GA_FLAGS::ALLOW_WHILE_PAUSED)
|
|
|
|
return true;
|
2017-01-11 23:53:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-15 16:32:11 +01:00
|
|
|
static GameActionResult::Ptr QueryInternal(const GameAction* action, bool topLevel)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
|
|
|
Guard::ArgumentNotNull(action);
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t actionFlags = action->GetActionFlags();
|
2019-04-20 20:39:47 +02:00
|
|
|
if (topLevel && !CheckActionInPausedMode(actionFlags))
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
GameActionResult::Ptr result = std::make_unique<GameActionResult>();
|
|
|
|
|
|
|
|
result->Error = GA_ERROR::GAME_PAUSED;
|
2018-08-04 14:30:13 +02:00
|
|
|
result->ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
|
2017-07-20 19:28:56 +02:00
|
|
|
result->ErrorMessage = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED;
|
|
|
|
|
|
|
|
return result;
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
2017-07-20 19:28:56 +02:00
|
|
|
|
|
|
|
auto result = action->Query();
|
2018-01-06 13:16:42 +01:00
|
|
|
|
2017-07-20 19:28:56 +02:00
|
|
|
if (result->Error == GA_ERROR::OK)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2019-05-10 22:00:38 +02:00
|
|
|
if (!finance_check_affordability(result->Cost, action->GetFlags()))
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
result->Error = GA_ERROR::INSUFFICIENT_FUNDS;
|
|
|
|
result->ErrorMessage = STR_NOT_ENOUGH_CASH_REQUIRES;
|
2020-04-20 23:55:15 +02:00
|
|
|
Formatter(result->ErrorMessageArgs.data()).Add<uint32_t>(result->Cost);
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 16:32:11 +01:00
|
|
|
GameActionResult::Ptr Query(const GameAction* action)
|
|
|
|
{
|
|
|
|
return QueryInternal(action, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
GameActionResult::Ptr QueryNested(const GameAction* action)
|
|
|
|
{
|
|
|
|
return QueryInternal(action, false);
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
static const char* GetRealm()
|
|
|
|
{
|
|
|
|
if (network_get_mode() == NETWORK_MODE_CLIENT)
|
|
|
|
return "cl";
|
|
|
|
else if (network_get_mode() == NETWORK_MODE_SERVER)
|
|
|
|
return "sv";
|
2018-12-15 23:12:49 +01:00
|
|
|
return "sp";
|
2018-11-20 06:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ActionLogContext_t
|
|
|
|
{
|
|
|
|
MemoryStream output;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void LogActionBegin(ActionLogContext_t& ctx, const GameAction* action)
|
|
|
|
{
|
|
|
|
MemoryStream& output = ctx.output;
|
|
|
|
|
|
|
|
char temp[128] = {};
|
2019-08-23 23:06:27 +02:00
|
|
|
snprintf(
|
|
|
|
temp, sizeof(temp), "[%s] Tick: %u, GA: %s (%08X) (", GetRealm(), gCurrentTicks, action->GetName(),
|
|
|
|
action->GetType());
|
2018-11-20 06:04:42 +01:00
|
|
|
|
|
|
|
output.Write(temp, strlen(temp));
|
|
|
|
|
|
|
|
DataSerialiser ds(true, ctx.output, true); // Logging mode.
|
|
|
|
|
|
|
|
// Write all parameters into output as text.
|
|
|
|
action->Serialise(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LogActionFinish(ActionLogContext_t& ctx, const GameAction* action, const GameActionResult::Ptr& result)
|
|
|
|
{
|
|
|
|
MemoryStream& output = ctx.output;
|
|
|
|
|
|
|
|
char temp[128] = {};
|
|
|
|
|
|
|
|
if (result->Error != GA_ERROR::OK)
|
|
|
|
{
|
|
|
|
snprintf(temp, sizeof(temp), ") Failed, %u", (uint32_t)result->Error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(temp, sizeof(temp), ") OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
output.Write(temp, strlen(temp) + 1);
|
|
|
|
|
|
|
|
const char* text = (const char*)output.GetData();
|
2018-12-15 19:41:55 +01:00
|
|
|
log_verbose("%s", text);
|
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
network_append_server_log(text);
|
|
|
|
}
|
|
|
|
|
2019-02-15 16:32:11 +01:00
|
|
|
static GameActionResult::Ptr ExecuteInternal(const GameAction* action, bool topLevel)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
|
|
|
Guard::ArgumentNotNull(action);
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t actionFlags = action->GetActionFlags();
|
|
|
|
uint32_t flags = action->GetFlags();
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2018-12-06 08:48:00 +01:00
|
|
|
auto* replayManager = OpenRCT2::GetContext()->GetReplayManager();
|
2018-12-11 08:13:47 +01:00
|
|
|
if (replayManager != nullptr && (replayManager->IsReplaying() || replayManager->IsNormalising()))
|
2018-12-06 08:48:00 +01:00
|
|
|
{
|
|
|
|
// We only accept replay commands as long the replay is active.
|
|
|
|
if ((flags & GAME_COMMAND_FLAG_REPLAY) == 0)
|
|
|
|
{
|
|
|
|
// TODO: Introduce proper error.
|
|
|
|
GameActionResult::Ptr result = std::make_unique<GameActionResult>();
|
|
|
|
|
|
|
|
result->Error = GA_ERROR::GAME_PAUSED;
|
|
|
|
result->ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
|
|
|
|
result->ErrorMessage = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 09:39:21 +01:00
|
|
|
GameActionResult::Ptr result = QueryInternal(action, topLevel);
|
2017-07-20 19:28:56 +02:00
|
|
|
if (result->Error == GA_ERROR::OK)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2019-02-15 08:43:05 +01:00
|
|
|
if (topLevel)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2019-02-15 08:43:05 +01:00
|
|
|
// Networked games send actions to the server to be run
|
|
|
|
if (network_get_mode() == NETWORK_MODE_CLIENT)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2019-02-15 08:43:05 +01:00
|
|
|
// As a client we have to wait or send it first.
|
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED))
|
|
|
|
{
|
|
|
|
log_verbose("[%s] GameAction::Execute %s (Out)", GetRealm(), action->GetName());
|
|
|
|
network_send_game_action(action);
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2019-02-15 08:43:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
2019-02-15 08:43:05 +01:00
|
|
|
else if (network_get_mode() == NETWORK_MODE_SERVER)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2019-02-15 08:43:05 +01:00
|
|
|
// If player is the server it would execute right away as where clients execute the commands
|
|
|
|
// at the beginning of the frame, so we have to put them into the queue.
|
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED))
|
|
|
|
{
|
|
|
|
log_verbose("[%s] GameAction::Execute %s (Queue)", GetRealm(), action->GetName());
|
2019-08-20 23:51:12 +02:00
|
|
|
Enqueue(action, gCurrentTicks);
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2019-02-15 08:43:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
2017-03-21 20:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
ActionLogContext_t logContext;
|
|
|
|
LogActionBegin(logContext, action);
|
2017-07-17 21:27:21 +02:00
|
|
|
|
2017-01-11 23:53:33 +01:00
|
|
|
// Execute the action, changing the game state
|
2017-07-15 11:24:08 +02:00
|
|
|
result = action->Execute();
|
2017-01-11 23:53:33 +01:00
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
LogActionFinish(logContext, action, result);
|
|
|
|
|
2019-02-15 08:43:05 +01:00
|
|
|
// If not top level just give away the result.
|
2019-05-10 22:00:38 +02:00
|
|
|
if (!topLevel)
|
2019-02-15 08:43:05 +01:00
|
|
|
return result;
|
|
|
|
|
2017-01-11 23:53:33 +01:00
|
|
|
// Update money balance
|
2019-02-10 23:24:40 +01:00
|
|
|
if (result->Error == GA_ERROR::OK && finance_check_money_required(flags) && result->Cost != 0)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2019-12-22 09:20:40 +01:00
|
|
|
finance_payment(result->Cost, result->Expenditure);
|
2020-01-19 16:50:01 +01:00
|
|
|
MoneyEffect::Create(result->Cost, result->Position);
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:34:27 +01:00
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && result->Error == GA_ERROR::OK)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2019-12-31 10:23:12 +01:00
|
|
|
if (network_get_mode() != NETWORK_MODE_NONE)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2018-11-20 06:04:42 +01:00
|
|
|
NetworkPlayerId_t playerId = action->GetPlayer();
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2018-12-05 07:39:57 +01:00
|
|
|
int32_t playerIndex = network_get_player_index(playerId.id);
|
|
|
|
Guard::Assert(playerIndex != -1);
|
|
|
|
|
|
|
|
network_set_player_last_action(playerIndex, action->GetType());
|
2017-09-30 23:57:38 +02:00
|
|
|
if (result->Cost != 0)
|
|
|
|
{
|
2018-12-05 07:39:57 +01:00
|
|
|
network_add_player_money_spent(playerIndex, result->Cost);
|
2017-09-30 23:57:38 +02:00
|
|
|
}
|
2019-02-11 00:53:21 +01:00
|
|
|
|
2019-12-31 09:01:17 +01:00
|
|
|
if (!result->Position.isNull())
|
2019-02-11 00:53:21 +01:00
|
|
|
{
|
2020-02-28 16:23:41 +01:00
|
|
|
network_set_player_last_action_coord(playerIndex, result->Position);
|
2019-02-11 00:53:21 +01:00
|
|
|
}
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
2019-12-31 10:23:12 +01:00
|
|
|
else
|
2018-12-06 06:34:27 +01:00
|
|
|
{
|
2019-04-01 19:58:16 +02:00
|
|
|
bool commandExecutes = (flags & GAME_COMMAND_FLAG_GHOST) == 0 && (flags & GAME_COMMAND_FLAG_NO_SPEND) == 0;
|
2018-12-15 11:00:59 +01:00
|
|
|
|
2018-12-11 08:13:47 +01:00
|
|
|
bool recordAction = false;
|
|
|
|
if (replayManager)
|
|
|
|
{
|
2018-12-15 11:00:59 +01:00
|
|
|
if (replayManager->IsRecording() && commandExecutes)
|
2018-12-11 08:13:47 +01:00
|
|
|
recordAction = true;
|
|
|
|
else if (replayManager->IsNormalising() && (flags & GAME_COMMAND_FLAG_REPLAY) != 0)
|
|
|
|
recordAction = true; // In normalisation we only feed back actions issued by the replay manager.
|
|
|
|
}
|
|
|
|
if (recordAction)
|
2018-12-06 06:34:27 +01:00
|
|
|
{
|
|
|
|
replayManager->AddGameAction(gCurrentTicks, action);
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow autosave to commence
|
|
|
|
if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE)
|
|
|
|
{
|
2017-07-14 08:02:56 +02:00
|
|
|
gLastAutoSaveUpdate = platform_get_ticks();
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
2017-01-11 23:53:33 +01:00
|
|
|
|
|
|
|
// Call callback for asynchronous events
|
2017-07-21 20:44:44 +02:00
|
|
|
auto cb = action->GetCallback();
|
|
|
|
if (cb != nullptr)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2017-07-21 20:44:44 +02:00
|
|
|
cb(action, result.get());
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
2017-01-11 23:53:33 +01:00
|
|
|
|
2019-03-15 21:27:06 +01:00
|
|
|
// Only show errors when its not a ghost and not a preview and also top level action.
|
2019-04-20 20:39:47 +02:00
|
|
|
bool shouldShowError = !(flags & GAME_COMMAND_FLAG_GHOST) && !(flags & GAME_COMMAND_FLAG_NO_SPEND) && topLevel;
|
2019-03-15 21:27:06 +01:00
|
|
|
|
|
|
|
// In network mode the error should be only shown to the issuer of the action.
|
|
|
|
if (network_get_mode() != NETWORK_MODE_NONE)
|
|
|
|
{
|
2019-05-01 22:53:11 +02:00
|
|
|
// If the action was never networked and query fails locally the player id is not assigned.
|
|
|
|
// So compare only if the action went into the queue otherwise show errors by default.
|
|
|
|
const bool isActionFromNetwork = (action->GetFlags() & GAME_COMMAND_FLAG_NETWORKED) != 0;
|
|
|
|
if (isActionFromNetwork && action->GetPlayer() != network_get_current_player_id())
|
2019-03-15 21:27:06 +01:00
|
|
|
{
|
|
|
|
shouldShowError = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 20:39:47 +02:00
|
|
|
if (result->Error != GA_ERROR::OK && shouldShowError)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
|
|
|
// Show the error box
|
2018-02-01 13:52:39 +01:00
|
|
|
std::copy(result->ErrorMessageArgs.begin(), result->ErrorMessageArgs.end(), gCommonFormatArgs);
|
2017-09-27 23:07:26 +02:00
|
|
|
context_show_error(result->ErrorTitle, result->ErrorMessage);
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
2019-02-15 08:43:05 +01:00
|
|
|
|
2017-03-25 18:05:18 +01:00
|
|
|
return result;
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
2018-11-20 06:04:42 +01:00
|
|
|
|
2019-02-15 16:32:11 +01:00
|
|
|
GameActionResult::Ptr Execute(const GameAction* action)
|
|
|
|
{
|
|
|
|
return ExecuteInternal(action, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
GameActionResult::Ptr ExecuteNested(const GameAction* action)
|
|
|
|
{
|
|
|
|
return ExecuteInternal(action, false);
|
|
|
|
}
|
|
|
|
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace GameActions
|