]> git.localhorst.tv Git - blank.git/commitdiff
send player updates from client to server
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 5 Sep 2015 14:16:45 +0000 (16:16 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 5 Sep 2015 14:19:01 +0000 (16:19 +0200)
doc/protocol
src/client/client.cpp
src/net/Client.hpp
src/net/ClientConnection.hpp
src/net/ConnectionHandler.hpp
src/net/Packet.hpp
src/net/net.cpp

index a20d8d641073f3c561aa3b04e9076c38d034fd95..43c14dae4933a0ad9de335dd7cf2bebd0f52d58b 100644 (file)
@@ -30,6 +30,7 @@ a while.
 
 Code: 0
 Payload: none
+Length: 0
 
 
 Login
@@ -43,6 +44,7 @@ Code: 1
 Payload:
        0 player name, max 32 byte UTF-8 string,
          shorter names should be zero terminated
+Length: 0-32
 
 
 Join
@@ -58,6 +60,7 @@ Payload:
        16 pos/vel/rot/ang of the player, 13x 32bit float
        68 name of the world the server's currently running
           max 32 byte UTF-8 string
+Length: 68-100
 
 
 Part
@@ -69,3 +72,16 @@ Optionally sent by the client on disconnect.
 
 Code: 3
 Payload: none
+Length: 0
+
+
+Player Update
+-------------
+
+Sent by clients to notify the server of their changes to the player.
+
+Code: 4
+Payload:
+        0 chunk coords of the player, 3x 32bit signed int
+       12 pos/vel/rot/ang of the player, 13x 32bit float
+Length: 64
index 8dd575a0ac8bf03aa9c10068319b998f0de235f0..9c87549a3e27094c1ad8ffc8cc47b3143c550b53 100644 (file)
@@ -99,6 +99,8 @@ void InteractiveState::Update(int dt) {
        chunk_renderer.Rebase(interface.Player().ChunkCoords());
        chunk_renderer.Update(dt);
 
+       master.GetClient().SendPlayerUpdate(interface.Player());
+
        glm::mat4 trans = interface.Player().Transform(interface.Player().ChunkCoords());
        glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
        glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
index bd29c85fa0add60cbb6dc3df4f85af51933491b3..5c0a9c2b378344f8e4c7db1700de664093bd0b69 100644 (file)
@@ -32,6 +32,7 @@ public:
 
        std::uint16_t SendPing();
        std::uint16_t SendLogin(const std::string &);
+       std::uint16_t SendPlayerUpdate(const Entity &);
 
 private:
        void HandlePacket(const UDPpacket &);
index acbee6ec98ce47a7ee15979e4f296f1efc4e36b1..8c5306c07945d4f80d4af15c44400d160508126e 100644 (file)
@@ -28,9 +28,12 @@ public:
 
        void AttachPlayer(Entity &);
        void DetachPlayer();
+       bool HasPlayer() const noexcept { return player; }
+       Entity &Player() noexcept { return *player; }
 
        void On(const Packet::Login &) override;
        void On(const Packet::Part &) override;
+       void On(const Packet::PlayerUpdate &) override;
 
 private:
        Server &server;
index 3eb8f55d1e09e6c6550f6bcb5e4902692767b8f0..29595e5061e01c9bac2a8671158191cd6e768d68 100644 (file)
@@ -22,6 +22,7 @@ private:
        virtual void On(const Packet::Login &) { }
        virtual void On(const Packet::Join &) { }
        virtual void On(const Packet::Part &) { }
+       virtual void On(const Packet::PlayerUpdate &) { }
 
 };
 
index 5e9751cb434a29c1117a6203b6b59fe88e6a7430..0dab9495fef9327e40ce7cd8e5283d4c4e8ab207 100644 (file)
@@ -86,6 +86,14 @@ struct Packet {
                static constexpr std::size_t MAX_LEN = 0;
        };
 
+       struct PlayerUpdate : public Payload {
+               static constexpr std::uint8_t TYPE = 4;
+               static constexpr std::size_t MAX_LEN = 64;
+
+               void WritePlayer(const Entity &) noexcept;
+               void ReadPlayer(Entity &) const noexcept;
+       };
+
 
        template<class PayloadType>
        PayloadType As() {
index 7f12e63ff3626b4d23f741d6d38cea9304ed7ad6..837a7fb18125dccf52b7ea4e8eca491e5d27b566 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <cstring>
 #include <iostream>
+#include <glm/gtx/io.hpp>
 
 using namespace std;
 
@@ -21,6 +22,7 @@ constexpr size_t Packet::Ping::MAX_LEN;
 constexpr size_t Packet::Login::MAX_LEN;
 constexpr size_t Packet::Join::MAX_LEN;
 constexpr size_t Packet::Part::MAX_LEN;
+constexpr size_t Packet::PlayerUpdate::MAX_LEN;
 
 namespace {
 
@@ -101,6 +103,12 @@ uint16_t Client::SendLogin(const string &name) {
        return conn.Send(client_pack, client_sock);
 }
 
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
+       auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
+       pack.WritePlayer(player);
+       return conn.Send(client_pack, client_sock);
+}
+
 
 ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
 : server(server)
@@ -162,6 +170,11 @@ void ClientConnection::On(const Packet::Part &) {
        conn.Close();
 }
 
+void ClientConnection::On(const Packet::PlayerUpdate &pack) {
+       if (!HasPlayer()) return;
+       pack.ReadPlayer(Player());
+}
+
 
 Connection::Connection(const IPaddress &addr)
 : handler(nullptr)
@@ -291,6 +304,8 @@ const char *Packet::Type2String(uint8_t t) noexcept {
                        return "Join";
                case Part::TYPE:
                        return "Part";
+               case PlayerUpdate::TYPE:
+                       return "PlayerUpdate";
                default:
                        return "Unknown";
        }
@@ -383,6 +398,33 @@ void Packet::Join::ReadWorldName(string &name) const noexcept {
        ReadString(name, 68, 32);
 }
 
+void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept {
+       Write(player.ChunkCoords(), 0);
+       Write(player.Position(), 12);
+       Write(player.Velocity(), 24);
+       Write(player.Orientation(), 36);
+       Write(player.AngularVelocity(), 52);
+}
+
+void Packet::PlayerUpdate::ReadPlayer(Entity &player) const noexcept {
+       glm::ivec3 chunk_coords(0);
+       glm::vec3 pos;
+       glm::vec3 vel;
+       glm::quat rot;
+       glm::vec3 ang;
+
+       Read(chunk_coords, 0);
+       Read(pos, 12);
+       Read(vel, 24);
+       Read(rot, 36);
+       Read(ang, 52);
+
+       player.Position(chunk_coords, pos);
+       player.Velocity(vel);
+       player.Orientation(rot);
+       player.AngularVelocity(ang);
+}
+
 
 void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
        const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
@@ -399,6 +441,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
                case Packet::Part::TYPE:
                        On(Packet::As<Packet::Part>(udp_pack));
                        break;
+               case Packet::PlayerUpdate::TYPE:
+                       On(Packet::As<Packet::PlayerUpdate>(udp_pack));
+                       break;
                default:
                        // drop unknown or unhandled packets
                        break;