]> git.localhorst.tv Git - blank.git/commitdiff
cache chunks received by the client
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 18 Sep 2015 10:45:24 +0000 (12:45 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 18 Sep 2015 10:45:24 +0000 (12:45 +0200)
cache is not synced with the server atm

src/client/ChunkRequester.hpp [new file with mode: 0644]
src/client/InteractiveState.hpp
src/client/client.cpp
src/world/ChunkLoader.hpp
src/world/chunk.cpp

diff --git a/src/client/ChunkRequester.hpp b/src/client/ChunkRequester.hpp
new file mode 100644 (file)
index 0000000..6d34f6c
--- /dev/null
@@ -0,0 +1,40 @@
+#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 39b393a578032e3e4aac22cb64153d8fcefd911f..77e02c35365d9e2484a7ad7f0f652aa680dd167a 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef BLANK_CLIENT_INTERACTIVESTATE_HPP_
 #define BLANK_CLIENT_INTERACTIVESTATE_HPP_
 
+#include "ChunkRequester.hpp"
 #include "../app/IntervalTimer.hpp"
 #include "../app/State.hpp"
 #include "../io/WorldSave.hpp"
@@ -49,6 +50,7 @@ private:
        WorldSave save;
        World world;
        Interface interface;
+       ChunkRequester chunk_requester;
        ChunkReceiver chunk_receiver;
        ChunkRenderer chunk_renderer;
        Skeletons skeletons;
index 4be9dbe88430cc8a77f677db8243af5520c6ee54..bb91847baa558be55b02922a407c356a96700062 100644 (file)
@@ -1,3 +1,4 @@
+#include "ChunkRequester.hpp"
 #include "InitialState.hpp"
 #include "InteractiveState.hpp"
 #include "MasterState.hpp"
@@ -6,6 +7,8 @@
 #include "../app/init.hpp"
 #include "../app/TextureIndex.hpp"
 #include "../model/CompositeModel.hpp"
+#include "../io/WorldSave.hpp"
+#include "../world/ChunkStore.hpp"
 
 #include <iostream>
 #include <glm/gtx/io.hpp>
@@ -16,6 +19,63 @@ 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() {
@@ -54,11 +114,16 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        world,
        world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
 )
+// TODO: looks like chunk requester and receiver can and should be merged
+, chunk_requester(world.Chunks(), save)
 , chunk_receiver(world.Chunks())
 , chunk_renderer(*interface.GetPlayer().chunks)
 , skeletons()
 , loop_timer(16)
 , player_hist() {
+       if (!save.Exists()) {
+               save.Write(master.GetWorldConf());
+       }
        TextureIndex tex_index;
        master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
        chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
@@ -105,6 +170,7 @@ void InteractiveState::Update(int dt) {
        loop_timer.Update(dt);
        master.Update(dt);
        chunk_receiver.Update(dt);
+       chunk_requester.Update(dt);
 
        interface.Update(dt);
        int world_dt = 0;
index 0198651f2ab307e137d6b7e220c291461b51dd54..1c0b98f792b349e69a9f7e997228038cdffcba5a 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef BLANK_WORLD_CHUNKLOADER_HPP_
 #define BLANK_WORLD_CHUNKLOADER_HPP_
 
-#include <list>
+#include <cstddef>
 
 
 namespace blank {
index 1d1a118a80030131b94e31534d2b5b7115591b80..6fb46794a474f4ae30ef202912cd1391d16e8644 100644 (file)
@@ -920,12 +920,14 @@ void ChunkIndex::Unset(int index) noexcept {
 }
 
 Chunk::Pos ChunkIndex::NextMissing() noexcept {
-       int roundtrip = last_missing;
-       while (chunks[last_missing]) {
-               ++last_missing;
-               last_missing %= total_length;
-               if (last_missing == roundtrip) {
-                       break;
+       if (MissingChunks() > 0) {
+               int roundtrip = last_missing;
+               last_missing = (last_missing + 1) % total_length;
+               while (chunks[last_missing]) {
+                       last_missing = (last_missing + 1) % total_length;
+                       if (last_missing == roundtrip) {
+                               break;
+                       }
                }
        }
        return PositionOf(last_missing);