]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
exchange block updates with clients
[blank.git] / src / client / client.cpp
index cfc89cd2588dabca879a6dec218d40582dac5f48..91d6aed6992e7327fc23116fcea59c9f902c8c00 100644 (file)
@@ -8,6 +8,7 @@
 #include "../app/TextureIndex.hpp"
 #include "../model/CompositeModel.hpp"
 #include "../io/WorldSave.hpp"
+#include "../world/ChunkIndex.hpp"
 #include "../world/ChunkStore.hpp"
 
 #include <iostream>
@@ -111,7 +112,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , player(*world.AddPlayer(master.GetConfig().player.name))
 , hud(master.GetEnv(), master.GetConfig(), player)
 , manip(master.GetEnv(), player.GetEntity())
-, input(world, player, manip)
+, input(world, player, master.GetClient())
 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
 // TODO: looks like chunk requester and receiver can and should be merged
 , chunk_requester(world.Chunks(), save)
@@ -119,8 +120,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , chunk_renderer(player.GetChunks())
 , skeletons()
 , loop_timer(16)
-, sky(master.GetEnv().loader.LoadCubeMap("skybox"))
-, player_hist() {
+, sky(master.GetEnv().loader.LoadCubeMap("skybox")) {
        if (!save.Exists()) {
                save.Write(master.GetWorldConf());
        }
@@ -190,7 +190,7 @@ void InteractiveState::Update(int dt) {
        chunk_renderer.Update(dt);
 
        if (world_dt > 0) {
-               PushPlayerUpdate(player.GetEntity(), world_dt);
+               input.PushPlayerUpdate(world_dt);
        }
 
        glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
@@ -201,83 +201,6 @@ void InteractiveState::Update(int dt) {
        master.GetEnv().audio.Orientation(dir, up);
 }
 
-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(), 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);
-       }
-}
-
-void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &corrected_state) {
-       if (player_hist.empty()) return;
-
-       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) {
-               pack_diff = int16_t(seq) - int16_t(entry->packet);
-               if (pack_diff > 0) {
-                       entry = player_hist.erase(entry);
-               } else {
-                       break;
-               }
-       }
-
-       EntityState replay_state(corrected_state);
-       EntityState &player_state = player.GetEntity().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(entry->delta_t);
-               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;
-       }
-
-       // 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) {
-               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) {
        viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
        if (master.GetConfig().video.world) {
@@ -288,6 +211,31 @@ void InteractiveState::Render(Viewport &viewport) {
        hud.Render(viewport);
 }
 
+void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntityState &state) {
+       input.MergePlayerCorrection(pack, state);
+}
+
+void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
+       glm::ivec3 pos;
+       pack.ReadChunkCoords(pos);
+       Chunk *chunk = player.GetChunks().Get(pos);
+       if (!chunk) {
+               // this change doesn't concern us
+               return;
+       }
+       uint32_t count = 0;
+       pack.ReadBlockCount(count);
+       for (uint32_t i = 0; i < count; ++i) {
+               uint16_t index;
+               Block block;
+               pack.ReadIndex(index, i);
+               pack.ReadBlock(block, i);
+               if (index < Chunk::size && block.type < block_types.Size()) {
+                       manip.SetBlock(*chunk, index, block);
+               }
+       }
+}
+
 void InteractiveState::SetAudio(bool b) {
        master.GetConfig().audio.enabled = b;
        if (b) {
@@ -550,5 +498,13 @@ void MasterState::On(const Packet::ChunkData &pack) {
        state->GetChunkReceiver().Handle(pack);
 }
 
+void MasterState::On(const Packet::BlockUpdate &pack) {
+       if (!state) {
+               cout << "received block update, but the world has not been created yet" << endl;
+               return;
+       }
+       state->Handle(pack);
+}
+
 }
 }