]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
merge chunk requester into receiver
[blank.git] / src / client / client.cpp
index 91d6aed6992e7327fc23116fcea59c9f902c8c00..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)
@@ -126,11 +66,14 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        }
        TextureIndex tex_index;
        master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
-       interface.SetInventorySlots(block_types.Size() - 1);
+       interface.SetInventorySlots(block_types.size() - 1);
        chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
        chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
        skeletons.Load();
        loop_timer.Start();
+       if (save.Exists(player)) {
+               save.Read(player);
+       }
 }
 
 void InteractiveState::OnEnter() {
@@ -158,7 +101,7 @@ void InteractiveState::Handle(const SDL_Event &event) {
                        interface.Handle(event.wheel);
                        break;
                case SDL_QUIT:
-                       master.Quit();
+                       Exit();
                        break;
                default:
                        break;
@@ -178,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;
@@ -230,7 +172,7 @@ void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
                Block block;
                pack.ReadIndex(index, i);
                pack.ReadBlock(block, i);
-               if (index < Chunk::size && block.type < block_types.Size()) {
+               if (index < Chunk::size && block.type < block_types.size()) {
                        manip.SetBlock(*chunk, index, block);
                }
        }
@@ -273,6 +215,7 @@ void InteractiveState::SetDebug(bool b) {
 }
 
 void InteractiveState::Exit() {
+       save.Write(player);
        master.Quit();
 }
 
@@ -333,9 +276,8 @@ void MasterState::OnPacketLost(uint16_t id) {
 
 void MasterState::OnTimeout() {
        if (client.GetConnection().Closed()) {
-               // TODO: push disconnected message
-               cout << "connection timed out" << endl;
                Quit();
+               env.ShowMessage("connection timed out");
        }
 }
 
@@ -363,20 +305,19 @@ void MasterState::On(const Packet::Join &pack) {
 }
 
 void MasterState::On(const Packet::Part &pack) {
+       Quit();
        if (state) {
                // kicked
-               cout << "kicked by server" << endl;
+               env.ShowMessage("kicked by server");
        } else {
                // join refused
-               cout << "login refused by server" << endl;
+               env.ShowMessage("login refused by server");
        }
-       Quit();
 }
 
 void MasterState::On(const Packet::SpawnEntity &pack) {
        if (!state) {
                cout << "got entity spawn before world was created" << endl;
-               Quit();
                return;
        }
        uint32_t entity_id;
@@ -397,7 +338,6 @@ void MasterState::On(const Packet::SpawnEntity &pack) {
 void MasterState::On(const Packet::DespawnEntity &pack) {
        if (!state) {
                cout << "got entity despawn before world was created" << endl;
-               Quit();
                return;
        }
        uint32_t entity_id;
@@ -415,7 +355,6 @@ void MasterState::On(const Packet::DespawnEntity &pack) {
 void MasterState::On(const Packet::EntityUpdate &pack) {
        if (!state) {
                cout << "got entity update before world was created" << endl;
-               Quit();
                return;
        }
 
@@ -470,7 +409,6 @@ void MasterState::ClearEntity(uint32_t entity_id) {
 void MasterState::On(const Packet::PlayerCorrection &pack) {
        if (!state) {
                cout << "got player correction without a player :S" << endl;
-               Quit();
                return;
        }
        uint16_t pack_seq;