Merge IGameAction into GameAction

This commit is contained in:
Ted John 2017-07-21 18:54:05 +01:00 committed by Michał Janiszewski
parent 2b57d36174
commit aa30859ab7
9 changed files with 114 additions and 152 deletions

View File

@ -63,11 +63,11 @@ namespace GameActions
initialized = true;
}
std::unique_ptr<IGameAction> Create(uint32 id)
std::unique_ptr<GameAction> Create(uint32 id)
{
Initialize();
IGameAction * result = nullptr;
GameAction * result = nullptr;
if (id < Util::CountOf(_actions))
{
GameActionFactory factory = _actions[id];
@ -76,7 +76,7 @@ namespace GameActions
result = factory();
}
}
return std::unique_ptr<IGameAction>(result);
return std::unique_ptr<GameAction>(result);
}
static bool CheckActionInPausedMode(uint32 actionFlags)
@ -95,7 +95,7 @@ namespace GameActions
return false;
}
GameActionResult::Ptr Query(const IGameAction * action)
GameActionResult::Ptr Query(const GameAction * action)
{
Guard::ArgumentNotNull(action);
@ -123,7 +123,7 @@ namespace GameActions
return result;
}
GameActionResult::Ptr Execute(const IGameAction * action)
GameActionResult::Ptr Execute(const GameAction * action)
{
Guard::ArgumentNotNull(action);
@ -191,7 +191,7 @@ namespace GameActions
}
// Call callback for asynchronous events
const GameActionCallback_t& cb = action->GetCallback();
const GameAction::GameActionCallback_t& cb = action->GetCallback();
if (cb)
{
cb(action, result);

View File

@ -79,52 +79,95 @@ struct GameActionResult
GameActionResult(const GameActionResult&) = delete;
};
typedef std::function<void(const struct IGameAction *, const GameActionResult::Ptr&)> GameActionCallback_t;
/**
* Represents an action that changes the state of the game. Can be serialised and
* deserialised into a stream.
*/
struct IGameAction
struct GameAction
{
public:
typedef std::unique_ptr<IGameAction> Ptr;
typedef std::unique_ptr<GameAction> Ptr;
typedef std::function<void(const struct GameAction *, const GameActionResult::Ptr&)> GameActionCallback_t;
private:
uint32 const _type;
uint16 const _actionFlags;
uint32 _playerId = 0; // Callee
uint32 _flags = 0; // GAME_COMMAND_FLAGS
uint32 _networkId = 0;
GameActionCallback_t _callback;
public:
GameAction(uint32 type, uint16 actionFlags)
: _type(type),
_actionFlags(actionFlags)
{
}
uint32 GetPlayer() const
{
return _playerId;
}
void SetPlayer(uint32 playerId)
{
_playerId = playerId;
}
/**
* Gets the GA_FLAGS flags that are enabled for this game action.
*/
virtual uint16 GetActionFlags() const abstract;
uint16 GetActionFlags() const
{
return _actionFlags;
}
/**
* Currently used for GAME_COMMAND_FLAGS, needs refactoring once everything is replaced.
*/
virtual uint32 GetFlags() const abstract;
virtual uint32 SetFlags(uint32 flags) abstract;
uint32 GetFlags() const
{
return _flags;
}
virtual uint32 GetType() const abstract;
uint32 SetFlags(uint32 flags)
{
return _flags = flags;
}
/**
* Gets/Sets player who owns this action, 0 if server or local client.
*/
virtual void SetPlayer(uint32 playerId) abstract;
virtual uint32 GetPlayer() const abstract;
uint32 GetType() const
{
return _type;
}
virtual void SetCallback(const GameActionCallback_t& cb) abstract;
virtual const GameActionCallback_t& GetCallback() const abstract;
void SetCallback(const GameActionCallback_t& cb)
{
_callback = cb;
}
virtual void SetNetworkId(uint32_t id) abstract;
virtual uint32 GetNetworkId() const abstract;
const GameActionCallback_t& GetCallback() const
{
return _callback;
}
/**
* Writes or reads the game action directly to the given stream. Used for
* sending across the network in multiplayer.
*/
virtual void Serialise(DataSerialiser& stream) abstract;
void SetNetworkId(uint32_t id)
{
_networkId = id;
}
uint32 GetNetworkId() const
{
return _networkId;
}
virtual void Serialise(DataSerialiser& stream)
{
stream << _networkId;
stream << _flags;
stream << _playerId;
}
// Helper function, allows const Objects to still serialize into DataSerialiser while being const.
void Serialise(DataSerialiser& stream) const
{
return const_cast<IGameAction&>(*this).Serialise(stream);
return const_cast<GameAction&>(*this).Serialise(stream);
}
/**
@ -135,121 +178,40 @@ public:
/**
* Apply the game action and change the game state.
*/
virtual ~IGameAction() {};
virtual GameActionResult::Ptr Execute() const abstract;
};
typedef IGameAction *(*GameActionFactory)();
template<uint32 TType, uint16 TActionFlags>
struct GameAction : public IGameAction
{
struct GameActionBase : GameAction
{
public:
constexpr static uint32 Type = TType;
constexpr static uint16 ActionFlags = TActionFlags;
static constexpr uint32 TYPE = TType;
private:
uint32 _playerId; // Callee
uint32 _flags; // GAME_COMMAND_FLAGS
uint32 _networkId;
GameActionCallback_t _callback;
public:
GameAction() : _playerId(0), _flags(0), _networkId(0)
GameActionBase()
: GameAction(TYPE, TActionFlags)
{
}
virtual void SetPlayer(uint32 playerId) override final
{
_playerId = playerId;
}
virtual uint32 GetPlayer() const override final
{
return _playerId;
}
/**
* Gets the GA_FLAGS flags that are enabled for this game action.
*/
virtual uint16 GetActionFlags() const override final
{
return ActionFlags;
}
/**
* Currently used for GAME_COMMAND_FLAGS, needs refactoring once everything is replaced.
*/
virtual uint32 GetFlags() const override final
{
return _flags;
}
virtual uint32 SetFlags(uint32 flags) override final
{
return _flags = flags;
}
virtual uint32 GetType() const override final
{
return Type;
}
virtual void SetCallback(const GameActionCallback_t& cb) override final
{
_callback = cb;
}
virtual const GameActionCallback_t& GetCallback() const override final
{
return _callback;
}
virtual void SetNetworkId(uint32_t id) override final
{
_networkId = id;
}
virtual uint32 GetNetworkId() const override final
{
return _networkId;
}
virtual void Serialise(DataSerialiser& stream) override
{
stream << _networkId;
stream << _flags;
stream << _playerId;
}
/**
* Query the result of the game action without changing the game state.
*/
virtual GameActionResult::Ptr Query() const override abstract;
/**
* Apply the game action and change the game state.
*/
virtual GameActionResult::Ptr Execute() const override abstract;
};
typedef GameAction *(*GameActionFactory)();
namespace GameActions
{
void Initialize();
void Register();
IGameAction::Ptr Create(uint32 id);
GameActionResult::Ptr Query(const IGameAction * action);
GameActionResult::Ptr Execute(const IGameAction * action);
GameAction::Ptr Create(uint32 id);
GameActionResult::Ptr Query(const GameAction * action);
GameActionResult::Ptr Execute(const GameAction * action);
GameActionFactory Register(uint32 id, GameActionFactory action);
template<typename T>
static GameActionFactory Register()
{
GameActionFactory factory = []() -> IGameAction *
GameActionFactory factory = []() -> GameAction *
{
return new T();
};
Register(T::Type, factory);
Register(T::TYPE, factory);
return factory;
}
}

View File

@ -109,7 +109,7 @@ extern "C"
auto gameAction = RideCreateAction();
gameAction.rideType = listItem.type;
gameAction.rideSubType = listItem.entry_index;
gameAction.SetCallback([](const IGameAction *ga, const GameActionResult::Ptr& res)
gameAction.SetCallback([](const GameAction *ga, const GameActionResult::Ptr& res)
{
if (res->Error != GA_ERROR::OK)
return;

View File

@ -37,7 +37,7 @@ struct PlaceParkEntranceGameActionResult : public GameActionResult
}
};
struct PlaceParkEntranceAction : public GameAction<GAME_COMMAND_PLACE_PARK_ENTRANCE, GA_FLAGS::EDITOR_ONLY>
struct PlaceParkEntranceAction : public GameActionBase<GAME_COMMAND_PLACE_PARK_ENTRANCE, GA_FLAGS::EDITOR_ONLY>
{
public:
sint16 x;

View File

@ -41,7 +41,7 @@ struct RideCreateGameActionResult : public GameActionResult
uint32 rideColor;
};
struct RideCreateAction : public GameAction<GAME_COMMAND_CREATE_RIDE, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct RideCreateAction : public GameActionBase<GAME_COMMAND_CREATE_RIDE, GA_FLAGS::ALLOW_WHILE_PAUSED>
{
public:
sint32 rideType;

View File

@ -28,7 +28,7 @@ extern "C"
#include "../ride/ride.h"
}
struct RideSetStatusAction : public GameAction<GAME_COMMAND_SET_RIDE_STATUS, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct RideSetStatusAction : public GameActionBase<GAME_COMMAND_SET_RIDE_STATUS, GA_FLAGS::ALLOW_WHILE_PAUSED>
{
public:
uint8 RideIndex;

View File

@ -27,7 +27,7 @@ extern "C"
#include "../world/park.h"
}
struct SetParkEntranceFeeAction : public GameAction<GAME_COMMAND_SET_PARK_ENTRANCE_FEE, GA_FLAGS::ALLOW_WHILE_PAUSED>
struct SetParkEntranceFeeAction : public GameActionBase<GAME_COMMAND_SET_PARK_ENTRANCE_FEE, GA_FLAGS::ALLOW_WHILE_PAUSED>
{
public:
money16 Fee;

View File

@ -1148,7 +1148,7 @@ void Network::Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx
SendPacketToClients(*packet, false, true);
}
void Network::Client_Send_GAME_ACTION(const IGameAction *action)
void Network::Client_Send_GAME_ACTION(const GameAction *action)
{
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
@ -1156,7 +1156,7 @@ void Network::Client_Send_GAME_ACTION(const IGameAction *action)
networkId = ++_actionId;
// I know its ugly, want basic functionality for now.
const_cast<IGameAction*>(action)->SetNetworkId(networkId);
const_cast<GameAction*>(action)->SetNetworkId(networkId);
if(action->GetCallback())
{
_gameActionCallbacks.insert(std::make_pair(networkId, action->GetCallback()));
@ -1170,7 +1170,7 @@ void Network::Client_Send_GAME_ACTION(const IGameAction *action)
server_connection->QueuePacket(std::move(packet));
}
void Network::Server_Send_GAME_ACTION(const IGameAction *action)
void Network::Server_Send_GAME_ACTION(const GameAction *action)
{
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
@ -1415,7 +1415,7 @@ void Network::ProcessGameCommandQueue()
if (gc.action != nullptr) {
IGameAction *action = gc.action.get();
GameAction *action = gc.action.get();
action->SetFlags(action->GetFlags() | GAME_COMMAND_FLAG_NETWORKED);
Guard::Assert(action != nullptr);
@ -1476,13 +1476,13 @@ void Network::ProcessGameCommandQueue()
}
}
void Network::EnqueueGameAction(const IGameAction *action)
void Network::EnqueueGameAction(const GameAction *action)
{
MemoryStream stream;
DataSerialiser dsOut(true, stream);
action->Serialise(dsOut);
std::unique_ptr<IGameAction> ga = GameActions::Create(action->GetType());
std::unique_ptr<GameAction> ga = GameActions::Create(action->GetType());
ga->SetCallback(action->GetCallback());
stream.SetPosition(0);
@ -2126,7 +2126,7 @@ void Network::Client_Handle_GAME_ACTION(NetworkConnection& connection, NetworkPa
DataSerialiser ds(false, stream);
IGameAction::Ptr action = GameActions::Create(type);
GameAction::Ptr action = GameActions::Create(type);
if (!action)
{
// TODO: Handle error.
@ -2199,7 +2199,7 @@ void Network::Server_Handle_GAME_ACTION(NetworkConnection& connection, NetworkPa
}
// Run game command, and if it is successful send to clients
IGameAction::Ptr ga = GameActions::Create(type);
GameAction::Ptr ga = GameActions::Create(type);
if (!ga)
{
// TODO: Handle error.
@ -3078,7 +3078,7 @@ void network_send_chat(const char* text)
}
}
void network_send_game_action(const IGameAction *action)
void network_send_game_action(const GameAction *action)
{
switch (gNetwork.GetMode()) {
case NETWORK_MODE_SERVER:
@ -3090,7 +3090,7 @@ void network_send_game_action(const IGameAction *action)
}
}
void network_enqueue_game_action(const IGameAction *action)
void network_enqueue_game_action(const GameAction *action)
{
gNetwork.EnqueueGameAction(action);
}
@ -3187,9 +3187,9 @@ uint32 network_get_server_tick() { return gCurrentTicks; }
void network_flush() {}
void network_send_tick() {}
void network_check_desynchronization() {}
void network_enqueue_game_action(const IGameAction *action) {}
void network_enqueue_game_action(const GameAction *action) {}
void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback) {}
void network_send_game_action(const IGameAction *action) {}
void network_send_game_action(const GameAction *action) {}
void network_send_map() {}
void network_update() {}
void network_process_game_commands() {}

View File

@ -44,7 +44,7 @@ enum {
#include "../Version.h"
#include "NetworkTypes.h"
typedef struct IGameAction IGameAction;
typedef struct GameAction GameAction;
#ifndef DISABLE_NETWORK
@ -107,7 +107,7 @@ public:
void Update();
void Flush();
void ProcessGameCommandQueue();
void EnqueueGameAction(const IGameAction *action);
void EnqueueGameAction(const GameAction *action);
std::vector<std::unique_ptr<NetworkPlayer>>::iterator GetPlayerIteratorByID(uint8 id);
NetworkPlayer* GetPlayerByID(uint8 id);
std::vector<std::unique_ptr<NetworkGroup>>::iterator GetGroupIteratorByID(uint8 id);
@ -148,8 +148,8 @@ public:
void Server_Send_CHAT(const char* text);
void Client_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback);
void Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 playerid, uint8 callback);
void Client_Send_GAME_ACTION(const IGameAction *action);
void Server_Send_GAME_ACTION(const IGameAction *action);
void Client_Send_GAME_ACTION(const GameAction *action);
void Server_Send_GAME_ACTION(const GameAction *action);
void Server_Send_TICK();
void Server_Send_PLAYERLIST();
void Client_Send_PING();
@ -169,7 +169,7 @@ public:
std::vector<std::unique_ptr<NetworkGroup>> group_list;
NetworkKey _key;
std::vector<uint8> _challenge;
std::map<uint32, GameActionCallback_t> _gameActionCallbacks;
std::map<uint32, GameAction::GameActionCallback_t> _gameActionCallbacks;
NetworkUserManager _userManager;
std::string ServerName;
@ -203,7 +203,7 @@ private:
commandIndex = id;
}
GameCommand(uint32 t, std::unique_ptr<IGameAction>&& ga, uint32 id)
GameCommand(uint32 t, std::unique_ptr<GameAction>&& ga, uint32 id)
{
tick = t;
action = std::move(ga);
@ -216,7 +216,7 @@ private:
uint32 tick;
uint32 eax, ebx, ecx, edx, esi, edi, ebp;
IGameAction::Ptr action;
GameAction::Ptr action;
uint8 playerid;
uint8 callback;
uint32 commandIndex;
@ -354,8 +354,8 @@ sint32 network_get_pickup_peep_old_x(uint8 playerid);
void network_send_map();
void network_send_chat(const char* text);
void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback);
void network_send_game_action(const IGameAction *action);
void network_enqueue_game_action(const IGameAction *action);
void network_send_game_action(const GameAction *action);
void network_enqueue_game_action(const GameAction *action);
void network_send_password(const char* password);
void network_set_password(const char* password);