-
-
Notifications
You must be signed in to change notification settings - Fork 409
/
CVehicle.cpp
964 lines (820 loc) · 27.3 KB
/
CVehicle.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CVehicle.cpp
* PURPOSE: Vehicle entity class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
extern CGame* g_pGame;
CVehicle::CVehicle(CVehicleManager* pVehicleManager, CElement* pParent, unsigned short usModel, unsigned char ucVariant, unsigned char ucVariant2)
: CElement(pParent)
{
CElementRefManager::AddElementRefs(ELEMENT_REF_DEBUG(this, "CVehicle"), &m_pTowedVehicle, &m_pTowedByVehicle, &m_pSyncer, &m_pJackingPlayer, NULL);
// Init
m_pVehicleManager = pVehicleManager;
m_usModel = usModel;
m_pUpgrades = new CVehicleUpgrades(this);
m_iType = CElement::VEHICLE;
SetTypeName("vehicle");
m_eVehicleType = CVehicleManager::GetVehicleType(m_usModel);
m_fHealth = DEFAULT_VEHICLE_HEALTH;
m_fLastSyncedHealthHealth = DEFAULT_VEHICLE_HEALTH;
m_llIdleTime = CTickCount::Now();
m_fTurretPositionX = 0;
m_fTurretPositionY = 0;
m_bSirenActive = false;
m_bLandingGearDown = true;
m_usAdjustableProperty = 0;
m_bIsFrozen = false;
m_bUnoccupiedSyncable = true;
m_pSyncer = NULL;
GetInitialDoorStates(m_ucDoorStates);
memset(&m_ucWheelStates[0], 0, sizeof(m_ucWheelStates));
memset(&m_ucPanelStates[0], 0, sizeof(m_ucPanelStates));
memset(&m_ucLightStates[0], 0, sizeof(m_ucLightStates));
m_ucOverrideLights = 0;
m_pTowedVehicle = NULL;
m_pTowedByVehicle = NULL;
m_ucPaintjob = 3;
m_ucMaxPassengersOverride = VEHICLE_PASSENGERS_UNDEFINED;
m_pHandlingEntry = NULL;
m_fRespawnHealth = DEFAULT_VEHICLE_HEALTH;
m_bRespawnEnabled = false;
m_ulBlowRespawnInterval = 10000;
m_ulIdleRespawnInterval = 60000;
m_bEngineOn = false;
for (unsigned int i = 0; i < 6; ++i)
m_fDoorOpenRatio[i] = 0.0f;
m_bLocked = false;
m_bDoorsUndamageable = false;
m_bDamageProof = false;
m_bGunsEnabled = false;
m_bFuelTankExplodable = false;
m_bOnGround = true;
m_bSmokeTrail = false;
m_ucAlpha = 255;
m_pJackingPlayer = NULL;
m_bInWater = false;
m_bDerailed = false;
m_bIsDerailable = true;
m_fTrainSpeed = 0.0f;
m_fTrainPosition = 0.0f;
m_ucTrackID = 0;
m_bTaxiLightState = false;
m_bTrainDirection = false;
m_HeadLightColor = SColorRGBA(255, 255, 255, 255);
m_bHeliSearchLightVisible = false;
m_bCollisionsEnabled = true;
m_bHandlingChanged = false;
m_ucVariant = ucVariant;
m_ucVariant2 = ucVariant2;
// Initialize the occupied Players
for (int i = 0; i < MAX_VEHICLE_SEATS; i++)
{
m_pOccupants[i] = NULL;
}
// Add us to the vehicle manager
pVehicleManager->AddToList(this);
// Randomize our color
RandomizeColor();
// Generate a random reg plate
GenerateRegPlate();
// Generate the handling data
GenerateHandlingData();
// Prepare the sirens
RemoveVehicleSirens();
m_tSirenBeaconInfo.m_bOverrideSirens = false;
}
CVehicle::~CVehicle()
{
if (m_pJackingPlayer && m_pJackingPlayer->GetJackingVehicle() == this)
{
if (m_pJackingPlayer->GetVehicleAction() == CPlayer::VEHICLEACTION_JACKING)
{
m_pJackingPlayer->SetVehicleAction(CPlayer::VEHICLEACTION_NONE);
}
m_pJackingPlayer->SetJackingVehicle(NULL);
}
// loop through players and fix their in out state
for (int i = 0; i < MAX_VEHICLE_SEATS; i++)
{
CPed* pPed = m_pOccupants[i];
if (pPed && pPed->IsPlayer())
{
CPlayer* pPlayer = static_cast<CPlayer*>(pPed);
// Is he already getting out?
if (pPlayer->GetVehicleAction() == CPlayer::VEHICLEACTION_EXITING)
{
// Does it have an occupant and is the occupant the requesting player?
unsigned char ucOccupiedSeat = pPlayer->GetOccupiedVehicleSeat();
if (pPlayer == GetOccupant(ucOccupiedSeat))
{
// Mark the player/vehicle as empty
SetOccupant(NULL, ucOccupiedSeat);
pPlayer->SetOccupiedVehicle(NULL, 0);
pPlayer->SetVehicleAction(CPlayer::VEHICLEACTION_NONE);
// Tell everyone he has exited the vehicle
CVehicleInOutPacket Reply(GetID(), ucOccupiedSeat, CGame::VEHICLE_NOTIFY_OUT_RETURN);
Reply.SetSourceElement(pPlayer);
g_pGame->GetPlayerManager()->BroadcastOnlyJoined(Reply);
}
}
}
}
// Unset any tow links
if (m_pTowedVehicle)
m_pTowedVehicle->SetTowedByVehicle(NULL);
if (m_pTowedByVehicle)
m_pTowedByVehicle->SetTowedVehicle(NULL);
// Make sure nobody's syncing us
SetSyncer(NULL);
// Unreference from our occupators
for (unsigned int i = 0; i < MAX_VEHICLE_SEATS; i++)
{
if (m_pOccupants[i])
{
m_pOccupants[i]->SetOccupiedVehicle(NULL, 0);
m_pOccupants[i]->SetVehicleAction(CPed::VEHICLEACTION_NONE);
}
}
delete m_pUpgrades;
delete m_pHandlingEntry;
CElementRefManager::RemoveElementRefs(ELEMENT_REF_DEBUG(this, "CVehicle"), &m_pTowedVehicle, &m_pTowedByVehicle, &m_pSyncer, &m_pJackingPlayer, NULL);
// Notify the vehicle manager that we are not to be respawned anymore if neccessary
if (m_bRespawnEnabled)
m_pVehicleManager->GetRespawnEnabledVehicles().remove(this);
// Remove us from the vehicle manager
Unlink();
}
CElement* CVehicle::Clone(bool* bAddEntity, CResource* pResource)
{
CVehicle* const pTemp = m_pVehicleManager->Create(GetParentEntity(), GetModel(), GetVariant(), GetVariant2());
if (pTemp)
{
CVector vecRotationDegrees;
GetRotationDegrees(vecRotationDegrees);
pTemp->SetRotationDegrees(vecRotationDegrees);
pTemp->SetHealth(GetHealth());
pTemp->SetColor(GetColor());
pTemp->SetUpgrades(GetUpgrades());
pTemp->m_ucDoorStates = m_ucDoorStates;
pTemp->m_ucWheelStates = m_ucWheelStates;
pTemp->m_ucPanelStates = m_ucPanelStates;
pTemp->m_ucLightStates = m_ucLightStates;
}
return pTemp;
}
void CVehicle::Unlink()
{
// Remove us from the vehicle manager
m_pVehicleManager->RemoveFromList(this);
}
bool CVehicle::ReadSpecialData(const int iLine)
{
// Grab the "posX" data
if (!GetCustomDataFloat("posX", m_vecPosition.fX, true))
{
CLogger::ErrorPrintf("Bad/missing 'posX' attribute in <vehicle> (line %d)\n", iLine);
return false;
}
// Grab the "posY" data
if (!GetCustomDataFloat("posY", m_vecPosition.fY, true))
{
CLogger::ErrorPrintf("Bad/missing 'posY' attribute in <vehicle> (line %d)\n", iLine);
return false;
}
// Grab the "posZ" data
if (!GetCustomDataFloat("posZ", m_vecPosition.fZ, true))
{
CLogger::ErrorPrintf("Bad/missing 'posZ' attribute in <vehicle> (line %d)\n", iLine);
return false;
}
// Grab the "rotX", "rotY" and "rotZ" data
GetCustomDataFloat("rotX", m_vecRotationDegrees.fX, true);
GetCustomDataFloat("rotY", m_vecRotationDegrees.fY, true);
GetCustomDataFloat("rotZ", m_vecRotationDegrees.fZ, true);
// Wrap them around 360
m_vecRotationDegrees.fX = WrapAround(m_vecRotationDegrees.fX, 360.0f);
m_vecRotationDegrees.fY = WrapAround(m_vecRotationDegrees.fY, 360.0f);
m_vecRotationDegrees.fZ = WrapAround(m_vecRotationDegrees.fZ, 360.0f);
// Set the respawn matrix
SetRespawnPosition(m_vecPosition);
SetRespawnRotationDegrees(m_vecRotationDegrees);
// Grab the "model" data
int iTemp;
if (GetCustomDataInt("model", iTemp, true))
{
// Is it valid?
if (CVehicleManager::IsValidModel(iTemp))
{
// Remember it and generate a new random color
SetModel(static_cast<unsigned short>(iTemp));
m_usAdjustableProperty = 0;
}
else
{
CLogger::ErrorPrintf("Bad 'model'(%d) id specified in <vehicle> (line %d)\n", iTemp, iLine);
return false;
}
}
else
{
CLogger::ErrorPrintf("Bad/missing 'model' attribute in <vehicle> (line %d)\n", iLine);
return false;
}
// Grab the variant data
if (GetCustomDataInt("variant1", iTemp, true))
m_ucVariant = static_cast<unsigned char>(iTemp);
if (GetCustomDataInt("variant2", iTemp, true))
m_ucVariant2 = static_cast<unsigned char>(iTemp);
if (m_ucVariant == 254 && m_ucVariant2 == 254)
CVehicleManager::GetRandomVariation(m_usModel, m_ucVariant, m_ucVariant2);
// Grab the "turretX" data
if (GetCustomDataFloat("turretX", m_fTurretPositionX, true))
m_fTurretPositionX = ConvertDegreesToRadians(m_fTurretPositionX);
// Grab the "turretY" data
if (GetCustomDataFloat("turretY", m_fTurretPositionY, true))
m_fTurretPositionY = ConvertDegreesToRadians(m_fTurretPositionY);
// Grab the "health" data
if (GetCustomDataFloat("health", m_fHealth, true))
if (m_fHealth < 0.0f)
m_fHealth = 0.0f;
// Grab the "sirens" data
GetCustomDataBool("sirens", m_bSirenActive, true);
// Grab the "landingGearDown" data
if (!GetCustomDataBool("landingGearDown", m_bLandingGearDown, true))
m_bLandingGearDown = true;
// Grab the "locked" data
GetCustomDataBool("locked", m_bLocked, true);
// Grab the "specialState" data
if (GetCustomDataInt("specialState", iTemp, true))
m_usAdjustableProperty = static_cast<unsigned short>(iTemp);
else
m_usAdjustableProperty = 0;
// Grab the "color" data
char szTemp[256];
if (GetCustomDataString("color", szTemp, 256, true))
{
std::vector<SColorRGBA> vecColors;
unsigned char ucCount;
if (ColorStringToRGB(szTemp, SColorRGBA(0, 0, 0, 0), vecColors, ucCount))
{
if (ucCount % 3 == 0)
m_Color.SetRGBColors(vecColors[0], vecColors[1], vecColors[2], vecColors[3]);
else
m_Color.SetPaletteColors(vecColors[0].R, vecColors[0].G, vecColors[0].B, vecColors[1].R);
}
else
{
CLogger::ErrorPrintf("Bad 'color' value specified in <vehicle> (line %u)\n", iLine);
return false;
}
}
// Grab the "headLightColor" data
if (GetCustomDataString("headLightColor", szTemp, 64, true))
{
// Convert it to RGBA
if (!XMLColorToInt(szTemp, m_HeadLightColor.R, m_HeadLightColor.G, m_HeadLightColor.B, m_HeadLightColor.A))
{
CLogger::ErrorPrintf("Bad 'headLightColor' value specified in <vehicle> (line %u)\n", iLine);
return false;
}
}
// Grab the "paintjob" data
if (GetCustomDataInt("paintjob", iTemp, true))
m_ucPaintjob = static_cast<unsigned char>(iTemp);
// Grab the "upgrades" data
if (GetCustomDataString("upgrades", szTemp, 256, true))
{
if (m_pUpgrades)
{
if (stricmp(szTemp, "all") == 0)
m_pUpgrades->AddAllUpgrades();
else
{
bool bTemp = true;
while (char* token = strtok((bTemp) ? szTemp : NULL, ", "))
{
bTemp = false;
unsigned short usUpgrade = static_cast<unsigned short>(atoi(token));
if (CVehicleUpgrades::IsValidUpgrade(usUpgrade))
m_pUpgrades->AddUpgrade(usUpgrade);
}
}
}
}
// Grab the "plate" data
if (GetCustomDataString("plate", szTemp, 9, true))
SetRegPlate(szTemp);
// Grab the "interior" data
if (GetCustomDataInt("interior", iTemp, true))
m_ucInterior = static_cast<unsigned char>(iTemp);
// Grab the "dimension" data
if (GetCustomDataInt("dimension", iTemp, true))
m_usDimension = static_cast<unsigned short>(iTemp);
// Grab the "collisions" data
if (!GetCustomDataBool("collisions", m_bCollisionsEnabled, true))
m_bCollisionsEnabled = true;
// Grab the "alpha" data
if (GetCustomDataInt("alpha", iTemp, true))
m_ucAlpha = static_cast<unsigned char>(iTemp);
// Grab the "frozen" data
GetCustomDataBool("frozen", m_bIsFrozen, true);
// Grab the "taxiLightOn" data
GetCustomDataBool("taxiLightOn", m_bTaxiLightState, true);
// Grab the "engineOn" data
GetCustomDataBool("engineOn", m_bEngineOn, true);
// Grab the "lightsOn" data
bool bLightsOn;
if (GetCustomDataBool("lightsOn", bLightsOn, true))
m_ucOverrideLights = bLightsOn ? 2 : 1;
// Grab the "damageProof" data
GetCustomDataBool("damageProof", m_bDamageProof, true);
// Grab the "explodableFuelTank" data
GetCustomDataBool("explodableFuelTank", m_bFuelTankExplodable, true);
// Grab the "toggleRespawn" data
bool bRespawnOn;
if (GetCustomDataBool("toggleRespawn", bRespawnOn, true))
SetRespawnEnabled(bRespawnOn);
// Grab the "respawnDelay" data
int iRespawnDelay;
if (GetCustomDataInt("respawnDelay", iRespawnDelay, true))
m_ulIdleRespawnInterval = iRespawnDelay;
// Grab the respawn position and rotation data
GetCustomDataFloat("respawnPosX", m_vecRespawnPosition.fX, true);
GetCustomDataFloat("respawnPosY", m_vecRespawnPosition.fY, true);
GetCustomDataFloat("respawnPosZ", m_vecRespawnPosition.fZ, true);
GetCustomDataFloat("respawnRotX", m_vecRespawnRotationDegrees.fX, true);
GetCustomDataFloat("respawnRotY", m_vecRespawnRotationDegrees.fY, true);
GetCustomDataFloat("respawnRotZ", m_vecRespawnRotationDegrees.fZ, true);
return true;
}
void CVehicle::GetMatrix(CMatrix& matrix)
{
CVector vecRotation;
GetRotation(vecRotation);
matrix.SetRotation(vecRotation);
matrix.vPos = GetPosition();
}
void CVehicle::SetMatrix(const CMatrix& matrix)
{
// Set position and rotation from matrix
SetPosition(matrix.vPos);
CVector vecRotation = matrix.GetRotation();
ConvertRadiansToDegreesNoWrap(vecRotation);
SetRotationDegrees(vecRotation);
}
const CVector& CVehicle::GetPosition()
{
// Are we attached to something?
if (m_pAttachedTo)
GetAttachedPosition(m_vecPosition);
return m_vecPosition;
}
void CVehicle::SetPosition(const CVector& vecPosition)
{
// If we're attached to something, dont allow any change
if (m_pAttachedTo)
return;
// Different to where we are now?
if (m_vecPosition != vecPosition)
{
// Update our stored vectors
m_vecPosition = vecPosition;
UpdateSpatialData();
}
}
void CVehicle::GetRotation(CVector& vecRotation)
{
if (m_pAttachedTo)
GetAttachedRotation(vecRotation);
else
{
GetRotationDegrees(vecRotation);
ConvertDegreesToRadians(vecRotation);
}
}
void CVehicle::GetRotationDegrees(CVector& vecRotation)
{
if (m_pAttachedTo)
{
GetAttachedRotation(vecRotation);
ConvertRadiansToDegrees(vecRotation);
}
else
vecRotation = m_vecRotationDegrees;
}
void CVehicle::SetRotationDegrees(const CVector& vecRotation)
{
if (m_pAttachedTo)
return;
m_vecRotationDegrees = vecRotation;
}
void CVehicle::SetModel(unsigned short usModel)
{
if (usModel != m_usModel)
{
m_usModel = usModel;
m_eVehicleType = CVehicleManager::GetVehicleType(m_usModel);
RandomizeColor();
ResetDoors();
CVehicleManager::GetRandomVariation(m_usModel, m_ucVariant, m_ucVariant2);
// Generate new handling data to fit the vehicle
GenerateHandlingData();
}
}
bool CVehicle::HasValidModel()
{
return CVehicleManager::IsValidModel(m_usModel);
}
void CVehicle::SetVariants(unsigned char ucVariant, unsigned char ucVariant2)
{
m_ucVariant = ucVariant;
m_ucVariant2 = ucVariant2;
}
CVehicleColor& CVehicle::RandomizeColor()
{
// Grab a random color for this vehicle and return it
m_Color = m_pVehicleManager->GetRandomColor(m_usModel);
return m_Color;
}
void CVehicle::SetDoorOpenRatio(unsigned char ucDoor, float fRatio)
{
if (ucDoor <= 5)
m_fDoorOpenRatio[ucDoor] = SharedUtil::Clamp(0.0f, fRatio, 1.0f);
}
float CVehicle::GetDoorOpenRatio(unsigned char ucDoor) const
{
return (ucDoor <= 5) ? m_fDoorOpenRatio[ucDoor] : 0.0f;
}
void CVehicle::GetTurretPosition(float& fPositionX, float& fPositionY)
{
fPositionX = m_fTurretPositionX;
fPositionY = m_fTurretPositionY;
}
void CVehicle::SetTurretPosition(float fPositionX, float fPositionY)
{
m_fTurretPositionX = fPositionX;
m_fTurretPositionY = fPositionY;
}
CPed* CVehicle::GetOccupant(unsigned int uiSeat)
{
if (uiSeat < MAX_VEHICLE_SEATS)
{
return m_pOccupants[uiSeat];
}
return NULL;
}
CPed* CVehicle::GetFirstOccupant()
{
// Try finding a seat with a Player in it
unsigned int i = 0;
for (; i < MAX_VEHICLE_SEATS; i++)
{
if (m_pOccupants[i])
{
return m_pOccupants[i];
}
}
// No seats with Players in it
return NULL;
}
CPed* CVehicle::GetController()
{
CPed* pController = m_pOccupants[0];
if (!pController)
{
CVehicle* pCurrentVehicle = this;
CVehicle* pTowedByVehicle = m_pTowedByVehicle;
pController = pCurrentVehicle->GetOccupant(0);
while (pTowedByVehicle)
{
pCurrentVehicle = pTowedByVehicle;
pTowedByVehicle = pCurrentVehicle->GetTowedByVehicle();
CPed* pCurrentDriver = pCurrentVehicle->GetOccupant(0);
if (pCurrentDriver)
pController = pCurrentDriver;
}
}
return pController;
}
bool CVehicle::SetOccupant(CPed* pPed, unsigned int uiSeat)
{
// CPlayer::SetOccupiedVehicle will also call this so we need to prevent an infinite recursive loop
static bool bAlreadySetting = false;
if (!bAlreadySetting)
{
// Set the Player
if (m_pOccupants[uiSeat] != pPed)
{
if (g_pBandwidthSettings->bLightSyncEnabled)
{
if (uiSeat == 0 && m_pOccupants[0] && IS_PLAYER(m_pOccupants[0]))
g_pGame->GetPlayerManager()->BroadcastOnlyJoined(CVehicleResyncPacket(this));
}
m_pOccupants[uiSeat] = pPed;
}
// Make sure the Player record is up to date
if (pPed)
{
bAlreadySetting = true;
pPed->SetOccupiedVehicle(this, uiSeat);
bAlreadySetting = false;
}
// Ensure idle timer is stopped when the vehicle has any occupants
if (GetFirstOccupant())
StopIdleTimer();
return true;
}
return true;
}
void CVehicle::SetSyncer(CPlayer* pPlayer)
{
// Prevent a recursive call loop when setting vehicle's syncer
static bool bAlreadyIn = false;
if (!bAlreadyIn)
{
// Update the last Player if any
bAlreadyIn = true;
if (m_pSyncer)
{
m_pSyncer->RemoveSyncingVehicle(this);
}
// Update the vehicle
if (pPlayer)
{
pPlayer->AddSyncingVehicle(this);
}
bAlreadyIn = false;
// Set it
m_pSyncer = pPlayer;
}
}
unsigned char CVehicle::GetMaxPassengers()
{
return ((m_ucMaxPassengersOverride == VEHICLE_PASSENGERS_UNDEFINED) ? CVehicleManager::GetMaxPassengers(m_usModel) : m_ucMaxPassengersOverride);
}
unsigned char CVehicle::GetFreePassengerSeat()
{
// Grab the max passengers this vehicle can have and check the rage
unsigned char ucMaxPassengers = GetMaxPassengers();
if (ucMaxPassengers < MAX_VEHICLE_SEATS)
{
// Check for free slots
for (int i = 1; i < ucMaxPassengers + 1; i++)
{
if (!m_pOccupants[i])
{
return static_cast<unsigned char>(i);
}
}
}
return 0xFF;
}
void CVehicle::SetUpgrades(CVehicleUpgrades* pUpgrades)
{
delete m_pUpgrades;
m_pUpgrades = new CVehicleUpgrades(this, pUpgrades);
}
bool CVehicle::SetTowedVehicle(CVehicle* pVehicle)
{
if (m_pTowedVehicle)
{
m_pTowedVehicle->m_pTowedByVehicle = NULL;
m_pTowedVehicle = NULL;
}
if (pVehicle)
{
// Are we trying to establish a circular loop? (would freeze everything up)
CVehicle* pCircTestVehicle = pVehicle;
while (pCircTestVehicle)
{
if (pCircTestVehicle == this)
return false;
pCircTestVehicle = pCircTestVehicle->m_pTowedVehicle;
}
pVehicle->m_pTowedByVehicle = this;
}
m_pTowedVehicle = pVehicle;
return true;
}
bool CVehicle::SetTowedByVehicle(CVehicle* pVehicle)
{
if (m_pTowedByVehicle)
{
m_pTowedByVehicle->m_pTowedVehicle = NULL;
m_pTowedByVehicle = NULL;
}
if (pVehicle)
{
// Are we trying to establish a circular loop? (would freeze everything up)
CVehicle* pCircTestVehicle = pVehicle;
while (pCircTestVehicle)
{
if (pCircTestVehicle == this)
return false;
pCircTestVehicle = pCircTestVehicle->m_pTowedByVehicle;
}
pVehicle->m_pTowedVehicle = this;
}
m_pTowedByVehicle = pVehicle;
return true;
}
void CVehicle::SpawnAt(const CVector& vecPosition, const CVector& vecRotation)
{
SetHealth(GetRespawnHealth());
SetIsBlown(false);
StopIdleTimer();
ResetDoorsWheelsPanelsLights();
SetLandingGearDown(true);
SetAdjustableProperty(0);
SetTowedByVehicle(NULL);
AttachTo(NULL);
m_vecTurnSpeed = CVector();
m_vecVelocity = CVector();
m_vecPosition = vecPosition;
m_vecRotationDegrees = vecRotation;
UpdateSpatialData();
}
void CVehicle::Respawn()
{
SpawnAt(m_vecRespawnPosition, m_vecRespawnRotationDegrees);
}
void CVehicle::SetRegPlate(const char* szRegPlate)
{
// Copy the text and make sure non-used chars are nulled.
memset(m_szRegPlate, 0, 9);
STRNCPY(m_szRegPlate, szRegPlate, 9);
}
void CVehicle::GenerateRegPlate()
{
// For all our 8 letters
for (int i = 0; i < 8; i++)
{
// Put a space in letter 5
if (i != 4)
{
// Generate a random letter
int iChar = rand() % 36 + 'A';
if (iChar > 'Z')
{
iChar = iChar - 43;
}
// Put it in the plate
m_szRegPlate[i] = iChar;
}
}
// Null terminator and space in number 5
m_szRegPlate[4] = ' ';
m_szRegPlate[8] = 0;
}
void CVehicle::SetPaintjob(unsigned char ucPaintjob)
{
m_ucPaintjob = ucPaintjob;
}
void CVehicle::GetInitialDoorStates(SFixedArray<unsigned char, MAX_DOORS>& ucOutDoorStates)
{
// Keep the bonet and boot intact
ucOutDoorStates[0] = ucOutDoorStates[1] = DT_DOOR_INTACT;
memset(&ucOutDoorStates[0], DT_DOOR_INTACT, MAX_DOORS);
}
void CVehicle::GenerateHandlingData()
{
// Make a new CHandlingEntry
if (m_pHandlingEntry == NULL)
m_pHandlingEntry = g_pGame->GetHandlingManager()->CreateHandlingData();
// Apply the model handling info
m_pHandlingEntry->ApplyHandlingData(g_pGame->GetHandlingManager()->GetModelHandlingData(static_cast<eVehicleTypes>(m_usModel)));
m_bHandlingChanged = false;
}
void CVehicle::SetVehicleSirenPosition(unsigned char ucSirenID, CVector vecPos)
{
m_tSirenBeaconInfo.m_tSirenInfo[ucSirenID].m_vecSirenPositions = vecPos;
}
void CVehicle::SetVehicleSirenMinimumAlpha(unsigned char ucSirenID, DWORD dwPercentage)
{
m_tSirenBeaconInfo.m_tSirenInfo[ucSirenID].m_dwMinSirenAlpha = dwPercentage;
}
void CVehicle::SetVehicleSirenColour(unsigned char ucSirenID, SColor tVehicleSirenColour)
{
m_tSirenBeaconInfo.m_tSirenInfo[ucSirenID].m_RGBBeaconColour = tVehicleSirenColour;
}
void CVehicle::SetVehicleFlags(bool bEnable360, bool bEnableRandomiser, bool bEnableLOSCheck, bool bEnableSilent)
{
m_tSirenBeaconInfo.m_b360Flag = bEnable360;
m_tSirenBeaconInfo.m_bDoLOSCheck = bEnableLOSCheck;
m_tSirenBeaconInfo.m_bUseRandomiser = bEnableRandomiser;
m_tSirenBeaconInfo.m_bSirenSilent = bEnableSilent;
}
void CVehicle::RemoveVehicleSirens()
{
for (int i = 0; i <= 7; i++)
{
m_tSirenBeaconInfo.m_tSirenInfo[i] = SSirenBeaconInfo();
SetVehicleSirenPosition(i, CVector(0, 0, 0));
SetVehicleSirenMinimumAlpha(i, 0);
SetVehicleSirenColour(i, SColor());
}
m_tSirenBeaconInfo.m_ucSirenCount = 0;
}
void CVehicle::ResetDoors()
{
GetInitialDoorStates(m_ucDoorStates);
for (unsigned int i = 0; i < 6; ++i)
m_fDoorOpenRatio[i] = 0.0f;
}
void CVehicle::ResetDoorsWheelsPanelsLights()
{
ResetDoors();
memset(&m_ucWheelStates[0], 0, sizeof(m_ucWheelStates));
memset(&m_ucPanelStates[0], 0, sizeof(m_ucPanelStates));
memset(&m_ucLightStates[0], 0, sizeof(m_ucLightStates));
}
// For blow respawn timer
void CVehicle::SetIsBlown(bool bBlown)
{
if (!bBlown)
m_llBlowTime = CTickCount(0LL);
else
m_llBlowTime = CTickCount::Now();
}
bool CVehicle::GetIsBlown()
{
return m_llBlowTime.ToLongLong() != 0;
}
bool CVehicle::IsBlowTimerFinished()
{
return GetIsBlown() && CTickCount::Now() > m_llBlowTime + CTickCount((long long)m_ulBlowRespawnInterval);
}
void CVehicle::StopIdleTimer()
{
m_llIdleTime = CTickCount(0LL);
}
void CVehicle::RestartIdleTimer()
{
m_llIdleTime = CTickCount::Now();
}
bool CVehicle::IsIdleTimerRunning()
{
return m_llIdleTime.ToLongLong() != 0;
}
bool CVehicle::IsIdleTimerFinished()
{
return IsIdleTimerRunning() && CTickCount::Now() > m_llIdleTime + CTickCount((long long)m_ulIdleRespawnInterval);
}
// Check if vehicle has not moved (much) since the last call
bool CVehicle::IsStationary()
{
const CVector& vecPosition = GetPosition();
if ((vecPosition - m_vecStationaryCheckPosition).LengthSquared() < 0.1f * 0.1f)
return true;
m_vecStationaryCheckPosition = vecPosition;
return false;
}
void CVehicle::SetJackingPlayer(CPlayer* pPlayer)
{
if (pPlayer == m_pJackingPlayer)
return;
// Remove old
if (m_pJackingPlayer)
{
CPlayer* pPrev = m_pJackingPlayer;
m_pJackingPlayer = NULL;
pPrev->SetJackingVehicle(NULL);
}
// Set new
m_pJackingPlayer = pPlayer;
if (m_pJackingPlayer)
m_pJackingPlayer->SetJackingVehicle(this);
}
void CVehicle::OnRelayUnoccupiedSync()
{
// Detect dimension change
m_bNeedsDimensionResync |= (GetDimension() != m_usLastUnoccupiedSyncDimension);
m_usLastUnoccupiedSyncDimension = GetDimension();
}
void CVehicle::HandleDimensionResync()
{
if (m_bNeedsDimensionResync)
{
// Unoccupied vehicle might be desynced because of dimension optimizations, so resync to players in new dimension
g_pGame->GetPlayerManager()->BroadcastDimensionOnlyJoined(CVehicleResyncPacket(this), GetDimension());
m_bNeedsDimensionResync = false;
}
}
void CVehicle::SetRespawnEnabled(bool bEnabled)
{
// If we changed the state, update the internal var and notify the vehicle manager
if (bEnabled != m_bRespawnEnabled)
{
if (bEnabled)
m_pVehicleManager->GetRespawnEnabledVehicles().push_back(this);
else
m_pVehicleManager->GetRespawnEnabledVehicles().remove(this);
m_bRespawnEnabled = bEnabled;
}
}