forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathproto_sniper.cpp
More file actions
4002 lines (3298 loc) · 105 KB
/
proto_sniper.cpp
File metadata and controls
4002 lines (3298 loc) · 105 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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "ai_default.h"
#include "ai_basenpc.h"
#include "ammodef.h"
#include "ai_task.h"
#include "ai_schedule.h"
#include "ai_node.h"
#include "ai_hull.h"
#include "ai_memory.h"
#include "ai_senses.h"
#include "beam_shared.h"
#include "game.h"
#include "npcevent.h"
#include "entitylist.h"
#include "activitylist.h"
#include "soundent.h"
#include "gib.h"
#include "ndebugoverlay.h"
#include "smoke_trail.h"
#include "weapon_rpg.h"
#include "player.h"
#include "mathlib/mathlib.h"
#include "vstdlib/random.h"
#include "engine/IEngineSound.h"
#include "IEffects.h"
#include "effect_color_tables.h"
#include "npc_rollermine.h"
#include "eventqueue.h"
#ifdef MAPBASE
#include "CRagdollMagnet.h"
#endif
#ifdef EXPANDED_RESPONSE_SYSTEM_USAGE
#include "mapbase/expandedrs_combine.h"
#include "ai_speech.h"
#endif
#include "effect_dispatch_data.h"
#include "te_effect_dispatch.h"
#include "collisionutils.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
extern Vector PointOnLineNearestPoint(const Vector& vStartPos, const Vector& vEndPos, const Vector& vPoint);
ConVar bulletSpeed( "bulletspeed", "6000" );
ConVar sniperLines( "showsniperlines", "0" );
ConVar sniperviewdist("sniperviewdist", "35" );
ConVar showsniperdist("showsniperdist", "0" );
ConVar sniperspeak( "sniperspeak", "0" );
ConVar sniper_xbox_delay( "sniper_xbox_delay", "1" );
#ifdef MAPBASE
// ConVars for configuring Unhidden Snipers
ConVar npc_sniper_riflemodel("npc_sniper_riflemodel", "models/weapons/w_irifle.mdl");
ConVar npc_sniper_rifle_offset_x("npc_sniper_rifle_offset_x", "10.5");
ConVar npc_sniper_rifle_offset_y("npc_sniper_rifle_offset_y", "-2.5");
ConVar npc_sniper_rifle_offset_z("npc_sniper_rifle_offset_z", "45.0");
ConVar npc_sniper_rifle_despawn_t("npc_sniper_rifle_despawn_t", "5");
ConVar npc_sniper_rifle_use_attachment("npc_sniper_rifle_use_attachment", "1");
ConVar npc_sniper_rifle_attachment_point("npc_sniper_rifle_attachment_point", "lefthand");
ConVar npc_sniper_rifle_offset_x_attachment("npc_sniper_rifle_offset_x_attachment", "-3.5");
ConVar npc_sniper_rifle_offset_y_attachment("npc_sniper_rifle_offset_y_attachment", "-7.5");
ConVar npc_sniper_rifle_offset_z_attachment("npc_sniper_rifle_offset_z_attachment", "0");
ConVar npc_sniper_use_aiming_anims("npc_sniper_use_aiming_anims", "1");
ConVar npc_sniper_should_rotate_body("npc_sniper_should_rotate_body", "1");
ConVar sk_npc_sniper_easy_delay("sk_npc_sniper_easy_delay", "5.0");
#endif
// Moved to HL2_SharedGameRules because these are referenced by shared AmmoDef functions
extern ConVar sk_dmg_sniper_penetrate_plr;
extern ConVar sk_dmg_sniper_penetrate_npc;
// No model, impervious to damage.
#define SF_SNIPER_HIDDEN (1 << 16)
#define SF_SNIPER_VIEWCONE (1 << 17) ///< when set, sniper only sees in a small cone around the laser.
#define SF_SNIPER_NOCORPSE (1 << 18) ///< when set, no corpse
#define SF_SNIPER_STARTDISABLED (1 << 19)
#define SF_SNIPER_FAST (1 << 20) ///< This is faster-shooting sniper. Paint time is decreased 25%. Bullet speed increases 150%.
#define SF_SNIPER_NOSWEEP (1 << 21) ///< This sniper doesn't sweep to the target or use decoys.
#ifdef MAPBASE
#define SF_SNIPER_DIE_ON_FIRE (1 << 22) // This sniper dies on fire.
#define SF_SNIPER_DELAY_FIRE_ON_EASY (1 << 23) // This sniper will delay fire by 5 seconds on Easy.
#endif
// If the last time I fired at someone was between 0 and this many seconds, draw
// a bead on them much faster. (use subsequent paint time)
#define SNIPER_FASTER_ATTACK_PERIOD 3.0f
// These numbers determine the interval between shots. They used to be constants,
// but are now keyfields. HL2 backwards compatibility was maintained by supplying
// default values in the constructor.
#if 0
// How long to aim at someone before shooting them.
#define SNIPER_PAINT_ENEMY_TIME 1.0f
// ...plus this
#define SNIPER_PAINT_NPC_TIME_NOISE 0.75f
#else
// How long to aim at someone before shooting them.
#define SNIPER_DEFAULT_PAINT_ENEMY_TIME 1.0f
// ...plus this
#define SNIPER_DEFAULT_PAINT_NPC_TIME_NOISE 0.75f
#endif
#define SNIPER_SUBSEQUENT_PAINT_TIME ( ( IsXbox() ) ? 1.0f : 0.4f )
#define SNIPER_FOG_PAINT_ENEMY_TIME 0.25f
#define SNIPER_PAINT_DECOY_TIME 2.0f
#define SNIPER_PAINT_FRUSTRATED_TIME 1.0f
#define SNIPER_QUICKAIM_TIME 0.2f
#define SNIPER_PAINT_NO_SHOT_TIME 0.7f
#define SNIPER_DECOY_MAX_MASS 200.0f
// #def'ing this will turn on heaps of sniper debug messages.
#undef SNIPER_DEBUG
// Target protection
#define SNIPER_PROTECTION_MINDIST (1024.0*1024.0) // Distance around protect target that sniper does priority modification in
#define SNIPER_PROTECTION_PRIORITYCAP 100.0 // Max addition to priority of an enemy right next to the protect target, falls to 0 at SNIPER_PROTECTION_MINDIST.
//---------------------------------------------------------
// Like an infotarget, but shares a spawnflag that has
// relevance to the sniper.
//---------------------------------------------------------
#define SF_SNIPERTARGET_SHOOTME 1
#define SF_SNIPERTARGET_NOINTERRUPT 2
#define SF_SNIPERTARGET_SNAPSHOT 4
#define SF_SNIPERTARGET_RESUME 8
#define SF_SNIPERTARGET_SNAPTO 16
#define SF_SNIPERTARGET_FOCUS 32
#define SNIPER_DECOY_RADIUS 256
#define SNIPER_NUM_DECOYS 5
#define NUM_OLDDECOYS 5
#define NUM_PENETRATIONS 3
#define PENETRATION_THICKNESS 5
#define SNIPER_MAX_GROUP_TARGETS 16
//=========================================================
//=========================================================
class CSniperTarget : public CPointEntity
{
DECLARE_DATADESC();
public:
DECLARE_CLASS( CSniperTarget, CPointEntity );
bool KeyValue( const char *szKeyName, const char *szValue );
string_t m_iszGroupName;
};
//---------------------------------------------------------
// Save/Restore
//---------------------------------------------------------
BEGIN_DATADESC( CSniperTarget )
DEFINE_FIELD( m_iszGroupName, FIELD_STRING ),
END_DATADESC()
//=========================================================
//=========================================================
class CSniperBullet : public CBaseEntity
{
public:
DECLARE_CLASS( CSniperBullet, CBaseEntity );
CSniperBullet( void ) { Init(); }
Vector m_vecDir;
Vector m_vecStart;
Vector m_vecEnd;
float m_flLastThink;
float m_SoundTime;
int m_AmmoType;
int m_PenetratedAmmoType;
float m_Speed;
bool m_bDirectShot;
void Precache( void );
bool IsActive( void ) { return m_fActive; }
bool Start( const Vector &vecOrigin, const Vector &vecTarget, CBaseEntity *pOwner, bool bDirectShot );
void Stop( void );
void BulletThink( void );
void Init( void );
DECLARE_DATADESC();
private:
// Only one shot per sniper at a time. If a bullet hasn't
// hit, the shooter must wait.
bool m_fActive;
// This tracks how many times this single bullet has
// struck. This is for penetration, so the bullet can
// go through things.
int m_iImpacts;
};
//=========================================================
//=========================================================
#ifdef EXPANDED_RESPONSE_SYSTEM_USAGE
class CProtoSniper : public CAI_ExpresserHost<CAI_BaseNPC>
{
DECLARE_CLASS( CProtoSniper, CAI_ExpresserHost<CAI_BaseNPC> );
#else
class CProtoSniper : public CAI_BaseNPC
{
DECLARE_CLASS( CProtoSniper, CAI_BaseNPC );
#endif
public:
CProtoSniper( void );
void Precache( void );
void Spawn( void );
Class_T Classify( void );
float MaxYawSpeed( void );
Vector EyePosition( void );
void UpdateEfficiency( bool bInPVS ) { SetEfficiency( ( GetSleepState() != AISS_AWAKE ) ? AIE_DORMANT : AIE_NORMAL ); SetMoveEfficiency( AIME_NORMAL ); }
bool IsLaserOn( void ) { return m_pBeam != NULL; }
void Event_Killed( const CTakeDamageInfo &info );
void Event_KilledOther( CBaseEntity *pVictim, const CTakeDamageInfo &info );
void UpdateOnRemove( void );
int OnTakeDamage_Alive( const CTakeDamageInfo &info );
bool WeaponLOSCondition(const Vector &ownerPos, const Vector &targetPos, bool bSetConditions) {return true;}
int IRelationPriority( CBaseEntity *pTarget );
bool IsFastSniper() { return HasSpawnFlags(SF_SNIPER_FAST); }
bool QuerySeeEntity( CBaseEntity *pEntity, bool bOnlyHateOrFearIfNPC = false );
virtual bool FInViewCone( CBaseEntity *pEntity );
void StartTask( const Task_t *pTask );
void RunTask( const Task_t *pTask );
int RangeAttack1Conditions ( float flDot, float flDist );
bool FireBullet( const Vector &vecTarget, bool bDirectShot );
float GetBulletSpeed();
Vector DesiredBodyTarget( CBaseEntity *pTarget );
Vector LeadTarget( CBaseEntity *pTarget );
CBaseEntity *PickDeadPlayerTarget();
virtual int SelectSchedule( void );
virtual int TranslateSchedule( int scheduleType );
#ifdef MAPBASE
Activity NPC_TranslateActivity( Activity eNewActivity );
#endif
bool KeyValue( const char *szKeyName, const char *szValue );
void PrescheduleThink( void );
static const char *pAttackSounds[];
bool FCanCheckAttacks ( void );
bool FindDecoyObject( void );
void ScopeGlint();
int GetSoundInterests( void );
void OnListened();
Vector GetBulletOrigin( void );
virtual int Restore( IRestore &restore );
virtual void OnScheduleChange( void );
bool FVisible( CBaseEntity *pEntity, int traceMask = MASK_BLOCKLOS, CBaseEntity **ppBlocker = NULL );
bool ShouldNotDistanceCull() { return true; }
int DrawDebugTextOverlays();
void NotifyShotMissedTarget();
#ifdef EXPANDED_RESPONSE_SYSTEM_USAGE
//DeclareResponseSystem()
bool SpeakIfAllowed(const char *concept, const char *modifiers = NULL);
void ModifyOrAppendCriteria( AI_CriteriaSet& set );
virtual CAI_Expresser *CreateExpresser( void );
virtual CAI_Expresser *GetExpresser() { return m_pExpresser; }
virtual void PostConstructor( const char *szClassname );
#endif
private:
#ifdef EXPANDED_RESPONSE_SYSTEM_USAGE
CAI_Expresser * m_pExpresser;
#endif
bool ShouldSnapShot( void );
void ClearTargetGroup( void );
float GetPositionParameter( float flTime, bool fLinear );
void GetPaintAim( const Vector &vecStart, const Vector &vecGoal, float flParameter, Vector *pProgress );
bool IsSweepingRandomly( void ) { return m_iNumGroupTargets > 0; }
void ClearOldDecoys( void );
void AddOldDecoy( CBaseEntity *pDecoy );
bool HasOldDecoy( CBaseEntity *pDecoy );
bool FindFrustratedShot( float flNoise );
bool VerifyShot( CBaseEntity *pTarget );
void SetSweepTarget( const char *pszTarget );
// Inputs
void InputEnableSniper( inputdata_t &inputdata );
void InputDisableSniper( inputdata_t &inputdata );
void InputSetDecoyRadius( inputdata_t &inputdata );
void InputSweepTarget( inputdata_t &inputdata );
void InputSweepTargetHighestPriority( inputdata_t &inputdata );
void InputSweepGroupRandomly( inputdata_t &inputdata );
void InputStopSweeping( inputdata_t &inputdata );
void InputProtectTarget( inputdata_t &inputdata );
#if HL2_EPISODIC
void InputSetPaintInterval( inputdata_t &inputdata );
void InputSetPaintIntervalVariance( inputdata_t &inputdata );
#endif
void LaserOff( void );
void LaserOn( const Vector &vecTarget, const Vector &vecDeviance );
void PaintTarget( const Vector &vecTarget, float flPaintTime );
bool IsPlayerAllySniper();
#ifdef MAPBASE
const Vector &GetPaintCursor() { return m_vecPaintCursor; }
#endif
private:
/// This is the variable from which m_flPaintTime gets set.
/// How long to aim at someone before shooting them.
float m_flKeyfieldPaintTime;
/// A random number from 0 to this is added to m_flKeyfieldPaintTime
/// to yield m_flPaintTime's initial delay.
float m_flKeyfieldPaintTimeNoise;
// This keeps track of the last spot the laser painted. For
// continuous sweeping that changes direction.
Vector m_vecPaintCursor;
float m_flPaintTime;
bool m_fWeaponLoaded;
bool m_fEnabled;
bool m_fIsPatient;
float m_flPatience;
int m_iMisses;
EHANDLE m_hDecoyObject;
EHANDLE m_hSweepTarget;
Vector m_vecDecoyObjectTarget;
Vector m_vecFrustratedTarget;
Vector m_vecPaintStart; // used to track where a sweep starts for the purpose of interpolating.
float m_flFrustration;
float m_flThinkInterval;
float m_flDecoyRadius;
CBeam *m_pBeam;
bool m_fSnapShot;
int m_iNumGroupTargets;
CBaseEntity *m_pGroupTarget[ SNIPER_MAX_GROUP_TARGETS ];
bool m_bSweepHighestPriority; // My hack :[ (sjb)
int m_iBeamBrightness;
// bullet stopping energy shield effect.
float m_flShieldDist;
float m_flShieldRadius;
float m_flTimeLastAttackedPlayer;
// Protection
EHANDLE m_hProtectTarget; // Entity that this sniper is supposed to protect
float m_flDangerEnemyDistance; // Distance to the enemy nearest the protect target
// Have I warned the target that I'm pointing my laser at them?
bool m_bWarnedTargetEntity;
float m_flTimeLastShotMissed;
bool m_bKilledPlayer;
bool m_bShootZombiesInChest; ///< if true, do not try to shoot zombies in the headcrab
#ifdef MAPBASE
string_t m_iszBeamName; // Custom beam texture
color32 m_BeamColor; // Custom beam color
#endif
COutputEvent m_OnShotFired;
#ifdef MAPBASE
CBaseEntity* m_FakeRifle;
#endif
DEFINE_CUSTOM_AI;
DECLARE_DATADESC();
#ifdef MAPBASE_VSCRIPT
DECLARE_ENT_SCRIPTDESC();
#endif
};
//=========================================================
//=========================================================
// NOTES about the Sniper:
//
// PATIENCE:
// The concept of "patience" is simply a restriction placed
// on how close a target has to be to the sniper before the
// sniper will take his first shot at the target. This
// distance is referred to as "patience" is set by the `
// designer in Worldcraft. The sniper won't attack unless
// the target enters this radius. Once the sniper takes
// this first shot, he will not return to a patient state.
// He will then shoot at any/all targets to which there is
// a clear shot, regardless of distance. (sjb)
//
//
// TODO: Sniper accumulates frustration while reloading.
// probably should subtract reload time from frustration.
//=========================================================
//=========================================================
//=========================================================
//=========================================================
short sFlashSprite;
short sHaloSprite;
//=========================================================
//=========================================================
BEGIN_DATADESC( CProtoSniper )
DEFINE_FIELD( m_fWeaponLoaded, FIELD_BOOLEAN ),
DEFINE_FIELD( m_fEnabled, FIELD_BOOLEAN ),
DEFINE_FIELD( m_fIsPatient, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flPatience, FIELD_FLOAT ),
DEFINE_FIELD( m_iMisses, FIELD_INTEGER ),
DEFINE_FIELD( m_hDecoyObject, FIELD_EHANDLE ),
DEFINE_FIELD( m_hSweepTarget, FIELD_EHANDLE ),
DEFINE_FIELD( m_vecDecoyObjectTarget, FIELD_VECTOR ),
DEFINE_FIELD( m_vecFrustratedTarget, FIELD_VECTOR ),
DEFINE_FIELD( m_vecPaintStart, FIELD_VECTOR ),
DEFINE_FIELD( m_flPaintTime, FIELD_TIME ),
DEFINE_FIELD( m_vecPaintCursor, FIELD_VECTOR ),
DEFINE_FIELD( m_flFrustration, FIELD_TIME ),
DEFINE_FIELD( m_flThinkInterval, FIELD_FLOAT ),
DEFINE_FIELD( m_flDecoyRadius, FIELD_FLOAT ),
DEFINE_FIELD( m_pBeam, FIELD_CLASSPTR ),
DEFINE_FIELD( m_fSnapShot, FIELD_BOOLEAN ),
DEFINE_FIELD( m_iNumGroupTargets, FIELD_INTEGER ),
DEFINE_ARRAY( m_pGroupTarget, FIELD_CLASSPTR, SNIPER_MAX_GROUP_TARGETS ),
DEFINE_KEYFIELD( m_iBeamBrightness, FIELD_INTEGER, "beambrightness" ),
DEFINE_KEYFIELD(m_flShieldDist, FIELD_FLOAT, "shielddistance" ),
DEFINE_KEYFIELD(m_flShieldRadius, FIELD_FLOAT, "shieldradius" ),
DEFINE_KEYFIELD(m_bShootZombiesInChest, FIELD_BOOLEAN, "shootZombiesInChest" ),
DEFINE_KEYFIELD(m_flKeyfieldPaintTime, FIELD_FLOAT, "PaintInterval" ),
DEFINE_KEYFIELD(m_flKeyfieldPaintTimeNoise, FIELD_FLOAT, "PaintIntervalVariance" ),
DEFINE_FIELD( m_flTimeLastAttackedPlayer, FIELD_TIME ),
DEFINE_FIELD( m_hProtectTarget, FIELD_EHANDLE ),
DEFINE_FIELD( m_flDangerEnemyDistance, FIELD_FLOAT ),
DEFINE_FIELD( m_bSweepHighestPriority, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bWarnedTargetEntity, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flTimeLastShotMissed, FIELD_TIME ),
#ifdef MAPBASE
DEFINE_KEYFIELD( m_iszBeamName, FIELD_STRING, "BeamName" ),
DEFINE_KEYFIELD( m_BeamColor, FIELD_COLOR32, "BeamColor" ),
#endif
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "EnableSniper", InputEnableSniper ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisableSniper", InputDisableSniper ),
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetDecoyRadius", InputSetDecoyRadius ),
DEFINE_INPUTFUNC( FIELD_STRING, "SweepTarget", InputSweepTarget ),
DEFINE_INPUTFUNC( FIELD_STRING, "SweepTargetHighestPriority", InputSweepTargetHighestPriority ),
DEFINE_INPUTFUNC( FIELD_STRING, "SweepGroupRandomly", InputSweepGroupRandomly ),
DEFINE_INPUTFUNC( FIELD_STRING, "StopSweeping", InputStopSweeping ),
DEFINE_INPUTFUNC( FIELD_STRING, "ProtectTarget", InputProtectTarget ),
#if HL2_EPISODIC
DEFINE_INPUTFUNC( FIELD_FLOAT, "SetPaintInterval", InputSetPaintInterval ),
DEFINE_INPUTFUNC( FIELD_FLOAT, "SetPaintIntervalVariance", InputSetPaintIntervalVariance ),
#endif
// Outputs
DEFINE_OUTPUT( m_OnShotFired, "OnShotFired" ),
END_DATADESC()
#ifdef MAPBASE_VSCRIPT
BEGIN_ENT_SCRIPTDESC( CProtoSniper, CAI_BaseNPC, "Combine sniper NPC." )
DEFINE_SCRIPTFUNC( GetBulletSpeed, "" )
DEFINE_SCRIPTFUNC( GetBulletOrigin, "" )
DEFINE_SCRIPTFUNC( ScopeGlint, "" )
DEFINE_SCRIPTFUNC( GetPositionParameter, "" )
DEFINE_SCRIPTFUNC( IsSweepingRandomly, "" )
DEFINE_SCRIPTFUNC( FindFrustratedShot, "" )
DEFINE_SCRIPTFUNC( IsLaserOn, "" )
DEFINE_SCRIPTFUNC( LaserOn, "" )
DEFINE_SCRIPTFUNC( LaserOff, "" )
DEFINE_SCRIPTFUNC( GetPaintCursor, "Get the point the sniper is currently aiming at." )
END_SCRIPTDESC()
#endif
//=========================================================
//=========================================================
BEGIN_DATADESC( CSniperBullet )
DEFINE_FIELD( m_SoundTime, FIELD_TIME ),
DEFINE_FIELD( m_AmmoType, FIELD_INTEGER ),
DEFINE_FIELD( m_PenetratedAmmoType, FIELD_INTEGER ),
DEFINE_FIELD( m_fActive, FIELD_BOOLEAN ),
DEFINE_FIELD( m_iImpacts, FIELD_INTEGER ),
DEFINE_FIELD( m_vecOrigin, FIELD_VECTOR ),
DEFINE_FIELD( m_vecDir, FIELD_VECTOR ),
DEFINE_FIELD( m_flLastThink, FIELD_TIME ),
DEFINE_FIELD( m_Speed, FIELD_FLOAT ),
DEFINE_FIELD( m_bDirectShot, FIELD_BOOLEAN ),
DEFINE_FIELD( m_vecStart, FIELD_VECTOR ),
DEFINE_FIELD( m_vecEnd, FIELD_VECTOR ),
DEFINE_THINKFUNC( BulletThink ),
END_DATADESC()
//=========================================================
// Private conditions
//=========================================================
enum Sniper_Conds
{
COND_SNIPER_CANATTACKDECOY = LAST_SHARED_CONDITION,
COND_SNIPER_SUPPRESSED,
COND_SNIPER_ENABLED,
COND_SNIPER_DISABLED,
COND_SNIPER_FRUSTRATED,
COND_SNIPER_SWEEP_TARGET,
COND_SNIPER_NO_SHOT,
#ifdef MAPBASE
// Using COND_ENEMY_DEAD made us take credit for other people's kills
COND_SNIPER_KILLED_ENEMY,
#endif
};
//=========================================================
// schedules
//=========================================================
enum
{
SCHED_PSNIPER_SCAN = LAST_SHARED_SCHEDULE,
SCHED_PSNIPER_CAMP,
SCHED_PSNIPER_ATTACK,
SCHED_PSNIPER_RELOAD,
SCHED_PSNIPER_ATTACKDECOY,
SCHED_PSNIPER_SUPPRESSED,
SCHED_PSNIPER_DISABLEDWAIT,
SCHED_PSNIPER_FRUSTRATED_ATTACK,
SCHED_PSNIPER_SWEEP_TARGET,
SCHED_PSNIPER_SWEEP_TARGET_NOINTERRUPT,
SCHED_PSNIPER_SNAPATTACK,
SCHED_PSNIPER_NO_CLEAR_SHOT,
SCHED_PSNIPER_PLAYER_DEAD,
};
//=========================================================
// tasks
//=========================================================
enum
{
TASK_SNIPER_FRUSTRATED_ATTACK = LAST_SHARED_TASK,
TASK_SNIPER_PAINT_ENEMY,
TASK_SNIPER_PAINT_DECOY,
TASK_SNIPER_PAINT_FRUSTRATED,
TASK_SNIPER_PAINT_SWEEP_TARGET,
TASK_SNIPER_ATTACK_CURSOR,
TASK_SNIPER_PAINT_NO_SHOT,
TASK_SNIPER_PLAYER_DEAD,
};
CProtoSniper::CProtoSniper( void ) : m_flKeyfieldPaintTime(SNIPER_DEFAULT_PAINT_ENEMY_TIME),
m_flKeyfieldPaintTimeNoise(SNIPER_DEFAULT_PAINT_NPC_TIME_NOISE)
{
#ifdef _DEBUG
m_vecPaintCursor.Init();
m_vecDecoyObjectTarget.Init();
m_vecFrustratedTarget.Init();
m_vecPaintStart.Init();
#endif
m_iMisses = 0;
m_flDecoyRadius = SNIPER_DECOY_RADIUS;
m_fSnapShot = false;
m_iBeamBrightness = 100;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CProtoSniper::QuerySeeEntity( CBaseEntity *pEntity, bool bOnlyHateOrFearIfNPC )
{
Disposition_t disp = IRelationType(pEntity);
#ifdef MAPBASE
if( disp > D_FR )
#else
if( disp != D_HT )
#endif
{
// Don't bother with anything I wouldn't shoot.
return false;
}
if( !FInViewCone(pEntity) )
{
// Yes, this does call FInViewCone twice a frame for all entities checked for
// visibility, but doing this allows us to cut out a bunch of traces that would
// be done by VerifyShot for entities that aren't even in our viewcone.
return false;
}
if( VerifyShot( pEntity ) )
{
return BaseClass::QuerySeeEntity(pEntity, bOnlyHateOrFearIfNPC);
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CProtoSniper::FInViewCone ( CBaseEntity *pEntity )
{
if( pEntity->GetFlags() & FL_CLIENT )
{
CBasePlayer *pPlayer;
pPlayer = ToBasePlayer( pEntity );
if( m_spawnflags & SF_SNIPER_VIEWCONE )
{
// See how close this spot is to the laser.
Vector vecEyes;
Vector vecLOS;
float flDist;
Vector vecNearestPoint;
vecEyes = EyePosition();
vecLOS = m_vecPaintCursor - vecEyes;
VectorNormalize(vecLOS);
vecNearestPoint = PointOnLineNearestPoint( EyePosition(), EyePosition() + vecLOS * 8192, pPlayer->EyePosition() );
flDist = ( pPlayer->EyePosition() - vecNearestPoint ).Length();
if( showsniperdist.GetFloat() != 0 )
{
Msg( "Dist from beam: %f\n", flDist );
}
if( flDist <= sniperviewdist.GetFloat() )
{
return true;
}
return false;
}
}
return BaseClass::FInViewCone( pEntity->EyePosition() );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CProtoSniper::LaserOff( void )
{
if( m_pBeam )
{
UTIL_Remove( m_pBeam);
m_pBeam = NULL;
}
SetNextThink( gpGlobals->curtime + 0.1f );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#define LASER_LEAD_DIST 64
void CProtoSniper::LaserOn( const Vector &vecTarget, const Vector &vecDeviance )
{
if (!m_pBeam)
{
#ifdef MAPBASE
m_pBeam = CBeam::BeamCreate( STRING(m_iszBeamName), 1.0f );
m_pBeam->SetColor( m_BeamColor.r, m_BeamColor.g, m_BeamColor.b );
#else
m_pBeam = CBeam::BeamCreate( "effects/bluelaser1.vmt", 1.0f );
m_pBeam->SetColor( 0, 100, 255 );
#endif
}
else
{
// Beam seems to be on.
//return;
}
// Don't aim right at the guy right now.
Vector vecInitialAim;
if( vecDeviance == vec3_origin )
{
// Start the aim where it last left off!
vecInitialAim = m_vecPaintCursor;
}
else
{
vecInitialAim = vecTarget;
}
vecInitialAim.x += random->RandomFloat( -vecDeviance.x, vecDeviance.x );
vecInitialAim.y += random->RandomFloat( -vecDeviance.y, vecDeviance.y );
vecInitialAim.z += random->RandomFloat( -vecDeviance.z, vecDeviance.z );
// The beam is backwards, sortof. The endpoint is the sniper. This is
// so that the beam can be tapered to very thin where it emits from the sniper.
#ifndef MAPBASE
m_pBeam->PointsInit(vecInitialAim, GetBulletOrigin());
#endif
#ifdef MAPBASE
if (HasSpawnFlags(SF_SNIPER_HIDDEN))
{
m_pBeam->PointsInit(vecInitialAim, GetBulletOrigin());
}
else
{
// Try to make the laser emit from the rifle or the NPC origin
if (GetNavigator() != NULL && GetNavigator()->IsGoalActive())
{
vecInitialAim = vecTarget;
}
if (m_FakeRifle != NULL)
{
m_pBeam->PointsInit(vecInitialAim, m_FakeRifle->GetAbsOrigin());
}
else
{
m_pBeam->PointsInit(vecInitialAim, GetAbsOrigin());
}
}
#endif
m_pBeam->SetBrightness( 255 );
m_pBeam->SetNoise( 0 );
m_pBeam->SetWidth( 1.0f );
m_pBeam->SetEndWidth( 0 );
m_pBeam->SetScrollRate( 0 );
m_pBeam->SetFadeLength( 0 );
m_pBeam->SetHaloTexture( sHaloSprite );
m_pBeam->SetHaloScale( 4.0f );
m_vecPaintStart = vecInitialAim;
// Think faster whilst painting. Higher resolution on the
// beam movement.
SetNextThink( gpGlobals->curtime + 0.02 );
}
//-----------------------------------------------------------------------------
// Crikey!
//-----------------------------------------------------------------------------
float CProtoSniper::GetPositionParameter( float flTime, bool fLinear )
{
float flElapsedTime;
float flTimeParameter;
flElapsedTime = flTime - (GetWaitFinishTime() - gpGlobals->curtime);
flTimeParameter = ( flElapsedTime / flTime );
if( fLinear )
{
return flTimeParameter;
}
else
{
return (1 + sin( (M_PI * flTimeParameter) - (M_PI / 2) ) ) / 2;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CProtoSniper::GetPaintAim( const Vector &vecStart, const Vector &vecGoal, float flParameter, Vector *pProgress )
{
#if 0
Vector vecDelta;
vecDelta = vecGoal - vecStart;
float flDist = VectorNormalize( vecDelta );
vecDelta = vecStart + vecDelta * (flDist * flParameter);
vecDelta = (vecDelta - GetBulletOrigin() ).Normalize();
*pProgress = vecDelta;
#else
// Quaternions
Vector vecIdealDir;
QAngle vecIdealAngles;
QAngle vecCurrentAngles;
Vector vecCurrentDir;
Vector vecBulletOrigin = GetBulletOrigin();
// vecIdealDir is where the gun should be aimed when the painting
// time is up. This can be approximate. This is only for drawing the
// laser, not actually aiming the weapon. A large discrepancy will look
// bad, though.
vecIdealDir = vecGoal - vecBulletOrigin;
VectorNormalize(vecIdealDir);
// Now turn vecIdealDir into angles!
VectorAngles( vecIdealDir, vecIdealAngles );
// This is the vector of the beam's current aim.
vecCurrentDir = vecStart - vecBulletOrigin;
VectorNormalize(vecCurrentDir);
// Turn this to angles, too.
VectorAngles( vecCurrentDir, vecCurrentAngles );
Quaternion idealQuat;
Quaternion currentQuat;
Quaternion aimQuat;
AngleQuaternion( vecIdealAngles, idealQuat );
AngleQuaternion( vecCurrentAngles, currentQuat );
QuaternionSlerp( currentQuat, idealQuat, flParameter, aimQuat );
QuaternionAngles( aimQuat, vecCurrentAngles );
// Rebuild the current aim vector.
AngleVectors( vecCurrentAngles, &vecCurrentDir );
*pProgress = vecCurrentDir;
#endif
}
//-----------------------------------------------------------------------------
// Sweep the laser sight towards the point where the gun should be aimed
//-----------------------------------------------------------------------------
void CProtoSniper::PaintTarget( const Vector &vecTarget, float flPaintTime )
{
Vector vecCurrentDir;
Vector vecStart;
// vecStart is the barrel of the gun (or the laser sight)
vecStart = GetBulletOrigin();
float P;
// keep painttime from hitting 0 exactly.
flPaintTime = MAX( flPaintTime, 0.000001f );
P = GetPositionParameter( flPaintTime, false );
// Vital allies are sharper about avoiding the sniper.
if( P > 0.25f && GetEnemy() && GetEnemy()->IsNPC() && HasCondition(COND_SEE_ENEMY) && !m_bWarnedTargetEntity )
{
m_bWarnedTargetEntity = true;
if( GetEnemy()->Classify() == CLASS_PLAYER_ALLY_VITAL && GetEnemy()->MyNPCPointer()->FVisible(this) )
{
CSoundEnt::InsertSound( SOUND_DANGER | SOUND_CONTEXT_REACT_TO_SOURCE, GetEnemy()->EarPosition(), 16, 1.0f, this );
}
}
GetPaintAim( m_vecPaintStart, vecTarget, clamp(P,0.0f,1.0f), &vecCurrentDir );
#if 1
#define THRESHOLD 0.8f
float flNoiseScale;
if ( P >= THRESHOLD )
{
flNoiseScale = 1 - (1 / (1 - THRESHOLD)) * ( P - THRESHOLD );
}
else if ( P <= 1 - THRESHOLD )
{
flNoiseScale = P / (1 - THRESHOLD);
}
else
{
flNoiseScale = 1;
}
// mult by P
vecCurrentDir.x += flNoiseScale * ( sin( 3 * M_PI * gpGlobals->curtime ) * 0.0006 );
vecCurrentDir.y += flNoiseScale * ( sin( 2 * M_PI * gpGlobals->curtime + 0.5 * M_PI ) * 0.0006 );
vecCurrentDir.z += flNoiseScale * ( sin( 1.5 * M_PI * gpGlobals->curtime + M_PI ) * 0.0006 );
#endif
trace_t tr;
UTIL_TraceLine( vecStart, vecStart + vecCurrentDir * 8192, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
m_pBeam->SetStartPos( tr.endpos );
m_pBeam->RelinkBeam();
m_vecPaintCursor = tr.endpos;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CProtoSniper::IsPlayerAllySniper()
{
CBaseEntity *pPlayer = AI_GetSinglePlayer();
return IRelationType( pPlayer ) == D_LI;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CProtoSniper::InputSetDecoyRadius( inputdata_t &inputdata )
{
m_flDecoyRadius = (float)inputdata.value.Int();
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CProtoSniper::OnScheduleChange( void )
{
LaserOff();
#ifdef MAPBASE
if ( m_bKilledPlayer && HasCondition( COND_SEE_PLAYER ) )
{
// IMPOSSIBLE! (possible when SP respawn is enabled)
m_bKilledPlayer = false;
}
#endif
BaseClass::OnScheduleChange();