]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
basic message state
[blank.git] / src / client / client.cpp
index 524454b43e31a70310f9b0fa377c3a29ec3d8071..7f58dfaca0a5e23486bb37c7411b424a883da87d 100644 (file)
@@ -8,6 +8,7 @@
 #include "../app/TextureIndex.hpp"
 #include "../model/CompositeModel.hpp"
 #include "../io/WorldSave.hpp"
+#include "../world/ChunkIndex.hpp"
 #include "../world/ChunkStore.hpp"
 
 #include <iostream>
@@ -130,6 +131,9 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
        skeletons.Load();
        loop_timer.Start();
+       if (save.Exists(player)) {
+               save.Read(player);
+       }
 }
 
 void InteractiveState::OnEnter() {
@@ -157,7 +161,7 @@ void InteractiveState::Handle(const SDL_Event &event) {
                        interface.Handle(event.wheel);
                        break;
                case SDL_QUIT:
-                       master.Quit();
+                       Exit();
                        break;
                default:
                        break;
@@ -214,6 +218,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 +276,7 @@ void InteractiveState::SetDebug(bool b) {
 }
 
 void InteractiveState::Exit() {
+       save.Write(player);
        master.Quit();
 }
 
@@ -311,9 +337,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 +366,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 +399,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 +416,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 +470,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 +497,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);
+}
+
 }
 }