Skip to content

Commit ea36ee9

Browse files
Synchronize changes from 1.6 master branch [ci skip]
361e1e2 Fix ped's camera synchronization (PR #3987, Fixes #3554)
2 parents 9489b66 + 361e1e2 commit ea36ee9

File tree

10 files changed

+101
-8
lines changed

10 files changed

+101
-8
lines changed

Client/mods/deathmatch/logic/CClientPed.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,14 +3333,20 @@ void CClientPed::SetTargetRotation(float fRotation)
33333333
SetCurrentRotation(fRotation);
33343334
}
33353335

3336-
void CClientPed::SetTargetRotation(unsigned long ulDelay, float fRotation, float fCameraRotation)
3336+
void CClientPed::SetTargetRotation(unsigned long ulDelay, std::optional<float> rotation, std::optional<float> cameraRotation)
33373337
{
33383338
m_ulBeginRotationTime = CClientTime::GetTime();
33393339
m_ulEndRotationTime = m_ulBeginRotationTime + ulDelay;
3340-
m_fBeginRotation = (m_pPlayerPed) ? m_pPlayerPed->GetCurrentRotation() : m_fCurrentRotation;
3341-
m_fTargetRotationA = fRotation;
3342-
m_fBeginCameraRotation = GetCameraRotation();
3343-
m_fTargetCameraRotation = fCameraRotation;
3340+
if (rotation.has_value())
3341+
{
3342+
m_fBeginRotation = (m_pPlayerPed) ? m_pPlayerPed->GetCurrentRotation() : m_fCurrentRotation;
3343+
m_fTargetRotationA = rotation.value();
3344+
}
3345+
if (cameraRotation.has_value())
3346+
{
3347+
m_fBeginCameraRotation = GetCameraRotation();
3348+
m_fTargetCameraRotation = cameraRotation.value();
3349+
}
33443350
}
33453351

33463352
// Temporary

Client/mods/deathmatch/logic/CClientPed.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct SLastSyncedPedData
105105
CVector vPosition;
106106
CVector vVelocity;
107107
float fRotation;
108+
float cameraRotation{};
108109
bool bOnFire;
109110
bool bIsInWater;
110111
bool isReloadingWeapon;
@@ -205,7 +206,7 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
205206
float GetCurrentRotation();
206207
void SetCurrentRotation(float fRotation, bool bIncludeTarget = true);
207208
void SetTargetRotation(float fRotation);
208-
void SetTargetRotation(unsigned long ulDelay, float fRotation, float fCameraRotation);
209+
void SetTargetRotation(unsigned long ulDelay, std::optional<float> rotation, std::optional<float> cameraRotation);
209210

210211
float GetCameraRotation();
211212
void SetCameraRotation(float fRotation);

Client/mods/deathmatch/logic/CPedSync.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ void CPedSync::Packet_PedStartSync(NetBitStreamInterface& BitStream)
124124
float fHealth, fArmor;
125125
BitStream.Read(fHealth);
126126
BitStream.Read(fArmor);
127+
128+
if (BitStream.Can(eBitStreamVersion::PedSync_CameraRotation))
129+
{
130+
float cameraRotation{};
131+
BitStream.Read(cameraRotation);
132+
133+
pPed->SetCameraRotation(cameraRotation);
134+
}
127135

128136
// Set data
129137
pPed->SetPosition(vecPosition);
@@ -179,10 +187,15 @@ void CPedSync::Packet_PedSync(NetBitStreamInterface& BitStream)
179187
unsigned char ucFlags = 0;
180188
BitStream.Read(ucFlags);
181189

190+
std::uint8_t flags2{};
191+
if (BitStream.Can(eBitStreamVersion::PedSync_CameraRotation))
192+
BitStream.Read(flags2);
193+
182194
CVector vecPosition{ CVector::NoInit{} }, vecMoveSpeed{ CVector::NoInit{} };
183195
float fRotation, fHealth, fArmor;
184196
bool bOnFire;
185197
bool bIsInWater;
198+
float cameraRotation;
186199

