]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
linear model instance state
[blank.git] / src / client / client.cpp
index e2702245a228fa1ba0d4e39c3e66c2d252dbc92b..6a2dd32993b1fb45031ef9485428ce256ae0f4a7 100644 (file)
@@ -1,4 +1,3 @@
-#include "ChunkRequester.hpp"
 #include "InitialState.hpp"
 #include "InteractiveState.hpp"
 #include "MasterState.hpp"
@@ -6,7 +5,7 @@
 #include "../app/Environment.hpp"
 #include "../app/init.hpp"
 #include "../app/TextureIndex.hpp"
-#include "../model/CompositeModel.hpp"
+#include "../model/Model.hpp"
 #include "../io/WorldSave.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/ChunkStore.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() {
@@ -106,6 +48,7 @@ void InitialState::Render(Viewport &viewport) {
 // TODO: this clutter is a giant mess
 InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 : master(master)
+, shapes()
 , block_types()
 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
 , world(block_types, master.GetWorldConf())
@@ -114,9 +57,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)
@@ -125,11 +66,12 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
                save.Write(master.GetWorldConf());
        }
        TextureIndex tex_index;
-       master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
-       interface.SetInventorySlots(block_types.Size() - 1);
+       master.GetEnv().loader.LoadShapes("default", shapes);
+       master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index, shapes);
+       skeletons.Load(shapes, tex_index);
+       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);
@@ -181,7 +123,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;
@@ -233,7 +174,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);
                }
        }
@@ -337,9 +278,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");
        }
 }
 
@@ -367,20 +307,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;
@@ -390,7 +329,7 @@ void MasterState::On(const Packet::SpawnEntity &pack) {
        pack.ReadEntity(entity);
        uint32_t skel_id;
        pack.ReadSkeletonID(skel_id);
-       CompositeModel *skel = state->GetSkeletons().ByID(skel_id);
+       Model *skel = state->GetSkeletons().ByID(skel_id);
        if (skel) {
                skel->Instantiate(entity.GetModel());
        }
@@ -401,7 +340,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;
@@ -419,7 +357,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;
        }
 
@@ -474,7 +411,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;