]> git.localhorst.tv Git - blank.git/commitdiff
merge chunk requester into receiver
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 9 Oct 2015 14:14:34 +0000 (16:14 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 9 Oct 2015 14:14:34 +0000 (16:14 +0200)
it won't request anything either since the server decides
which chunks the client gets

src/client/ChunkReceiver.hpp
src/client/ChunkRequester.hpp [deleted file]
src/client/InteractiveState.hpp
src/client/client.cpp
src/client/net.cpp

index 74a03a74318f4fd9b75defb779c1a7d5a5da3fb5..d304a85384f739e60fe05a55ad7b881e858f8683 100644 (file)
@@ -11,6 +11,7 @@
 namespace blank {
 
 class ChunkStore;
+class WorldSave;
 
 namespace client {
 
@@ -19,11 +20,18 @@ class ChunkTransmission;
 class ChunkReceiver {
 
 public:
-       explicit ChunkReceiver(ChunkStore &);
+       ChunkReceiver(ChunkStore &, const WorldSave &);
        ~ChunkReceiver();
 
        void Update(int dt);
 
+       int ToLoad() const noexcept;
+
+       void LoadOne();
+       void LoadN(std::size_t n);
+
+       void StoreN(std::size_t n);
+
        void Handle(const Packet::ChunkBegin &);
        void Handle(const Packet::ChunkData &);
 
@@ -33,6 +41,7 @@ private:
 
 private:
        ChunkStore &store;
+       const WorldSave &save;
        std::list<ChunkTransmission> transmissions;
        IntervalTimer timer;
 
diff --git a/src/client/ChunkRequester.hpp b/src/client/ChunkRequester.hpp
deleted file mode 100644 (file)
index 6d34f6c..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-#ifndef BLANK_CLIENT_CHUNKREQUESTER_HPP_
-#define BLANK_CLIENT_CHUNKREQUESTER_HPP_
-
-#include <cstddef>
-
-
-namespace blank {
-
-class ChunkStore;
-class WorldSave;
-
-namespace client {
-
-class ChunkRequester {
-
-public:
-       ChunkRequester(
-               ChunkStore &,
-               const WorldSave &
-       ) noexcept;
-
-       const WorldSave &SaveFile() const noexcept { return save; }
-
-       void Update(int dt);
-
-       int ToLoad() const noexcept;
-
-       void LoadOne();
-       void LoadN(std::size_t n);
-
-private:
-       ChunkStore &store;
-       const WorldSave &save;
-
-};
-
-}
-}
-
-#endif
index 2db227067711aecb78223231658514eebbb7bf89..8e7064eacf0a1e2be1f98bf309abe463f26d7043 100644 (file)
@@ -5,7 +5,6 @@
 #include "../ui/ClientController.hpp"
 
 #include "ChunkReceiver.hpp"
-#include "ChunkRequester.hpp"
 #include "NetworkedInput.hpp"
 #include "../app/IntervalTimer.hpp"
 #include "../graphics/SkyBox.hpp"
@@ -67,7 +66,6 @@ private:
        InteractiveManipulator manip;
        NetworkedInput input;
        Interface interface;
-       ChunkRequester chunk_requester;
        ChunkReceiver chunk_receiver;
        ChunkRenderer chunk_renderer;
        Skeletons skeletons;
index ada697b6f90668747d9ca29bc070f815dcde1d50..1978f3929c5b623ef987d224fe3ed2e9e5ffcaa9 100644 (file)
@@ -1,4 +1,3 @@
-#include "ChunkRequester.hpp"
 #include "InitialState.hpp"
 #include "InteractiveState.hpp"
 #include "MasterState.hpp"
@@ -20,63 +19,6 @@ using namespace std;
 namespace blank {
 namespace client {
 
-ChunkRequester::ChunkRequester(
-       ChunkStore &store,
-       const WorldSave &save
-) noexcept
-: store(store)
-, save(save) {
-
-}
-
-void ChunkRequester::Update(int dt) {
-       // check if there's chunks waiting to be loaded
-       LoadN(10);
-
-       // store a few chunks as well
-       constexpr int max_save = 10;
-       int saved = 0;
-       for (Chunk &chunk : store) {
-               if (chunk.ShouldUpdateSave()) {
-                       save.Write(chunk);
-                       ++saved;
-                       if (saved >= max_save) {
-                               break;
-                       }
-               }
-       }
-}
-
-int ChunkRequester::ToLoad() const noexcept {
-       return store.EstimateMissing();
-}
-
-void ChunkRequester::LoadOne() {
-       if (!store.HasMissing()) return;
-
-       Chunk::Pos pos = store.NextMissing();
-       Chunk *chunk = store.Allocate(pos);
-       if (!chunk) {
-               // chunk store corrupted?
-               return;
-       }
-
-       if (save.Exists(pos)) {
-               save.Read(*chunk);
-               // TODO: request chunk from server with cache tag
-       } else {
-               // TODO: request chunk from server
-       }
-}
-
-void ChunkRequester::LoadN(std::size_t n) {
-       std::size_t end = std::min(n, std::size_t(ToLoad()));
-       for (std::size_t i = 0; i < end && store.HasMissing(); ++i) {
-               LoadOne();
-       }
-}
-
-
 InitialState::InitialState(MasterState &master)
 : master(master)
 , message() {
@@ -114,9 +56,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , manip(master.GetEnv(), player.GetEntity())
 , 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)
-, chunk_receiver(world.Chunks())
+, chunk_receiver(world.Chunks(), save)
 , chunk_renderer(player.GetChunks())
 , skeletons()
 , loop_timer(16)
@@ -181,7 +121,6 @@ void InteractiveState::Update(int dt) {
        loop_timer.Update(dt);
        master.Update(dt);
        chunk_receiver.Update(dt);
-       chunk_requester.Update(dt);
 
        hud.Update(dt);
        int world_dt = 0;
index 7d8681aecd15f1a4b324bfb2614423a4f3b13808..5c06bbe6a0c115f8952fd19c3315a3b7da8cfb59 100644 (file)
@@ -4,6 +4,7 @@
 #include "NetworkedInput.hpp"
 
 #include "../app/init.hpp"
+#include "../io/WorldSave.hpp"
 #include "../net/Packet.hpp"
 #include "../world/Chunk.hpp"
 #include "../world/ChunkStore.hpp"
@@ -21,8 +22,9 @@ namespace blank {
 namespace client {
 
 
-ChunkReceiver::ChunkReceiver(ChunkStore &store)
+ChunkReceiver::ChunkReceiver(ChunkStore &store, const WorldSave &save)
 : store(store)
+, save(save)
 , transmissions()
 , timer(5000) {
        timer.Start();
@@ -48,8 +50,50 @@ void ChunkReceiver::Update(int dt) {
                        }
                }
        }
+       LoadN(10);
+       StoreN(10);
 }
 
+int ChunkReceiver::ToLoad() const noexcept {
+       return store.EstimateMissing();
+}
+
+void ChunkReceiver::LoadOne() {
+       if (!store.HasMissing()) return;
+
+       Chunk::Pos pos = store.NextMissing();
+       Chunk *chunk = store.Allocate(pos);
+       if (!chunk) {
+               // chunk store corrupted?
+               return;
+       }
+
+       if (save.Exists(pos)) {
+               save.Read(*chunk);
+       }
+}
+
+void ChunkReceiver::LoadN(size_t n) {
+       size_t end = min(n, size_t(ToLoad()));
+       for (size_t i = 0; i < end && store.HasMissing(); ++i) {
+               LoadOne();
+       }
+}
+
+void ChunkReceiver::StoreN(size_t n) {
+       size_t saved = 0;
+       for (Chunk &chunk : store) {
+               if (chunk.ShouldUpdateSave()) {
+                       save.Write(chunk);
+                       ++saved;
+                       if (saved >= n) {
+                               break;
+                       }
+               }
+       }
+}
+
+
 void ChunkReceiver::Handle(const Packet::ChunkBegin &pack) {
        uint32_t id;
        pack.ReadTransmissionId(id);
@@ -244,8 +288,8 @@ uint16_t Client::SendPlayerUpdate(
        const glm::vec3 &movement,
        float pitch,
        float yaw,
-       std::uint8_t actions,
-       std::uint8_t slot
+       uint8_t actions,
+       uint8_t slot
 ) {
        auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
        pack.WritePredictedState(prediction);
@@ -279,7 +323,7 @@ void NetworkedInput::Update(int dt) {
 void NetworkedInput::PushPlayerUpdate(int dt) {
        const EntityState &state = GetPlayer().GetEntity().GetState();
 
-       std::uint16_t packet = client.SendPlayerUpdate(
+       uint16_t packet = client.SendPlayerUpdate(
                state,
                GetMovement(),
                GetPitch(),