]> git.localhorst.tv Git - blank.git/commitdiff
group entity updates in as few packets as possible
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 18 Sep 2015 14:41:53 +0000 (16:41 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 18 Sep 2015 14:41:53 +0000 (16:41 +0200)
src/client/client.cpp
src/server/ClientConnection.hpp
src/server/net.cpp

index 52bc0a9eea97ab729e7e2e62612b6ab8142fba2a..7dac3ad0991a2438350a0f15c755370ab42da786 100644 (file)
@@ -396,7 +396,8 @@ void MasterState::On(const Packet::SpawnEntity &pack) {
        if (skel) {
                skel->Instantiate(entity.GetModel());
        }
        if (skel) {
                skel->Instantiate(entity.GetModel());
        }
-       cout << "spawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+       cout << "spawned entity #" << entity_id << "  (" << entity.Name()
+               << ") at " << entity.AbsolutePosition() << endl;
 }
 
 void MasterState::On(const Packet::DespawnEntity &pack) {
 }
 
 void MasterState::On(const Packet::DespawnEntity &pack) {
@@ -411,7 +412,7 @@ void MasterState::On(const Packet::DespawnEntity &pack) {
        for (Entity &entity : state->GetWorld().Entities()) {
                if (entity.ID() == entity_id) {
                        entity.Kill();
        for (Entity &entity : state->GetWorld().Entities()) {
                if (entity.ID() == entity_id) {
                        entity.Kill();
-                       cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+                       cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
                        return;
                }
        }
                        return;
                }
        }
index c5efb51bbd412d7d9aeffbe7e5b29803b83f4a2d..2f03e67eca5bea2315b3b24602fa4f825e220ff7 100644 (file)
@@ -12,6 +12,7 @@
 #include <deque>
 #include <list>
 #include <SDL_net.h>
 #include <deque>
 #include <list>
 #include <SDL_net.h>
+#include <vector>
 
 
 namespace blank {
 
 
 namespace blank {
@@ -77,7 +78,8 @@ private:
 
        void SendSpawn(SpawnStatus &);
        void SendDespawn(SpawnStatus &);
 
        void SendSpawn(SpawnStatus &);
        void SendDespawn(SpawnStatus &);
-       void SendUpdate(SpawnStatus &);
+       void QueueUpdate(SpawnStatus &);
+       void SendUpdates();
 
        void CheckPlayerFix();
 
 
        void CheckPlayerFix();
 
@@ -90,6 +92,8 @@ private:
        std::list<SpawnStatus> spawns;
        unsigned int confirm_wait;
 
        std::list<SpawnStatus> spawns;
        unsigned int confirm_wait;
 
+       std::vector<SpawnStatus *> entity_updates;
+
        EntityState player_update_state;
        std::uint16_t player_update_pack;
        IntervalTimer player_update_timer;
        EntityState player_update_state;
        std::uint16_t player_update_pack;
        IntervalTimer player_update_timer;
index 0cf0cdd60cc12ad6b9ab36d1a50745cf85c0999f..abe00a99fb72600449f9cecde40bbcfd7b22834f 100644 (file)
@@ -174,6 +174,7 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
 , player(nullptr, nullptr)
 , spawns()
 , confirm_wait(0)
 , player(nullptr, nullptr)
 , spawns()
 , confirm_wait(0)
+, entity_updates()
 , player_update_state()
 , player_update_pack(0)
 , player_update_timer(1500)
 , player_update_state()
 , player_update_pack(0)
 , player_update_timer(1500)
@@ -206,7 +207,7 @@ void ClientConnection::Update(int dt) {
                                        SendDespawn(*local_iter);
                                } else {
                                        // update
                                        SendDespawn(*local_iter);
                                } else {
                                        // update
-                                       SendUpdate(*local_iter);
+                                       QueueUpdate(*local_iter);
                                }
                                ++global_iter;
                                ++local_iter;
                                }
                                ++global_iter;
                                ++local_iter;
@@ -238,6 +239,7 @@ void ClientConnection::Update(int dt) {
                        SendDespawn(*local_iter);
                        ++local_iter;
                }
                        SendDespawn(*local_iter);
                        ++local_iter;
                }
+               SendUpdates();
 
                CheckPlayerFix();
                CheckChunkQueue();
 
                CheckPlayerFix();
                CheckChunkQueue();
@@ -300,15 +302,31 @@ void ClientConnection::SendDespawn(SpawnStatus &status) {
        ++confirm_wait;
 }
 
        ++confirm_wait;
 }
 
-void ClientConnection::SendUpdate(SpawnStatus &status) {
+void ClientConnection::QueueUpdate(SpawnStatus &status) {
        // don't send updates while spawn not ack'd or despawn sent
        // don't send updates while spawn not ack'd or despawn sent
-       if (status.spawn_pack != -1 || status.despawn_pack != -1) return;
+       if (status.spawn_pack == -1 && status.despawn_pack == -1) {
+               entity_updates.push_back(&status);
+       }
+}
 
 
-       // TODO: pack entity updates
+void ClientConnection::SendUpdates() {
        auto pack = Prepare<Packet::EntityUpdate>();
        auto pack = Prepare<Packet::EntityUpdate>();
-       pack.WriteEntityCount(1);
-       pack.WriteEntity(*status.entity, 0);
-       Send(Packet::EntityUpdate::GetSize(1));
+       int entity_pos = 0;
+       for (SpawnStatus *status : entity_updates) {
+               pack.WriteEntity(*status->entity, entity_pos);
+               ++entity_pos;
+               if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) {
+                       pack.WriteEntityCount(entity_pos);
+                       Send(Packet::EntityUpdate::GetSize(entity_pos));
+                       pack = Prepare<Packet::EntityUpdate>();
+                       entity_pos = 0;
+               }
+       }
+       if (entity_pos > 0) {
+               pack.WriteEntityCount(entity_pos);
+               Send(Packet::EntityUpdate::GetSize(entity_pos));
+       }
+       entity_updates.clear();
 }
 
 void ClientConnection::CheckPlayerFix() {
 }
 
 void ClientConnection::CheckPlayerFix() {