187200
if (BitStream.Can(eBitStreamVersion::PedSync_Revision))
188201
{
@@ -231,6 +244,13 @@ void CPedSync::Packet_PedSync(NetBitStreamInterface& BitStream)
231244
if (ucFlags & 0x10)
232245
BitStream.Read(fArmor);
233246

247+
if (flags2 & 0x01)
248+
{
249+
SCameraRotationSync camRotation;
250+
BitStream.Read(&camRotation);
251+
cameraRotation = camRotation.data.fRotation;
252+
}
253+
234254
// And the burning state
235255
if (BitStream.Version() >= 0x04E && ucFlags & 0x20)
236256
BitStream.ReadBit(bOnFire);
@@ -246,13 +266,15 @@ void CPedSync::Packet_PedSync(NetBitStreamInterface& BitStream)
246266
if (ucFlags & 0x01)
247267
pPed->SetTargetPosition(vecPosition, PED_SYNC_RATE);
248268
if (ucFlags & 0x02)
249-
pPed->SetTargetRotation(PED_SYNC_RATE, fRotation, 0.0f);
269+
pPed->SetTargetRotation(PED_SYNC_RATE, fRotation, std::nullopt);
250270
if (ucFlags & 0x04)
251271
pPed->SetMoveSpeed(vecMoveSpeed);
252272
if (ucFlags & 0x08)
253273
pPed->LockHealth(fHealth);
254274
if (ucFlags & 0x10)
255275
pPed->LockArmor(fArmor);
276+
if (flags2 & 0x01)
277+
pPed->SetTargetRotation(PED_SYNC_RATE, std::nullopt, cameraRotation);
256278
if (BitStream.Version() >= 0x04E && ucFlags & 0x20)
257279
pPed->SetOnFire(bOnFire);
258280
if (BitStream.Version() >= 0x55 && ucFlags & 0x40)
@@ -312,8 +334,12 @@ void CPedSync::WritePedInformation(NetBitStreamInterface* pBitStream, CClientPed
312334
if (pPed->HasSyncedAnim() && (!pPed->IsRunningAnimation() || pPed->m_animationOverridedByClient))
313335
ucFlags |= 0x80;
314336

337+
std::uint8_t flags2{};
338+
if (!IsNearlyEqual(pPed->GetCameraRotation(), pPed->m_LastSyncedData->cameraRotation) && pBitStream->Can(eBitStreamVersion::PedSync_CameraRotation))
339+
flags2 |= 0x01;
340+
315341
// Do we really have to sync this ped?
316-
if (ucFlags == 0)
342+
if (ucFlags == 0 && flags2 == 0)
317343
return;
318344

319345
// Write the ped id
@@ -325,6 +351,10 @@ void CPedSync::WritePedInformation(NetBitStreamInterface* pBitStream, CClientPed
325351
// Write flags
326352
pBitStream->Write(ucFlags);
327353

354+
// Write flags 2
355+
if (pBitStream->Can(eBitStreamVersion::PedSync_CameraRotation))
356+
pBitStream->Write(flags2);
357+
328358
// Write position if needed
329359
if (ucFlags & 0x01)
330360
{
@@ -389,6 +419,14 @@ void CPedSync::WritePedInformation(NetBitStreamInterface* pBitStream, CClientPed
389419
pPed->m_LastSyncedData->fArmour = pPed->GetArmor();
390420
}
391421

422+
if (flags2 & 0x01)
423+
{
424+
SCameraRotationSync camRotation;
425+
camRotation.data.fRotation = pPed->GetCameraRotation();
426+
pBitStream->Write(&camRotation);
427+
pPed->m_LastSyncedData->cameraRotation = camRotation.data.fRotation;
428+
}
429+
392430
if (ucFlags & 0x20 && pBitStream->Version() >= 0x04E)
393431
{
394432
pBitStream->WriteBit(pPed->IsOnFire());

Server/mods/deathmatch/logic/CPed.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ class CPed : public CElement
236236
float GetRotation() { return m_fRotation; }
237237
void SetRotation(float fRotation) { m_fRotation = fRotation; }
238238

239+
float GetCameraRotation() const { return m_cameraRotation; }
240+
void SetCameraRotation(float fRotation) { m_cameraRotation = fRotation; }
241+
239242
void GetRotation(CVector& vecRotation);
240243
void GetMatrix(CMatrix& matrix);
241244
void SetMatrix(const CMatrix& matrix);
@@ -346,6 +349,7 @@ class CPed : public CElement
346349
bool m_reloadingWeapon{};
347350
CVehicle* m_pJackingVehicle;
348351
SPlayerAnimData m_animData{};
352+
float m_cameraRotation{};
349353

350354
CVehicle* m_pVehicle;
351355
unsigned int m_uiVehicleSeat;

Server/mods/deathmatch/logic/CPedSync.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ void CPedSync::Packet_PedSync(CPedSyncPacket& Packet)
277277
if (Data.ucFlags & 0x10)
278278
pPed->SetArmor(Data.fArmor);
279279

280+
if (Data.flags2 & 0x01)
281+
pPed->SetCameraRotation(Data.cameraRotation);
282+
280283
if (Data.ucFlags & 0x20)
281284
pPed->SetOnFire(Data.bOnFire);
282285

Server/mods/deathmatch/logic/packets/CPedStartSyncPacket.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ bool CPedStartSyncPacket::Write(NetBitStreamInterface& BitStream) const
3737
BitStream.Write(m_pPed->GetHealth());
3838
BitStream.Write(m_pPed->GetArmor());
3939

40+
if (BitStream.Can(eBitStreamVersion::PedSync_CameraRotation))
41+
BitStream.Write(m_pPed->GetCameraRotation());
42+
4043
return true;
4144
}

Server/mods/deathmatch/logic/packets/CPedSyncPacket.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ bool CPedSyncPacket::Read(NetBitStreamInterface& BitStream)
3838
return false;
3939
Data.ucFlags = ucFlags;
4040

41+
if (BitStream.Can(eBitStreamVersion::PedSync_CameraRotation))
42+
{
43+
if (!BitStream.Read(Data.flags2))
44+
return false;
45+
}
46+
else
47+
Data.flags2 = 0;
48+
4149
// Did we recieve position?
4250
if (ucFlags & 0x01)
4351
{
@@ -57,6 +65,14 @@ bool CPedSyncPacket::Read(NetBitStreamInterface& BitStream)
5765
return false;
5866
}
5967

68+
if (Data.flags2 & 0x01)
69+
{
70+
SCameraRotationSync camRotation;
71+
if (!BitStream.Read(&camRotation))
72+
return false;
73+
Data.cameraRotation = camRotation.data.fRotation;
74+
}
75+
6076
// On Fire
6177
if (ucFlags & 0x20)
6278
{
@@ -100,6 +116,9 @@ bool CPedSyncPacket::Write(NetBitStreamInterface& BitStream) const
100116

101117
BitStream.Write(Data.ucFlags);
102118

119+
if (BitStream.Can(eBitStreamVersion::PedSync_CameraRotation))
120+
BitStream.Write(Data.flags2);
121+
103122
if (BitStream.Can(eBitStreamVersion::PedSync_Revision))
104123
{
105124
// Position and rotation
@@ -140,6 +159,14 @@ bool CPedSyncPacket::Write(NetBitStreamInterface& BitStream) const
140159
BitStream.Write(Data.fHealth);
141160
if (Data.ucFlags & 0x10)
142161
BitStream.Write(Data.fArmor);
162+
163+
if (Data.flags2 & 0x01)
164+
{
165+
SCameraRotationSync camRotation;
166+
camRotation.data.fRotation = Data.cameraRotation;
167+
BitStream.Write(&camRotation);
168+
}
169+
143170
if (Data.ucFlags & 0x20)
144171
BitStream.WriteBit(Data.bOnFire);
145172
if (Data.ucFlags & 0x60 && BitStream.Can(eBitStreamVersion::IsPedReloadingWeapon))

Server/mods/deathmatch/logic/packets/CPedSyncPacket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class CPedSyncPacket final : public CPacket
2323
{
2424
ElementID ID;
2525
unsigned char ucFlags;
26+
std::uint8_t flags2;
2627
unsigned char ucSyncTimeContext;
2728
SPositionSync position;
2829
SPedRotationSync rotation;
@@ -32,6 +33,7 @@ class CPedSyncPacket final : public CPacket
3233
bool bOnFire;
3334
bool bIsInWater;
3435
bool isReloadingWeapon;
36+
float cameraRotation;
3537

3638
bool ReadSpatialData(NetBitStreamInterface& BitStream);
3739
// Backward compatibility

Shared/sdk/SharedUtil.Math.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,9 @@ namespace SharedUtil
113113
{
114114
return std::uniform_real_distribution<float>{minRange, maxRange}(randomEngine);
115115
}
116+
117+
inline bool IsNearlyEqual(float a, float b, float epsilon = std::numeric_limits<float>().epsilon()) noexcept
118+
{
119+
return std::fabs(a - b) <= epsilon;
120+
}
116121
} // namespace SharedUtil

Shared/sdk/net/bitstream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,10 @@ enum class eBitStreamVersion : unsigned short
608608
// 2025-01-10
609609
WorldSpecialProperty_FlyingComponents,
610610

611+
// Ped's camera synchronization
612+
// 2025-01-29
613+
PedSync_CameraRotation,
614+
611615
// This allows us to automatically increment the BitStreamVersion when things are added to this enum.
612616
// Make sure you only add things above this comment.
613617
Next,

0 commit comments

Comments
 (0)