-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentryMK2.as
2407 lines (2031 loc) · 69.7 KB
/
sentryMK2.as
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
/** Sentry Mk2
*
* by Giegue
*
* "Mk2" is just a fancy name. This a special sentry that allows players
* to attach their weapons to the turret, your very own "Do It Yourself" sentry.
*
* Ever wondered a shotgun sentry? A minigun sentry? A barnacle sentry?
* Heh. Time to build.
*
* Inspired by Outerbeast's deployable sentries.
* Thanks to Solokiller for the OP4 barnacle grapple code.
*
* Add this to your map cfg: "map_script sentry2" to start.
* Then just spawn "monster_sentry_mk2" in your map.
*
* If you have cheat access, creating allied sentries will allow players
* to attach their weapons to the sentry.
* > create monster_sentry_mk2 1
*
* You can also skip the "1" to spawn enemy sentries, which will create
* sentries with random weapons.
*/
// Sentry spawnflags
const int SF_SENTRY_STARTDAMAGE = ( 1 << 5 ); // If sentry is inactive, auto-start if it takes damage
const int SF_SENTRY_STARTOFF = ( 1 << 6 ); // Do not turn on automatically after spawn
const int SF_SENTRY_CANDISARM = ( 1 << 7 ); // Allow dismantling of enemy turrets
const int SF_SENTRY_IGNORE_LOS = ( 1 << 8 ); // Ignore Line of Sight - Always fire
// Sentry Animations
enum e_sequences
{
IDLE = 0, // idle, turret is sleeping
FIRE, // active, turret is attacking
SPIN, // idle, turret is awake but without a target
DEPLOY, // activating
RETIRE, // deactivating
DIE, // dead.bsp
SPIN_UP, // minigun only, barrel is spinning up
SPIN_DOWN, // minigun only, spinning down
WEAPON_OPEN, // opening weapon slot
WEAPON_CLOSE // closing weapon slot
};
// Sentry Weapons
enum e_weapons
{
W_NONE = 3,
W_CROWBAR,
W_WRENCH,
W_MEDKIT,
W_GRAPPLE,
W_GLOCK,
W_PYTHON,
W_UZI,
W_UZIAKIMBO,
W_DESERT_EAGLE,
W_MP5,
W_SHOTGUN,
W_CROSSBOW,
W_M16,
W_RPG,
W_GAUSS,
W_EGON,
W_HORNETGUN,
W_SNIPERRIFLE,
W_M249,
W_SPORELAUNCHER,
W_SHOCKRIFLE,
W_DISPLACER,
W_MINIGUN
};
// Sentry fire states
enum e_firestate
{
STATE_OFF = 0,
STATE_FIRING,
STATE_READY // for minigun, barrel is already spun up
};
// bodygroup of rpg missile
const int B_RPG = 27;
class CSentryMK2 : ScriptBaseMonsterEntity
{
float m_flStartYaw; // starting angle
float m_flPingTime; // when to emit next "ping" sound
float m_fTurnRate; // current turning speed
float m_flLastSight; // the last time the turret could see its enemy
float m_flNextFire; // when to fire next shoot
int m_iMinPitch; // how low can the turret barrel go
int m_iBaseTurnRate; // how fast can the turret turn
int m_iAttackRange; // how far can the turret see (and attack)
int m_iWeapon; // current sentry weapon
int m_iWeaponState; // misc var to keep track of various states during fire
int m_iClip; // how many bullets per burst-fire (used for M16)
Vector m_vecGoalAngles; // angles to where the turret must look at
Vector m_vecCurAngles; // current turret angles
Vector m_vecLastSight; // where the enemy was located the last time it could see it
int m_Smoke; // model index of the smoke sprite, used for death effect
int m_BodyGibs; // model index of the metal gibs, used for death effect
int m_Beam; // model index of the laser sprite, used for gauss fire effect
CBeam@ m_pBeam; // for egon: "sinus" shaped laser. for grapple: tongue effect
CBeam@ m_pNoise; // laser entity, used for egon attack
CSprite@ m_pSprite; // "cloud" sprite used when egon hits something it can hurt
CSentryTongue@ m_pTip; // "tip" of grapple tongue
// beware, if spawned from a squadmaker, the owner will be the squadmaker instead of a player
EHandle m_hPlayerOwner; // owner of this sentry (player)
// Initialize the turret
void Spawn()
{
// obligatory
Precache();
g_EntityFuncs.SetModel( self, "models/deployable_sentry.mdl" );
// gun position
self.pev.view_ofs.z = 48;
// lol
self.m_bloodColor = DONT_BLEED;
// set a default health if none is specified
if ( self.pev.health == 0 )
self.pev.health = g_EngineFuncs.CVarGetFloat( "sk_sentry_health" ) * 2;
self.pev.max_health = self.pev.health;
// monster properties
self.pev.movetype = MOVETYPE_STEP;
self.pev.gravity = 1;
self.pev.solid = SOLID_SLIDEBOX;
self.pev.takedamage = DAMAGE_AIM;
self.pev.flags |= FL_MONSTER;
// prepare a few vars for first use
m_iMinPitch = -60;
m_flStartYaw = self.pev.angles.y;
m_vecGoalAngles.x = 0;
m_iBaseTurnRate = 30;
// if no attack range defined, set default
if ( m_iAttackRange == 0 )
m_iAttackRange = 1200;
// spawned sentry is ally or enemy? setup accordingly
if ( self.IsPlayerAlly() )
{
// save player owner if it exists
if ( self.pev.owner !is null )
{
self.pev.colormap = self.pev.owner.vars.colormap; // pass player's color to sentry
m_hPlayerOwner = g_EntityFuncs.Instance( self.pev.owner );
@self.pev.owner = null; // to keep collisions going
}
// start with no weapon, setup model bodygroup
self.pev.body = m_iWeapon = W_NONE;
// stay asleep
SetThink( ThinkFunction( SleepThink ) );
self.pev.nextthink = g_Engine.time + 0.1;
// wait for +use if ally
SetUse( UseFunction( OpenUse ) );
}
else
{
// if no starting weapon, pick a random one
if ( m_iWeapon == 0 )
m_iWeapon = Math.RandomLong( W_CROWBAR, W_MINIGUN );
self.pev.body = m_iWeapon;
// turn on
if ( !self.pev.SpawnFlagBitSet( SF_SENTRY_STARTOFF ) )
{
SetThink( ThinkFunction( Deploy ) );
self.pev.nextthink = g_Engine.time + 0.3;
}
else
{
// wait until trigger
SetUse( UseFunction( TurretUse ) );
SetThink( ThinkFunction( SleepThink ) );
self.pev.nextthink = g_Engine.time + 0.1;
}
}
// reset to idle
SetSentryAnim( IDLE );
// controllers
self.SetBoneController( 0, 0 );
self.SetBoneController( 1, 0 );
// can search new targets in a full 360 angle
self.m_flFieldOfView = VIEW_FIELD_FULL;
// BBOX size
g_EntityFuncs.SetSize( self.pev, Vector( -16, -16, -1 ), Vector( 16, 16, 64 ) );
// link to world then drop to floor
g_EntityFuncs.SetOrigin( self, self.pev.origin );
g_EngineFuncs.DropToFloor( self.edict() );
// starting name
if ( string( self.m_FormattedName ).Length() == 0 )
self.m_FormattedName = "Sentry MkII";
self.StudioFrameAdvance();
// HACK - custom monsters cannot override the IsMachine() method.
// The IsMachine() BaseClass is a check by CLASSNAME!
//
// YES, CLASSNAME! WHY!?
//
// Cheat the game into thinking that this is a "machine" to prevent
// player medkit from working. Machines are supposed to be healed
// with the wrench, not with the medkit! -Giegue
g_EntityFuncs.DispatchKeyValue( self.edict(), "classname", "monster_sentry" );
}
// Precache all resources
void Precache()
{
g_Game.PrecacheModel( "models/deployable_sentry.mdl" );
m_Smoke = g_Game.PrecacheModel( "sprites/steam1.spr" );
m_Beam = g_Game.PrecacheModel( "sprites/laserbeam.spr" );
m_BodyGibs = g_Game.PrecacheModel( "models/metalplategibs_green.mdl" ); // it should be red but there is no appropiate model for it
g_Game.PrecacheOther( "sentry_tongue" );
g_SoundSystem.PrecacheSound( "turret/tu_fire1.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_ping.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_active2.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_die.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_die2.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_die3.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_deploy.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_spinup.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_spindown.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_search.wav" );
g_SoundSystem.PrecacheSound( "turret/tu_alert.wav" );
// strange, these aren't precached by default?
g_SoundSystem.PrecacheSound( "weapons/m16_3round.wav" );
g_SoundSystem.PrecacheSound( "barnacle/bcl_chew3.wav" );
}
// Classification of the monster
int Classify()
{
// player ally override
if ( self.IsPlayerAlly() )
return CLASS_PLAYER_ALLY;
// allow custom monster classifications
if ( self.m_fOverrideClass )
return self.m_iClassSelection;
// default
return CLASS_MACHINE;
}
// Process keyvalues
bool KeyValue( const string& in szKey, const string& in szValue )
{
if ( szKey == "attackrange" )
{
m_iAttackRange = atoi( szValue );
return true;
}
else if ( szKey == "weapon" )
{
m_iWeapon = atoi( szValue );
// clamp to valid values
if ( m_iWeapon != 0 )
m_iWeapon = Math.clamp( W_CROWBAR, W_MINIGUN, m_iWeapon );
return true;
}
else
return BaseClass.KeyValue( szKey, szValue );
}
// Make +use-able if ally
int ObjectCaps()
{
int oCaps = BaseClass.ObjectCaps();
if ( self.IsPlayerAlly() || self.pev.SpawnFlagBitSet( SF_SENTRY_CANDISARM ) )
oCaps |= FCAP_IMPULSE_USE;
return oCaps;
}
// Entity is being removed from world, clean up effects
void OnDestroy()
{
CleanFireEffects();
}
// The actual cleaning
void CleanFireEffects()
{
if ( m_iWeaponState == STATE_FIRING )
{
switch ( m_iWeapon )
{
case W_EGON: EgonEnd(); break;
case W_GRAPPLE: GrappleEnd(); break;
case W_M16: m_iClip = 3; break; // reload
}
}
}
// Trigger to awaken
void TurretUse( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value )
{
// Or to disarm
if ( pActivator.IsPlayer() && self.IRelationship( pActivator ) != R_AL && self.pev.SpawnFlagBitSet( SF_SENTRY_CANDISARM ) )
{
if ( self.pev.sequence == GetSequence( IDLE ) )
OpenDisarm( pActivator, pCaller, useType, value );
else
{
// Before we retrace, make sure that we are spun down.
m_flLastSight = 0;
SetThink( ThinkFunction( Retire ) );
}
}
else
{
self.pev.nextthink = g_Engine.time + 0.1;
SetThink( ThinkFunction( Deploy ) );
}
SetUse( null ); // only once
}
// Dummy think for animation purposes
void SleepThink()
{
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
}
// Open weapon slot for insertion
void OpenUse( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value )
{
// players only, and must be alive
if ( pActivator.IsPlayer() && pActivator.IsAlive() )
{
// prepare to retrieve weapon
SetUse( UseFunction( CloseUse ) );
SetThink( ThinkFunction( OpenThink ) );
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
}
}
// Open weapon slot for disarming
void OpenDisarm( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value )
{
// players only, and must be alive
if ( pActivator.IsPlayer() && pActivator.IsAlive() )
{
// retire the weapon first if not in idle
if ( self.pev.sequence != GetSequence( IDLE ) )
{
m_flLastSight = 0;
SetThink( ThinkFunction( Retire ) );
}
else
{
// prepare to retrieve weapon
SetThink( ThinkFunction( OpenDisarmThink ) );
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
}
SetUse( null );
}
}
// Why can't I make the animation work properly? :C
void OpenThink()
{
// open up
SetSentryAnim( WEAPON_OPEN );
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
}
void OpenDisarmThink()
{
// open up
SetSentryAnim( WEAPON_OPEN );
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
if ( self.m_fSequenceFinished )
SetTouch( TouchFunction( DisarmTouch ) );
}
void DisarmTouch( CBaseEntity@ pOther )
{
if ( pOther.IsPlayer() && pOther.IsAlive() )
{
CBasePlayer@ pPlayer = cast< CBasePlayer@ >( pOther );
string weaponClassname;
// give sentry weapon to player
switch ( m_iWeapon )
{
case W_CROWBAR: weaponClassname = "weapon_crowbar"; break;
case W_GLOCK: weaponClassname = "weapon_9mmhandgun"; break;
case W_PYTHON: weaponClassname = "weapon_357"; break;
case W_MP5: weaponClassname = "weapon_9mmAR"; break;
case W_CROSSBOW: weaponClassname = "weapon_crossbow"; break;
case W_SHOTGUN: weaponClassname = "weapon_shotgun"; break;
case W_RPG: weaponClassname = "weapon_rpg"; break;
case W_GAUSS: weaponClassname = "weapon_gauss"; break;
case W_EGON: weaponClassname = "weapon_egon"; break;
case W_HORNETGUN: weaponClassname = "weapon_hornetgun"; break;
case W_UZI: weaponClassname = "weapon_uzi"; break;
case W_UZIAKIMBO: weaponClassname = "weapon_uziakimbo"; break;
case W_MEDKIT: weaponClassname = "weapon_medkit"; break;
case W_WRENCH: weaponClassname = "weapon_pipewrench"; break;
case W_MINIGUN: weaponClassname = "weapon_minigun"; break;
case W_GRAPPLE: weaponClassname = "weapon_grapple"; break;
case W_SNIPERRIFLE: weaponClassname = "weapon_sniperrifle"; break;
case W_M249: weaponClassname = "weapon_m249"; break;
case W_M16: weaponClassname = "weapon_m16"; break;
case W_SPORELAUNCHER: weaponClassname = "weapon_sporelauncher"; break;
case W_DESERT_EAGLE: weaponClassname = "weapon_eagle"; break;
case W_SHOCKRIFLE: weaponClassname = "weapon_shockrifle"; break;
case W_DISPLACER: weaponClassname = "weapon_displacer"; break;
}
CBaseEntity@ pWeapon = g_EntityFuncs.Create( weaponClassname, pPlayer.pev.origin, g_vecZero, true );
pWeapon.pev.spawnflags = SF_NORESPAWN | SF_CREATEDWEAPON;
g_EntityFuncs.DispatchSpawn( pWeapon.edict() );
self.pev.body = m_iWeapon = W_NONE;
SetTouch( null );
SetSentryAnim( WEAPON_CLOSE );
self.pev.nextthink = g_Engine.time + 0.1;
SetThink( ThinkFunction( CloseDisarmThink ) );
}
}
// Check weapon
void CloseUse( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value )
{
// same old, same check
if ( pActivator.IsPlayer() && pActivator.IsAlive() )
{
// if just opened, wait for current animation to finish
if ( self.m_fSequenceFinished )
{
// get player current weapon
CBasePlayer@ pPlayer = cast< CBasePlayer@ >( pActivator );
CBasePlayerWeapon@ pWeapon = cast< CBasePlayerWeapon@ >( pPlayer.m_hActiveItem.GetEntity() );
if ( pWeapon !is null )
{
int newWeapon = W_NONE;
// only these weapons are valid
switch ( pWeapon.m_iId )
{
case WEAPON_CROWBAR: newWeapon = W_CROWBAR; break;
case WEAPON_GLOCK: newWeapon = W_GLOCK; break;
case WEAPON_PYTHON: newWeapon = W_PYTHON; break;
case WEAPON_MP5: newWeapon = W_MP5; break;
case WEAPON_CROSSBOW: newWeapon = W_CROSSBOW; break;
case WEAPON_SHOTGUN: newWeapon = W_SHOTGUN; break;
case WEAPON_RPG: newWeapon = W_RPG; break;
case WEAPON_GAUSS: newWeapon = W_GAUSS; break;
case WEAPON_EGON: newWeapon = W_EGON; break;
case WEAPON_HORNETGUN: newWeapon = W_HORNETGUN; break;
case WEAPON_UZI:
{
// check player anim for single or akimbo
if ( pPlayer.get_m_szAnimExtension() == 'uzis' )
newWeapon = W_UZIAKIMBO;
else
newWeapon = W_UZI;
break;
}
case WEAPON_MEDKIT: newWeapon = W_MEDKIT; break;
case WEAPON_PIPEWRENCH: newWeapon = W_WRENCH; break;
case WEAPON_MINIGUN: newWeapon = W_MINIGUN; break;
case WEAPON_GRAPPLE: newWeapon = W_GRAPPLE; break;
case WEAPON_SNIPERRIFLE: newWeapon = W_SNIPERRIFLE; break;
case WEAPON_M249: newWeapon = W_M249; break;
case WEAPON_M16: newWeapon = W_M16; break;
case WEAPON_SPORELAUNCHER: newWeapon = W_SPORELAUNCHER; break;
case WEAPON_DESERT_EAGLE: newWeapon = W_DESERT_EAGLE; break;
case WEAPON_SHOCKRIFLE: newWeapon = W_SHOCKRIFLE; break;
case WEAPON_DISPLACER: newWeapon = W_DISPLACER; break;
}
// not supported, any custom weapon will also fall here
if ( newWeapon == W_NONE )
{
g_PlayerFuncs.ClientPrint( pPlayer, HUD_PRINTCENTER, "Deploy sentry N/A: Unsupported weapon\n" );
return;
}
// set new sentry weapon
self.pev.body = m_iWeapon = newWeapon;
if ( m_iWeapon == W_M16 )
m_iClip = 3; // starting clip
// remove the player weapon
g_EntityFuncs.Remove( pWeapon );
// close and deploy
SetSentryAnim( WEAPON_CLOSE );
self.pev.nextthink = g_Engine.time + 0.1;
SetThink( ThinkFunction( CloseThink ) );
SetUse( null ); // not usable again
}
else
{
// no weapon, no deploy
g_PlayerFuncs.ClientPrint( pPlayer, HUD_PRINTCENTER, "Deploy sentry N/A: No weapon\n" );
}
}
}
}
// Close weapon slot and prepare to deploy
void CloseThink()
{
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
// deploy after animation is complete
if ( self.m_fSequenceFinished )
{
self.pev.nextthink = g_Engine.time + 0.3;
SetThink( ThinkFunction( Deploy ) );
}
}
void CloseDisarmThink()
{
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
// dummy sentry after it's done
if ( self.m_fSequenceFinished )
SetThink( null );
}
// Deactivate the turret
void Retire()
{
// make the turret level
m_vecGoalAngles.x = 0;
m_vecGoalAngles.y = m_flStartYaw;
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
if ( !MoveTurret() )
{
if ( self.pev.sequence != GetSequence( RETIRE ) )
{
SetSentryAnim( RETIRE, true );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_BODY, "turret/tu_deploy.wav", 0.5, ATTN_NORM, 0, 120 );
}
else if ( self.m_fSequenceFinished )
{
m_flLastSight = 0;
SetSentryAnim( IDLE );
SetThink( null );
SetUse( UseFunction( TurretUse ) );
}
}
else
{
SetSentryAnim( SPIN );
}
}
// Put the sentry awake
void Deploy()
{
self.pev.nextthink = g_Engine.time + 0.1;
self.StudioFrameAdvance();
// check here to play the sound only once
if ( self.pev.sequence != GetSequence( DEPLOY ) )
{
SetSentryAnim( DEPLOY );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_BODY, "turret/tu_deploy.wav", 0.5, ATTN_NORM, 0, PITCH_NORM );
}
// when sequence finishes...
if ( self.m_fSequenceFinished )
{
m_vecCurAngles.x = 0;
m_vecCurAngles.y = Math.AngleMod( self.pev.angles.y );
SetSentryAnim( SPIN );
self.pev.framerate = 0;
SetThink( ThinkFunction( SearchThink ) );
if ( self.pev.SpawnFlagBitSet( SF_SENTRY_CANDISARM ) )
SetUse( UseFunction( OpenDisarm ) );
}
}
// Search for a new target
void SearchThink()
{
// ensure rethink
if ( m_iWeapon == W_MINIGUN && m_iWeaponState != STATE_OFF ) // firing? (or was preparing to fire)
{
// spin down
SetSentryAnim( SPIN_DOWN );
// wait until spin stops before attacking again
m_iWeaponState = STATE_OFF;
m_flNextFire = g_Engine.time + 1.0;
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "hassault/hw_spindown.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM );
}
else
{
SetSentryAnim( SPIN );
// don't spin the barrel!
if ( m_iWeapon == W_MINIGUN )
self.pev.framerate = 0;
}
self.StudioFrameAdvance();
self.pev.nextthink = g_Engine.time + 0.1;
Ping();
// If we have a target and we're still healthy
if ( self.m_hEnemy.IsValid() )
{
if ( !self.m_hEnemy.GetEntity().IsAlive() || self.m_hEnemy.GetEntity().pev.FlagBitSet( FL_NOTARGET ) )
self.m_hEnemy = null; // Dead or untargetable enemy forces a search for new one
}
// Acquire Target
if ( !self.m_hEnemy.IsValid() )
{
// If this is a healing sentry (has medkit) then only care for nearby allies
if ( m_iWeapon == W_MEDKIT )
self.m_hEnemy = BestVisibleAlly( GetRange() );
else
{
self.Look( GetRange() );
self.m_hEnemy = BestVisibleEnemy( GetRange() );
}
}
// If we've found a target, start to attack
if ( self.m_hEnemy.IsValid() )
{
SetThink( ThinkFunction( ActiveThink ) );
}
else
{
// generic hunt for new victims
m_vecGoalAngles.y = ( m_vecGoalAngles.y + 0.1 * m_fTurnRate );
if ( m_vecGoalAngles.y >= 360 )
m_vecGoalAngles.y -= 360;
MoveTurret();
}
}
// Move sentry angles towards destination
bool MoveTurret()
{
bool state = false;
// any x movement?
if ( m_vecCurAngles.x != m_vecGoalAngles.x )
{
float flDir = m_vecGoalAngles.x > m_vecCurAngles.x ? 1 : -1;
m_vecCurAngles.x += 0.1 * m_fTurnRate * flDir;
// if we started below the goal, and now we're past, peg to goal
if ( flDir == 1 )
{
if ( m_vecCurAngles.x > m_vecGoalAngles.x )
m_vecCurAngles.x = m_vecGoalAngles.x;
}
else
{
if ( m_vecCurAngles.x < m_vecGoalAngles.x )
m_vecCurAngles.x = m_vecGoalAngles.x;
}
self.SetBoneController( 1, -m_vecCurAngles.x );
state = true;
}
if ( m_vecCurAngles.y != m_vecGoalAngles.y )
{
float flDir = m_vecGoalAngles.y > m_vecCurAngles.y ? 1 : -1;
float flDist = abs( m_vecGoalAngles.y - m_vecCurAngles.y );
if ( flDist > 180 )
{
flDist = 360 - flDist;
flDir = -flDir;
}
if ( flDist > 30 )
{
if ( m_fTurnRate < m_iBaseTurnRate * 10 )
{
m_fTurnRate += m_iBaseTurnRate;
}
}
else if ( m_fTurnRate > 45 )
{
m_fTurnRate -= m_iBaseTurnRate;
}
else
{
m_fTurnRate += m_iBaseTurnRate;
}
m_vecCurAngles.y += 0.1 * m_fTurnRate * flDir;
if ( m_vecCurAngles.y < 0 )
m_vecCurAngles.y += 360;
else if ( m_vecCurAngles.y >= 360 )
m_vecCurAngles.y -= 360;
if ( flDist < ( 0.05 * m_iBaseTurnRate ) )
m_vecCurAngles.y = m_vecGoalAngles.y;
self.SetBoneController( 0, m_vecCurAngles.y - self.pev.angles.y );
state = true;
}
if ( !state )
m_fTurnRate = m_iBaseTurnRate;
return state;
}
// Attacking an enemy
void ActiveThink()
{
bool fAttack = false;
Vector vecDirToEnemy;
self.pev.nextthink = g_Engine.time + 0.1;
self.pev.framerate = 1;
self.StudioFrameAdvance();
if ( !self.m_hEnemy.IsValid() )
{
// enemy no longer exists, search for a new one
self.m_hEnemy = null;
m_flLastSight = 0;
SetThink( ThinkFunction( SearchThink ) );
CleanFireEffects();
return;
}
// if it's dead (or no longer targeteable), look for something new
if ( !self.m_hEnemy.GetEntity().IsAlive() || self.m_hEnemy.GetEntity().pev.FlagBitSet( FL_NOTARGET ) )
{
if ( m_flLastSight == 0 )
{
m_flLastSight = g_Engine.time + 0.5; // continue-shooting timeout
}
else
{
if ( g_Engine.time > m_flLastSight )
{
self.m_hEnemy = null;
m_flLastSight = 0;
SetThink( ThinkFunction( SearchThink ) );
CleanFireEffects();
return;
}
}
}
Vector vecMid = self.pev.origin + self.pev.view_ofs;
Vector vecMidEnemy = self.m_hEnemy.GetEntity().BodyTarget( vecMid );
// Look for our current enemy
bool fEnemyVisible = self.pev.SpawnFlagBitSet( SF_SENTRY_IGNORE_LOS ) ? true : self.FVisible( self.m_hEnemy.GetEntity(), true );
vecDirToEnemy = vecMidEnemy - vecMid; // calculate dir and dist to enemy
float flDistToEnemy = vecDirToEnemy.Length();
Vector vec = Math.VecToAngles( vecMidEnemy - vecMid );
// Current enemy is not visible.
if ( !fEnemyVisible || ( flDistToEnemy > GetRange() ) )
{
if ( m_flLastSight == 0 )
m_flLastSight = g_Engine.time + 0.5;
else
{
// Should we look for a new target?
if ( g_Engine.time > m_flLastSight )
{
self.m_hEnemy = null;
m_flLastSight = 0;
SetThink( ThinkFunction( SearchThink ) );
CleanFireEffects();
return;
}
}
fEnemyVisible = false;
}
else
{
m_vecLastSight = vecMidEnemy;
}
Math.MakeAimVectors( m_vecCurAngles );
Vector vecLOS = vecDirToEnemy; //vecMid - m_vecLastSight;
vecLOS = vecLOS.Normalize();
float dot = DotProduct( vecLOS, g_Engine.v_forward );
// Is the Gun looking at the target
if ( m_iWeapon != W_GRAPPLE && dot <= 0.866 ) // 30 degree slop
fAttack = false;
else if ( m_iWeapon == W_GRAPPLE && dot <= 0.99 ) // for barnacle, either it's (almost) DIRECTLY aiming at the target or bust
fAttack = false;
else
fAttack = true;
// if the sentry is grappling something it will never let go
// until its target dies or the sentry goes down in the attempt
if ( m_iWeapon == W_GRAPPLE && m_pTip !is null && m_pTip.IsStuck() )
{
fAttack = true;
fEnemyVisible = true;
}
// fire the gun
if ( fAttack && g_Engine.time > m_flNextFire )
{
// minigun override
if ( m_iWeapon == W_MINIGUN )
{
if ( m_iWeaponState == STATE_OFF ) // idle
{
// spin up
SetSentryAnim( SPIN_UP );
// prepare to fire
m_iWeaponState = STATE_FIRING;
m_flNextFire = g_Engine.time + 1.0;
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "hassault/hw_spinup.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM );
return;
}
}
Vector vecSrc, vecAng;
self.GetAttachment( 0, vecSrc, vecAng );
if ( m_iWeapon > W_WRENCH )
{
// force it to fix muzzle
SetSentryAnim( FIRE, true );
}
Shoot( vecSrc, g_Engine.v_forward );
}
else
{
if ( m_iWeapon != W_MINIGUN )
{
// don't change back the animation if we have an enemy (melee)
if ( !self.m_hEnemy.IsValid() && ( m_iWeapon == W_CROWBAR || m_iWeapon == W_WRENCH ) )
{
SetSentryAnim( SPIN );
}
CleanFireEffects();
}
}
//move the gun
if ( fEnemyVisible )
{
if ( vec.y > 360 )
vec.y -= 360;
if ( vec.y < 0 )
vec.y += 360;
if ( vec.x < -180 )
vec.x += 360;
if ( vec.x > 180 )
vec.x -= 360;
// now all numbers should be in [1...360]
// pin to turret limitations to [-90...15]
if ( vec.x > 90 )
vec.x = 90;
else if ( vec.x < m_iMinPitch )
vec.x = m_iMinPitch;
m_vecGoalAngles.y = vec.y;
m_vecGoalAngles.x = vec.x;
}
MoveTurret();
}
// Make the sentry fire its weapon
void Shoot( Vector vecSrc, Vector vecDirToEnemy )
{
// TODO: ideally the firing spreads should match a real player weapon when it's
// unmoving and standing. If devs are willing to give such info, that is... -Giegue
switch ( m_iWeapon )
{
case W_GLOCK:
{
MyFireBullets( self, 1, vecSrc, vecDirToEnemy, VECTOR_CONE_1DEGREES, GetRange(), BULLET_PLAYER_9MM, 0 );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/pl_gun3.wav", Math.RandomFloat( 0.92, 1.0 ), ATTN_NORM, 0, 98 + Math.RandomLong( 0, 3 ) );
m_flNextFire = g_Engine.time + 0.3;
break;
}
case W_PYTHON:
{
MyFireBullets( self, 1, vecSrc, vecDirToEnemy, VECTOR_CONE_4DEGREES, GetRange(), BULLET_PLAYER_357, 0 );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/357_shot1.wav", Math.RandomFloat( 0.8, 0.9 ), ATTN_NORM, 0, PITCH_NORM );
m_flNextFire = g_Engine.time + 0.75;
break;
}
case W_DESERT_EAGLE:
{
MyFireBullets( self, 1, vecSrc, vecDirToEnemy, VECTOR_CONE_2DEGREES, GetRange(), BULLET_PLAYER_EAGLE, 0 );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/de_shot1.wav", Math.RandomFloat( 0.92, 1.0 ), ATTN_NORM, 0, 98 + Math.RandomLong( 0, 3 ) );
m_flNextFire = g_Engine.time + 0.6;
break;
}
case W_MP5:
{
MyFireBullets( self, 1, vecSrc, vecDirToEnemy, VECTOR_CONE_3DEGREES, GetRange(), BULLET_PLAYER_MP5, 0 );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/hks1.wav", VOL_NORM, ATTN_NORM, 0, 94 + Math.RandomLong( 0, 0xF ) );
//m_flNextFire = g_Engine.time + 0.1;
break;
}
case W_UZI:
{
MyFireBullets( self, 1, vecSrc, vecDirToEnemy, VECTOR_CONE_4DEGREES, GetRange(), BULLET_PLAYER_CUSTOMDAMAGE, int( g_EngineFuncs.CVarGetFloat( "sk_plr_uzi" ) ) );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/uzi/shoot1.wav", VOL_NORM, ATTN_NORM, 0, 94 + Math.RandomLong( 0, 0xF ) );
//m_flNextFire = g_Engine.time + 0.1;
break;
}
case W_UZIAKIMBO:
{
MyFireBullets( self, 2, vecSrc, vecDirToEnemy, VECTOR_CONE_5DEGREES, GetRange(), BULLET_PLAYER_CUSTOMDAMAGE, int( g_EngineFuncs.CVarGetFloat( "sk_plr_uzi" ) ) );
g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, "weapons/uzi/fire_both1.wav", VOL_NORM, ATTN_NORM, 0, 94 + Math.RandomLong( 0, 0xF ) );
//m_flNextFire = g_Engine.time + 0.1;
break;
}
case W_SHOTGUN:
{