]> git.localhorst.tv Git - blank.git/commitdiff
second attempt at "nice" client state correction
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 16 Sep 2015 09:46:58 +0000 (11:46 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 16 Sep 2015 09:46:58 +0000 (11:46 +0200)
src/app/ServerState.cpp
src/app/ServerState.hpp
src/client/InteractiveState.hpp
src/client/client.cpp
src/net/ClientConnection.hpp
src/net/net.cpp

index d980cbcc697ce4dda8a4c393bdefb88a0a6398e8..13abdc9144d27cb4623e0a55dd5dfd0e9af0b08d 100644 (file)
@@ -24,14 +24,12 @@ ServerState::ServerState(
 , skeletons()
 , spawner(world, skeletons, gc.seed)
 , server(sc, world)
-, loop_timer(16)
-, push_timer(16) {
+, loop_timer(16) {
        TextureIndex tex_index;
        env.loader.LoadBlockTypes("default", block_types, tex_index);
        skeletons.LoadHeadless();
 
        loop_timer.Start();
-       push_timer.Start();
 
        std::cout << "listening on UDP port " << sc.port << std::endl;
 }
@@ -46,16 +44,17 @@ void ServerState::Handle(const SDL_Event &event) {
 
 void ServerState::Update(int dt) {
        loop_timer.Update(dt);
-       push_timer.Update(dt);
        server.Handle();
+       int world_dt = 0;
        while (loop_timer.HitOnce()) {
                spawner.Update(loop_timer.Interval());
                world.Update(loop_timer.Interval());
+               world_dt += loop_timer.Interval();
                loop_timer.PopIteration();
        }
        chunk_loader.Update(dt);
-       if (push_timer.Hit()) {
-               server.Update(dt);
+       if (world_dt > 0) {
+               server.Update(world_dt);
        }
 }
 
index afa7840bbfc8e040993ca47653d85dc6e311f4fb..db21d0fd3f643c3ca1a5a995321a9f1ee7615d36 100644 (file)
@@ -43,7 +43,6 @@ private:
        Spawner spawner;
        Server server;
        IntervalTimer loop_timer;
-       IntervalTimer push_timer;
 
 };
 
index eaca4c9c78880d8679755c24d71f2558f7ea1977..09a9919a2e7b15c8c563a480bdae426420480c02 100644 (file)
@@ -38,7 +38,7 @@ public:
        void Update(int dt) override;
        void Render(Viewport &) override;
 
-       void PushPlayerUpdate(const Entity &);
+       void PushPlayerUpdate(const Entity &, int dt);
        void MergePlayerCorrection(std::uint16_t, const EntityState &);
 
 private:
@@ -50,13 +50,13 @@ private:
        ChunkRenderer chunk_renderer;
        Skeletons skeletons;
        IntervalTimer loop_timer;
-       IntervalTimer update_timer;
 
        struct PlayerHistory {
                EntityState state;
+               int delta_t;
                std::uint16_t packet;
-               PlayerHistory(EntityState s, std::uint16_t p)
-               : state(s), packet(p) { }
+               PlayerHistory(EntityState s, int dt, std::uint16_t p)
+               : state(s), delta_t(dt), packet(p) { }
        };
        std::list<PlayerHistory> player_hist;
 
index dbdaa73641c5f52c58949d82d2f7b09bb0038bed..72d860d97772e75706d8ae81e2f5df5ec90fa756 100644 (file)
@@ -57,7 +57,6 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , chunk_renderer(*interface.GetPlayer().chunks)
 , skeletons()
 , loop_timer(16)
-, update_timer(16)
 , player_hist() {
        TextureIndex tex_index;
        master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
@@ -67,7 +66,6 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        // TODO: better solution for initializing HUD
        interface.SelectNext();
        loop_timer.Start();
-       update_timer.Start();
 }
 
 void InteractiveState::OnEnter() {
@@ -104,20 +102,21 @@ void InteractiveState::Handle(const SDL_Event &event) {
 
 void InteractiveState::Update(int dt) {
        loop_timer.Update(dt);
-       update_timer.Update(dt);
        master.Update(dt);
 
        interface.Update(dt);
+       int world_dt = 0;
        while (loop_timer.HitOnce()) {
                world.Update(loop_timer.Interval());
+               world_dt += loop_timer.Interval();
                loop_timer.PopIteration();
        }
        chunk_renderer.Update(dt);
 
        Entity &player = *interface.GetPlayer().entity;
 
-       if (update_timer.Hit()) {
-               PushPlayerUpdate(player);
+       if (world_dt > 0) {
+               PushPlayerUpdate(player, world_dt);
        }
 
        glm::mat4 trans = player.Transform(player.ChunkCoords());
@@ -128,13 +127,14 @@ void InteractiveState::Update(int dt) {
        master.GetEnv().audio.Orientation(dir, up);
 }
 
-void InteractiveState::PushPlayerUpdate(const Entity &player) {
+void InteractiveState::PushPlayerUpdate(const Entity &player, int dt) {
        std::uint16_t packet = master.GetClient().SendPlayerUpdate(player);
        if (player_hist.size() < 16) {
-               player_hist.emplace_back(player.GetState(), packet);
+               player_hist.emplace_back(player.GetState(), dt, packet);
        } else {
                auto entry = player_hist.begin();
                entry->state = player.GetState();
+               entry->delta_t = dt;
                entry->packet = packet;
                player_hist.splice(player_hist.end(), player_hist, entry);
        }
@@ -146,9 +146,16 @@ void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &co
        auto entry = player_hist.begin();
        auto end = player_hist.end();
 
+       // we may have received an older packet
+       int pack_diff = int16_t(seq) - int16_t(entry->packet);
+       if (pack_diff < 0) {
+               // indeed we have, just ignore it
+               return;
+       }
+
        // drop anything older than the fix
        while (entry != end) {
-               int pack_diff = int16_t(seq) - int16_t(entry->packet);
+               pack_diff = int16_t(seq) - int16_t(entry->packet);
                if (pack_diff > 0) {
                        entry = player_hist.erase(entry);
                } else {
@@ -167,7 +174,7 @@ void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &co
 
        while (entry != end) {
                replay_state.velocity = entry->state.velocity;
-               replay_state.Update(16);
+               replay_state.Update(entry->delta_t);
                entry->state.chunk_pos = replay_state.chunk_pos;
                entry->state.block_pos = replay_state.block_pos;
                ++entry;
@@ -180,7 +187,10 @@ void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &co
                return;
        }
 
-       constexpr float warp_thresh = 1.0f;
+       // if offset > 10cm, warp the player
+       // otherwise, move at most 1cm per frame towards
+       // the fixed position (160ms, so shouldn't be too noticeable)
+       constexpr float warp_thresh = 0.01f; // (1/10)^2
        constexpr float max_disp = 0.0001f; // (1/100)^2
 
        if (disp_squared > warp_thresh) {
index 7fb797bea1d89870034ffcb4ef23803f83b69def..f344efdcf883d6aa277e97ae2327ed8b34f4e089 100644 (file)
@@ -4,6 +4,7 @@
 #include "Connection.hpp"
 #include "ConnectionHandler.hpp"
 #include "../app/IntervalTimer.hpp"
+#include "../world/EntityState.hpp"
 
 #include <list>
 #include <SDL_net.h>
@@ -70,6 +71,7 @@ private:
        Entity *player;
        std::list<SpawnStatus> spawns;
        unsigned int confirm_wait;
+       EntityState player_update_state;
        std::uint16_t player_update_pack;
        IntervalTimer player_update_timer;
 
index 98954bf0f168635ac5810b61b70889b2f6cb471e..6bd561cb27018af0bd928d1672fb7e43121be343 100644 (file)
@@ -128,6 +128,7 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
 , player(nullptr)
 , spawns()
 , confirm_wait(0)
+, player_update_state()
 , player_update_pack(0)
 , player_update_timer(1500) {
        conn.SetHandler(this);
@@ -253,11 +254,20 @@ void ClientConnection::SendUpdate(SpawnStatus &status) {
 }
 
 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());
+       // player_update_state's position holds the client's most recent prediction
+       glm::vec3 diff = player_update_state.Diff(Player().GetState());
+       float dist_squared = dot(diff, diff);
+
+       // if client's prediction is off by more than 1cm, send
+       // our (authoritative) state back so it can fix it
+       constexpr float fix_thresh = 0.0001f;
+
+       if (dist_squared > fix_thresh) {
+               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) {
@@ -324,6 +334,7 @@ void ClientConnection::On(const Packet::Login &pack) {
                response.WriteWorldName(server.GetWorld().Name());
                conn.Send(server.GetPacket(), server.GetSocket());
                // set up update tracking
+               player_update_state = new_player->GetState();
                player_update_pack = pack.Seq();
                player_update_timer.Reset();
                player_update_timer.Start();
@@ -347,11 +358,10 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) {
        player_update_timer.Reset();
        if (pack_diff > 0 || overdue) {
                player_update_pack = pack.Seq();
-               // TODO: do client input validation here
-               EntityState new_state;
-               pack.ReadPlayerState(new_state);
-               Player().Velocity(new_state.velocity);
-               Player().Orientation(new_state.orient);
+               pack.ReadPlayerState(player_update_state);
+               // accept velocity and orientation as "user input"
+               Player().Velocity(player_update_state.velocity);
+               Player().Orientation(player_update_state.orient);
        }
 }