From 7a73fd467cfadfe3b3a5cf43d3eea9392f4c7f34 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 8 Nov 2022 22:14:45 +0000 Subject: Protocol: Use correct calculation for delta movements (#5455) * Protocol: Use correct calculation for delta movements --- src/Protocol/Protocol_1_9.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/Protocol/Protocol_1_9.cpp') diff --git a/src/Protocol/Protocol_1_9.cpp b/src/Protocol/Protocol_1_9.cpp index dd2133f77..9a4bcdc4a 100644 --- a/src/Protocol/Protocol_1_9.cpp +++ b/src/Protocol/Protocol_1_9.cpp @@ -307,12 +307,15 @@ void cProtocol_1_9_0::SendEntityPosition(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - const auto Delta = (a_Entity.GetPosition() - a_Entity.GetLastSentPosition()) * 32 * 128; + const auto Delta = (a_Entity.GetPosition() * 32 * 128).Floor() - (a_Entity.GetLastSentPosition() * 32 * 128).Floor(); - // Limitations of a short - static const auto Max = std::numeric_limits::max(); - - if ((std::abs(Delta.x) <= Max) && (std::abs(Delta.y) <= Max) && (std::abs(Delta.z) <= Max)) + // Ensure that the delta has enough precision and is within range of a BEInt16: + if ( + Delta.HasNonZeroLength() && + cByteBuffer::CanBEInt16Represent(Delta.x) && + cByteBuffer::CanBEInt16Represent(Delta.y) && + cByteBuffer::CanBEInt16Represent(Delta.z) + ) { const auto Move = static_cast>(Delta); @@ -341,7 +344,8 @@ void cProtocol_1_9_0::SendEntityPosition(const cEntity & a_Entity) return; } - // Too big a movement, do a teleport + // Too big or small a movement, do a teleport. + cPacketizer Pkt(*this, pktTeleportEntity); Pkt.WriteVarInt32(a_Entity.GetUniqueID()); Pkt.WriteBEDouble(a_Entity.GetPosX()); -- cgit v1.2.3