-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMovableMan.cpp
More file actions
1957 lines (1630 loc) · 65 KB
/
MovableMan.cpp
File metadata and controls
1957 lines (1630 loc) · 65 KB
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
#include "MovableMan.h"
#include "PrimitiveMan.h"
#include "PostProcessMan.h"
#include "PerformanceMan.h"
#include "PresetMan.h"
#include "AEmitter.h"
#include "AHuman.h"
#include "MOPixel.h"
#include "HeldDevice.h"
#include "SLTerrain.h"
#include "Controller.h"
#include "AtomGroup.h"
#include "Actor.h"
#include "ADoor.h"
#include "Atom.h"
#include "Scene.h"
#include "FrameMan.h"
#include "SceneMan.h"
#include "SettingsMan.h"
#include "LuaMan.h"
#include "ThreadMan.h"
#include "tracy/Tracy.hpp"
#include <execution>
using namespace RTE;
AlarmEvent::AlarmEvent(const Vector& pos, int team, float range) {
m_ScenePos = pos;
m_Team = (Activity::Teams)team;
m_Range = range * g_FrameMan.GetPlayerScreenWidth() * 0.51F;
}
const std::string MovableMan::c_ClassName = "MovableMan";
// Comparison functor for sorting movable objects by their X position using STL's sort
struct MOXPosComparison {
bool operator()(MovableObject* pRhs, MovableObject* pLhs) { return pRhs->GetPos().m_X < pLhs->GetPos().m_X; }
};
MovableMan::MovableMan() {
Clear();
}
MovableMan::~MovableMan() {
Destroy();
}
void MovableMan::Clear() {
m_Actors.clear();
m_ContiguousActorIDs.clear();
m_Items.clear();
m_Particles.clear();
m_AddedActors.clear();
m_AddedItems.clear();
m_AddedParticles.clear();
m_ValidActors.clear();
m_ValidItems.clear();
m_ValidParticles.clear();
m_ActorRoster[Activity::TeamOne].clear();
m_ActorRoster[Activity::TeamTwo].clear();
m_ActorRoster[Activity::TeamThree].clear();
m_ActorRoster[Activity::TeamFour].clear();
m_SortTeamRoster[Activity::TeamOne] = false;
m_SortTeamRoster[Activity::TeamTwo] = false;
m_SortTeamRoster[Activity::TeamThree] = false;
m_SortTeamRoster[Activity::TeamFour] = false;
m_AddedAlarmEvents.clear();
m_AlarmEvents.clear();
m_MOIDIndex.clear();
m_SplashRatio = 0.75;
m_MaxDroppedItems = 100;
m_SettlingEnabled = true;
m_MOSubtractionEnabled = true;
}
int MovableMan::Initialize() {
// TODO: Increase this number, or maybe only for certain classes?
Entity::ClassInfo::FillAllPools();
return 0;
}
int MovableMan::ReadProperty(const std::string_view& propName, Reader& reader) {
StartPropertyList(return Serializable::ReadProperty(propName, reader));
MatchProperty("AddEffect", { g_PresetMan.GetEntityPreset(reader); });
MatchProperty("AddAmmo", { g_PresetMan.GetEntityPreset(reader); });
MatchProperty("AddDevice", { g_PresetMan.GetEntityPreset(reader); });
MatchProperty("AddActor", { g_PresetMan.GetEntityPreset(reader); });
MatchProperty("SplashRatio", { reader >> m_SplashRatio; });
EndPropertyList;
}
int MovableMan::Save(Writer& writer) const {
Serializable::Save(writer);
writer << m_Actors.size();
for (std::deque<Actor*>::const_iterator itr = m_Actors.begin(); itr != m_Actors.end(); ++itr)
writer << **itr;
writer << m_Particles.size();
for (std::deque<MovableObject*>::const_iterator itr2 = m_Particles.begin(); itr2 != m_Particles.end(); ++itr2)
writer << **itr2;
return 0;
}
void MovableMan::Destroy() {
for (std::deque<Actor*>::iterator it1 = m_Actors.begin(); it1 != m_Actors.end(); ++it1)
delete (*it1);
for (std::deque<MovableObject*>::iterator it2 = m_Items.begin(); it2 != m_Items.end(); ++it2)
delete (*it2);
for (std::deque<MovableObject*>::iterator it3 = m_Particles.begin(); it3 != m_Particles.end(); ++it3)
delete (*it3);
for (std::vector<AlarmEvent*>::iterator it4 = m_AlarmEvents.begin(); it4 != m_AlarmEvents.end(); ++it4)
delete (*it4);
for (std::vector<AlarmEvent*>::iterator it5 = m_AddedAlarmEvents.begin(); it5 != m_AddedAlarmEvents.end(); ++it5)
delete (*it5);
Clear();
}
MovableObject* MovableMan::GetMOFromID(MOID whichID) {
if (whichID != g_NoMOID && whichID != 0 && whichID < m_MOIDIndex.size()) {
// This is really, really awful
// But, Lua scripts can take ownership of an MO which exists in this list
// And then the MO can be deallocated by Lua GC
// Meaning that this ptr points to stale memory.
// Due to our pooled memory system, we can avoid a crash by just... checking that the MO isn't NoMOID
// But this is still atrociously awful and terrible and this can point to a newly-allocated object that just so happens to be allocated the same place.
// Anyways, until we can fix the god-awful abomination that is this game's memory ownership semantics, we're stuck with this
// Which is also technically undefined behaviour
MovableObject* candidate = m_MOIDIndex[whichID];
if (candidate->GetID() != whichID) {
return nullptr;
}
return candidate;
}
return nullptr;
}
MOID MovableMan::GetMOIDPixel(int pixelX, int pixelY, const std::vector<int>& moidList) {
// Note - We loop through the MOs in reverse to make sure that the topmost (last drawn) MO that overlaps the specified coordinates is the one returned.
for (auto itr = moidList.rbegin(), itrEnd = moidList.rend(); itr < itrEnd; ++itr) {
MOID moid = *itr;
const MovableObject* mo = GetMOFromID(moid);
// Commented... see MovableMan::GetMOFromID
// RTEAssert(mo, "Null MO found in MOID list!");
if (mo == nullptr) {
continue;
}
if (mo->GetScale() == 0.0f) {
return g_NoMOID;
} else if (mo->HitTestAtPixel(pixelX, pixelY)) {
return moid;
}
}
return g_NoMOID;
}
void MovableMan::RegisterObject(MovableObject* mo) {
if (!mo) {
return;
}
std::lock_guard<std::mutex> guard(m_ObjectRegisteredMutex);
m_KnownObjects[mo->GetUniqueID()] = mo;
}
void MovableMan::UnregisterObject(MovableObject* mo) {
if (!mo) {
return;
}
std::lock_guard<std::mutex> guard(m_ObjectRegisteredMutex);
m_KnownObjects.erase(mo->GetUniqueID());
}
const std::vector<MovableObject*>* MovableMan::GetMOsInBox(const Box& box, int ignoreTeam, bool getsHitByMOsOnly) const {
std::vector<MovableObject*>* vectorForLua = new std::vector<MovableObject*>();
*vectorForLua = std::move(g_SceneMan.GetMOIDGrid().GetMOsInBox(box, ignoreTeam, getsHitByMOsOnly));
return vectorForLua;
}
const std::vector<MovableObject*>* MovableMan::GetMOsInRadius(const Vector& centre, float radius, int ignoreTeam, bool getsHitByMOsOnly) const {
std::vector<MovableObject*>* vectorForLua = new std::vector<MovableObject*>();
*vectorForLua = std::move(g_SceneMan.GetMOIDGrid().GetMOsInRadius(centre, radius, ignoreTeam, getsHitByMOsOnly));
return vectorForLua;
}
const std::vector<MovableObject*>* MovableMan::GetMOsAtPosition(int pixelX, int pixelY, int ignoreTeam, bool getsHitByMOsOnly) const {
std::vector<MovableObject*>* vectorForLua = new std::vector<MovableObject*>();
*vectorForLua = std::move(g_SceneMan.GetMOIDGrid().GetMOsAtPosition(pixelX, pixelY, ignoreTeam, getsHitByMOsOnly));
return vectorForLua;
}
void MovableMan::PurgeAllMOs() {
for (std::deque<Actor*>::iterator itr = m_Actors.begin(); itr != m_Actors.end(); ++itr) {
(*itr)->DestroyScriptState();
}
for (std::deque<MovableObject*>::iterator itr = m_Items.begin(); itr != m_Items.end(); ++itr) {
(*itr)->DestroyScriptState();
}
for (std::deque<MovableObject*>::iterator itr = m_Particles.begin(); itr != m_Particles.end(); ++itr) {
(*itr)->DestroyScriptState();
}
for (std::deque<Actor*>::iterator itr = m_Actors.begin(); itr != m_Actors.end(); ++itr) {
delete (*itr);
}
for (std::deque<MovableObject*>::iterator itr = m_Items.begin(); itr != m_Items.end(); ++itr) {
delete (*itr);
}
for (std::deque<MovableObject*>::iterator itr = m_Particles.begin(); itr != m_Particles.end(); ++itr) {
delete (*itr);
}
m_Actors.clear();
m_Items.clear();
m_Particles.clear();
m_AddedActors.clear();
m_AddedItems.clear();
m_AddedParticles.clear();
m_ValidActors.clear();
m_ValidItems.clear();
m_ValidParticles.clear();
m_ActorRoster[Activity::TeamOne].clear();
m_ActorRoster[Activity::TeamTwo].clear();
m_ActorRoster[Activity::TeamThree].clear();
m_ActorRoster[Activity::TeamFour].clear();
m_SortTeamRoster[Activity::TeamOne] = false;
m_SortTeamRoster[Activity::TeamTwo] = false;
m_SortTeamRoster[Activity::TeamThree] = false;
m_SortTeamRoster[Activity::TeamFour] = false;
m_AddedAlarmEvents.clear();
m_AlarmEvents.clear();
m_MOIDIndex.clear();
// We want to keep known objects around, 'cause these can exist even when not in the simulation (they're here from creation till deletion, regardless of whether they are in sim)
// m_KnownObjects.clear();
}
Actor* MovableMan::GetNextActorInGroup(std::string group, Actor* pAfterThis) {
if (group.empty())
return 0;
// Begin at the beginning
std::deque<Actor*>::const_iterator aIt = m_Actors.begin();
// Search for the actor to start search from, if specified
if (pAfterThis) {
// Make the iterator point to the specified starting point actor
for (; aIt != m_Actors.end() && !((*aIt)->IsInGroup(group) && *aIt == pAfterThis); ++aIt)
;
// If we couldn't find the one to search for,
// then just start at the beginning again and get the first actor at the next step
if (aIt == m_Actors.end())
aIt = m_Actors.begin();
// Go one more step so we're not pointing at the one we're not supposed to get
else
++aIt;
}
// Now search for the first actor of the team from the search point (beginning or otherwise)
for (; aIt != m_Actors.end() && !(*aIt)->IsInGroup(group); ++aIt)
;
// If nothing found between a specified actor and the end,
// then restart and see if there's anything between beginning and that specified actor
if (pAfterThis && aIt == m_Actors.end()) {
for (aIt = m_Actors.begin(); aIt != m_Actors.end() && !(*aIt)->IsInGroup(group); ++aIt)
;
// Still nothing?? Should at least get the specified actor and return it! - EDIT No becuase it just may not be there!
// RTEAssert(aIt != m_Actors.end(), "Search for something after specified actor, and didn't even find the specified actor!?");
}
// Still nothing, so return nothing
if (aIt == m_Actors.end())
return 0;
if ((*aIt)->IsInGroup(group))
return *aIt;
return 0;
}
Actor* MovableMan::GetPrevActorInGroup(std::string group, Actor* pBeforeThis) {
if (group.empty())
return 0;
// Begin at the reverse beginning
std::deque<Actor*>::reverse_iterator aIt = m_Actors.rbegin();
// Search for the actor to start search from, if specified
if (pBeforeThis) {
// Make the iterator point to the specified starting point actor
for (; aIt != m_Actors.rend() && !((*aIt)->IsInGroup(group) && *aIt == pBeforeThis); ++aIt)
;
// If we couldn't find the one to search for,
// then just start at the beginning again and get the first actor at the next step
if (aIt == m_Actors.rend())
aIt = m_Actors.rbegin();
// Go one more step so we're not pointing at the one we're not supposed to get
else
++aIt;
}
// Now search for the first actor of the team from the search point (beginning or otherwise)
for (; aIt != m_Actors.rend() && !(*aIt)->IsInGroup(group); ++aIt)
;
// If nothing found between a specified actor and the end,
// then restart and see if there's anything between beginning and that specified actor
if (pBeforeThis && aIt == m_Actors.rend()) {
for (aIt = m_Actors.rbegin(); aIt != m_Actors.rend() && !(*aIt)->IsInGroup(group); ++aIt)
;
// Still nothing?? Should at least get the specified actor and return it! - EDIT No becuase it just may not be there!
// RTEAssert(aIt != m_Actors.rend(), "Search for something after specified actor, and didn't even find the specified actor!?");
}
// Still nothing, so return nothing
if (aIt == m_Actors.rend())
return 0;
if ((*aIt)->IsInGroup(group))
return *aIt;
return 0;
}
Actor* MovableMan::GetNextTeamActor(int team, Actor* pAfterThis) {
if (team < Activity::TeamOne || team >= Activity::MaxTeamCount || m_ActorRoster[team].empty())
return 0;
/*
// Begin at the beginning
std::deque<Actor *>::const_iterator aIt = m_Actors.begin();
// Search for the actor to start search from, if specified
if (pAfterThis)
{
// Make the iterator point to the specified starting point actor
for (; aIt != m_Actors.end() && !((*aIt)->GetTeam() == team && *aIt == pAfterThis); ++aIt)
;
// If we couldn't find the one to search for,
// then just start at the beginning again and get the first actor at the next step
if (aIt == m_Actors.end())
aIt = m_Actors.begin();
// Go one more step so we're not pointing at the one we're not supposed to get
else
++aIt;
}
// Now search for the first actor of the team from the search point (beginning or otherwise)
for (; aIt != m_Actors.end() && (*aIt)->GetTeam() != team; ++aIt)
;
// If nothing found between a specified actor and the end,
// then restart and see if there's anything between beginning and that specified actor
if (pAfterThis && aIt == m_Actors.end())
{
for (aIt = m_Actors.begin(); aIt != m_Actors.end() && (*aIt)->GetTeam() != team; ++aIt)
;
// Still nothing?? Should at least get the specified actor and return it! - EDIT No becuase it just may not be there!
// RTEAssert(aIt != m_Actors.end(), "Search for something after specified actor, and didn't even find the specified actor!?");
}
// Still nothing, so return nothing
if (aIt == m_Actors.end())
return 0;
if ((*aIt)->GetTeam() == team)
return *aIt;
return 0;
*/
// First sort the roster
m_ActorRoster[team].sort(MOXPosComparison());
// Begin at the beginning
std::list<Actor*>::const_iterator aIt = m_ActorRoster[team].begin();
// Search for the actor to start search from, if specified
if (pAfterThis) {
// Make the iterator point to the specified starting point actor
for (; aIt != m_ActorRoster[team].end() && *aIt != pAfterThis; ++aIt)
;
// If we couldn't find the one to search for, then just return the first one
if (aIt == m_ActorRoster[team].end())
aIt = m_ActorRoster[team].begin();
// Go one more step so we're not pointing at the one we're not supposed to get
else {
++aIt;
// If that was the last one, then return the first in the list
if (aIt == m_ActorRoster[team].end())
aIt = m_ActorRoster[team].begin();
}
}
RTEAssert((*aIt)->GetTeam() == team, "Actor of wrong team found in the wrong roster!");
return *aIt;
}
Actor* MovableMan::GetPrevTeamActor(int team, Actor* pBeforeThis) {
if (team < Activity::TeamOne || team >= Activity::MaxTeamCount || m_Actors.empty() || m_ActorRoster[team].empty())
return 0;
/* Obsolete, now uses team rosters which are sorted
// Begin at the reverse beginning
std::deque<Actor *>::const_reverse_iterator aIt = m_Actors.rbegin();
// Search for the actor to start search from, if specified
if (pBeforeThis)
{
// Make the iterator point to the specified starting point actor
for (; aIt != m_Actors.rend() && !((*aIt)->GetTeam() == team && *aIt == pBeforeThis); ++aIt)
;
// If we couldn't find the one to search for,
// then just start at the beginning again and get the first actor at the next step
if (aIt == m_Actors.rend())
aIt = m_Actors.rbegin();
// Go one more step so we're not pointing at the one we're not supposed to get
else
++aIt;
}
// Now search for the first actor of the team from the search point (beginning or otherwise)
for (; aIt != m_Actors.rend() && (*aIt)->GetTeam() != team; ++aIt)
;
// If nothing found between a specified actor and the end,
// then restart and see if there's anything between beginning and that specified actor
if (pBeforeThis && aIt == m_Actors.rend())
{
for (aIt = m_Actors.rbegin(); aIt != m_Actors.rend() && (*aIt)->GetTeam() != team; ++aIt)
;
// Still nothing?? Should at least get the specified actor and return it! - EDIT No becuase it just may not be there!
// RTEAssert(aIt != m_Actors.rend(), "Search for something after specified actor, and didn't even find the specified actor!?");
}
// Still nothing, so return nothing
if (aIt == m_Actors.rend())
return 0;
if ((*aIt)->GetTeam() == team)
return *aIt;
return 0;
*/
// First sort the roster
m_ActorRoster[team].sort(MOXPosComparison());
// Begin at the reverse beginning of roster
std::list<Actor*>::reverse_iterator aIt = m_ActorRoster[team].rbegin();
// Search for the actor to start search from, if specified
if (pBeforeThis) {
// Make the iterator point to the specified starting point actor
for (; aIt != m_ActorRoster[team].rend() && *aIt != pBeforeThis; ++aIt)
;
// If we couldn't find the one to search for, then just return the one at the end
if (aIt == m_ActorRoster[team].rend())
aIt = m_ActorRoster[team].rbegin();
// Go one more step so we're not pointing at the one we're not supposed to get
else {
++aIt;
// If that was the first one, then return the last in the list
if (aIt == m_ActorRoster[team].rend())
aIt = m_ActorRoster[team].rbegin();
}
}
RTEAssert((*aIt)->GetTeam() == team, "Actor of wrong team found in the wrong roster!");
return *aIt;
}
Actor* MovableMan::GetClosestTeamActor(int team, int player, const Vector& scenePoint, int maxRadius, Vector& getDistance, bool onlyPlayerControllableActors, const Actor* excludeThis) {
if (team < Activity::NoTeam || team >= Activity::MaxTeamCount || m_Actors.empty() || m_ActorRoster[team].empty())
return 0;
Activity* pActivity = g_ActivityMan.GetActivity();
float sqrShortestDistance = static_cast<float>(maxRadius * maxRadius);
Actor* pClosestActor = 0;
// If we're looking for a noteam actor, then go through the entire actor list instead
if (team == Activity::NoTeam) {
for (std::deque<Actor*>::iterator aIt = m_Actors.begin(); aIt != m_Actors.end(); ++aIt) {
if ((*aIt) == excludeThis || (*aIt)->GetTeam() != Activity::NoTeam || (onlyPlayerControllableActors && !(*aIt)->IsPlayerControllable())) {
continue;
}
// Check if even within search radius
float sqrDistance = g_SceneMan.ShortestDistance((*aIt)->GetPos(), scenePoint, g_SceneMan.SceneWrapsX() || g_SceneMan.SceneWrapsY()).GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestActor = *aIt;
}
}
}
// A specific team, so use the rosters instead
else {
for (std::list<Actor*>::iterator aIt = m_ActorRoster[team].begin(); aIt != m_ActorRoster[team].end(); ++aIt) {
if ((*aIt) == excludeThis || (onlyPlayerControllableActors && !(*aIt)->IsPlayerControllable()) || (player != NoPlayer && ((*aIt)->GetController()->IsPlayerControlled(player) || (pActivity && pActivity->IsOtherPlayerBrain(*aIt, player))))) {
continue;
}
Vector distanceVec = g_SceneMan.ShortestDistance((*aIt)->GetPos(), scenePoint, g_SceneMan.SceneWrapsX() || g_SceneMan.SceneWrapsY());
// Check if even within search radius
float sqrDistance = distanceVec.GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestActor = *aIt;
getDistance.SetXY(distanceVec.GetX(), distanceVec.GetY());
}
}
}
return pClosestActor;
}
Actor* MovableMan::GetClosestEnemyActor(int team, const Vector& scenePoint, int maxRadius, Vector& getDistance) {
if (team < Activity::NoTeam || team >= Activity::MaxTeamCount || m_Actors.empty())
return 0;
Activity* pActivity = g_ActivityMan.GetActivity();
float sqrShortestDistance = static_cast<float>(maxRadius * maxRadius);
Actor* pClosestActor = 0;
for (std::deque<Actor*>::iterator aIt = m_Actors.begin(); aIt != m_Actors.end(); ++aIt) {
if ((*aIt)->GetTeam() == team)
continue;
Vector distanceVec = g_SceneMan.ShortestDistance((*aIt)->GetPos(), scenePoint, g_SceneMan.SceneWrapsX() || g_SceneMan.SceneWrapsY());
// Check if even within search radius
float sqrDistance = distanceVec.GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestActor = *aIt;
getDistance.SetXY(distanceVec.GetX(), distanceVec.GetY());
}
}
return pClosestActor;
}
Actor* MovableMan::GetClosestActor(const Vector& scenePoint, int maxRadius, Vector& getDistance, const Actor* pExcludeThis) {
if (m_Actors.empty())
return 0;
Activity* pActivity = g_ActivityMan.GetActivity();
float sqrShortestDistance = static_cast<float>(maxRadius * maxRadius);
Actor* pClosestActor = 0;
for (std::deque<Actor*>::iterator aIt = m_Actors.begin(); aIt != m_Actors.end(); ++aIt) {
if ((*aIt) == pExcludeThis)
continue;
Vector distanceVec = g_SceneMan.ShortestDistance((*aIt)->GetPos(), scenePoint, g_SceneMan.SceneWrapsX() || g_SceneMan.SceneWrapsY());
// Check if even within search radius
float sqrDistance = distanceVec.GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestActor = *aIt;
getDistance.SetXY(distanceVec.GetX(), distanceVec.GetY());
}
}
return pClosestActor;
}
Actor* MovableMan::GetClosestBrainActor(int team, const Vector& scenePoint) const {
if (team < Activity::TeamOne || team >= Activity::MaxTeamCount || m_ActorRoster[team].empty())
return 0;
float sqrShortestDistance = std::numeric_limits<float>::infinity();
sqrShortestDistance *= sqrShortestDistance;
Actor* pClosestBrain = 0;
for (std::list<Actor*>::const_iterator aIt = m_ActorRoster[team].begin(); aIt != m_ActorRoster[team].end(); ++aIt) {
if (!(*aIt)->HasObjectInGroup("Brains"))
continue;
// Check if closer than best so far
float sqrDistance = g_SceneMan.ShortestDistance((*aIt)->GetPos(), scenePoint, g_SceneMan.SceneWrapsX() || g_SceneMan.SceneWrapsY()).GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestBrain = *aIt;
}
}
return pClosestBrain;
}
Actor* MovableMan::GetClosestOtherBrainActor(int notOfTeam, const Vector& scenePoint) const {
if (notOfTeam < Activity::TeamOne || notOfTeam >= Activity::MaxTeamCount || m_Actors.empty())
return 0;
float sqrShortestDistance = std::numeric_limits<float>::infinity();
sqrShortestDistance *= sqrShortestDistance;
Actor* pClosestBrain = 0;
Actor* pContenderBrain = 0;
for (int t = Activity::TeamOne; t < g_ActivityMan.GetActivity()->GetTeamCount(); ++t) {
if (t != notOfTeam) {
pContenderBrain = GetClosestBrainActor(t, scenePoint);
float sqrDistance = (pContenderBrain->GetPos() - scenePoint).GetSqrMagnitude();
if (sqrDistance < sqrShortestDistance) {
sqrShortestDistance = sqrDistance;
pClosestBrain = pContenderBrain;
}
}
}
return pClosestBrain;
}
Actor* MovableMan::GetUnassignedBrain(int team) const {
if (/*m_Actors.empty() || */ m_ActorRoster[team].empty())
return 0;
for (std::list<Actor*>::const_iterator aIt = m_ActorRoster[team].begin(); aIt != m_ActorRoster[team].end(); ++aIt) {
if ((*aIt)->HasObjectInGroup("Brains") && !g_ActivityMan.GetActivity()->IsAssignedBrain(*aIt))
return *aIt;
}
// Also need to look through all the actors added this frame, one might be a brain.
int actorTeam = Activity::NoTeam;
for (std::deque<Actor*>::const_iterator aaIt = m_AddedActors.begin(); aaIt != m_AddedActors.end(); ++aaIt) {
int actorTeam = (*aaIt)->GetTeam();
// Accept no-team brains too - ACTUALLY, DON'T
if ((actorTeam == team /* || actorTeam == Activity::NoTeam*/) && (*aaIt)->HasObjectInGroup("Brains") && !g_ActivityMan.GetActivity()->IsAssignedBrain(*aaIt))
return *aaIt;
}
return 0;
}
bool MovableMan::AddMO(MovableObject* movableObjectToAdd) {
if (!movableObjectToAdd) {
return false;
}
if (Actor* actorToAdd = dynamic_cast<Actor*>(movableObjectToAdd)) {
AddActor(actorToAdd);
return true;
} else if (HeldDevice* heldDeviceToAdd = dynamic_cast<HeldDevice*>(movableObjectToAdd)) {
AddItem(heldDeviceToAdd);
return true;
}
AddParticle(movableObjectToAdd);
return true;
}
void MovableMan::AddActor(Actor* actorToAdd) {
if (actorToAdd && g_ActivityMan.GetActivity()) {
actorToAdd->SetAsAddedToMovableMan();
actorToAdd->CorrectAttachableAndWoundPositionsAndRotations();
if (actorToAdd->IsTooFast()) {
actorToAdd->SetToDelete(true);
} else {
if (!dynamic_cast<ADoor*>(actorToAdd)) {
actorToAdd->MoveOutOfTerrain(g_MaterialGrass);
}
if (actorToAdd->IsStatus(Actor::INACTIVE)) {
actorToAdd->SetStatus(Actor::STABLE);
}
actorToAdd->NotResting();
actorToAdd->NewFrame();
actorToAdd->SetAge(0);
}
{
std::lock_guard<std::mutex> lock(m_AddedActorsMutex);
m_AddedActors.push_back(actorToAdd);
m_ValidActors.insert(actorToAdd);
// This will call SetTeam and subsequently force the team as active.
AddActorToTeamRoster(actorToAdd);
}
}
}
void MovableMan::AddItem(HeldDevice* itemToAdd) {
if (itemToAdd && g_ActivityMan.GetActivity()) {
g_ActivityMan.GetActivity()->ForceSetTeamAsActive(itemToAdd->GetTeam());
itemToAdd->SetAsAddedToMovableMan();
itemToAdd->CorrectAttachableAndWoundPositionsAndRotations();
if (itemToAdd->IsTooFast()) {
itemToAdd->SetToDelete(true);
} else {
if (!itemToAdd->IsSetToDelete()) {
itemToAdd->MoveOutOfTerrain(g_MaterialGrass);
}
itemToAdd->NotResting();
itemToAdd->NewFrame();
itemToAdd->SetAge(0);
}
std::lock_guard<std::mutex> lock(m_AddedItemsMutex);
m_AddedItems.push_back(itemToAdd);
m_ValidItems.insert(itemToAdd);
}
}
void MovableMan::AddParticle(MovableObject* particleToAdd) {
if (particleToAdd && g_ActivityMan.GetActivity()) {
g_ActivityMan.GetActivity()->ForceSetTeamAsActive(particleToAdd->GetTeam());
particleToAdd->SetAsAddedToMovableMan();
if (MOSRotating* particleToAddAsMOSRotating = dynamic_cast<MOSRotating*>(particleToAdd)) {
particleToAddAsMOSRotating->CorrectAttachableAndWoundPositionsAndRotations();
}
if (particleToAdd->IsTooFast()) {
particleToAdd->SetToDelete(true);
} else {
// TODO consider moving particles out of grass. It's old code that was removed because it's slow to do this for every particle.
particleToAdd->NotResting();
particleToAdd->NewFrame();
particleToAdd->SetAge(0);
}
if (particleToAdd->IsDevice()) {
std::lock_guard<std::mutex> lock(m_AddedItemsMutex);
m_AddedItems.push_back(particleToAdd);
m_ValidItems.insert(particleToAdd);
} else {
std::lock_guard<std::mutex> lock(m_AddedParticlesMutex);
m_AddedParticles.push_back(particleToAdd);
m_ValidParticles.insert(particleToAdd);
}
}
}
Actor* MovableMan::RemoveActor(MovableObject* pActorToRem) {
Actor* removed = nullptr;
if (pActorToRem) {
for (std::deque<Actor*>::iterator itr = m_Actors.begin(); itr != m_Actors.end(); ++itr) {
if (*itr == pActorToRem) {
std::lock_guard<std::mutex> lock(m_ActorsMutex);
removed = *itr;
m_ValidActors.erase(*itr);
m_Actors.erase(itr);
break;
}
}
// Try the newly added actors if we couldn't find it in the regular deque
if (!removed) {
for (std::deque<Actor*>::iterator itr = m_AddedActors.begin(); itr != m_AddedActors.end(); ++itr) {
if (*itr == pActorToRem) {
std::lock_guard<std::mutex> lock(m_AddedActorsMutex);
removed = *itr;
m_ValidActors.erase(*itr);
m_AddedActors.erase(itr);
break;
}
}
}
RemoveActorFromTeamRoster(dynamic_cast<Actor*>(pActorToRem));
pActorToRem->SetAsAddedToMovableMan(false);
}
return removed;
}
MovableObject* MovableMan::RemoveItem(MovableObject* pItemToRem) {
MovableObject* removed = nullptr;
if (pItemToRem) {
for (std::deque<MovableObject*>::iterator itr = m_Items.begin(); itr != m_Items.end(); ++itr) {
if (*itr == pItemToRem) {
std::lock_guard<std::mutex> lock(m_ItemsMutex);
removed = *itr;
m_ValidItems.erase(*itr);
m_Items.erase(itr);
break;
}
}
// Try the newly added items if we couldn't find it in the regular deque
if (!removed) {
for (std::deque<MovableObject*>::iterator itr = m_AddedItems.begin(); itr != m_AddedItems.end(); ++itr) {
if (*itr == pItemToRem) {
std::lock_guard<std::mutex> lock(m_AddedItemsMutex);
removed = *itr;
m_ValidItems.erase(*itr);
m_AddedItems.erase(itr);
break;
}
}
}
pItemToRem->SetAsAddedToMovableMan(false);
}
return removed;
}
MovableObject* MovableMan::RemoveParticle(MovableObject* pMOToRem) {
MovableObject* removed = nullptr;
if (pMOToRem) {
for (std::deque<MovableObject*>::iterator itr = m_Particles.begin(); itr != m_Particles.end(); ++itr) {
if (*itr == pMOToRem) {
std::lock_guard<std::mutex> lock(m_ParticlesMutex);
removed = *itr;
m_ValidParticles.erase(*itr);
m_Particles.erase(itr);
break;
}
}
// Try the newly added particles if we couldn't find it in the regular deque
if (!removed) {
for (std::deque<MovableObject*>::iterator itr = m_AddedParticles.begin(); itr != m_AddedParticles.end(); ++itr) {
if (*itr == pMOToRem) {
std::lock_guard<std::mutex> lock(m_AddedParticlesMutex);
removed = *itr;
m_ValidParticles.erase(*itr);
m_AddedParticles.erase(itr);
break;
}
}
}
pMOToRem->SetAsAddedToMovableMan(false);
}
return removed;
}
void MovableMan::AddActorToTeamRoster(Actor* pActorToAdd) {
if (!pActorToAdd) {
return;
}
// Add to the team roster and then sort it too
int team = pActorToAdd->GetTeam();
// Also re-set the TEam so that the Team Icons get set up properly
pActorToAdd->SetTeam(team);
// Only add to a roster if it's on a team AND is controllable (eg doors are not)
if (team >= Activity::TeamOne && team < Activity::MaxTeamCount && pActorToAdd->IsControllable()) {
std::lock_guard<std::mutex> lock(m_ActorRosterMutex);
m_ActorRoster[pActorToAdd->GetTeam()].push_back(pActorToAdd);
m_ActorRoster[pActorToAdd->GetTeam()].sort(MOXPosComparison());
}
}
void MovableMan::RemoveActorFromTeamRoster(Actor* pActorToRem) {
if (!pActorToRem) {
return;
}
int team = pActorToRem->GetTeam();
// Remove from roster as well
if (team >= Activity::TeamOne && team < Activity::MaxTeamCount) {
std::lock_guard<std::mutex> lock(m_ActorRosterMutex);
m_ActorRoster[team].remove(pActorToRem);
}
}
void MovableMan::ChangeActorTeam(Actor* pActor, int team) {
if (!pActor) {
return;
}
if (pActor->IsPlayerControlled()) {
g_ActivityMan.GetActivity()->LoseControlOfActor(pActor->GetController()->GetPlayer());
}
RemoveActorFromTeamRoster(pActor);
pActor->SetTeam(team);
AddActorToTeamRoster(pActor);
// Because doors affect the team-based pathfinders, we need to tell them there's been a change.
// This is hackily done by erasing the door material, updating the pathfinders, then redrawing it and updating them again so they properly account for the door's new team.
if (ADoor* actorAsADoor = dynamic_cast<ADoor*>(pActor); actorAsADoor && actorAsADoor->GetDoorMaterialDrawn()) {
actorAsADoor->TempEraseOrRedrawDoorMaterial(true);
g_SceneMan.GetTerrain()->AddUpdatedMaterialArea(actorAsADoor->GetBoundingBox());
g_SceneMan.GetScene()->UpdatePathFinding();
actorAsADoor->TempEraseOrRedrawDoorMaterial(false);
g_SceneMan.GetTerrain()->AddUpdatedMaterialArea(actorAsADoor->GetBoundingBox());
g_SceneMan.GetScene()->UpdatePathFinding();
}
}
bool MovableMan::ValidateMOIDs() {
#ifdef DEBUG_BUILD
for (const MovableObject* mo: m_MOIDIndex) {
RTEAssert(mo, "Null MO found!");
}
#endif
return true;
}
bool MovableMan::ValidMO(const MovableObject* pMOToCheck) {
bool exists = m_ValidActors.find(pMOToCheck) != m_ValidActors.end() ||
m_ValidItems.find(pMOToCheck) != m_ValidItems.end() ||
m_ValidParticles.find(pMOToCheck) != m_ValidParticles.end();
return pMOToCheck && exists;
}
bool MovableMan::IsActor(const MovableObject* pMOToCheck) {
return pMOToCheck && m_ValidActors.find(pMOToCheck) != m_ValidActors.end();
}
bool MovableMan::IsDevice(const MovableObject* pMOToCheck) {
return pMOToCheck && m_ValidItems.find(pMOToCheck) != m_ValidItems.end();
}
bool MovableMan::IsParticle(const MovableObject* pMOToCheck) {
return pMOToCheck && m_ValidParticles.find(pMOToCheck) != m_ValidParticles.end();
}
bool MovableMan::IsOfActor(MOID checkMOID) {
if (checkMOID == g_NoMOID)
return false;
bool found = false;
MovableObject* pMO = GetMOFromID(checkMOID);
if (pMO) {
MOID rootMOID = pMO->GetRootID();
if (checkMOID != g_NoMOID) {
for (std::deque<Actor*>::iterator itr = m_Actors.begin(); !found && itr != m_Actors.end(); ++itr) {
if ((*itr)->GetID() == checkMOID || (*itr)->GetID() == rootMOID) {
found = true;
break;
}
}
// Check actors just added this frame
if (!found) {
for (std::deque<Actor*>::iterator itr = m_AddedActors.begin(); !found && itr != m_AddedActors.end(); ++itr) {
if ((*itr)->GetID() == checkMOID || (*itr)->GetID() == rootMOID) {
found = true;
break;
}
}
}
}
}
return found;
}
int MovableMan::GetContiguousActorID(const Actor* actor) const {
auto itr = m_ContiguousActorIDs.find(actor);
if (itr == m_ContiguousActorIDs.end()) {
return -1;
}
return itr->second;
}
MOID MovableMan::GetRootMOID(MOID checkMOID) {
MovableObject* pMO = GetMOFromID(checkMOID);
if (pMO)
return pMO->GetRootID();
return g_NoMOID;
}
bool MovableMan::RemoveMO(MovableObject* pMOToRem) {
if (pMOToRem) {
if (RemoveActor(pMOToRem))
return true;
if (RemoveItem(pMOToRem))
return true;
if (RemoveParticle(pMOToRem))
return true;
}
return false;
}
int MovableMan::KillAllTeamActors(int teamToKill) const {
int killCount = 0;
for (std::deque<Actor*> actorList: {m_Actors, m_AddedActors}) {
for (Actor* actor: actorList) {