-
Notifications
You must be signed in to change notification settings - Fork 10
/
Entity.cpp
5403 lines (4556 loc) · 131 KB
/
Entity.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
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "sys/platform.h"
#include "idlib/geometry/JointTransform.h"
#include "idlib/LangDict.h"
#include "framework/async/NetworkSystem.h"
#include "framework/DeclEntityDef.h"
#include "renderer/ModelManager.h"
#include "gamesys/SysCvar.h"
#include "physics/Physics_Parametric.h"
#include "physics/Physics_Actor.h"
#include "script/Script_Thread.h"
#include "Fx.h"
#include "AFEntity.h"
#include "Player.h"
#include "Mover.h"
#include "WorldSpawn.h"
#include "SmokeParticles.h"
#include "Entity.h"
/*
===============================================================================
idEntity
===============================================================================
*/
// overridable events
const idEventDef EV_PostSpawn( "<postspawn>", NULL );
const idEventDef EV_FindTargets( "<findTargets>", NULL );
const idEventDef EV_Touch( "<touch>", "et" );
const idEventDef EV_GetName( "getName", NULL, 's' );
const idEventDef EV_SetName( "setName", "s" );
const idEventDef EV_Activate( "activate", "e" );
const idEventDef EV_ActivateTargets( "activateTargets", "e" );
const idEventDef EV_NumTargets( "numTargets", NULL, 'f' );
const idEventDef EV_GetTarget( "getTarget", "f", 'e' );
const idEventDef EV_RandomTarget( "randomTarget", "s", 'e' );
const idEventDef EV_Bind( "bind", "e" );
const idEventDef EV_BindPosition( "bindPosition", "e" );
const idEventDef EV_BindToJoint( "bindToJoint", "esf" );
const idEventDef EV_Unbind( "unbind", NULL );
const idEventDef EV_RemoveBinds( "removeBinds" );
const idEventDef EV_SpawnBind( "<spawnbind>", NULL );
const idEventDef EV_SetOwner( "setOwner", "e" );
const idEventDef EV_SetModel( "setModel", "s" );
const idEventDef EV_SetSkin( "setSkin", "s" );
const idEventDef EV_GetWorldOrigin( "getWorldOrigin", NULL, 'v' );
const idEventDef EV_SetWorldOrigin( "setWorldOrigin", "v" );
const idEventDef EV_GetOrigin( "getOrigin", NULL, 'v' );
const idEventDef EV_SetOrigin( "setOrigin", "v" );
const idEventDef EV_GetAngles( "getAngles", NULL, 'v' );
const idEventDef EV_SetAngles( "setAngles", "v" );
const idEventDef EV_GetLinearVelocity( "getLinearVelocity", NULL, 'v' );
const idEventDef EV_SetLinearVelocity( "setLinearVelocity", "v" );
const idEventDef EV_GetAngularVelocity( "getAngularVelocity", NULL, 'v' );
const idEventDef EV_SetAngularVelocity( "setAngularVelocity", "v" );
const idEventDef EV_GetSize( "getSize", NULL, 'v' );
const idEventDef EV_SetSize( "setSize", "vv" );
const idEventDef EV_GetMins( "getMins", NULL, 'v' );
const idEventDef EV_GetMaxs( "getMaxs", NULL, 'v' );
const idEventDef EV_IsHidden( "isHidden", NULL, 'd' );
const idEventDef EV_Hide( "hide", NULL );
const idEventDef EV_Show( "show", NULL );
const idEventDef EV_Touches( "touches", "E", 'd' );
const idEventDef EV_ClearSignal( "clearSignal", "d" );
const idEventDef EV_GetShaderParm( "getShaderParm", "d", 'f' );
const idEventDef EV_SetShaderParm( "setShaderParm", "df" );
const idEventDef EV_SetShaderParms( "setShaderParms", "ffff" );
const idEventDef EV_SetColor( "setColor", "fff" );
const idEventDef EV_GetColor( "getColor", NULL, 'v' );
const idEventDef EV_CacheSoundShader( "cacheSoundShader", "s" );
const idEventDef EV_StartSoundShader( "startSoundShader", "sd", 'f' );
const idEventDef EV_StartSound( "startSound", "sdd", 'f' );
const idEventDef EV_StopSound( "stopSound", "dd" );
const idEventDef EV_FadeSound( "fadeSound", "dff" );
const idEventDef EV_SetGuiParm( "setGuiParm", "ss" );
const idEventDef EV_SetGuiFloat( "setGuiFloat", "sf" );
const idEventDef EV_GetNextKey( "getNextKey", "ss", 's' );
const idEventDef EV_SetKey( "setKey", "ss" );
const idEventDef EV_GetKey( "getKey", "s", 's' );
const idEventDef EV_GetIntKey( "getIntKey", "s", 'f' );
const idEventDef EV_GetFloatKey( "getFloatKey", "s", 'f' );
const idEventDef EV_GetVectorKey( "getVectorKey", "s", 'v' );
const idEventDef EV_GetEntityKey( "getEntityKey", "s", 'e' );
const idEventDef EV_RestorePosition( "restorePosition" );
const idEventDef EV_UpdateCameraTarget( "<updateCameraTarget>", NULL );
const idEventDef EV_DistanceTo( "distanceTo", "E", 'f' );
const idEventDef EV_DistanceToPoint( "distanceToPoint", "v", 'f' );
const idEventDef EV_StartFx( "startFx", "s" );
const idEventDef EV_HasFunction( "hasFunction", "s", 'd' );
const idEventDef EV_CallFunction( "callFunction", "s" );
const idEventDef EV_SetNeverDormant( "setNeverDormant", "d" );
ABSTRACT_DECLARATION( idClass, idEntity )
EVENT( EV_GetName, idEntity::Event_GetName )
EVENT( EV_SetName, idEntity::Event_SetName )
EVENT( EV_FindTargets, idEntity::Event_FindTargets )
EVENT( EV_ActivateTargets, idEntity::Event_ActivateTargets )
EVENT( EV_NumTargets, idEntity::Event_NumTargets )
EVENT( EV_GetTarget, idEntity::Event_GetTarget )
EVENT( EV_RandomTarget, idEntity::Event_RandomTarget )
EVENT( EV_BindToJoint, idEntity::Event_BindToJoint )
EVENT( EV_RemoveBinds, idEntity::Event_RemoveBinds )
EVENT( EV_Bind, idEntity::Event_Bind )
EVENT( EV_BindPosition, idEntity::Event_BindPosition )
EVENT( EV_Unbind, idEntity::Event_Unbind )
EVENT( EV_SpawnBind, idEntity::Event_SpawnBind )
EVENT( EV_SetOwner, idEntity::Event_SetOwner )
EVENT( EV_SetModel, idEntity::Event_SetModel )
EVENT( EV_SetSkin, idEntity::Event_SetSkin )
EVENT( EV_GetShaderParm, idEntity::Event_GetShaderParm )
EVENT( EV_SetShaderParm, idEntity::Event_SetShaderParm )
EVENT( EV_SetShaderParms, idEntity::Event_SetShaderParms )
EVENT( EV_SetColor, idEntity::Event_SetColor )
EVENT( EV_GetColor, idEntity::Event_GetColor )
EVENT( EV_IsHidden, idEntity::Event_IsHidden )
EVENT( EV_Hide, idEntity::Event_Hide )
EVENT( EV_Show, idEntity::Event_Show )
EVENT( EV_CacheSoundShader, idEntity::Event_CacheSoundShader )
EVENT( EV_StartSoundShader, idEntity::Event_StartSoundShader )
EVENT( EV_StartSound, idEntity::Event_StartSound )
EVENT( EV_StopSound, idEntity::Event_StopSound )
EVENT( EV_FadeSound, idEntity::Event_FadeSound )
EVENT( EV_GetWorldOrigin, idEntity::Event_GetWorldOrigin )
EVENT( EV_SetWorldOrigin, idEntity::Event_SetWorldOrigin )
EVENT( EV_GetOrigin, idEntity::Event_GetOrigin )
EVENT( EV_SetOrigin, idEntity::Event_SetOrigin )
EVENT( EV_GetAngles, idEntity::Event_GetAngles )
EVENT( EV_SetAngles, idEntity::Event_SetAngles )
EVENT( EV_GetLinearVelocity, idEntity::Event_GetLinearVelocity )
EVENT( EV_SetLinearVelocity, idEntity::Event_SetLinearVelocity )
EVENT( EV_GetAngularVelocity, idEntity::Event_GetAngularVelocity )
EVENT( EV_SetAngularVelocity, idEntity::Event_SetAngularVelocity )
EVENT( EV_GetSize, idEntity::Event_GetSize )
EVENT( EV_SetSize, idEntity::Event_SetSize )
EVENT( EV_GetMins, idEntity::Event_GetMins)
EVENT( EV_GetMaxs, idEntity::Event_GetMaxs )
EVENT( EV_Touches, idEntity::Event_Touches )
EVENT( EV_SetGuiParm, idEntity::Event_SetGuiParm )
EVENT( EV_SetGuiFloat, idEntity::Event_SetGuiFloat )
EVENT( EV_GetNextKey, idEntity::Event_GetNextKey )
EVENT( EV_SetKey, idEntity::Event_SetKey )
EVENT( EV_GetKey, idEntity::Event_GetKey )
EVENT( EV_GetIntKey, idEntity::Event_GetIntKey )
EVENT( EV_GetFloatKey, idEntity::Event_GetFloatKey )
EVENT( EV_GetVectorKey, idEntity::Event_GetVectorKey )
EVENT( EV_GetEntityKey, idEntity::Event_GetEntityKey )
EVENT( EV_RestorePosition, idEntity::Event_RestorePosition )
EVENT( EV_UpdateCameraTarget, idEntity::Event_UpdateCameraTarget )
EVENT( EV_DistanceTo, idEntity::Event_DistanceTo )
EVENT( EV_DistanceToPoint, idEntity::Event_DistanceToPoint )
EVENT( EV_StartFx, idEntity::Event_StartFx )
EVENT( EV_Thread_WaitFrame, idEntity::Event_WaitFrame )
EVENT( EV_Thread_Wait, idEntity::Event_Wait )
EVENT( EV_HasFunction, idEntity::Event_HasFunction )
EVENT( EV_CallFunction, idEntity::Event_CallFunction )
EVENT( EV_SetNeverDormant, idEntity::Event_SetNeverDormant )
END_CLASS
/*
================
UpdateGuiParms
================
*/
void UpdateGuiParms( idUserInterface *gui, const idDict *args ) {
if ( gui == NULL || args == NULL ) {
return;
}
const idKeyValue *kv = args->MatchPrefix( "gui_parm", NULL );
while( kv ) {
gui->SetStateString( kv->GetKey(), kv->GetValue() );
kv = args->MatchPrefix( "gui_parm", kv );
}
gui->SetStateBool( "noninteractive", args->GetBool( "gui_noninteractive" ) ) ;
gui->StateChanged( gameLocal.time );
}
/*
================
AddRenderGui
================
*/
void AddRenderGui( const char *name, idUserInterface **gui, const idDict *args ) {
const idKeyValue *kv = args->MatchPrefix( "gui_parm", NULL );
*gui = uiManager->FindGui( name, true, ( kv != NULL ) );
UpdateGuiParms( *gui, args );
}
/*
================
idGameEdit::ParseSpawnArgsToRenderEntity
parse the static model parameters
this is the canonical renderEntity parm parsing,
which should be used by dmap and the editor
================
*/
void idGameEdit::ParseSpawnArgsToRenderEntity( const idDict *args, renderEntity_t *renderEntity ) {
int i;
const char *temp;
idVec3 color;
float angle;
const idDeclModelDef *modelDef;
memset( renderEntity, 0, sizeof( *renderEntity ) );
temp = args->GetString( "model" );
modelDef = NULL;
if ( temp[0] != '\0' ) {
modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, temp, false ) );
if ( modelDef ) {
renderEntity->hModel = modelDef->ModelHandle();
}
if ( !renderEntity->hModel ) {
renderEntity->hModel = renderModelManager->FindModel( temp );
}
}
if ( renderEntity->hModel ) {
renderEntity->bounds = renderEntity->hModel->Bounds( renderEntity );
} else {
renderEntity->bounds.Zero();
}
temp = args->GetString( "skin" );
if ( temp[0] != '\0' ) {
renderEntity->customSkin = declManager->FindSkin( temp );
} else if ( modelDef ) {
renderEntity->customSkin = modelDef->GetDefaultSkin();
}
temp = args->GetString( "shader" );
if ( temp[0] != '\0' ) {
renderEntity->customShader = declManager->FindMaterial( temp );
}
args->GetVector( "origin", "0 0 0", renderEntity->origin );
// get the rotation matrix in either full form, or single angle form
if ( !args->GetMatrix( "rotation", "1 0 0 0 1 0 0 0 1", renderEntity->axis ) ) {
angle = args->GetFloat( "angle" );
if ( angle != 0.0f ) {
renderEntity->axis = idAngles( 0.0f, angle, 0.0f ).ToMat3();
} else {
renderEntity->axis.Identity();
}
}
renderEntity->referenceSound = NULL;
// get shader parms
args->GetVector( "_color", "1 1 1", color );
renderEntity->shaderParms[ SHADERPARM_RED ] = color[0];
renderEntity->shaderParms[ SHADERPARM_GREEN ] = color[1];
renderEntity->shaderParms[ SHADERPARM_BLUE ] = color[2];
renderEntity->shaderParms[ 3 ] = args->GetFloat( "shaderParm3", "1" );
renderEntity->shaderParms[ 4 ] = args->GetFloat( "shaderParm4", "0" );
renderEntity->shaderParms[ 5 ] = args->GetFloat( "shaderParm5", "0" );
renderEntity->shaderParms[ 6 ] = args->GetFloat( "shaderParm6", "0" );
renderEntity->shaderParms[ 7 ] = args->GetFloat( "shaderParm7", "0" );
renderEntity->shaderParms[ 8 ] = args->GetFloat( "shaderParm8", "0" );
renderEntity->shaderParms[ 9 ] = args->GetFloat( "shaderParm9", "0" );
renderEntity->shaderParms[ 10 ] = args->GetFloat( "shaderParm10", "0" );
renderEntity->shaderParms[ 11 ] = args->GetFloat( "shaderParm11", "0" );
// check noDynamicInteractions flag
renderEntity->noDynamicInteractions = args->GetBool( "noDynamicInteractions" );
// check noshadows flag
renderEntity->noShadow = args->GetBool( "noshadows" );
// check noselfshadows flag
renderEntity->noSelfShadow = args->GetBool( "noselfshadows" );
// init any guis, including entity-specific states
for( i = 0; i < MAX_RENDERENTITY_GUI; i++ ) {
temp = args->GetString( i == 0 ? "gui" : va( "gui%d", i + 1 ) );
if ( temp[ 0 ] != '\0' ) {
AddRenderGui( temp, &renderEntity->gui[ i ], args );
}
}
}
/*
================
idGameEdit::ParseSpawnArgsToRefSound
parse the sound parameters
this is the canonical refSound parm parsing,
which should be used by dmap and the editor
================
*/
void idGameEdit::ParseSpawnArgsToRefSound( const idDict *args, refSound_t *refSound ) {
const char *temp;
memset( refSound, 0, sizeof( *refSound ) );
refSound->parms.minDistance = args->GetFloat( "s_mindistance" );
refSound->parms.maxDistance = args->GetFloat( "s_maxdistance" );
refSound->parms.volume = args->GetFloat( "s_volume" );
refSound->parms.shakes = args->GetFloat( "s_shakes" );
args->GetVector( "origin", "0 0 0", refSound->origin );
refSound->referenceSound = NULL;
// if a diversity is not specified, every sound start will make
// a random one. Specifying diversity is usefull to make multiple
// lights all share the same buzz sound offset, for instance.
refSound->diversity = args->GetFloat( "s_diversity", "-1" );
refSound->waitfortrigger = args->GetBool( "s_waitfortrigger" );
if ( args->GetBool( "s_omni" ) ) {
refSound->parms.soundShaderFlags |= SSF_OMNIDIRECTIONAL;
}
if ( args->GetBool( "s_looping" ) ) {
refSound->parms.soundShaderFlags |= SSF_LOOPING;
}
if ( args->GetBool( "s_occlusion" ) ) {
refSound->parms.soundShaderFlags |= SSF_NO_OCCLUSION;
}
if ( args->GetBool( "s_global" ) ) {
refSound->parms.soundShaderFlags |= SSF_GLOBAL;
}
if ( args->GetBool( "s_unclamped" ) ) {
refSound->parms.soundShaderFlags |= SSF_UNCLAMPED;
}
refSound->parms.soundClass = args->GetInt( "s_soundClass" );
temp = args->GetString( "s_shader" );
if ( temp[0] != '\0' ) {
refSound->shader = declManager->FindSound( temp );
}
}
/*
===============
idEntity::UpdateChangeableSpawnArgs
Any key val pair that might change during the course of the game ( via a gui or whatever )
should be initialize here so a gui or other trigger can change something and have it updated
properly. An optional source may be provided if the values reside in an outside dictionary and
first need copied over to spawnArgs
===============
*/
void idEntity::UpdateChangeableSpawnArgs( const idDict *source ) {
int i;
const char *target;
if ( !source ) {
source = &spawnArgs;
}
cameraTarget = NULL;
target = source->GetString( "cameraTarget" );
if ( target && target[0] ) {
// update the camera taget
PostEventMS( &EV_UpdateCameraTarget, 0 );
}
for ( i = 0; i < MAX_RENDERENTITY_GUI; i++ ) {
UpdateGuiParms( renderEntity.gui[ i ], source );
}
}
/*
================
idEntity::idEntity
================
*/
idEntity::idEntity() {
entityNumber = ENTITYNUM_NONE;
entityDefNumber = -1;
spawnNode.SetOwner( this );
activeNode.SetOwner( this );
snapshotNode.SetOwner( this );
snapshotSequence = -1;
snapshotBits = 0;
thinkFlags = 0;
dormantStart = 0;
cinematic = false;
renderView = NULL;
cameraTarget = NULL;
health = 0;
physics = NULL;
bindMaster = NULL;
bindJoint = INVALID_JOINT;
bindBody = -1;
teamMaster = NULL;
teamChain = NULL;
signals = NULL;
memset( PVSAreas, 0, sizeof( PVSAreas ) );
numPVSAreas = -1;
memset( &fl, 0, sizeof( fl ) );
fl.neverDormant = true; // most entities never go dormant
memset( &renderEntity, 0, sizeof( renderEntity ) );
modelDefHandle = -1;
memset( &refSound, 0, sizeof( refSound ) );
mpGUIState = -1;
}
/*
================
idEntity::FixupLocalizedStrings
================
*/
void idEntity::FixupLocalizedStrings() {
for ( int i = 0; i < spawnArgs.GetNumKeyVals(); i++ ) {
const idKeyValue *kv = spawnArgs.GetKeyVal( i );
if ( idStr::Cmpn( kv->GetValue(), STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 ){
spawnArgs.Set( kv->GetKey(), common->GetLanguageDict()->GetString( kv->GetValue() ) );
}
}
}
/*
================
idEntity::Spawn
================
*/
void idEntity::Spawn( void ) {
int i;
const char *temp;
idVec3 origin;
idMat3 axis;
const idKeyValue *networkSync;
const char *classname;
const char *scriptObjectName;
gameLocal.RegisterEntity( this );
spawnArgs.GetString( "classname", NULL, &classname );
const idDeclEntityDef *def = gameLocal.FindEntityDef( classname, false );
if ( def ) {
entityDefNumber = def->Index();
}
FixupLocalizedStrings();
// parse static models the same way the editor display does
gameEdit->ParseSpawnArgsToRenderEntity( &spawnArgs, &renderEntity );
renderEntity.entityNum = entityNumber;
// go dormant within 5 frames so that when the map starts most monsters are dormant
dormantStart = gameLocal.time - DELAY_DORMANT_TIME + USERCMD_MSEC * 5;
origin = renderEntity.origin;
axis = renderEntity.axis;
// do the audio parsing the same way dmap and the editor do
gameEdit->ParseSpawnArgsToRefSound( &spawnArgs, &refSound );
// only play SCHANNEL_PRIVATE when sndworld->PlaceListener() is called with this listenerId
// don't spatialize sounds from the same entity
refSound.listenerId = entityNumber + 1;
cameraTarget = NULL;
temp = spawnArgs.GetString( "cameraTarget" );
if ( temp && temp[0] ) {
// update the camera taget
PostEventMS( &EV_UpdateCameraTarget, 0 );
}
for ( i = 0; i < MAX_RENDERENTITY_GUI; i++ ) {
UpdateGuiParms( renderEntity.gui[ i ], &spawnArgs );
}
fl.solidForTeam = spawnArgs.GetBool( "solidForTeam", "0" );
fl.neverDormant = spawnArgs.GetBool( "neverDormant", "0" );
fl.hidden = spawnArgs.GetBool( "hide", "0" );
if ( fl.hidden ) {
// make sure we're hidden, since a spawn function might not set it up right
PostEventMS( &EV_Hide, 0 );
}
cinematic = spawnArgs.GetBool( "cinematic", "0" );
networkSync = spawnArgs.FindKey( "networkSync" );
if ( networkSync ) {
fl.networkSync = ( atoi( networkSync->GetValue() ) != 0 );
}
#if 0
if ( !gameLocal.isClient ) {
// common->DPrintf( "NET: DBG %s - %s is synced: %s\n", spawnArgs.GetString( "classname", "" ), GetType()->classname, fl.networkSync ? "true" : "false" );
if ( spawnArgs.GetString( "classname", "" )[ 0 ] == '\0' && !fl.networkSync ) {
common->DPrintf( "NET: WRN %s entity, no classname, and no networkSync?\n", GetType()->classname );
}
}
#endif
// every object will have a unique name
temp = spawnArgs.GetString( "name", va( "%s_%s_%d", GetClassname(), spawnArgs.GetString( "classname" ), entityNumber ) );
SetName( temp );
// if we have targets, wait until all entities are spawned to get them
if ( spawnArgs.MatchPrefix( "target" ) || spawnArgs.MatchPrefix( "guiTarget" ) ) {
if ( gameLocal.GameState() == GAMESTATE_STARTUP ) {
PostEventMS( &EV_FindTargets, 0 );
} else {
// not during spawn, so it's ok to get the targets
FindTargets();
}
}
health = spawnArgs.GetInt( "health" );
InitDefaultPhysics( origin, axis );
SetOrigin( origin );
SetAxis( axis );
temp = spawnArgs.GetString( "model" );
if ( temp && *temp ) {
SetModel( temp );
}
if ( spawnArgs.GetString( "bind", "", &temp ) ) {
PostEventMS( &EV_SpawnBind, 0 );
}
// auto-start a sound on the entity
if ( refSound.shader && !refSound.waitfortrigger ) {
StartSoundShader( refSound.shader, SND_CHANNEL_ANY, 0, false, NULL );
}
// setup script object
if ( ShouldConstructScriptObjectAtSpawn() && spawnArgs.GetString( "scriptobject", NULL, &scriptObjectName ) ) {
if ( !scriptObject.SetType( scriptObjectName ) ) {
gameLocal.Error( "Script object '%s' not found on entity '%s'.", scriptObjectName, name.c_str() );
}
ConstructScriptObject();
}
}
/*
================
idEntity::~idEntity
================
*/
idEntity::~idEntity( void ) {
if ( gameLocal.GameState() != GAMESTATE_SHUTDOWN && !gameLocal.isClient && fl.networkSync && entityNumber >= MAX_CLIENTS ) {
idBitMsg msg;
byte msgBuf[ MAX_GAME_MESSAGE_SIZE ];
msg.Init( msgBuf, sizeof( msgBuf ) );
msg.WriteByte( GAME_RELIABLE_MESSAGE_DELETE_ENT );
msg.WriteBits( gameLocal.GetSpawnId( this ), 32 );
networkSystem->ServerSendReliableMessage( -1, msg );
}
DeconstructScriptObject();
scriptObject.Free();
if ( thinkFlags ) {
BecomeInactive( thinkFlags );
}
activeNode.Remove();
Signal( SIG_REMOVED );
// we have to set back the default physics object before unbinding because the entity
// specific physics object might be an entity variable and as such could already be destroyed.
SetPhysics( NULL );
// remove any entities that are bound to me
RemoveBinds();
// unbind from master
Unbind();
QuitTeam();
gameLocal.RemoveEntityFromHash( name.c_str(), this );
delete renderView;
renderView = NULL;
delete signals;
signals = NULL;
FreeModelDef();
FreeSoundEmitter( false );
gameLocal.UnregisterEntity( this );
}
/*
================
idEntity::Save
================
*/
void idEntity::Save( idSaveGame *savefile ) const {
int i, j;
savefile->WriteInt( entityNumber );
savefile->WriteInt( entityDefNumber );
// spawnNode and activeNode are restored by gameLocal
savefile->WriteInt( snapshotSequence );
savefile->WriteInt( snapshotBits );
savefile->WriteDict( &spawnArgs );
savefile->WriteString( name );
scriptObject.Save( savefile );
savefile->WriteInt( thinkFlags );
savefile->WriteInt( dormantStart );
savefile->WriteBool( cinematic );
savefile->WriteObject( cameraTarget );
savefile->WriteInt( health );
savefile->WriteInt( targets.Num() );
for( i = 0; i < targets.Num(); i++ ) {
targets[ i ].Save( savefile );
}
entityFlags_s flags = fl;
LittleBitField( &flags, sizeof( flags ) );
savefile->Write( &flags, sizeof( flags ) );
savefile->WriteRenderEntity( renderEntity );
savefile->WriteInt( modelDefHandle );
savefile->WriteRefSound( refSound );
savefile->WriteObject( bindMaster );
savefile->WriteJoint( bindJoint );
savefile->WriteInt( bindBody );
savefile->WriteObject( teamMaster );
savefile->WriteObject( teamChain );
savefile->WriteStaticObject( defaultPhysicsObj );
savefile->WriteInt( numPVSAreas );
for( i = 0; i < MAX_PVS_AREAS; i++ ) {
savefile->WriteInt( PVSAreas[ i ] );
}
if ( !signals ) {
savefile->WriteBool( false );
} else {
savefile->WriteBool( true );
for( i = 0; i < NUM_SIGNALS; i++ ) {
savefile->WriteInt( signals->signal[ i ].Num() );
for( j = 0; j < signals->signal[ i ].Num(); j++ ) {
savefile->WriteInt( signals->signal[ i ][ j ].threadnum );
savefile->WriteString( signals->signal[ i ][ j ].function->Name() );
}
}
}
savefile->WriteInt( mpGUIState );
}
/*
================
idEntity::Restore
================
*/
void idEntity::Restore( idRestoreGame *savefile ) {
int i, j;
int num;
idStr funcname;
savefile->ReadInt( entityNumber );
savefile->ReadInt( entityDefNumber );
// spawnNode and activeNode are restored by gameLocal
savefile->ReadInt( snapshotSequence );
savefile->ReadInt( snapshotBits );
savefile->ReadDict( &spawnArgs );
savefile->ReadString( name );
SetName( name );
scriptObject.Restore( savefile );
savefile->ReadInt( thinkFlags );
savefile->ReadInt( dormantStart );
savefile->ReadBool( cinematic );
savefile->ReadObject( reinterpret_cast<idClass *&>( cameraTarget ) );
savefile->ReadInt( health );
targets.Clear();
savefile->ReadInt( num );
targets.SetNum( num );
for( i = 0; i < num; i++ ) {
targets[ i ].Restore( savefile );
}
savefile->Read( &fl, sizeof( fl ) );
LittleBitField( &fl, sizeof( fl ) );
savefile->ReadRenderEntity( renderEntity );
savefile->ReadInt( modelDefHandle );
savefile->ReadRefSound( refSound );
savefile->ReadObject( reinterpret_cast<idClass *&>( bindMaster ) );
savefile->ReadJoint( bindJoint );
savefile->ReadInt( bindBody );
savefile->ReadObject( reinterpret_cast<idClass *&>( teamMaster ) );
savefile->ReadObject( reinterpret_cast<idClass *&>( teamChain ) );
savefile->ReadStaticObject( defaultPhysicsObj );
RestorePhysics( &defaultPhysicsObj );
savefile->ReadInt( numPVSAreas );
for( i = 0; i < MAX_PVS_AREAS; i++ ) {
savefile->ReadInt( PVSAreas[ i ] );
}
bool readsignals;
savefile->ReadBool( readsignals );
if ( readsignals ) {
signals = new signalList_t;
for( i = 0; i < NUM_SIGNALS; i++ ) {
savefile->ReadInt( num );
signals->signal[ i ].SetNum( num );
for( j = 0; j < num; j++ ) {
savefile->ReadInt( signals->signal[ i ][ j ].threadnum );
savefile->ReadString( funcname );
signals->signal[ i ][ j ].function = gameLocal.program.FindFunction( funcname );
if ( !signals->signal[ i ][ j ].function ) {
savefile->Error( "Function '%s' not found", funcname.c_str() );
}
}
}
}
savefile->ReadInt( mpGUIState );
// restore must retrieve modelDefHandle from the renderer
if ( modelDefHandle != -1 ) {
modelDefHandle = gameRenderWorld->AddEntityDef( &renderEntity );
}
}
/*
================
idEntity::GetEntityDefName
================
*/
const char * idEntity::GetEntityDefName( void ) const {
if ( entityDefNumber < 0 ) {
return "*unknown*";
}
return declManager->DeclByIndex( DECL_ENTITYDEF, entityDefNumber, false )->GetName();
}
/*
================
idEntity::SetName
================
*/
void idEntity::SetName( const char *newname ) {
if ( name.Length() ) {
gameLocal.RemoveEntityFromHash( name.c_str(), this );
gameLocal.program.SetEntity( name, NULL );
}
name = newname;
if ( name.Length() ) {
if ( ( name == "NULL" ) || ( name == "null_entity" ) ) {
gameLocal.Error( "Cannot name entity '%s'. '%s' is reserved for script.", name.c_str(), name.c_str() );
}
gameLocal.AddEntityToHash( name.c_str(), this );
gameLocal.program.SetEntity( name, this );
}
}
/*
================
idEntity::GetName
================
*/
const char * idEntity::GetName( void ) const {
return name.c_str();
}
/***********************************************************************
Thinking
***********************************************************************/
/*
================
idEntity::Think
================
*/
void idEntity::Think( void ) {
RunPhysics();
Present();
}
/*
================
idEntity::DoDormantTests
Monsters and other expensive entities that are completely closed
off from the player can skip all of their work
================
*/
bool idEntity::DoDormantTests( void ) {
if ( fl.neverDormant ) {
return false;
}
// if the monster area is not topologically connected to a player
if ( !gameLocal.InPlayerConnectedArea( this ) ) {
if ( dormantStart == 0 ) {
dormantStart = gameLocal.time;
}
if ( gameLocal.time - dormantStart < DELAY_DORMANT_TIME ) {
// just got closed off, don't go dormant yet
return false;
}
return true;
}
// the monster area is topologically connected to a player, but if
// the monster hasn't been woken up before, do the more precise PVS check
if ( !fl.hasAwakened ) {
if ( !gameLocal.InPlayerPVS( this ) ) {
return true; // stay dormant
}
}
// wake up
dormantStart = 0;
fl.hasAwakened = true; // only go dormant when area closed off now, not just out of PVS
return false;
}
/*
================
idEntity::CheckDormant
Monsters and other expensive entities that are completely closed
off from the player can skip all of their work
================
*/
bool idEntity::CheckDormant( void ) {
bool dormant;
dormant = DoDormantTests();
if ( dormant && !fl.isDormant ) {
fl.isDormant = true;
DormantBegin();
} else if ( !dormant && fl.isDormant ) {
fl.isDormant = false;
DormantEnd();
}
return dormant;
}
/*
================
idEntity::DormantBegin
called when entity becomes dormant
================
*/
void idEntity::DormantBegin( void ) {
}
/*
================
idEntity::DormantEnd
called when entity wakes from being dormant
================
*/
void idEntity::DormantEnd( void ) {
}
/*
================
idEntity::IsActive
================
*/
bool idEntity::IsActive( void ) const {
return activeNode.InList();
}
/*
================
idEntity::BecomeActive
================
*/
void idEntity::BecomeActive( int flags ) {
if ( ( flags & TH_PHYSICS ) ) {
// enable the team master if this entity is part of a physics team
if ( teamMaster && teamMaster != this ) {
teamMaster->BecomeActive( TH_PHYSICS );
} else if ( !( thinkFlags & TH_PHYSICS ) ) {
// if this is a pusher
if ( physics->IsType( idPhysics_Parametric::Type ) || physics->IsType( idPhysics_Actor::Type ) ) {
gameLocal.sortPushers = true;
}
}
}
int oldFlags = thinkFlags;
thinkFlags |= flags;
if ( thinkFlags ) {
if ( !IsActive() ) {
activeNode.AddToEnd( gameLocal.activeEntities );
} else if ( !oldFlags ) {
// we became inactive this frame, so we have to decrease the count of entities to deactivate
gameLocal.numEntitiesToDeactivate--;
}
}
}
/*
================
idEntity::BecomeInactive
================
*/
void idEntity::BecomeInactive( int flags ) {
if ( ( flags & TH_PHYSICS ) ) {
// may only disable physics on a team master if no team members are running physics or bound to a joints
if ( teamMaster == this ) {
for ( idEntity *ent = teamMaster->teamChain; ent; ent = ent->teamChain ) {
if ( ( ent->thinkFlags & TH_PHYSICS ) || ( ( ent->bindMaster == this ) && ( ent->bindJoint != INVALID_JOINT ) ) ) {
flags &= ~TH_PHYSICS;
break;
}
}
}
}
if ( thinkFlags ) {
thinkFlags &= ~flags;
if ( !thinkFlags && IsActive() ) {
gameLocal.numEntitiesToDeactivate++;
}
}
if ( ( flags & TH_PHYSICS ) ) {
// if this entity has a team master
if ( teamMaster && teamMaster != this ) {
// if the team master is at rest
if ( teamMaster->IsAtRest() ) {
teamMaster->BecomeInactive( TH_PHYSICS );
}
}
}
}