]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
don't send chunks and entities simultaneously while skipping
[blank.git] / src / server / net.cpp
index 1e60ff6ead06d3dfd7905946866d4d4920ead0fd..aa1bdeb89448c46c757f6a4f47eb283c730ce581 100644 (file)
@@ -4,11 +4,12 @@
 
 #include "../app/init.hpp"
 #include "../io/WorldSave.hpp"
-#include "../model/CompositeModel.hpp"
+#include "../model/Model.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/Entity.hpp"
 #include "../world/World.hpp"
 
+#include <algorithm>
 #include <iostream>
 #include <zlib.h>
 #include <glm/gtx/io.hpp>
@@ -179,6 +180,7 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
 , spawns()
 , confirm_wait(0)
 , entity_updates()
+, entity_updates_skipped(0)
 , player_update_state()
 , player_update_pack(0)
 , player_update_timer(1500)
@@ -210,7 +212,7 @@ void ClientConnection::Update(int dt) {
                                // they're the same
                                if (CanDespawn(*global_iter)) {
                                        SendDespawn(*local_iter);
-                               } else {
+                               } else if (SendingUpdates()) {
                                        // update
                                        QueueUpdate(*local_iter);
                                }
@@ -246,7 +248,6 @@ void ClientConnection::Update(int dt) {
                }
                SendUpdates();
 
-               input->Update(dt);
                CheckPlayerFix();
                CheckChunkQueue();
        }
@@ -308,6 +309,10 @@ void ClientConnection::SendDespawn(SpawnStatus &status) {
        ++confirm_wait;
 }
 
