-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectile.cpp
2284 lines (1910 loc) · 65.1 KB
/
Projectile.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
// RAVEN BEGIN
// bdube: note that this file is no longer merged with Doom3 updates
//
// MERGE_DATE 09/30/2004
#include "../idlib/precompiled.h"
#pragma hdrstop
#include "Game_local.h"
#include "ai/AI_Manager.h"
#include "Projectile.h"
#include "spawner.h"
/*
===============================================================================
idProjectile
===============================================================================
*/
static const float BOUNCE_SOUND_MIN_VELOCITY = 200.0f;
static const float BOUNCE_SOUND_MAX_VELOCITY = 400.0f;
const idEventDef EV_Explode( "<explode>", NULL );
const idEventDef EV_Fizzle( "<fizzle>", NULL );
const idEventDef EV_RadiusDamage( "<radiusdmg>", "E" );
const idEventDef EV_ResidualDamage ( "<residualdmg>", "E" );
const idEventDef EV_EmitDamage ( "<emitdmg>", "E" );
CLASS_DECLARATION( idEntity, idProjectile )
EVENT( EV_Explode, idProjectile::Event_Explode )
EVENT( EV_Fizzle, idProjectile::Event_Fizzle )
EVENT( EV_Touch, idProjectile::Event_Touch )
EVENT( EV_RadiusDamage, idProjectile::Event_RadiusDamage )
EVENT( EV_ResidualDamage, idProjectile::Event_ResidualDamage )
EVENT( EV_EmitDamage, idProjectile::Event_EmitDamage )
END_CLASS
/*
================
idProjectile::idProjectile
================
*/
idProjectile::idProjectile( void ) {
methodOfDeath = -1;
owner = NULL;
memset( &projectileFlags, 0, sizeof( projectileFlags ) );
damagePower = 1.0f;
memset( &renderLight, 0, sizeof( renderLight ) );
lightDefHandle = -1;
lightOffset.Zero();
lightStartTime = 0;
lightEndTime = 0;
lightColor.Zero();
visualAngles.Zero();
angularVelocity.Zero();
speed.Init( 0.0f, 0.0f, 0.0f, 0.0f );
updateVelocity = false;
rotation.Init( 0, 0.0f, mat3_identity.ToQuat(), mat3_identity.ToQuat() );
flyEffect = NULL;
flyEffectAttenuateSpeed = 0.0f;
bounceCount = 0;
hitCount = 0;
state = SPAWNED;
fl.networkSync = true;
prePredictTime = 0;
syncPhysics = false;
launchTime = 0;
launchOrig = vec3_origin;
launchDir = vec3_origin;
launchSpeed = 0.0f;
playedDamageEffect = false;
predictedProjectiles = false;
}
/*
================
idProjectile::Spawn
================
*/
void idProjectile::Spawn( void ) {
physicsObj.SetSelf( this );
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
RV_PUSH_HEAP_MEM(this);
// RAVEN END
physicsObj.SetClipModel( new idClipModel( GetPhysics()->GetClipModel() ), 1.0f );
// RAVEN BEGIN
// mwhitlock: Dynamic memory consolidation
RV_POP_HEAP();
// RAVEN END
physicsObj.SetContents( 0 );
physicsObj.SetClipMask( 0 );
physicsObj.PutToRest();
SetPhysics( &physicsObj );
prePredictTime = spawnArgs.GetInt( "predictTime", "0" );
syncPhysics = spawnArgs.GetBool( "net_syncPhysics", "0" );
if ( gameLocal.isClient ) {
Hide();
}
predictedProjectiles = g_predictedProjectiles.GetBool();
}
/*
================
idProjectile::Save
================
*/
void idProjectile::Save( idSaveGame *savefile ) const {
savefile->WriteInt( methodOfDeath ); // cnicholson: Added unsaved var
owner.Save( savefile );
savefile->Write( &projectileFlags, sizeof( projectileFlags ) );
savefile->WriteFloat( damagePower );
savefile->WriteRenderLight( renderLight );
savefile->WriteInt( ( int )lightDefHandle );
savefile->WriteVec3( lightOffset );
savefile->WriteInt( lightStartTime );
savefile->WriteInt( lightEndTime );
savefile->WriteVec3( lightColor );
savefile->WriteStaticObject( physicsObj );
savefile->WriteAngles( visualAngles ); // cnicholson: added unsaved var
savefile->WriteAngles( angularVelocity ); // cnicholson: moved var
// Save speed
savefile->WriteBool ( updateVelocity );
savefile->WriteFloat( speed.GetStartTime() );
savefile->WriteFloat( speed.GetDuration() );
savefile->WriteFloat( speed.GetStartValue() );
savefile->WriteFloat( speed.GetEndValue() );
// rotation; this is a class, so it doesnt get saved here
flyEffect.Save( savefile ); // cnicholson: added unsaved var
savefile->WriteFloat( flyEffectAttenuateSpeed ); // cnicholson: added unsaved var
savefile->WriteInt ( bounceCount );
savefile->WriteInt ( hitCount );
savefile->WriteInt( (int)state );
}
/*
================
idProjectile::Restore
================
*/
void idProjectile::Restore( idRestoreGame *savefile ) {
float fset;
idVec3 temp;
savefile->ReadInt( methodOfDeath ); // cnicholson: Added unrestored var
owner.Restore( savefile );
savefile->Read( &projectileFlags, sizeof( projectileFlags ) );
savefile->ReadFloat( damagePower );
savefile->ReadRenderLight( renderLight );
savefile->ReadInt( (int &)lightDefHandle );
if ( lightDefHandle != -1 ) {
//get the handle again as it's out of date after a restore!
lightDefHandle = gameRenderWorld->AddLightDef( &renderLight );
}
savefile->ReadVec3( lightOffset );
savefile->ReadInt( lightStartTime );
savefile->ReadInt( lightEndTime );
savefile->ReadVec3( lightColor );
// Restore the physics
savefile->ReadStaticObject( physicsObj );
RestorePhysics ( &physicsObj );
savefile->ReadAngles( visualAngles ); // cnicholson: added unrestored var
savefile->ReadAngles( angularVelocity ); // cnicholson: moved var
// Restore speed
savefile->ReadBool ( updateVelocity );
savefile->ReadFloat( fset );
speed.SetStartTime( fset );
savefile->ReadFloat( fset );
speed.SetDuration( fset );
savefile->ReadFloat( fset );
speed.SetStartValue( fset );
savefile->ReadFloat( fset );
speed.SetEndValue( fset );
// rotation?
flyEffect.Restore( savefile ); // cnicholson: added unrestored var
savefile->ReadFloat( flyEffectAttenuateSpeed ); // cnicholson: added unsaved var
savefile->ReadInt ( bounceCount );
savefile->ReadInt ( hitCount );
savefile->ReadInt( (int &)state );
}
/*
================
idProjectile::GetOwner
================
*/
idEntity *idProjectile::GetOwner( void ) const {
return owner.GetEntity();
}
/*
================
idProjectile::SetSpeed
================
*/
void idProjectile::SetSpeed( float s, int accelTime ) {
idVec3 vel;
vel = physicsObj.GetLinearVelocity();
vel.Normalize();
speed.Init( gameLocal.time, accelTime, speed.GetCurrentValue(gameLocal.time), s );
if ( accelTime > 0 ) {
updateVelocity = true;
} else {
updateVelocity = false;
}
// Update the velocity to match the direction we are facing and include any accelerations
physicsObj.SetLinearVelocity( speed.GetCurrentValue( gameLocal.time ) * vel );
}
/*
================
idProjectile::Create
================
*/
void idProjectile::Create( idEntity* _owner, const idVec3 &start, const idVec3 &dir, idEntity* ignore, idEntity* extraPassEntity ) {
idDict args;
idStr shaderName;
idVec3 light_color;
idVec3 light_offset;
idVec3 tmp;
idMat3 axis;
Unbind();
axis = dir.ToMat3();
physicsObj.SetOrigin( start );
physicsObj.SetAxis( axis );
physicsObj.GetClipModel()->SetOwner( ignore ? ignore : _owner );
physicsObj.extraPassEntity = extraPassEntity;
owner = _owner;
memset( &renderLight, 0, sizeof( renderLight ) );
shaderName = spawnArgs.GetString( "mtr_light_shader" );
if ( *shaderName ) {
renderLight.shader = declManager->FindMaterial( shaderName, false );
renderLight.pointLight = true;
renderLight.lightRadius[0] =
renderLight.lightRadius[1] =
renderLight.lightRadius[2] = spawnArgs.GetFloat( "light_radius" );
spawnArgs.GetVector( "light_color", "1 1 1", light_color );
renderLight.shaderParms[0] = light_color[0];
renderLight.shaderParms[1] = light_color[1];
renderLight.shaderParms[2] = light_color[2];
renderLight.shaderParms[3] = 1.0f;
// RAVEN BEGIN
// dluetscher: added detail levels to render lights
renderLight.detailLevel = DEFAULT_LIGHT_DETAIL_LEVEL;
// dluetscher: set the projectile lights to be no shadows
renderLight.noShadows = cvarSystem->GetCVarInteger("com_machineSpec") < 3;
// RAVEN END
}
spawnArgs.GetVector( "light_offset", "0 0 0", lightOffset );
lightStartTime = 0;
lightEndTime = 0;
damagePower = 1.0f;
UpdateVisuals();
state = CREATED;
}
/*
=================
idProjectile::~idProjectile
=================
*/
idProjectile::~idProjectile() {
StopSound( SND_CHANNEL_ANY, false );
FreeLightDef();
SetPhysics( NULL );
}
/*
=================
idProjectile::FreeLightDef
=================
*/
void idProjectile::FreeLightDef( void ) {
if ( lightDefHandle != -1 ) {
gameRenderWorld->FreeLightDef( lightDefHandle );
lightDefHandle = -1;
}
}
/*
=================
idProjectile::Launch
=================
*/
void idProjectile::Launch( const idVec3 &start, const idVec3 &dir, const idVec3 &pushVelocity, const float timeSinceFire, const float dmgPower ) {
float fuse;
idVec3 velocity;
float linear_friction;
float angular_friction;
float contact_friction;
float bounce;
float mass;
float gravity;
float temp, temp2;
idVec3 gravVec;
idVec3 tmp;
int contents;
int clipMask;
// allow characters to throw projectiles during cinematics, but not the player
if ( owner.GetEntity() && !owner.GetEntity()->IsType( idPlayer::GetClassType() ) ) {
cinematic = owner.GetEntity()->cinematic;
} else {
cinematic = false;
}
// Set the damage
damagePower = dmgPower;
if ( !spawnArgs.GetFloat( "speed", "0", temp ) ) {
spawnArgs.GetVector( "velocity", "0 0 0", tmp );
temp = tmp[0];
} else {
float speedRandom;
if ( !spawnArgs.GetFloat( "speedRandom", "0", speedRandom ) ) {
temp += gameLocal.random.CRandomFloat()*speedRandom;
}
}
if ( !spawnArgs.GetFloat( "speed_end", "0", temp2 ) ) {
temp2 = temp;
}
float speedDuration;
speedDuration = SEC2MS( spawnArgs.GetFloat( "speed_duration", "0" ) );
speed.Init( gameLocal.time, speedDuration, temp, temp2 );
if ( speedDuration > 0 && temp != temp2 ) {
// only support constant velocity projectiles in MP
// ( we also assume that no MP projectiles use speedRandom )
assert( !gameLocal.isServer );
updateVelocity = true;
}
launchSpeed = temp;
spawnArgs.GetAngles( "angular_velocity", "0 0 0", angularVelocity );
linear_friction = spawnArgs.GetFloat( "linear_friction" );
angular_friction = spawnArgs.GetFloat( "angular_friction" );
contact_friction = spawnArgs.GetFloat( "contact_friction" );
bounce = spawnArgs.GetFloat( "bounce" );
mass = spawnArgs.GetFloat( "mass" );
gravity = spawnArgs.GetFloat( "gravity" );
fuse = spawnArgs.GetFloat( "fuse" ) + ( spawnArgs.GetFloat( "fuse_random", "0" ) * gameLocal.random.RandomFloat() );
bounceCount = spawnArgs.GetInt( "bounce_count", "-1" );
//spawn impact entity information
impactEntity = spawnArgs.GetString("def_impactEntity","");
numImpactEntities = spawnArgs.GetInt("numImpactEntities","0");
ieMinPitch = spawnArgs.GetInt("ieMinPitch","0");
ieMaxPitch = spawnArgs.GetInt("ieMaxPitch","0");
ieSlicePercentage = spawnArgs.GetFloat("ieSlicePercentage","0.0");
projectileFlags.detonate_on_world = spawnArgs.GetBool( "detonate_on_world" );
projectileFlags.detonate_on_actor = spawnArgs.GetBool( "detonate_on_actor" );
projectileFlags.randomShaderSpin = spawnArgs.GetBool( "random_shader_spin" );
projectileFlags.detonate_on_bounce = spawnArgs.GetBool( "detonate_on_bounce" );
lightStartTime = 0;
lightEndTime = 0;
impactedEntity = 0;
if ( health ) {
fl.takedamage = true;
}
// RAVEN BEGIN
// abahr:
gravVec = ( idMath::Fabs(gravity) > VECTOR_EPSILON ) ? gameLocal.GetCurrentGravity(this) * gravity : vec3_zero;
// RAVEN END
Unbind();
contents = 0;
clipMask = spawnArgs.GetBool( "clipmask_rendermodel", "1" ) ? MASK_SHOT_RENDERMODEL : MASK_SHOT_BOUNDINGBOX;
// all projectiles are projectileclip
clipMask |= CONTENTS_PROJECTILECLIP;
if ( spawnArgs.GetBool( "clipmask_largeshot", "1" ) ) {
clipMask |= CONTENTS_LARGESHOTCLIP;
}
if ( spawnArgs.GetBool( "clipmask_moveable", "0" ) ) {
clipMask |= CONTENTS_MOVEABLECLIP;
}
if ( spawnArgs.GetBool( "clipmask_monsterclip", "0" ) ) {
clipMask |= CONTENTS_MONSTERCLIP;
}
if ( spawnArgs.GetBool( "detonate_on_trigger" ) ) {
contents |= CONTENTS_TRIGGER;
}
if ( !spawnArgs.GetBool( "no_contents", "1" ) ) {
contents |= CONTENTS_PROJECTILE;
}
clipMask |= CONTENTS_PROJECTILE;
// don't do tracers on client, we don't know origin and direction
if ( spawnArgs.GetBool( "tracers" ) && gameLocal.random.RandomFloat() > 0.5f ) {
SetModel( spawnArgs.GetString( "model_tracer" ) );
projectileFlags.isTracer = true;
}
physicsObj.SetMass( mass );
physicsObj.SetFriction( linear_friction, angular_friction, contact_friction );
physicsObj.SetBouncyness( bounce, !projectileFlags.detonate_on_bounce );
physicsObj.SetGravity( gravVec );
physicsObj.SetContents( contents );
physicsObj.SetClipMask( clipMask | CONTENTS_WATER );
physicsObj.SetLinearVelocity( dir * speed.GetCurrentValue(gameLocal.time) + pushVelocity );
physicsObj.SetOrigin( start );
physicsObj.SetAxis( dir.ToMat3() );
if ( !gameLocal.isClient ) {
if ( fuse <= 0 ) {
// run physics for 1 second
RunPhysics();
PostEventMS( &EV_Remove, spawnArgs.GetInt( "remove_time", "1500" ) );
} else if ( spawnArgs.GetBool( "detonate_on_fuse" ) ) {
fuse -= timeSinceFire;
if ( fuse < 0.0f ) {
fuse = 0.0f;
}
PostEventSec( &EV_Explode, fuse );
} else {
fuse -= timeSinceFire;
if ( fuse < 0.0f ) {
fuse = 0.0f;
}
PostEventSec( &EV_Fizzle, fuse );
}
}
idQuat q( dir.ToMat3().ToQuat() );
rotation.Init( gameLocal.GetTime(), 0.0f, q, q );
if ( projectileFlags.isTracer ) {
StartSound( "snd_tracer", SND_CHANNEL_BODY, 0, false, NULL );
} else {
StartSound( "snd_fly", SND_CHANNEL_BODY, 0, false, NULL );
}
// used for the plasma bolts but may have other uses as well
if ( projectileFlags.randomShaderSpin ) {
float f = gameLocal.random.RandomFloat();
f *= 0.5f;
renderEntity.shaderParms[SHADERPARM_DIVERSITY] = f;
}
UpdateVisuals();
// Make sure these come after update visuals so the origin and axis are correct
PlayEffect( "fx_launch", renderEntity.origin, renderEntity.axis );
flyEffect = PlayEffect( "fx_fly", renderEntity.origin, renderEntity.axis, true );
flyEffectAttenuateSpeed = spawnArgs.GetFloat( "flyEffectAttenuateSpeed", "0" );
state = LAUNCHED;
hitCount = 0;
predictTime = prePredictTime;
if ( spawnArgs.GetFloat( "delay_emit_damage" ) > 0.0f ) {
PostEventSec( &EV_EmitDamage, spawnArgs.GetFloat( "wait_emit_damage", "0" ), this );
}
if ( gameLocal.isServer ) {
// store launch information for networking
launchTime = gameLocal.time;
launchOrig = physicsObj.GetOrigin();
launchDir = dir;
} else {
if ( predictedProjectiles ) {
physicsObj.Evaluate( gameLocal.time - launchTime, gameLocal.time );
}
}
if ( g_perfTest_noProjectiles.GetBool() ) {
PostEventMS( &EV_Remove, 0 );
}
}
/*
================
idProjectile::Think
================
*/
void idProjectile::Think( void ) {
// run physics
if ( thinkFlags & TH_PHYSICS ) {
// Update the velocity to match the changing speed
if ( updateVelocity ) {
idVec3 vel;
vel = physicsObj.GetLinearVelocity ( );
vel.Normalize ( );
physicsObj.SetLinearVelocity ( speed.GetCurrentValue ( gameLocal.time ) * vel );
if ( speed.IsDone ( gameLocal.time ) ) {
updateVelocity = false;
}
}
RunPhysics();
// If we werent at rest and are now then start the atrest fuse
if ( physicsObj.IsAtRest( ) ) {
float fuse = spawnArgs.GetFloat( "fuse_atrest" );
if ( fuse > 0.0f ) {
if ( spawnArgs.GetBool( "detonate_on_fuse" ) ) {
CancelEvents( &EV_Explode );
PostEventSec( &EV_Explode, fuse );
} else {
CancelEvents( &EV_Fizzle );
PostEventSec( &EV_Fizzle, fuse );
}
}
}
// Stop the trail effect if the physics flag was removed
if ( flyEffect && flyEffectAttenuateSpeed > 0.0f ) {
if ( physicsObj.IsAtRest( ) ) {
flyEffect->Stop( );
flyEffect = NULL;
} else {
float speed;
speed = idMath::ClampFloat( 0, flyEffectAttenuateSpeed, physicsObj.GetLinearVelocity ( ).LengthFast ( ) );
flyEffect->Attenuate( speed / flyEffectAttenuateSpeed );
}
}
UpdateVisualAngles();
}
Present();
// add the light
if ( renderLight.lightRadius.x > 0.0f && g_projectileLights.GetBool() ) {
renderLight.origin = GetPhysics()->GetOrigin() + GetPhysics()->GetAxis() * lightOffset;
renderLight.axis = GetPhysics()->GetAxis();
if ( ( lightDefHandle != -1 ) ) {
if ( lightEndTime > 0 && gameLocal.time <= lightEndTime + gameLocal.GetMSec() ) {
idVec3 color( 0, 0, 0 );
if ( gameLocal.time < lightEndTime ) {
float frac = ( float )( gameLocal.time - lightStartTime ) / ( float )( lightEndTime - lightStartTime );
color.Lerp( lightColor, color, frac );
}
renderLight.shaderParms[SHADERPARM_RED] = color.x;
renderLight.shaderParms[SHADERPARM_GREEN] = color.y;
renderLight.shaderParms[SHADERPARM_BLUE] = color.z;
}
gameRenderWorld->UpdateLightDef( lightDefHandle, &renderLight );
} else {
lightDefHandle = gameRenderWorld->AddLightDef( &renderLight );
}
}
}
/*
=================
idProjectile::UpdateVisualAngles
=================
*/
void idProjectile::UpdateVisualAngles() {
idVec3 linearVelocity( GetPhysics()->GetLinearVelocity() );
if( angularVelocity.Compare(ang_zero, VECTOR_EPSILON) ) {
rotation.Init( gameLocal.GetTime(), 0.0f, rotation.GetCurrentValue(gameLocal.GetTime()), linearVelocity.ToNormal().ToMat3().ToQuat() );
return;
}
if( physicsObj.GetNumContacts() ) {
return;
}
if( !rotation.IsDone(gameLocal.GetTime()) ) {
return;
}
if( linearVelocity.Length() <= BOUNCE_SOUND_MIN_VELOCITY ) {
return;
}
visualAngles += angularVelocity;
idQuat q = visualAngles.ToQuat() * linearVelocity.ToNormal().ToMat3().ToQuat();
rotation.Init( gameLocal.GetTime(), gameLocal.GetMSec(), rotation.GetCurrentValue(gameLocal.GetTime()), q );
}
/*
=================
idProjectile::Collide
=================
*/
bool idProjectile::Collide( const trace_t &collision, const idVec3 &velocity ) {
bool dummy = false;
return Collide( collision, velocity, dummy );
}
bool idProjectile::Collide( const trace_t &collision, const idVec3 &velocity, bool &hitTeleporter ) {
idEntity* ent;
idEntity* actualHitEnt = NULL;
idEntity* ignore;
const char* damageDefName;
idVec3 dir;
bool canDamage;
hitTeleporter = false;
if ( state == EXPLODED || state == FIZZLED || ( state == IMPACTED && predictedProjectiles ) ) {
return true;
}
if ( IsHidden() && predictedProjectiles ) {
// teleported, ignore impacts
return false;
}
// allow projectiles to hit triggers (teleports)
// predict this on a client
if ( collision.c.contents & CONTENTS_TRIGGER ) {
idEntity* trigger = gameLocal.entities[ collision.c.entityNum ];
if ( trigger ) {
if ( trigger->RespondsTo( EV_Touch ) || trigger->HasSignal( SIG_TOUCH ) ) {
hitTeleporter = true;
trace_t trace;
trace.endpos = physicsObj.GetOrigin();
trace.endAxis = physicsObj.GetAxis();
trace.c.contents = collision.c.contents;
trace.c.entityNum = collision.c.entityNum;
if( trigger->GetPhysics()->GetClipModel() ) {
trace.c.id = trigger->GetPhysics()->GetClipModel()->GetId();
} else {
trace.c.id = 0;
}
// hack to play the effect on clients when mispredicted (Hide() ensures it can't play twice for the same teleport)
int wasNewFrame = gameLocal.isNewFrame;
if ( gameLocal.isClient && predictedProjectiles ) {
gameLocal.isNewFrame = true;
}
trigger->Signal( SIG_TOUCH );
trigger->ProcessEvent( &EV_Touch, this, &trace );
if ( gameLocal.isClient && predictedProjectiles ) {
Hide();
gameLocal.isNewFrame = wasNewFrame;
}
}
}
// when we hit a trigger, align our velocity to the trigger's coordinate plane
if( gameLocal.isServer ) {
idVec3 up( 0.0f, 0.0f, 1.0f );
idVec3 right = collision.c.normal.Cross( up );
idMat3 mat( collision.c.normal, right, up );
physicsObj.SetLinearVelocity( -1.0f * (physicsObj.GetLinearVelocity() * mat.Transpose()) );
physicsObj.SetLinearVelocity( idVec3( physicsObj.GetLinearVelocity()[ 0 ], -1.0 *physicsObj.GetLinearVelocity()[ 1 ], -1.0 * physicsObj.GetLinearVelocity()[ 2 ] ) );
// update the projectile's launchdir and launch origin
// this will propagate the change to the clients for prediction
// re-launch the projectile
idVec3 newDir = physicsObj.GetLinearVelocity();
newDir.Normalize();
launchTime = gameLocal.time;
launchDir = newDir;
physicsObj.SetOrigin( physicsObj.GetOrigin() + idVec3( 0.0f, 0.0f, 32.0f ) );
physicsObj.SetAxis( newDir.ToMat3() );
launchOrig = physicsObj.GetOrigin();
}
if( !(collision.c.contents & CONTENTS_SOLID) || hitTeleporter ) {
return false;
}
}
// network clients: heuristic to skip predicting collisions
if ( gameLocal.isClient ) {
if ( predictedProjectiles ) {
if ( syncPhysics ) {
return false;
}
} else {
if ( spawnArgs.GetBool( "no_impact_prediction" ) || syncPhysics ) {
return false;
}
}
}
// remove projectile when a 'noimpact' surface is hit
if ( ( collision.c.material != NULL ) && ( collision.c.material->GetSurfaceFlags() & SURF_NOIMPACT ) ) {
PostEventMS( &EV_Remove, 0 );
StopEffect( "fx_fly" );
if( flyEffect) {
//flyEffect->Event_Remove();
}
return true;
}
// get the entity the projectile collided with
ent = gameLocal.entities[ collision.c.entityNum ];
if ( ent == owner.GetEntity() ) {
return true;
}
// just get rid of the projectile when it hits a player in noclip
if ( ent->IsType( idPlayer::GetClassType() ) && static_cast<idPlayer *>( ent )->noclip ) {
PostEventMS( &EV_Remove, 0 );
common->DPrintf( "Projectile collision no impact\n" );
return true;
}
// If the hit entity is bound to an actor use the actor instead
if ( ent->GetTeamMaster() && ent->GetTeamMaster()->IsType ( idActor::GetClassType() ) ) {
actualHitEnt = ent;
ent = ent->GetTeamMaster();
}
// Can the projectile damage?
canDamage = ent->fl.takedamage && !(( collision.c.material != NULL ) && ( collision.c.material->GetSurfaceFlags() & SURF_NODAMAGE ));
// direction of projectile
dir = velocity;
dir.Normalize();
// projectiles can apply an additional impulse next to the rigid body physics impulse
// RAVEN BEGIN
// abahr: added call to SkipDamageImpulse changed where push comes from
damageDefName = NULL;
if ( collision.c.materialType ) {
damageDefName = spawnArgs.GetString( va("def_damage_%s", collision.c.materialType->GetName()) );
}
if ( !damageDefName || !*damageDefName ) {
damageDefName = spawnArgs.GetString ( "def_damage" );
}
if( damageDefName && damageDefName[0] ) {
const idDict* dict = gameLocal.FindEntityDefDict( damageDefName, false );
if ( dict ) {
ent->ApplyImpulse( this, collision.c.id, collision.endpos, dir, dict );
}
}
// RAVEN END
//Spawn any impact entities if necessary.
SpawnImpactEntities(collision, velocity);
//Apply any impact force if the necessary
//ApplyImpactForce(ent, collision, dir);
// MP: projectiles open doors
if ( gameLocal.isMultiplayer && ent->IsType( idDoor::GetClassType() ) && !static_cast< idDoor * >(ent)->IsOpen() && !ent->spawnArgs.GetBool( "no_touch" ) ) {
ent->ProcessEvent( &EV_Activate , this );
}
// If the projectile hits water then we need to let the projectile keep going
if ( ent->GetPhysics()->GetContents() & CONTENTS_WATER ) {
if ( !physicsObj.IsInWater( ) ) {
StopEffect( "fx_fly" );
if( flyEffect) {
//flyEffect->Event_Remove();
}
}
// Pass through water
return false;
} else if ( canDamage && ent->IsType( idActor::GetClassType() ) ) {
if ( !projectileFlags.detonate_on_actor ) {
return false;
}
} else {
bool bounce = false;
// Determine if the projectile should bounce
bounce = !physicsObj.IsInWater() && !projectileFlags.detonate_on_world && !canDamage;
bounce = bounce && (bounceCount == -1 || bounceCount > 0);
//assert(collision.c.material);
if ( !bounce && collision.c.material && (collision.c.material->GetSurfaceFlags() & SURF_BOUNCE) ) {
bounce = !projectileFlags.detonate_on_bounce;
}
if ( bounce ) {
if ( bounceCount != -1 ) {
bounceCount--;
}
StartSound( "snd_ricochet", SND_CHANNEL_ITEM, 0, true, NULL );
float len = velocity.Length();
if ( len > BOUNCE_SOUND_MIN_VELOCITY ) {
if ( ent->IsType ( idMover::GetClassType ( ) ) ) {
ent->PlayEffect(
gameLocal.GetEffect(spawnArgs,"fx_bounce",collision.c.materialType),
collision.c.point, collision.c.normal.ToMat3(),
false, vec3_origin, true );
} else {
gameLocal.PlayEffect(
gameLocal.GetEffect(spawnArgs,"fx_bounce",collision.c.materialType),
collision.c.point, collision.c.normal.ToMat3(),
false, vec3_origin, true );
}
} else {
// FIXME: clean up
idMat3 axis( rotation.GetCurrentValue(gameLocal.GetTime()).ToMat3() );
axis[0].ProjectOntoPlane( collision.c.normal );
axis[0].Normalize();
axis[2] = collision.c.normal;
axis[1] = axis[2].Cross( axis[0] ).ToNormal();
rotation.Init( gameLocal.GetTime(), SEC2MS(spawnArgs.GetFloat("settle_duration")), rotation.GetCurrentValue(gameLocal.GetTime()), axis.ToQuat() );
}
if ( actualHitEnt
&& actualHitEnt != ent
&& actualHitEnt->spawnArgs.GetBool( "takeBounceDamage" ) )
{//bleh...
if ( damageDefName[0] != '\0' ) {
idVec3 dir = velocity;
dir.Normalize();
actualHitEnt->Damage( this, owner, dir, damageDefName, damagePower, CLIPMODEL_ID_TO_JOINT_HANDLE( collision.c.id ) );
}
}
return false;
}
}
SetOrigin( collision.endpos );
// SetAxis( collision.endAxis );
// unlink the clip model because we no longer need it
GetPhysics()->UnlinkClip();
ignore = NULL;
// RAVEN BEGIN
// jshepard: Single Player- if the the player is the attacker and the victim is teammate, don't play any blood effects.
bool willPlayDamageEffect = true;
if ( owner.GetEntity() && owner.GetEntity()->IsType( idPlayer::GetClassType() ) ) {
// if the projectile hit an ai
if ( ent->IsType( idAI::GetClassType() ) ) {
idPlayer *player = static_cast<idPlayer *>( owner.GetEntity() );
player->AddProjectileHits( 1 );
// jshepard: Single Player- if the the player is the attacker and the victim is teammate, don't play any blood effects.
idAI * ai_ent = static_cast<idAI* >(ent);
if( ai_ent->team == player->team) {
willPlayDamageEffect = false;
}
}
}
// RAVEN END
// if the hit entity takes damage
if ( canDamage ) {
if ( damageDefName[0] != '\0' ) {
idVec3 dir = velocity;
dir.Normalize();
// RAVEN BEGIN
// jdischler: code from the 'other' project..to ensure that if an attached head is hit, the body will use the head joint
// otherwise damage zones for head attachments no-worky
int hitJoint = CLIPMODEL_ID_TO_JOINT_HANDLE(collision.c.id);
if ( ent->IsType(idActor::GetClassType()) )
{
idActor* entActor = static_cast<idActor*>(ent);
if ( entActor && entActor->GetHead() && entActor->GetHead()->IsType(idAFAttachment::GetClassType()) )
{
idAFAttachment* headEnt = static_cast<idAFAttachment*>(entActor->GetHead());
if ( headEnt && headEnt->entityNumber == collision.c.entityNum )
{//hit ent's head, get the proper joint for the head
hitJoint = entActor->GetAnimator()->GetJointHandle("head");
}
}
}
// RAVEN END
ent->Damage( this, owner, dir, damageDefName, damagePower, hitJoint );
if( owner && owner->IsType( idPlayer::GetClassType() ) && ent->IsType( idActor::GetClassType() ) ) {
statManager->WeaponHit( (const idActor*)(owner.GetEntity()), ent, methodOfDeath, hitCount == 0 );
hitCount++;
}
}
}
ignore = ent;
if ( predictedProjectiles ) {
if ( ( gameLocal.isClient || gameLocal.isListenServer ) && !playedDamageEffect ) {
ent->AddDamageEffect( collision, velocity, damageDefName, owner );
}
// hack to play the effect on clients when mispredicted (state/playedDamageEffect ensure it can't happen twice)
int wasNewFrame = gameLocal.isNewFrame;
if ( gameLocal.isClient ) {
gameLocal.isNewFrame = true;
}
// if the the player is the attacker and the victim is teammate, don't play any effects.
if ( ( gameLocal.isClient || gameLocal.isListenServer ) && !playedDamageEffect && ( willPlayDamageEffect || spawnArgs.GetBool( "friendly_impact") ) ) {
DefaultDamageEffect( collision, velocity, damageDefName );
playedDamageEffect = true;
}
Explode( &collision, false, ignore );
gameLocal.isNewFrame = wasNewFrame;
} else {
ent->AddDamageEffect ( collision, velocity, damageDefName, owner );
// if the the player is the attacker and the victim is teammate, don't play any effects.
if ( willPlayDamageEffect || spawnArgs.GetBool( "friendly_impact") ) {
DefaultDamageEffect( collision, velocity, damageDefName );
}
// don't predict explosions on clients
if ( gameLocal.isClient ) {
return true;
}
Explode( &collision, false, ignore );
}
return true;
}
void idProjectile::SpawnImpactEntities(const trace_t& collision, const idVec3 velocity)
{
if( impactEntity.Length() == 0 || numImpactEntities == 0 )
return;
const idDict* impactEntityDict = gameLocal.FindEntityDefDict(impactEntity);
if(impactEntityDict == NULL)
return;
idVec3 tempDirection;
idVec3 direction;
direction.Zero();
idVec3 up = collision.c.normal;
//Calculate the axes for that are oriented to the impact point.
idMat3 impactAxes;
idVec3 right = velocity.Cross(up);
idVec3 forward = up.Cross(right);
right.Normalize();