-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathCNetAPI.cpp
2434 lines (2074 loc) · 86.5 KB
/
CNetAPI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CNetAPI.cpp
* PURPOSE: Packet processing class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include <StdInc.h>
#include <net/SyncStructures.h>
#include <game/CWeapon.h>
#include <game/CWeaponStat.h>
#include <game/CWeaponStatManager.h>
extern CClientGame* g_pClientGame;
CTickRateSettings g_TickRateSettings;
CNetAPI::CNetAPI(CClientManager* pManager)
{
// Init
m_pManager = pManager;
m_pPlayerManager = pManager->GetPlayerManager();
m_pVehicleManager = pManager->GetVehicleManager();
m_ulLastPuresyncTime = 0;
m_ulLastSyncReturnTime = 0;
m_bStoredReturnSync = false;
m_bIncreaseTimeoutTime = false;
}
bool CNetAPI::ProcessPacket(unsigned char bytePacketID, NetBitStreamInterface& BitStream)
{
switch (bytePacketID)
{
case PACKET_ID_LIGHTSYNC:
{
ElementID PlayerID;
while (BitStream.Read(PlayerID) == true)
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
// Read out the lightsync data (and apply if the player exists on the client)
ReadLightweightSync(pPlayer, BitStream);
}
return true;
}
case PACKET_ID_VEHICLE_RESYNC:
{
// Read out the vehicle ID
ElementID VehicleID;
if (BitStream.Read(VehicleID))
{
// Grab the vehicle
CClientVehicle* pVehicle = m_pVehicleManager->Get(VehicleID);
if (pVehicle)
{
// Read out and apply the resync data
ReadVehicleResync(pVehicle, BitStream);
}
}
return true;
}
case PACKET_ID_PLAYER_PURESYNC:
{
// Read out the player ID
ElementID PlayerID;
if (BitStream.Read(PlayerID))
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
if (pPlayer)
{
// Read out and apply the puresync data
ReadPlayerPuresync(pPlayer, BitStream);
}
}
return true;
}
case PACKET_ID_PLAYER_VEHICLE_PURESYNC:
{
// Read out the player ID
ElementID PlayerID;
if (BitStream.Read(PlayerID))
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
if (pPlayer)
{
// Grab the player vehicle
CClientVehicle* pVehicle = pPlayer->GetOccupiedVehicle();
if (pVehicle)
{
// Read out the vehicle puresync data
ReadVehiclePuresync(pPlayer, pVehicle, BitStream);
}
}
}
return true;
}
case PACKET_ID_PLAYER_KEYSYNC:
{
// Read out the player ID
ElementID PlayerID;
if (BitStream.Read(PlayerID))
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
if (pPlayer)
{
// Read out the keysync data
ReadKeysync(pPlayer, BitStream);
}
}
return true;
}
case PACKET_ID_PLAYER_BULLETSYNC:
{
// Read out the player ID
ElementID PlayerID;
if (BitStream.Read(PlayerID))
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
if (pPlayer)
{
// Read out the bulletsync data
ReadBulletsync(pPlayer, BitStream);
}
}
return true;
}
case PACKET_ID_WEAPON_BULLETSYNC:
{
// Read out the player ID
ElementID PlayerID;
if (BitStream.Read(PlayerID))
{
// Grab the player
CClientPlayer* pPlayer = m_pPlayerManager->Get(PlayerID);
if (pPlayer)
{
// Read out the bulletsync data
ReadWeaponBulletsync(pPlayer, BitStream);
}
}
return true;
}
case PACKET_ID_RETURN_SYNC:
{
// Grab the in vehicle flag
bool bInVehicle = BitStream.ReadBit();
SPositionSync position(false);
// Are we in a vehicle?
if (bInVehicle)
{
CClientVehicle* pVehicle = m_pPlayerManager->GetLocalPlayer()->GetOccupiedVehicle();
#ifdef MTA_DEBUG
if (m_pPlayerManager->GetLocalPlayer()->GetOccupiedVehicleSeat() == 0)
{
if (pVehicle)
{
pVehicle->m_pLastSyncer = m_pPlayerManager->GetLocalPlayer();
pVehicle->m_ulLastSyncTime = GetTickCount32();
pVehicle->m_szLastSyncType = "pure";
}
}
#endif
// Read out position
BitStream.Read(&position);
// And rotation
SRotationDegreesSync rotation(false);
BitStream.Read(&rotation);
// Remember that position
m_vecLastReturnPosition = position.data.vecPosition;
m_vecLastReturnRotation = rotation.data.vecRotation;
m_bVehicleLastReturn = true;
}
else
{
// Read out the position
BitStream.Read(&position);
/* Test code:
CClientPlayer* pLocalPlayer = m_pPlayerManager->GetLocalPlayer ();
CVector vecCurrentPosition;
pLocalPlayer->GetPosition ( vecCurrentPosition );
// Are we 'x' distance away from our server position?
if ( DistanceBetweenPoints3D ( vecCurrentPosition, vecPosition ) > 5.0f )
{
// Move us back to our server position to stay in-sync
pLocalPlayer->SetPosition ( vecPosition );
}*/
// Remember that position
m_vecLastReturnPosition = position.data.vecPosition;
m_vecLastReturnRotation = CVector(0.0f, 0.0f, 0.0f);
m_bVehicleLastReturn = false;
}
// Remember the last return sync time
m_ulLastSyncReturnTime = CClientTime::GetTime();
m_bStoredReturnSync = true;
return true;
}
}
// We couldn't handle it
return false;
}
void CNetAPI::ResetReturnPosition()
{
m_bStoredReturnSync = false;
CClientPlayer* pPlayer = m_pPlayerManager->GetLocalPlayer();
if (pPlayer)
{
m_bStoredReturnSync = true;
m_ulLastSyncReturnTime = CClientTime::GetTime();
m_ulLastPuresyncTime = 0;
CClientVehicle* pVehicle = pPlayer->GetOccupiedVehicle();
if (pVehicle)
{
pVehicle->GetPosition(m_vecLastReturnPosition);
pVehicle->GetRotationDegrees(m_vecLastReturnRotation);
m_bVehicleLastReturn = true;
}
else
{
pPlayer->GetPosition(m_vecLastReturnPosition);
m_vecLastReturnRotation = CVector(0.0f, 0.0f, 0.0f);
m_bVehicleLastReturn = false;
}
}
}
void CNetAPI::AddInterpolation(const CVector& vecPosition)
{
// Store our current position for interpolation purposes
unsigned long ulCurrentTime = CClientTime::GetTime();
m_Interpolator.Push(vecPosition, ulCurrentTime);
}
bool CNetAPI::GetInterpolation(CVector& vecPosition, unsigned short usLatency)
{
unsigned long ulInterTime = CClientTime::GetTime() - usLatency;
return m_Interpolator.Evaluate(ulInterTime, &vecPosition);
}
bool CNetAPI::IsWeaponIDAkimbo(unsigned char ucWeaponID)
{
return (ucWeaponID == 22 || ucWeaponID == 26 || ucWeaponID == 28 || ucWeaponID == 32);
}
bool CNetAPI::IsDriveByWeapon(unsigned char ucID)
{
return ((ucID >= 22 && ucID <= 33) || ucID == 38);
}
void CNetAPI::DoPulse()
{
m_bIsNetworkTrouble = false;
// If we're ingame
if (m_pManager->IsGameLoaded())
{
// Increase timeout time if downloading something
if (g_pClientGame->IsDownloadingBigPacket() || g_pClientGame->GetResourceFileDownloadManager()->IsTransferringInitialFiles())
{
m_bIncreaseTimeoutTime = true;
m_IncreaseTimeoutTimeTimer.Reset();
}
else if (m_bIncreaseTimeoutTime)
{
// Extra 5 seconds after download has finished before restoring default timeout time
if (m_IncreaseTimeoutTimeTimer.Get() > 5000)
m_bIncreaseTimeoutTime = false;
}
g_pNet->SetTimeoutTime(m_bIncreaseTimeoutTime ? 30000 : 10000);
// Grab the local player
CClientPlayer* pPlayer = m_pPlayerManager->GetLocalPlayer();
if (pPlayer && !pPlayer->IsDeadOnNetwork())
{
unsigned long ulCurrentTime = CClientTime::GetTime();
// Grab the player vehicle
CClientVehicle* pVehicle = pPlayer->GetOccupiedVehicle();
// Record local data in the packet recorder
m_pManager->GetPacketRecorder()->RecordLocalData(pPlayer);
// We should do a puresync?
if (IsPureSyncNeeded() && !g_pClientGame->IsDownloadingBigPacket())
{
// Are in a vehicle?
if (pVehicle)
{
// Are we getting out and physically left the car
if (pPlayer->GetVehicleInOutState() == VEHICLE_INOUT_GETTING_OUT && !pPlayer->GetRealOccupiedVehicle())
{
// Send a player puresync packet
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
WritePlayerPuresync(pPlayer, *pBitStream);
g_pNet->SendPacket(PACKET_ID_PLAYER_PURESYNC, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_UNRELIABLE_SEQUENCED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
else
{
// Send a vehicle puresync packet
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
WriteVehiclePuresync(pPlayer, pVehicle, *pBitStream);
g_pNet->SendPacket(PACKET_ID_PLAYER_VEHICLE_PURESYNC, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_UNRELIABLE_SEQUENCED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
// Sync its damage model too
static_cast<CDeathmatchVehicle*>(pVehicle)->SyncDamageModel();
}
else
{
// Send a puresync only if we're not doing anything vehicle related
// except getting in and jacking.
if (pPlayer->GetVehicleInOutState() == VEHICLE_INOUT_NONE || pPlayer->GetVehicleInOutState() == VEHICLE_INOUT_GETTING_IN ||
pPlayer->GetVehicleInOutState() == VEHICLE_INOUT_JACKING)
{
// Send a puresync packet
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
// Write our data
WritePlayerPuresync(pPlayer, *pBitStream);
// Send the packet and destroy it
g_pNet->SendPacket(PACKET_ID_PLAYER_PURESYNC, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_UNRELIABLE_SEQUENCED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
}
}
else
{
// We should do a keysync?
if (IsSmallKeySyncNeeded(pPlayer) && !g_pClientGame->IsDownloadingBigPacket())
{
// Send a keysync packet
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
// Write the generic keysync data
WriteKeysync(pPlayer, *pBitStream);
// Send the packet
g_pNet->SendPacket(PACKET_ID_PLAYER_KEYSYNC, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_UNRELIABLE_SEQUENCED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
}
// Time to freeze because of lack of return sync?
if (!g_pClientGame->IsDownloadingBigPacket() && (m_bStoredReturnSync) && (m_ulLastPuresyncTime != 0) && (m_ulLastSyncReturnTime != 0) &&
(ulCurrentTime <= m_ulLastPuresyncTime + 5000) && (ulCurrentTime >= m_ulLastSyncReturnTime + 10000) &&
(!g_pClientGame->GetLocalPlayer()->m_bIsGettingIntoVehicle) && (!m_bIncreaseTimeoutTime))
{
// No vehicle or vehicle in seat 0?
if (!pVehicle || pPlayer->GetOccupiedVehicleSeat() == 0)
{
// Disable our controls (swap with a blank controller state)
CControllerState state;
g_pCore->GetGame()->GetPad()->SetCurrentControllerState(&state);
// Grab our vehicle
CClientVehicle* pVehicle = pPlayer->GetOccupiedVehicle();
if (pVehicle)
{
// Freeze us at the last position
pVehicle->SetMoveSpeed(CVector(0, 0, 0));
pVehicle->SetTurnSpeed(CVector(0, 0, 0));
if (m_bVehicleLastReturn)
{
pVehicle->SetPosition(m_vecLastReturnPosition);
pVehicle->SetRotationDegrees(m_vecLastReturnRotation);
}
}
else
{
// Freeze us at the last position
pPlayer->SetMoveSpeed(CVector(0, 0, 0));
pPlayer->SetPosition(m_vecLastReturnPosition);
}
}
// Display network trouble
m_bIsNetworkTrouble = true;
}
}
if (pPlayer)
{
// Do camera sync even if player is dead
if (IsCameraSyncNeeded())
{
// Send a camera-sync packet
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
// Write our data
WriteCameraSync(*pBitStream);
// Send the packet and destroy it
g_pNet->SendPacket(PACKET_ID_CAMERA_SYNC, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_UNRELIABLE_SEQUENCED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
}
}
}
bool CNetAPI::IsSmallKeySyncNeeded(CClientPed* pPlayerModel)
{
// Grab the current and the old controllerstate
CControllerState ControllerState;
pPlayerModel->GetControllerState(ControllerState);
CControllerState LastControllerState;
float fLastCameraRotation, fLastAimY;
GetLastSentControllerState(&LastControllerState, &fLastCameraRotation, &fLastAimY);
CClientVehicle* pVehicle = pPlayerModel->GetOccupiedVehicle();
if (pVehicle && (pVehicle->GetVehicleType() == CLIENTVEHICLE_PLANE || pVehicle->GetVehicleType() == CLIENTVEHICLE_HELI))
{
if (ControllerState.LeftShoulder2 != LastControllerState.LeftShoulder2 || ControllerState.RightShoulder2 != LastControllerState.RightShoulder2)
return true;
}
// Compare the parts we sync
if (ControllerState.LeftShoulder1 != LastControllerState.LeftShoulder1 || ControllerState.RightShoulder1 != LastControllerState.RightShoulder1 ||
ControllerState.ButtonSquare != LastControllerState.ButtonSquare || ControllerState.ButtonCross != LastControllerState.ButtonCross ||
ControllerState.ButtonCircle != LastControllerState.ButtonCircle || ControllerState.ButtonTriangle != LastControllerState.ButtonTriangle ||
ControllerState.ShockButtonL != LastControllerState.ShockButtonL || ControllerState.m_bPedWalk != LastControllerState.m_bPedWalk)
return true;
// Movement direction buttons change ?
short LeftStickXDelta = abs(ControllerState.LeftStickX - LastControllerState.LeftStickX);
short LeftStickYDelta = abs(ControllerState.LeftStickY - LastControllerState.LeftStickY);
if (LeftStickXDelta > 32 || LeftStickYDelta > 32)
{
// If movement within -127 to -1 or 1 to 127, then apply analog update limits
if ((ControllerState.LeftStickX >= -127 && ControllerState.LeftStickX <= -1 && ControllerState.LeftStickY >= -127 && ControllerState.LeftStickY <= -1 &&
LastControllerState.LeftStickX >= -127 && LastControllerState.LeftStickX <= -1 && LastControllerState.LeftStickY >= -127 &&
LastControllerState.LeftStickY <= -1) ||
(ControllerState.LeftStickX >= 1 && ControllerState.LeftStickX <= 127 && ControllerState.LeftStickY >= 1 && ControllerState.LeftStickY <= 127 &&
LastControllerState.LeftStickX >= 1 && LastControllerState.LeftStickX <= 127 && LastControllerState.LeftStickY >= 1 &&
LastControllerState.LeftStickY <= 127))
{
// Analog movement is restricted to within axis direction.
// - Only continue if KeySyncMove has a shorter update interval than puresync
if (g_TickRateSettings.iKeySyncAnalogMove < g_TickRateSettings.iPureSync)
// Only allow if enough time since last keysync
if (m_TimeSinceMouseOrAnalogStateSent.Get() >= g_TickRateSettings.iKeySyncAnalogMove)
return true;
}
else
return true;
}
// Only continue if KeySyncRotation has a shorter update interval than puresync
if (g_TickRateSettings.iKeySyncRotation < g_TickRateSettings.iPureSync)
{
// Only allow if enough time since last keysync
if (m_TimeSinceMouseOrAnalogStateSent.Get() >= g_TickRateSettings.iKeySyncRotation)
{
// Camera orbiting?
float fCameraRotationDelta = WrapAround(-PI, fLastCameraRotation - pPlayerModel->GetCameraRotation(), PI);
if (abs(fCameraRotationDelta) > 0.05f)
{
if (!pVehicle)
{
// Camera orbiting + Movement direction button held?
if (abs(ControllerState.LeftStickX) > 32)
return true;
if (abs(ControllerState.LeftStickY) > 32)
return true;
}
// Camera orbiting + Aiming button held?
if (ControllerState.RightShoulder1)
return true;
}
// Aiming up and down?
float fAimY = pPlayerModel->GetAim().fY;
float fAimYDelta = WrapAround(-PI, fLastAimY - fAimY, PI);
if (abs(fAimYDelta) > 0.05f)
{
// Aiming up and down + Aiming button held?
if (ControllerState.RightShoulder1)
return true;
}
}
}
return false;
}
bool CNetAPI::IsPureSyncNeeded()
{
unsigned long ulCurrentTime = CClientTime::GetTime();
if (ulCurrentTime >= m_ulLastPuresyncTime + TICK_RATE)
{
m_ulLastPuresyncTime = ulCurrentTime;
return true;
}
return false;
}
bool CNetAPI::IsCameraSyncNeeded()
{
// Camera sync is sent at a constant rate even if there are no changes because the server does not issue a receipt
if (m_CameraSyncTimer.Get() > CAM_SYNC_RATE)
{
m_CameraSyncTimer.Reset();
return true;
}
return false;
}
void CNetAPI::ReadKeysync(CClientPlayer* pPlayer, NetBitStreamInterface& BitStream)
{
// Read out the small keysync data
CControllerState ControllerState;
ReadSmallKeysync(ControllerState, BitStream);
// Read the rotations
SKeysyncRotation rotation;
BitStream.Read(&rotation);
float fPlayerRotation = rotation.data.fPlayerRotation;
float fCameraRotation = rotation.data.fCameraRotation;
// Flags
SKeysyncFlags flags;
BitStream.Read(&flags);
// Grab the occupied vehicle
CClientVehicle* pVehicle = pPlayer->GetOccupiedVehicle();
// If he's shooting or aiming
if (ControllerState.ButtonCircle || ControllerState.RightShoulder1)
{
// Read out his current weapon slot
SWeaponSlotSync slot;
BitStream.Read(&slot);
unsigned int uiSlot = slot.data.uiSlot;
CWeapon* pWeapon = pPlayer->GetWeapon(static_cast<eWeaponSlot>(uiSlot));
// Is the current weapon a goggle (44 or 45) or a camera (43), detonator (40), don't apply the fire key
if (uiSlot == 11 || uiSlot == 12 || (pWeapon && pWeapon->GetType() == 43))
ControllerState.ButtonCircle = 0;
if (CWeaponNames::DoesSlotHaveAmmo(uiSlot))
{
unsigned char ucCurrentWeaponType = 0;
float fWeaponRange = 1.6f;
if (pWeapon)
{
ucCurrentWeaponType = pWeapon->GetType();
float fSkill = pPlayer->GetStat(g_pGame->GetStats()->GetSkillStatIndex(pWeapon->GetType()));
CWeaponStat* pWeaponInfo = g_pGame->GetWeaponStatManager()->GetWeaponStatsFromSkillLevel(pWeapon->GetType(), fSkill);
fWeaponRange = pWeaponInfo->GetWeaponRange();
}
// Read out the weapon ammo
SWeaponAmmoSync ammo(ucCurrentWeaponType, false, true);
BitStream.Read(&ammo);
unsigned short usWeaponAmmo = ammo.data.usAmmoInClip;
// Valid current weapon id? Add it to the change weapon queue
if (CClientPickupManager::IsValidWeaponID(ucCurrentWeaponType))
{
if (pVehicle)
{
// TODO?
}
else
{
pPlayer->AddChangeWeapon(TICK_RATE_AIM, static_cast<eWeaponSlot>(uiSlot), usWeaponAmmo);
}
}
else
{
if (pVehicle)
{
pPlayer->RemoveAllWeapons();
}
else
{
pPlayer->AddChangeWeapon(TICK_RATE_AIM, WEAPONSLOT_TYPE_UNARMED, 1);
}
}
// Make sure that if he doesn't have an akimbo weapon his hands up state is false
if (!IsWeaponIDAkimbo(ucCurrentWeaponType))
{
flags.data.bAkimboTargetUp = false;
}
// Read the weapon aim data
SWeaponAimSync aim(fWeaponRange);
BitStream.Read(&aim);
// Read out the driveby direction
eVehicleAimDirection ucDriveByAim;
BitStream.Read(*reinterpret_cast<char*>(&ucDriveByAim));
// Set the aim data (immediately if in vehicle, otherwize delayed/interpolated)
if (pVehicle)
{
pPlayer->SetAimingData(TICK_RATE_AIM, aim.data.vecTarget, aim.data.fArm, 0.0f, ucDriveByAim, &(aim.data.vecOrigin), false);
}
else
{
pPlayer->SetTargetTarget(TICK_RATE_AIM, aim.data.vecOrigin, aim.data.vecTarget);
pPlayer->SetAimInterpolated(TICK_RATE_AIM, fPlayerRotation, aim.data.fArm, flags.data.bAkimboTargetUp, ucDriveByAim);
}
}
else if (uiSlot != 0)
{
pPlayer->AddChangeWeapon(TICK_RATE_AIM, static_cast<eWeaponSlot>(uiSlot), 1);
}
else
{
pPlayer->SetCurrentWeaponSlot(WEAPONSLOT_TYPE_UNARMED);
}
}
// Are we syncing a vehicle?
if (pVehicle && flags.data.bSyncingVehicle)
{
// Eventually read vehicle specific keysync data
ReadSmallVehicleSpecific(pVehicle, BitStream, pVehicle->GetModel());
if (pVehicle->GetUpgrades()->HasUpgrade(1087)) // Hydraulics?
{
short sRightStickX, sRightStickY;
BitStream.Read(sRightStickX);
BitStream.Read(sRightStickY);
ControllerState.RightStickX = sRightStickX;
ControllerState.RightStickY = sRightStickY;
}
// Jax: temp fix for rhino firing, CPlayerInfo::m_LastTimeBigGunFired needs to be context-switched
if (pVehicle->GetModel() == VT_RHINO)
{
ControllerState.ButtonCircle = 0;
}
if (pVehicle->GetVehicleType() == CLIENTVEHICLE_PLANE || pVehicle->GetVehicleType() == CLIENTVEHICLE_HELI)
{
ControllerState.LeftShoulder2 = 255 * BitStream.ReadBit();
ControllerState.RightShoulder2 = 255 * BitStream.ReadBit();
}
// Apply the new keysync data immediately
pPlayer->SetChoking(false);
}
else
{
// null out the crouch key or it will conflict with the crouch syncing
ControllerState.ShockButtonL = 0;
pPlayer->Duck(flags.data.bIsDucked);
pPlayer->SetChoking(flags.data.bIsChoking);
}
pPlayer->SetControllerState(ControllerState);
pPlayer->SetCameraRotation(fCameraRotation);
pPlayer->SetTargetRotation(fPlayerRotation);
// Increment keysync counter
pPlayer->IncrementKeySync();
}
void CNetAPI::WriteKeysync(CClientPed* pPlayerModel, NetBitStreamInterface& BitStream)
{
// Grab the local controllerstates
CControllerState ControllerState;
pPlayerModel->GetControllerState(ControllerState);
CClientVehicle* pVehicle = pPlayerModel->GetOccupiedVehicle();
// Write them to the bitstream
WriteSmallKeysync(ControllerState, BitStream);
// Write the rotations
SKeysyncRotation rotation;
rotation.data.fPlayerRotation = pPlayerModel->GetCurrentRotation();
rotation.data.fCameraRotation = pPlayerModel->GetCameraRotation();
BitStream.Write(&rotation);
// Flags
SKeysyncFlags flags;
flags.data.bIsDucked = (pPlayerModel->IsDucked() == true);
flags.data.bIsChoking = (pPlayerModel->IsChoking() == true);
flags.data.bAkimboTargetUp = (g_pMultiplayer->GetAkimboTargetUp() == true);
flags.data.bSyncingVehicle = (pVehicle != NULL && pPlayerModel->GetOccupiedVehicleSeat() == 0);
// Write the flags
BitStream.Write(&flags);
// Are we shooting or aiming ?
if (ControllerState.ButtonCircle || ControllerState.RightShoulder1)
{
// Grab the current weapon
CWeapon* pPlayerWeapon = pPlayerModel->GetWeapon();
if (pPlayerWeapon)
{
BitStream.WriteBit(true);
// To confirm weapon type at the other end
unsigned char ucWeaponType = pPlayerWeapon->GetType();
BitStream.Write(ucWeaponType);
// Write the type
unsigned int uiSlot = pPlayerWeapon->GetSlot();
SWeaponSlotSync slot;
slot.data.uiSlot = uiSlot;
BitStream.Write(&slot);
if (CWeaponNames::DoesSlotHaveAmmo(uiSlot))
{
eWeaponType eWeapon = pPlayerWeapon->GetType();
// Write the clip ammo
SWeaponAmmoSync ammo(eWeapon, false, true);
ammo.data.usAmmoInClip = static_cast<unsigned short>(pPlayerWeapon->GetAmmoInClip());
BitStream.Write(&ammo);
// Write the aim data
float fSkill = pPlayerModel->GetStat(g_pGame->GetStats()->GetSkillStatIndex(eWeapon));
CWeaponStat* pCurrentWeaponInfo = g_pGame->GetWeaponStatManager()->GetWeaponStatsFromSkillLevel(eWeapon, fSkill);
float fRange = pCurrentWeaponInfo->GetWeaponRange();
SWeaponAimSync aim(fRange);
if (pVehicle)
pPlayerModel->GetShotData(&aim.data.vecOrigin, &aim.data.vecTarget, NULL, NULL, &aim.data.fArm);
else
pPlayerModel->GetShotData(&aim.data.vecOrigin, &aim.data.vecTarget, NULL, NULL, NULL, &aim.data.fArm);
BitStream.Write(&aim);
// Write the driveby direction
CShotSyncData* pShotsyncData = g_pMultiplayer->GetLocalShotSyncData();
BitStream.Write(static_cast<char>(pShotsyncData->m_cInVehicleAimDirection));
}
}
else
{
BitStream.WriteBit(false);
}
}
// Eventually write vehicle specific stuff.
if (flags.data.bSyncingVehicle)
{
WriteSmallVehicleSpecific(pVehicle, BitStream);
CVehicleUpgrades* pUpgrades = pVehicle->GetUpgrades();
if (pUpgrades)
{
if (pUpgrades->HasUpgrade(1087)) // Hydraulics?
{
BitStream.Write(ControllerState.RightStickX);
BitStream.Write(ControllerState.RightStickY);
}
}
if (pVehicle->GetVehicleType() == CLIENTVEHICLE_PLANE || pVehicle->GetVehicleType() == CLIENTVEHICLE_HELI)
{
BitStream.WriteBit(ControllerState.LeftShoulder2 != 0);
BitStream.WriteBit(ControllerState.RightShoulder2 != 0);
}
}
SetLastSentControllerState(ControllerState, pPlayerModel->GetCameraRotation(), pPlayerModel->GetAim().fY);
}
void CNetAPI::ReadPlayerPuresync(CClientPlayer* pPlayer, NetBitStreamInterface& BitStream)
{
// Read out the sync time context. See CClientEntity for documentation on that.
unsigned char ucSyncTimeContext = 0;
BitStream.Read(ucSyncTimeContext);
// Only update the sync if this packet is from the same context.
if (!pPlayer->CanUpdateSync(ucSyncTimeContext))
{
return;
}
// Read out the time it took for the packet to go from the remote client to the server and to us
unsigned short usLatency;
BitStream.ReadCompressed(usLatency);
pPlayer->SetLatency(usLatency + g_pNet->GetPing());
pPlayer->SetPing(usLatency);
// Read out the keysync data
CControllerState ControllerState;
ReadFullKeysync(ControllerState, BitStream);
// Read out puresync flags
SPlayerPuresyncFlags flags;
BitStream.Read(&flags);
// Set the jetpack and google states
if (flags.data.bHasJetPack != pPlayer->HasJetPack())
pPlayer->SetHasJetPack(flags.data.bHasJetPack);
pPlayer->SetWearingGoggles(flags.data.bWearsGoogles);
CClientEntity* pContactEntity = NULL;
if (flags.data.bHasContact)
{
ElementID Temp;
BitStream.Read(Temp);
pContactEntity = CElementIDs::GetElement(Temp);
}
// Player position
SPositionSync position(false);
BitStream.Read(&position);
// If the players in contact with an object/vehicle, make that the origin
if (pContactEntity)
{
CVector vecTempPos;
pContactEntity->GetPosition(vecTempPos);
position.data.vecPosition += vecTempPos;
}
// Player rotation
SPedRotationSync rotation;
BitStream.Read(&rotation);
// Move speed vector
if (flags.data.bSyncingVelocity)
{
SVelocitySync velocity;
if (BitStream.Read(&velocity))
pPlayer->SetMoveSpeed(velocity.data.vecVelocity);
}
// Player health
SPlayerHealthSync health;
BitStream.Read(&health);
pPlayer->SetHealth(health.data.fValue);
pPlayer->LockHealth(health.data.fValue);
// Player armor
SPlayerArmorSync armor;
BitStream.Read(&armor);
pPlayer->SetArmor(armor.data.fValue);
pPlayer->LockArmor(armor.data.fValue);
// Read the camera rotation (Determines base for left stick movement)
SCameraRotationSync camRotation;
BitStream.Read(&camRotation);
float fCameraRotation = camRotation.data.fRotation;
// Current weapon id
if (flags.data.bHasAWeapon)
{
SWeaponSlotSync slot;
BitStream.Read(&slot);
unsigned int uiSlot = slot.data.uiSlot;
CWeapon* pWeapon = pPlayer->GetWeapon(static_cast<eWeaponSlot>(uiSlot));
// Is the current weapon goggles (44 or 45) or a camera (43), or a detonator (40), don't apply the fire key
if (uiSlot == 11 || uiSlot == 12 || (pWeapon && pWeapon->GetType() == 43))
ControllerState.ButtonCircle = 0;
if (CWeaponNames::DoesSlotHaveAmmo(uiSlot))
{
unsigned char ucCurrentWeapon = 0;
float fWeaponRange = 0.01f;
if (pWeapon)
{
ucCurrentWeapon = pWeapon->GetType();
float fSkill = pPlayer->GetStat(g_pGame->GetStats()->GetSkillStatIndex(pWeapon->GetType()));
CWeaponStat* pWeaponInfo = g_pGame->GetWeaponStatManager()->GetWeaponStatsFromSkillLevel(pWeapon->GetType(), fSkill);
fWeaponRange = pWeaponInfo->GetWeaponRange();
}
// Read out the weapon ammo
SWeaponAmmoSync ammo(ucCurrentWeapon, false, true);
BitStream.Read(&ammo);
unsigned short usWeaponAmmo = ammo.data.usAmmoInClip;
// Valid current weapon id?
if (CClientPickupManager::IsValidWeaponID(ucCurrentWeapon))
{
pPlayer->AddChangeWeapon(TICK_RATE_AIM, static_cast<eWeaponSlot>(uiSlot), usWeaponAmmo);
}
else
{
pPlayer->AddChangeWeapon(TICK_RATE_AIM, WEAPONSLOT_TYPE_UNARMED, 0);
}
// Make sure that if he doesn't have an akimbo weapon his hands up state is false
if (!IsWeaponIDAkimbo(ucCurrentWeapon))
{
flags.data.bAkimboTargetUp = false;
}
// Read out the aim directions
SWeaponAimSync aim(fWeaponRange, (ControllerState.RightShoulder1 || ControllerState.ButtonCircle));
BitStream.Read(&aim);
// Interpolate the aiming
pPlayer->SetAimInterpolated(TICK_RATE_AIM, rotation.data.fRotation, aim.data.fArm, flags.data.bAkimboTargetUp, eVehicleAimDirection::FORWARDS);
// Read the aim data only if he's shooting or aiming
if (aim.isFull())
{
// Interpolate the source/target vectors
pPlayer->SetTargetTarget(TICK_RATE_AIM, aim.data.vecOrigin, aim.data.vecTarget);
}
}
else
{
pPlayer->SetCurrentWeaponSlot(static_cast<eWeaponSlot>(slot.data.uiSlot));
}
}
else
{
// Make him empty-handed
pPlayer->SetCurrentWeaponSlot(WEAPONSLOT_TYPE_UNARMED);
}
// null out the crouch bit or it'll conflict with the crouched syncing
ControllerState.ShockButtonL = 0;
// If the players in contact with an object/vehicle, revert to contact position
if (pContactEntity)
{
CVector vecTempPos;
pContactEntity->GetPosition(vecTempPos);
position.data.vecPosition -= vecTempPos;
}
// Set position and rotation
pPlayer->SetTargetPosition(position.data.vecPosition, TICK_RATE, pContactEntity);
pPlayer->SetTargetRotation(rotation.data.fRotation);
// Set move speed, controller state and camera rotation + duck state
pPlayer->SetControllerState(ControllerState);
pPlayer->SetCameraRotation(fCameraRotation);
pPlayer->Duck(flags.data.bIsDucked);
pPlayer->SetChoking(flags.data.bIsChoking);
pPlayer->SetOnFire(flags.data.bIsOnFire);
pPlayer->SetStealthAiming(flags.data.bStealthAiming);
// Remember now as the last puresync time
pPlayer->SetLastPuresyncTime(CClientTime::GetTime());
pPlayer->SetLastPuresyncPosition(position.data.vecPosition);
pPlayer->IncrementPlayerSync();
pPlayer->SetLastPuresyncType(PURESYNC_TYPE_PURESYNC);
}
void WriteCameraOrientation(const CVector& vecPositionBase, NetBitStreamInterface& BitStream)
{
// Calc the camera position and rotation