]> git.localhorst.tv Git - blank.git/blobdiff - src/net/net.cpp
add packet for merging player state back to client
[blank.git] / src / net / net.cpp
index 4e6a63a9ad07a31330e004de4171f813aa848c40..3f4bf7453eab78a0ac79b7f045026ec10114a8f0 100644 (file)
@@ -8,6 +8,8 @@
 
 #include "../app/init.hpp"
 #include "../model/CompositeModel.hpp"
+#include "../world/Entity.hpp"
+#include "../world/EntityState.hpp"
 #include "../world/World.hpp"
 
 #include <cstring>
@@ -27,6 +29,7 @@ constexpr size_t Packet::PlayerUpdate::MAX_LEN;
 constexpr size_t Packet::SpawnEntity::MAX_LEN;
 constexpr size_t Packet::DespawnEntity::MAX_LEN;
 constexpr size_t Packet::EntityUpdate::MAX_LEN;
+constexpr size_t Packet::PlayerCorrection::MAX_LEN;
 
 namespace {
 
@@ -51,13 +54,11 @@ IPaddress client_resolve(const char *host, Uint16 port) {
 Client::Client(const Config &conf)
 : conn(client_resolve(conf.host.c_str(), conf.port))
 , client_sock(client_bind(0))
-, client_pack{ -1, nullptr, 0 }
-, update_timer(16) {
+, client_pack{ -1, nullptr, 0 } {
        client_pack.data = new Uint8[sizeof(Packet)];
        client_pack.maxlen = sizeof(Packet);
        // establish connection
        SendPing();
-       update_timer.Start();
 }
 
 Client::~Client() {
@@ -93,7 +94,6 @@ void Client::HandlePacket(const UDPpacket &udp_pack) {
 }
 
 void Client::Update(int dt) {
-       update_timer.Update(dt);
        conn.Update(dt);
        if (conn.ShouldPing()) {
                SendPing();
@@ -110,9 +110,7 @@ uint16_t Client::SendLogin(const string &name) {
        return conn.Send(client_pack, client_sock);
 }
 
-int Client::SendPlayerUpdate(const Entity &player) {
-       // don't send all too many updates
-       if (!update_timer.Hit()) return -1;
+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);
@@ -190,6 +188,8 @@ void ClientConnection::Update(int dt) {
                        SendDespawn(*local_iter);
                        ++local_iter;
                }
+
+               CheckPlayerFix();
        }
        if (conn.ShouldPing()) {
                conn.SendPing(server.GetPacket(), server.GetSocket());
@@ -252,6 +252,14 @@ void ClientConnection::SendUpdate(SpawnStatus &status) {
        conn.Send(server.GetPacket(), server.GetSocket());
 }
 
+void ClientConnection::CheckPlayerFix() {
+       // check always succeeds for now ;)
+       auto pack = Packet::Make<Packet::PlayerCorrection>(server.GetPacket());
+       pack.WritePacketSeq(player_update_pack);
+       pack.WritePlayer(Player());
+       conn.Send(server.GetPacket(), server.GetSocket());
+}
+
 void ClientConnection::AttachPlayer(Entity &new_player) {
        DetachPlayer();
        player = &new_player;
@@ -339,7 +347,8 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) {
        player_update_timer.Reset();
        if (pack_diff > 0 || overdue) {
                player_update_pack = pack.Seq();
-               pack.ReadPlayer(Player());
+               // TODO: do client input validation here
+               pack.ReadPlayerState(Player().GetState());
        }
 }
 
@@ -493,6 +502,8 @@ const char *Packet::Type2String(uint8_t t) noexcept {
                        return "DespawnEntity";
                case EntityUpdate::TYPE:
                        return "EntityUpdate";
+               case PlayerCorrection::TYPE:
+                       return "PlayerCorrection";
                default:
                        return "Unknown";
        }
@@ -547,34 +558,15 @@ void Packet::Login::ReadPlayerName(string &name) const noexcept {
 
 void Packet::Join::WritePlayer(const Entity &player) noexcept {
        Write(player.ID(), 0);
-       Write(player.ChunkCoords(), 4);
-       Write(player.Position(), 16);
-       Write(player.Velocity(), 28);
-       Write(player.Orientation(), 40);
-       Write(player.AngularVelocity(), 56);
+       Write(player.GetState(), 4);
 }
 
 void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept {
        Read(id, 0);
 }
 
-void Packet::Join::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, 4);
-       Read(pos, 16);
-       Read(vel, 28);
-       Read(rot, 40);
-       Read(ang, 56);
-
-       player.Position(chunk_coords, pos);
-       player.Velocity(vel);
-       player.Orientation(rot);
-       player.AngularVelocity(ang);
+void Packet::Join::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 4);
 }
 
 void Packet::Join::WriteWorldName(const string &name) noexcept {
@@ -586,30 +578,11 @@ void Packet::Join::ReadWorldName(string &name) const noexcept {
 }
 
 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);
+       Write(player.GetState(), 0);
 }
 
-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 Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 0);
 }
 
 void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
@@ -619,11 +592,7 @@ void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
        } else {
                Write(uint32_t(0), 4);
        }
