-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
CElement.cpp
1602 lines (1321 loc) · 46.2 KB
/
CElement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CElement.cpp
* PURPOSE: Base entity (element) class
* DEVELOPERS: Christian Myhre Lundheim <>
* Ed Lyons <>
* Jax <>
* Cecill Etheredge <>
* lil_Toady <>
* Alberto Alonso <rydencillo@gmail.com>
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
extern CGame* g_pGame;
#if defined(_MSC_VER)
#pragma warning( disable : 4355 ) // warning C4355: 'this' : used in base member initializer list
#endif
CElement::CElement ( CElement* pParent, CXMLNode* pNode )
{
// Allocate us an unique ID
m_ID = CElementIDs::PopUniqueID ( this );
// Init
m_pParent = pParent;
m_pXMLNode = pNode;
m_pElementGroup = NULL;
m_bCallPropagationEnabled = true;
m_iType = CElement::UNKNOWN;
m_strName = "";
m_strTypeName = "unknown";
m_bIsBeingDeleted = false;
m_usDimension = 0;
m_ucSyncTimeContext = 1;
m_ucInterior = 0;
m_bDoubleSided = false;
m_bUpdatingSpatialData = false;
m_pChildrenListSnapshot = NULL;
m_uiChildrenListSnapshotRevision = 0;
// Store the line
if ( m_pXMLNode )
{
m_uiLine = m_pXMLNode->GetLine ();
}
else
{
m_uiLine = 0;
}
// Add us to our parent's list
if ( pParent )
{
pParent->m_Children.push_back ( this );
}
m_uiTypeHash = GetTypeHashFromString ( m_strTypeName.c_str () );
if ( m_pParent )
CElement::AddEntityFromRoot ( m_uiTypeHash, this );
// Make an event manager for us
m_pEventManager = new CMapEventManager;
m_pCustomData = new CCustomData;
m_pAttachedTo = NULL;
}
CElement::~CElement ( void )
{
// Get rid of the children elements
ClearChildren ();
SetParentObject ( NULL );
// Remove ourselves from our element group
if ( m_pElementGroup )
m_pElementGroup->Remove ( this );
// Delete our event manager
delete m_pCustomData;
delete m_pEventManager;
// Unreference us from what's referencing us
list < CPerPlayerEntity* > ::const_iterator iter = m_ElementReferenced.begin ();
for ( ; iter != m_ElementReferenced.end (); iter++ )
{
(*iter)->m_ElementReferences.remove ( this );
}
RemoveAllCollisions ();
// Null all camera elements referencing us
std::list < CPlayerCamera* > cloneFollowingCameras = m_FollowingCameras;
for ( std::list < CPlayerCamera* > ::const_iterator iter = cloneFollowingCameras.begin () ; iter != cloneFollowingCameras.end () ; iter++ )
{
(*iter)->SetTarget ( NULL );
}
if ( m_pAttachedTo )
m_pAttachedTo->RemoveAttachedElement ( this );
list < CElement* > ::iterator iterAttached = m_AttachedElements.begin ();
for ( ; iterAttached != m_AttachedElements.end () ; iterAttached++ )
{
// Make sure our attached element stores it's current position
(*iterAttached)->GetPosition ();
// Unlink it
(*iterAttached)->m_pAttachedTo = NULL;
}
list < CPed * > ::iterator iterUsers = m_OriginSourceUsers.begin ();
for ( ; iterUsers != m_OriginSourceUsers.end () ; iterUsers++ )
{
CPed* pPed = *iterUsers;
if ( pPed->m_pContactElement == this )
pPed->m_pContactElement = NULL;
}
// Remove from spatial database
GetSpatialDatabase ()->RemoveEntity ( this );
if ( GetID() != INVALID_ELEMENT_ID && GetID() >= MAX_SERVER_ELEMENTS )
CLogger::ErrorPrintf( "ERROR: Element ID is incorrect (%08x) (Type:%d)\n", GetID().Value(), GetType() );
// Deallocate our unique ID
CElementIDs::PushUniqueID ( this );
// Remove our reference from the element deleter
g_pGame->GetElementDeleter ()->Unreference ( this );
// Ensure nothing has inadvertently set a parent
assert ( m_pParent == NULL );
CElementRefManager::OnElementDelete ( this );
SAFE_RELEASE( m_pChildrenListSnapshot );
}
const CVector & CElement::GetPosition ( void )
{
if ( m_pAttachedTo ) GetAttachedPosition ( m_vecPosition );
return m_vecPosition;
}
void CElement::GetMatrix( CMatrix& matrix )
{
matrix = CMatrix();
matrix.vPos = GetPosition();
}
void CElement::SetMatrix( const CMatrix& matrix )
{
SetPosition( matrix.vPos );
}
// Static function
uint CElement::GetTypeHashFromString ( const SString& strTypeName )
{
// Standard types use unique index upto EElementType::UNKNOWN
EElementType elementType;
if ( StringToEnum ( strTypeName, elementType ) )
return elementType;
// Custom types use name hash. TODO: Make it use a unique index instead
uint uiTypeHash = HashString ( strTypeName );
uiTypeHash = ( uiTypeHash % 0xFFFFFF00 ) + CElement::UNKNOWN + 1;
return uiTypeHash;
}
void CElement::SetTypeName ( const std::string& strTypeName )
{
CElement::RemoveEntityFromRoot ( m_uiTypeHash, this );
m_uiTypeHash = GetTypeHashFromString ( strTypeName.c_str () );
m_strTypeName = strTypeName;
if ( m_pParent )
CElement::AddEntityFromRoot ( m_uiTypeHash, this );
}
CElement* CElement::FindChild ( const char* szName, unsigned int uiIndex, bool bRecursive )
{
assert ( szName );
// Is it our name?
unsigned int uiCurrentIndex = 0;
if ( strcmp ( szName, m_strName.c_str () ) == 0 )
{
if ( uiIndex == 0 )
{
return this;
}
else
{
++uiCurrentIndex;
}
}
// Find it among our children
return FindChildIndex ( szName, uiIndex, uiCurrentIndex, bRecursive );
}
CElement* CElement::FindChildByType ( const char* szType, unsigned int uiIndex, bool bRecursive )
{
assert ( szType );
// Is it our type?
unsigned int uiCurrentIndex = 0;
if ( strcmp ( szType, GetTypeName ().c_str () ) == 0 )
{
if ( uiIndex == 0 )
{
return this;
}
else
{
++uiCurrentIndex;
}
}
// Find it among our children
unsigned int uiTypeHash = GetTypeHashFromString ( szType );
return FindChildByTypeIndex ( uiTypeHash, uiIndex, uiCurrentIndex, bRecursive );
}
void CElement::FindAllChildrenByType ( const char* szType, lua_State* pLua )
{
assert ( szType );
assert ( pLua );
// Add all children of the given type to the table
unsigned int uiIndex = 0;
unsigned int uiTypeHash = GetTypeHashFromString ( szType );
if ( this == g_pGame->GetMapManager ()->GetRootElement () )
{
GetEntitiesFromRoot ( uiTypeHash, pLua );
}
else
{
FindAllChildrenByTypeIndex ( uiTypeHash, pLua, uiIndex );
}
}
// Includes this (if the type matches), unless it is the root element.
// - Fast if called on root or an end node.
void CElement::GetDescendantsByType ( std::vector < CElement* >& outResult, EElementType elementType )
{
// Type hash is the same as element type up to EElementType::UNKNOWN
unsigned int uiTypeHash = elementType;
if ( this == g_pGame->GetMapManager ()->GetRootElement () )
{
GetEntitiesFromRoot ( uiTypeHash, outResult );
}
else
{
if ( GetTypeHash () == uiTypeHash )
outResult.push_back ( this );
GetDescendantsByTypeSlow ( outResult, uiTypeHash );
}
}
void CElement::GetDescendantsByTypeSlow ( std::vector < CElement* >& outResult, uint uiTypeHash )
{
for ( CChildListType::const_iterator iter = m_Children.begin () ; iter != m_Children.end () ; ++iter )
{
CElement* pChild = *iter;
if ( pChild->GetTypeHash () == uiTypeHash )
outResult.push_back ( pChild );
if ( !pChild->m_Children.empty () )
pChild->GetDescendantsByTypeSlow ( outResult, uiTypeHash );
}
}
void CElement::GetChildren ( lua_State* pLua )
{
assert ( pLua );
// Add all our children to the table on top of the given lua main's stack
unsigned int uiIndex = 0;
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
// Add it to the table
lua_pushnumber ( pLua, ++uiIndex );
lua_pushelement ( pLua, *iter );
lua_settable ( pLua, -3 );
}
}
void CElement::GetChildrenByType ( const char* szType, lua_State* pLua )
{
assert ( szType );
assert ( pLua );
// Add all our children to the table on top of the given lua main's stack
unsigned int uiIndex = 0;
unsigned int uiTypeHash = GetTypeHashFromString ( szType );
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
// Name matches?
if ( (*iter)->GetTypeHash() == uiTypeHash )
{
// Add it to the table
lua_pushnumber ( pLua, ++uiIndex );
lua_pushelement ( pLua, *iter );
lua_settable ( pLua, -3 );
}
}
}
// Also returns true if the element is the same
bool CElement::IsMyChild ( CElement* pElement, bool bRecursive )
{
return pElement && pElement->IsMyParent ( this, bRecursive );
}
// Also returns true if the element is the same
bool CElement::IsMyParent ( CElement* pElement, bool bRecursive )
{
// Is he us?
if ( pElement == this )
return true;
if ( bRecursive && pElement && m_pParent && m_pParent->IsMyParent ( pElement, true ) )
return true;
return false;
}
void CElement::ClearChildren ( void )
{
// Sanity check
assert ( m_pParent != this );
// Process our children - Move up to our parent
if ( !m_Children.empty () ) // This check reduces cpu usage when unloading large maps (due to recursion)
{
while ( !m_Children.empty () )
(*m_Children.begin())->SetParentObject ( m_pParent, false );
// Do this at the end for speed
if ( CElement* pRoot = GetRootElement () )
pRoot->UpdatePerPlayerEntities ();
}
}
CElement* CElement::SetParentObject ( CElement* pParent, bool bUpdatePerPlayerEntities )
{
// Different parent?
if ( pParent != m_pParent )
{
// Already got a parent?
if ( m_pParent )
{
// Call the on children remove on our current parent
m_pParent->OnSubtreeRemove ( this );
// Eventually unreference us from the previous parent entity
m_pParent->m_Children.remove ( this );
}
// Get into/out-of FromRoot info
bool bOldFromRoot = CElement::IsFromRoot ( m_pParent );
bool bNewFromRoot = CElement::IsFromRoot ( pParent );
// Moving out of FromRoot?
if ( bOldFromRoot && !bNewFromRoot )
CElement::RemoveEntityFromRoot ( m_uiTypeHash, this );
// Grab the root element now
CElement* pRoot;
if ( pParent )
{
pRoot = pParent->GetRootElement ();
}
else
{
assert ( m_pParent );
pRoot = m_pParent->GetRootElement ();
}
// Set the new parent
m_pParent = pParent;
// New parent?
if ( pParent )
{
// Add us to the new parent's child list
pParent->m_Children.push_back ( this );
// Moving into FromRoot?
if ( !bOldFromRoot && bNewFromRoot )
CElement::AddEntityFromRoot ( m_uiTypeHash, this );
// Call the on children add event on the new parent
pParent->OnSubtreeAdd ( this );
}
// Update all per-player references from the root and down
if ( bUpdatePerPlayerEntities )
pRoot->UpdatePerPlayerEntities ();
}
return pParent;
}
void CElement::SetXMLNode ( CXMLNode* pNode )
{
// Set the node
m_pXMLNode = pNode;
// If the node exists, set our line
if ( pNode )
{
m_uiLine = pNode->GetLine ();
}
else
{
m_uiLine = 0;
}
};
bool CElement::AddEvent ( CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction, bool bPropagated, EEventPriorityType eventPriority, float fPriorityMod )
{
return m_pEventManager->Add ( pLuaMain, szName, iLuaFunction, bPropagated, eventPriority, fPriorityMod );
}
bool CElement::CallEvent ( const char* szName, const CLuaArguments& Arguments, CPlayer* pCaller )
{
if ( !g_pGame->GetDebugHookManager()->OnPreEvent( szName, Arguments, this, pCaller ) )
return false;
CEvents* pEvents = g_pGame->GetEvents();
// Make sure our event-manager knows we're about to call an event
pEvents->PreEventPulse ();
// Call the event on our parents/us first
CallParentEvent ( szName, Arguments, this, pCaller );
// Call it on all our children
if ( g_pGame->GetConfig()->IsEnableDiagnostic("CallEvent") )
CallEventNoParentSlow ( szName, Arguments, this, pCaller );
else
CallEventNoParent ( szName, Arguments, this, pCaller );
// Tell the event manager that we're done calling the event
pEvents->PostEventPulse ();
g_pGame->GetDebugHookManager()->OnPostEvent( szName, Arguments, this, pCaller );
// Return whether our event was cancelled or not
return ( !pEvents->WasEventCancelled () );
}
bool CElement::DeleteEvent ( CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction )
{
return m_pEventManager->Delete ( pLuaMain, szName, iLuaFunction );
}
void CElement::DeleteEvents ( CLuaMain* pLuaMain, bool bRecursive )
{
// Delete it from our events
m_pEventManager->Delete ( pLuaMain );
// Delete it from all our children's events
if ( bRecursive )
{
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
(*iter)->DeleteEvents ( pLuaMain, true );
}
}
}
void CElement::DeleteAllEvents ( void )
{
m_pEventManager->DeleteAll ();
}
void CElement::ReadCustomData ( CEvents* pEvents )
{
assert ( pEvents );
// Got an XML node?
if ( m_pXMLNode )
{
// Iterate the attributes of our XML node
CXMLAttributes* pAttributes = &(m_pXMLNode->GetAttributes ());
unsigned int uiAttributeCount = pAttributes->Count ();
for ( unsigned int uiIndex = 0; uiIndex < uiAttributeCount; uiIndex++ )
{
// Grab the node (we can assume it exists here)
CXMLAttribute* pAttribute = pAttributes->Get ( uiIndex );
// Make a lua argument from it and set the content
CLuaArguments args;
if ( !args.ReadFromJSONString ( pAttribute->GetValue ().c_str() ) )
args.PushString ( pAttribute->GetValue ().c_str () );
// Don't trigger onElementDataChanged event
SetCustomData ( pAttribute->GetName ().c_str (), *args[0], g_pGame->GetConfig ()->GetSyncMapElementData (), NULL, false );
}
}
}
CLuaArgument* CElement::GetCustomData ( const char* szName, bool bInheritData, bool* pbIsSynced )
{
assert ( szName );
// Grab it and return a pointer to the variable
SCustomData* pData = m_pCustomData->Get ( szName );
if ( pData )
{
if ( pbIsSynced )
*pbIsSynced = pData->bSynchronized;
return &pData->Variable;
}
// If none, try returning parent's custom data
if ( bInheritData && m_pParent )
{
return m_pParent->GetCustomData ( szName, true, pbIsSynced );
}
// None available
return NULL;
}
CLuaArguments* CElement::GetAllCustomData ( CLuaArguments * table )
{
assert ( table );
// Grab it and return a pointer to the variable
map < string, SCustomData > :: const_iterator iter = m_pCustomData->IterBegin();
for ( ; iter != m_pCustomData->IterEnd(); iter++ )
{
table->PushString( iter->first.c_str () ); // key
table->PushArgument ( iter->second.Variable ); // value
}
return table;
}
bool CElement::GetCustomDataString ( const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData )
{
// Grab the custom data variable
CLuaArgument* pData = GetCustomData ( szName, bInheritData );
if ( pData )
{
// Make sure it gets 0 terminated
sizeBuffer -= 1;
pOut [sizeBuffer] = 0;
// Write the content depending on what type it is
int iType = pData->GetType ();
if ( iType == LUA_TSTRING )
{
strncpy ( pOut, pData->GetString ().c_str (), sizeBuffer );
}
else if ( iType == LUA_TNUMBER )
{
snprintf ( pOut, sizeBuffer, "%f", pData->GetNumber () );
}
else if ( iType == LUA_TBOOLEAN )
{
snprintf ( pOut, sizeBuffer, "%u", pData->GetBoolean () );
}
else if ( iType == LUA_TNIL )
{
pOut [0] = 0;
}
else
{
return false;
}
return true;
}
return false;
}
bool CElement::GetCustomDataInt ( const char* szName, int& iOut, bool bInheritData )
{
// Grab the custom data variable
CLuaArgument* pData = GetCustomData ( szName, bInheritData );
if ( pData )
{
// Write the content depending on what type it is
int iType = pData->GetType ();
if ( iType == LUA_TSTRING )
{
iOut = atoi ( pData->GetString ().c_str () );
}
else if ( iType == LUA_TNUMBER )
{
iOut = static_cast < int > ( pData->GetNumber () );
}
else if ( iType == LUA_TBOOLEAN )
{
if ( pData->GetBoolean () )
{
iOut = 1;
}
else
{
iOut = 0;
}
}
else
{
return false;
}
return true;
}
return false;
}
bool CElement::GetCustomDataFloat ( const char* szName, float& fOut, bool bInheritData )
{
// Grab the custom data variable
CLuaArgument* pData = GetCustomData ( szName, bInheritData );
if ( pData )
{
// Write the content depending on what type it is
int iType = pData->GetType ();
if ( iType == LUA_TSTRING )
{
fOut = static_cast < float > ( atof ( pData->GetString ().c_str () ) );
}
else if ( iType == LUA_TNUMBER )
{
fOut = static_cast < float > ( pData->GetNumber () );
}
else
{
return false;
}
return true;
}
return false;
}
bool CElement::GetCustomDataBool ( const char* szName, bool& bOut, bool bInheritData )
{
// Grab the custom data variable
CLuaArgument* pData = GetCustomData ( szName, bInheritData );
if ( pData )
{
// Write the content depending on what type it is
int iType = pData->GetType ();
if ( iType == LUA_TSTRING )
{
const char* szString = pData->GetString ().c_str ();
if ( strcmp ( szString, "true" ) == 0 || strcmp ( szString, "1" ) == 0 )
{
bOut = true;
}
else if ( strcmp ( szString, "false" ) == 0 || strcmp ( szString, "0" ) == 0 )
{
bOut = false;
}
else
{
return false;
}
}
else if ( iType == LUA_TNUMBER )
{
int iNumber = static_cast < int > ( pData->GetNumber () );
if ( iNumber == 1 )
{
bOut = true;
}
else if ( iNumber == 0 )
{
bOut = false;
}
else
{
return false;
}
}
else if ( iType == LUA_TBOOLEAN )
{
bOut = pData->GetBoolean ();
}
else
{
return false;
}
return true;
}
return false;
}
void CElement::SetCustomData ( const char* szName, const CLuaArgument& Variable, bool bSynchronized, CPlayer* pClient, bool bTriggerEvent )
{
assert ( szName );
if ( strlen ( szName ) > MAX_CUSTOMDATA_NAME_LENGTH )
{
// Don't allow it to be set if the name is too long
CLogger::ErrorPrintf ( "Custom data name too long (%s)\n", *SStringX ( szName ).Left ( MAX_CUSTOMDATA_NAME_LENGTH + 1 ) );
return;
}
// Grab the old variable
CLuaArgument oldVariable;
const SCustomData * pData = m_pCustomData->Get ( szName );
if ( pData )
{
oldVariable = pData->Variable;
}
// Set the new data
m_pCustomData->Set ( szName, Variable, bSynchronized );
if ( bTriggerEvent )
{
// Trigger the onElementDataChange event on us
CLuaArguments Arguments;
Arguments.PushString ( szName );
Arguments.PushArgument ( oldVariable );
Arguments.PushArgument ( Variable );
CallEvent ( "onElementDataChange", Arguments, pClient );
}
}
void CElement::DeleteCustomData ( const char* szName )
{
// Grab the old variable
SCustomData * pData = m_pCustomData->Get ( szName );
if ( pData )
{
CLuaArgument oldVariable;
oldVariable = pData->Variable;
// Delete the custom data
m_pCustomData->Delete ( szName );
// Trigger the onElementDataChange event on us
CLuaArguments Arguments;
Arguments.PushString ( szName );
Arguments.PushArgument ( oldVariable );
Arguments.PushArgument ( CLuaArgument() ); // Use nil as the new value to indicate the data has been removed
CallEvent ( "onElementDataChange", Arguments );
}
}
// Used to send the root element data when a player joins
void CElement::SendAllCustomData ( CPlayer* pPlayer )
{
for ( map < std::string, SCustomData >::const_iterator iter = m_pCustomData->SyncedIterBegin() ; iter != m_pCustomData->SyncedIterEnd(); ++iter )
{
const std::string& strName = iter->first;
const SCustomData& customData = iter->second;
if ( customData.bSynchronized )
{
// Tell our clients to update their data
unsigned short usNameLength = static_cast < unsigned short > ( strName.length () );
CBitStream BitStream;
BitStream.pBitStream->WriteCompressed ( usNameLength );
BitStream.pBitStream->Write ( strName.c_str (), usNameLength );
customData.Variable.WriteToBitStream ( *BitStream.pBitStream );
pPlayer->Send ( CElementRPCPacket ( this, SET_ELEMENT_DATA, *BitStream.pBitStream ) );
}
}
}
CXMLNode* CElement::OutputToXML ( CXMLNode* pNodeParent )
{
// Create a new node for this element
CXMLNode * pNode = pNodeParent->CreateSubNode ( GetTypeName ().c_str () );
// Output the custom data values to it as arguments
m_pCustomData->OutputToXML ( pNode );
// Go through each child element and call this function on it
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
(*iter)->OutputToXML ( pNode );
}
return pNode;
}
void CElement::CleanUpForVM ( CLuaMain* pLuaMain, bool bRecursive )
{
// Delete all our events and custom datas attached to that VM
DeleteEvents ( pLuaMain, false );
//DeleteCustomData ( pLuaMain, false ); * Removed to keep custom data global
// If recursive, do it on our children too
if ( bRecursive )
{
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
(*iter)->CleanUpForVM ( pLuaMain, true );
}
}
}
bool CElement::LoadFromCustomData ( CEvents* pEvents )
{
assert ( pEvents );
// Read out all the attributes into our custom data records
ReadCustomData ( pEvents );
// Grab the "id" custom data into our m_strName member
char szBuf[MAX_ELEMENT_NAME_LENGTH + 1] = {0};
GetCustomDataString ( "id", szBuf, MAX_ELEMENT_NAME_LENGTH, false );
m_strName = szBuf;
// Grab the attaching custom data
szBuf[0] = 0;
GetCustomDataString ( "attachTo", szBuf, MAX_ELEMENT_NAME_LENGTH, true );
m_strAttachToID = szBuf;
GetCustomDataFloat ( "attachX", m_vecAttachedPosition.fX, true );
GetCustomDataFloat ( "attachY", m_vecAttachedPosition.fY, true );
GetCustomDataFloat ( "attachZ", m_vecAttachedPosition.fZ, true );
// Load the special attributes from our custom data
return ReadSpecialData ();
}
void CElement::OnSubtreeAdd ( CElement* pElement )
{
// Call the event on the elements that references us
if ( !m_ElementReferenced.empty () ) // This check reduces cpu usage when loading large maps (due to recursion)
{
list < CPerPlayerEntity* > ::const_iterator iter = m_ElementReferenced.begin ();
for ( ; iter != m_ElementReferenced.end (); iter++ )
{
(*iter)->OnReferencedSubtreeAdd ( pElement );
}
}
// Call the event on our parent
if ( m_pParent )
{
m_pParent->OnSubtreeAdd ( pElement );
}
}
void CElement::OnSubtreeRemove ( CElement* pElement )
{
// Call the event on the elements that references us
if ( !m_ElementReferenced.empty () ) // This check reduces cpu usage when unloading large maps (due to recursion)
{
list < CPerPlayerEntity* > ::const_iterator iter = m_ElementReferenced.begin ();
for ( ; iter != m_ElementReferenced.end (); iter++ )
{
(*iter)->OnReferencedSubtreeRemove ( pElement );
}
}
// Call the event on our parent
if ( m_pParent )
{
m_pParent->OnSubtreeRemove ( pElement );
}
}
void CElement::UpdatePerPlayerEntities ( void )
{
// Call a virtual method that will end up CPerPlayerEntity or some other class using references
UpdatePerPlayer ();
// Call ourselves on our children
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
(*iter)->UpdatePerPlayerEntities ();
}
}
CElement* CElement::GetRootElement ( void )
{
if ( m_pParent )
{
return m_pParent->GetRootElement ();
}
return this;
}
CElement* CElement::FindChildIndex ( const char* szName, unsigned int uiIndex, unsigned int& uiCurrentIndex, bool bRecursive )
{
assert ( szName );
// Look among our children
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
// Name matches?
if ( strcmp ( (*iter)->GetName ().c_str (), szName ) == 0 )
{
// Does the index match? If it doesn't, increment it and keep searching
if ( uiIndex == uiCurrentIndex )
{
return *iter;
}
else
{
++uiCurrentIndex;
}
}
// Tell this child to search too if recursive
if ( bRecursive )
{
CElement* pElement = (*iter)->FindChildIndex ( szName, uiIndex, uiCurrentIndex, true );
if ( pElement )
{
if ( pElement->IsBeingDeleted() )
{
// If it's being deleted right now we cannot return it.
// Since we found a match we have to abort the search here.
return NULL;
}
return pElement;
}
}
}
// Doesn't exist within us
return NULL;
}
CElement* CElement::FindChildByTypeIndex ( unsigned int uiTypeHash, unsigned int uiIndex, unsigned int& uiCurrentIndex, bool bRecursive )
{
// Look among our children
CChildListType ::const_iterator iter = m_Children.begin ();
for ( ; iter != m_Children.end (); iter++ )
{
// Name matches?
if ( (*iter)->GetTypeHash() == uiTypeHash )
{
// Does the index match? If it doesn't, increment it and keep searching
if ( uiIndex == uiCurrentIndex )
{
return *iter;
}
else