+bool ClientConnection::SendingUpdates() const noexcept {
+       return entity_updates_skipped >= NetStat().SuggestedPacketSkip();
+}
+
 void ClientConnection::QueueUpdate(SpawnStatus &status) {
        // don't send updates while spawn not ack'd or despawn sent
        if (status.spawn_pack == -1 && status.despawn_pack == -1) {
@@ -316,10 +321,17 @@ void ClientConnection::QueueUpdate(SpawnStatus &status) {
 }
 
 void ClientConnection::SendUpdates() {
+       if (!SendingUpdates()) {
+               entity_updates.clear();
+               ++entity_updates_skipped;
+               return;
+       }
+       auto base = PlayerChunks().Base();
        auto pack = Prepare<Packet::EntityUpdate>();
+       pack.WriteChunkBase(base);
        int entity_pos = 0;
        for (SpawnStatus *status : entity_updates) {
-               pack.WriteEntity(*status->entity, entity_pos);
+               pack.WriteEntity(*status->entity, base, entity_pos);
                ++entity_pos;
                if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) {
                        pack.WriteEntityCount(entity_pos);
@@ -333,6 +345,7 @@ void ClientConnection::SendUpdates() {
                Send(Packet::EntityUpdate::GetSize(entity_pos));
        }
        entity_updates.clear();
+       entity_updates_skipped = 0;
 }
 
 void ClientConnection::CheckPlayerFix() {
@@ -352,6 +365,23 @@ void ClientConnection::CheckPlayerFix() {
        }
 }
 
+namespace {
+
+struct QueueCompare {
+       explicit QueueCompare(const glm::ivec3 &base)
+       : base(base) { }
+       bool operator ()(const glm::ivec3 &left, const glm::ivec3 &right) const noexcept {
+               const glm::ivec3 ld(left - base);
+               const glm::ivec3 rd(right - base);
+               return
+                       ld.x * ld.x + ld.y * ld.y + ld.z * ld.z <
+                       rd.x * rd.x + rd.y * rd.y + rd.z * rd.z;
+       }
+       const glm::ivec3 &base;
+};
+
+}
+
 void ClientConnection::CheckChunkQueue() {
        if (PlayerChunks().Base() != old_base) {
                Chunk::Pos begin = PlayerChunks().CoordsBegin();
@@ -366,6 +396,12 @@ void ClientConnection::CheckChunkQueue() {
                        }
                }
                old_base = PlayerChunks().Base();
+               sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
+       }
+       // if we have packet skip enabled and just pushed an entity
+       // update, don't also send chunk data
+       if (NetStat().SuggestedPacketSkip() > 0 && entity_updates_skipped == 0) {
+               return;
        }
        if (transmitter.Transmitting()) {
                transmitter.Transmit();
@@ -406,17 +442,23 @@ void ClientConnection::AttachPlayer(Player &player) {
                        }
                }
        }
+       sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
        // TODO: should the server do this?
        if (HasPlayerModel()) {
                GetPlayerModel().Instantiate(PlayerEntity().GetModel());
        }
 
-       cout << "player \"" << player.Name() << "\" joined" << endl;
+       string msg = "player \"" + player.Name() + "\" joined";
+       cout << msg << endl;
+       server.DistributeMessage(0, 0, msg);
 }
 
 void ClientConnection::DetachPlayer() {
        if (!HasPlayer()) return;
-       cout << "player \"" << input->GetPlayer().Name() << "\" left" << endl;
+       string msg = "player \"" + input->GetPlayer().Name() + "\" left";
+       cout << msg << endl;
+       server.DistributeMessage(0, 0, msg);
+
        server.GetWorldSave().Write(input->GetPlayer());
        PlayerEntity().Kill();
        PlayerEntity().UnRef();
@@ -426,7 +468,7 @@ void ClientConnection::DetachPlayer() {
        old_actions = 0;
 }
 
-void ClientConnection::SetPlayerModel(const CompositeModel &m) noexcept {
+void ClientConnection::SetPlayerModel(const Model &m) noexcept {
        player_model = &m;
        if (HasPlayer()) {
                m.Instantiate(PlayerEntity().GetModel());
@@ -437,7 +479,7 @@ bool ClientConnection::HasPlayerModel() const noexcept {
        return player_model;
 }
 
-const CompositeModel &ClientConnection::GetPlayerModel() const noexcept {
+const Model &ClientConnection::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
@@ -523,21 +565,17 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) {
                return;
        }
        glm::vec3 movement(0.0f);
-       float pitch = 0.0f;
-       float yaw = 0.0f;
        uint8_t new_actions;
        uint8_t slot;
 
        player_update_pack = pack.Seq();
        pack.ReadPredictedState(player_update_state);
        pack.ReadMovement(movement);
-       pack.ReadPitch(pitch);
-       pack.ReadYaw(yaw);
        pack.ReadActions(new_actions);
        pack.ReadSlot(slot);
 
        input->SetMovement(movement);
-       input->TurnHead(pitch - input->GetPitch(), yaw - input->GetYaw());
+       input->TurnHead(player_update_state.pitch - input->GetPitch(), player_update_state.yaw - input->GetYaw());
        input->SelectInventory(slot);
 
        if ((new_actions & 0x01) && !(old_actions & 0x01)) {
@@ -562,6 +600,19 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept {
        return HasPlayer() && PlayerChunks().InRange(pos);
 }
 
+void ClientConnection::On(const Packet::Message &pack) {
+       uint8_t type;
+       uint32_t ref;
+       string msg;
+       pack.ReadType(type);
+       pack.ReadReferral(ref);
+       pack.ReadMessage(msg);
+
+       if (type == 1 && HasPlayer()) {
+               server.DispatchMessage(input->GetPlayer(), msg);
+       }
+}
+
 
 Server::Server(
        const Config::Network &conf,
@@ -574,7 +625,8 @@ Server::Server(
 , world(world)
 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , save(save)
-, player_model(nullptr) {
+, player_model(nullptr)
+, cli(world) {
        serv_sock = SDLNet_UDP_Open(conf.port);
        if (!serv_sock) {
                throw NetError("SDLNet_UDP_Open");
@@ -642,7 +694,7 @@ void Server::Update(int dt) {
        }
 }
 
-void Server::SetPlayerModel(const CompositeModel &m) noexcept {
+void Server::SetPlayerModel(const Model &m) noexcept {
        player_model = &m;
        for (ClientConnection &client : clients) {
                client.SetPlayerModel(m);
@@ -653,7 +705,7 @@ bool Server::HasPlayerModel() const noexcept {
        return player_model;
 }
 
-const CompositeModel &Server::GetPlayerModel() const noexcept {
+const Model &Server::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
@@ -689,5 +741,31 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
        }
 }
 
+void Server::DispatchMessage(Player &player, const string &msg) {
+       if (msg.empty()) {
+               return;
+       }
+       if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') {
+               cli.Execute(player, msg.substr(1));
+       } else {
+               DistributeMessage(1, player.GetEntity().ID(), msg);
+       }
+}
+
+void Server::DistributeMessage(uint8_t type, uint32_t ref, const string &msg) {
+       auto pack = Packet::Make<Packet::Message>(serv_pack);
+       pack.WriteType(type);
+       pack.WriteReferral(ref);
+       pack.WriteMessage(msg);
+       serv_pack.len = sizeof(Packet::Header) + Packet::Message::GetSize(msg);
+       SendAll();
+}
+
+void Server::SendAll() {
+       for (ClientConnection &client : clients) {
+               client.GetConnection().Send(serv_pack, serv_sock);
+       }
+}
+
 }
 }