diff --git a/include/game/client/Client.h b/include/game/client/Client.h index 90378a7..00a03b4 100644 --- a/include/game/client/Client.h +++ b/include/game/client/Client.h @@ -7,6 +7,7 @@ #include "game/Player.h" #include "protocol/Protocol.h" +#include "protocol/packets/SendMobsPacket.h" #include "render/Renderer.h" diff --git a/include/protocol/PacketHandler.h b/include/protocol/PacketHandler.h index fb06092..030ae35 100644 --- a/include/protocol/PacketHandler.h +++ b/include/protocol/PacketHandler.h @@ -1,6 +1,7 @@ #pragma once #include "protocol/Protocol.h" +#include "protocol/PacketsForward.h" namespace td { namespace protocol { diff --git a/include/protocol/Packets.h b/include/protocol/Packets.h new file mode 100644 index 0000000..4486f04 --- /dev/null +++ b/include/protocol/Packets.h @@ -0,0 +1,27 @@ +#include "packets/ConnectionInfoPacket.h" +#include "packets/DisconnectPacket.h" +#include "packets/KeepAlivePacket.h" +#include "packets/PlaceTowerPacket.h" +#include "packets/PlayerBuyItemPacket.h" +#include "packets/PlayerBuyMobUpgradePacket.h" +#include "packets/PlayerJoinPacket.h" +#include "packets/PlayerLeavePacket.h" +#include "packets/PlayerListPacket.h" +#include "packets/PlayerLoginPacket.h" +#include "packets/RemoveTowerPacket.h" +#include "packets/SelectTeamPacket.h" +#include "packets/SendMobsPacket.h" +#include "packets/ServerTpsPacket.h" +#include "packets/SpawnMobPacket.h" +#include "packets/UpdateCastleLifePacket.h" +#include "packets/UpdateExpPacket.h" +#include "packets/UpdateGameStatePacket.h" +#include "packets/UpdateLobbyTimePacket.h" +#include "packets/UpdateMobStatesPacket.h" +#include "packets/UpdateMoneyPacket.h" +#include "packets/UpdatePlayerTeamPacket.h" +#include "packets/UpgradeTowerPacket.h" +#include "packets/WorldAddTowerPacket.h" +#include "packets/WorldAddTowerPacket.h" +#include "packets/WorldBeginDataPacket.h" +#include "packets/WorldDataPacket.h" \ No newline at end of file diff --git a/include/protocol/PacketsForward.h b/include/protocol/PacketsForward.h new file mode 100644 index 0000000..cdb522f --- /dev/null +++ b/include/protocol/PacketsForward.h @@ -0,0 +1,34 @@ +#pragma once + +namespace td { +namespace protocol { + +class PlayerLoginPacket; +class WorldBeginDataPacket; +class WorldDataPacket; +class KeepAlivePacket; +class UpdateExpPacket; +class UpdateMoneyPacket; +class UpdateLobbyTimePacket; +class UpdateGameStatePacket; +class PlayerListPacket; +class PlayerJoinPacket; +class PlayerLeavePacket; +class ConnexionInfoPacket; +class SelectTeamPacket; +class UpdatePlayerTeamPacket; +class DisconnectPacket; +class ServerTpsPacket; +class SpawnMobPacket; +class PlaceTowerPacket; +class WorldAddTowerPacket; +class RemoveTowerPacket; +class SendMobsPacket; +class UpgradeTowerPacket; +class UpdateCastleLifePacket; +class UpdateMobStatesPacket; +class PlayerBuyItemPacket; +class PlayerBuyMobUpgradePacket; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/Protocol.h b/include/protocol/Protocol.h index fbddf47..33f74d1 100644 --- a/include/protocol/Protocol.h +++ b/include/protocol/Protocol.h @@ -1,8 +1,6 @@ #pragma once #include "misc/DataBuffer.h" -#include "game/World.h" -#include "game/BaseGame.h" #include @@ -47,26 +45,6 @@ enum class PacketType : std::uint8_t { PACKET_COUNT }; -struct WorldHeader { - game::TowerTileColorPalette m_TowerPlacePalette; - Color m_WalkablePalette; - std::vector m_DecorationPalette; - Color m_Background; - - game::SpawnColorPalette m_SpawnColorPalette; - - game::TilePalette m_TilePalette; - - game::Spawn m_RedSpawn, m_BlueSpawn; - game::TeamCastle m_RedCastle, m_BlueCastle; - - const game::World* m_World; -}; - -struct WorldData { - std::unordered_map m_Chunks; -}; - class Packet { public: Packet() {} @@ -84,556 +62,5 @@ public: typedef std::unique_ptr PacketPtr; -class KeepAlivePacket : public Packet { -private: - std::uint64_t m_AliveID; -public: - KeepAlivePacket() {} - KeepAlivePacket(std::uint64_t aliveID) : m_AliveID(aliveID) {} - virtual ~KeepAlivePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint64_t GetAliveID() const { return m_AliveID; } - - virtual PacketType GetType() const { return PacketType::KeepAlive; } -}; - -class PlayerLoginPacket : public Packet { -private: - std::string m_PlayerName; -public: - PlayerLoginPacket() {} - PlayerLoginPacket(std::string playerName) : m_PlayerName(playerName) {} - virtual ~PlayerLoginPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - virtual PacketType GetType() const { return PacketType::PlayerLogin; } - - const std::string& GetPlayerName() const { return m_PlayerName; } -}; - -class WorldBeginDataPacket : public Packet { -private: - WorldHeader m_Header; -public: - WorldBeginDataPacket() {} - WorldBeginDataPacket(const game::World* world) { - m_Header.m_World = world; - } - virtual ~WorldBeginDataPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - virtual PacketType GetType() const { return PacketType::WorldBeginData; } - - const game::TowerTileColorPalette& GetTowerTilePalette() const { return m_Header.m_TowerPlacePalette; } - const Color& GetWalkableTileColor() const { return m_Header.m_WalkablePalette; } - const std::vector& GetDecorationPalette() const { return m_Header.m_DecorationPalette; } - const Color& GetBackgroundColor() const { return m_Header.m_Background; } - - const game::Spawn& GetRedSpawn() const { return m_Header.m_RedSpawn; } - const game::Spawn& GetBlueSpawn() const { return m_Header.m_BlueSpawn; } - - const game::SpawnColorPalette& GetSpawnPalette() const { return m_Header.m_SpawnColorPalette; } - - const game::TeamCastle& GetRedCastle() const { return m_Header.m_RedCastle; } - const game::TeamCastle& GetBlueCastle() const { return m_Header.m_BlueCastle; } - - const game::TilePalette GetTilePalette() const { return m_Header.m_TilePalette; } - - void setWorldHeader(const WorldHeader& header) { m_Header = header; } -}; - -class WorldDataPacket : public Packet { -private: - WorldData m_WorldData; - - const game::World* m_World; -public: - WorldDataPacket() {} - WorldDataPacket(const game::World* world) : m_World(world) {} - virtual ~WorldDataPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - virtual PacketType GetType() const { return PacketType::WorldData; } - - const std::unordered_map& GetChunks() const { return m_WorldData.m_Chunks; } - - DataBuffer SerializeCustom() const; // allow serialisation with invalid World member - void SetWorldData(const WorldData& worldData) { m_WorldData = worldData; } -}; - -class UpdateMoneyPacket : public Packet { -private: - std::uint32_t m_NewAmount; -public: - UpdateMoneyPacket() {} - UpdateMoneyPacket(std::uint32_t newAmount) : m_NewAmount(newAmount) {} - virtual ~UpdateMoneyPacket() {} - - std::uint32_t GetGold() const { return m_NewAmount; } - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - virtual PacketType GetType() const { return PacketType::UpdateMoney; } -}; - -class UpdateExpPacket : public Packet { -private: - std::uint32_t m_NewAmount; -public: - UpdateExpPacket() {} - UpdateExpPacket(std::uint32_t newAmount) : m_NewAmount(newAmount) {} - virtual ~UpdateExpPacket() {} - - std::uint32_t GetExp() const { return m_NewAmount; } - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - virtual PacketType GetType() const { return PacketType::UpdateEXP; } -}; - -class UpdateLobbyTimePacket : public Packet { -private: - std::uint32_t m_RemainingTime; -public: - UpdateLobbyTimePacket() {} - UpdateLobbyTimePacket(std::uint32_t remainingTime) : m_RemainingTime(remainingTime) {} - virtual ~UpdateLobbyTimePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint32_t GetRemainingTime() const { return m_RemainingTime; } - - virtual PacketType GetType() const { return PacketType::UpdateLobbyTime; } -}; - -class UpdateGameStatePacket : public Packet { -private: - game::GameState m_GameState; -public: - UpdateGameStatePacket() {} - UpdateGameStatePacket(game::GameState gameState) : m_GameState(gameState) {} - virtual ~UpdateGameStatePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::GameState GetGameState() const { return m_GameState; } - - virtual PacketType GetType() const { return PacketType::UpdateGameState; } -}; - -struct PlayerInfo { - std::string name; - game::TeamColor team; -}; - -class PlayerListPacket : public Packet { -private: - std::map m_Players; -public: - PlayerListPacket() {} - PlayerListPacket(std::map players) : m_Players(players) {} - virtual ~PlayerListPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - const std::map& GetPlayers() const { return m_Players; } - - virtual PacketType GetType() const { return PacketType::PlayerList; } -}; - -class PlayerJoinPacket : public Packet { -private: - std::uint8_t m_PlayerID; - std::string m_PlayerName; -public: - PlayerJoinPacket() {} - PlayerJoinPacket(std::uint8_t playerID, const std::string& playerName) : m_PlayerID(playerID), m_PlayerName(playerName) {} - virtual ~PlayerJoinPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint8_t GetPlayerID() const { return m_PlayerID; } - const std::string& GetPlayerName() const { return m_PlayerName; } - - virtual PacketType GetType() const { return PacketType::PlayerJoin; } -}; - -class PlayerLeavePacket : public Packet { -private: - std::uint8_t m_PlayerID; -public: - PlayerLeavePacket() {} - PlayerLeavePacket(std::uint8_t playerID) : m_PlayerID(playerID) {} - virtual ~PlayerLeavePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint8_t GetPlayerID() const { return m_PlayerID; } - - virtual PacketType GetType() const { return PacketType::PlayerLeave; } -}; - -class ConnexionInfoPacket : public Packet { -private: - std::uint8_t m_ConnectionID; -public: - ConnexionInfoPacket() {} - ConnexionInfoPacket(std::uint8_t connectionID) : m_ConnectionID(connectionID) {} - virtual ~ConnexionInfoPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint8_t GetConnectionID() const { return m_ConnectionID; } - - virtual PacketType GetType() const { return PacketType::ConnectionInfo; } -}; - -class SelectTeamPacket : public Packet { -private: - game::TeamColor m_SelectedTeam; -public: - SelectTeamPacket() {} - SelectTeamPacket(game::TeamColor selectedTeam) : m_SelectedTeam(selectedTeam) {} - virtual ~SelectTeamPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::TeamColor GetSelectedTeam() const { return m_SelectedTeam; } - - virtual PacketType GetType() const { return PacketType::SelectTeam; } -}; - -class UpdatePlayerTeamPacket : public Packet { -private: - std::uint8_t m_PlayerID; - game::TeamColor m_SelectedTeam; -public: - UpdatePlayerTeamPacket() {} - UpdatePlayerTeamPacket(std::uint8_t playerID, game::TeamColor selectedTeam) : m_PlayerID(playerID), m_SelectedTeam(selectedTeam) {} - virtual ~UpdatePlayerTeamPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::TeamColor GetSelectedTeam() const { return m_SelectedTeam; } - std::uint8_t GetPlayerID() const { return m_PlayerID; } - - virtual PacketType GetType() const { return PacketType::UpdatePlayerTeam; } -}; - -class DisconnectPacket : public Packet { -private: - std::string m_Reason; // only when sent from server -public: - DisconnectPacket() {} - DisconnectPacket(std::string reason) : m_Reason(reason) {} - virtual ~DisconnectPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - const std::string& GetReason() const { return m_Reason; } - - virtual PacketType GetType() const { return PacketType::Disconnect; } -}; - -class ServerTpsPacket : public Packet { -private: - float m_TPS; - float m_MSPT; - std::uint64_t m_PacketSendTime; // used to calculate ping -public: - ServerTpsPacket() {} - ServerTpsPacket(float tps, float mspt, std::uint64_t sendTime) : m_TPS(tps), m_MSPT(mspt), m_PacketSendTime(sendTime) {} - virtual ~ServerTpsPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - float GetTPS() const { return m_TPS; } - float GetMSPT() const { return m_MSPT; } - std::uint64_t GetPacketSendTime() const { return m_PacketSendTime; } - - virtual PacketType GetType() const { return PacketType::ServerTps; } -}; - -struct MobSend { // represents a mob send - game::MobType mobType : 4; - game::MobLevel mobLevel : 4; - std::uint8_t mobCount; // the max is 12 -}; - -class SendMobsPacket : public Packet { -private: - std::vector m_MobSends; -public: - SendMobsPacket() {} - SendMobsPacket(const std::vector& mobSends) : m_MobSends(mobSends) {} - virtual ~SendMobsPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - const std::vector& GetMobSends() const { return m_MobSends; } - - virtual PacketType GetType() const { return PacketType::SendMobs; } -}; - -class SpawnMobPacket : public Packet { -private: - game::MobID m_MobID; - game::MobType m_MobType; - game::MobLevel m_MobLevel; - game::Direction m_MobDirection; - game::PlayerID m_Sender; - float m_MobX, m_MobY; -public: - SpawnMobPacket() {} - SpawnMobPacket(game::MobID id, game::MobType type, std::uint8_t level, game::PlayerID sender, - float x, float y, game::Direction dir) : m_MobID(id), m_MobType(type), m_MobLevel(level), - m_MobDirection(dir), m_Sender(sender), m_MobX(x), m_MobY(y) { - } - virtual ~SpawnMobPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::MobID GetMobID() const { return m_MobID; } - game::MobType GetMobType() const { return m_MobType; } - game::MobLevel GetMobLevel() const { return m_MobLevel; } - game::Direction GetMobDirection() const { return m_MobDirection; } - game::PlayerID GetSender() const { return m_Sender; } - float GetMobX() const { return m_MobX; } - float GetMobY() const { return m_MobY; } - - virtual PacketType GetType() const { return PacketType::SpawnMob; } -}; - -class PlaceTowerPacket : public Packet { -private: - std::int32_t m_TowerX, m_TowerY; - game::TowerType m_TowerType; -public: - PlaceTowerPacket() {} - PlaceTowerPacket(std::int32_t x, std::int32_t y, game::TowerType type) : - m_TowerX(x), m_TowerY(y), m_TowerType(type) { - } - virtual ~PlaceTowerPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::int32_t GetTowerX() const { return m_TowerX; } - std::int32_t GetTowerY() const { return m_TowerY; } - game::TowerType GetTowerType() const { return m_TowerType; } - - virtual PacketType GetType() const { return PacketType::PlaceTower; } -}; - -class WorldAddTowerPacket : public Packet { -private: - game::TowerID m_TowerID; - std::int32_t m_TowerX, m_TowerY; - game::TowerType m_TowerType; - game::PlayerID m_Builder; -public: - WorldAddTowerPacket() {} - WorldAddTowerPacket(game::TowerID id, std::int32_t x, std::int32_t y, game::TowerType type, game::PlayerID player) : - m_TowerID(id), m_TowerX(x), m_TowerY(y), m_TowerType(type), m_Builder(player) { - } - virtual ~WorldAddTowerPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::TowerID GetTowerID() const { return m_TowerID; } - std::int32_t GetTowerX() const { return m_TowerX; } - std::int32_t GetTowerY() const { return m_TowerY; } - game::TowerType GetTowerType() const { return m_TowerType; } - game::PlayerID GetBuilder() const { return m_Builder; } - - virtual PacketType GetType() const { return PacketType::WorldAddTower; } -}; - -class RemoveTowerPacket : public Packet { -private: - game::TowerID m_TowerID; -public: - RemoveTowerPacket() {} - RemoveTowerPacket(game::TowerID id) : m_TowerID(id) {} - virtual ~RemoveTowerPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::TowerID GetTowerID() const { return m_TowerID; } - - virtual PacketType GetType() const { return PacketType::RemoveTower; } -}; - -class UpgradeTowerPacket : public Packet { -private: - game::TowerID m_TowerID; - game::TowerLevel m_TowerLevel; -public: - UpgradeTowerPacket() {} - UpgradeTowerPacket(game::TowerID tower, game::TowerLevel level) : m_TowerID(tower), m_TowerLevel(level) {} - virtual ~UpgradeTowerPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::TowerID GetTowerID() const { return m_TowerID; } - game::TowerLevel GetTowerLevel() const { return m_TowerLevel; } - - virtual PacketType GetType() const { return PacketType::UpgradeTower; } -}; - -class MobState { - using Point = utils::shape::Point; -private: - game::MobID m_MobID; - Point m_MobPosition; - float m_MobLife; - game::Direction m_MobDirection; -public: - MobState() {} - MobState(game::MobID id, const Point& position, float life, game::Direction direction) : - m_MobID(id), m_MobPosition(position), m_MobLife(life), m_MobDirection(direction) { - } - - game::MobID GetMobId() const { return m_MobID; } - Point GetMobPosition() const { return m_MobPosition; } - float GetMobLife() const { return m_MobLife; } - game::Direction GetMobDirection() const { return m_MobDirection; } -}; - -class UpdateMobStatesPacket : public Packet { -private: - std::vector m_MobStates; -public: - UpdateMobStatesPacket() {} - virtual ~UpdateMobStatesPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - void addMobState(MobState mobState) { m_MobStates.push_back(mobState); } - - const std::vector& GetMobStates() const { return m_MobStates; } - - virtual PacketType GetType() const { return PacketType::UpdateMobStates; } -}; - -class UpdateCastleLifePacket : public Packet { -private: - std::uint16_t m_CastleLife; - game::TeamColor m_Team; -public: - UpdateCastleLifePacket() {} - UpdateCastleLifePacket(std::uint16_t life, game::TeamColor team) : m_CastleLife(life), m_Team(team) {} - virtual ~UpdateCastleLifePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - std::uint16_t GetCastleLife() const { return m_CastleLife; } - game::TeamColor GetTeamColor() const { return m_Team; } - - virtual PacketType GetType() const { return PacketType::UpdateCastleLife; } -}; - -enum class ItemType : std::uint8_t { - // Upgrades - ClickerUpgrade, - GoldPerSecUpgrade, - - // Items -}; - -/** Packet used by the client to buy items or upgrades - Packet used by the server to confirm transaction */ -class PlayerBuyItemPacket : public Packet { -private: - ItemType m_ItemType; - std::uint8_t m_Count; -public: - PlayerBuyItemPacket() {} - PlayerBuyItemPacket(ItemType itemType, std::uint8_t count) : m_ItemType(itemType), m_Count(count) {} - virtual ~PlayerBuyItemPacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - ItemType GetItemType() const { return m_ItemType; } - std::uint8_t GetCount() const { return m_Count; } - - virtual PacketType GetType() const { return PacketType::PlayerBuyItem; } -}; - -/** Packet used by the client to buy mob upgrades - Packet used by the server to confirm transaction */ -class PlayerBuyMobUpgradePacket : public Packet { -private: - game::MobType m_MobType; - std::uint8_t m_MobLevel; -public: - PlayerBuyMobUpgradePacket() {} - PlayerBuyMobUpgradePacket(game::MobType mobType, std::uint8_t level) : m_MobType(mobType), m_MobLevel(level) {} - virtual ~PlayerBuyMobUpgradePacket() {} - - virtual DataBuffer Serialize(bool packetID = true) const; - virtual void Deserialize(DataBuffer& data); - virtual void Dispatch(PacketHandler* handler) const; - - game::MobType GetMobType() const { return m_MobType; } - std::uint8_t GetLevel() const { return m_MobLevel; } - - virtual PacketType GetType() const { return PacketType::PlayerBuyMobUpgrade; } -}; - } // namespace protocol } // namespace td \ No newline at end of file diff --git a/include/protocol/packets/ConnectionInfoPacket.h b/include/protocol/packets/ConnectionInfoPacket.h new file mode 100644 index 0000000..8f8fd44 --- /dev/null +++ b/include/protocol/packets/ConnectionInfoPacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class ConnexionInfoPacket : public Packet { +private: + std::uint8_t m_ConnectionID; +public: + ConnexionInfoPacket() {} + ConnexionInfoPacket(std::uint8_t connectionID) : m_ConnectionID(connectionID) {} + virtual ~ConnexionInfoPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint8_t GetConnectionID() const { return m_ConnectionID; } + + virtual PacketType GetType() const { return PacketType::ConnectionInfo; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/DisconnectPacket.h b/include/protocol/packets/DisconnectPacket.h new file mode 100644 index 0000000..3365bd7 --- /dev/null +++ b/include/protocol/packets/DisconnectPacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class DisconnectPacket : public Packet { +private: + std::string m_Reason; // only when sent from server +public: + DisconnectPacket() {} + DisconnectPacket(std::string reason) : m_Reason(reason) {} + virtual ~DisconnectPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + const std::string& GetReason() const { return m_Reason; } + + virtual PacketType GetType() const { return PacketType::Disconnect; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/KeepAlivePacket.h b/include/protocol/packets/KeepAlivePacket.h new file mode 100644 index 0000000..9a611a6 --- /dev/null +++ b/include/protocol/packets/KeepAlivePacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class KeepAlivePacket : public Packet { +private: + std::uint64_t m_AliveID; +public: + KeepAlivePacket() {} + KeepAlivePacket(std::uint64_t aliveID) : m_AliveID(aliveID) {} + virtual ~KeepAlivePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint64_t GetAliveID() const { return m_AliveID; } + + virtual PacketType GetType() const { return PacketType::KeepAlive; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlaceTowerPacket.h b/include/protocol/packets/PlaceTowerPacket.h new file mode 100644 index 0000000..a3e44fa --- /dev/null +++ b/include/protocol/packets/PlaceTowerPacket.h @@ -0,0 +1,32 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class PlaceTowerPacket : public Packet { +private: + std::int32_t m_TowerX, m_TowerY; + game::TowerType m_TowerType; +public: + PlaceTowerPacket() {} + PlaceTowerPacket(std::int32_t x, std::int32_t y, game::TowerType type) : + m_TowerX(x), m_TowerY(y), m_TowerType(type) { + } + virtual ~PlaceTowerPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::int32_t GetTowerX() const { return m_TowerX; } + std::int32_t GetTowerY() const { return m_TowerY; } + game::TowerType GetTowerType() const { return m_TowerType; } + + virtual PacketType GetType() const { return PacketType::PlaceTower; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerBuyItemPacket.h b/include/protocol/packets/PlayerBuyItemPacket.h new file mode 100644 index 0000000..8ee2285 --- /dev/null +++ b/include/protocol/packets/PlayerBuyItemPacket.h @@ -0,0 +1,38 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +enum class ItemType : std::uint8_t { + // Upgrades + ClickerUpgrade, + GoldPerSecUpgrade, + + // Items +}; + +/** Packet used by the client to buy items or upgrades + Packet used by the server to confirm transaction */ +class PlayerBuyItemPacket : public Packet { +private: + ItemType m_ItemType; + std::uint8_t m_Count; +public: + PlayerBuyItemPacket() {} + PlayerBuyItemPacket(ItemType itemType, std::uint8_t count) : m_ItemType(itemType), m_Count(count) {} + virtual ~PlayerBuyItemPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + ItemType GetItemType() const { return m_ItemType; } + std::uint8_t GetCount() const { return m_Count; } + + virtual PacketType GetType() const { return PacketType::PlayerBuyItem; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerBuyMobUpgradePacket.h b/include/protocol/packets/PlayerBuyMobUpgradePacket.h new file mode 100644 index 0000000..2ba3983 --- /dev/null +++ b/include/protocol/packets/PlayerBuyMobUpgradePacket.h @@ -0,0 +1,31 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +/** Packet used by the client to buy mob upgrades + Packet used by the server to confirm transaction */ +class PlayerBuyMobUpgradePacket : public Packet { +private: + game::MobType m_MobType; + std::uint8_t m_MobLevel; +public: + PlayerBuyMobUpgradePacket() {} + PlayerBuyMobUpgradePacket(game::MobType mobType, std::uint8_t level) : m_MobType(mobType), m_MobLevel(level) {} + virtual ~PlayerBuyMobUpgradePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::MobType GetMobType() const { return m_MobType; } + std::uint8_t GetLevel() const { return m_MobLevel; } + + virtual PacketType GetType() const { return PacketType::PlayerBuyMobUpgrade; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerJoinPacket.h b/include/protocol/packets/PlayerJoinPacket.h new file mode 100644 index 0000000..aab8bcc --- /dev/null +++ b/include/protocol/packets/PlayerJoinPacket.h @@ -0,0 +1,28 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class PlayerJoinPacket : public Packet { +private: + std::uint8_t m_PlayerID; + std::string m_PlayerName; +public: + PlayerJoinPacket() {} + PlayerJoinPacket(std::uint8_t playerID, const std::string& playerName) : m_PlayerID(playerID), m_PlayerName(playerName) {} + virtual ~PlayerJoinPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint8_t GetPlayerID() const { return m_PlayerID; } + const std::string& GetPlayerName() const { return m_PlayerName; } + + virtual PacketType GetType() const { return PacketType::PlayerJoin; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerLeavePacket.h b/include/protocol/packets/PlayerLeavePacket.h new file mode 100644 index 0000000..e8fbbd0 --- /dev/null +++ b/include/protocol/packets/PlayerLeavePacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class PlayerLeavePacket : public Packet { +private: + std::uint8_t m_PlayerID; +public: + PlayerLeavePacket() {} + PlayerLeavePacket(std::uint8_t playerID) : m_PlayerID(playerID) {} + virtual ~PlayerLeavePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint8_t GetPlayerID() const { return m_PlayerID; } + + virtual PacketType GetType() const { return PacketType::PlayerLeave; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerListPacket.h b/include/protocol/packets/PlayerListPacket.h new file mode 100644 index 0000000..5cf7f07 --- /dev/null +++ b/include/protocol/packets/PlayerListPacket.h @@ -0,0 +1,32 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +struct PlayerInfo { + std::string name; + game::TeamColor team; +}; + +class PlayerListPacket : public Packet { +private: + std::map m_Players; +public: + PlayerListPacket() {} + PlayerListPacket(std::map players) : m_Players(players) {} + virtual ~PlayerListPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + const std::map& GetPlayers() const { return m_Players; } + + virtual PacketType GetType() const { return PacketType::PlayerList; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/PlayerLoginPacket.h b/include/protocol/packets/PlayerLoginPacket.h new file mode 100644 index 0000000..fceeb81 --- /dev/null +++ b/include/protocol/packets/PlayerLoginPacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class PlayerLoginPacket : public Packet { +private: + std::string m_PlayerName; +public: + PlayerLoginPacket() {} + PlayerLoginPacket(std::string playerName) : m_PlayerName(playerName) {} + virtual ~PlayerLoginPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + virtual PacketType GetType() const { return PacketType::PlayerLogin; } + + const std::string& GetPlayerName() const { return m_PlayerName; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/RemoveTowerPacket.h b/include/protocol/packets/RemoveTowerPacket.h new file mode 100644 index 0000000..2727813 --- /dev/null +++ b/include/protocol/packets/RemoveTowerPacket.h @@ -0,0 +1,27 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class RemoveTowerPacket : public Packet { +private: + game::TowerID m_TowerID; +public: + RemoveTowerPacket() {} + RemoveTowerPacket(game::TowerID id) : m_TowerID(id) {} + virtual ~RemoveTowerPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::TowerID GetTowerID() const { return m_TowerID; } + + virtual PacketType GetType() const { return PacketType::RemoveTower; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/SelectTeamPacket.h b/include/protocol/packets/SelectTeamPacket.h new file mode 100644 index 0000000..0375140 --- /dev/null +++ b/include/protocol/packets/SelectTeamPacket.h @@ -0,0 +1,27 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class SelectTeamPacket : public Packet { +private: + game::TeamColor m_SelectedTeam; +public: + SelectTeamPacket() {} + SelectTeamPacket(game::TeamColor selectedTeam) : m_SelectedTeam(selectedTeam) {} + virtual ~SelectTeamPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::TeamColor GetSelectedTeam() const { return m_SelectedTeam; } + + virtual PacketType GetType() const { return PacketType::SelectTeam; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/SendMobsPacket.h b/include/protocol/packets/SendMobsPacket.h new file mode 100644 index 0000000..92c3300 --- /dev/null +++ b/include/protocol/packets/SendMobsPacket.h @@ -0,0 +1,33 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +struct MobSend { // represents a mob send + game::MobType mobType : 4; + game::MobLevel mobLevel : 4; + std::uint8_t mobCount; // the max is 12 +}; + +class SendMobsPacket : public Packet { +private: + std::vector m_MobSends; +public: + SendMobsPacket() {} + SendMobsPacket(const std::vector& mobSends) : m_MobSends(mobSends) {} + virtual ~SendMobsPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + const std::vector& GetMobSends() const { return m_MobSends; } + + virtual PacketType GetType() const { return PacketType::SendMobs; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/ServerTpsPacket.h b/include/protocol/packets/ServerTpsPacket.h new file mode 100644 index 0000000..4326b91 --- /dev/null +++ b/include/protocol/packets/ServerTpsPacket.h @@ -0,0 +1,30 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class ServerTpsPacket : public Packet { +private: + float m_TPS; + float m_MSPT; + std::uint64_t m_PacketSendTime; // used to calculate ping +public: + ServerTpsPacket() {} + ServerTpsPacket(float tps, float mspt, std::uint64_t sendTime) : m_TPS(tps), m_MSPT(mspt), m_PacketSendTime(sendTime) {} + virtual ~ServerTpsPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + float GetTPS() const { return m_TPS; } + float GetMSPT() const { return m_MSPT; } + std::uint64_t GetPacketSendTime() const { return m_PacketSendTime; } + + virtual PacketType GetType() const { return PacketType::ServerTps; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/SpawnMobPacket.h b/include/protocol/packets/SpawnMobPacket.h new file mode 100644 index 0000000..78abec3 --- /dev/null +++ b/include/protocol/packets/SpawnMobPacket.h @@ -0,0 +1,41 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class SpawnMobPacket : public Packet { +private: + game::MobID m_MobID; + game::MobType m_MobType; + game::MobLevel m_MobLevel; + game::Direction m_MobDirection; + game::PlayerID m_Sender; + float m_MobX, m_MobY; +public: + SpawnMobPacket() {} + SpawnMobPacket(game::MobID id, game::MobType type, std::uint8_t level, game::PlayerID sender, + float x, float y, game::Direction dir) : m_MobID(id), m_MobType(type), m_MobLevel(level), + m_MobDirection(dir), m_Sender(sender), m_MobX(x), m_MobY(y) { + } + virtual ~SpawnMobPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::MobID GetMobID() const { return m_MobID; } + game::MobType GetMobType() const { return m_MobType; } + game::MobLevel GetMobLevel() const { return m_MobLevel; } + game::Direction GetMobDirection() const { return m_MobDirection; } + game::PlayerID GetSender() const { return m_Sender; } + float GetMobX() const { return m_MobX; } + float GetMobY() const { return m_MobY; } + + virtual PacketType GetType() const { return PacketType::SpawnMob; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateCastleLifePacket.h b/include/protocol/packets/UpdateCastleLifePacket.h new file mode 100644 index 0000000..63d5163 --- /dev/null +++ b/include/protocol/packets/UpdateCastleLifePacket.h @@ -0,0 +1,29 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class UpdateCastleLifePacket : public Packet { +private: + std::uint16_t m_CastleLife; + game::TeamColor m_Team; +public: + UpdateCastleLifePacket() {} + UpdateCastleLifePacket(std::uint16_t life, game::TeamColor team) : m_CastleLife(life), m_Team(team) {} + virtual ~UpdateCastleLifePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint16_t GetCastleLife() const { return m_CastleLife; } + game::TeamColor GetTeamColor() const { return m_Team; } + + virtual PacketType GetType() const { return PacketType::UpdateCastleLife; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateExpPacket.h b/include/protocol/packets/UpdateExpPacket.h new file mode 100644 index 0000000..bf1313b --- /dev/null +++ b/include/protocol/packets/UpdateExpPacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class UpdateExpPacket : public Packet { +private: + std::uint32_t m_NewAmount; +public: + UpdateExpPacket() {} + UpdateExpPacket(std::uint32_t newAmount) : m_NewAmount(newAmount) {} + virtual ~UpdateExpPacket() {} + + std::uint32_t GetExp() const { return m_NewAmount; } + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + virtual PacketType GetType() const { return PacketType::UpdateEXP; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateGameStatePacket.h b/include/protocol/packets/UpdateGameStatePacket.h new file mode 100644 index 0000000..b25fc85 --- /dev/null +++ b/include/protocol/packets/UpdateGameStatePacket.h @@ -0,0 +1,27 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class UpdateGameStatePacket : public Packet { +private: + game::GameState m_GameState; +public: + UpdateGameStatePacket() {} + UpdateGameStatePacket(game::GameState gameState) : m_GameState(gameState) {} + virtual ~UpdateGameStatePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::GameState GetGameState() const { return m_GameState; } + + virtual PacketType GetType() const { return PacketType::UpdateGameState; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateLobbyTimePacket.h b/include/protocol/packets/UpdateLobbyTimePacket.h new file mode 100644 index 0000000..e910c59 --- /dev/null +++ b/include/protocol/packets/UpdateLobbyTimePacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class UpdateLobbyTimePacket : public Packet { +private: + std::uint32_t m_RemainingTime; +public: + UpdateLobbyTimePacket() {} + UpdateLobbyTimePacket(std::uint32_t remainingTime) : m_RemainingTime(remainingTime) {} + virtual ~UpdateLobbyTimePacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + std::uint32_t GetRemainingTime() const { return m_RemainingTime; } + + virtual PacketType GetType() const { return PacketType::UpdateLobbyTime; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateMobStatesPacket.h b/include/protocol/packets/UpdateMobStatesPacket.h new file mode 100644 index 0000000..ba84e80 --- /dev/null +++ b/include/protocol/packets/UpdateMobStatesPacket.h @@ -0,0 +1,47 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class MobState { + using Point = utils::shape::Point; +private: + game::MobID m_MobID; + Point m_MobPosition; + float m_MobLife; + game::Direction m_MobDirection; +public: + MobState() {} + MobState(game::MobID id, const Point& position, float life, game::Direction direction) : + m_MobID(id), m_MobPosition(position), m_MobLife(life), m_MobDirection(direction) { + } + + game::MobID GetMobId() const { return m_MobID; } + Point GetMobPosition() const { return m_MobPosition; } + float GetMobLife() const { return m_MobLife; } + game::Direction GetMobDirection() const { return m_MobDirection; } +}; + +class UpdateMobStatesPacket : public Packet { +private: + std::vector m_MobStates; +public: + UpdateMobStatesPacket() {} + virtual ~UpdateMobStatesPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + void addMobState(MobState mobState) { m_MobStates.push_back(mobState); } + + const std::vector& GetMobStates() const { return m_MobStates; } + + virtual PacketType GetType() const { return PacketType::UpdateMobStates; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdateMoneyPacket.h b/include/protocol/packets/UpdateMoneyPacket.h new file mode 100644 index 0000000..1fce4d1 --- /dev/null +++ b/include/protocol/packets/UpdateMoneyPacket.h @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol/Protocol.h" + +namespace td { +namespace protocol { + +class UpdateMoneyPacket : public Packet { +private: + std::uint32_t m_NewAmount; +public: + UpdateMoneyPacket() {} + UpdateMoneyPacket(std::uint32_t newAmount) : m_NewAmount(newAmount) {} + virtual ~UpdateMoneyPacket() {} + + std::uint32_t GetGold() const { return m_NewAmount; } + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + virtual PacketType GetType() const { return PacketType::UpdateMoney; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpdatePlayerTeamPacket.h b/include/protocol/packets/UpdatePlayerTeamPacket.h new file mode 100644 index 0000000..46731d7 --- /dev/null +++ b/include/protocol/packets/UpdatePlayerTeamPacket.h @@ -0,0 +1,29 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class UpdatePlayerTeamPacket : public Packet { +private: + std::uint8_t m_PlayerID; + game::TeamColor m_SelectedTeam; +public: + UpdatePlayerTeamPacket() {} + UpdatePlayerTeamPacket(std::uint8_t playerID, game::TeamColor selectedTeam) : m_PlayerID(playerID), m_SelectedTeam(selectedTeam) {} + virtual ~UpdatePlayerTeamPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::TeamColor GetSelectedTeam() const { return m_SelectedTeam; } + std::uint8_t GetPlayerID() const { return m_PlayerID; } + + virtual PacketType GetType() const { return PacketType::UpdatePlayerTeam; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/UpgradeTowerPacket.h b/include/protocol/packets/UpgradeTowerPacket.h new file mode 100644 index 0000000..124c8c8 --- /dev/null +++ b/include/protocol/packets/UpgradeTowerPacket.h @@ -0,0 +1,29 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class UpgradeTowerPacket : public Packet { +private: + game::TowerID m_TowerID; + game::TowerLevel m_TowerLevel; +public: + UpgradeTowerPacket() {} + UpgradeTowerPacket(game::TowerID tower, game::TowerLevel level) : m_TowerID(tower), m_TowerLevel(level) {} + virtual ~UpgradeTowerPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::TowerID GetTowerID() const { return m_TowerID; } + game::TowerLevel GetTowerLevel() const { return m_TowerLevel; } + + virtual PacketType GetType() const { return PacketType::UpgradeTower; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/WorldAddTowerPacket.h b/include/protocol/packets/WorldAddTowerPacket.h new file mode 100644 index 0000000..0851451 --- /dev/null +++ b/include/protocol/packets/WorldAddTowerPacket.h @@ -0,0 +1,36 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +class WorldAddTowerPacket : public Packet { +private: + game::TowerID m_TowerID; + std::int32_t m_TowerX, m_TowerY; + game::TowerType m_TowerType; + game::PlayerID m_Builder; +public: + WorldAddTowerPacket() {} + WorldAddTowerPacket(game::TowerID id, std::int32_t x, std::int32_t y, game::TowerType type, game::PlayerID player) : + m_TowerID(id), m_TowerX(x), m_TowerY(y), m_TowerType(type), m_Builder(player) { + } + virtual ~WorldAddTowerPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + game::TowerID GetTowerID() const { return m_TowerID; } + std::int32_t GetTowerX() const { return m_TowerX; } + std::int32_t GetTowerY() const { return m_TowerY; } + game::TowerType GetTowerType() const { return m_TowerType; } + game::PlayerID GetBuilder() const { return m_Builder; } + + virtual PacketType GetType() const { return PacketType::WorldAddTower; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/WorldBeginDataPacket.h b/include/protocol/packets/WorldBeginDataPacket.h new file mode 100644 index 0000000..59fbdaa --- /dev/null +++ b/include/protocol/packets/WorldBeginDataPacket.h @@ -0,0 +1,60 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +struct WorldHeader { + game::TowerTileColorPalette m_TowerPlacePalette; + Color m_WalkablePalette; + std::vector m_DecorationPalette; + Color m_Background; + + game::SpawnColorPalette m_SpawnColorPalette; + + game::TilePalette m_TilePalette; + + game::Spawn m_RedSpawn, m_BlueSpawn; + game::TeamCastle m_RedCastle, m_BlueCastle; + + const game::World* m_World; +}; + +class WorldBeginDataPacket : public Packet { +private: + WorldHeader m_Header; +public: + WorldBeginDataPacket() {} + WorldBeginDataPacket(const game::World* world) { + m_Header.m_World = world; + } + virtual ~WorldBeginDataPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + virtual PacketType GetType() const { return PacketType::WorldBeginData; } + + const game::TowerTileColorPalette& GetTowerTilePalette() const { return m_Header.m_TowerPlacePalette; } + const Color& GetWalkableTileColor() const { return m_Header.m_WalkablePalette; } + const std::vector& GetDecorationPalette() const { return m_Header.m_DecorationPalette; } + const Color& GetBackgroundColor() const { return m_Header.m_Background; } + + const game::Spawn& GetRedSpawn() const { return m_Header.m_RedSpawn; } + const game::Spawn& GetBlueSpawn() const { return m_Header.m_BlueSpawn; } + + const game::SpawnColorPalette& GetSpawnPalette() const { return m_Header.m_SpawnColorPalette; } + + const game::TeamCastle& GetRedCastle() const { return m_Header.m_RedCastle; } + const game::TeamCastle& GetBlueCastle() const { return m_Header.m_BlueCastle; } + + const game::TilePalette GetTilePalette() const { return m_Header.m_TilePalette; } + + void setWorldHeader(const WorldHeader& header) { m_Header = header; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/protocol/packets/WorldDataPacket.h b/include/protocol/packets/WorldDataPacket.h new file mode 100644 index 0000000..8ade9e2 --- /dev/null +++ b/include/protocol/packets/WorldDataPacket.h @@ -0,0 +1,36 @@ +#pragma once + +#include "protocol/Protocol.h" +#include "game/BaseGame.h" + +namespace td { +namespace protocol { + +struct WorldData { + std::unordered_map m_Chunks; +}; + +class WorldDataPacket : public Packet { +private: + WorldData m_WorldData; + + const game::World* m_World; +public: + WorldDataPacket() {} + WorldDataPacket(const game::World* world) : m_World(world) {} + virtual ~WorldDataPacket() {} + + virtual DataBuffer Serialize(bool packetID = true) const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler) const; + + virtual PacketType GetType() const { return PacketType::WorldData; } + + const std::unordered_map& GetChunks() const { return m_WorldData.m_Chunks; } + + DataBuffer SerializeCustom() const; // allow serialisation with invalid World member + void SetWorldData(const WorldData& worldData) { m_WorldData = worldData; } +}; + +} // namespace protocol +} // namespace td diff --git a/include/render/WorldRenderer.h b/include/render/WorldRenderer.h index aed88cd..bf53a80 100644 --- a/include/render/WorldRenderer.h +++ b/include/render/WorldRenderer.h @@ -10,8 +10,6 @@ #include "render/gui/MobTooltip.h" #include "render/gui/CastleTooltip.h" -#include "render/gui/imgui/imgui.h" - namespace td { namespace client { @@ -20,7 +18,6 @@ class ClientGame; } // namespace client - namespace render { class WorldRenderer : public game::WorldListener { @@ -48,8 +45,6 @@ public: void LoadModels(); - static ImVec4 GetImGuiTeamColor(game::TeamColor color); - void Update(); void Render(); diff --git a/include/render/gui/ImGuiTeamColor.h b/include/render/gui/ImGuiTeamColor.h new file mode 100644 index 0000000..661f14e --- /dev/null +++ b/include/render/gui/ImGuiTeamColor.h @@ -0,0 +1,13 @@ +#pragma once + +#include "render/gui/imgui/imgui.h" +#include "game/Team.h" + +namespace td { +namespace render { + +ImVec4 GetImGuiTeamColor(game::TeamColor color); + +} // namespace render +} // namespace td + diff --git a/src/game/World.cpp b/src/game/World.cpp index aecd088..fd131b0 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -1,6 +1,8 @@ #include "game/World.h" #include "protocol/PacketDispatcher.h" #include "protocol/Protocol.h" +#include "protocol/packets/WorldBeginDataPacket.h" +#include "protocol/packets/WorldDataPacket.h" #include "game/BaseGame.h" #include "misc/Random.h" #include "misc/Compression.h" diff --git a/src/game/client/Client.cpp b/src/game/client/Client.cpp index cdd5767..9f77e7f 100644 --- a/src/game/client/Client.cpp +++ b/src/game/client/Client.cpp @@ -1,6 +1,12 @@ #include "game/client/Client.h" #include "misc/Format.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/PlaceTowerPacket.h" +#include "protocol/packets/RemoveTowerPacket.h" +#include "protocol/packets/SelectTeamPacket.h" +#include "protocol/packets/UpgradeTowerPacket.h" + namespace td { namespace client { diff --git a/src/game/client/ClientConnexion.cpp b/src/game/client/ClientConnexion.cpp index 7528f7a..f538699 100644 --- a/src/game/client/ClientConnexion.cpp +++ b/src/game/client/ClientConnexion.cpp @@ -1,6 +1,12 @@ #include "game/client/ClientConnexion.h" #include "render/WorldRenderer.h" +#include "protocol/packets/ConnectionInfoPacket.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/KeepAlivePacket.h" +#include "protocol/packets/PlayerLoginPacket.h" +#include "protocol/packets/ServerTpsPacket.h" + namespace td { namespace client { @@ -32,7 +38,7 @@ void ClientConnexion::HandlePacket(const protocol::ServerTpsPacket* packet) { } void ClientConnexion::Login() { - td::protocol::PlayerLoginPacket loginPacket("Persson" + std::to_string(m_ConnectionID)); + td::protocol::PlayerLoginPacket loginPacket("Persson" + std::to_string(static_cast(m_ConnectionID))); SendPacket(&loginPacket); } diff --git a/src/game/client/ClientGame.cpp b/src/game/client/ClientGame.cpp index cd72abb..572d52c 100644 --- a/src/game/client/ClientGame.cpp +++ b/src/game/client/ClientGame.cpp @@ -2,6 +2,18 @@ #include "protocol/PacketDispatcher.h" #include "game/client/Client.h" +#include "protocol/packets/ConnectionInfoPacket.h" +#include "protocol/packets/PlayerJoinPacket.h" +#include "protocol/packets/PlayerListPacket.h" +#include "protocol/packets/PlayerLeavePacket.h" +#include "protocol/packets/UpdatePlayerTeamPacket.h" +#include "protocol/packets/UpdateLobbyTimePacket.h" +#include "protocol/packets/UpdateGameStatePacket.h" +#include "protocol/packets/UpdateMoneyPacket.h" +#include "protocol/packets/UpdateExpPacket.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/WorldDataPacket.h" + namespace td { namespace client { @@ -43,7 +55,7 @@ void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) { void ClientGame::HandlePacket(const protocol::PlayerLeavePacket* packet) { game::Player* player = &m_Players[packet->GetPlayerID()]; if (player->GetTeamColor() != game::TeamColor::None) { - m_Teams[(std::size_t)player->GetTeamColor()].RemovePlayer(player); + m_Teams[static_cast(player->GetTeamColor())].RemovePlayer(player); } m_Players.erase(player->GetID()); } @@ -57,7 +69,7 @@ void ClientGame::HandlePacket(const protocol::PlayerListPacket* packet) { player.SetTeamColor(playerInfo.team); m_Players.insert({ playerID, player }); if (player.GetTeamColor() != game::TeamColor::None) { - m_Teams[(std::size_t)player.GetTeamColor()].AddPlayer(&m_Players[playerID]); + m_Teams[static_cast(player.GetTeamColor())].AddPlayer(&m_Players[playerID]); } } m_Player = &m_Players[m_ConnexionID]; diff --git a/src/game/client/WorldClient.cpp b/src/game/client/WorldClient.cpp index 5a93812..a0dbd33 100644 --- a/src/game/client/WorldClient.cpp +++ b/src/game/client/WorldClient.cpp @@ -3,6 +3,15 @@ #include "game/client/ClientGame.h" #include "render/WorldRenderer.h" +#include "protocol/packets/WorldAddTowerPacket.h" +#include "protocol/packets/WorldBeginDataPacket.h" +#include "protocol/packets/WorldDataPacket.h" +#include "protocol/packets/SpawnMobPacket.h" +#include "protocol/packets/UpgradeTowerPacket.h" +#include "protocol/packets/RemoveTowerPacket.h" +#include "protocol/packets/UpdateCastleLifePacket.h" +#include "protocol/packets/UpdateMobStatesPacket.h" + namespace td { namespace client { diff --git a/src/game/server/Lobby.cpp b/src/game/server/Lobby.cpp index fd0856e..3c6ced0 100644 --- a/src/game/server/Lobby.cpp +++ b/src/game/server/Lobby.cpp @@ -1,6 +1,8 @@ #include "game/server/Lobby.h" #include "game/server/Server.h" +#include "protocol/packets/UpdateLobbyTimePacket.h" + #include "misc/Time.h" #ifdef NDEBUG diff --git a/src/game/server/Server.cpp b/src/game/server/Server.cpp index fecadfd..a32d83a 100644 --- a/src/game/server/Server.cpp +++ b/src/game/server/Server.cpp @@ -1,6 +1,10 @@ #include "game/server/Server.h" #include "protocol/PacketFactory.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/ServerTpsPacket.h" +#include "protocol/packets/PlayerLeavePacket.h" + #include "misc/Format.h" namespace td { diff --git a/src/game/server/ServerConnexion.cpp b/src/game/server/ServerConnexion.cpp index 0d43452..e6e0f2f 100644 --- a/src/game/server/ServerConnexion.cpp +++ b/src/game/server/ServerConnexion.cpp @@ -3,6 +3,23 @@ #include "protocol/PacketFactory.h" #include "game/server/Server.h" +#include "protocol/packets/ConnectionInfoPacket.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/KeepAlivePacket.h" +#include "protocol/packets/PlaceTowerPacket.h" +#include "protocol/packets/PlayerLoginPacket.h" +#include "protocol/packets/PlayerJoinPacket.h" +#include "protocol/packets/PlayerListPacket.h" +#include "protocol/packets/RemoveTowerPacket.h" +#include "protocol/packets/SelectTeamPacket.h" +#include "protocol/packets/SendMobsPacket.h" +#include "protocol/packets/UpdatePlayerTeamPacket.h" +#include "protocol/packets/UpdateGameStatePacket.h" +#include "protocol/packets/UpgradeTowerPacket.h" +#include "protocol/packets/WorldBeginDataPacket.h" +#include "protocol/packets/WorldDataPacket.h" +#include "protocol/packets/WorldAddTowerPacket.h" + #include "misc/Time.h" #include "misc/Random.h" diff --git a/src/game/server/ServerGame.cpp b/src/game/server/ServerGame.cpp index ee1e33d..99b8a22 100644 --- a/src/game/server/ServerGame.cpp +++ b/src/game/server/ServerGame.cpp @@ -1,6 +1,15 @@ #include "game/server/ServerGame.h" #include "game/server/Server.h" +#include "protocol/packets/DisconnectPacket.h" +#include "protocol/packets/UpdatePlayerTeamPacket.h" +#include "protocol/packets/UpdateGameStatePacket.h" +#include "protocol/packets/UpdateMoneyPacket.h" +#include "protocol/packets/UpdateExpPacket.h" +#include "protocol/packets/UpdateMobStatesPacket.h" +#include "protocol/packets/WorldDataPacket.h" +#include "protocol/packets/WorldBeginDataPacket.h" + namespace td { namespace server { diff --git a/src/game/server/ServerWorld.cpp b/src/game/server/ServerWorld.cpp index 308f48e..4e052f1 100644 --- a/src/game/server/ServerWorld.cpp +++ b/src/game/server/ServerWorld.cpp @@ -2,6 +2,9 @@ #include "game/server/Server.h" #include "misc/Random.h" +#include "protocol/packets/SpawnMobPacket.h" +#include "protocol/packets/UpdateCastleLifePacket.h" + #define MOB_SPAWN_PRECISION 100.0f namespace td { diff --git a/src/protocol/PacketFactory.cpp b/src/protocol/PacketFactory.cpp index b72b072..7eab92a 100644 --- a/src/protocol/PacketFactory.cpp +++ b/src/protocol/PacketFactory.cpp @@ -1,4 +1,5 @@ #include "protocol/PacketFactory.h" +#include "protocol/Packets.h" #include #include diff --git a/src/protocol/Protocol.cpp b/src/protocol/Protocol.cpp index 25f2b01..0d208ab 100644 --- a/src/protocol/Protocol.cpp +++ b/src/protocol/Protocol.cpp @@ -1,6 +1,5 @@ #include "protocol/PacketHandler.h" - -#include +#include "protocol/Packets.h" #define REGISTER_DISPATCH_CLASS(className) void className::Dispatch(PacketHandler* handler) const { \ handler->HandlePacket(this);\ @@ -9,565 +8,11 @@ namespace td { namespace protocol { -const int BITS_IN_BYTE = 8; -const int BITS_IN_LONG = BITS_IN_BYTE * sizeof(std::uint64_t); - -static unsigned int countBits(unsigned int number) { - // log function in base 2 - // take only integer part - return static_cast(std::log2(number) + 1); -} - void Packet::WritePacketID(DataBuffer& data, bool packetID) const { if (packetID) data << GetID(); } -static DataBuffer& operator<<(DataBuffer& buffer, game::TilePtr tile) { - buffer << tile->GetType(); - - switch (tile->GetType()) { - - case game::TileType::Tower: { - const game::TowerTile* towerTile = dynamic_cast(tile.get()); - buffer << towerTile->color_palette_ref << towerTile->team_owner; - break; - } - - case game::TileType::Walk: { - const game::WalkableTile* walkTile = dynamic_cast(tile.get()); - buffer << walkTile->direction; - break; - } - - case game::TileType::Decoration: { - const game::DecorationTile* decoTile = dynamic_cast(tile.get()); - buffer << decoTile->color_palette_ref; - break; - } - - default: - break; - } - - return buffer; -} - -static DataBuffer& operator>>(DataBuffer& buffer, game::TilePtr& tile) { - game::TileType tileType; - buffer >> tileType; - switch (tileType) { - case game::TileType::Tower: { - auto tilePtr = std::make_shared(); - buffer >> tilePtr->color_palette_ref >> tilePtr->team_owner; - tile = tilePtr; - break; - } - case game::TileType::Walk: { - auto tilePtr = std::make_shared(); - buffer >> tilePtr->direction; - tile = tilePtr; - break; - } - case game::TileType::Decoration: { - auto tilePtr = std::make_shared(); - buffer >> tilePtr->color_palette_ref; - tile = tilePtr; - break; - } - default: - break; - } - return buffer; -} - -DataBuffer PlayerLoginPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_PlayerName; - return data; -} - -void PlayerLoginPacket::Deserialize(DataBuffer& data) { - data >> m_PlayerName; -} - -DataBuffer WorldBeginDataPacket::Serialize(bool packetID) const { - DataBuffer data; - const game::TowerTileColorPalette& towerTilePalette = m_Header.m_World->GetTowerTileColorPalette(); - const std::vector& decoTilePalette = m_Header.m_World->GetDecorationPalette(); - - WritePacketID(data, packetID); - - data << towerTilePalette << m_Header.m_World->GetWalkableTileColor() - << static_cast(decoTilePalette.size()); - - // deco color palette - std::size_t bufferSize = data.GetSize(); - data.Resize(bufferSize + decoTilePalette.size() * sizeof(Color)); - - memcpy(reinterpret_cast(data.data()) + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(Color)); - - data << m_Header.m_World->GetBackgroundColor(); - - const game::Spawn& redSpawn = m_Header.m_World->GetRedTeam().GetSpawn(), blueSpawn = m_Header.m_World->GetBlueTeam().GetSpawn(); - const game::TeamCastle& redCastle = m_Header.m_World->GetRedTeam().GetCastle(), blueCastle = m_Header.m_World->GetBlueTeam().GetCastle(); - - data << redSpawn << static_cast(redCastle); - data << blueSpawn << static_cast(blueCastle); - - // tile palette - data << static_cast(m_Header.m_World->GetTilePalette().size()); - - for (game::TilePtr tile : m_Header.m_World->GetTilePalette()) { - data << tile; - } - - data << m_Header.m_World->GetSpawnColors(); - - return data; -} - -void WorldBeginDataPacket::Deserialize(DataBuffer& data) { - data >> m_Header.m_TowerPlacePalette >> m_Header.m_WalkablePalette; - - std::uint16_t decoPaletteSize; - data >> decoPaletteSize; - - std::size_t decoPalletteSizeByte = decoPaletteSize * sizeof(Color); - - m_Header.m_DecorationPalette.resize(decoPaletteSize); - - memcpy(reinterpret_cast(m_Header.m_DecorationPalette.data()), data.data() + data.GetReadOffset(), decoPalletteSizeByte); - - data.SetReadOffset(data.GetReadOffset() + decoPalletteSizeByte); - - data >> m_Header.m_Background; - - utils::shape::Rectangle redCastle, blueCastle; - - data >> m_Header.m_RedSpawn >> redCastle; - data >> m_Header.m_BlueSpawn >> blueCastle; - - m_Header.m_RedCastle.SetShape(redCastle); - m_Header.m_BlueCastle.SetShape(blueCastle); - - std::uint64_t tilePaletteSize; - data >> tilePaletteSize; - - m_Header.m_TilePalette.reserve(tilePaletteSize); - - for (std::uint64_t tileNumber = 0; tileNumber < tilePaletteSize; tileNumber++) { - game::TilePtr tile; - data >> tile; - m_Header.m_TilePalette.push_back(tile); - } - - data >> m_Header.m_SpawnColorPalette; -} - -typedef std::vector ChunkPackedData; - -DataBuffer WorldDataPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - - data << m_World->GetChunks().size(); - for (const auto& pair : m_World->GetChunks()) { - game::ChunkCoord coords = pair.first; - game::ChunkPtr chunk = pair.second; - - data << coords.x << coords.y << static_cast(chunk->palette.size()); - - std::size_t bufferSize = data.GetSize(); - data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); - memcpy(reinterpret_cast(data.data()) + bufferSize, chunk->palette.data(), chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); - - std::uint8_t bitsPerTile = countBits(chunk->palette.size()); - - game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); - - ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); - - for (unsigned int tileNumber = 0; tileNumber < game::Chunk::ChunkSize; tileNumber++) { - std::size_t startLong = static_cast((tileNumber * bitsPerTile) / BITS_IN_LONG); - std::size_t startOffset = static_cast((tileNumber * bitsPerTile) % BITS_IN_LONG); - std::size_t endLong = static_cast(((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG); - - std::uint64_t value = static_cast(chunk->tiles[tileNumber]); - - value &= individualValueMask; - - chunkData[startLong] |= (value << startOffset); - - if (startLong != endLong) { - chunkData[endLong] = (value >> (BITS_IN_LONG - startOffset)); - } - } - - bufferSize = data.GetSize(); - data.Resize(data.GetSize() + chunkData.size() * sizeof(ChunkPackedData::value_type)); - memcpy(reinterpret_cast(data.data()) + bufferSize, chunkData.data(), chunkData.size() * sizeof(ChunkPackedData::value_type)); - } - return data; -} - -void WorldDataPacket::Deserialize(DataBuffer& data) { - std::uint64_t chunkCount; - data >> chunkCount; - - for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) { - game::ChunkPtr chunk = std::make_shared(); - - decltype(game::ChunkCoord::x) chunkX, chunkY; - data >> chunkX >> chunkY; - - std::uint64_t chunkPaletteSize; - data >> chunkPaletteSize; - - game::ChunkPalette chunkPalette(chunkPaletteSize); - - memcpy(reinterpret_cast(chunkPalette.data()), data.data() + data.GetReadOffset(), chunkPaletteSize * sizeof(game::ChunkPalette::value_type)); - data.SetReadOffset(data.GetReadOffset() + chunkPaletteSize * sizeof(game::ChunkPalette::value_type)); - - chunk->palette = chunkPalette; - - std::uint8_t bitsPerTile = countBits(chunkPaletteSize); - - // A bitmask that contains bitsPerTile set bits - game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); - - ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); - - memcpy(reinterpret_cast(chunkData.data()), data.data() + data.GetReadOffset(), chunkData.size() * sizeof(ChunkPackedData::value_type)); - data.SetReadOffset(data.GetReadOffset() + chunkData.size() * sizeof(ChunkPackedData::value_type)); - - for (unsigned int tileNumber = 0; tileNumber < game::Chunk::ChunkSize; tileNumber++) { - std::size_t startLong = (tileNumber * bitsPerTile) / BITS_IN_LONG; - std::size_t startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG; - std::size_t endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG; - - game::Chunk::ChunkData::value_type value; - if (startLong == endLong) { - value = (chunkData[startLong] >> startOffset); - } else { - int endOffset = BITS_IN_LONG - startOffset; - value = (chunkData[startLong] >> startOffset | chunkData[endLong] << endOffset); - } - value &= individualValueMask; - - chunk->tiles[tileNumber] = value; - } - - - m_WorldData.m_Chunks.insert({ {chunkX, chunkY}, chunk }); - } -} - -DataBuffer KeepAlivePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_AliveID; - return data; -} - -void KeepAlivePacket::Deserialize(DataBuffer& data) { - data >> m_AliveID; -} - -DataBuffer UpdateMoneyPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_NewAmount; - return data; -} - -void UpdateMoneyPacket::Deserialize(DataBuffer& data) { - data >> m_NewAmount; -} - -DataBuffer UpdateExpPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_NewAmount; - return data; -} - -void UpdateExpPacket::Deserialize(DataBuffer& data) { - data >> m_NewAmount; -} - -DataBuffer UpdateLobbyTimePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_RemainingTime; - return data; -} - -void UpdateLobbyTimePacket::Deserialize(DataBuffer& data) { - data >> m_RemainingTime; -} - -DataBuffer UpdateGameStatePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_GameState; - return data; -} - -void UpdateGameStatePacket::Deserialize(DataBuffer& data) { - data >> m_GameState; -} - -DataBuffer PlayerListPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << static_cast(m_Players.size()); - for (auto [playerID, playerInfo] : m_Players) { - data << playerID << playerInfo.name << playerInfo.team; - } - return data; -} - -void PlayerListPacket::Deserialize(DataBuffer& data) { - std::uint8_t playerCount; - data >> playerCount; - - for (int i = 0; i < playerCount; i++) { - std::uint8_t playerID; - PlayerInfo playerInfo; - data >> playerID >> playerInfo.name >> playerInfo.team; - m_Players.insert({ playerID, playerInfo }); - } -} - -DataBuffer PlayerJoinPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_PlayerID << m_PlayerName; - return data; -} - -void PlayerJoinPacket::Deserialize(DataBuffer& data) { - data >> m_PlayerID >> m_PlayerName; -} - -DataBuffer PlayerLeavePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_PlayerID; - return data; -} - -void PlayerLeavePacket::Deserialize(DataBuffer& data) { - data >> m_PlayerID; -} - -DataBuffer ConnexionInfoPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_ConnectionID; - return data; -} - -void ConnexionInfoPacket::Deserialize(DataBuffer& data) { - data >> m_ConnectionID; -} - -DataBuffer SelectTeamPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_SelectedTeam; - return data; -} - -void SelectTeamPacket::Deserialize(DataBuffer& data) { - data >> m_SelectedTeam; -} - -DataBuffer UpdatePlayerTeamPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_PlayerID << m_SelectedTeam; - return data; -} - -void UpdatePlayerTeamPacket::Deserialize(DataBuffer& data) { - data >> m_PlayerID >> m_SelectedTeam; -} - -DataBuffer DisconnectPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_Reason; - return data; -} - -void DisconnectPacket::Deserialize(DataBuffer& data) { - data >> m_Reason; -} - -DataBuffer ServerTpsPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_TPS << m_MSPT << m_PacketSendTime; - return data; -} - -void ServerTpsPacket::Deserialize(DataBuffer& data) { - data >> m_TPS >> m_MSPT >> m_PacketSendTime; -} - -DataBuffer SpawnMobPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_MobID << m_MobType << m_MobLevel << m_MobDirection - << m_Sender << m_MobX << m_MobY; - return data; -} - -void SpawnMobPacket::Deserialize(DataBuffer& data) { - data >> m_MobID >> m_MobType >> m_MobLevel >> m_MobDirection - >> m_Sender >> m_MobX >> m_MobY; -} - -DataBuffer PlaceTowerPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_TowerX << m_TowerY << m_TowerType; - return data; -} - -void PlaceTowerPacket::Deserialize(DataBuffer& data) { - data >> m_TowerX >> m_TowerY >> m_TowerType; -} - -DataBuffer WorldAddTowerPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_TowerID << m_TowerX << m_TowerY << m_TowerType << m_Builder; - return data; -} - -void WorldAddTowerPacket::Deserialize(DataBuffer& data) { - data >> m_TowerID >> m_TowerX >> m_TowerY >> m_TowerType >> m_Builder; -} - -DataBuffer RemoveTowerPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_TowerID; - return data; -} - -void RemoveTowerPacket::Deserialize(DataBuffer& data) { - data >> m_TowerID; -} - -DataBuffer SendMobsPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << static_cast(m_MobSends.size()); - - data.WriteSome(reinterpret_cast(m_MobSends.data()), m_MobSends.size() * sizeof(m_MobSends)); - - return data; -} - -void SendMobsPacket::Deserialize(DataBuffer& data) { - std::uint8_t mobSendCount; - data >> mobSendCount; - - m_MobSends.resize(mobSendCount); - data.ReadSome(reinterpret_cast(m_MobSends.data()), mobSendCount * sizeof(MobSend)); -} - -DataBuffer UpgradeTowerPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_TowerID << m_TowerLevel; - return data; -} - -void UpgradeTowerPacket::Deserialize(DataBuffer& data) { - data >> m_TowerID >> m_TowerLevel; -} - -DataBuffer UpdateCastleLifePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_CastleLife << m_Team; - return data; -} - -void UpdateCastleLifePacket::Deserialize(DataBuffer& data) { - data >> m_CastleLife >> m_Team; -} - -DataBuffer UpdateMobStatesPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << static_cast(m_MobStates.size()); - - data.WriteSome(reinterpret_cast(m_MobStates.data()), m_MobStates.size() * sizeof(MobState)); - return data; -} - -void UpdateMobStatesPacket::Deserialize(DataBuffer& data) { - std::uint64_t mobCount; - data >> mobCount; - m_MobStates.resize(mobCount); - - data.ReadSome(reinterpret_cast(m_MobStates.data()), mobCount * sizeof(MobState)); -} - -DataBuffer PlayerBuyItemPacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_ItemType << m_Count; - return data; -} - -void PlayerBuyItemPacket::Deserialize(DataBuffer& data) { - data >> m_ItemType >> m_Count; -} - -DataBuffer PlayerBuyMobUpgradePacket::Serialize(bool packetID) const { - DataBuffer data; - - WritePacketID(data, packetID); - data << m_MobType << m_MobLevel; - return data; -} - -void PlayerBuyMobUpgradePacket::Deserialize(DataBuffer& data) { - data >> m_MobType >> m_MobLevel; -} - REGISTER_DISPATCH_CLASS(PlayerLoginPacket) REGISTER_DISPATCH_CLASS(WorldBeginDataPacket) REGISTER_DISPATCH_CLASS(WorldDataPacket) diff --git a/src/protocol/packets/ConnectionInfoPacket.cpp b/src/protocol/packets/ConnectionInfoPacket.cpp new file mode 100644 index 0000000..276b686 --- /dev/null +++ b/src/protocol/packets/ConnectionInfoPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/ConnectionInfoPacket.h" + +namespace td { +namespace protocol { + +DataBuffer ConnexionInfoPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_ConnectionID; + return data; +} + +void ConnexionInfoPacket::Deserialize(DataBuffer& data) { + data >> m_ConnectionID; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/DisconnectPacket.cpp b/src/protocol/packets/DisconnectPacket.cpp new file mode 100644 index 0000000..63527fd --- /dev/null +++ b/src/protocol/packets/DisconnectPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/DisconnectPacket.h" + +namespace td { +namespace protocol { + +DataBuffer DisconnectPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_Reason; + return data; +} + +void DisconnectPacket::Deserialize(DataBuffer& data) { + data >> m_Reason; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/KeepAlivePacket.cpp b/src/protocol/packets/KeepAlivePacket.cpp new file mode 100644 index 0000000..a9cfa56 --- /dev/null +++ b/src/protocol/packets/KeepAlivePacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/KeepAlivePacket.h" + +namespace td { +namespace protocol { + +DataBuffer KeepAlivePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_AliveID; + return data; +} + +void KeepAlivePacket::Deserialize(DataBuffer& data) { + data >> m_AliveID; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlaceTowerPacket.cpp b/src/protocol/packets/PlaceTowerPacket.cpp new file mode 100644 index 0000000..f604042 --- /dev/null +++ b/src/protocol/packets/PlaceTowerPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/PlaceTowerPacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlaceTowerPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_TowerX << m_TowerY << m_TowerType; + return data; +} + +void PlaceTowerPacket::Deserialize(DataBuffer& data) { + data >> m_TowerX >> m_TowerY >> m_TowerType; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerBuyItemPacket.cpp b/src/protocol/packets/PlayerBuyItemPacket.cpp new file mode 100644 index 0000000..e985822 --- /dev/null +++ b/src/protocol/packets/PlayerBuyItemPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/PlayerBuyItemPacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerBuyItemPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_ItemType << m_Count; + return data; +} + +void PlayerBuyItemPacket::Deserialize(DataBuffer& data) { + data >> m_ItemType >> m_Count; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerBuyMobUpgradePacket.cpp b/src/protocol/packets/PlayerBuyMobUpgradePacket.cpp new file mode 100644 index 0000000..eb2c69d --- /dev/null +++ b/src/protocol/packets/PlayerBuyMobUpgradePacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/PlayerBuyMobUpgradePacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerBuyMobUpgradePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_MobType << m_MobLevel; + return data; +} + +void PlayerBuyMobUpgradePacket::Deserialize(DataBuffer& data) { + data >> m_MobType >> m_MobLevel; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerJoinPacket.cpp b/src/protocol/packets/PlayerJoinPacket.cpp new file mode 100644 index 0000000..ca22659 --- /dev/null +++ b/src/protocol/packets/PlayerJoinPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/PlayerJoinPacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerJoinPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_PlayerID << m_PlayerName; + return data; +} + +void PlayerJoinPacket::Deserialize(DataBuffer& data) { + data >> m_PlayerID >> m_PlayerName; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerLeavePacket.cpp b/src/protocol/packets/PlayerLeavePacket.cpp new file mode 100644 index 0000000..eb5d733 --- /dev/null +++ b/src/protocol/packets/PlayerLeavePacket.cpp @@ -0,0 +1,20 @@ +#include "protocol/packets/PlayerLeavePacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerLeavePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_PlayerID; + return data; +} + +void PlayerLeavePacket::Deserialize(DataBuffer& data) { + data >> m_PlayerID; +} + + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerListPacket.cpp b/src/protocol/packets/PlayerListPacket.cpp new file mode 100644 index 0000000..9f1ed84 --- /dev/null +++ b/src/protocol/packets/PlayerListPacket.cpp @@ -0,0 +1,30 @@ +#include "protocol/packets/PlayerListPacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerListPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << static_cast(m_Players.size()); + for (auto [playerID, playerInfo] : m_Players) { + data << playerID << playerInfo.name << playerInfo.team; + } + return data; +} + +void PlayerListPacket::Deserialize(DataBuffer& data) { + std::uint8_t playerCount; + data >> playerCount; + + for (int i = 0; i < playerCount; i++) { + std::uint8_t playerID; + PlayerInfo playerInfo; + data >> playerID >> playerInfo.name >> playerInfo.team; + m_Players.insert({ playerID, playerInfo }); + } +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/PlayerLoginPacket.cpp b/src/protocol/packets/PlayerLoginPacket.cpp new file mode 100644 index 0000000..40c7c17 --- /dev/null +++ b/src/protocol/packets/PlayerLoginPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/PlayerLoginPacket.h" + +namespace td { +namespace protocol { + +DataBuffer PlayerLoginPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_PlayerName; + return data; +} + +void PlayerLoginPacket::Deserialize(DataBuffer& data) { + data >> m_PlayerName; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/RemoveTowerPacket.cpp b/src/protocol/packets/RemoveTowerPacket.cpp new file mode 100644 index 0000000..e7b0845 --- /dev/null +++ b/src/protocol/packets/RemoveTowerPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/RemoveTowerPacket.h" + +namespace td { +namespace protocol { + +DataBuffer RemoveTowerPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_TowerID; + return data; +} + +void RemoveTowerPacket::Deserialize(DataBuffer& data) { + data >> m_TowerID; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/SelectTeamPacket.cpp b/src/protocol/packets/SelectTeamPacket.cpp new file mode 100644 index 0000000..73e72a6 --- /dev/null +++ b/src/protocol/packets/SelectTeamPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/SelectTeamPacket.h" + +namespace td { +namespace protocol { + +DataBuffer SelectTeamPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_SelectedTeam; + return data; +} + +void SelectTeamPacket::Deserialize(DataBuffer& data) { + data >> m_SelectedTeam; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/SendMobsPacket.cpp b/src/protocol/packets/SendMobsPacket.cpp new file mode 100644 index 0000000..7460bc5 --- /dev/null +++ b/src/protocol/packets/SendMobsPacket.cpp @@ -0,0 +1,26 @@ +#include "protocol/packets/SendMobsPacket.h" + +namespace td { +namespace protocol { + +DataBuffer SendMobsPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << static_cast(m_MobSends.size()); + + data.WriteSome(reinterpret_cast(m_MobSends.data()), m_MobSends.size() * sizeof(m_MobSends)); + + return data; +} + +void SendMobsPacket::Deserialize(DataBuffer& data) { + std::uint8_t mobSendCount; + data >> mobSendCount; + + m_MobSends.resize(mobSendCount); + data.ReadSome(reinterpret_cast(m_MobSends.data()), mobSendCount * sizeof(MobSend)); +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/ServerTpsPacket.cpp b/src/protocol/packets/ServerTpsPacket.cpp new file mode 100644 index 0000000..34e6547 --- /dev/null +++ b/src/protocol/packets/ServerTpsPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/ServerTpsPacket.h" + +namespace td { +namespace protocol { + +DataBuffer ServerTpsPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_TPS << m_MSPT << m_PacketSendTime; + return data; +} + +void ServerTpsPacket::Deserialize(DataBuffer& data) { + data >> m_TPS >> m_MSPT >> m_PacketSendTime; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/SpawnMobPacket.cpp b/src/protocol/packets/SpawnMobPacket.cpp new file mode 100644 index 0000000..32c08a7 --- /dev/null +++ b/src/protocol/packets/SpawnMobPacket.cpp @@ -0,0 +1,21 @@ +#include "protocol/packets/SpawnMobPacket.h" + +namespace td { +namespace protocol { + +DataBuffer SpawnMobPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_MobID << m_MobType << m_MobLevel << m_MobDirection + << m_Sender << m_MobX << m_MobY; + return data; +} + +void SpawnMobPacket::Deserialize(DataBuffer& data) { + data >> m_MobID >> m_MobType >> m_MobLevel >> m_MobDirection + >> m_Sender >> m_MobX >> m_MobY; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateCastleLifePacket.cpp b/src/protocol/packets/UpdateCastleLifePacket.cpp new file mode 100644 index 0000000..0195078 --- /dev/null +++ b/src/protocol/packets/UpdateCastleLifePacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpdateCastleLifePacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateCastleLifePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_CastleLife << m_Team; + return data; +} + +void UpdateCastleLifePacket::Deserialize(DataBuffer& data) { + data >> m_CastleLife >> m_Team; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateExpPacket.cpp b/src/protocol/packets/UpdateExpPacket.cpp new file mode 100644 index 0000000..3a2b5dd --- /dev/null +++ b/src/protocol/packets/UpdateExpPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpdateExpPacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateExpPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_NewAmount; + return data; +} + +void UpdateExpPacket::Deserialize(DataBuffer& data) { + data >> m_NewAmount; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateGameStatePacket.cpp b/src/protocol/packets/UpdateGameStatePacket.cpp new file mode 100644 index 0000000..76e36f9 --- /dev/null +++ b/src/protocol/packets/UpdateGameStatePacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpdateGameStatePacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateGameStatePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_GameState; + return data; +} + +void UpdateGameStatePacket::Deserialize(DataBuffer& data) { + data >> m_GameState; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateLobbyTimePacket.cpp b/src/protocol/packets/UpdateLobbyTimePacket.cpp new file mode 100644 index 0000000..f0f8bde --- /dev/null +++ b/src/protocol/packets/UpdateLobbyTimePacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpdateLobbyTimePacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateLobbyTimePacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_RemainingTime; + return data; +} + +void UpdateLobbyTimePacket::Deserialize(DataBuffer& data) { + data >> m_RemainingTime; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateMobStatesPacket.cpp b/src/protocol/packets/UpdateMobStatesPacket.cpp new file mode 100644 index 0000000..6d7ebfb --- /dev/null +++ b/src/protocol/packets/UpdateMobStatesPacket.cpp @@ -0,0 +1,25 @@ +#include "protocol/packets/UpdateMobStatesPacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateMobStatesPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << static_cast(m_MobStates.size()); + + data.WriteSome(reinterpret_cast(m_MobStates.data()), m_MobStates.size() * sizeof(MobState)); + return data; +} + +void UpdateMobStatesPacket::Deserialize(DataBuffer& data) { + std::uint64_t mobCount; + data >> mobCount; + m_MobStates.resize(mobCount); + + data.ReadSome(reinterpret_cast(m_MobStates.data()), mobCount * sizeof(MobState)); +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdateMoneyPacket.cpp b/src/protocol/packets/UpdateMoneyPacket.cpp new file mode 100644 index 0000000..b648ae4 --- /dev/null +++ b/src/protocol/packets/UpdateMoneyPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpdateMoneyPacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpdateMoneyPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_NewAmount; + return data; +} + +void UpdateMoneyPacket::Deserialize(DataBuffer& data) { + data >> m_NewAmount; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpdatePlayerTeamPacket.cpp b/src/protocol/packets/UpdatePlayerTeamPacket.cpp new file mode 100644 index 0000000..d02241f --- /dev/null +++ b/src/protocol/packets/UpdatePlayerTeamPacket.cpp @@ -0,0 +1,20 @@ +#include "protocol/packets/UpdatePlayerTeamPacket.h" + +namespace td { +namespace protocol { + + +DataBuffer UpdatePlayerTeamPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_PlayerID << m_SelectedTeam; + return data; +} + +void UpdatePlayerTeamPacket::Deserialize(DataBuffer& data) { + data >> m_PlayerID >> m_SelectedTeam; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/UpgradeTowerPacket.cpp b/src/protocol/packets/UpgradeTowerPacket.cpp new file mode 100644 index 0000000..b274238 --- /dev/null +++ b/src/protocol/packets/UpgradeTowerPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/UpgradeTowerPacket.h" + +namespace td { +namespace protocol { + +DataBuffer UpgradeTowerPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_TowerID << m_TowerLevel; + return data; +} + +void UpgradeTowerPacket::Deserialize(DataBuffer& data) { + data >> m_TowerID >> m_TowerLevel; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/WorldAddTowerPacket.cpp b/src/protocol/packets/WorldAddTowerPacket.cpp new file mode 100644 index 0000000..9ddba29 --- /dev/null +++ b/src/protocol/packets/WorldAddTowerPacket.cpp @@ -0,0 +1,19 @@ +#include "protocol/packets/WorldAddTowerPacket.h" + +namespace td { +namespace protocol { + +DataBuffer WorldAddTowerPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + data << m_TowerID << m_TowerX << m_TowerY << m_TowerType << m_Builder; + return data; +} + +void WorldAddTowerPacket::Deserialize(DataBuffer& data) { + data >> m_TowerID >> m_TowerX >> m_TowerY >> m_TowerType >> m_Builder; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/WorldBeginDataPacket.cpp b/src/protocol/packets/WorldBeginDataPacket.cpp new file mode 100644 index 0000000..29a96a2 --- /dev/null +++ b/src/protocol/packets/WorldBeginDataPacket.cpp @@ -0,0 +1,81 @@ +#include "protocol/packets/WorldBeginDataPacket.h" + +namespace td { +namespace protocol { + +DataBuffer WorldBeginDataPacket::Serialize(bool packetID) const { + DataBuffer data; + const game::TowerTileColorPalette& towerTilePalette = m_Header.m_World->GetTowerTileColorPalette(); + const std::vector& decoTilePalette = m_Header.m_World->GetDecorationPalette(); + + WritePacketID(data, packetID); + + data << towerTilePalette << m_Header.m_World->GetWalkableTileColor() + << static_cast(decoTilePalette.size()); + + // deco color palette + std::size_t bufferSize = data.GetSize(); + data.Resize(bufferSize + decoTilePalette.size() * sizeof(Color)); + + memcpy(reinterpret_cast(data.data()) + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(Color)); + + data << m_Header.m_World->GetBackgroundColor(); + + const game::Spawn& redSpawn = m_Header.m_World->GetRedTeam().GetSpawn(), blueSpawn = m_Header.m_World->GetBlueTeam().GetSpawn(); + const game::TeamCastle& redCastle = m_Header.m_World->GetRedTeam().GetCastle(), blueCastle = m_Header.m_World->GetBlueTeam().GetCastle(); + + data << redSpawn << static_cast(redCastle); + data << blueSpawn << static_cast(blueCastle); + + // tile palette + data << static_cast(m_Header.m_World->GetTilePalette().size()); + + for (game::TilePtr tile : m_Header.m_World->GetTilePalette()) { + data << tile; + } + + data << m_Header.m_World->GetSpawnColors(); + + return data; +} + +void WorldBeginDataPacket::Deserialize(DataBuffer& data) { + data >> m_Header.m_TowerPlacePalette >> m_Header.m_WalkablePalette; + + std::uint16_t decoPaletteSize; + data >> decoPaletteSize; + + std::size_t decoPalletteSizeByte = decoPaletteSize * sizeof(Color); + + m_Header.m_DecorationPalette.resize(decoPaletteSize); + + memcpy(reinterpret_cast(m_Header.m_DecorationPalette.data()), data.data() + data.GetReadOffset(), decoPalletteSizeByte); + + data.SetReadOffset(data.GetReadOffset() + decoPalletteSizeByte); + + data >> m_Header.m_Background; + + utils::shape::Rectangle redCastle, blueCastle; + + data >> m_Header.m_RedSpawn >> redCastle; + data >> m_Header.m_BlueSpawn >> blueCastle; + + m_Header.m_RedCastle.SetShape(redCastle); + m_Header.m_BlueCastle.SetShape(blueCastle); + + std::uint64_t tilePaletteSize; + data >> tilePaletteSize; + + m_Header.m_TilePalette.reserve(tilePaletteSize); + + for (std::uint64_t tileNumber = 0; tileNumber < tilePaletteSize; tileNumber++) { + game::TilePtr tile; + data >> tile; + m_Header.m_TilePalette.push_back(tile); + } + + data >> m_Header.m_SpawnColorPalette; +} + +} // namespace protocol +} // namespace td diff --git a/src/protocol/packets/WorldDataPacket.cpp b/src/protocol/packets/WorldDataPacket.cpp new file mode 100644 index 0000000..dd44a98 --- /dev/null +++ b/src/protocol/packets/WorldDataPacket.cpp @@ -0,0 +1,173 @@ +#include "protocol/packets/WorldDataPacket.h" + +namespace td { +namespace protocol { + +const int BITS_IN_BYTE = 8; +const int BITS_IN_LONG = BITS_IN_BYTE * sizeof(std::uint64_t); + +static unsigned int countBits(unsigned int number) { + // log function in base 2 + // take only integer part + return static_cast(std::log2(number) + 1); +} + +static DataBuffer& operator<<(DataBuffer& buffer, game::TilePtr tile) { + buffer << tile->GetType(); + + switch (tile->GetType()) { + + case game::TileType::Tower: { + const game::TowerTile* towerTile = dynamic_cast(tile.get()); + buffer << towerTile->color_palette_ref << towerTile->team_owner; + break; + } + + case game::TileType::Walk: { + const game::WalkableTile* walkTile = dynamic_cast(tile.get()); + buffer << walkTile->direction; + break; + } + + case game::TileType::Decoration: { + const game::DecorationTile* decoTile = dynamic_cast(tile.get()); + buffer << decoTile->color_palette_ref; + break; + } + + default: + break; + } + + return buffer; +} + +static DataBuffer& operator>>(DataBuffer& buffer, game::TilePtr& tile) { + game::TileType tileType; + buffer >> tileType; + switch (tileType) { + case game::TileType::Tower: { + auto tilePtr = std::make_shared(); + buffer >> tilePtr->color_palette_ref >> tilePtr->team_owner; + tile = tilePtr; + break; + } + case game::TileType::Walk: { + auto tilePtr = std::make_shared(); + buffer >> tilePtr->direction; + tile = tilePtr; + break; + } + case game::TileType::Decoration: { + auto tilePtr = std::make_shared(); + buffer >> tilePtr->color_palette_ref; + tile = tilePtr; + break; + } + default: + break; + } + return buffer; +} + +typedef std::vector ChunkPackedData; + +DataBuffer WorldDataPacket::Serialize(bool packetID) const { + DataBuffer data; + + WritePacketID(data, packetID); + + data << m_World->GetChunks().size(); + for (const auto& pair : m_World->GetChunks()) { + game::ChunkCoord coords = pair.first; + game::ChunkPtr chunk = pair.second; + + data << coords.x << coords.y << static_cast(chunk->palette.size()); + + std::size_t bufferSize = data.GetSize(); + data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); + memcpy(reinterpret_cast(data.data()) + bufferSize, chunk->palette.data(), chunk->palette.size() * sizeof(game::ChunkPalette::value_type)); + + std::uint8_t bitsPerTile = countBits(chunk->palette.size()); + + game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); + + ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); + + for (unsigned int tileNumber = 0; tileNumber < game::Chunk::ChunkSize; tileNumber++) { + std::size_t startLong = static_cast((tileNumber * bitsPerTile) / BITS_IN_LONG); + std::size_t startOffset = static_cast((tileNumber * bitsPerTile) % BITS_IN_LONG); + std::size_t endLong = static_cast(((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG); + + std::uint64_t value = static_cast(chunk->tiles[tileNumber]); + + value &= individualValueMask; + + chunkData[startLong] |= (value << startOffset); + + if (startLong != endLong) { + chunkData[endLong] = (value >> (BITS_IN_LONG - startOffset)); + } + } + + bufferSize = data.GetSize(); + data.Resize(data.GetSize() + chunkData.size() * sizeof(ChunkPackedData::value_type)); + memcpy(reinterpret_cast(data.data()) + bufferSize, chunkData.data(), chunkData.size() * sizeof(ChunkPackedData::value_type)); + } + return data; +} + +void WorldDataPacket::Deserialize(DataBuffer& data) { + std::uint64_t chunkCount; + data >> chunkCount; + + for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) { + game::ChunkPtr chunk = std::make_shared(); + + decltype(game::ChunkCoord::x) chunkX, chunkY; + data >> chunkX >> chunkY; + + std::uint64_t chunkPaletteSize; + data >> chunkPaletteSize; + + game::ChunkPalette chunkPalette(chunkPaletteSize); + + memcpy(reinterpret_cast(chunkPalette.data()), data.data() + data.GetReadOffset(), chunkPaletteSize * sizeof(game::ChunkPalette::value_type)); + data.SetReadOffset(data.GetReadOffset() + chunkPaletteSize * sizeof(game::ChunkPalette::value_type)); + + chunk->palette = chunkPalette; + + std::uint8_t bitsPerTile = countBits(chunkPaletteSize); + + // A bitmask that contains bitsPerTile set bits + game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1); + + ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0); + + memcpy(reinterpret_cast(chunkData.data()), data.data() + data.GetReadOffset(), chunkData.size() * sizeof(ChunkPackedData::value_type)); + data.SetReadOffset(data.GetReadOffset() + chunkData.size() * sizeof(ChunkPackedData::value_type)); + + for (unsigned int tileNumber = 0; tileNumber < game::Chunk::ChunkSize; tileNumber++) { + std::size_t startLong = (tileNumber * bitsPerTile) / BITS_IN_LONG; + std::size_t startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG; + std::size_t endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG; + + game::Chunk::ChunkData::value_type value; + if (startLong == endLong) { + value = (chunkData[startLong] >> startOffset); + } else { + int endOffset = BITS_IN_LONG - startOffset; + value = (chunkData[startLong] >> startOffset | chunkData[endLong] << endOffset); + } + value &= individualValueMask; + + chunk->tiles[tileNumber] = value; + } + + + m_WorldData.m_Chunks.insert({ {chunkX, chunkY}, chunk }); + } +} + +} // namespace protocol +} // namespace td diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index 74b0ffc..f4ba27f 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -11,18 +11,6 @@ namespace td { namespace render { -ImVec4 WorldRenderer::GetImGuiTeamColor(game::TeamColor color) { - switch (color) { - case td::game::TeamColor::None: - break; - case td::game::TeamColor::Red: - return ImVec4(1, 0, 0, 1); - case td::game::TeamColor::Blue: - return ImVec4(0, 0, 1, 1); - } - return ImVec4(1, 1, 1, 1); -} - void WorldRenderer::LoadModels() { utils::LOGD("World Created !"); m_WorldVao = std::make_unique(std::move(WorldLoader::LoadWorldModel(m_World))); diff --git a/src/render/gui/CastleTooltip.cpp b/src/render/gui/CastleTooltip.cpp index 4d8d7d7..e8b3518 100644 --- a/src/render/gui/CastleTooltip.cpp +++ b/src/render/gui/CastleTooltip.cpp @@ -1,6 +1,7 @@ #include "render/gui/CastleTooltip.h" #include "render/gui/imgui/imgui.h" #include "render/gui/LifeProgress.h" +#include "render/gui/ImGuiTeamColor.h" #include "render/WorldRenderer.h" @@ -18,7 +19,7 @@ void CastleTooltip::Render() { if (ImGui::GetIO().KeyShift) { ImGui::BeginTooltip(); - ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(m_Castle->GetTeam()->GetColor())); + ImGui::PushStyleColor(ImGuiCol_Text, render::GetImGuiTeamColor(m_Castle->GetTeam()->GetColor())); ImGui::Text("Castle : "); ImGui::PopStyleColor(); ImGui::Text("\tCastle HP : %i/%i", static_cast(m_Castle->GetLife()), game::TeamCastle::CastleMaxLife); diff --git a/src/render/gui/GameMenu.cpp b/src/render/gui/GameMenu.cpp index ba5943d..78ed984 100644 --- a/src/render/gui/GameMenu.cpp +++ b/src/render/gui/GameMenu.cpp @@ -1,5 +1,6 @@ #include "render/gui/GameMenu.h" #include "render/gui/imgui/imgui.h" +#include "render/gui/ImGuiTeamColor.h" #include "render/WorldRenderer.h" @@ -43,7 +44,7 @@ void GameMenu::ShowPlayers() { if (ImGui::TreeNode(std::string("Players (" + std::to_string(GetClient()->GetGame().GetPlayers().size()) + ")##player_list").c_str())) { for (auto pair : GetClient()->GetGame().GetPlayers()) { const td::game::Player& player = pair.second; - ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(player.GetTeamColor())); + ImGui::PushStyleColor(ImGuiCol_Text, render::GetImGuiTeamColor(player.GetTeamColor())); ImGui::Text("%s", player.GetName().c_str()); ImGui::PopStyleColor(); } diff --git a/src/render/gui/ImGuiTeamColor.cpp b/src/render/gui/ImGuiTeamColor.cpp new file mode 100644 index 0000000..309c041 --- /dev/null +++ b/src/render/gui/ImGuiTeamColor.cpp @@ -0,0 +1,19 @@ +#include "render/gui/ImGuiTeamColor.h" + +namespace td { +namespace render { + +ImVec4 GetImGuiTeamColor(game::TeamColor color) { + switch (color) { + case td::game::TeamColor::None: + break; + case td::game::TeamColor::Red: + return ImVec4(1, 0, 0, 1); + case td::game::TeamColor::Blue: + return ImVec4(0, 0, 1, 1); + } + return ImVec4(1, 1, 1, 1); +} + +} // namespace render +} // namespace td diff --git a/src/render/gui/MobTooltip.cpp b/src/render/gui/MobTooltip.cpp index aa0279a..9adb67b 100644 --- a/src/render/gui/MobTooltip.cpp +++ b/src/render/gui/MobTooltip.cpp @@ -1,6 +1,7 @@ #include "render/gui/MobTooltip.h" #include "render/gui/imgui/imgui.h" #include "render/gui/LifeProgress.h" +#include "render/gui/ImGuiTeamColor.h" #include "render/WorldRenderer.h" @@ -25,7 +26,7 @@ void MobTooltip::Render() { ImGui::BeginTooltip(); ImGui::Text("Sender :"); ImGui::SameLine(); - ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(sender->GetTeamColor())); + ImGui::PushStyleColor(ImGuiCol_Text, render::GetImGuiTeamColor(sender->GetTeamColor())); ImGui::Text("%s", sender->GetName().c_str()); ImGui::PopStyleColor(); ImGui::Text("Mob HP : %.1f/%i", m_Mob->GetHealth(), m_Mob->GetStats()->GetMaxLife());