-       Write(e.ChunkCoords(), 8);
-       Write(e.Position(), 20);
-       Write(e.Velocity(), 32);
-       Write(e.Orientation(), 44);
-       Write(e.AngularVelocity(), 60);
+       Write(e.GetState(), 8);
        Write(e.Bounds(), 72);
        uint32_t flags = 0;
        if (e.WorldCollidable()) {
@@ -642,28 +611,17 @@ void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept {
 }
 
 void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept {
-       glm::ivec3 chunk_coords(0);
-       glm::vec3 pos;
-       glm::vec3 vel;
-       glm::quat rot;
-       glm::vec3 ang;
+       EntityState state;
        AABB bounds;
        uint32_t flags = 0;
        string name;
 
-       Read(chunk_coords, 8);
-       Read(pos, 20);
-       Read(vel, 32);
-       Read(rot, 44);
-       Read(ang, 60);
+       Read(state, 8);
        Read(bounds, 72);
        Read(flags, 96);
        ReadString(name, 100, 32);
 
-       e.Position(chunk_coords, pos);
-       e.Velocity(vel);
-       e.Orientation(rot);
-       e.AngularVelocity(ang);
+       e.SetState(state);
        e.Bounds(bounds);
        e.WorldCollidable(flags & 1);
        e.Name(name);
@@ -689,36 +647,32 @@ void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexc
        uint32_t off = 4 + (num * 64);
 
        Write(entity.ID(), off);
-       Write(entity.ChunkCoords(), off + 4);
-       Write(entity.Position(), off + 16);
-       Write(entity.Velocity(), off + 28);
-       Write(entity.Orientation(), off + 40);
-       Write(entity.AngularVelocity(), off + 56);
+       Write(entity.GetState(), off + 4);
 }
 
 void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept {
        Read(id, 4 + (num * 64));
 }
 
-void Packet::EntityUpdate::ReadEntity(Entity &entity, uint32_t num) const noexcept {
+void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept {
        uint32_t off = 4 + (num * 64);
+       Read(state, off + 4);
+}
 
-       glm::ivec3 chunk_coords(0);
-       glm::vec3 pos;
-       glm::vec3 vel;
-       glm::quat rot;
-       glm::vec3 ang;
+void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept {
+       Write(s, 0);
+}
 
-       Read(chunk_coords, off + 4);
-       Read(pos, off + 16);
-       Read(vel, off + 28);
-       Read(rot, off + 40);
-       Read(ang, off + 56);
+void Packet::PlayerCorrection::ReadPacketSeq(std::uint16_t &s) const noexcept {
+       Read(s, 0);
+}
 
-       entity.Position(chunk_coords, pos);
-       entity.Velocity(vel);
-       entity.Orientation(rot);
-       entity.AngularVelocity(ang);
+void Packet::PlayerCorrection::WritePlayer(const Entity &player) noexcept {
+       Write(player.GetState(), 2);
+}
+
+void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 2);
 }
 
 
@@ -749,6 +703,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
                case Packet::EntityUpdate::TYPE:
                        On(Packet::As<Packet::EntityUpdate>(udp_pack));
                        break;
+               case Packet::PlayerCorrection::TYPE:
+                       On(Packet::As<Packet::PlayerCorrection>(udp_pack));
+                       break;
                default:
                        // drop unknown or unhandled packets
                        break;