2017-01-11 22:47:17 +01:00
|
|
|
/*****************************************************************************
|
2018-06-15 14:07:34 +02:00
|
|
|
* Copyright (c) 2014-2018 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-02-01 13:52:39 +01:00
|
|
|
#include <algorithm>
|
2017-09-27 23:07:26 +02:00
|
|
|
#include "../Context.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"
|
2017-01-11 22:47:17 +01:00
|
|
|
#include "../core/Util.hpp"
|
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"
|
2018-01-25 20:47:57 +01:00
|
|
|
#include "GameAction.h"
|
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-20 17:28:51 +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
|
|
|
|
{
|
|
|
|
static GameActionFactory _actions[GAME_COMMAND_COUNT];
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
GameActionFactory Register(uint32_t id, GameActionFactory factory)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2017-09-30 22:10:13 +02:00
|
|
|
Guard::Assert(id < Util::CountOf(_actions));
|
2017-01-11 22:47:17 +01:00
|
|
|
Guard::ArgumentNotNull(factory);
|
|
|
|
|
|
|
|
_actions[id] = factory;
|
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2017-07-21 19:54:05 +02:00
|
|
|
GameAction * result = nullptr;
|
2017-01-11 22:47:17 +01:00
|
|
|
if (id < Util::CountOf(_actions))
|
|
|
|
{
|
|
|
|
GameActionFactory factory = _actions[id];
|
|
|
|
if (factory != nullptr)
|
|
|
|
{
|
|
|
|
result = factory();
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 12:36:45 +02:00
|
|
|
Guard::ArgumentNotNull(result, "Attempting to create unregistered gameaction: %u", id);
|
2017-07-21 19:54:05 +02:00
|
|
|
return std::unique_ptr<GameAction>(result);
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
static bool CheckActionInPausedMode(uint32_t actionFlags)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
|
|
|
if (gGamePaused == 0) return true;
|
|
|
|
if (gCheatsBuildInPauseMode) return true;
|
|
|
|
if (actionFlags & GA_FLAGS::ALLOW_WHILE_PAUSED) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckActionAffordability(const GameActionResult * result)
|
|
|
|
{
|
|
|
|
if (gParkFlags & PARK_FLAGS_NO_MONEY) return true;
|
|
|
|
if (result->Cost <= 0) return true;
|
2017-12-07 18:31:23 +01:00
|
|
|
if (result->Cost <= gCash) return true;
|
2017-01-11 23:53:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-21 19:54:05 +02:00
|
|
|
GameActionResult::Ptr Query(const GameAction * action)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
|
|
|
Guard::ArgumentNotNull(action);
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t actionFlags = action->GetActionFlags();
|
2017-01-11 23:53:33 +01:00
|
|
|
if (!CheckActionInPausedMode(actionFlags))
|
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
GameActionResult::Ptr result = std::make_unique<GameActionResult>();
|
|
|
|
|
|
|
|
result->Error = GA_ERROR::GAME_PAUSED;
|
|
|
|
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
|
|
|
|
|
|
|
gCommandPosition.x = result->Position.x;
|
|
|
|
gCommandPosition.y = result->Position.y;
|
|
|
|
gCommandPosition.z = result->Position.z;
|
|
|
|
|
2017-07-20 19:28:56 +02:00
|
|
|
if (result->Error == GA_ERROR::OK)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
if (!CheckActionAffordability(result.get()))
|
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;
|
2018-06-20 17:28:51 +02:00
|
|
|
set_format_arg_on(result->ErrorMessageArgs.data(), 0, uint32_t, result->Cost);
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 19:54:05 +02:00
|
|
|
GameActionResult::Ptr Execute(const GameAction * action)
|
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
|
|
|
|
2017-07-20 19:28:56 +02:00
|
|
|
GameActionResult::Ptr result = Query(action);
|
|
|
|
if (result->Error == GA_ERROR::OK)
|
2017-01-11 22:47:17 +01:00
|
|
|
{
|
2017-03-21 20:05:53 +01:00
|
|
|
// Networked games send actions to the server to be run
|
2017-07-15 11:24:08 +02:00
|
|
|
if (network_get_mode() == NETWORK_MODE_CLIENT)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2017-07-15 11:24:08 +02:00
|
|
|
// As a client we have to wait or send it first.
|
2017-09-04 21:37:58 +02:00
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED))
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2017-10-27 11:14:37 +02:00
|
|
|
log_verbose("[%s] GameAction::Execute\n", "cl");
|
2017-07-17 21:27:21 +02:00
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
network_send_game_action(action);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (network_get_mode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
|
|
|
// 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.
|
2017-09-04 21:37:58 +02:00
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED))
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2017-10-27 11:14:37 +02:00
|
|
|
log_verbose("[%s] GameAction::Execute\n", "sv-cl");
|
2017-07-15 11:24:08 +02:00
|
|
|
network_enqueue_game_action(action);
|
|
|
|
|
|
|
|
return result;
|
2017-03-21 20:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 11:14:37 +02:00
|
|
|
log_verbose("[%s] GameAction::Execute\n", "sv");
|
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-01-06 13:16:42 +01:00
|
|
|
gCommandPosition.x = result->Position.x;
|
|
|
|
gCommandPosition.y = result->Position.y;
|
|
|
|
gCommandPosition.z = result->Position.z;
|
|
|
|
|
2017-01-11 23:53:33 +01:00
|
|
|
// Update money balance
|
2018-01-06 13:16:42 +01:00
|
|
|
if (!(gParkFlags & PARK_FLAGS_NO_MONEY) &&
|
|
|
|
!(flags & GAME_COMMAND_FLAG_GHOST) &&
|
|
|
|
!(flags & GAME_COMMAND_FLAG_5) &&
|
|
|
|
result->Cost != 0)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
finance_payment(result->Cost, result->ExpenditureType);
|
|
|
|
money_effect_create(result->Cost);
|
2017-01-11 23:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY))
|
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
if (network_get_mode() == NETWORK_MODE_SERVER && result->Error == GA_ERROR::OK)
|
2017-01-11 23:53:33 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t playerId = action->GetPlayer();
|
2017-07-15 11:24:08 +02:00
|
|
|
|
|
|
|
network_set_player_last_action(network_get_player_index(playerId), action->GetType());
|
2017-09-30 23:57:38 +02:00
|
|
|
if (result->Cost != 0)
|
|
|
|
{
|
2017-07-20 19:28:56 +02:00
|
|
|
network_add_player_money_spent(playerId, result->Cost);
|
2017-09-30 23:57:38 +02:00
|
|
|
}
|
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
|
|
|
|
2018-01-06 13:16:42 +01:00
|
|
|
if (result->Error != GA_ERROR::OK &&
|
|
|
|
!(flags & GAME_COMMAND_FLAG_GHOST) &&
|
2018-05-14 12:36:45 +02:00
|
|
|
!(flags & GAME_COMMAND_FLAG_5))
|
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
|
|
|
}
|
2017-03-25 18:05:18 +01:00
|
|
|
return result;
|
2017-01-11 22:47:17 +01:00
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace GameActions
|