From aa30859ab7d70e4bdd880482b1b2a3dd448bd4c6 Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 21 Jul 2017 18:54:05 +0100 Subject: [PATCH] Merge IGameAction into GameAction --- src/openrct2/actions/GameAction.cpp | 12 +- src/openrct2/actions/GameAction.h | 202 +++++++----------- src/openrct2/actions/GameActionCompat.cpp | 2 +- .../actions/PlaceParkEntranceAction.hpp | 2 +- src/openrct2/actions/RideCreateAction.hpp | 2 +- src/openrct2/actions/RideSetStatus.hpp | 2 +- .../actions/SetParkEntranceFeeAction.hpp | 2 +- src/openrct2/network/Network.cpp | 24 +-- src/openrct2/network/network.h | 18 +- 9 files changed, 114 insertions(+), 152 deletions(-) diff --git a/src/openrct2/actions/GameAction.cpp b/src/openrct2/actions/GameAction.cpp index 4fd4dc107e..e3a9bdabde 100644 --- a/src/openrct2/actions/GameAction.cpp +++ b/src/openrct2/actions/GameAction.cpp @@ -63,11 +63,11 @@ namespace GameActions initialized = true; } - std::unique_ptr Create(uint32 id) + std::unique_ptr 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(result); + return std::unique_ptr(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); diff --git a/src/openrct2/actions/GameAction.h b/src/openrct2/actions/GameAction.h index a71abbf7ea..4eda16ce02 100644 --- a/src/openrct2/actions/GameAction.h +++ b/src/openrct2/actions/GameAction.h @@ -79,52 +79,95 @@ struct GameActionResult GameActionResult(const GameActionResult&) = delete; }; -typedef std::function 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 Ptr; + typedef std::unique_ptr Ptr; + typedef std::function 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(*this).Serialise(stream); + return const_cast(*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 -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 static GameActionFactory Register() { - GameActionFactory factory = []() -> IGameAction * + GameActionFactory factory = []() -> GameAction * { return new T(); }; - Register(T::Type, factory); + Register(T::TYPE, factory); return factory; } } diff --git a/src/openrct2/actions/GameActionCompat.cpp b/src/openrct2/actions/GameActionCompat.cpp index 70549d868a..1fa6cdc12c 100644 --- a/src/openrct2/actions/GameActionCompat.cpp +++ b/src/openrct2/actions/GameActionCompat.cpp @@ -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; diff --git a/src/openrct2/actions/PlaceParkEntranceAction.hpp b/src/openrct2/actions/PlaceParkEntranceAction.hpp index 7feb130b49..c7d7ce748f 100644 --- a/src/openrct2/actions/PlaceParkEntranceAction.hpp +++ b/src/openrct2/actions/PlaceParkEntranceAction.hpp @@ -37,7 +37,7 @@ struct PlaceParkEntranceGameActionResult : public GameActionResult } }; -struct PlaceParkEntranceAction : public GameAction +struct PlaceParkEntranceAction : public GameActionBase { public: sint16 x; diff --git a/src/openrct2/actions/RideCreateAction.hpp b/src/openrct2/actions/RideCreateAction.hpp index a1d9787015..e7b1a2d97e 100644 --- a/src/openrct2/actions/RideCreateAction.hpp +++ b/src/openrct2/actions/RideCreateAction.hpp @@ -41,7 +41,7 @@ struct RideCreateGameActionResult : public GameActionResult uint32 rideColor; }; -struct RideCreateAction : public GameAction +struct RideCreateAction : public GameActionBase { public: sint32 rideType; diff --git a/src/openrct2/actions/RideSetStatus.hpp b/src/openrct2/actions/RideSetStatus.hpp index 1a666cdea6..f2652673f2 100644 --- a/src/openrct2/actions/RideSetStatus.hpp +++ b/src/openrct2/actions/RideSetStatus.hpp @@ -28,7 +28,7 @@ extern "C" #include "../ride/ride.h" } -struct RideSetStatusAction : public GameAction +struct RideSetStatusAction : public GameActionBase { public: uint8 RideIndex; diff --git a/src/openrct2/actions/SetParkEntranceFeeAction.hpp b/src/openrct2/actions/SetParkEntranceFeeAction.hpp index 7784fa5548..c2ec3d3eb3 100644 --- a/src/openrct2/actions/SetParkEntranceFeeAction.hpp +++ b/src/openrct2/actions/SetParkEntranceFeeAction.hpp @@ -27,7 +27,7 @@ extern "C" #include "../world/park.h" } -struct SetParkEntranceFeeAction : public GameAction +struct SetParkEntranceFeeAction : public GameActionBase { public: money16 Fee; diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index e61ca11acd..15810a8aa3 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -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 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(action)->SetNetworkId(networkId); + const_cast(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 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 ga = GameActions::Create(action->GetType()); + std::unique_ptr 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() {} diff --git a/src/openrct2/network/network.h b/src/openrct2/network/network.h index 7fcaca8520..75c85b497e 100644 --- a/src/openrct2/network/network.h +++ b/src/openrct2/network/network.h @@ -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>::iterator GetPlayerIteratorByID(uint8 id); NetworkPlayer* GetPlayerByID(uint8 id); std::vector>::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> group_list; NetworkKey _key; std::vector _challenge; - std::map _gameActionCallbacks; + std::map _gameActionCallbacks; NetworkUserManager _userManager; std::string ServerName; @@ -203,7 +203,7 @@ private: commandIndex = id; } - GameCommand(uint32 t, std::unique_ptr&& ga, uint32 id) + GameCommand(uint32 t, std::unique_ptr&& 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);