]> git.localhorst.tv Git - blank.git/blobdiff - src/net/net.cpp
grouped entity physics state into a struct
[blank.git] / src / net / net.cpp
index e6af690aeacb36f16ad0c289843b3df456833ffe..13ea4d372784c1f177e404b63394a32d325c5be5 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>
@@ -124,7 +126,9 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
 , conn(addr)
 , player(nullptr)
 , spawns()
-, confirm_wait(0) {
+, confirm_wait(0)
+, player_update_pack(0)
+, player_update_timer(1500) {
        conn.SetHandler(this);
 }
 
@@ -308,6 +312,10 @@ void ClientConnection::On(const Packet::Login &pack) {
                response.WritePlayer(*new_player);
                response.WriteWorldName(server.GetWorld().Name());
                conn.Send(server.GetPacket(), server.GetSocket());
+               // set up update tracking
+               player_update_pack = pack.Seq();
+               player_update_timer.Reset();
+               player_update_timer.Start();
        } else {
                // aw no :(
                cout << "rejected login from player \"" << name << '"' << endl;
@@ -323,7 +331,14 @@ void ClientConnection::On(const Packet::Part &) {
 
 void ClientConnection::On(const Packet::PlayerUpdate &pack) {
        if (!HasPlayer()) return;
-       pack.ReadPlayer(Player());
+       int pack_diff = int16_t(pack.Seq()) - int16_t(player_update_pack);
+       bool overdue = player_update_timer.HitOnce();
+       player_update_timer.Reset();
+       if (pack_diff > 0 || overdue) {
+               player_update_pack = pack.Seq();
+               // TODO: do client input validation here
+               pack.ReadPlayerState(Player().GetState());
+       }
 }
 
 
@@ -530,34 +545,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 {
@@ -569,30 +565,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 {
@@ -602,11 +579,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()) {
@@ -625,28 +598,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);
@@ -672,36 +634,16 @@ 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);
-
-       glm::ivec3 chunk_coords(0);
-       glm::vec3 pos;
-       glm::vec3 vel;
-       glm::quat rot;
-       glm::vec3 ang;
-
-       Read(chunk_coords, off + 4);
-       Read(pos, off + 16);
-       Read(vel, off + 28);
-       Read(rot, off + 40);
-       Read(ang, off + 56);
-
-       entity.Position(chunk_coords, pos);
-       entity.Velocity(vel);
-       entity.Orientation(rot);
-       entity.AngularVelocity(ang);
+       Read(state, off + 4);
 }