X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=1978f3929c5b623ef987d224fe3ed2e9e5ffcaa9;hb=1c2994622a6b73f90cbd3ec9c09ffb4d7724cab4;hp=524454b43e31a70310f9b0fa377c3a29ec3d8071;hpb=c1da86ebab41895bf49ed747c75ecf722e8c5586;p=blank.git diff --git a/src/client/client.cpp b/src/client/client.cpp index 524454b..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" @@ -8,6 +7,7 @@ #include "../app/TextureIndex.hpp" #include "../model/CompositeModel.hpp" #include "../io/WorldSave.hpp" +#include "../world/ChunkIndex.hpp" #include "../world/ChunkStore.hpp" #include @@ -19,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() { @@ -113,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) @@ -125,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() { @@ -157,7 +101,7 @@ void InteractiveState::Handle(const SDL_Event &event) { interface.Handle(event.wheel); break; case SDL_QUIT: - master.Quit(); + Exit(); break; default: break; @@ -177,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; @@ -214,6 +157,27 @@ void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntitySta input.MergePlayerCorrection(pack, state); } +void InteractiveState::Handle(const Packet::BlockUpdate &pack) { + glm::ivec3 pos; + pack.ReadChunkCoords(pos); + Chunk *chunk = player.GetChunks().Get(pos); + if (!chunk) { + // this change doesn't concern us + return; + } + uint32_t count = 0; + pack.ReadBlockCount(count); + for (uint32_t i = 0; i < count; ++i) { + uint16_t index; + Block block; + pack.ReadIndex(index, i); + pack.ReadBlock(block, i); + if (index < Chunk::size && block.type < block_types.size()) { + manip.SetBlock(*chunk, index, block); + } + } +} + void InteractiveState::SetAudio(bool b) { master.GetConfig().audio.enabled = b; if (b) { @@ -251,6 +215,7 @@ void InteractiveState::SetDebug(bool b) { } void InteractiveState::Exit() { + save.Write(player); master.Quit(); } @@ -311,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"); } } @@ -341,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; @@ -375,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; @@ -393,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; } @@ -448,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; @@ -476,5 +436,13 @@ void MasterState::On(const Packet::ChunkData &pack) { state->GetChunkReceiver().Handle(pack); } +void MasterState::On(const Packet::BlockUpdate &pack) { + if (!state) { + cout << "received block update, but the world has not been created yet" << endl; + return; + } + state->Handle(pack); +} + } }