/
CWorldSA.cpp
1358 lines (1244 loc) · 52.6 KB
/
CWorldSA.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: game_sa/CWorldSA.cpp
* PURPOSE: Game world/entity logic
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
CWorldSA::CWorldSA()
{
m_pBuildingRemovals = new std::multimap<unsigned short, SBuildingRemoval*>;
m_pDataBuildings = new std::multimap<unsigned short, sDataBuildingRemovalItem*>;
m_pBinaryBuildings = new std::multimap<unsigned short, sBuildingRemovalItem*>;
InstallHooks();
}
void HOOK_FallenPeds();
void HOOK_FallenCars();
void CWorldSA::InstallHooks(void)
{
HookInstall(0x565CB0, (DWORD)HOOK_FallenPeds, 5);
HookInstall(0x565E80, (DWORD)HOOK_FallenCars, 5);
}
DWORD CONTINUE_CWorld_FallenPeds = 0x00565CBA;
DWORD CONTINUE_CWorld_FallenCars = 0x00565E8A;
void _declspec(naked) HOOK_FallenPeds()
{
if (pGame && pGame->IsUnderWorldWarpEnabled())
{
_asm
{
sub esp, 2Ch
push ebx
mov ebx, ds:0B74490h
jmp CONTINUE_CWorld_FallenPeds
}
}
else
{
_asm
{
ret
}
}
}
void _declspec(naked) HOOK_FallenCars()
{
if (pGame && !pGame->IsUnderWorldWarpEnabled())
{
_asm
{
sub esp, 2Ch
push ebx
mov ebx, ds:0B74494h
jmp CONTINUE_CWorld_FallenCars
}
}
else
{
_asm
{
ret
}
}
}
void CWorldSA::Add(CEntity* pEntity, eDebugCaller CallerId)
{
DEBUG_TRACE("VOID CWorldSA::Add ( CEntity * pEntity )");
CEntitySA* pEntitySA = dynamic_cast<CEntitySA*>(pEntity);
if (pEntitySA)
{
CEntitySAInterface* pInterface = pEntitySA->GetInterface();
if ((DWORD)pInterface->vtbl == VTBL_CPlaceable)
{
SString strMessage("Caller: %i ", CallerId);
LogEvent(506, "CWorld::Add ( CEntity * ) Crash", "", strMessage);
}
DWORD dwEntity = (DWORD)pEntitySA->GetInterface();
DWORD dwFunction = FUNC_Add;
_asm
{
push dwEntity
call dwFunction
add esp, 4
}
}
}
void CWorldSA::Add(CEntitySAInterface* entityInterface, eDebugCaller CallerId)
{
DEBUG_TRACE("VOID CWorldSA::Add ( CEntitySAInterface * entityInterface )");
DWORD dwFunction = FUNC_Add;
if ((DWORD)entityInterface->vtbl == VTBL_CPlaceable)
{
SString strMessage("Caller: %i ", CallerId);
LogEvent(506, "CWorld::Add ( CEntitySAInterface * ) Crash", "", strMessage);
}
_asm
{
push entityInterface
call dwFunction
add esp, 4
}
}
void CWorldSA::Remove(CEntity* pEntity, eDebugCaller CallerId)
{
DEBUG_TRACE("VOID CWorldSA::Remove ( CEntity * entity )");
CEntitySA* pEntitySA = dynamic_cast<CEntitySA*>(pEntity);
if (pEntitySA)
{
CEntitySAInterface* pInterface = pEntitySA->GetInterface();
if ((DWORD)pInterface->vtbl == VTBL_CPlaceable)
{
SString strMessage("Caller: %i ", CallerId);
LogEvent(507, "CWorld::Remove ( CEntity * ) Crash", "", strMessage);
}
DWORD dwEntity = (DWORD)pInterface;
DWORD dwFunction = FUNC_Remove;
_asm
{
push dwEntity
call dwFunction
add esp, 4
}
}
}
void CWorldSA::Remove(CEntitySAInterface* entityInterface, eDebugCaller CallerId)
{
DEBUG_TRACE("VOID CWorldSA::Remove ( CEntitySAInterface * entityInterface )");
if ((DWORD)entityInterface->vtbl == VTBL_CPlaceable)
{
SString strMessage("Caller: %i ", CallerId);
LogEvent(507, "CWorld::Remove ( CEntitySAInterface * ) Crash", "", strMessage);
}
DWORD dwFunction = FUNC_Remove;
_asm
{
push entityInterface
call dwFunction
add esp, 4
/* mov ecx, entityInterface
mov esi, [ecx]
push 1
call dword ptr [esi+8]*/
}
}
void CWorldSA::RemoveReferencesToDeletedObject(CEntitySAInterface* entity)
{
DWORD dwFunc = FUNC_RemoveReferencesToDeletedObject;
DWORD dwEntity = (DWORD)entity;
_asm
{
push dwEntity
call dwFunc
add esp, 4
}
}
bool CWorldSA::TestLineSphere(CVector* vecStart, CVector* vecEnd, CVector* vecSphereCenter, float fSphereRadius, CColPoint** colCollision)
{
// THIS FUNCTION IS INCOMPLETE AND SHOULD NOT BE USED
// Create a CColLine for us
DWORD dwFunc = FUNC_CColLine_Constructor;
DWORD dwCColLine[10]; // I don't know how big CColLine is, so we'll just be safe
_asm
{
lea ecx, dwCColLine
push vecEnd
push vecStart
call dwFunc
}
// Now, lets make a CColSphere
BYTE byteColSphere[18]; // looks like its 18 bytes { vecPos, fSize, byteUnk, byteUnk, byteUnk }
dwFunc = FUNC_CColSphere_Set;
_asm
{
lea ecx, byteColSphere
push 255
push 0
push 0
push vecSphereCenter
push fSphereRadius
call dwFunc
}
}
void ConvertMatrixToEulerAngles(const CMatrix_Padded& matrixPadded, float& fX, float& fY, float& fZ)
{
// Convert the given matrix to a padded matrix
// CMatrix_Padded matrixPadded ( Matrix );
// Grab its pointer and call gta's func
const CMatrix_Padded* pMatrixPadded = &matrixPadded;
DWORD dwFunc = FUNC_CMatrix__ConvertToEulerAngles;
float* pfX = &fX;
float* pfY = &fY;
float* pfZ = &fZ;
int iUnknown = 21;
_asm
{
push iUnknown
push pfZ
push pfY
push pfX
mov ecx, pMatrixPadded
call dwFunc
}
}
bool CWorldSA::ProcessLineOfSight(const CVector* vecStart, const CVector* vecEnd, CColPoint** colCollision, CEntity** CollisionEntity,
const SLineOfSightFlags flags, SLineOfSightBuildingResult* pBuildingResult)
{
DEBUG_TRACE("VOID CWorldSA::ProcessLineOfSight(CVector * vecStart, CVector * vecEnd, CColPoint * colCollision, CEntity * CollisionEntity)");
DWORD dwPadding[100]; // stops the function missbehaving and overwriting the return address
dwPadding[0] = 0; // prevent the warning and eventual compiler optimizations from removing it
CColPointSA* pColPointSA = new CColPointSA();
CColPointSAInterface* pColPointSAInterface = pColPointSA->GetInterface();
// DWORD targetEntity;
CEntitySAInterface* targetEntity = NULL;
bool bReturn = false;
DWORD dwFunc = FUNC_ProcessLineOfSight;
// bool bCheckBuildings = true, bool bCheckVehicles = true, bool bCheckPeds = true,
// bool bCheckObjects = true, bool bCheckDummies = true, bool bSeeThroughStuff = false,
// bool bIgnoreSomeObjectsForCamera = false, bool bShootThroughStuff = false
MemPutFast<BYTE>(VAR_CWorld_bIncludeCarTires, flags.bCheckCarTires);
_asm
{
push flags.bShootThroughStuff
push flags.bIgnoreSomeObjectsForCamera
push flags.bSeeThroughStuff
push flags.bCheckDummies
push flags.bCheckObjects
push flags.bCheckPeds
push flags.bCheckVehicles
push flags.bCheckBuildings
lea eax, targetEntity
push eax
push pColPointSAInterface
push vecEnd
push vecStart
call dwFunc
mov bReturn, al
add esp, 0x30
}
MemPutFast<BYTE>(VAR_CWorld_bIncludeCarTires, 0);
// Building info needed?
if (pBuildingResult)
{
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
if (pPools)
{
if (targetEntity && targetEntity->nType == ENTITY_TYPE_BUILDING)
{
pBuildingResult->bValid = true;
pBuildingResult->usModelID = targetEntity->m_nModelIndex;
if (targetEntity->m_pLod)
pBuildingResult->usLODModelID = targetEntity->m_pLod->m_nModelIndex;
else
pBuildingResult->usLODModelID = 0;
pBuildingResult->pInterface = targetEntity;
pBuildingResult->vecPosition = targetEntity->Placeable.m_transform.m_translate;
if (targetEntity->Placeable.matrix)
{
CVector& vecRotation = pBuildingResult->vecRotation;
ConvertMatrixToEulerAngles(*targetEntity->Placeable.matrix, vecRotation.fX, vecRotation.fY, vecRotation.fZ);
vecRotation = -vecRotation;
}
}
if (targetEntity && targetEntity->nType == ENTITY_TYPE_OBJECT)
{
pBuildingResult->bValid = true;
pBuildingResult->usModelID = targetEntity->m_nModelIndex;
if (targetEntity->m_pLod)
pBuildingResult->usLODModelID = targetEntity->m_pLod->m_nModelIndex;
else
pBuildingResult->usLODModelID = 0;
pBuildingResult->pInterface = targetEntity;
}
}
}
if (CollisionEntity)
{
CPoolsSA* pPools = ((CPoolsSA*)pGame->GetPools());
if (pPools)
{
if (targetEntity)
{
switch (targetEntity->nType)
{
case ENTITY_TYPE_PED:
*CollisionEntity = pPools->GetPed((DWORD*)targetEntity);
break;
case ENTITY_TYPE_OBJECT:
*CollisionEntity = pPools->GetObject((DWORD*)targetEntity);
break;
case ENTITY_TYPE_VEHICLE:
*CollisionEntity = pPools->GetVehicle((DWORD*)targetEntity);
break;
}
/*CEntitySA * entity = new CEntitySA();
entity->SetInterface((CEntitySAInterface *)targetEntity);
eEntityType EntityType = entity->GetEntityType();
delete entity;
switch(EntityType)
{
case ENTITY_TYPE_PED:
case ENTITY_TYPE_OBJECT:
*CollisionEntity = pPools->GetPed((DWORD *)targetEntity);
if ( *CollisionEntity )
break;
*CollisionEntity = pPools->GetObject((CObjectSAInterface *)targetEntity);
break;
case ENTITY_TYPE_VEHICLE:
*CollisionEntity = pPools->GetVehicle((CVehicleSAInterface *)targetEntity);
break;
}*/
}
}
}
if (colCollision)
*colCollision = pColPointSA;
else
pColPointSA->Destroy();
return bReturn;
}
void CWorldSA::IgnoreEntity(CEntity* pEntity)
{
DEBUG_TRACE("VOID CWorldSA::IgnoreEntity(CEntity * entity)");
CEntitySA* pEntitySA = dynamic_cast<CEntitySA*>(pEntity);
if (pEntitySA)
MemPutFast<DWORD>(VAR_IgnoredEntity, (DWORD)pEntitySA->GetInterface());
else
MemPutFast<DWORD>(VAR_IgnoredEntity, 0);
}
// technically this is in CTheZones
BYTE CWorldSA::GetLevelFromPosition(CVector* vecPosition)
{
DEBUG_TRACE("BYTE CWorldSA::GetLevelFromPosition(CVector * vecPosition)");
DWORD dwFunc = FUNC_GetLevelFromPosition;
BYTE bReturn = 0;
_asm
{
push vecPosition
call dwFunc
mov bReturn, al
pop eax
}
return bReturn;
}
float CWorldSA::FindGroundZForPosition(float fX, float fY)
{
DEBUG_TRACE("FLOAT CWorldSA::FindGroundZForPosition(FLOAT fX, FLOAT fY)");
DWORD dwFunc = FUNC_FindGroundZFor3DCoord;
FLOAT fReturn = 0;
_asm
{
push fY
push fX
call dwFunc
fstp fReturn
add esp, 8
}
return fReturn;
}
float CWorldSA::FindGroundZFor3DPosition(CVector* vecPosition)
{
DEBUG_TRACE("FLOAT CWorldSA::FindGroundZFor3DPosition(CVector * vecPosition)");
DWORD dwFunc = FUNC_FindGroundZFor3DCoord;
FLOAT fReturn = 0;
FLOAT fX = vecPosition->fX;
FLOAT fY = vecPosition->fY;
FLOAT fZ = vecPosition->fZ;
_asm
{
push 0
push 0
push fZ
push fY
push fX
call dwFunc
fstp fReturn
add esp, 0x14
}
return fReturn;
}
void CWorldSA::LoadMapAroundPoint(CVector* vecPosition, FLOAT fRadius)
{
DEBUG_TRACE("VOID CWorldSA::LoadMapAroundPoint(CVector * vecPosition, FLOAT fRadius)");
DWORD dwFunc = FUNC_CTimer_Stop;
_asm
{
call dwFunc
}
dwFunc = FUNC_CRenderer_RequestObjectsInDirection;
_asm
{
push 32
push fRadius
push vecPosition
call dwFunc
add esp, 12
}
dwFunc = FUNC_CStreaming_LoadScene;
_asm
{
push vecPosition
call dwFunc
add esp, 4
}
dwFunc = FUNC_CTimer_Update;
_asm
{
call dwFunc
}
}
bool CWorldSA::IsLineOfSightClear(const CVector* vecStart, const CVector* vecEnd, const SLineOfSightFlags flags)
{
DWORD dwFunc = FUNC_IsLineOfSightClear;
bool bReturn = false;
// bool bCheckBuildings = true, bool bCheckVehicles = true, bool bCheckPeds = true,
// bool bCheckObjects = true, bool bCheckDummies = true, bool bSeeThroughStuff = false,
// bool bIgnoreSomeObjectsForCamera = false
_asm
{
push flags.bIgnoreSomeObjectsForCamera
push flags.bSeeThroughStuff
push flags.bCheckDummies
push flags.bCheckObjects
push flags.bCheckPeds
push flags.bCheckVehicles
push flags.bCheckBuildings
push vecEnd
push vecStart
call dwFunc
mov bReturn, al
add esp, 0x24
}
return bReturn;
}
bool CWorldSA::HasCollisionBeenLoaded(CVector* vecPosition)
{
DWORD dwFunc = FUNC_HasCollisionBeenLoaded;
bool bRet = false;
_asm
{
push 0
push vecPosition
call dwFunc
mov bRet, al
add esp, 8
}
return bRet;
}
DWORD CWorldSA::GetCurrentArea(void)
{
return *(DWORD*)VAR_currArea;
}
void CWorldSA::SetCurrentArea(DWORD dwArea)
{
MemPutFast<DWORD>(VAR_currArea, dwArea);
DWORD dwFunc = FUNC_RemoveBuildingsNotInArea;
_asm
{
push dwArea
call dwFunc
add esp, 4
}
}
void CWorldSA::SetJetpackMaxHeight(float fHeight)
{
MemPut<float>(VAR_fJetpackMaxHeight, fHeight);
}
float CWorldSA::GetJetpackMaxHeight(void)
{
return *(float*)(VAR_fJetpackMaxHeight);
}
void CWorldSA::SetAircraftMaxHeight(float fHeight)
{
g_pCore->GetMultiplayer()->SetAircraftMaxHeight(fHeight);
}
float CWorldSA::GetAircraftMaxHeight(void)
{
return g_pCore->GetMultiplayer()->GetAircraftMaxHeight();
}
void CWorldSA::SetAircraftMaxVelocity(float fVelocity)
{
g_pCore->GetMultiplayer()->SetAircraftMaxVelocity(fVelocity);
}
float CWorldSA::GetAircraftMaxVelocity(void)
{
return g_pCore->GetMultiplayer()->GetAircraftMaxVelocity();
}
void CWorldSA::SetOcclusionsEnabled(bool bEnabled)
{
if (!bEnabled)
{
MemPut<BYTE>(FUNC_COcclusion_ProcessBeforeRendering, 0xC3); // retn
MemPutFast<int>(VAR_COcclusion_NumActiveOccluders, 0);
MemCpy((void*)CALL_CCullZones_FindTunnelAttributesForCoors, "\xB8\x80\x28\x00\x00", 5); // mov eax, 0x2880
}
else
{
MemPut<BYTE>(FUNC_COcclusion_ProcessBeforeRendering, 0x51); // Standard value
MemCpy((void*)CALL_CCullZones_FindTunnelAttributesForCoors, "\xE8\xDE\x82\x1D\x00", 5); // call 0x72D9F0
}
}
bool CWorldSA::GetOcclusionsEnabled(void)
{
if (*(BYTE*)FUNC_COcclusion_ProcessBeforeRendering == 0x51) // Is standard value ?
return true;
return false;
}
void CWorldSA::FindWorldPositionForRailTrackPosition(float fRailTrackPosition, int iTrackId, CVector* pOutVecPosition)
{
DWORD dwFunc = FUNC_CWorld_FindPositionForTrackPosition; // __cdecl
_asm
{
push pOutVecPosition
push iTrackId
push fRailTrackPosition
call dwFunc
add esp, 3*4
}
}
int CWorldSA::FindClosestRailTrackNode(const CVector& vecPosition, uchar& ucOutTrackId, float& fOutRailDistance)
{
// Original function @ 0x6F7550
int iNodeId = -1;
float fMinDistance = 99999.898f;
int* aNumTrackNodes = (int*)ARRAY_NumRailTrackNodes;
SRailNodeSA** aTrackNodes = (SRailNodeSA**)ARRAY_RailTrackNodePointers;
uchar ucDesiredTrackId = ucOutTrackId;
for (uchar ucTrackId = 0; ucTrackId < NUM_RAILTRACKS; ++ucTrackId)
{
if ((ucDesiredTrackId == 0xFF || ucTrackId == ucDesiredTrackId) && aNumTrackNodes[ucTrackId] > 0)
{
for (int i = 0; i < aNumTrackNodes[ucTrackId]; ++i)
{
SRailNodeSA& railNode = aTrackNodes[ucTrackId][i];
float fDistance = sqrt(pow(vecPosition.fZ - railNode.sZ * 0.125f, 2) + pow(vecPosition.fY - railNode.sY * 0.125f, 2) +
pow(vecPosition.fX - railNode.sX * 0.125f, 2));
if (fDistance < fMinDistance)
{
fMinDistance = fDistance;
iNodeId = i;
ucOutTrackId = ucTrackId;
}
}
}
}
// Read rail distance
fOutRailDistance = aTrackNodes[ucOutTrackId][iNodeId].sRailDistance * 3.33333334f;
return iNodeId;
/*DWORD dwFunc = FUNC_GetTrainNodeNearPoint; // __cdecl
int iNodeId;
*pOutTrackId = 0;
float fX = vecPosition.fX;
float fY = vecPosition.fY;
float fZ = vecPosition.fZ;
_asm
{
push pOutTrackId
push fZ
push fY
push fZ
call dwFunc
mov iNodeId, eax
add esp, 4*4
}
return iNodeId;*/
}
void CWorldSA::RemoveBuilding(unsigned short usModelToRemove, float fRange, float fX, float fY, float fZ, char cInterior, uint* pOutAmount)
{
// New building Removal
SBuildingRemoval* pRemoval = new SBuildingRemoval();
pRemoval->m_usModel = usModelToRemove;
pRemoval->m_vecPos.fX = fX;
pRemoval->m_vecPos.fY = fY;
pRemoval->m_vecPos.fZ = fZ;
pRemoval->m_fRadius = fRange;
pRemoval->m_cInterior = cInterior;
// Push it to the back of the removal list
m_pBuildingRemovals->insert(std::pair<unsigned short, SBuildingRemoval*>(usModelToRemove, pRemoval));
bool bFound = false;
uint uiAmount = 0;
// Init loop variables
std::pair<std::multimap<unsigned short, sDataBuildingRemovalItem*>::iterator, std::multimap<unsigned short, sDataBuildingRemovalItem*>::iterator>
iterators = m_pDataBuildings->equal_range(usModelToRemove);
std::multimap<unsigned short, sDataBuildingRemovalItem*>::const_iterator iter = iterators.first;
for (; iter != iterators.second; ++iter)
{
sDataBuildingRemovalItem* pFind = (*iter).second;
if (pFind)
{
// if the count is <= 0 and the interface is valid check the distance in case we found a removal (count is used to store if we have already removed
// this once)
if (pFind->m_iCount <= 0 && pFind->m_pInterface)
{
// Grab distances across each axis
float fDistanceX = fX - pFind->m_pInterface->Placeable.m_transform.m_translate.fX;
float fDistanceY = fY - pFind->m_pInterface->Placeable.m_transform.m_translate.fY;
float fDistanceZ = fZ - pFind->m_pInterface->Placeable.m_transform.m_translate.fZ;
if (pFind->m_pInterface->Placeable.matrix != NULL)
{
fDistanceX = fX - pFind->m_pInterface->Placeable.matrix->vPos.fX;
fDistanceY = fY - pFind->m_pInterface->Placeable.matrix->vPos.fY;
fDistanceZ = fZ - pFind->m_pInterface->Placeable.matrix->vPos.fZ;
}
// Square root 'em
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
// Is it in range
if (fDistance <= fRange && (pFind->m_pInterface->m_areaCode == cInterior || cInterior == -1))
{
CEntitySAInterface* pInterface = pFind->m_pInterface;
// while ( pInterface && pInterface != NULL )
// if the interface is valid
if (pInterface && pInterface != NULL)
{
// if the building type is dummy or building and it's not already being removed
if ((pInterface->nType == ENTITY_TYPE_BUILDING || pInterface->nType == ENTITY_TYPE_DUMMY || pInterface->nType == ENTITY_TYPE_OBJECT) &&
pInterface->bRemoveFromWorld != 1)
{
if ((DWORD)(pInterface->vtbl) != VTBL_CPlaceable)
{
// Add the Data Building to the list
pRemoval->AddDataBuilding(pInterface);
// Remove the model from the world
Remove(pInterface, BuildingRemoval2);
m_pRemovedEntities[(DWORD)pInterface] = true;
bFound = true;
}
}
// Get next LOD ( LOD's can have LOD's so we keep checking pInterface )
// pInterface = pInterface->m_pLod;
}
// Set the count.
pFind->m_iCount = 1;
}
}
}
}
std::pair<std::multimap<unsigned short, sBuildingRemovalItem*>::iterator, std::multimap<unsigned short, sBuildingRemovalItem*>::iterator> iteratorsBinary =
m_pBinaryBuildings->equal_range(usModelToRemove);
std::multimap<unsigned short, sBuildingRemovalItem*>::const_iterator iterBinary = iteratorsBinary.first;
for (; iterBinary != iteratorsBinary.second; ++iterBinary)
{
sBuildingRemovalItem* pFindBinary = (*iterBinary).second;
if (pFindBinary)
{
// if the count is <= 0 and the interface is valid check the distance in case we found a removal (count is used to store if we have already removed
// this once)
if (pFindBinary->m_iCount <= 0 && pFindBinary->m_pInterface)
{
// Grab distances across each axis
float fDistanceX = fX - pFindBinary->m_pInterface->Placeable.m_transform.m_translate.fX;
float fDistanceY = fY - pFindBinary->m_pInterface->Placeable.m_transform.m_translate.fY;
float fDistanceZ = fZ - pFindBinary->m_pInterface->Placeable.m_transform.m_translate.fZ;
if (pFindBinary->m_pInterface->Placeable.matrix != NULL)
{
fDistanceX = fX - pFindBinary->m_pInterface->Placeable.matrix->vPos.fX;
fDistanceY = fY - pFindBinary->m_pInterface->Placeable.matrix->vPos.fY;
fDistanceZ = fZ - pFindBinary->m_pInterface->Placeable.matrix->vPos.fZ;
}
// Square root 'em
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
// Is it in range
if (fDistance <= fRange && (pFindBinary->m_pInterface->m_areaCode == cInterior || cInterior == -1))
{
CEntitySAInterface* pInterface = pFindBinary->m_pInterface;
// while ( pInterface && pInterface != NULL )
// if the interface is valid
if (pInterface && pInterface != NULL)
{
// if the building type is dummy or building and it's not already being removed
if ((pInterface->nType == ENTITY_TYPE_BUILDING || pInterface->nType == ENTITY_TYPE_DUMMY || pInterface->nType == ENTITY_TYPE_OBJECT) &&
pInterface->bRemoveFromWorld != 1)
{
if ((DWORD)(pInterface->vtbl) != VTBL_CPlaceable)
{
// Add the Data Building to the list
pRemoval->AddBinaryBuilding(pInterface);
// Remove the model from the world
Remove(pInterface, BuildingRemoval2);
m_pRemovedEntities[(DWORD)pInterface] = true;
bFound = true;
++uiAmount;
}
}
// Get next LOD ( LOD's can have LOD's so we keep checking pInterface )
// pInterface = pInterface->m_pLod;
}
// Set the count.
pFindBinary->m_iCount = 1;
}
}
}
}
if (bFound)
pGame->GetModelInfo(usModelToRemove)->RestreamIPL();
if (pOutAmount)
*pOutAmount = uiAmount;
}
bool CWorldSA::RestoreBuilding(unsigned short usModelToRestore, float fRange, float fX, float fY, float fZ, char cInterior, uint* pOutAmount)
{
bool bSuccess = false;
uint uiAmount = 0;
// Init some variables
std::pair<std::multimap<unsigned short, SBuildingRemoval*>::iterator, std::multimap<unsigned short, SBuildingRemoval*>::iterator> iterators =
m_pBuildingRemovals->equal_range(usModelToRestore);
std::multimap<unsigned short, SBuildingRemoval*>::const_iterator iter = iterators.first;
// Loop through the buildings list
for (; iter != iterators.second;)
{
SBuildingRemoval* pFind = (*iter).second;
// if pFind is valid and the model is the same
if (pFind)
{
// Grab distances across each axis
float fDistanceX = fX - pFind->m_vecPos.fX;
float fDistanceY = fY - pFind->m_vecPos.fY;
float fDistanceZ = fZ - pFind->m_vecPos.fZ;
// Square root 'em
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
if (fDistance <= pFind->m_fRadius && (cInterior == -1 || pFind->m_cInterior == cInterior))
{
// Init some variables
CEntitySAInterface* pEntity = NULL;
std::list<CEntitySAInterface*>::const_iterator entityIter = pFind->m_pBinaryRemoveList->begin();
if (pFind->m_pBinaryRemoveList->empty() == false)
{
// Loop through the Binary object list
for (; entityIter != pFind->m_pBinaryRemoveList->end();)
{
// Grab the pEntity
pEntity = (*entityIter);
// if it's valid re-add it to the world.
if (pEntity != NULL)
{
// Remove it from the binary list
pFind->m_pBinaryRemoveList->erase(entityIter++);
// if the building type is dummy or building and it's not already being removed
if ((pEntity->nType == ENTITY_TYPE_BUILDING || pEntity->nType == ENTITY_TYPE_DUMMY || pEntity->nType == ENTITY_TYPE_OBJECT) &&
pEntity->bRemoveFromWorld != 1)
{
// Don't call this on entities being removed.
if ((DWORD)(pEntity->vtbl) != VTBL_CPlaceable)
{
Add(pEntity, Building_Restore);
m_pRemovedEntities[(DWORD)pEntity] = false;
// If the building isn't streamed in, we won't find the building in the list (because the hook wasn't called) ->
// removeWorldModel doesn't work next time
AddBinaryBuilding(pEntity);
++uiAmount;
}
}
}
else
++entityIter;
}
}
// Start the iterator with the data remove list first item
entityIter = pFind->m_pDataRemoveList->begin();
if (pFind->m_pDataRemoveList->empty() == false)
{
// Loop through the Data list
for (; entityIter != pFind->m_pDataRemoveList->end();)
{
// Grab the pEntity
pEntity = (*entityIter);
// if it's valid re-add it to the world.
if (pEntity != NULL)
{
pFind->m_pDataRemoveList->erase(entityIter++);
// if the building type is dummy or building and it's not already being removed
if ((pEntity->nType == ENTITY_TYPE_BUILDING || pEntity->nType == ENTITY_TYPE_DUMMY || pEntity->nType == ENTITY_TYPE_OBJECT) &&
pEntity->bRemoveFromWorld != 1)
{
if ((DWORD)(pEntity->vtbl) != VTBL_CPlaceable)
{
Add(pEntity, Building_Restore2);
m_pRemovedEntities[(DWORD)pEntity] = false;
}
}
}
else
{
++entityIter;
}
}
}
// Remove the building from the list
m_pBuildingRemovals->erase(iter++);
delete pFind;
// Success! don't return incase there are any others to delete
bSuccess = true;
}
else
iter++;
}
else
iter++;
}
std::pair<std::multimap<unsigned short, sDataBuildingRemovalItem*>::iterator, std::multimap<unsigned short, sDataBuildingRemovalItem*>::iterator>
dataBuildingIterators = m_pDataBuildings->equal_range(usModelToRestore);
std::multimap<unsigned short, sDataBuildingRemovalItem*>::const_iterator iterator = dataBuildingIterators.first;
for (; iterator != dataBuildingIterators.second; ++iterator)
{
sDataBuildingRemovalItem* pFound = (*iterator).second;
if (pFound)
{
// Grab distances across each axis
float fDistanceX = fX - pFound->m_pInterface->Placeable.m_transform.m_translate.fX;
float fDistanceY = fY - pFound->m_pInterface->Placeable.m_transform.m_translate.fY;
float fDistanceZ = fZ - pFound->m_pInterface->Placeable.m_transform.m_translate.fZ;
if (pFound->m_pInterface->Placeable.matrix != NULL)
{
fDistanceX = fX - pFound->m_pInterface->Placeable.matrix->vPos.fX;
fDistanceY = fY - pFound->m_pInterface->Placeable.matrix->vPos.fY;
fDistanceZ = fZ - pFound->m_pInterface->Placeable.matrix->vPos.fZ;
}
// Square root 'em
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
if (fDistance <= fRange && (cInterior == -1 || pFound->m_pInterface->m_areaCode == cInterior))
{
// Fix the removed count.
pFound->m_iCount--;
if (pFound->m_iCount < 0)
pFound->m_iCount = 0;
}
}
}
std::pair<std::multimap<unsigned short, sBuildingRemovalItem*>::iterator, std::multimap<unsigned short, sBuildingRemovalItem*>::iterator>
binaryBuildingIterators = m_pBinaryBuildings->equal_range(usModelToRestore);
std::multimap<unsigned short, sBuildingRemovalItem*>::const_iterator iteratorBinary = binaryBuildingIterators.first;
for (; iteratorBinary != binaryBuildingIterators.second; ++iteratorBinary)
{
sBuildingRemovalItem* pFoundBinary = (*iteratorBinary).second;
if (pFoundBinary)
{
// Grab distances across each axis
float fDistanceX = fX - pFoundBinary->m_pInterface->Placeable.m_transform.m_translate.fX;
float fDistanceY = fY - pFoundBinary->m_pInterface->Placeable.m_transform.m_translate.fY;
float fDistanceZ = fZ - pFoundBinary->m_pInterface->Placeable.m_transform.m_translate.fZ;
if (pFoundBinary->m_pInterface->Placeable.matrix != NULL)
{
fDistanceX = fX - pFoundBinary->m_pInterface->Placeable.matrix->vPos.fX;
fDistanceY = fY - pFoundBinary->m_pInterface->Placeable.matrix->vPos.fY;
fDistanceZ = fZ - pFoundBinary->m_pInterface->Placeable.matrix->vPos.fZ;
}
// Square root 'em
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
if (fDistance <= fRange && (cInterior == -1 || pFoundBinary->m_pInterface->m_areaCode == cInterior))
{
// Fix the removed count.
pFoundBinary->m_iCount--;
if (pFoundBinary->m_iCount < 0)
pFoundBinary->m_iCount = 0;
}
}
}
if (pOutAmount)
*pOutAmount = uiAmount;
return bSuccess;
}
// Check Distance to see if the model being requested is in the radius
bool CWorldSA::IsRemovedModelInRadius(SIPLInst* pInst)
{
// Init some variables
std::pair<std::multimap<unsigned short, SBuildingRemoval*>::iterator, std::multimap<unsigned short, SBuildingRemoval*>::iterator> iterators =
m_pBuildingRemovals->equal_range(pInst->m_nModelIndex);
std::multimap<unsigned short, SBuildingRemoval*>::const_iterator iter = iterators.first;
// Loop through the buildings list
for (; iter != iterators.second; ++iter)
{
SBuildingRemoval* pFind = (*iter).second;
// if pFind is valid and the model is the same
if (pFind)
{
// Grab the distance
float fDistanceX = pFind->m_vecPos.fX - pInst->m_pPosition.fX;
float fDistanceY = pFind->m_vecPos.fY - pInst->m_pPosition.fY;
float fDistanceZ = pFind->m_vecPos.fZ - pInst->m_pPosition.fZ;
float fDistance = sqrt(fDistanceX * fDistanceX + fDistanceY * fDistanceY + fDistanceZ * fDistanceZ);
// is it in the removal spheres radius if so return else keep looking
if (fDistance <= pFind->m_fRadius && (pFind->m_cInterior == -1 || pFind->m_cInterior == pInst->m_nInterior))
{
return true;
}
}
}
return false;
}
// Check Distance to see if the model being requested is in the radius
bool CWorldSA::IsObjectRemoved(CEntitySAInterface* pInterface)
{
// Init some variables
std::pair<std::multimap<unsigned short, SBuildingRemoval*>::iterator, std::multimap<unsigned short, SBuildingRemoval*>::iterator> iterators =
m_pBuildingRemovals->equal_range(pInterface->m_nModelIndex);
std::multimap<unsigned short, SBuildingRemoval*>::const_iterator iter = iterators.first;
// Loop through the buildings list
for (; iter != iterators.second; ++iter)
{
SBuildingRemoval* pFind = (*iter).second;
// if pFind is valid and the model is the same
if (pFind)
{
// Are we using the interior param?? if so check for a match
if (pFind->m_cInterior == -1 || pFind->m_cInterior == pInterface->m_areaCode)
{
// Grab the distance