]> git.localhorst.tv Git - blank.git/commitdiff
some experiements with state sync
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 15 Sep 2015 15:47:15 +0000 (17:47 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 15 Sep 2015 15:47:15 +0000 (17:47 +0200)
src/app/IntervalTimer.hpp
src/app/ServerState.cpp
src/app/ServerState.hpp
src/client/InteractiveState.hpp
src/client/client.cpp
src/net/net.cpp

index 06caa5e21b9bb5c805a1f2e73d68d89c855f8996..5240f5d3f60a419e8593b5e883dab9fd5c8e8d1c 100644 (file)
@@ -40,9 +40,15 @@ public:
        int Elapsed() const noexcept {
                return value;
        }
+       int Interval() const noexcept {
+               return intv;
+       }
        int Iteration() const noexcept {
                return value / intv;
        }
+       void PopIteration() noexcept {
+               value -= intv;
+       }
 
        void Update(int dt) noexcept {
                value += dt * speed;
index 0be565307308ce330b9a38d793b2be1a5496dc3b..d980cbcc697ce4dda8a4c393bdefb88a0a6398e8 100644 (file)
@@ -24,11 +24,13 @@ ServerState::ServerState(
 , skeletons()
 , spawner(world, skeletons, gc.seed)
 , server(sc, world)
+, loop_timer(16)
 , push_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;
@@ -43,10 +45,14 @@ void ServerState::Handle(const SDL_Event &event) {
 
 
 void ServerState::Update(int dt) {
+       loop_timer.Update(dt);
        push_timer.Update(dt);
        server.Handle();
-       spawner.Update(dt);
-       world.Update(dt);
+       while (loop_timer.HitOnce()) {
+               spawner.Update(loop_timer.Interval());
+               world.Update(loop_timer.Interval());
+               loop_timer.PopIteration();
+       }
        chunk_loader.Update(dt);
        if (push_timer.Hit()) {
                server.Update(dt);
index 0deb65aff460ed5158fa28064a3ef2756300ca00..afa7840bbfc8e040993ca47653d85dc6e311f4fb 100644 (file)
@@ -42,6 +42,7 @@ private:
        Skeletons skeletons;
        Spawner spawner;
        Server server;
+       IntervalTimer loop_timer;
        IntervalTimer push_timer;
 
 };
index 91a5944181b2979b075ddce9b80af3d2657a5a48..eaca4c9c78880d8679755c24d71f2558f7ea1977 100644 (file)
@@ -49,14 +49,14 @@ private:
        Interface interface;
        ChunkRenderer chunk_renderer;
        Skeletons skeletons;
+       IntervalTimer loop_timer;
        IntervalTimer update_timer;
 
        struct PlayerHistory {
                EntityState state;
-               int timestamp;
                std::uint16_t packet;
-               PlayerHistory(EntityState s, int t, std::uint16_t p)
-               : state(s), timestamp(t), packet(p) { }
+               PlayerHistory(EntityState s, std::uint16_t p)
+               : state(s), packet(p) { }
        };
        std::list<PlayerHistory> player_hist;
 
index 4a48b45afc91eb5179f9a6d038c88b19244f213d..dbdaa73641c5f52c58949d82d2f7b09bb0038bed 100644 (file)
@@ -56,6 +56,7 @@ 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;
@@ -65,6 +66,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        skeletons.Load();
        // TODO: better solution for initializing HUD
        interface.SelectNext();
+       loop_timer.Start();
        update_timer.Start();
 }
 
@@ -101,14 +103,17 @@ 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);
-       world.Update(dt);
+       while (loop_timer.HitOnce()) {
+               world.Update(loop_timer.Interval());
+               loop_timer.PopIteration();
+       }
        chunk_renderer.Update(dt);
 
-       update_timer.Update(dt);
-
        Entity &player = *interface.GetPlayer().entity;
 
        if (update_timer.Hit()) {
@@ -126,11 +131,10 @@ void InteractiveState::Update(int dt) {
 void InteractiveState::PushPlayerUpdate(const Entity &player) {
        std::uint16_t packet = master.GetClient().SendPlayerUpdate(player);
        if (player_hist.size() < 16) {
-               player_hist.emplace_back(player.GetState(), update_timer.Elapsed(), packet);
+               player_hist.emplace_back(player.GetState(), packet);
        } else {
                auto entry = player_hist.begin();
                entry->state = player.GetState();
-               entry->timestamp = update_timer.Elapsed();
                entry->packet = packet;
                player_hist.splice(player_hist.end(), player_hist, entry);
        }
@@ -145,13 +149,49 @@ void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &co
        // drop anything older than the fix
        while (entry != end) {
                int pack_diff = int16_t(seq) - int16_t(entry->packet);
-               if (pack_diff < 0) {
+               if (pack_diff > 0) {
                        entry = player_hist.erase(entry);
                } else {
                        break;
                }
        }
-       if (entry == end) return;
+
+       EntityState replay_state(corrected_state);
+       EntityState &player_state = interface.GetPlayer().entity->GetState();
+
+       if (entry != end) {
+               entry->state.chunk_pos = replay_state.chunk_pos;
+               entry->state.block_pos = replay_state.block_pos;
+               ++entry;
+       }
+
+       while (entry != end) {
+               replay_state.velocity = entry->state.velocity;
+               replay_state.Update(16);
+               entry->state.chunk_pos = replay_state.chunk_pos;
+               entry->state.block_pos = replay_state.block_pos;
+               ++entry;
+       }
+
+       glm::vec3 displacement(replay_state.Diff(player_state));
+       const float disp_squared = dot(displacement, displacement);
+
+       if (disp_squared < 16.0f * numeric_limits<float>::epsilon()) {
+               return;
+       }
+
+       constexpr float warp_thresh = 1.0f;
+       constexpr float max_disp = 0.0001f; // (1/100)^2
+
+       if (disp_squared > warp_thresh) {
+               player_state.chunk_pos = replay_state.chunk_pos;
+               player_state.block_pos = replay_state.block_pos;
+       } else if (disp_squared < max_disp) {
+               player_state.block_pos += displacement;
+       } else {
+               displacement *= 0.01f / sqrt(disp_squared);
+               player_state.block_pos += displacement;
+       }
 }
 
 void InteractiveState::Render(Viewport &viewport) {
index 3f4bf7453eab78a0ac79b7f045026ec10114a8f0..98954bf0f168635ac5810b61b70889b2f6cb471e 100644 (file)
@@ -348,7 +348,10 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) {
        if (pack_diff > 0 || overdue) {
                player_update_pack = pack.Seq();
                // TODO: do client input validation here
-               pack.ReadPlayerState(Player().GetState());
+               EntityState new_state;
+               pack.ReadPlayerState(new_state);
+               Player().Velocity(new_state.velocity);
+               Player().Orientation(new_state.orient);
        }
 }