forked from ReactiveDrop/reactivedrop_public_src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasw_weapon_shared.cpp
More file actions
1791 lines (1534 loc) · 49.9 KB
/
asw_weapon_shared.cpp
File metadata and controls
1791 lines (1534 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cbase.h"
#include "in_buttons.h"
#ifdef CLIENT_DLL
#define CASW_Weapon C_ASW_Weapon
#define CASW_Marine C_ASW_Marine
#define CASW_Inhabitable_NPC C_ASW_Inhabitable_NPC
#define CBasePlayer C_BasePlayer
#include "c_asw_weapon.h"
#include "c_asw_marine.h"
#include "c_asw_player.h"
#include "c_asw_marine_resource.h"
#include "c_basecombatcharacter.h"
#include "c_baseplayer.h"
#include "c_asw_fx.h"
#include "prediction.h"
#include "igameevents.h"
#define CASW_Marine_Resource C_ASW_Marine_Resource
#define CRecipientFilter C_RecipientFilter
#else
#include "asw_lag_compensation.h"
#include "asw_weapon.h"
#include "asw_marine.h"
#include "asw_marine_resource.h"
#include "asw_player.h"
#include "asw_marine_speech.h"
#include "asw_gamestats.h"
#include "ammodef.h"
#include "asw_achievements.h"
#include "asw_fx_shared.h"
#endif
#include "asw_equipment_list.h"
#include "asw_weapon_parse.h"
#include "asw_marine_skills.h"
#include "asw_weapon_ammo_bag_shared.h"
#include "asw_weapon_fire_extinguisher_shared.h"
#include "asw_weapon_healgrenade_shared.h"
#include "asw_weapon_heal_gun_shared.h"
#include "asw_weapon_medrifle_shared.h"
#include "asw_gamerules.h"
#include "asw_melee_system.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "particle_parse.h"
#include "asw_deathmatch_mode_light.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar asw_weapon_max_shooting_distance( "asw_weapon_max_shooting_distance", "1500", FCVAR_REPLICATED | FCVAR_CHEAT, "Maximum distance of the hitscan weapons." );
ConVar asw_weapon_force_scale("asw_weapon_force_scale", "1.0f", FCVAR_REPLICATED | FCVAR_CHEAT, "Force of weapon shots");
ConVar asw_fast_reload_enabled( "asw_fast_reload_enabled", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Use fast reload mechanic?" );
#ifdef GAME_DLL
ConVar rd_fast_reload_explode_chance( "rd_fast_reload_explode_chance", "0.0f", FCVAR_CHEAT, "0-1 chance for marine to explode on failed fast reload" );
ConVar rd_fast_reload_explode_damage( "rd_fast_reload_explode_damage", "50.0f", FCVAR_CHEAT, "Damage of the fast reload fail explosion" );
ConVar rd_fast_reload_explode_radius( "rd_fast_reload_explode_radius", "50.0f", FCVAR_CHEAT, "Radius of the fast reload fail explosion" );
#endif
#ifndef CLIENT_DLL
extern ConVar asw_debug_marine_damage;
extern ConVar asw_DebugAutoAim;
extern ConVar rd_bot_strong;
#endif
BEGIN_DEFINE_LOGGING_CHANNEL( LOG_ASW_Weapons, "ASWWeapons", 0, LS_MESSAGE );
ADD_LOGGING_CHANNEL_TAG( "AlienSwarm" );
ADD_LOGGING_CHANNEL_TAG( "Weapons" );
END_DEFINE_LOGGING_CHANNEL();
#ifdef ASW_VERBOSE_MESSAGES
BEGIN_DEFINE_LOGGING_CHANNEL( LOG_ASW_WeaponDeveloper, "ASWVerboseWeapons", 0, LS_ERROR );
ADD_LOGGING_CHANNEL_TAG( "AlienSwarmVerbose" );
ADD_LOGGING_CHANNEL_TAG( "Weapons" );
END_DEFINE_LOGGING_CHANNEL();
#endif // #ifdef ASW_VERBOSE_MESSAGES
BEGIN_DEFINE_LOGGING_CHANNEL( LOG_ASW_Damage, "ASWDamage", 0, LS_MESSAGE );
ADD_LOGGING_CHANNEL_TAG( "AlienSwarm" );
ADD_LOGGING_CHANNEL_TAG( "Damage" );
END_DEFINE_LOGGING_CHANNEL();
void CASW_Weapon::Spawn()
{
//InitializeAttributes();
BaseClass::Spawn();
SetModel( GetWorldModel() );
m_nSkin = GetWeaponInfo()->m_iPlayerModelSkin;
}
// get the player owner of this weapon (either the marine's commander if the weapon is
// being held by marine, or the player directly if a player is holding this weapon)
CASW_Player* CASW_Weapon::GetCommander()
{
CASW_Player *pOwner = NULL;
CBaseCombatCharacter* pCombatCharOwner = GetOwner();
if ( pCombatCharOwner && pCombatCharOwner->IsInhabitableNPC() )
{
CASW_Inhabitable_NPC *pNPC = assert_cast< CASW_Inhabitable_NPC * >( pCombatCharOwner );
pOwner = pNPC->GetCommander();
}
else
{
pOwner = ToASW_Player( ToBasePlayer( pCombatCharOwner ) );
}
return pOwner;
}
CASW_Marine *CASW_Weapon::GetMarine()
{
CBaseEntity *pOwner = GetOwner();
if ( pOwner && pOwner->Classify() == CLASS_ASW_MARINE )
return assert_cast< CASW_Marine * >( pOwner );
return NULL;
}
#if PREDICTION_ERROR_CHECK_LEVEL > 0
extern void DiffPrint( bool bServer, int nCommandNumber, char const *fmt, ... );
void CASW_Weapon::DiffPrint( char const *fmt, ... )
{
CASW_Player *pPlayer = GetCommander();
if ( !pPlayer )
return;
va_list argptr;
char string[1024];
va_start (argptr,fmt);
Q_vsnprintf(string, sizeof( string ), fmt,argptr);
va_end (argptr);
::DiffPrint( CBaseEntity::IsServer(), pPlayer->CurrentCommandNumber(), "%s", string );
}
#else
void CASW_Weapon::DiffPrint(char const *fmt, ...)
{
// nothing
}
#endif
void CASW_Weapon::ItemBusyFrame( void )
{
CBaseCombatCharacter *pOwner = GetOwner();
if ( !pOwner || !pOwner->IsInhabitableNPC() )
return;
CASW_Inhabitable_NPC *pNPC = assert_cast< CASW_Inhabitable_NPC * >( pOwner );
bool bAttack1, bAttack2, bReload, bOldReload, bOldAttack1;
GetButtons(bAttack1, bAttack2, bReload, bOldReload, bOldAttack1 );
// check for clearing our weapon switching bool
if (m_bSwitchingWeapons && gpGlobals->curtime > m_flNextPrimaryAttack)
{
m_bSwitchingWeapons = false;
}
// check for clearing our firing bool from reloading
if (m_bInReload && gpGlobals->curtime > m_fReloadClearFiringTime)
{
ClearIsFiring();
}
if ( ( bReload && !bOldReload ) && UsesClipsForAmmo1() && asw_fast_reload_enabled.GetBool() )
{
CASW_Marine *pMarine = CASW_Marine::AsMarine( pNPC );
if ( pMarine && m_bInReload )
{
// check for a fast reload
//Msg("%f Check for fast reload while busy\n", gpGlobals->curtime);
if ( gpGlobals->curtime >= m_fFastReloadStart && gpGlobals->curtime <= m_fFastReloadEnd )
{
// todo: reduce next attack time
m_fFastReloadEnd = 0;
m_fFastReloadStart = 0;
float flSucceedDelay = gpGlobals->curtime + 0.5f;
pOwner->SetNextAttack( flSucceedDelay );
m_flNextPrimaryAttack = m_flNextSecondaryAttack = flSucceedDelay;
pMarine->DoAnimationEvent( PLAYERANIMEVENT_RELOAD_SUCCEED );
DispatchParticleEffect( "fast_reload", PATTACH_POINT_FOLLOW, this, "muzzle" );
#ifdef GAME_DLL
pMarine->m_nFastReloadsInARow++;
IGameEvent *event = gameeventmanager->CreateEvent( "fast_reload" );
if ( event )
{
event->SetInt( "marine", pMarine->entindex() );
event->SetInt( "reloads", pMarine->m_nFastReloadsInARow );
gameeventmanager->FireEvent( event );
}
if ( pMarine->m_nFastReloadsInARow >= 4 && pMarine->IsInhabited() )
{
if ( pMarine->GetMarineResource() )
{
pMarine->GetMarineResource()->m_bDidFastReloadsInARow = true;
}
if ( pMarine->GetCommander() )
{
pMarine->GetCommander()->AwardAchievement( ACHIEVEMENT_ASW_FAST_RELOADS_IN_A_ROW );
}
}
#endif
CSoundParameters params;
if ( !GetParametersForSound( "FastReload.Success", params, NULL ) )
return;
EmitSound_t playparams( params );
playparams.m_nPitch = params.pitch;
CBroadcastRecipientFilter filter;
if ( IsPredicted() && CBaseEntity::GetPredictionPlayer() )
{
filter.UsePredictionRules();
}
EmitSound( filter, entindex(), playparams );
//Msg("%f RELOAD SUCCESS! - bAttack1 = %d, bOldAttack1 = %d\n", gpGlobals->curtime, bAttack1, bOldAttack1 );
//Msg( "S: %f - %f - %f RELOAD SUCCESS! -- Progress = %f\n", gpGlobals->curtime, fFastStart, fFastEnd, flProgress );
#ifdef CLIENT_DLL
if ( prediction->InPrediction() && prediction->IsFirstTimePredicted() && gpGlobals->maxClients != 1 )
#endif
{
pMarine->DoAnimationEvent( PLAYERANIMEVENT_DROP_MAGAZINE_GIB );
}
#ifdef GAME_DLL
pMarine->GetMarineSpeech()->PersonalChatter( CHATTER_SELECTION );
#endif
m_bFastReloadSuccess = true;
m_bFastReloadFailure = false;
}
else if ( m_fFastReloadStart != 0 )
{
CSoundParameters params;
if ( !GetParametersForSound( "FastReload.Miss", params, NULL ) )
return;
EmitSound_t playparams( params );
playparams.m_nPitch = params.pitch;
CBroadcastRecipientFilter filter;
if ( IsPredicted() && CBaseEntity::GetPredictionPlayer() )
{
filter.UsePredictionRules();
}
EmitSound( filter, entindex(), playparams );
//Msg("%f RELOAD MISSED! - bAttack1 = %d, bOldAttack1 = %d\n", gpGlobals->curtime, bAttack1, bOldAttack1 );
//Msg( "S: %f - %f - %f RELOAD MISSED! -- Progress = %f\n", gpGlobals->curtime, fFastStart, fFastEnd, flProgress );
m_fFastReloadEnd = 0;
m_fFastReloadStart = 0;
if ( pOwner )
{
float flMissDelay = MAX( gpGlobals->curtime + 2.0f, m_flNextPrimaryAttack + 1.0f );
pOwner->SetNextAttack( flMissDelay );
m_flNextPrimaryAttack = m_flNextSecondaryAttack = flMissDelay;
m_flReloadFailTime = m_flNextPrimaryAttack - gpGlobals->curtime;
}
pMarine->DoAnimationEvent( PLAYERANIMEVENT_RELOAD_FAIL );
#ifdef GAME_DLL
IGameEvent *event = gameeventmanager->CreateEvent( "fast_reload_fail" );
if ( event )
{
event->SetInt( "marine", pMarine->entindex() );
gameeventmanager->FireEvent( event, true );
}
pMarine->m_nFastReloadsInARow = 0;
if ( rd_fast_reload_explode_chance.GetFloat() > 0 )
{
float flStartFraction = random->RandomFloat( 0.0f, 1.0f );
if ( rd_fast_reload_explode_chance.GetFloat() > flStartFraction )
{
UTIL_ASW_GrenadeExplosion( GetAbsOrigin(), rd_fast_reload_explode_radius.GetFloat() );
EmitSound( "ASWGrenade.Explode" );
// damage to nearby things
CTakeDamageInfo info( this, pMarine, rd_fast_reload_explode_damage.GetFloat(), DMG_BLAST );
info.SetWeapon( this );
ASWGameRules()->RadiusDamage( info, GetAbsOrigin(), rd_fast_reload_explode_radius.GetFloat(), CLASS_NONE, NULL );
}
}
#endif
DispatchParticleEffect( "reload_fail", PATTACH_POINT_FOLLOW, this, "muzzle" );
#ifdef GAME_DLL
pMarine->GetMarineSpeech()->PersonalChatter( CHATTER_PAIN_SMALL );
#endif
m_bFastReloadSuccess = false;
m_bFastReloadFailure = true;
}
}
}
#ifdef CLIENT_DLL
if ( m_bInReload )
{
float fStart = m_fReloadStart;
float fNext = MAX( m_flNextPrimaryAttack, pOwner->GetNextAttack() );
float fTotalTime = fNext - fStart;
if ( fTotalTime <= 0 )
fTotalTime = 0.1f;
m_fReloadProgress = ( gpGlobals->curtime - fStart ) / fTotalTime;
}
else
{
m_fReloadProgress = 0;
}
//Msg( "S: %f Reload Progress = %f\n", gpGlobals->curtime, m_fReloadProgress );
#endif //CLIENT_DLL
}
// check player or marine commander's buttons for firing/reload/etc
void CASW_Weapon::ItemPostFrame( void )
{
CBaseCombatCharacter *pOwner = GetOwner();
if ( !pOwner || !pOwner->IsInhabitableNPC() )
return;
CASW_Inhabitable_NPC *pNPC = assert_cast< CASW_Inhabitable_NPC * >( pOwner );
bool bThisActive = ( pNPC->GetActiveWeapon() == this );
bool bAttack1, bAttack2, bReload, bOldReload, bOldAttack1;
GetButtons( bAttack1, bAttack2, bReload, bOldReload, bOldAttack1 );
CASW_Marine *pMarine = CASW_Marine::AsMarine( pNPC );
if ( pMarine && ( pMarine->IsHacking() || pMarine->GetForcedActionRequest() || pMarine->IsFrozen() ) )
{
bThisActive = bAttack1 = bAttack2 = bReload = false;
}
// check for clearing our weapon switching bool
if ( m_bSwitchingWeapons && gpGlobals->curtime > m_flNextPrimaryAttack )
{
m_bSwitchingWeapons = false;
}
// check for clearing our firing bool from reloading
if ( m_bInReload && gpGlobals->curtime > m_fReloadClearFiringTime )
{
ClearIsFiring();
}
if ( m_bShotDelayed && gpGlobals->curtime > m_flDelayedFire )
{
DelayedAttack();
}
if ( UsesClipsForAmmo1() )
{
CheckReload();
}
bool bFired = false;
if ( bThisActive )
{
//Track the duration of the fire
//FIXME: Check for IN_ATTACK2 as well?
//FIXME: What if we're calling ItemBusyFrame?
m_fFireDuration = bAttack1 ? ( m_fFireDuration + gpGlobals->frametime ) : 0.0f;
if ( bAttack2 && ( m_flNextSecondaryAttack <= gpGlobals->curtime ) && ( !pMarine || gpGlobals->curtime > pMarine->m_fFFGuardTime ) )
{
if ( SecondaryAttackUsesPrimaryAmmo() )
{
if ( !IsMeleeWeapon() &&
( ( UsesClipsForAmmo1() && !( this->PrimaryAmmoLoaded() ) ) || ( !UsesClipsForAmmo1() && pNPC->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 ) ) )
{
HandleFireOnEmpty();
}
else
{
bFired = true;
SecondaryAttack();
#ifndef CLIENT_DLL
if ( pNPC->IsInhabited() )
{
IGameEvent *event = gameeventmanager->CreateEvent( "player_alt_fire" );
if ( event )
{
CASW_Player *pPlayer = pNPC->GetCommander();
event->SetInt( "userid", ( pPlayer ? pPlayer->GetUserID() : 0 ) );
gameeventmanager->FireEvent( event );
}
}
#endif
}
}
else if ( UsesSecondaryAmmo() &&
( ( UsesClipsForAmmo2() && m_iClip2 <= 0 ) ||
( !UsesClipsForAmmo2() && pNPC->GetAmmoCount( m_iSecondaryAmmoType ) <= 0 ) ) )
{
if ( m_flNextEmptySoundTime < gpGlobals->curtime )
{
WeaponSound( EMPTY );
m_flNextSecondaryAttack = m_flNextEmptySoundTime = gpGlobals->curtime + 0.5;
}
}
else
{
bFired = true;
SecondaryAttack();
#ifndef CLIENT_DLL
if ( pNPC->IsInhabited() )
{
IGameEvent *event = gameeventmanager->CreateEvent( "player_alt_fire" );
if ( event )
{
CASW_Player *pPlayer = pNPC->GetCommander();
event->SetInt( "userid", ( pPlayer ? pPlayer->GetUserID() : 0 ) );
gameeventmanager->FireEvent( event );
}
}
#endif
// Secondary ammo doesn't have a reload animation
// this code makes secondary ammo come from nowhere!
/*
if ( UsesClipsForAmmo2() )
{
// reload clip2 if empty
if (m_iClip2 < 1)
{
pOwner->RemoveAmmo( 1, m_iSecondaryAmmoType );
m_iClip2 = m_iClip2 + 1;
}
}*/
}
}
if ( !bFired && bAttack1 && ( m_flNextPrimaryAttack <= gpGlobals->curtime ) && ( !pMarine || gpGlobals->curtime > pMarine->m_fFFGuardTime ) )
{
// Clip empty? Or out of ammo on a no-clip weapon?
if ( !IsMeleeWeapon() &&
( ( UsesClipsForAmmo1() && !( this->PrimaryAmmoLoaded() ) ) || ( !UsesClipsForAmmo1() && pNPC->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 ) ) )
{
HandleFireOnEmpty();
}
else if ( pNPC->GetWaterLevel() == 3 && m_bFiresUnderwater == false )
{
// This weapon doesn't fire underwater
WeaponSound( EMPTY );
m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
return;
}
else
{
//NOTENOTE: There is a bug with this code with regards to the way machine guns catch the leading edge trigger
// on the player hitting the attack key. It relies on the gun catching that case in the same frame.
// However, because the player can also be doing a secondary attack, the edge trigger may be missed.
// We really need to hold onto the edge trigger and only clear the condition when the gun has fired its
// first shot. Right now that's too much of an architecture change -- jdw
// If the firing button was just pressed, reset the firing time
if ( bAttack1 )
{
#ifdef CLIENT_DLL
//Msg("[Client] setting nextprimaryattack to now %f\n", gpGlobals->curtime);
#else
//Msg("[Server] setting nextprimaryattack to now %f\n", gpGlobals->curtime);
// Fire event when a player fires a weapon
IGameEvent *event = gameeventmanager->CreateEvent( "weapon_fire" );
if ( event )
{
CASW_Player *pPlayer = pNPC->GetCommander();
event->SetInt( "userid", ( pPlayer ? pPlayer->GetUserID() : 0 ) );
event->SetInt( "marine", pNPC->entindex() );
event->SetInt( "weapon", entindex() );
gameeventmanager->FireEvent( event, true );
}
#endif
m_flNextPrimaryAttack = gpGlobals->curtime;
}
PrimaryAttack();
}
}
}
if ( !bAttack1 ) // clear our firing var if we don't have the attack button held down (not totally accurate since firing could continue for some time after pulling the trigger, but it's good enough for our purposes)
{
if ( SecondaryAttackEqualsPrimary() )
{
if ( !bAttack2 )
{
/* doesnt really important now
bool bOldAttack2 = false;
if ( pOwner->IsInhabited() && pOwner->GetCommander() )
{
bOldAttack2 = !!( pOwner->m_nOldButtons & IN_ATTACK2 );
}
*/
m_bIsFiring = false;
if ( bOldAttack1 /* || bOldAttack2 */ )
{
OnStoppedFiring();
}
}
}
else
{
m_bIsFiring = false; // NOTE: Only want to clear primary fire IsFiring bool here (i.e. don't call ClearIsFiring as that'll clear secondary fire too, in subclasses that have it)
if ( bOldAttack1 )
{
OnStoppedFiring();
}
}
}
// -----------------------
// Reload pressed / Clip Empty
// -----------------------
if ( bReload && UsesClipsForAmmo1() )
{
if ( !m_bInReload )
{
// reload when reload is pressed, or if no buttons are down and weapon is empty.
Reload();
m_fFireDuration = 0.0f;
#ifdef GAME_DLL
if ( pMarine && pMarine->GetMarineResource() )
pMarine->GetMarineResource()->m_iReloadsStarted++;
#endif
}
}
// -----------------------
// No buttons down
// -----------------------
if ( !bAttack2 && !bReload )
{
// no fire buttons down or reloading
if ( !ReloadOrSwitchWeapons() && !m_bInReload && !bAttack1 )
{
WeaponIdle();
}
}
#if !defined( CLIENT_DLL )
extern ConVar rd_infinite_ammo;
CASW_Weapon *pWeapon = pNPC->GetActiveASWWeapon();
if ( rd_infinite_ammo.GetBool() && pWeapon && pWeapon->Classify() != CLASS_ASW_HEAL_GUN && pWeapon->Classify() != CLASS_ASW_FIRE_EXTINGUISHER && pWeapon->Classify() != CLASS_ASW_HEALGRENADE )
{
pWeapon->m_iClip1 = pWeapon->GetMaxClip1();
int iPrimaryAmmoType = pWeapon->GetPrimaryAmmoType();
if ( iPrimaryAmmoType >= 0 )
pNPC->SetAmmoCount( GetAmmoDef()->MaxCarry( iPrimaryAmmoType, pNPC ), iPrimaryAmmoType );
if ( pWeapon->Classify() != CLASS_ASW_MEDRIFLE )
{
pWeapon->m_iClip2 = pWeapon->GetMaxClip2();
int iSecondaryAmmoType = pWeapon->GetSecondaryAmmoType();
if ( iSecondaryAmmoType >= 0 )
pNPC->SetAmmoCount( GetAmmoDef()->MaxCarry( iSecondaryAmmoType, pNPC ), iSecondaryAmmoType );
}
}
#endif
}
// just dry fire by default
void CASW_Weapon::SecondaryAttack( void )
{
SendWeaponAnim( ACT_VM_DRYFIRE );
BaseClass::WeaponSound( EMPTY );
m_flNextSecondaryAttack = gpGlobals->curtime + 0.5f;
}
//-----------------------------------------------------------------------------
// Purpose: If the current weapon has more ammo, reload it. Otherwise, switch
// to the next best weapon we've got. Returns true if it took any action.
//-----------------------------------------------------------------------------
bool CASW_Weapon::ReloadOrSwitchWeapons( void )
{
bool bAutoReload = true;
CBaseCombatCharacter *pCombatCharOwner = GetOwner();
CASW_Marine *pMarine = CASW_Marine::AsMarine( pCombatCharOwner );
if ( pMarine && pMarine->GetCommander() && pMarine->IsInhabited() )
{
CASW_Player *pPlayer = pMarine->GetCommander();
bAutoReload = pPlayer->ShouldAutoReload();
}
// if (HasAnyAmmo()) // asw add later!
m_bFireOnEmpty = false;
if ( UsesClipsForAmmo1() &&
( m_iClip1 == 0 ) &&
( GetWeaponFlags() & ITEM_FLAG_NOAUTORELOAD ) == false &&
bAutoReload &&
!m_bInReload )
{
// if we're successfully reloading, we're done
if ( Reload() )
{
#ifdef GAME_DLL
if ( pMarine && pMarine->GetMarineResource() )
pMarine->GetMarineResource()->m_iReloadsStarted++;
#endif
return true;
}
else
{
ClearIsFiring();
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this weapon has some ammo
//-----------------------------------------------------------------------------
bool CASW_Weapon::HasAmmo( void )
{
// Weapons with no ammo types can always be selected
if ( m_iPrimaryAmmoType == -1 && m_iSecondaryAmmoType == -1 )
return true;
if ( GetWeaponFlags() & ITEM_FLAG_SELECTONEMPTY )
return true;
CBaseCombatCharacter *pOwner = GetOwner();
if ( !pOwner )
return false;
return ( m_iClip1 > 0 || pOwner->GetAmmoCount( m_iPrimaryAmmoType ) || m_iClip2 > 0 || pOwner->GetAmmoCount( m_iSecondaryAmmoType ) );
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this weapon has some ammo loaded in the primary clip
//
// Useful because some weapons (like the ammo bag) may have special conditions
// other than how many bullets are in the clip - They can specialize this fn
//-----------------------------------------------------------------------------
bool CASW_Weapon::PrimaryAmmoLoaded( void )
{
return (m_iClip1 > 0);
}
void CASW_Weapon::PrimaryAttack( void )
{
// If my clip is empty (and I use clips) start reload
if ( UsesClipsForAmmo1() && !m_iClip1 )
{
Reload();
return;
}
CBaseCombatCharacter *pOwner = GetOwner();
if ( !pOwner || !pOwner->IsInhabitableNPC() )
return;
CASW_Inhabitable_NPC *pNPC = assert_cast< CASW_Inhabitable_NPC * >( pOwner );
CASW_Player *pPlayer = GetCommander();
m_bIsFiring = true;
// MUST call sound before removing a round from the clip of a CMachineGun
WeaponSound( SINGLE );
if ( m_iClip1 <= AmmoClickPoint() )
{
LowAmmoSound();
}
// tell the marine to tell its weapon to draw the muzzle flash
pNPC->DoMuzzleFlash();
// sets the animation on the weapon model itself
SendWeaponAnim( GetPrimaryAttackActivity() );
// sets the animation on the marine holding this weapon
//pMarine->SetAnimation( PLAYER_ATTACK1 );
#ifdef GAME_DLL // check for turning on lag compensation
if ( pPlayer && pNPC->IsInhabited() )
{
CASW_Lag_Compensation::RequestLagCompensation( pPlayer, pPlayer->GetCurrentUserCommand() );
}
#endif
FireBulletsInfo_t info;
info.m_vecSrc = pNPC->Weapon_ShootPosition();
CASW_Marine *pMarine = CASW_Marine::AsMarine( pNPC );
if ( pPlayer && pMarine && pMarine->IsInhabited() )
{
info.m_vecDirShooting = pPlayer->GetAutoaimVectorForMarine( pMarine, GetAutoAimAmount(), GetVerticalAdjustOnlyAutoAimAmount() ); // 45 degrees = 0.707106781187
}
else
{
#ifdef CLIENT_DLL
Msg( "Error, clientside firing of a weapon that's being controlled by an AI marine\n" );
#else
info.m_vecDirShooting = pNPC->GetActualShootTrajectory( info.m_vecSrc );
#endif
}
// To make the firing framerate independent, we may have to fire more than one bullet here on low-framerate systems,
// especially if the weapon we're firing has a really fast rate of fire.
info.m_iShots = 0;
float fireRate = GetFireRate();
while ( m_flNextPrimaryAttack <= gpGlobals->curtime )
{
m_flNextPrimaryAttack = m_flNextPrimaryAttack + fireRate;
info.m_iShots++;
if ( !fireRate )
break;
}
// Make sure we don't fire more than the amount in the clip
if ( UsesClipsForAmmo1() )
{
info.m_iShots = MIN( info.m_iShots, m_iClip1 );
m_iClip1 -= info.m_iShots;
#ifdef GAME_DLL
if ( m_iClip1 <= 0 && pMarine && pMarine->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
{
// check he doesn't have ammo in an ammo bay
CASW_Weapon_Ammo_Bag *pAmmoBag = NULL;
CASW_Weapon *pWeapon = pMarine->GetASWWeapon( 0 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
if ( !pAmmoBag )
{
pWeapon = pMarine->GetASWWeapon( 1 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
}
if ( !pAmmoBag || !pAmmoBag->CanGiveAmmoToWeapon( this ) )
pMarine->OnWeaponOutOfAmmo( true );
}
#endif
}
else
{
info.m_iShots = MIN( info.m_iShots, pNPC->GetAmmoCount( m_iPrimaryAmmoType ) );
pNPC->RemoveAmmo( info.m_iShots, m_iPrimaryAmmoType );
}
info.m_flDistance = asw_weapon_max_shooting_distance.GetFloat();
info.m_iAmmoType = m_iPrimaryAmmoType;
info.m_iTracerFreq = 1; // asw tracer test everytime
info.m_flDamageForceScale = asw_weapon_force_scale.GetFloat();
info.m_vecSpread = GetBulletSpread();
info.m_flDamage = GetWeaponDamage();
#ifndef CLIENT_DLL
if ( asw_debug_marine_damage.GetBool() )
Msg( "Weapon dmg = %f\n", info.m_flDamage );
Assert( SupportsDamageModifiers() );
if ( pMarine && pMarine->GetMarineResource() )
pMarine->GetMarineResource()->OnFired_ScaleDamage( info );
if ( asw_DebugAutoAim.GetBool() )
{
NDebugOverlay::Line( info.m_vecSrc, info.m_vecSrc + info.m_vecDirShooting * info.m_flDistance, 64, 0, 64, true, 1.0 );
}
#endif
pNPC->FireBullets( info );
// increment shooting stats
#ifndef CLIENT_DLL
if ( pMarine && pMarine->GetMarineResource() )
{
pMarine->GetMarineResource()->UsedWeapon( this, info.m_iShots );
pMarine->OnWeaponFired( this, info.m_iShots );
}
#endif
}
bool CASW_Weapon::IsPredicted(void) const
{
return true;
}
bool CASW_Weapon::ViewModelIsMarineAttachment() const
{
const CASW_EquipItem *pEquipItem = GetEquipItem();
return pEquipItem ? pEquipItem->m_bViewModelIsMarineAttachment : false;
}
bool CASW_Weapon::ViewModelHidesMarineBodyGroup1() const
{
const CASW_EquipItem *pEquipItem = GetEquipItem();
return pEquipItem ? pEquipItem->m_bViewModelHidesMarineBodyGroup1 : false;
}
// ========================
// reloading
// ========================
float CASW_Weapon::GetReloadTime()
{
// can adjust for marine's weapon skill here
float fReloadTime = GetEquipItem() ? GetEquipItem()->m_flReloadTime : 2.2f;
if ( GetMarine() )
{
float fSpeedScale = MarineSkills()->GetSkillBasedValueByMarine( GetMarine(), ASW_MARINE_SKILL_RELOADING, ASW_MARINE_SUBSKILL_RELOADING_SPEED_SCALE );
fReloadTime *= fSpeedScale;
#ifdef GAME_DLL
// riflemod: bots reload very fast because they are stupid to die
// during long reloads
if ( rd_bot_strong.GetBool() && !GetMarine()->IsInhabited() )
{
fReloadTime = 1.0f;
}
#endif
}
//CALL_ATTRIB_HOOK_FLOAT( fReloadTime, mod_reload_time );
return fReloadTime;
}
float CASW_Weapon::GetReloadFailTime()
{
return m_flReloadFailTime;
}
bool CASW_Weapon::Reload( void )
{
bool bReloaded = ASWReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
if ( bReloaded )
{
#ifdef GAME_DLL
CASW_Marine *pMarine = GetMarine();
if ( pMarine )
{
if ( pMarine->IsAlienNear() )
{
pMarine->GetMarineSpeech()->Chatter(CHATTER_RELOADING);
}
CASW_Marine_Resource *pMR = pMarine->GetMarineResource();
if ( pMR )
{
pMR->m_TimelineAmmo.RecordValue( pMarine->GetAllAmmoCount() );
}
}
#endif
}
return bReloaded;
}
bool CASW_Weapon::ASWReload( int iClipSize1, int iClipSize2, int iActivity )
{
if ( m_bInReload ) // we're already reloading!
{
Msg( "ASWReload already reloading\n" );
Assert( false );
return true;
}
CASW_Marine *pMarine = GetMarine();
if ( !pMarine || !ASWGameRules() )
return false;
bool bReload = false;
if ( m_bIsFiring )
{
OnStoppedFiring();
}
// If you don't have clips, then don't try to reload them.
if ( UsesClipsForAmmo1() )
{
// need to reload primary clip?
int primary = MIN( iClipSize1 - m_iClip1, pMarine->GetAmmoCount( m_iPrimaryAmmoType ) );
if ( primary != 0 )
{
bReload = true;
}
else
{
// check if we have an ammo bag we can take a clip from instead
CASW_Weapon_Ammo_Bag *pAmmoBag = NULL;
CASW_Weapon *pWeapon = pMarine->GetASWWeapon( 0 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
if ( !pAmmoBag )
{
pWeapon = pMarine->GetASWWeapon( 1 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
}
if ( pAmmoBag && pAmmoBag->CanGiveAmmoToWeapon( this ) )
{
#ifdef CLIENT_DLL
bReload = true;
#else
pAmmoBag->GiveClipTo( pMarine, m_iPrimaryAmmoType, true );
// now we've given a clip, check if we can reload
primary = MIN( iClipSize1 - m_iClip1, pMarine->GetAmmoCount( m_iPrimaryAmmoType ) );
if ( primary != 0 )
{
bReload = true;
}
#endif
}
}
}
if ( UsesClipsForAmmo2() )
{
// need to reload secondary clip?
int secondary = MIN( iClipSize2 - m_iClip2, pMarine->GetAmmoCount( m_iSecondaryAmmoType ) );
if ( secondary != 0 )
{
bReload = true;
}
}
if ( !bReload )
return false;
m_bFastReloadSuccess = false;
m_bFastReloadFailure = false;
#ifndef CLIENT_DLL
if ( GetMaxClip1() >= 1 )
{
// Fire event when a player reloads a weapon with more than a bullet per clip
IGameEvent *event = gameeventmanager->CreateEvent( "weapon_reload" );
if ( event )
{
CASW_Player *pPlayer = NULL;
pPlayer = pMarine->GetCommander();
int nClipSize = GetMaxClip1();
int nClips = pMarine->GetAmmoCount( m_iPrimaryAmmoType ) / nClipSize;
CASW_Weapon_Ammo_Bag *pAmmoBag = NULL;
CASW_Weapon *pWeapon = pMarine->GetASWWeapon( 0 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
if ( !pAmmoBag )
{
pWeapon = pMarine->GetASWWeapon( 1 );
if ( pWeapon && pWeapon->Classify() == CLASS_ASW_AMMO_BAG )
pAmmoBag = assert_cast< CASW_Weapon_Ammo_Bag * >( pWeapon );
}
if ( pAmmoBag && this != pAmmoBag )
{
nClips += pAmmoBag->NumClipsForWeapon( this );
}
event->SetInt( "userid", ( pPlayer ? pPlayer->GetUserID() : 0 ) );
event->SetInt( "marine", pMarine->entindex() );
event->SetInt( "lost", m_iClip1 );
event->SetInt( "clipsize", nClipSize );
event->SetInt( "clipsremaining", nClips - 1 );
event->SetInt( "clipsmax", GetAmmoDef()->MaxCarry( m_iPrimaryAmmoType, pMarine ) / nClipSize );
gameeventmanager->FireEvent( event );
}
}
#endif
m_fReloadClearFiringTime = gpGlobals->curtime + GetFireRate();
float fReloadTime = GetReloadTime();
float flSequenceEndTime = gpGlobals->curtime + fReloadTime;
pMarine->SetNextAttack( flSequenceEndTime );
m_flNextPrimaryAttack = m_flNextSecondaryAttack = flSequenceEndTime;
m_bInReload = true;
// set fast reload timings
// assuming 2.8 base reload time
// ~0.29
if ( CBaseEntity::GetPredictionRandomSeed() != -1 )
{
RandomSeed( CBaseEntity::GetPredictionRandomSeed() );
}