]> git.localhorst.tv Git - blank.git/blobdiff - src/client/net.cpp
send updates less frequently on bad connections
[blank.git] / src / client / net.cpp
index 73931f44c1825ba166a743cafd53c8e9747e4d7c..18f593ac5831d218f0019723c511ff977a7e2ffe 100644 (file)
@@ -294,8 +294,6 @@ uint16_t Client::SendPlayerUpdate(
        auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
        pack.WritePredictedState(prediction);
        pack.WriteMovement(movement);
-       pack.WritePitch(pitch);
-       pack.WriteYaw(yaw);
        pack.WriteActions(actions);
        pack.WriteSlot(slot);
        return conn.Send(client_pack, client_sock);
@@ -306,16 +304,34 @@ uint16_t Client::SendPart() {
        return conn.Send(client_pack, client_sock);
 }
 
+uint16_t Client::SendMessage(
+       uint8_t type,
+       uint32_t ref,
+       const string &msg
+) {
+       auto pack = Packet::Make<Packet::Message>(client_pack);
+       pack.WriteType(type);
+       pack.WriteReferral(ref);
+       pack.WriteMessage(msg);
+       client_pack.len = sizeof(Packet::Header) + Packet::Message::GetSize(msg);
+       return conn.Send(client_pack, client_sock);
+}
 
 NetworkedInput::NetworkedInput(World &world, Player &player, Client &client)
 : PlayerController(world, player)
 , client(client)
 , player_hist()
+, old_movement(0.0f)
+, old_actions(0)
 , actions(0) {
 
 }
 
-void NetworkedInput::Update(int dt) {
+bool NetworkedInput::UpdateImportant() const noexcept {
+       return old_actions != actions || !iszero(old_movement - GetMovement());
+}
+
+void NetworkedInput::Update(Entity &, float dt) {
        Invalidate();
        UpdatePlayer();
 }
@@ -332,15 +348,17 @@ void NetworkedInput::PushPlayerUpdate(int dt) {
                InventorySlot()
        );
        if (player_hist.size() < 16) {
-               player_hist.emplace_back(state, GetPlayer().GetEntity().TargetVelocity(), dt, packet);
+               player_hist.emplace_back(state, GetMovement(), dt * 0.001f, packet);
        } else {
                auto entry = player_hist.begin();
                entry->state = state;
-               entry->tgt_vel = GetPlayer().GetEntity().TargetVelocity();
-               entry->delta_t = dt;
+               entry->movement = GetMovement();
+               entry->delta_t = dt * 0.001f;
                entry->packet = packet;
                player_hist.splice(player_hist.end(), player_hist, entry);
        }
+       old_movement = GetMovement();
+       old_actions = actions;
 }
 
 void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corrected_state) {
@@ -366,7 +384,9 @@ void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corr
                }
        }
 
-       EntityState &player_state = GetPlayer().GetEntity().GetState();
+       glm::vec3 restore_movement(GetMovement());
+
+       EntityState player_state = GetPlayer().GetEntity().GetState();
        Entity replay(GetPlayer().GetEntity());
        replay.SetState(corrected_state);
 
@@ -379,12 +399,8 @@ void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corr
        vector<WorldCollision> col;
        while (entry != end) {
                replay.Velocity(entry->state.velocity);
-               replay.TargetVelocity(entry->tgt_vel);
-               replay.Update(entry->delta_t);
-               if (GetWorld().Intersection(replay, col)) {
-                       GetWorld().Resolve(replay, col);
-                       col.clear();
-               }
+               SetMovement(entry->movement);
+               GetWorld().Update(replay, entry->delta_t);
                entry->state.chunk_pos = replay.GetState().chunk_pos;
                entry->state.block_pos = replay.GetState().block_pos;
                ++entry;
@@ -394,6 +410,7 @@ void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corr
        const float disp_squared = dot(displacement, displacement);
 
        if (disp_squared < 16.0f * numeric_limits<float>::epsilon()) {
+               SetMovement(restore_movement);
                return;
        }
 
@@ -412,6 +429,8 @@ void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corr
                displacement *= 0.01f / sqrt(disp_squared);
                player_state.block_pos += displacement;
        }
+       GetPlayer().GetEntity().SetState(player_state);
+       SetMovement(restore_movement);
 }
 
 void NetworkedInput::StartPrimaryAction() {