From d03b708dbb87691cb8f9c13cff8bd1a98837b54c Mon Sep 17 00:00:00 2001 From: afaure Date: Sat, 1 Jun 2024 15:29:59 +0200 Subject: [PATCH] WIP BlockActionPacket --- .../network/client/ClientPacketHandler.cpp | 27 +++- .../network/client/ClientPacketHandler.hpp | 2 + .../network/server/ServerPacketHandler.cpp | 19 ++- .../network/server/ServerPacketHandler.hpp | 5 + .../shared/packets/BlockActionPacket.cpp | 121 ++++++++++++++++++ .../shared/packets/BlockActionPacket.hpp | 43 +++++++ .../packets/CtoS/PlayerConnectedPacket.cpp | 11 +- .../packets/CtoS/PlayerConnectedPacket.hpp | 8 +- src/app/network/shared/packets/IPacket.hpp | 1 + src/app/network/shared/packets/Packets.hpp | 1 + .../shared/packets/StoC/ConnectionPacket.cpp | 19 ++- .../shared/packets/StoC/ConnectionPacket.hpp | 12 +- src/app/world/World.cpp | 11 +- 13 files changed, 252 insertions(+), 28 deletions(-) create mode 100644 src/app/network/shared/packets/BlockActionPacket.cpp create mode 100644 src/app/network/shared/packets/BlockActionPacket.hpp diff --git a/src/app/network/client/ClientPacketHandler.cpp b/src/app/network/client/ClientPacketHandler.cpp index 448f9c82..47286fde 100644 --- a/src/app/network/client/ClientPacketHandler.cpp +++ b/src/app/network/client/ClientPacketHandler.cpp @@ -17,24 +17,37 @@ void ClientPacketHandler::handlePacket(std::shared_ptr packet) { switch (packet->GetType()) { - case IPacket::Type::PLAYER_CONNECTED: - handlePlayerConnectedPacket(std::dynamic_pointer_cast(packet)); + case IPacket::Type::CONNECTION: + handleConnectionPacket(std::dynamic_pointer_cast(packet)); break; + // case IPacket::Type::PLAYER_CONNECTED: + // handlePlayerConnectedPacket(std::dynamic_pointer_cast(packet)); + // break; case IPacket::Type::PLAYER_MOVE: handlePlayerMovePacket(std::dynamic_pointer_cast(packet)); break; case IPacket::Type::DISCONNECT: handleDisconnectPacket(std::dynamic_pointer_cast(packet)); break; + case IPacket::Type::BLOCK_ACTION: + handleBlockActionPacket(std::dynamic_pointer_cast(packet)); + break; default: break; } } -void ClientPacketHandler::handlePlayerConnectedPacket(std::shared_ptr packet) +void ClientPacketHandler::handleConnectionPacket(std::shared_ptr packet) { LOG_INFO("Player connected: " << packet->GetId()); - m_world.addPlayer(packet->GetId(), glm::vec3(0)); + m_world.addPlayer(packet->GetId(), packet->GetPosition()); +} + +void ClientPacketHandler::handlePlayerConnectedPacket(std::shared_ptr packet) +{ + (void)packet; + // LOG_INFO("Player connected: " << packet->GetId()); + // m_world.addPlayer(packet->GetId()); } void ClientPacketHandler::handlePlayerMovePacket(std::shared_ptr packet) @@ -48,3 +61,9 @@ void ClientPacketHandler::handleDisconnectPacket(std::shared_ptrGetPlayerId()); m_world.removePlayer(packet->GetPlayerId()); } + +void ClientPacketHandler::handleBlockActionPacket(std::shared_ptr packet) +{ + //idk + (void)packet; +} diff --git a/src/app/network/client/ClientPacketHandler.hpp b/src/app/network/client/ClientPacketHandler.hpp index 4923a9ef..dff1075a 100644 --- a/src/app/network/client/ClientPacketHandler.hpp +++ b/src/app/network/client/ClientPacketHandler.hpp @@ -22,7 +22,9 @@ class ClientPacketHandler Client & m_client; World & m_world; + void handleConnectionPacket(std::shared_ptr packet); void handlePlayerMovePacket(std::shared_ptr packet); void handlePlayerConnectedPacket(std::shared_ptr packet); void handleDisconnectPacket(std::shared_ptr packet); + void handleBlockActionPacket(std::shared_ptr packet); }; diff --git a/src/app/network/server/ServerPacketHandler.cpp b/src/app/network/server/ServerPacketHandler.cpp index 4ff3cfab..7dae6275 100644 --- a/src/app/network/server/ServerPacketHandler.cpp +++ b/src/app/network/server/ServerPacketHandler.cpp @@ -28,6 +28,11 @@ void ServerPacketHandler::handlePacket(std::shared_ptr packet) handleDisconnectPacket(std::dynamic_pointer_cast(packet)); break; } + case IPacket::Type::BLOCK_ACTION: + { + mirrorPacket(packet); + break; + } default: { break; @@ -39,7 +44,7 @@ void ServerPacketHandler::handleConnectionPacket(std::shared_ptr(packet->GetId()); + auto packet_to_send = std::make_shared(*packet); packet_to_send->SetConnectionId(packet->GetConnectionId()); m_server.sendAllExcept(packet_to_send, packet->GetConnectionId()); @@ -47,7 +52,7 @@ void ServerPacketHandler::handleConnectionPacket(std::shared_ptr(player.first); + auto packet_to_send = std::make_shared(player.first, player.second); packet_to_send->SetConnectionId(packet->GetConnectionId()); m_server.send(packet_to_send); } @@ -86,3 +91,13 @@ void ServerPacketHandler::handlePlayerMovePacket(std::shared_ptrGetId()] = packet->GetPosition(); } + +void ServerPacketHandler::mirrorPacket(std::shared_ptr packet) +{ + m_server.sendAll(packet); +} + +void ServerPacketHandler::relayPacket(std::shared_ptr packet) +{ + m_server.sendAllExcept(packet, packet->GetConnectionId()); +} diff --git a/src/app/network/server/ServerPacketHandler.hpp b/src/app/network/server/ServerPacketHandler.hpp index 2a2fe40d..c0b242e5 100644 --- a/src/app/network/server/ServerPacketHandler.hpp +++ b/src/app/network/server/ServerPacketHandler.hpp @@ -26,4 +26,9 @@ class ServerPacketHandler void handleConnectionPacket(std::shared_ptr packet); void handlePlayerMovePacket(std::shared_ptr packet); void handleDisconnectPacket(std::shared_ptr packet); + void handleBlockActionPacket(std::shared_ptr packet); + + + void mirrorPacket(std::shared_ptr packet); + void relayPacket(std::shared_ptr packet); }; diff --git a/src/app/network/shared/packets/BlockActionPacket.cpp b/src/app/network/shared/packets/BlockActionPacket.cpp new file mode 100644 index 00000000..ab45ddf5 --- /dev/null +++ b/src/app/network/shared/packets/BlockActionPacket.cpp @@ -0,0 +1,121 @@ +#include "BlockActionPacket.hpp" + +BlockActionPacket::BlockActionPacket() +{ +} + +BlockActionPacket::BlockActionPacket(BlockID block_id, glm::ivec3 position, Action action) +: m_block_id(block_id), m_position(position), m_action(action) +{ +} + +BlockActionPacket::BlockActionPacket(const BlockActionPacket & other) +: IPacket(other), m_block_id(other.m_block_id), m_position(other.m_position), m_action(other.m_action) +{ +} + +BlockActionPacket & BlockActionPacket::operator=(const BlockActionPacket & other) +{ + if (this != &other) + { + m_block_id = other.m_block_id; + m_position = other.m_position; + m_action = other.m_action; + ::IPacket::operator=(other); + } + return *this; +} + +BlockActionPacket::BlockActionPacket(BlockActionPacket && other) +: IPacket(other), m_block_id(other.m_block_id), m_position(other.m_position), m_action(other.m_action) +{ +} + +BlockActionPacket & BlockActionPacket::operator=(BlockActionPacket && other) +{ + if (this != &other) + { + m_block_id = other.m_block_id; + m_position = other.m_position; + m_action = other.m_action; + ::IPacket::operator=(other); + } + return *this; +} + +BlockActionPacket::~BlockActionPacket() +{ +} + +void BlockActionPacket::Serialize(uint8_t * buffer) const +{ + uint32_t type = static_cast(GetType()); + memcpy(buffer, &type, sizeof(uint32_t)); + buffer += sizeof(uint32_t); + + memcpy(buffer, &m_block_id, sizeof(m_block_id)); + buffer += sizeof(m_block_id); + + memcpy(buffer, &m_position, sizeof(m_position)); + buffer += sizeof(m_position); + + memcpy(buffer, &m_action, sizeof(m_action)); + buffer += sizeof(m_action); +} + +void BlockActionPacket::Deserialize(const uint8_t * buffer) +{ + memcpy(&m_block_id, buffer, sizeof(m_block_id)); + buffer += sizeof(m_block_id); + + memcpy(&m_position, buffer, sizeof(m_position)); + buffer += sizeof(m_position); + + memcpy(&m_action, buffer, sizeof(m_action)); + buffer += sizeof(m_action); +} + +uint32_t BlockActionPacket::Size() const +{ + return sizeof(IPacket::Type) + sizeof(m_block_id) + sizeof(m_position) + sizeof(m_action); +} + +IPacket::Type BlockActionPacket::GetType() const +{ + return IPacket::Type::BLOCK_ACTION; +} + +std::shared_ptr BlockActionPacket::Clone() const +{ + return std::make_shared(); +} + +BlockID BlockActionPacket::GetBlockID() const +{ + return m_block_id; +} + +glm::ivec3 BlockActionPacket::GetPosition() const +{ + return m_position; +} + +BlockActionPacket::Action BlockActionPacket::GetAction() const +{ + return m_action; +} + +void BlockActionPacket::SetBlockID(BlockID block_id) +{ + m_block_id = block_id; +} + +void BlockActionPacket::SetPosition(glm::ivec3 position) +{ + m_position = position; +} + +void BlockActionPacket::SetAction(Action action) +{ + m_action = action; +} diff --git a/src/app/network/shared/packets/BlockActionPacket.hpp b/src/app/network/shared/packets/BlockActionPacket.hpp new file mode 100644 index 00000000..d827ac18 --- /dev/null +++ b/src/app/network/shared/packets/BlockActionPacket.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "IPacket.hpp" +#include "Block.hpp" + +class BlockActionPacket : public IPacket +{ +public: + enum class Action : uint8_t + { + PLACE + }; + + BlockActionPacket(); + BlockActionPacket(BlockID block_id, glm::ivec3 position, Action action); + ~BlockActionPacket(); + + BlockActionPacket(const BlockActionPacket& other); + BlockActionPacket& operator=(const BlockActionPacket& other); + + BlockActionPacket(BlockActionPacket&& other); + BlockActionPacket& operator=(BlockActionPacket&& other); + + + virtual void Serialize(uint8_t * buffer) const override; + virtual void Deserialize(const uint8_t * buffer) override; + virtual uint32_t Size() const override; + virtual IPacket::Type GetType() const override; + + virtual std::shared_ptr Clone() const override; + + BlockID GetBlockID() const; + glm::ivec3 GetPosition() const; + Action GetAction() const; + + void SetBlockID(BlockID block_id); + void SetPosition(glm::ivec3 position); + void SetAction(Action action); +private: + BlockID m_block_id; + glm::ivec3 m_position; + Action m_action; +}; diff --git a/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.cpp b/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.cpp index b8963068..843520a9 100644 --- a/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.cpp +++ b/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.cpp @@ -4,7 +4,7 @@ PlayerConnectedPacket::PlayerConnectedPacket() { } -PlayerConnectedPacket::PlayerConnectedPacket(const uint8_t & id) +PlayerConnectedPacket::PlayerConnectedPacket(const uint32_t & id) : m_id(id) { } @@ -30,14 +30,15 @@ void PlayerConnectedPacket::Serialize(uint8_t * buffer) const memcpy(buffer, &type, sizeof(uint32_t)); buffer += sizeof(uint32_t); - buffer[0] = m_id; + memcpy(buffer, &m_id, sizeof(m_id)); } void PlayerConnectedPacket::Deserialize(const uint8_t * buffer) { + //skipping type buffer += sizeof(uint32_t); - m_id = buffer[0]; + memcpy(&m_id, buffer, sizeof(m_id)); } uint32_t PlayerConnectedPacket::Size() const @@ -55,12 +56,12 @@ std::shared_ptr PlayerConnectedPacket::Clone() const return std::make_shared(); } -uint8_t PlayerConnectedPacket::GetId() const +uint32_t PlayerConnectedPacket::GetId() const { return m_id; } -void PlayerConnectedPacket::setId(uint8_t id) +void PlayerConnectedPacket::setId(uint32_t id) { m_id = id; } diff --git a/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.hpp b/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.hpp index 8abd3d82..6c0a14cf 100644 --- a/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.hpp +++ b/src/app/network/shared/packets/CtoS/PlayerConnectedPacket.hpp @@ -6,7 +6,7 @@ class PlayerConnectedPacket : public IPacket { public: PlayerConnectedPacket(); - PlayerConnectedPacket(const uint8_t & id); + PlayerConnectedPacket(const uint32_t & id); virtual ~PlayerConnectedPacket(); PlayerConnectedPacket(const PlayerConnectedPacket& other) = delete; @@ -22,9 +22,9 @@ class PlayerConnectedPacket : public IPacket std::shared_ptr Clone() const override; - uint8_t GetId() const; + uint32_t GetId() const; - void setId(uint8_t id); + void setId(uint32_t id); private: - uint8_t m_id; + uint32_t m_id; }; diff --git a/src/app/network/shared/packets/IPacket.hpp b/src/app/network/shared/packets/IPacket.hpp index f8c9445a..dfa05ff3 100644 --- a/src/app/network/shared/packets/IPacket.hpp +++ b/src/app/network/shared/packets/IPacket.hpp @@ -17,6 +17,7 @@ class IPacket PLAYER_MOVE = 2, ENTITY_MOVE = 3, DISCONNECT = 4, + BLOCK_ACTION = 5, }; virtual ~IPacket(); diff --git a/src/app/network/shared/packets/Packets.hpp b/src/app/network/shared/packets/Packets.hpp index 8ac4d912..adfe18dc 100644 --- a/src/app/network/shared/packets/Packets.hpp +++ b/src/app/network/shared/packets/Packets.hpp @@ -4,6 +4,7 @@ #include "PlayerConnectedPacket.hpp" #include "ConnectionPacket.hpp" #include "DisconnectPacket.hpp" +#include "BlockActionPacket.hpp" #include "Client.hpp" #include "Server.hpp" diff --git a/src/app/network/shared/packets/StoC/ConnectionPacket.cpp b/src/app/network/shared/packets/StoC/ConnectionPacket.cpp index 28c792e1..d8f590eb 100644 --- a/src/app/network/shared/packets/StoC/ConnectionPacket.cpp +++ b/src/app/network/shared/packets/StoC/ConnectionPacket.cpp @@ -5,12 +5,27 @@ ConnectionPacket::ConnectionPacket() } ConnectionPacket::ConnectionPacket(uint32_t id, glm::vec3 position) - : m_id(id), m_position(position) +: m_id(id), m_position(position) { } +ConnectionPacket::ConnectionPacket(const ConnectionPacket & other) +: m_id(other.m_id), m_position(other.m_position) +{ +} + +ConnectionPacket & ConnectionPacket::operator=(const ConnectionPacket & other) +{ + if (this != &other) + { + m_id = other.m_id; + m_position = other.m_position; + } + return *this; +} + ConnectionPacket::ConnectionPacket(ConnectionPacket && other) - : m_id(other.m_id), m_position(other.m_position) +: m_id(other.m_id), m_position(other.m_position) { } diff --git a/src/app/network/shared/packets/StoC/ConnectionPacket.hpp b/src/app/network/shared/packets/StoC/ConnectionPacket.hpp index 63c48698..0885f8a4 100644 --- a/src/app/network/shared/packets/StoC/ConnectionPacket.hpp +++ b/src/app/network/shared/packets/StoC/ConnectionPacket.hpp @@ -10,17 +10,17 @@ class ConnectionPacket : public IPacket ConnectionPacket(uint32_t id, glm::vec3 position); ~ConnectionPacket(); - ConnectionPacket(const ConnectionPacket& other) = delete; - ConnectionPacket& operator=(const ConnectionPacket& other) = delete; + ConnectionPacket(const ConnectionPacket& other); + ConnectionPacket& operator=(const ConnectionPacket& other); ConnectionPacket(ConnectionPacket&& other); ConnectionPacket& operator=(ConnectionPacket&& other); - virtual void Serialize(uint8_t * buffer) const override; - virtual void Deserialize(const uint8_t * buffer) override; - virtual uint32_t Size() const override; - virtual IPacket::Type GetType() const override; + virtual void Serialize(uint8_t * buffer) const override; + virtual void Deserialize(const uint8_t * buffer) override; + virtual uint32_t Size() const override; + virtual IPacket::Type GetType() const override; virtual std::shared_ptr Clone() const override; diff --git a/src/app/world/World.cpp b/src/app/world/World.cpp index 1948deff..1f22a4da 100644 --- a/src/app/world/World.cpp +++ b/src/app/world/World.cpp @@ -922,13 +922,14 @@ void World::addPlayer(const uint64_t player_id, const glm::vec3 & position) // auto world_scene_lock = m_worldScene.entity_mesh_list.lock(); m_worldScene.entity_mesh_list.insert( player_id, { - m_vulkanAPI.cube_mesh_id, Transform( - player->transform.position + player->hitbox.position, - glm::vec3(0.0f), - player->hitbox.size - ).model() + m_vulkanAPI.cube_mesh_id, {} } ); + m_worldScene.entity_mesh_list.at(player_id).model = Transform( + player->transform.position + player->hitbox.position, + glm::vec3(0.0f), + player->hitbox.size + ).model(); } }