-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathasw_util_shared.cpp
More file actions
3715 lines (3228 loc) · 104 KB
/
asw_util_shared.cpp
File metadata and controls
3715 lines (3228 loc) · 104 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cbase.h"
#include "asw_shareddefs.h"
#include "gamestringpool.h"
#ifdef GAME_DLL
#include "asw_player.h"
#include "asw_marine.h"
#include "asw_marine_resource.h"
#include "asw_gamerules.h"
#include "asw_game_resource.h"
#include "props.h"
#include "vphysics_interface.h"
#include "physics.h"
#include "vphysics/friction.h"
#include "asw_computer_area.h"
#include "point_camera.h"
#include "asw_remote_turret_shared.h"
#include "asw_computer_area.h"
#include "asw_button_area.h"
#include "fogcontroller.h"
#include "asw_point_camera.h"
#include "asw_deathmatch_mode.h"
#include "asw_sentry_base.h"
#include "asw_sentry_top.h"
#else
#include "asw_gamerules.h"
#include "c_asw_drone_advanced.h"
#include "c_asw_fx.h"
#include "c_asw_marine.h"
#include "c_asw_game_resource.h"
#include "c_asw_marine_resource.h"
#include "c_asw_computer_area.h"
#include "c_point_camera.h"
#include "asw_remote_turret_shared.h"
#include "c_asw_player.h"
#include "c_playerresource.h"
#include "c_asw_computer_area.h"
#include "c_asw_button_area.h"
#include "vgui/cursor.h"
#include "iinput.h"
#include <vgui/ISurface.h>
#include "vguimatsurface/imatsystemsurface.h"
#include "vgui_controls\Controls.h"
#include <vgui/IVGUI.h>
#include "ivieweffects.h"
#include "asw_input.h"
#include "c_asw_point_camera.h"
#include "baseparticleentity.h"
#include "asw_hud_floating_number.h"
#include "takedamageinfo.h"
#include "clientmode_asw.h"
#include "engine/IVDebugOverlay.h"
#include "c_user_message_register.h"
#include "prediction.h"
#include "asw_medal_store.h"
#define CASW_Marine C_ASW_Marine
#define CASW_Game_Resource C_ASW_Game_Resource
#define CASW_Marine_Resource C_ASW_Marine_Resource
#define CASW_Computer_Area C_ASW_Computer_Area
#define CPointCamera C_PointCamera
#define CASW_PointCamera C_ASW_PointCamera
#define CASW_Remote_Turret C_ASW_Remote_Turret
#define CASW_Button_Area C_ASW_Button_Area
#define CASW_Computer_Area C_ASW_Computer_Area
#endif
#include "shake.h"
#include "asw_util_shared.h"
#include "tier2/fileutils.h"
#include "vpklib/packedstore.h"
#include "vgui/ILocalize.h"
#include "iregistry.h"
#include <ctime>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar rd_override_commander_promotion( "rd_override_commander_promotion", "-1", FCVAR_REPLICATED );
ConVar rd_override_commander_level( "rd_override_commander_level", "-1", FCVAR_REPLICATED );
#ifndef CLIENT_DLL
ConVar asw_debug_marine_can_see("asw_debug_marine_can_see", "0", FCVAR_CHEAT, "Display lines for waking up aliens");
ConVar rd_hit_confirms_to_recorders( "rd_hit_confirms_to_recorders", "1", FCVAR_NONE, "Send hit confirm user messages to players who are recording, not just players who are spectating the relevant character." );
#else
ConVar rd_load_all_localization_files( "rd_load_all_localization_files", "1", FCVAR_DEVELOPMENTONLY, "Load reactivedrop_english.txt, etc. from all addons rather than just the last one." );
extern int g_asw_iGUIWindowsOpen;
#endif
ConVar asw_marine_view_cone_dist("asw_marine_view_cone_dist", "700", FCVAR_REPLICATED, "Distance for marine view cone checks");
ConVar asw_marine_view_cone_dot("asw_marine_view_cone_dot", "0.5", FCVAR_REPLICATED, "Dot for marine view cone checks");
extern ConVar asw_rts_controls;
extern ConVar rd_legacy_ui;
ConVar asw_shake_test_punch_dirx("asw_shake_test_punch_dirx","0", FCVAR_REPLICATED|FCVAR_HIDDEN );
ConVar asw_shake_test_punch_diry("asw_shake_test_punch_diry","0", FCVAR_REPLICATED|FCVAR_HIDDEN );
ConVar asw_shake_test_punch_dirz("asw_shake_test_punch_dirz","1", FCVAR_REPLICATED|FCVAR_HIDDEN );
ConVar asw_shake_test_punch_freq("asw_shake_test_punch_freq","1.5", FCVAR_REPLICATED|FCVAR_HIDDEN );
ConVar asw_shake_test_punch_amp("asw_shake_test_punch_amp","60", FCVAR_REPLICATED|FCVAR_HIDDEN );
ConVar asw_shake_test_punch_dura("asw_shake_test_punch_dura","0.75", FCVAR_REPLICATED|FCVAR_HIDDEN );
// rotates one angle towards another, with a fixed turning rate over the time
float ASW_ClampYaw( float yawSpeedPerSec, float current, float target, float time )
{
if (current != target)
{
float speed = yawSpeedPerSec * time;
float move = target - current;
if (target > current)
{
if (move >= 180)
move = move - 360;
}
else
{
if (move <= -180)
move = move + 360;
}
if (move > 0)
{// turning to the npc's left
if (move > speed)
move = speed;
}
else
{// turning to the npc's right
if (move < -speed)
move = -speed;
}
return anglemod(current + move);
}
return target;
}
float ASW_Linear_Approach( float current, float target, float delta)
{
if (current < target)
current = MIN(current + delta, target);
else if (current > target)
current = MAX(current - delta, target);
return current;
}
// time independent movement of one angle to a fraction of the desired
float ASW_ClampYaw_Fraction( float fraction, float current, float target, float time )
{
if (current != target)
{
float move = target - current;
if (target > current)
{
if (move >= 180)
move = move - 360;
}
else
{
if (move <= -180)
move = move + 360;
}
move = move * pow(fraction, time);
float r = anglemod(target - move);
return r;
}
return target;
}
bool ASW_LineCircleIntersection(
const Vector2D ¢er,
const float radius,
const Vector2D &vLinePt,
const Vector2D &vLineDir,
float *fIntersection1,
float *fIntersection2)
{
// Line = P + Vt
// Sphere = r (assume we've translated to origin)
// (P + Vt)^2 = r^2
// VVt^2 + 2PVt + (PP - r^2)
// Solve as quadratic: (-b +/- sqrt(b^2 - 4ac)) / 2a
// If (b^2 - 4ac) is < 0 there is no solution.
// If (b^2 - 4ac) is = 0 there is one solution (a case this function doesn't support).
// If (b^2 - 4ac) is > 0 there are two solutions.
Vector2D P;
float a, b, c, sqr, insideSqr;
// Translate circle to origin.
P[0] = vLinePt[0] - center[0];
P[1] = vLinePt[1] - center[1];
a = vLineDir.Dot(vLineDir);
b = 2.0f * P.Dot(vLineDir);
c = P.Dot(P) - (radius * radius);
insideSqr = b*b - 4*a*c;
if(insideSqr <= 0.000001f)
return false;
// Ok, two solutions.
sqr = (float)FastSqrt(insideSqr);
float denom = 1.0 / (2.0f * a);
*fIntersection1 = (-b - sqr) * denom;
*fIntersection2 = (-b + sqr) * denom;
return true;
}
#ifdef GAME_DLL
// a local helper to normalize some code below -- gets inlined
static void ASW_WriteScreenShakeToMessage( CASW_Inhabitable_NPC *pNPC, ShakeCommand_t eCommand, float amplitude, float frequency, float duration, const Vector &direction )
{
CASW_ViewNPCRecipientFilter user( pNPC );
user.MakeReliable();
if ( direction.IsZeroFast() ) // nondirectional shake
{
UserMessageBegin( user, "Shake" );
WRITE_BYTE( eCommand ); // shake command (SHAKE_START, STOP, FREQUENCY, AMPLITUDE)
WRITE_FLOAT( amplitude ); // shake magnitude/amplitude
WRITE_FLOAT( frequency ); // shake noise frequency
WRITE_FLOAT( duration ); // shake lasts this long
WRITE_ENTITY( pNPC->entindex() );
MessageEnd();
}
else // directional shake
{
UserMessageBegin( user, "ShakeDir" );
WRITE_BYTE( eCommand ); // shake command (SHAKE_START, STOP, FREQUENCY, AMPLITUDE)
WRITE_FLOAT( amplitude ); // shake magnitude/amplitude
WRITE_FLOAT( frequency ); // shake noise frequency
WRITE_FLOAT( duration ); // shake lasts this long
WRITE_VEC3NORMAL( direction );
WRITE_ENTITY( pNPC->entindex() );
MessageEnd();
}
}
#endif
//-----------------------------------------------------------------------------
// Transmits the actual shake event
//-----------------------------------------------------------------------------
void ASW_TransmitShakeEvent( CASW_Inhabitable_NPC *pNPC, float localAmplitude, float frequency, float duration, ShakeCommand_t eCommand, const Vector &direction )
{
if (( localAmplitude > 0 ) || ( eCommand == SHAKE_STOP ))
{
if ( eCommand == SHAKE_STOP )
localAmplitude = 0;
#ifdef GAME_DLL
ASW_WriteScreenShakeToMessage( pNPC, eCommand, localAmplitude, frequency, duration, direction );
#else
ScreenShake_t shake;
shake.command = eCommand;
shake.amplitude = localAmplitude;
shake.frequency = frequency;
shake.duration = duration;
shake.direction = direction;
ASW_TransmitShakeEvent( pNPC, shake);
#endif
}
}
void ASW_TransmitShakeEvent( CASW_Inhabitable_NPC *pNPC, const ScreenShake_t &shake )
{
if ( shake.command == SHAKE_STOP && shake.amplitude != 0 )
{
// create a corrected screenshake and recursively call myself
AssertMsg1( false, "A ScreenShake_t had a SHAKE_STOP command but a nonzero amplitude %.1f; this is meaningless.\n", shake.amplitude);
ScreenShake_t localShake = shake;
localShake.amplitude = 0;
ASW_TransmitShakeEvent( pNPC, localShake );
}
#ifdef GAME_DLL
ASW_WriteScreenShakeToMessage( pNPC, shake.command, shake.amplitude, shake.frequency, shake.duration, shake.direction );
#else
if ( !( prediction && prediction->InPrediction() && !prediction->IsFirstTimePredicted() ) )
{
GetViewEffects()->Shake( shake );
}
#endif
}
#ifdef GAME_DLL
//-----------------------------------------------------------------------------
// Compute shake amplitude
//-----------------------------------------------------------------------------
inline float ASW_ComputeShakeAmplitude( const Vector ¢er, const Vector &shakePt, float amplitude, float radius )
{
if ( radius <= 0 )
return amplitude;
float localAmplitude = -1;
Vector delta = center - shakePt;
float distance = delta.Length();
if ( distance <= radius )
{
// Make the amplitude fall off over distance
float flPerc = 1.0 - (distance / radius);
localAmplitude = amplitude * flPerc;
}
return localAmplitude;
}
//-----------------------------------------------------------------------------
// Purpose: Shake the screen of all clients within radius.
// radius == 0, shake all clients
// UNDONE: Fix falloff model (disabled)?
// UNDONE: Affect user controls?
// Input : center - Center of screen shake, radius is measured from here.
// amplitude - Amplitude of shake
// frequency -
// duration - duration of shake in seconds.
// radius - Radius of effect, 0 shakes all clients.
// command - One of the following values:
// SHAKE_START - starts the screen shake for all players within the radius
// SHAKE_STOP - stops the screen shake for all players within the radius
// SHAKE_AMPLITUDE - modifies the amplitude of the screen shake
// for all players within the radius
// SHAKE_FREQUENCY - modifies the frequency of the screen shake
// for all players within the radius
// bAirShake - completely ignored
//-----------------------------------------------------------------------------
const float ASW_MAX_SHAKE_AMPLITUDE = 16.0f;
void UTIL_ASW_ScreenShake( const Vector ¢er, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, bool bAirShake, CASW_Marine *pOnlyMarine )
{
int i;
float localAmplitude;
if ( amplitude > ASW_MAX_SHAKE_AMPLITUDE )
{
amplitude = ASW_MAX_SHAKE_AMPLITUDE;
}
CASW_Game_Resource *pGameResource = ASWGameResource();
if ( !pGameResource )
{
return;
}
for ( i = 0; i < pGameResource->GetMaxMarineResources(); i++ )
{
CASW_Marine_Resource *pMR = pGameResource->GetMarineResource( i );
if ( !pMR )
{
continue;
}
CASW_Marine *pMarine = pMR->GetMarineEntity();
if ( !pMarine )
{
continue;
}
// Only start shakes for players that are on the ground unless doing an air shake.
if ( !bAirShake && !pMarine->m_bOnGround )
continue;
if ( pOnlyMarine && pMarine != pOnlyMarine )
continue;
Vector vecMarinePos = pMarine->WorldSpaceCenter();
if ( pMarine->IsControllingTurret() && pMarine->GetRemoteTurret() )
vecMarinePos = pMarine->GetRemoteTurret()->GetAbsOrigin();
localAmplitude = ASW_ComputeShakeAmplitude( center, vecMarinePos, amplitude, radius );
// This happens if the player is outside the radius, in which case we should ignore
// all commands
if (localAmplitude < 0)
continue;
ASW_TransmitShakeEvent( pMarine, localAmplitude, frequency, duration, eCommand );
}
}
//-----------------------------------------------------------------------------
// Purpose: Perform a directional "punch" on the screen of all clients within radius.
// radius == 0, shake all clients
// Input : center - Center of screen shake, radius is measured from here.
// direction - (world space) direction in which to punch camera. punching down makes it look like the world is moving up. must be normal.
// amplitude - Amplitude of shake, in world units for the camera
// frequency - controls number of bounces before shake settles; a frequency of 1 means three peaks (forward, back, little forward, settle)
// duration - duration of shake in seconds.
// radius - Radius of effect, 0 shakes all clients.
//-----------------------------------------------------------------------------
void UTIL_ASW_ScreenPunch( const Vector ¢er, const Vector &direction, float amplitude, float frequency, float duration, float radius )
{
ScreenShake_t shake;
shake.command = SHAKE_START;
shake.direction = direction;
shake.amplitude = amplitude;
shake.frequency = frequency;
shake.duration = duration;
UTIL_ASW_ScreenPunch( center, radius, shake );
}
void UTIL_ASW_ScreenPunch( const Vector ¢er, float radius, const ScreenShake_t &shake )
{
int i;
const float radiusSqr = radius * radius;
AssertMsg( CloseEnough(shake.direction.LengthSqr(), 1), "Direction param to ASW_ScreenPunch is abnormal\n" );
CASW_Game_Resource *pGameResource = ASWGameResource();
if ( !pGameResource )
{
return;
}
for ( i = 0; i < pGameResource->GetMaxMarineResources(); i++ )
{
CASW_Marine_Resource *pMR = pGameResource->GetMarineResource( i );
if ( !pMR )
{
continue;
}
CASW_Marine *pMarine = pMR->GetMarineEntity();
if ( !pMarine )
{
continue;
}
Vector vecMarinePos = pMarine->WorldSpaceCenter();
if ( pMarine->IsControllingTurret() && pMarine->GetRemoteTurret() )
vecMarinePos = pMarine->GetRemoteTurret()->GetAbsOrigin();
if ( vecMarinePos.DistToSqr(center) > radiusSqr )
continue;
ASW_TransmitShakeEvent( pMarine, shake );
}
}
// returns the nearest marine to this point
CASW_Marine* UTIL_ASW_NearestMarine( const Vector &pos, float &marine_distance, ASW_Marine_Class marineClass, bool bAIOnly )
{
// check through all marines, finding the closest that we're aware of
CASW_Game_Resource* pGameResource = ASWGameResource();
float distance = 0.0f;
marine_distance = -1.0f;
CASW_Marine *pNearest = NULL;
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMR = pGameResource->GetMarineResource(i);
if (pMR!=NULL && pMR->GetMarineEntity()!=NULL && pMR->GetMarineEntity()->GetHealth() > 0)
{
if ( bAIOnly && pMR->IsInhabited() )
continue;
if ( marineClass != MARINE_CLASS_UNDEFINED && pMR->GetProfile() && pMR->GetProfile()->GetMarineClass() != marineClass )
continue;
distance = pMR->GetMarineEntity()->GetAbsOrigin().DistTo(pos);
if (marine_distance == -1.0f || distance < marine_distance)
{
marine_distance = distance;
pNearest = pMR->GetMarineEntity();
}
}
}
return pNearest;
}
CASW_Marine* UTIL_ASW_NearestMarine( const CASW_Marine *pMarine, float &marine_distance )
{
// check through all marines, finding the closest that we're aware of
CASW_Game_Resource* pGameResource = ASWGameResource();
float distance = 0;
marine_distance = -1.0f;
CASW_Marine *pNearest = NULL;
for ( int i = 0; i < pGameResource->GetMaxMarineResources(); i++ )
{
CASW_Marine_Resource* pMR = pGameResource->GetMarineResource(i);
if ( pMR != NULL && pMR->GetMarineEntity() != NULL && pMR->GetMarineEntity() != pMarine && pMR->GetMarineEntity()->GetHealth() > 0 )
{
distance = pMR->GetMarineEntity()->GetAbsOrigin().DistTo( pMarine->GetAbsOrigin() );
if ( marine_distance == -1.0f || distance < marine_distance )
{
marine_distance = distance;
pNearest = pMR->GetMarineEntity();
}
}
}
return pNearest;
}
int UTIL_ASW_NumCommandedMarines( const CASW_Player *pPlayer )
{
int nNumMarines = 0;
// check through all marines
CASW_Game_Resource* pGameResource = ASWGameResource();
for ( int i = 0; i < pGameResource->GetMaxMarineResources(); i++ )
{
CASW_Marine_Resource* pMR = pGameResource->GetMarineResource(i);
if ( pMR && pMR->GetMarineEntity() && pMR->GetMarineEntity()->GetHealth() > 0 )
{
if ( pMR->GetMarineEntity()->GetCommander() == pPlayer )
{
nNumMarines++;
}
}
}
return nNumMarines;
}
#else
// make a specific clientside entity gib
bool UTIL_ASW_ClientsideGib(C_BaseAnimating* pEnt)
{
if (!pEnt)
return false;
C_BaseAnimating* pAnimating = dynamic_cast<C_BaseAnimating*>(pEnt);
if (!pAnimating)
return false;
if (!stricmp(STRING(pAnimating->GetModelName()), SWARM_DRONE_MODEL))
{
Vector vMins, vMaxs, vGibOrigin, vGibVelocity(0,0,1);
if ( pEnt->m_pRagdoll )
{
pEnt->m_pRagdoll->GetRagdollBounds( vMins, vMaxs );
vGibOrigin =pEnt->m_pRagdoll->GetRagdollOrigin() + ( ( vMins + vMaxs ) / 2.0f );
pEnt->m_pRagdoll->GetElement(0)->GetVelocity( &vGibVelocity, NULL );
}
else
{
vGibOrigin = pEnt->WorldSpaceCenter();
}
FX_DroneGib( vGibOrigin, Vector(0,0,1), 0.5f, pAnimating->GetSkin(), pEnt->IsOnFire() );
return true;
}
else if (!stricmp(STRING(pAnimating->GetModelName()), SWARM_HARVESTER_MODEL))
{
FX_HarvesterGib( pEnt->WorldSpaceCenter(), Vector(0,0,1), 0.5f, pAnimating->GetSkin(), pEnt->IsOnFire() );
return true;
}
else if (!stricmp(STRING(pAnimating->GetModelName()), SWARM_SHIELDBUG_MODEL))
{
FX_HarvesterGib( pEnt->WorldSpaceCenter(), Vector(0,0,1), 0.5f, 1, pEnt->IsOnFire() );
return true;
}
// todo: code to gib other types of things clientside?
return false;
}
#endif
//void UTIL_ASW_ValidateSoundName( string_t &name, const char *defaultStr )
void UTIL_ASW_ValidateSoundName( char *szString, int stringlength, const char *defaultStr )
{
Assert(szString);
if (szString[0] == '\0')
{
Q_snprintf(szString, stringlength, "%s", defaultStr);
}
}
#ifdef GAME_DLL
void UTIL_ASW_PoisonBlur( CASW_Marine *pMarine, float duration )
{
if ( !pMarine )
return;
CASW_ViewNPCRecipientFilter user( pMarine );
user.MakeReliable();
UserMessageBegin( user, "ASWBlur" );
WRITE_ENTITY( pMarine->entindex() );
WRITE_SHORT( (int) (duration * 10.0f) ); // blur lasts this long / 10
MessageEnd();
}
// tests if a particular entity is blocking any marines (used by phys props to see if they should leave pushaway mode)
bool UTIL_ASW_BlockingMarine( CBaseEntity *pEntity )
{
CASW_Game_Resource* pGameResource = ASWGameResource();
if (!pGameResource)
return false;
int iCurrentGroup = pEntity->GetCollisionGroup();
pEntity->SetCollisionGroup(COLLISION_GROUP_NONE);
bool bBlockedMarine = false;
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMR = pGameResource->GetMarineResource(i);
if (pMR!=NULL && pMR->GetMarineEntity()!=NULL && pMR->GetMarineEntity()->GetHealth() > 0)
{
CASW_Marine *pMarine = pMR->GetMarineEntity();
// check if this marine's bounding box trace collides with the specified entity
Ray_t ray;
trace_t tr;
ray.Init( pMarine->GetAbsOrigin(), pMarine->GetAbsOrigin() - Vector(0,0,1),
pMarine->CollisionProp()->OBBMins(), pMarine->CollisionProp()->OBBMaxs() );
if (pEntity->TestCollision( ray, MASK_PLAYERSOLID, tr ))
{
bBlockedMarine = true;
break;
}
}
}
pEntity->SetCollisionGroup(iCurrentGroup);
return bBlockedMarine;
}
CASW_Marine* UTIL_ASW_Marine_Can_Chatter_Spot(CBaseEntity *pEntity, float fDist)
{
CASW_Game_Resource *pGameResource = ASWGameResource();
if (!pGameResource)
return NULL;
// find how many marines can see us
int iFound = 0;
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMarineResource = pGameResource->GetMarineResource(i);
if (!pMarineResource)
continue;
CASW_Marine *pMarine = pMarineResource->GetMarineEntity();
if (!pMarine)
continue;
if (pMarine->GetAbsOrigin().DistTo(pEntity->GetAbsOrigin()) < fDist)
{
Vector vecFacing;
AngleVectors(pMarine->GetAbsAngles(), &vecFacing);
Vector vecDir = pEntity->GetAbsOrigin() - pMarine->GetAbsOrigin();
vecDir.NormalizeInPlace();
if (vecFacing.Dot(vecDir) > 0.5f)
iFound++;
}
}
if (iFound <= 0)
return NULL;
// randomly pick one
int iChosen = random->RandomInt(0, iFound-1);
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMarineResource = pGameResource->GetMarineResource(i);
if (!pMarineResource)
continue;
CASW_Marine *pMarine = pMarineResource->GetMarineEntity();
if (!pMarine)
continue;
if (pMarine->GetAbsOrigin().DistTo(pEntity->GetAbsOrigin()) < 600.0f)
{
Vector vecFacing;
AngleVectors(pMarine->GetAbsAngles(), &vecFacing);
Vector vecDir = pEntity->GetAbsOrigin() - pMarine->GetAbsOrigin();
vecDir.NormalizeInPlace();
if (vecFacing.Dot(vecDir) > 0.5f)
{
if (iChosen <= 0)
return pMarine;
iChosen--;
}
}
}
return NULL;
}
CASW_ViewNPCRecipientFilter::CASW_ViewNPCRecipientFilter()
{
}
CASW_ViewNPCRecipientFilter::CASW_ViewNPCRecipientFilter( CASW_Inhabitable_NPC *pNPC, bool bSendToRecorders )
{
AddRecipientsByViewNPC( pNPC, bSendToRecorders );
}
void CASW_ViewNPCRecipientFilter::AddRecipientsByViewNPC( CASW_Inhabitable_NPC *pNPC, bool bSendToRecorders)
{
for ( int i = 1; i <= MAX_PLAYERS; i++ )
{
CASW_Player *pPlayer = ToASW_Player( UTIL_PlayerByIndex( i ) );
if ( pPlayer && ( pPlayer->GetViewNPC() == pNPC || ( bSendToRecorders && ( pPlayer->IsAnyBot() || pPlayer->m_iWantsAutoRecord != 0 ) ) ) )
{
AddRecipient( pPlayer );
}
}
}
void UTIL_RD_HitConfirm( CBaseEntity *pTarget, int iHealthBefore, const CTakeDamageInfo &info )
{
Assert( pTarget );
Assert( iHealthBefore > 0 );
CASW_ViewNPCRecipientFilter filter;
CASW_Inhabitable_NPC *pInhabitableTarget = NULL;
if ( pTarget && pTarget->IsInhabitableNPC() )
{
pInhabitableTarget = assert_cast< CASW_Inhabitable_NPC * >( pTarget );
filter.AddRecipientsByViewNPC( pInhabitableTarget, rd_hit_confirms_to_recorders.GetBool() );
}
CBaseEntity *pAttacker = info.GetAttacker();
CBaseEntity *pWeapon = info.GetWeapon();
if ( CASW_Sentry_Top *pSentry = dynamic_cast< CASW_Sentry_Top * >( pAttacker ) )
{
CASW_Sentry_Base *pBase = pSentry->GetSentryBase();
if ( pBase && pBase->m_hDeployer )
{
pAttacker = pBase->m_hDeployer;
pWeapon = pBase;
}
}
CASW_Inhabitable_NPC *pInhabitableAttacker = NULL;
if ( pAttacker && pAttacker->IsInhabitableNPC() )
{
pInhabitableAttacker = assert_cast< CASW_Inhabitable_NPC * >( pAttacker );
filter.AddRecipientsByViewNPC( pInhabitableAttacker, rd_hit_confirms_to_recorders.GetBool() );
}
Vector vecDamagePosition = info.GetDamagePosition();
if ( pTarget && vecDamagePosition == vec3_origin )
{
vecDamagePosition = pTarget->WorldSpaceCenter();
}
bool bKilled = pTarget && pTarget->GetHealth() <= 0;
bool bDamageOverTime = info.GetDamageType() & DMG_DIRECT;
bool bBlastDamage = info.GetDamageType() & DMG_BLAST;
Disposition_t iDisposition = pInhabitableAttacker && pTarget ? pInhabitableAttacker->IRelationType( pTarget ) : D_HT;
int iHealth = pTarget ? pTarget->GetHealth() : 0;
float flDamage = MIN( iHealthBefore - iHealth, iHealthBefore );
UserMessageBegin( filter, "RDHitConfirm" );
WRITE_ENTITY( pAttacker ? pAttacker->entindex() : -1 );
WRITE_ENTITY( pTarget ? pTarget->entindex() : -1 );
WRITE_VEC3COORD( vecDamagePosition );
WRITE_BOOL( bKilled );
WRITE_BOOL( bDamageOverTime );
WRITE_BOOL( bBlastDamage );
WRITE_UBITLONG( iDisposition, 3 );
WRITE_FLOAT( flDamage );
WRITE_ENTITY( pWeapon ? pWeapon->entindex() : -1 );
MessageEnd();
ReactiveDropInventory::OnHitConfirm( pAttacker, pTarget, vecDamagePosition, bKilled, bDamageOverTime, bBlastDamage, iDisposition, flDamage, pWeapon );
}
void UTIL_RD_ExitOnLevelChange()
{
Assert( engine->IsDedicatedServer() );
if ( !engine->IsDedicatedServer() )
return;
static bool &s_bExitOnLevelChange = reinterpret_cast< bool *const * >( *reinterpret_cast< const byte *const * >( engine ) + 1 )[0][68];
if ( !s_bExitOnLevelChange )
{
Msg( "Server will exit on next level change.\n" );
s_bExitOnLevelChange = true;
// handle the case where the server hibernates rather than changing level:
engine->ServerCommand( "sv_shutdown\n" );
}
// assert that it wasn't some other nonzero value
Assert( s_bExitOnLevelChange == true );
}
#endif
//-----------------------------------------------------------------------------
// Purpose: Trace filter that only hits aliens (all NPCS but the marines, eggs, goo)
//-----------------------------------------------------------------------------
bool CTraceFilterAliensEggsGoo::ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
if ( CTraceFilterSimple::ShouldHitEntity(pServerEntity, contentsMask) )
{
#ifndef CLIENT_DLL
CBaseEntity *pEntity = EntityFromEntityHandle( pServerEntity );
if ( pEntity->Classify() == CLASS_ASW_MARINE ) // we dont hit marines in coop
return ASWDeathmatchMode() != NULL; // but hit them for deathmatch
if ( IsAlienClass( pEntity->Classify() ) )
return true;
#endif // !CLIENT_DLL
}
return false;
}
// NOTE: This function assumes 75 fov and 4:3 ratio (todo: support widescreen all the time?)
bool CanFrustumSee(const Vector &vecCameraCenter, const QAngle &angCameraFacing,
const Vector &pos, const int padding, const int forward_limit, float fov=75.0f)
{
Vector vForward, vRight, vUp;
AngleVectors(angCameraFacing, &vForward, &vRight, &vUp);
Vector vecTestPos = pos;
// bring in the x coord by the padding
if (vecTestPos.x < vecCameraCenter.x)
vecTestPos.x = MIN(vecCameraCenter.x, vecTestPos.x + padding);
else if (vecTestPos.x > vecCameraCenter.x)
vecTestPos.x = MAX(vecCameraCenter.x, vecTestPos.x - padding);
// bring in the y coord by the padding
if (vecTestPos.y < vecCameraCenter.y)
vecTestPos.y = MIN(vecCameraCenter.y, vecTestPos.y + padding);
else if (vecTestPos.y > vecCameraCenter.y)
vecTestPos.y = MAX(vecCameraCenter.y, vecTestPos.y - padding);
float ratio = 4.0f / 3.0f; // assume 4:3 res
float fov_tangent = tan(DEG2RAD(fov) * 0.5f);
Vector vDiff = vecTestPos - vecCameraCenter; // vector from camera to testing position
float forward_diff = vDiff.Dot(vForward);
if (forward_diff < 0) // behind the camera
return false;
if (forward_limit > 0 && forward_diff > forward_limit)
return false; // too far away
//int padding_at_this_distance = padding * (forward_diff / 405.0f); // adjust padding by ratio of distance to default camera height
float up_diff = vDiff.Dot(vUp);
float max_up_diff = forward_diff * fov_tangent;
if (up_diff > max_up_diff || up_diff < -max_up_diff)
return false;
float right_diff = vDiff.Dot(vRight);
float max_right_diff = max_up_diff * ratio;
if (right_diff > max_right_diff || right_diff < -max_right_diff)
return false;
return true;
}
CASW_Marine* UTIL_ASW_MarineCanSee(CASW_Marine_Resource* pMarineResource, const Vector &pos, const int padding, bool &bCorpseCanSee, const int forward_limit)
{
if (!pMarineResource)
return NULL;
Vector vecMarinePos;
CASW_Marine* pMarine = NULL;
#ifndef CLIENT_DLL
if (pMarineResource->GetHealthPercent() <=0 || !pMarineResource->IsAlive()) // if we're dead, take the corpse position
{
vecMarinePos = pMarineResource->m_vecDeathPosition;
// reactivedrop: this makes sure that dead marine is not considered as the one
// who sees aliens, thus preventing them from going into sleep state.
// This fixes the bug: When marine dies to a horde and other marines are far
// enough for horde to see them, the horde gets stuck in non-sleeping state
// and thus not decreasing the ammount of non-sleeping aliens. This leads to
// no hordes and wanderers being spawned at all and can be exploited on hard
// challenges and maps like Survival Desert.
if ( gpGlobals->curtime > pMarineResource->m_fDeathTime + 7.0f ) // After 6 seconds of marine's death player is switched to spectating alive marines, so after 7 seconds it is safe to make aliens that killed this marine enter the sleep state
return NULL;
}
else
#endif
{
pMarine = pMarineResource->GetMarineEntity();
if (!pMarine)
return NULL;
vecMarinePos = pMarine->GetAbsOrigin();
}
// note: assumes 60 degree pitch camera and 405 dist (actual convars for these are on the client...)
QAngle angCameraFacing(60, 90, 0);
Vector vForward, vRight, vUp;
AngleVectors(angCameraFacing, &vForward, &vRight, &vUp);
Vector vecCameraCenter = vecMarinePos - vForward * 405;
// see if they're beyond the fog plane
#ifdef CLIENT_DLL
C_ASW_Player *pPlayer = C_ASW_Player::GetLocalASWPlayer();
if (pPlayer)
{
if (pPlayer->GetPlayerFog().m_hCtrl->m_fog.enable)
{
float dist = (vecCameraCenter - pos).Length();
if (dist > pPlayer->GetPlayerFog().m_hCtrl->m_fog.end)
return NULL;
}
}
#else
// ASWTODO - no worldfogparams anymore?
/*
fogparams_t fog;
GetWorldFogParams(fog);
if (fog.enable.Get())
{
float dist = (vecCameraCenter - pos).Length();
if (dist > fog.end.Get())
return NULL;
}
*/
#endif
// check for plain near the marine
bool bNearby = CanFrustumSee(vecCameraCenter, angCameraFacing, pos, padding, forward_limit);
// check if he's looking through a remote turret and can possibly see us from that
if (!bNearby && pMarine && pMarine->IsControllingTurret() && pMarine->GetRemoteTurret())
{
// a turret can look in any direction, so let's just do a radius check (would match up with the fog anyways)
bNearby = (pMarine->GetRemoteTurret()->GetAbsOrigin().DistTo(pos) <= 1024); // assume fog distance of 1024 when in first person
}
// check if he's looking through a security cam
if (!bNearby && pMarine)
{
CBaseEntity* pUsing = pMarine->m_hUsingEntity.Get();
if ( pUsing && pUsing->Classify() == CLASS_ASW_COMPUTER_AREA )
{
CASW_Computer_Area* pComputer = assert_cast<CASW_Computer_Area*>(pUsing);
CPointCamera *pCam = pComputer->GetActiveCam();
if (pCam)
{
Vector vecCamFacing;
AngleVectors(pCam->GetAbsAngles(), &vecCamFacing);
bNearby = (vecCamFacing.Dot(pos - pCam->GetAbsOrigin()) > 0) && // check facing
(pCam->GetAbsOrigin().DistTo(pos) <= 1024); // assume fog distance of 1024 when in first person
}
}
}
#ifndef CLIENT_DLL
if (asw_debug_marine_can_see.GetBool())
{
if (bNearby)
{
NDebugOverlay::Line(pos, vecMarinePos + Vector(0,0,10), 255, 255, 0, true, 0.1f);
}
else
{
NDebugOverlay::Line(pos, vecMarinePos + Vector(0,0,10), 255, 0, 0, true, 0.1f);
}
}
#endif
if (bNearby)
{
if (!pMarine)
bCorpseCanSee = true;
return pMarine;
}
return NULL;
}
CASW_Marine* UTIL_ASW_AnyMarineCanSee(const Vector &pos, const int padding, bool &bCorpseCanSee, const int forward_limit)
{
// find the closest marine
CASW_Game_Resource *pGameResource = ASWGameResource();
if (!pGameResource)
return NULL;
bCorpseCanSee = false;
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMarineResource = pGameResource->GetMarineResource(i);
bool bCorpse = false;
CASW_Marine *pMarine = (UTIL_ASW_MarineCanSee(pMarineResource, pos, padding, bCorpse, forward_limit));
bCorpseCanSee |= bCorpse;
if (pMarine)
return pMarine;
}
return NULL;
}
bool UTIL_ASW_MarineViewCone(const Vector &pos)
{
// find the closest marine
CASW_Game_Resource *pGameResource = ASWGameResource();
if (!pGameResource)
return false;
for (int i=0;i<pGameResource->GetMaxMarineResources();i++)
{
CASW_Marine_Resource* pMarineResource = pGameResource->GetMarineResource(i);
if (!pMarineResource)
continue;
Vector vecMarinePos;
CASW_Marine* pMarine = pMarineResource->GetMarineEntity();
if (!pMarine)
continue;
// check it's not too far away
Vector vecDiff = pos - pMarine->GetAbsOrigin();
if (vecDiff.LengthSqr() > asw_marine_view_cone_dist.GetFloat())
continue;
// check dot
Vector vecFacing;