Skip to content

Commit

Permalink
Ped sync improvements (PR multitheftauto#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
tederis authored Jun 17, 2024
1 parent 5d4d7df commit f5b599c
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 82 deletions.
21 changes: 11 additions & 10 deletions Client/mods/deathmatch/logic/CClientPed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3325,7 +3325,7 @@ void CClientPed::SetTargetRotation(unsigned long ulDelay, float fRotation, float
{
m_ulBeginRotationTime = CClientTime::GetTime();
m_ulEndRotationTime = m_ulBeginRotationTime + ulDelay;
m_fBeginRotation = (m_pPlayerPed) ? m_pPlayerPed->GetTargetRotation() : m_fCurrentRotation;
m_fBeginRotation = (m_pPlayerPed) ? m_pPlayerPed->GetCurrentRotation() : m_fCurrentRotation;
m_fTargetRotationA = fRotation;
m_fBeginCameraRotation = GetCameraRotation();
m_fTargetCameraRotation = fCameraRotation;
Expand Down Expand Up @@ -3383,23 +3383,24 @@ void CClientPed::Interpolate()
{
// We're not at the end?
if (ulCurrentTime < m_ulEndRotationTime)
{
// Interpolate the player rotation
float fDeltaTime = float(m_ulEndRotationTime - m_ulBeginRotationTime);
float fDelta = GetOffsetDegrees(m_fBeginRotation, m_fTargetRotationA);
float fCameraDelta = GetOffsetDegrees(m_fBeginCameraRotation, m_fTargetCameraRotation);
float fProgress = float(ulCurrentTime - m_ulBeginRotationTime);
float fNewRotation = m_fBeginRotation + (fDelta / fDeltaTime * fProgress);
float fNewCameraRotation = m_fBeginCameraRotation + (fCameraDelta / fDeltaTime * fProgress);
{
const float fDelta = GetOffsetRadians(m_fBeginRotation, m_fTargetRotationA);

// Hack for the wrap-around (the edge seems to be varying...)
if (fDelta < -5.0f || fDelta > 5.0f)
if (fDelta < -M_PI || fDelta > M_PI)
{
SetCurrentRotation(m_fTargetRotationA);
SetCameraRotation(m_fTargetCameraRotation);
}
else
{
// Interpolate the player rotation
const float fDeltaTime = float(m_ulEndRotationTime - m_ulBeginRotationTime);
const float fCameraDelta = GetOffsetRadians(m_fBeginCameraRotation, m_fTargetCameraRotation);
const float fProgress = float(ulCurrentTime - m_ulBeginRotationTime);
const float fNewRotation = m_fBeginRotation + fDelta * (fProgress / fDeltaTime);
const float fNewCameraRotation = m_fBeginCameraRotation + fCameraDelta * (fProgress / fDeltaTime);

SetCurrentRotation(fNewRotation);
SetCameraRotation(fNewCameraRotation);
}
Expand Down
96 changes: 72 additions & 24 deletions Client/mods/deathmatch/logic/CPedSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,50 @@ void CPedSync::Packet_PedSync(NetBitStreamInterface& BitStream)
unsigned char ucFlags = 0;
BitStream.Read(ucFlags);

CVector vecPosition, vecMoveSpeed;
CVector vecPosition{ CVector::NoInit{} }, vecMoveSpeed{ CVector::NoInit{} };
float fRotation, fHealth, fArmor;
bool bOnFire;
bool bIsInWater;

// Read out the position
if (ucFlags & 0x01)
if (BitStream.Can(eBitStreamVersion::PedSync_Revision))
{
BitStream.Read(vecPosition.fX);
BitStream.Read(vecPosition.fY);
BitStream.Read(vecPosition.fZ);
}
// Read out the position
SPositionSync position(false);
if (ucFlags & 0x01)
BitStream.Read(&position);

// And rotation
if (ucFlags & 0x02)
BitStream.Read(fRotation);
// And rotation
SPedRotationSync rotation;
if (ucFlags & 0x02)
BitStream.Read(&rotation);

// And the move speed
SVelocitySync velocity;
if (ucFlags & 0x04)
BitStream.Read(&velocity);

// And the move speed
if (ucFlags & 0x04)
vecPosition = position.data.vecPosition;
fRotation = rotation.data.fRotation;
vecMoveSpeed = velocity.data.vecVelocity;
}
else
{
BitStream.Read(vecMoveSpeed.fX);
BitStream.Read(vecMoveSpeed.fY);
BitStream.Read(vecMoveSpeed.fZ);
if (ucFlags & 0x01)
{
BitStream.Read(vecPosition.fX);
BitStream.Read(vecPosition.fY);
BitStream.Read(vecPosition.fZ);
}

if (ucFlags & 0x02)
BitStream.Read(fRotation);

if (ucFlags & 0x04)
{
BitStream.Read(vecMoveSpeed.fX);
BitStream.Read(vecMoveSpeed.fY);
BitStream.Read(vecMoveSpeed.fZ);
}
}

// And health with armour
Expand All @@ -223,9 +244,9 @@ void CPedSync::Packet_PedSync(NetBitStreamInterface& BitStream)
if (pPed && pPed->CanUpdateSync(ucSyncTimeContext))
{
if (ucFlags & 0x01)
pPed->SetPosition(vecPosition);
pPed->SetTargetPosition(vecPosition, PED_SYNC_RATE);
if (ucFlags & 0x02)
pPed->SetCurrentRotation(fRotation);
pPed->SetTargetRotation(PED_SYNC_RATE, fRotation, 0.0f);
if (ucFlags & 0x04)
pPed->SetMoveSpeed(vecMoveSpeed);
if (ucFlags & 0x08)
Expand Down Expand Up @@ -303,24 +324,51 @@ void CPedSync::WritePedInformation(NetBitStreamInterface* pBitStream, CClientPed
// Write position if needed
if (ucFlags & 0x01)
{
pBitStream->Write(vecPosition.fX);
pBitStream->Write(vecPosition.fY);
pBitStream->Write(vecPosition.fZ);
if (pBitStream->Can(eBitStreamVersion::PedSync_Revision))
{
SPositionSync position(false);
position.data.vecPosition = vecPosition;
pBitStream->Write(&position);
}
else
{
pBitStream->Write(vecPosition.fX);
pBitStream->Write(vecPosition.fY);
pBitStream->Write(vecPosition.fZ);
}

pPed->m_LastSyncedData->vPosition = vecPosition;
}

if (ucFlags & 0x02)
{
pBitStream->Write(pPed->GetCurrentRotation());
if (pBitStream->Can(eBitStreamVersion::PedSync_Revision))
{
SPedRotationSync rotation;
rotation.data.fRotation = pPed->GetCurrentRotation();
pBitStream->Write(&rotation);
}
else
pBitStream->Write(pPed->GetCurrentRotation());

pPed->m_LastSyncedData->fRotation = pPed->GetCurrentRotation();
}

// Write velocity
if (ucFlags & 0x04)
{
pBitStream->Write(vecVelocity.fX);
pBitStream->Write(vecVelocity.fY);
pBitStream->Write(vecVelocity.fZ);
if (pBitStream->Can(eBitStreamVersion::PedSync_Revision))
{
SVelocitySync velocity;
pBitStream->Write(&velocity);
}
else
{
pBitStream->Write(vecVelocity.fX);
pBitStream->Write(vecVelocity.fY);
pBitStream->Write(vecVelocity.fZ);
}

pPed->m_LastSyncedData->vVelocity = vecVelocity;
}

Expand Down
6 changes: 3 additions & 3 deletions Server/mods/deathmatch/logic/CPedSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ void CPedSync::Packet_PedSync(CPedSyncPacket& Packet)
// Apply the data to the ped
if (Data.ucFlags & 0x01)
{
pPed->SetPosition(Data.vecPosition);
pPed->SetPosition(Data.position.data.vecPosition);
g_pGame->GetColManager()->DoHitDetection(pPed->GetPosition(), pPed);
}
if (Data.ucFlags & 0x02)
pPed->SetRotation(Data.fRotation);
pPed->SetRotation(Data.rotation.data.fRotation);
if (Data.ucFlags & 0x04)
pPed->SetVelocity(Data.vecVelocity);
pPed->SetVelocity(Data.velocity.data.vecVelocity);

if (Data.ucFlags & 0x08)
{
Expand Down
114 changes: 83 additions & 31 deletions Server/mods/deathmatch/logic/packets/CPedSyncPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,8 @@ bool CPedSyncPacket::Read(NetBitStreamInterface& BitStream)

// Did we recieve position?
if (ucFlags & 0x01)
{
if (!BitStream.Read(Data.vecPosition.fX) || !BitStream.Read(Data.vecPosition.fY) || !BitStream.Read(Data.vecPosition.fZ))
return false;
}

// Rotation
if (ucFlags & 0x02)
{
if (!BitStream.Read(Data.fRotation))
return false;
}

// Velocity
if (ucFlags & 0x04)
{
if (!BitStream.Read(Data.vecVelocity.fX) || !BitStream.Read(Data.vecVelocity.fY) || !BitStream.Read(Data.vecVelocity.fZ))
{
if (!(BitStream.Can(eBitStreamVersion::PedSync_Revision) ? Data.ReadSpatialData(BitStream) : Data.ReadSpatialDataBC(BitStream)))
return false;
}

Expand Down Expand Up @@ -108,25 +94,39 @@ bool CPedSyncPacket::Write(NetBitStreamInterface& BitStream) const

BitStream.Write(Data.ucFlags);

// Position and rotation
if (Data.ucFlags & 0x01)
if (BitStream.Can(eBitStreamVersion::PedSync_Revision))
{
BitStream.Write(Data.vecPosition.fX);
BitStream.Write(Data.vecPosition.fY);
BitStream.Write(Data.vecPosition.fZ);
}
// Position and rotation
if (Data.ucFlags & 0x01)
BitStream.Write(&Data.position);

if (Data.ucFlags & 0x02)
{
BitStream.Write(Data.fRotation);
}
if (Data.ucFlags & 0x02)
BitStream.Write(&Data.rotation);

// Velocity
if (Data.ucFlags & 0x04)
// Velocity
if (Data.ucFlags & 0x04)
BitStream.Write(&Data.velocity);
}
else
{
BitStream.Write(Data.vecVelocity.fX);
BitStream.Write(Data.vecVelocity.fY);
BitStream.Write(Data.vecVelocity.fZ);
// Position and rotation
if (Data.ucFlags & 0x01)
{
BitStream.Write(Data.position.data.vecPosition.fX);
BitStream.Write(Data.position.data.vecPosition.fY);
BitStream.Write(Data.position.data.vecPosition.fZ);
}

if (Data.ucFlags & 0x02)
BitStream.Write(Data.rotation.data.fRotation);

// Velocity
if (Data.ucFlags & 0x04)
{
BitStream.Write(Data.velocity.data.vecVelocity.fX);
BitStream.Write(Data.velocity.data.vecVelocity.fY);
BitStream.Write(Data.velocity.data.vecVelocity.fZ);
}
}

// Health, armour, on fire and is in water
Expand All @@ -141,3 +141,55 @@ bool CPedSyncPacket::Write(NetBitStreamInterface& BitStream) const

return true;
}

bool CPedSyncPacket::SyncData::ReadSpatialData(NetBitStreamInterface& BitStream)
{
// Did we recieve position?
if (ucFlags & 0x01)
{
if (!BitStream.Read(&position))
return false;
}

// Rotation
if (ucFlags & 0x02)
{
if (!BitStream.Read(&rotation))
return false;
}

// Velocity
if (ucFlags & 0x04)
{
if (!BitStream.Read(&velocity))
return false;
}

return true;
}

bool CPedSyncPacket::SyncData::ReadSpatialDataBC(NetBitStreamInterface& BitStream)
{
// Did we recieve position?
if (ucFlags & 0x01)
{
if (!BitStream.Read(position.data.vecPosition.fX) || !BitStream.Read(position.data.vecPosition.fY) || !BitStream.Read(position.data.vecPosition.fZ))
return false;
}

// Rotation
if (ucFlags & 0x02)
{
if (!BitStream.Read(rotation.data.fRotation))
return false;
}

// Velocity
if (ucFlags & 0x04)
{
if (!BitStream.Read(velocity.data.vecVelocity.fX) || !BitStream.Read(velocity.data.vecVelocity.fY) || !BitStream.Read(velocity.data.vecVelocity.fZ))
return false;
}

return true;
}
25 changes: 15 additions & 10 deletions Server/mods/deathmatch/logic/packets/CPedSyncPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,28 @@

#include <CVector.h>
#include "CPacket.h"
#include <net/SyncStructures.h>
#include <vector>

class CPedSyncPacket final : public CPacket
{
public:
struct SyncData
{
ElementID ID;
unsigned char ucFlags;
unsigned char ucSyncTimeContext;
CVector vecPosition;
float fRotation;
CVector vecVelocity;
float fHealth;
float fArmor;
bool bOnFire;
bool bIsInWater;
ElementID ID;
unsigned char ucFlags;
unsigned char ucSyncTimeContext;
SPositionSync position;
SPedRotationSync rotation;
SVelocitySync velocity;
float fHealth;
float fArmor;
bool bOnFire;
bool bIsInWater;

bool ReadSpatialData(NetBitStreamInterface& BitStream);
// Backward compatibility
bool ReadSpatialDataBC(NetBitStreamInterface& BitStream);
};

public:
Expand Down
9 changes: 9 additions & 0 deletions Shared/mods/deathmatch/logic/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ inline float GetOffsetDegrees(float a, float b)
c = (360.0f + c);
return c;
}
inline float GetOffsetRadians(float a, float b)
{
float c = (b > a) ? b - a : 0.0f - (a - b);
if (c > PI)
c = 0.0f - (2 * PI - c);
else if (c <= -PI)
c = (2 * PI + c);
return c;
}

// Assuming fValue is the result of a difference calculation, calculate
// the shortest positive distance after wrapping
Expand Down
Loading

0 comments on commit f5b599c

Please sign in to comment.