From d5ef462f3bd70a9717c1309466ce8d90b038c5cd Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 9 Oct 2015 16:14:34 +0200 Subject: [PATCH 1/1] merge chunk requester into receiver it won't request anything either since the server decides which chunks the client gets --- src/client/ChunkReceiver.hpp | 11 +++++- src/client/ChunkRequester.hpp | 40 --------------------- src/client/InteractiveState.hpp | 2 -- src/client/client.cpp | 63 +-------------------------------- src/client/net.cpp | 52 ++++++++++++++++++++++++--- 5 files changed, 59 insertions(+), 109 deletions(-) delete mode 100644 src/client/ChunkRequester.hpp diff --git a/src/client/ChunkReceiver.hpp b/src/client/ChunkReceiver.hpp index 74a03a7..d304a85 100644 --- a/src/client/ChunkReceiver.hpp +++ b/src/client/ChunkReceiver.hpp @@ -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 transmissions; IntervalTimer timer; diff --git a/src/client/ChunkRequester.hpp b/src/client/ChunkRequester.hpp deleted file mode 100644 index 6d34f6c..0000000 --- a/src/client/ChunkRequester.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#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 2db2270..8e7064e 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -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; diff --git a/src/client/client.cpp b/src/client/client.cpp index ada697b..1978f39 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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; diff --git a/src/client/net.cpp b/src/client/net.cpp index 7d8681a..5c06bbe 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -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(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(), -- 2.39.2