-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathCSimVehiclePuresyncPacket.cpp
513 lines (429 loc) · 17.2 KB
/
CSimVehiclePuresyncPacket.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
*
*****************************************************************************/
#include "StdInc.h"
#include "SimHeaders.h"
#include "Utils.h"
#include "CVehicleManager.h"
#include "CWeaponNames.h"
CSimVehiclePuresyncPacket::CSimVehiclePuresyncPacket(ElementID PlayerID, ushort usPlayerLatency, uchar ucPlayerSyncTimeContext, bool bPlayerHasOccupiedVehicle,
ushort usVehicleGotModel, uchar ucPlayerGotOccupiedVehicleSeat, uchar ucPlayerGotWeaponType,
float fPlayerGotWeaponRange, CControllerState& sharedControllerState, uint uiDamageInfoSendPhase,
const SSimVehicleDamageInfo& damageInfo)
: m_PlayerID(PlayerID),
m_usPlayerLatency(usPlayerLatency),
m_ucPlayerSyncTimeContext(ucPlayerSyncTimeContext),
m_bPlayerHasOccupiedVehicle(bPlayerHasOccupiedVehicle),
m_usVehicleGotModel(usVehicleGotModel),
m_ucPlayerGotOccupiedVehicleSeat(ucPlayerGotOccupiedVehicleSeat),
m_ucPlayerGotWeaponType(ucPlayerGotWeaponType),
m_fPlayerGotWeaponRange(fPlayerGotWeaponRange),
m_sharedControllerState(sharedControllerState),
m_uiDamageInfoSendPhase(uiDamageInfoSendPhase),
m_DamageInfo(damageInfo)
{
}
//
// Should do the same this as what CVehiclePuresyncPacket::Read() does
//
bool CSimVehiclePuresyncPacket::Read(NetBitStreamInterface& BitStream)
{
// Player is in a vehicle?
if (m_bPlayerHasOccupiedVehicle)
{
// Read out the time context
if (!BitStream.Read(m_Cache.ucTimeContext))
return false;
// If incoming time context is zero, use the one here
if (m_Cache.ucTimeContext == 0)
m_Cache.ucTimeContext = m_ucPlayerSyncTimeContext;
// Only read this packet if it matches the current time context that
// player is in.
if (!CanUpdateSync(m_Cache.ucTimeContext))
return false;
// Read out the keysync data
if (!ReadFullKeysync(m_sharedControllerState, BitStream))
return false;
// Read out the remote model
if (BitStream.Version() >= 0x05F)
BitStream.Read(m_Cache.iModelID);
else
m_Cache.iModelID = m_usVehicleGotModel;
// Read out its position
SPositionSync position(false);
if (!BitStream.Read(&position))
return false;
m_Cache.PlrPosition = position.data.vecPosition;
if (CVehicleManager::GetVehicleType(m_Cache.iModelID) == VEHICLE_TRAIN)
{
// Train specific data
float fRailPosition = 0.0f;
uchar ucRailTrack = 0;
bool bRailDirection = false;
float fRailSpeed = 0.0f;
if (!BitStream.Read(fRailPosition) || !BitStream.ReadBit(bRailDirection) || !BitStream.Read(ucRailTrack) || !BitStream.Read(fRailSpeed))
{
return false;
}
m_Cache.fRailPosition = fRailPosition;
m_Cache.bRailDirection = bRailDirection;
m_Cache.ucRailTrack = ucRailTrack;
m_Cache.fRailSpeed = fRailSpeed;
}
// Read the camera orientation
ReadCameraOrientation(position.data.vecPosition, BitStream, m_Cache.vecCamPosition, m_Cache.vecCamFwd);
// Jax: don't allow any outdated packets through
SOccupiedSeatSync seat;
if (!BitStream.Read(&seat))
return false;
if (seat.data.ucSeat != m_ucPlayerGotOccupiedVehicleSeat)
{
// Mis-matching seats can happen when we warp into a different one,
// which will screw up the whole packet
return false;
}
// Read out the vehicle matrix only if he's the driver
const unsigned int uiSeat = m_ucPlayerGotOccupiedVehicleSeat;
if (uiSeat == 0)
{
// Read out the vehicle rotation in degrees
SRotationDegreesSync rotation;
if (!BitStream.Read(&rotation))
return false;
// Set it
m_Cache.VehPosition = position.data.vecPosition;
m_Cache.VehRotationDeg = rotation.data.vecRotation;
// Move speed vector
SVelocitySync velocity;
if (!BitStream.Read(&velocity))
return false;
m_Cache.BothVelocity = velocity.data.vecVelocity;
// Turn speed vector
SVelocitySync turnSpeed;
if (!BitStream.Read(&turnSpeed))
return false;
m_Cache.VehTurnSpeed = turnSpeed.data.vecVelocity;
// Health
SVehicleHealthSync health;
if (!BitStream.Read(&health))
return false;
m_Cache.fVehHealth = health.data.fValue;
// Trailer chain
bool bHasTrailer;
if (!BitStream.ReadBit(bHasTrailer))
return false;
while (bHasTrailer)
{
STrailerInfo info;
BitStream.Read(info.m_TrailerID);
// Read out the trailer position and rotation
SPositionSync trailerPosition(false);
if (!BitStream.Read(&trailerPosition))
return false;
SRotationDegreesSync trailerRotation;
if (!BitStream.Read(&trailerRotation))
return false;
// If we found the trailer
if (true)
{
// Set its position and rotation
info.m_TrailerPosition = trailerPosition.data.vecPosition;
info.m_TrailerRotationDeg = trailerRotation.data.vecRotation;
m_Cache.TrailerList.push_back(info);
}
else
break;
if (BitStream.ReadBit(bHasTrailer) == false)
return false;
}
}
// Read Damage info, but do not store, as we do not relay this info
if (BitStream.Version() >= 0x047)
{
if (BitStream.ReadBit() == true)
{
ElementID DamagerID;
if (!BitStream.Read(DamagerID))
return false;
SWeaponTypeSync weaponType;
if (!BitStream.Read(&weaponType))
return false;
SBodypartSync bodyPart;
if (!BitStream.Read(&bodyPart))
return false;
}
}
// Player health
SPlayerHealthSync health;
if (!BitStream.Read(&health))
return false;
m_Cache.fPlrHealth = health.data.fValue;
// Armor
SPlayerArmorSync armor;
if (!BitStream.Read(&armor))
return false;
m_Cache.fArmor = armor.data.fValue;
// Flags
if (!BitStream.Read(&m_Cache.flags))
return false;
// Weapon sync
if (m_Cache.flags.data.bHasAWeapon)
{
SWeaponSlotSync slot;
if (!BitStream.Read(&slot))
return false;
m_Cache.ucWeaponSlot = slot.data.uiSlot;
if (m_Cache.flags.data.bIsDoingGangDriveby && CWeaponNames::DoesSlotHaveAmmo(slot.data.uiSlot))
{
// Read the ammo states
SWeaponAmmoSync ammo(m_ucPlayerGotWeaponType, BitStream.Version() >= 0x44, true);
if (!BitStream.Read(&ammo))
return false;
m_Cache.usAmmoInClip = ammo.data.usAmmoInClip;
m_Cache.usTotalAmmo = ammo.data.usTotalAmmo;
// Read aim data
SWeaponAimSync aim(m_fPlayerGotWeaponRange, true);
if (!BitStream.Read(&aim))
return false;
m_Cache.fAimDirection = aim.data.fArm;
m_Cache.vecSniperSource = aim.data.vecOrigin;
m_Cache.vecTargetting = aim.data.vecTarget;
// Read the driveby direction
SDrivebyDirectionSync driveby;
if (!BitStream.Read(&driveby))
return false;
m_Cache.ucDriveByDirection = driveby.data.ucDirection;
}
}
else
m_Cache.ucWeaponSlot = 0;
// Vehicle specific data if he's the driver
if (uiSeat == 0)
{
ReadVehicleSpecific(BitStream);
}
// Read the vehicle_look_left and vehicle_look_right control states
// if it's an aircraft.
if (m_Cache.flags.data.bIsAircraft)
{
m_sharedControllerState.LeftShoulder2 = BitStream.ReadBit() * 255;
m_sharedControllerState.RightShoulder2 = BitStream.ReadBit() * 255;
}
if (BitStream.Can(eBitStreamVersion::SetElementOnFire))
m_Cache.isOnFire = BitStream.ReadBit();
if (BitStream.Can(eBitStreamVersion::IsVehicleNitroActivated_Serverside))
{
float vehicleNitro;
if (!BitStream.Read(vehicleNitro))
return false;
m_Cache.vehNitroLevel = vehicleNitro;
}
// Success
return true;
}
return false;
}
//
// Should do the same this as what CVehiclePuresyncPacket::Write() does
//
bool CSimVehiclePuresyncPacket::Write(NetBitStreamInterface& BitStream) const
{
// Player is in a vehicle and is the driver?
if (m_bPlayerHasOccupiedVehicle)
{
// Player ID
BitStream.Write(m_PlayerID);
// Write the time context of that player
BitStream.Write(m_Cache.ucTimeContext);
// Write his ping divided with 2 plus a small number so the client can find out when this packet was sent
BitStream.WriteCompressed(m_usPlayerLatency);
// Write the keysync data
WriteFullKeysync(m_sharedControllerState, BitStream);
// Write the serverside model (#8800)
if (BitStream.Version() >= 0x05F)
BitStream.Write(m_Cache.iModelID);
// Write the vehicle matrix only if he's the driver
CVector vecTemp;
unsigned int uiSeat = m_ucPlayerGotOccupiedVehicleSeat;
if (uiSeat == 0)
{
// Vehicle position
SPositionSync position(false);
position.data.vecPosition = m_Cache.VehPosition;
BitStream.Write(&position);
if (m_usVehicleGotModel == 449 || m_usVehicleGotModel == 537 || m_usVehicleGotModel == 538 || m_usVehicleGotModel == 570 ||
m_usVehicleGotModel == 569 || m_usVehicleGotModel == 590)
{
BitStream.Write(m_Cache.fRailPosition);
BitStream.WriteBit(m_Cache.bRailDirection);
BitStream.Write(m_Cache.ucRailTrack);
BitStream.Write(m_Cache.fRailSpeed);
}
// Vehicle rotation
SRotationDegreesSync rotation;
rotation.data.vecRotation = m_Cache.VehRotationDeg;
BitStream.Write(&rotation);
// Move speed vector
SVelocitySync velocity;
velocity.data.vecVelocity = m_Cache.BothVelocity;
BitStream.Write(&velocity);
// Turn speed vector
SVelocitySync turnSpeed;
turnSpeed.data.vecVelocity = m_Cache.VehTurnSpeed;
BitStream.Write(&turnSpeed);
// Health
SVehicleHealthSync health;
health.data.fValue = m_Cache.fVehHealth;
BitStream.Write(&health);
// Write trailer chain
if (BitStream.Version() >= 0x42)
{
for (std::vector<STrailerInfo>::const_iterator it = m_Cache.TrailerList.begin(); it != m_Cache.TrailerList.end(); ++it)
{
BitStream.WriteBit(true);
BitStream.Write(it->m_TrailerID);
SPositionSync trailerPosition(false);
trailerPosition.data.vecPosition = it->m_TrailerPosition;
BitStream.Write(&trailerPosition);
SRotationDegreesSync trailerRotation;
trailerRotation.data.vecRotation = it->m_TrailerRotationDeg;
BitStream.Write(&trailerRotation);
}
BitStream.WriteBit(false);
}
}
// Player health and armor
SPlayerHealthSync health;
health.data.fValue = m_Cache.fPlrHealth;
BitStream.Write(&health);
SPlayerArmorSync armor;
armor.data.fValue = m_Cache.fArmor;
BitStream.Write(&armor);
// Weapon
unsigned char ucWeaponType = m_ucPlayerGotWeaponType;
BitStream.Write(&m_Cache.flags);
// Write the weapon stuff
if (m_Cache.flags.data.bHasAWeapon)
{
// Write the weapon slot
SWeaponSlotSync slot;
slot.data.uiSlot = m_Cache.ucWeaponSlot;
BitStream.Write(&slot);
if (m_Cache.flags.data.bIsDoingGangDriveby && CWeaponNames::DoesSlotHaveAmmo(slot.data.uiSlot))
{
// Write the ammo states
SWeaponAmmoSync ammo(ucWeaponType, BitStream.Version() >= 0x44, true);
ammo.data.usAmmoInClip = m_Cache.usAmmoInClip;
ammo.data.usTotalAmmo = m_Cache.usTotalAmmo;
BitStream.Write(&ammo);
// Sync aim data
SWeaponAimSync aim(0.0f, true);
aim.data.vecOrigin = m_Cache.vecSniperSource;
aim.data.vecTarget = m_Cache.vecTargetting;
aim.data.fArm = m_Cache.fAimDirection;
BitStream.Write(&aim);
// Sync driveby direction
SDrivebyDirectionSync driveby;
driveby.data.ucDirection = m_Cache.ucDriveByDirection;
BitStream.Write(&driveby);
}
}
// Vehicle specific data only if he's the driver
if (uiSeat == 0)
{
WriteVehicleSpecific(BitStream);
}
// Write vehicle_look_left and vehicle_look_right control states when
// it's an aircraft.
if (m_Cache.flags.data.bIsAircraft)
{
BitStream.WriteBit(m_sharedControllerState.LeftShoulder2 != 0);
BitStream.WriteBit(m_sharedControllerState.RightShoulder2 != 0);
}
// Write parts state
if (BitStream.Version() >= 0x5D)
{
SVehicleDamageSyncMethodeB damage;
// Check where we are in the cycle
uint uiPhase = (m_uiDamageInfoSendPhase & 3);
damage.data.bSyncDoors = (uiPhase == 0);
damage.data.bSyncWheels = (uiPhase == 1);
damage.data.bSyncPanels = (uiPhase == 2);
damage.data.bSyncLights = (uiPhase == 3);
damage.data.doors.data.ucStates = m_DamageInfo.m_ucDoorStates;
damage.data.wheels.data.ucStates = m_DamageInfo.m_ucWheelStates;
damage.data.panels.data.ucStates = m_DamageInfo.m_ucPanelStates;
damage.data.lights.data.ucStates = m_DamageInfo.m_ucLightStates;
BitStream.Write(&damage);
}
if (BitStream.Can(eBitStreamVersion::SetElementOnFire))
BitStream.WriteBit(m_Cache.isOnFire);
if (BitStream.Can(eBitStreamVersion::IsVehicleNitroActivated_Serverside))
BitStream.Write(m_Cache.vehNitroLevel);
// Success
return true;
}
return false;
}
void CSimVehiclePuresyncPacket::ReadVehicleSpecific(NetBitStreamInterface& BitStream)
{
if (CVehicleManager::HasTurret(m_Cache.iModelID))
{
// Read out the turret position
SVehicleTurretSync vehicle;
if (!BitStream.Read(&vehicle))
return;
// Set the data
m_Cache.fTurretX = vehicle.data.fTurretX;
m_Cache.fTurretY = vehicle.data.fTurretY;
}
// Adjustable property value
if (CVehicleManager::HasAdjustableProperty(m_Cache.iModelID))
{
unsigned short usAdjustableProperty;
if (BitStream.Read(usAdjustableProperty))
{
m_Cache.usAdjustableProperty = usAdjustableProperty;
}
}
// Door angles.
if (CVehicleManager::HasDoors(m_Cache.iModelID))
{
SDoorOpenRatioSync door;
for (unsigned int i = 2; i < 6; ++i)
{
if (!BitStream.Read(&door))
return;
m_Cache.fDoorOpenRatio[i - 2] = door.data.fRatio;
}
}
}
void CSimVehiclePuresyncPacket::WriteVehicleSpecific(NetBitStreamInterface& BitStream) const
{
// Turret states
if (CVehicleManager::HasTurret(m_Cache.iModelID))
{
SVehicleTurretSync vehicle;
vehicle.data.fTurretX = m_Cache.fTurretX;
vehicle.data.fTurretY = m_Cache.fTurretY;
BitStream.Write(&vehicle);
}
// Adjustable property value
if (CVehicleManager::HasAdjustableProperty(m_Cache.iModelID))
{
BitStream.Write(m_Cache.usAdjustableProperty);
}
// Door angles.
if (CVehicleManager::HasDoors(m_Cache.iModelID))
{
SDoorOpenRatioSync door;
for (unsigned int i = 2; i < 6; ++i)
{
door.data.fRatio = m_Cache.fDoorOpenRatio[i - 2];
BitStream.Write(&door);
}
}
}