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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "ai_basenpc.h"
#include "ammodef.h"
#include "ai_memory.h"
#include "weapon_rpg.h"
#include "effect_color_tables.h"
#include "te_effect_dispatch.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
extern const char* g_pModelNameLaser;
// No model, impervious to damage.
#define SF_STARTDISABLED (1 << 19)
#define CANNON_PAINT_ENEMY_TIME 1.0f
#define CANNON_SUBSEQUENT_PAINT_TIME 0.4f
#define CANNON_PAINT_NPC_TIME_NOISE 1.0f
#define NUM_ANCILLARY_BEAMS 4
int gHaloTexture = 0;
//-----------------------------------------------------------------------------
//
// Combine Cannon
//
//-----------------------------------------------------------------------------
class CNPC_Combine_Cannon : public CAI_BaseNPC
{
DECLARE_CLASS( CNPC_Combine_Cannon, CAI_BaseNPC );
public:
CNPC_Combine_Cannon( void );
virtual void Precache( void );
virtual void Spawn( void );
virtual Class_T Classify( void );
virtual float MaxYawSpeed( void );
virtual Vector EyePosition( void );
virtual void UpdateOnRemove( void );
virtual int OnTakeDamage_Alive( const CTakeDamageInfo &info );
virtual bool QuerySeeEntity( CBaseEntity *pEntity, bool bOnlyHateOrFearIfNPC = false );
virtual void StartTask( const Task_t *pTask );
virtual void RunTask( const Task_t *pTask );
virtual int RangeAttack1Conditions( float flDot, float flDist );
virtual int SelectSchedule( void );
virtual int TranslateSchedule( int scheduleType );
virtual void PrescheduleThink( void );
virtual bool FCanCheckAttacks ( void );
virtual int Restore( IRestore &restore );
virtual void OnScheduleChange( void );
virtual bool FVisible( CBaseEntity *pEntity, int traceMask = MASK_BLOCKLOS, CBaseEntity **ppBlocker = NULL );
virtual bool WeaponLOSCondition( const Vector &ownerPos, const Vector &targetPos, bool bSetConditions) { return true; }
virtual int GetSoundInterests( void ) { return (SOUND_PLAYER|SOUND_COMBAT|SOUND_DANGER); }
virtual bool ShouldNotDistanceCull( void ) { return true; }
virtual void UpdateEfficiency( bool bInPVS ) { SetEfficiency( ( GetSleepState() != AISS_AWAKE ) ? AIE_DORMANT : AIE_NORMAL ); SetMoveEfficiency( AIME_NORMAL ); }
virtual const char *GetTracerType( void ) { return "HelicopterTracer"; }
private:
void ScopeGlint( void );
void AdjustShotPosition( CBaseEntity *pTarget, Vector *vecIn );
float GetRefireTime( void ) { return 0.1f; }
bool IsLaserOn( void ) { return m_pBeam != NULL; }
bool FireBullet( const Vector &vecTarget, bool bDirectShot );
Vector DesiredBodyTarget( CBaseEntity *pTarget );
Vector LeadTarget( CBaseEntity *pTarget );
Vector GetBulletOrigin( void );
static const char *pAttackSounds[];
void ClearTargetGroup( void );
float GetWaitTimePercentage( float flTime, bool fLinear );
void GetPaintAim( const Vector &vecStart, const Vector &vecGoal, float flParameter, Vector *pProgress );
bool VerifyShot( CBaseEntity *pTarget );
void SetSweepTarget( const char *pszTarget );
// Inputs
void InputEnableSniper( inputdata_t &inputdata );
void InputDisableSniper( inputdata_t &inputdata );
void LaserOff( void );
void LaserOn( const Vector &vecTarget, const Vector &vecDeviance );
void PaintTarget( const Vector &vecTarget, float flPaintTime );
private:
void CreateLaser( void );
void CreateAncillaryBeams( void );
void UpdateAncillaryBeams( float flConvergencePerc, const Vector &vecOrigin, const Vector &vecBasis );
int m_iAmmoType;
float m_flBarrageDuration;
Vector m_vecPaintCursor;
float m_flPaintTime;
CHandle<CBeam> m_pBeam;
CHandle<CBeam> m_pAncillaryBeams[NUM_ANCILLARY_BEAMS];
EHANDLE m_hBarrageTarget;
bool m_fEnabled;
Vector m_vecPaintStart; // used to track where a sweep starts for the purpose of interpolating.
float m_flTimeLastAttackedPlayer;
float m_flTimeLastShotMissed;
float m_flSightDist;
DEFINE_CUSTOM_AI;
DECLARE_DATADESC();
};
LINK_ENTITY_TO_CLASS( npc_combine_cannon, CNPC_Combine_Cannon );
//=========================================================
//=========================================================
BEGIN_DATADESC( CNPC_Combine_Cannon )
DEFINE_FIELD( m_fEnabled, FIELD_BOOLEAN ),
DEFINE_FIELD( m_vecPaintStart, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( m_flPaintTime, FIELD_TIME ),
DEFINE_FIELD( m_vecPaintCursor, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( m_pBeam, FIELD_EHANDLE ),
DEFINE_FIELD( m_flTimeLastAttackedPlayer, FIELD_TIME ),
DEFINE_FIELD( m_flTimeLastShotMissed, FIELD_TIME ),
DEFINE_FIELD( m_iAmmoType, FIELD_INTEGER ),
DEFINE_FIELD( m_flBarrageDuration, FIELD_TIME ),
DEFINE_FIELD( m_hBarrageTarget, FIELD_EHANDLE ),
DEFINE_ARRAY( m_pAncillaryBeams, FIELD_EHANDLE, NUM_ANCILLARY_BEAMS ),
DEFINE_KEYFIELD( m_flSightDist, FIELD_FLOAT, "sightdist" ),
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "EnableSniper", InputEnableSniper ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisableSniper", InputDisableSniper ),
END_DATADESC()
//=========================================================
// Private conditions
//=========================================================
enum Sniper_Conds
{
COND_CANNON_ENABLED = LAST_SHARED_CONDITION,
COND_CANNON_DISABLED,
COND_CANNON_NO_SHOT,
};
//=========================================================
// schedules
//=========================================================
enum
{
SCHED_CANNON_CAMP = LAST_SHARED_SCHEDULE,
SCHED_CANNON_ATTACK,
SCHED_CANNON_DISABLEDWAIT,
SCHED_CANNON_SNAPATTACK,
};
//=========================================================
// tasks
//=========================================================
enum
{
TASK_CANNON_PAINT_ENEMY = LAST_SHARED_TASK,
TASK_CANNON_PAINT_DECOY,
TASK_CANNON_ATTACK_CURSOR,
};
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CNPC_Combine_Cannon::CNPC_Combine_Cannon( void ) :
m_pBeam( NULL ),
m_hBarrageTarget( NULL )
{
#ifdef _DEBUG
m_vecPaintCursor.Init();
m_vecPaintStart.Init();
#endif
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CNPC_Combine_Cannon::QuerySeeEntity( CBaseEntity *pEntity, bool bOnlyHateOrFearIfNPC )
{
Disposition_t disp = IRelationType(pEntity);
if ( disp != D_HT )
{
// 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;
}
//-----------------------------------------------------------------------------
// Purpose: Hide the beams
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::LaserOff( void )
{
if ( m_pBeam != NULL )
{
m_pBeam->TurnOn();
}
for ( int i = 0; i < NUM_ANCILLARY_BEAMS; i++ )
{
if ( m_pAncillaryBeams[i] == NULL )
continue;
m_pAncillaryBeams[i]->TurnOn();
}
SetNextThink( gpGlobals->curtime + 0.1f );
}
//-----------------------------------------------------------------------------
// Purpose: Switch on the laser and point it at a direction
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::LaserOn( const Vector &vecTarget, const Vector &vecDeviance )
{
if ( m_pBeam != NULL )
{
m_pBeam->TurnOff();
// 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 );
m_pBeam->SetStartPos( GetBulletOrigin() );
m_pBeam->SetEndPos( vecInitialAim );
m_vecPaintStart = vecInitialAim;
}
for ( int i = 0; i < NUM_ANCILLARY_BEAMS; i++ )
{
if ( m_pAncillaryBeams[i] == NULL )
continue;
m_pAncillaryBeams[i]->TurnOff();
}
}
//-----------------------------------------------------------------------------
// Crikey!
//-----------------------------------------------------------------------------
float CNPC_Combine_Cannon::GetWaitTimePercentage( 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 CNPC_Combine_Cannon::GetPaintAim( const Vector &vecStart, const Vector &vecGoal, float flParameter, Vector *pProgress )
{
// 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 = m_vecPaintStart - 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;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::CreateLaser( void )
{
if ( m_pBeam != NULL )
return;
m_pBeam = CBeam::BeamCreate( g_pModelNameLaser, 2.0f );
m_pBeam->SetColor( 0, 100, 255 );
m_pBeam->PointsInit( vec3_origin, GetBulletOrigin() );
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( gHaloTexture );
m_pBeam->SetHaloScale( 16.0f );
// Think faster while painting
SetNextThink( gpGlobals->curtime + 0.02f );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::CreateAncillaryBeams( void )
{
for ( int i = 0; i < NUM_ANCILLARY_BEAMS; i++ )
{
if ( m_pAncillaryBeams[i] != NULL )
continue;
m_pAncillaryBeams[i] = CBeam::BeamCreate( g_pModelNameLaser, 2.0f );
m_pAncillaryBeams[i]->SetColor( 0, 100, 255 );
m_pAncillaryBeams[i]->PointsInit( vec3_origin, GetBulletOrigin() );
m_pAncillaryBeams[i]->SetBrightness( 255 );
m_pAncillaryBeams[i]->SetNoise( 0 );
m_pAncillaryBeams[i]->SetWidth( 1.0f );
m_pAncillaryBeams[i]->SetEndWidth( 0 );
m_pAncillaryBeams[i]->SetScrollRate( 0 );
m_pAncillaryBeams[i]->SetFadeLength( 0 );
m_pAncillaryBeams[i]->SetHaloTexture( gHaloTexture );
m_pAncillaryBeams[i]->SetHaloScale( 16.0f );
m_pAncillaryBeams[i]->TurnOff();
}
}
#define LINE_LENGTH 1600.0f
//-----------------------------------------------------------------------------
// Purpose:
// Input : flConvergencePerc -
// vecBasis -
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::UpdateAncillaryBeams( float flConvergencePerc, const Vector &vecOrigin, const Vector &vecBasis )
{
// Multiple beams deviate from the basis direction by a certain number of degrees and "converge"
// at the basis vector over a duration of time, the position in that duration expressed by
// flConvergencePerc. The beams are most deviated at 0 and fully converged at 1.
float flRotationOffset = (2*M_PI)/(float)NUM_ANCILLARY_BEAMS; // Degrees separating each beam, in radians
float flDeviation = DEG2RAD(90) * ( 1.0f - flConvergencePerc );
float flOffset;
Vector vecFinal;
Vector vecOffset;
matrix3x4_t matRotate;
QAngle vecAngles;
VectorAngles( vecBasis, vecAngles );
vecAngles[PITCH] += 90.0f;
AngleMatrix( vecAngles, vecOrigin, matRotate );
trace_t tr;
float flScale = LINE_LENGTH * flDeviation;
// For each beam, find its offset and trace outwards to place its endpoint
for ( int i = 0; i < NUM_ANCILLARY_BEAMS; i++ )
{
if ( flConvergencePerc >= 0.99f )
{
m_pAncillaryBeams[i]->TurnOn();
continue;
}
m_pAncillaryBeams[i]->TurnOff();
// Find the number of radians offset we are
flOffset = (float) i * flRotationOffset + DEG2RAD( 30.0f );
flOffset += (M_PI/8.0f) * sin( gpGlobals->curtime * 3.0f );
// Construct a circle that's also offset by the line's length
vecOffset.x = cos( flOffset ) * flScale;
vecOffset.y = sin( flOffset ) * flScale;
vecOffset.z = LINE_LENGTH;
// Rotate this whole thing into the space of the basis vector
VectorRotate( vecOffset, matRotate, vecFinal );
VectorNormalize( vecFinal );
// Trace a line down that vector to find where we'll eventually stop our line
UTIL_TraceLine( vecOrigin, vecOrigin + ( vecFinal * LINE_LENGTH ), MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
// Move the beam to that position
m_pAncillaryBeams[i]->SetBrightness( 255.0f * flConvergencePerc );
m_pAncillaryBeams[i]->SetEndPos( tr.startpos );
m_pAncillaryBeams[i]->SetStartPos( tr.endpos );
}
}
//-----------------------------------------------------------------------------
// Sweep the laser sight towards the point where the gun should be aimed
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::PaintTarget( const Vector &vecTarget, float flPaintTime )
{
// vecStart is the barrel of the gun (or the laser sight)
Vector vecStart = GetBulletOrigin();
// keep painttime from hitting 0 exactly.
flPaintTime = MAX( flPaintTime, 0.000001f );
// Find out where we are in the arc of the paint duration
float flPaintPerc = GetWaitTimePercentage( flPaintTime, false );
ScopeGlint();
// Find out where along our line we're painting
Vector vecCurrentDir;
float flInterp = RemapValClamped( flPaintPerc, 0.0f, 0.5f, 0.0f, 1.0f );
flInterp = clamp( flInterp, 0.0f, 1.0f );
GetPaintAim( m_vecPaintStart, vecTarget, flInterp, &vecCurrentDir );
#define THRESHOLD 0.9f
float flNoiseScale;
if ( flPaintPerc >= THRESHOLD )
{
flNoiseScale = 1 - (1 / (1 - THRESHOLD)) * ( flPaintPerc - THRESHOLD );
}
else if ( flPaintPerc <= 1 - THRESHOLD )
{
flNoiseScale = flPaintPerc / (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 );
// Find where our center is
trace_t tr;
UTIL_TraceLine( vecStart, vecStart + vecCurrentDir * 8192, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
m_vecPaintCursor = tr.endpos;
// Update our beam position
m_pBeam->SetEndPos( tr.startpos );
m_pBeam->SetStartPos( tr.endpos );
m_pBeam->SetBrightness( 255.0f * flPaintPerc );
m_pBeam->RelinkBeam();
// Find points around that center point and make our designators converge at that point over time
UpdateAncillaryBeams( flPaintPerc, vecStart, vecCurrentDir );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::OnScheduleChange( void )
{
LaserOff();
m_hBarrageTarget = NULL;
BaseClass::OnScheduleChange();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::Precache( void )
{
PrecacheModel("models/combine_soldier.mdl");
PrecacheModel("effects/bluelaser1.vmt");
gHaloTexture = PrecacheModel("sprites/light_glow03.vmt");
PrecacheScriptSound( "NPC_Combine_Cannon.FireBullet" );
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::Spawn( void )
{
Precache();
/// HACK:
SetModel( "models/combine_soldier.mdl" );
// Setup our ancillary beams but keep them hidden for now
CreateLaser();
CreateAncillaryBeams();
m_iAmmoType = GetAmmoDef()->Index( "CombineHeavyCannon" );
SetHullType( HULL_HUMAN );
SetHullSizeNormal();
UTIL_SetSize( this, Vector( -16, -16 , 0 ), Vector( 16, 16, 64 ) );
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetMoveType( MOVETYPE_FLY );
m_bloodColor = DONT_BLEED;
m_iHealth = 10;
m_flFieldOfView = DOT_45DEGREE;
m_NPCState = NPC_STATE_NONE;
if( HasSpawnFlags( SF_STARTDISABLED ) )
{
m_fEnabled = false;
}
else
{
m_fEnabled = true;
}
CapabilitiesClear();
CapabilitiesAdd( bits_CAP_INNATE_RANGE_ATTACK1 | bits_CAP_SIMPLE_RADIUS_DAMAGE );
m_HackedGunPos = Vector ( 0, 0, 0 );
AddSpawnFlags( SF_NPC_LONG_RANGE | SF_NPC_ALWAYSTHINK );
NPCInit();
// Limit our look distance
SetDistLook( m_flSightDist );
AddEffects( EF_NODRAW );
AddSolidFlags( FSOLID_NOT_SOLID );
// Point the cursor straight ahead so that the sniper's
// first sweep of the laser doesn't look weird.
Vector vecForward;
AngleVectors( GetLocalAngles(), &vecForward );
m_vecPaintCursor = GetBulletOrigin() + vecForward * 1024;
// none!
GetEnemies()->SetFreeKnowledgeDuration( 0.0f );
GetEnemies()->SetEnemyDiscardTime( 2.0f );
m_flTimeLastAttackedPlayer = 0.0f;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Class_T CNPC_Combine_Cannon::Classify( void )
{
if ( m_fEnabled )
return CLASS_COMBINE;
return CLASS_NONE;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Vector CNPC_Combine_Cannon::GetBulletOrigin( void )
{
return GetAbsOrigin();
}
//-----------------------------------------------------------------------------
// Purpose: Nothing kills the cannon but entity I/O
//-----------------------------------------------------------------------------
int CNPC_Combine_Cannon::OnTakeDamage_Alive( const CTakeDamageInfo &info )
{
// We are invulnerable to normal attacks for the moment
return 0;
}
//---------------------------------------------------------
// Purpose:
//---------------------------------------------------------
void CNPC_Combine_Cannon::UpdateOnRemove( void )
{
// Remove the main laser
if ( m_pBeam != NULL )
{
UTIL_Remove( m_pBeam);
m_pBeam = NULL;
}
// Remove our ancillary beams
for ( int i = 0; i < NUM_ANCILLARY_BEAMS; i++ )
{
if ( m_pAncillaryBeams[i] == NULL )
continue;
UTIL_Remove( m_pAncillaryBeams[i] );
m_pAncillaryBeams[i] = NULL;
}
BaseClass::UpdateOnRemove();
}
//---------------------------------------------------------
// Purpose:
//---------------------------------------------------------
int CNPC_Combine_Cannon::SelectSchedule ( void )
{
// Fire at our target
if( GetEnemy() && HasCondition( COND_CAN_RANGE_ATTACK1 ) )
return SCHED_RANGE_ATTACK1;
// Wait for a target
// TODO: Sweep like a sniper?
return SCHED_COMBAT_STAND;
}
//---------------------------------------------------------
// Purpose:
//---------------------------------------------------------
bool CNPC_Combine_Cannon::FCanCheckAttacks ( void )
{
return true;
}
//---------------------------------------------------------
//---------------------------------------------------------
bool CNPC_Combine_Cannon::VerifyShot( CBaseEntity *pTarget )
{
trace_t tr;
Vector vecTarget = DesiredBodyTarget( pTarget );
UTIL_TraceLine( GetBulletOrigin(), vecTarget, MASK_SHOT, pTarget, COLLISION_GROUP_NONE, &tr );
if( tr.fraction != 1.0 )
{
if( pTarget->IsPlayer() )
{
// if the target is the player, do another trace to see if we can shoot his eyeposition. This should help
// improve sniper responsiveness in cases where the player is hiding his chest from the sniper with his
// head in full view.
UTIL_TraceLine( GetBulletOrigin(), pTarget->EyePosition(), MASK_SHOT, pTarget, COLLISION_GROUP_NONE, &tr );
if( tr.fraction == 1.0 )
{
return true;
}
}
// Trace hit something.
if( tr.m_pEnt )
{
if( tr.m_pEnt->m_takedamage == DAMAGE_YES )
{
// Just shoot it if I can hurt it. Probably a breakable or glass pane.
return true;
}
}
return false;
}
else
{
return true;
}
}
//---------------------------------------------------------
//---------------------------------------------------------
int CNPC_Combine_Cannon::RangeAttack1Conditions( float flDot, float flDist )
{
if ( GetNextAttack() > gpGlobals->curtime )
return COND_NONE;
if ( HasCondition( COND_SEE_ENEMY ) && !HasCondition( COND_ENEMY_OCCLUDED ) )
{
if ( VerifyShot( GetEnemy() ) )
{
// Can see the enemy, have a clear shot to his midsection
ClearCondition( COND_CANNON_NO_SHOT );
return COND_CAN_RANGE_ATTACK1;
}
else
{
// Can see the enemy, but can't take a shot at his midsection
SetCondition( COND_CANNON_NO_SHOT );
return COND_NONE;
}
}
return COND_NONE;
}
//---------------------------------------------------------
//---------------------------------------------------------
int CNPC_Combine_Cannon::TranslateSchedule( int scheduleType )
{
switch( scheduleType )
{
case SCHED_RANGE_ATTACK1:
return SCHED_CANNON_ATTACK;
break;
}
return BaseClass::TranslateSchedule( scheduleType );
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::ScopeGlint( void )
{
CEffectData data;
data.m_vOrigin = GetAbsOrigin();
data.m_vNormal = vec3_origin;
data.m_vAngles = vec3_angle;
data.m_nColor = COMMAND_POINT_BLUE;
DispatchEffect( "CommandPointer", data );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *vecIn -
//-----------------------------------------------------------------------------
void CNPC_Combine_Cannon::AdjustShotPosition( CBaseEntity *pTarget, Vector *vecIn )
{
if ( pTarget == NULL || vecIn == NULL )
return;
Vector low = pTarget->WorldSpaceCenter() - ( pTarget->WorldSpaceCenter() - pTarget->GetAbsOrigin() ) * .25;
Vector high = pTarget->EyePosition();
Vector delta = high - low;
Vector result = low + delta * 0.5;
// Only take the height
(*vecIn)[2] = result[2];
}
//---------------------------------------------------------
// This starts the bullet state machine. The actual effects
// of the bullet will happen later. This function schedules
// those effects.
//
// fDirectShot indicates whether the bullet is a "direct shot"
// that is - fired with the intent that it will strike the
// enemy. Otherwise, the bullet is intended to strike a
// decoy object or nothing at all in particular.
//---------------------------------------------------------
bool CNPC_Combine_Cannon::FireBullet( const Vector &vecTarget, bool bDirectShot )
{
Vector vecBulletOrigin = GetBulletOrigin();
Vector vecDir = ( vecTarget - vecBulletOrigin );
VectorNormalize( vecDir );
FireBulletsInfo_t info;
info.m_iShots = 1;
info.m_iTracerFreq = 1.0f;
info.m_vecDirShooting = vecDir;
info.m_vecSrc = vecBulletOrigin;
info.m_flDistance = MAX_TRACE_LENGTH;
info.m_pAttacker = this;
info.m_iAmmoType = m_iAmmoType;
info.m_iPlayerDamage = 20.0f;
info.m_vecSpread = Vector( 0.015f, 0.015f, 0.015f ); // medium cone
FireBullets( info );
EmitSound( "NPC_Combine_Cannon.FireBullet" );
// Don't attack for a certain amount of time
SetNextAttack( gpGlobals->curtime + GetRefireTime() );
// Sniper had to be aiming here to fire here, so make it the cursor
m_vecPaintCursor = vecTarget;
LaserOff();
return true;
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::StartTask( const Task_t *pTask )
{
switch( pTask->iTask )
{
case TASK_CANNON_ATTACK_CURSOR:
break;
case TASK_RANGE_ATTACK1:
// Setup the information for this barrage
m_flBarrageDuration = gpGlobals->curtime + random->RandomFloat( 0.25f, 0.5f );
m_hBarrageTarget = GetEnemy();
break;
case TASK_CANNON_PAINT_ENEMY:
{
if ( GetEnemy()->IsPlayer() )
{
float delay = random->RandomFloat( 0.0f, 0.3f );
if ( ( gpGlobals->curtime - m_flTimeLastAttackedPlayer ) < 1.0f )
{
SetWait( CANNON_SUBSEQUENT_PAINT_TIME );
m_flPaintTime = CANNON_SUBSEQUENT_PAINT_TIME;
}
else
{
SetWait( CANNON_PAINT_ENEMY_TIME + delay );
m_flPaintTime = CANNON_PAINT_ENEMY_TIME + delay;
}
}
else
{
// Use a random time
m_flPaintTime = CANNON_PAINT_ENEMY_TIME + random->RandomFloat( 0, CANNON_PAINT_NPC_TIME_NOISE );
SetWait( m_flPaintTime );
}
// Try to start the laser where the player can't miss seeing it!
Vector vecCursor;
AngleVectors( GetEnemy()->GetLocalAngles(), &vecCursor );
vecCursor *= 300;
vecCursor += GetEnemy()->EyePosition();
LaserOn( vecCursor, Vector( 16, 16, 16 ) );
}
break;
default:
BaseClass::StartTask( pTask );
break;
}
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::RunTask( const Task_t *pTask )
{
switch( pTask->iTask )
{
case TASK_CANNON_ATTACK_CURSOR:
if( FireBullet( m_vecPaintCursor, true ) )
{
TaskComplete();
}
break;
case TASK_RANGE_ATTACK1:
{
// Where we're focusing our fire
Vector vecTarget = ( m_hBarrageTarget == NULL ) ? m_vecPaintCursor : LeadTarget( m_hBarrageTarget );
// Fire at enemy
if ( FireBullet( vecTarget, true ) )
{
bool bPlayerIsEnemy = ( m_hBarrageTarget && m_hBarrageTarget->IsPlayer() );
bool bBarrageFinished = m_flBarrageDuration < gpGlobals->curtime;
bool bNoShot = ( QuerySeeEntity( m_hBarrageTarget ) == false ); // FIXME: Store this info off better
bool bSeePlayer = HasCondition( COND_SEE_PLAYER );
// Treat the player differently to normal NPCs
if ( bPlayerIsEnemy )
{
// Store the last time we shot for doing an abbreviated attack telegraph
m_flTimeLastAttackedPlayer = gpGlobals->curtime;
// If we've got no shot and we're done with our current barrage
if ( bNoShot && bBarrageFinished )
{
TaskComplete();
}
}
else if ( bBarrageFinished || bSeePlayer )
{
// Done with the barrage or we saw the player as a better target
TaskComplete();
}
}
}
break;
case TASK_CANNON_PAINT_ENEMY:
{
// See if we're done painting our target
if ( IsWaitFinished() )
{
TaskComplete();
}
// Continue to paint the target
PaintTarget( LeadTarget( GetEnemy() ), m_flPaintTime );
}
break;
default:
BaseClass::RunTask( pTask );
break;
}
}
//-----------------------------------------------------------------------------
// The sniper throws away the circular list of old decoys when we restore.
//-----------------------------------------------------------------------------
int CNPC_Combine_Cannon::Restore( IRestore &restore )
{
return BaseClass::Restore( restore );
}
//-----------------------------------------------------------------------------
// Purpose:
//
//
//-----------------------------------------------------------------------------
float CNPC_Combine_Cannon::MaxYawSpeed( void )
{
return 60;
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::PrescheduleThink( void )
{
BaseClass::PrescheduleThink();
// NOTE: We'll deal with this on the client
// Think faster if the beam is on, this gives the beam higher resolution.
if( m_pBeam )
{
SetNextThink( gpGlobals->curtime + 0.03 );
}
else
{
SetNextThink( gpGlobals->curtime + 0.1f );
}
// If the enemy has just stepped into view, or we've acquired a new enemy,
// Record the last time we've seen the enemy as right now.
//
// If the enemy has been out of sight for a full second, mark him eluded.
if( GetEnemy() != NULL )
{
if( gpGlobals->curtime - GetEnemies()->LastTimeSeen( GetEnemy() ) > 30 )
{
// Stop pestering enemies after 30 seconds of frustration.
GetEnemies()->ClearMemory( GetEnemy() );
SetEnemy(NULL);
}
}
}
//---------------------------------------------------------
//---------------------------------------------------------
Vector CNPC_Combine_Cannon::EyePosition( void )
{
return GetAbsOrigin();
}
//---------------------------------------------------------
//---------------------------------------------------------
Vector CNPC_Combine_Cannon::DesiredBodyTarget( CBaseEntity *pTarget )
{
// By default, aim for the center
Vector vecTarget = pTarget->WorldSpaceCenter();
float flTimeSinceLastMiss = gpGlobals->curtime - m_flTimeLastShotMissed;
if( pTarget->GetFlags() & FL_CLIENT )
{
if( !BaseClass::FVisible( vecTarget ) )
{
// go to the player's eyes if his center is concealed.
// Bump up an inch so the player's not looking straight down a beam.
vecTarget = pTarget->EyePosition() + Vector( 0, 0, 1 );
}
}
else
{
if( pTarget->Classify() == CLASS_HEADCRAB )
{
// Headcrabs are tiny inside their boxes.
vecTarget = pTarget->GetAbsOrigin();
vecTarget.z += 4.0;
}
else if( pTarget->Classify() == CLASS_ZOMBIE )
{
if( flTimeSinceLastMiss > 0.0f && flTimeSinceLastMiss < 4.0f && hl2_episodic.GetBool() )
{
vecTarget = pTarget->BodyTarget( GetBulletOrigin(), false );
}
else
{
// Shoot zombies in the headcrab
vecTarget = pTarget->HeadTarget( GetBulletOrigin() );
}
}
else if( pTarget->Classify() == CLASS_ANTLION )
{
// Shoot about a few inches above the origin. This makes it easy to hit antlions
// even if they are on their backs.
vecTarget = pTarget->GetAbsOrigin();
vecTarget.z += 18.0f;
}
else if( pTarget->Classify() == CLASS_EARTH_FAUNA )
{
// Shoot birds in the center
}
else
{
// Shoot NPCs in the chest
vecTarget.z += 8.0f;
}
}
return vecTarget;
}
//---------------------------------------------------------
//---------------------------------------------------------
Vector CNPC_Combine_Cannon::LeadTarget( CBaseEntity *pTarget )
{
if ( pTarget != NULL )
{
Vector vecFuturePos;
UTIL_PredictedPosition( pTarget, 0.05f, &vecFuturePos );
AdjustShotPosition( pTarget, &vecFuturePos );
return vecFuturePos;
}
return vec3_origin;
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::InputEnableSniper( inputdata_t &inputdata )
{
ClearCondition( COND_CANNON_DISABLED );
SetCondition( COND_CANNON_ENABLED );
m_fEnabled = true;
}
//---------------------------------------------------------
//---------------------------------------------------------
void CNPC_Combine_Cannon::InputDisableSniper( inputdata_t &inputdata )
{
ClearCondition( COND_CANNON_ENABLED );
SetCondition( COND_CANNON_DISABLED );
m_fEnabled = false;
}
//---------------------------------------------------------
// See all NPC's easily.
//
// Only see the player if you can trace to both of his
// eyeballs. That is, allow the player to peek around corners.
// This is a little more expensive than the base class' check!
//---------------------------------------------------------
#define CANNON_EYE_DIST 0.75
#define CANNON_TARGET_VERTICAL_OFFSET Vector( 0, 0, 5 );
bool CNPC_Combine_Cannon::FVisible( CBaseEntity *pEntity, int traceMask, CBaseEntity **ppBlocker )
{
// NPC
if ( pEntity->IsPlayer() == false )
return BaseClass::FVisible( pEntity, traceMask, ppBlocker );
if ( pEntity->GetFlags() & FL_NOTARGET )
return false;
Vector vecVerticalOffset;
Vector vecRight;
Vector vecEye;
trace_t tr;
if( fabs( GetAbsOrigin().z - pEntity->WorldSpaceCenter().z ) <= 120.f )
{
// If the player is around the same elevation, look straight at his eyes.
// At the same elevation, the vertical peeking allowance makes it too easy
// for a player to dispatch the sniper from cover.
vecVerticalOffset = vec3_origin;
}
else
{
// Otherwise, look at a spot below his eyes. This allows the player to back away
// from his cover a bit and have a peek at the sniper without being detected.
vecVerticalOffset = CANNON_TARGET_VERTICAL_OFFSET;
}
AngleVectors( pEntity->GetLocalAngles(), NULL, &vecRight, NULL );
vecEye = vecRight * CANNON_EYE_DIST - vecVerticalOffset;
UTIL_TraceLine( EyePosition(), pEntity->EyePosition() + vecEye, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
#if 0
NDebugOverlay::Line(EyePosition(), tr.endpos, 0,255,0, true, 0.1);
#endif
bool fCheckFailed = false;
if ( tr.fraction != 1.0 && tr.m_pEnt != pEntity )
{
fCheckFailed = true;
}
// Don't check the other eye if the first eye failed.
if( !fCheckFailed )
{
vecEye = -vecRight * CANNON_EYE_DIST - vecVerticalOffset;
UTIL_TraceLine( EyePosition(), pEntity->EyePosition() + vecEye, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
#if 0
NDebugOverlay::Line(EyePosition(), tr.endpos, 0,255,0, true, 0.1);
#endif
if ( tr.fraction != 1.0 && tr.m_pEnt != pEntity )
{
fCheckFailed = true;
}
}
if( !fCheckFailed )
{
// Can see the player.
return true;
}
// Now, if the check failed, see if the player is ducking and has recently
// fired a muzzleflash. If yes, see if you'd be able to see the player if
// they were standing in their current position instead of ducking. Since
// the sniper doesn't have a clear shot in this situation, he will harrass
// near the player.
CBasePlayer *pPlayer;
pPlayer = ToBasePlayer( pEntity );
if( (pPlayer->GetFlags() & FL_DUCKING) && pPlayer->MuzzleFlashTime() > gpGlobals->curtime )
{
vecEye = pPlayer->EyePosition() + Vector( 0, 0, 32 );
UTIL_TraceLine( EyePosition(), vecEye, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
if( tr.fraction != 1.0 )
{
// Everything failed.
if (ppBlocker)
{
*ppBlocker = tr.m_pEnt;
}
return false;
}
else
{
// Fake being able to see the player.
return true;
}
}
if (ppBlocker)
{
*ppBlocker = tr.m_pEnt;
}
return false;
}
//-----------------------------------------------------------------------------
//
// Schedules
//
//-----------------------------------------------------------------------------
AI_BEGIN_CUSTOM_NPC( npc_combine_cannon, CNPC_Combine_Cannon )
DECLARE_CONDITION( COND_CANNON_ENABLED );
DECLARE_CONDITION( COND_CANNON_DISABLED );
DECLARE_CONDITION( COND_CANNON_NO_SHOT );
DECLARE_TASK( TASK_CANNON_PAINT_ENEMY );
DECLARE_TASK( TASK_CANNON_ATTACK_CURSOR );
//=========================================================
// CAMP
//=========================================================
DEFINE_SCHEDULE
(
SCHED_CANNON_CAMP,
" Tasks"
" TASK_WAIT 1"
" "
" Interrupts"
" COND_NEW_ENEMY"
" COND_ENEMY_DEAD"
" COND_CAN_RANGE_ATTACK1"
" COND_HEAR_DANGER"
" COND_CANNON_DISABLED"
)
//=========================================================
// ATTACK
//=========================================================
DEFINE_SCHEDULE
(
SCHED_CANNON_ATTACK,
" Tasks"
" TASK_CANNON_PAINT_ENEMY 0"
" TASK_RANGE_ATTACK1 0"
" "
" Interrupts"
" COND_HEAR_DANGER"
" COND_CANNON_DISABLED"
)
//=========================================================
// ATTACK
//=========================================================
DEFINE_SCHEDULE
(
SCHED_CANNON_SNAPATTACK,
" Tasks"
" TASK_CANNON_ATTACK_CURSOR 0"
" "
" Interrupts"
" COND_ENEMY_OCCLUDED"
" COND_ENEMY_DEAD"
" COND_NEW_ENEMY"
" COND_HEAR_DANGER"
" COND_CANNON_DISABLED"
)
//=========================================================
// Sniper is allowed to process a couple conditions while
// disabled, but mostly he waits until he's enabled.
//=========================================================
DEFINE_SCHEDULE
(
SCHED_CANNON_DISABLEDWAIT,
" Tasks"
" TASK_WAIT 0.5"
" "
" Interrupts"
" COND_CANNON_ENABLED"
" COND_NEW_ENEMY"
" COND_ENEMY_DEAD"
)
AI_END_CUSTOM_NPC()
|