]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
exchange block updates with clients
[blank.git] / src / client / client.cpp
index 524454b43e31a70310f9b0fa377c3a29ec3d8071..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>
@@ -214,6 +215,27 @@ void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntitySta
        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) {
@@ -476,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);
+}
+
 }
 }