]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
sync entities with clients
[blank.git] / src / client / client.cpp
index 49ea4007d6a63524c392ad9953fc67812ef0dfb0..829cc6b1eae49697439bda9a13e697aab34f4a6e 100644 (file)
@@ -7,6 +7,9 @@
 #include "../app/TextureIndex.hpp"
 
 #include <iostream>
+#include <glm/gtx/io.hpp>
+
+using namespace std;
 
 
 namespace blank {
@@ -38,13 +41,19 @@ void InitialState::Render(Viewport &viewport) {
 }
 
 
-InteractiveState::InteractiveState(MasterState &master)
+// TODO: this clutter is a giant mess
+InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 : master(master)
 , block_types()
 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetClientConf().host))
 , world(block_types, master.GetWorldConf(), save)
 , chunk_renderer(world, master.GetWorldConf().load.load_dist)
-, interface(master.GetInterfaceConf(), master.GetEnv(), world) {
+, interface(
+       master.GetInterfaceConf(),
+       master.GetEnv(),
+       world,
+       *world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
+) {
        TextureIndex tex_index;
        master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
        chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
@@ -93,6 +102,8 @@ void InteractiveState::Update(int dt) {
        chunk_renderer.Rebase(interface.Player().ChunkCoords());
        chunk_renderer.Update(dt);
 
+       master.GetClient().SendPlayerUpdate(interface.Player());
+
        glm::mat4 trans = interface.Player().Transform(interface.Player().ChunkCoords());
        glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
        glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
@@ -126,6 +137,9 @@ MasterState::MasterState(
 }
 
 void MasterState::Quit() {
+       if (!client.GetConnection().Closed()) {
+               client.SendPart();
+       }
        env.state.PopUntil(this);
 }
 
@@ -152,7 +166,7 @@ void MasterState::Render(Viewport &) {
 }
 
 
-void MasterState::OnPacketLost(std::uint16_t id) {
+void MasterState::OnPacketLost(uint16_t id) {
        if (id == login_packet) {
                login_packet = client.SendLogin(intf_conf.player_name);
        }
@@ -160,8 +174,9 @@ void MasterState::OnPacketLost(std::uint16_t id) {
 
 void MasterState::OnTimeout() {
        if (client.GetConnection().Closed()) {
-               Quit();
                // TODO: push disconnected message
+               cout << "connection timed out" << endl;
+               Quit();
        }
 }
 
@@ -170,12 +185,15 @@ void MasterState::On(const Packet::Join &pack) {
 
        if (state) {
                // changing worlds
-               std::cout << "server changing worlds" << std::endl;
+               cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
        } else {
                // joining game
-               std::cout << "joined game" << std::endl;
+               cout << "joined game \"" << world_conf.name << '"' << endl;
        }
-       state.reset(new InteractiveState(*this));
+
+       uint32_t player_id;
+       pack.ReadPlayerID(player_id);
+       state.reset(new InteractiveState(*this, player_id));
 
        pack.ReadPlayer(state->GetInterface().Player());
 
@@ -186,13 +204,78 @@ void MasterState::On(const Packet::Join &pack) {
 void MasterState::On(const Packet::Part &pack) {
        if (state) {
                // kicked
-               std::cout << "kicked by server" << std::endl;
+               cout << "kicked by server" << endl;
        } else {
                // join refused
-               std::cout << "login refused by server" << std::endl;
+               cout << "login refused by server" << endl;
        }
        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;
+       pack.ReadEntityID(entity_id);
+       Entity *entity = state->GetWorld().AddEntity(entity_id);
+       if (!entity) {
+               cout << "entity ID inconsistency" << endl;
+               Quit();
+               return;
+       }
+       pack.ReadEntity(*entity);
+       cout << "spawned entity " << entity->Name() << " at " << entity->AbsolutePosition() << endl;
+}
+
+void MasterState::On(const Packet::DespawnEntity &pack) {
+       if (!state) {
+               cout << "got entity despawn before world was created" << endl;
+               Quit();
+               return;
+       }
+       uint32_t entity_id;
+       pack.ReadEntityID(entity_id);
+       for (Entity &entity : state->GetWorld().Entities()) {
+               if (entity.ID() == entity_id) {
+                       entity.Kill();
+                       cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+                       return;
+               }
+       }
+}
+
+void MasterState::On(const Packet::EntityUpdate &pack) {
+       if (!state) {
+               cout << "got entity update before world was created" << endl;
+               Quit();
+               return;
+       }
+
+       auto world_iter = state->GetWorld().Entities().begin();
+       auto world_end = state->GetWorld().Entities().end();
+
+       uint32_t count = 0;
+       pack.ReadEntityCount(count);
+
+       for (uint32_t i = 0; i < count; ++i) {
+               uint32_t entity_id = 0;
+               pack.ReadEntityID(entity_id, i);
+
+               while (world_iter != world_end && world_iter->ID() < entity_id) {
+                       ++world_iter;
+               }
+               if (world_iter == world_end) {
+                       // nothing can be done from here
+                       return;
+               }
+               if (world_iter->ID() == entity_id) {
+                       pack.ReadEntity(*world_iter, i);
+               }
+       }
+}
+
 }
 }