From: Daniel Karbach Date: Fri, 18 Sep 2015 10:45:24 +0000 (+0200) Subject: cache chunks received by the client X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=7fd76e64de47f564117b9e6f73f1482d93842108;p=blank.git cache chunks received by the client cache is not synced with the server atm --- diff --git a/src/client/ChunkRequester.hpp b/src/client/ChunkRequester.hpp new file mode 100644 index 0000000..6d34f6c --- /dev/null +++ b/src/client/ChunkRequester.hpp @@ -0,0 +1,40 @@ +#ifndef BLANK_CLIENT_CHUNKREQUESTER_HPP_ +#define BLANK_CLIENT_CHUNKREQUESTER_HPP_ + +#include + + +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 diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index 39b393a..77e02c3 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -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; diff --git a/src/client/client.cpp b/src/client/client.cpp index 4be9dbe..bb91847 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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 #include @@ -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; diff --git a/src/world/ChunkLoader.hpp b/src/world/ChunkLoader.hpp index 0198651..1c0b98f 100644 --- a/src/world/ChunkLoader.hpp +++ b/src/world/ChunkLoader.hpp @@ -1,7 +1,7 @@ #ifndef BLANK_WORLD_CHUNKLOADER_HPP_ #define BLANK_WORLD_CHUNKLOADER_HPP_ -#include +#include namespace blank { diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index 1d1a118..6fb4679 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -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);