namespace blank {
class ChunkStore;
+class WorldSave;
namespace client {
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 &);
private:
ChunkStore &store;
+ const WorldSave &save;
std::list<ChunkTransmission> transmissions;
IntervalTimer timer;
+++ /dev/null
-#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
#include "../ui/ClientController.hpp"
#include "ChunkReceiver.hpp"
-#include "ChunkRequester.hpp"
#include "NetworkedInput.hpp"
#include "../app/IntervalTimer.hpp"
#include "../graphics/SkyBox.hpp"
InteractiveManipulator manip;
NetworkedInput input;
Interface interface;
- ChunkRequester chunk_requester;
ChunkReceiver chunk_receiver;
ChunkRenderer chunk_renderer;
Skeletons skeletons;
-#include "ChunkRequester.hpp"
#include "InitialState.hpp"
#include "InteractiveState.hpp"
#include "MasterState.hpp"
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() {
, 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)
loop_timer.Update(dt);
master.Update(dt);
chunk_receiver.Update(dt);
- chunk_requester.Update(dt);
hud.Update(dt);
int world_dt = 0;
#include "NetworkedInput.hpp"
#include "../app/init.hpp"
+#include "../io/WorldSave.hpp"
#include "../net/Packet.hpp"
#include "../world/Chunk.hpp"
#include "../world/ChunkStore.hpp"
namespace client {
-ChunkReceiver::ChunkReceiver(ChunkStore &store)
+ChunkReceiver::ChunkReceiver(ChunkStore &store, const WorldSave &save)
: store(store)
+, save(save)
, transmissions()
, timer(5000) {
timer.Start();
}
}
}
+ 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);
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);
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(),