summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_weaponbase_gun.cpp
blob: a4c1a13ece4bae13c8d3e459bd7f63e006dfeb43 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Weapon Base Gun 
//
//=============================================================================

#include "cbase.h"
#include "tf_weaponbase_gun.h"
#include "tf_fx_shared.h"
#include "effect_dispatch_data.h"
#include "takedamageinfo.h"
#include "tf_projectile_nail.h"
#include "tf_weapon_jar.h"
#include "tf_weapon_flaregun.h"
#include "tf_projectile_energy_ring.h"

#if !defined( CLIENT_DLL )	// Server specific.

	#include "tf_gamestats.h"
	#include "tf_player.h"
	#include "tf_fx.h"
	#include "te_effect_dispatch.h"

	#include "tf_projectile_flare.h"
	#include "tf_projectile_rocket.h"
	#include "tf_projectile_arrow.h"
	#include "tf_projectile_energy_ball.h"
	#include "tf_weapon_grenade_pipebomb.h"
	#include "te.h"

#else	// Client specific.

	#include "c_tf_player.h"
	#include "c_te_effect_dispatch.h"
	#include "c_tf_gamestats.h"

#endif

//=============================================================================
//
// TFWeaponBase Gun tables.
//
IMPLEMENT_NETWORKCLASS_ALIASED( TFWeaponBaseGun, DT_TFWeaponBaseGun )

BEGIN_NETWORK_TABLE( CTFWeaponBaseGun, DT_TFWeaponBaseGun )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFWeaponBaseGun )
END_PREDICTION_DATA()

// Server specific.
#if !defined( CLIENT_DLL ) 
BEGIN_DATADESC( CTFWeaponBaseGun )
DEFINE_THINKFUNC( ZoomOutIn ),
DEFINE_THINKFUNC( ZoomOut ),
DEFINE_THINKFUNC( ZoomIn ),
END_DATADESC()
#endif

//=============================================================================
//
// TFWeaponBase Gun functions.
//

//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CTFWeaponBaseGun::CTFWeaponBaseGun()
{
	m_iWeaponMode = TF_WEAPON_PRIMARY_MODE;
	m_iAmmoToAdd = 0;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::PrimaryAttack( void )
{
	float flUberChargeAmmoPerShot = UberChargeAmmoPerShot();
	if ( flUberChargeAmmoPerShot > 0.0f )
	{
		if ( !HasPrimaryAmmo() )
			return;
	}

	// Check for ammunition.
	if ( m_iClip1 <= 0 && m_iClip1 != -1 )
		return;

	// Are we capable of firing again?
	if ( m_flNextPrimaryAttack > gpGlobals->curtime )
		return;

	// Get the player owning the weapon.
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	if ( !CanAttack() )
		return;

	float flFireDelay = ApplyFireDelay( m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_flTimeFireDelay );
	CALL_ATTRIB_HOOK_FLOAT_ON_OTHER( pPlayer, flFireDelay, hwn_mult_postfiredelay );

	// Some weapons change fire delay based on player's health
	float flReducedHealthBonus = 1.0f;
	CALL_ATTRIB_HOOK_FLOAT( flReducedHealthBonus, mult_postfiredelay_with_reduced_health );
	if ( flReducedHealthBonus != 1.0f )
	{
		flReducedHealthBonus = RemapValClamped( pPlayer->HealthFraction(), 0.2f, 0.9f, flReducedHealthBonus, 1.0f );
		flFireDelay *= flReducedHealthBonus;
	}

	if ( pPlayer->m_Shared.InCond( TF_COND_BLASTJUMPING ) )
	{
		CALL_ATTRIB_HOOK_FLOAT( flFireDelay, rocketjump_attackrate_bonus );	
	}
	else
	{
		CALL_ATTRIB_HOOK_FLOAT( flFireDelay, mul_nonrocketjump_attackrate );
	}

	if ( m_iPrimaryAmmoType == TF_AMMO_METAL )
	{
		if ( GetOwner() && GetAmmoPerShot() > GetOwner()->GetAmmoCount( m_iPrimaryAmmoType ) )
		{
			WeaponSound( EMPTY );
			m_flNextPrimaryAttack = gpGlobals->curtime + flFireDelay;
			return;
		}
	}

	CalcIsAttackCritical();

#ifndef CLIENT_DLL
	if ( pPlayer->m_Shared.IsStealthed() && ShouldRemoveInvisibilityOnPrimaryAttack() )
	{
		pPlayer->RemoveInvisibility();
	}
	
	// Minigun has custom handling
	if ( GetWeaponID() != TF_WEAPON_MINIGUN )
	{
		pPlayer->SpeakWeaponFire();
	}
	CTF_GameStats.Event_PlayerFiredWeapon( pPlayer, IsCurrentAttackACrit() );
#else
	C_CTF_GameStats.Event_PlayerFiredWeapon( pPlayer, IsCurrentAttackACrit() );
#endif

	// Minigun has custom handling
	if ( GetWeaponID() != TF_WEAPON_MINIGUN )
	{
		// Set the weapon mode.
		m_iWeaponMode = TF_WEAPON_PRIMARY_MODE;
	}

	SendWeaponAnim( ACT_VM_PRIMARYATTACK );

	pPlayer->SetAnimation( PLAYER_ATTACK1 );

	CBaseEntity* pProj = FireProjectile( pPlayer );
	ModifyProjectile( pProj );

	if ( !UsesClipsForAmmo1() )
	{
		// Sniper rifles and such don't actually reload, so we hook reduced reload here
		float flBaseFireDelay = flFireDelay;
		CALL_ATTRIB_HOOK_FLOAT( flFireDelay, fast_reload );

		float flPlaybackRate = flFireDelay == 0.f ? 0.f : flBaseFireDelay / flFireDelay;

		if ( pPlayer->GetViewModel( 0 ) )
		{
			pPlayer->GetViewModel( 0 )->SetPlaybackRate( flPlaybackRate );
		}
		if ( pPlayer->GetViewModel( 1 ) )
		{
			pPlayer->GetViewModel( 1 )->SetPlaybackRate( flPlaybackRate );
		}
	}

	// Set next attack times.
	m_flNextPrimaryAttack = gpGlobals->curtime + flFireDelay;

	// Don't push out secondary attack, because our secondary fire
	// systems are all separate from primary fire (sniper zooming, demoman pipebomb detonating, etc)
	//m_flNextSecondaryAttack = gpGlobals->curtime + m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_flTimeFireDelay;

	// Set the idle animation times based on the sequence duration, so that we play full fire animations
	// that last longer than the refire rate may allow.
	if ( Clip1() > 0 )
	{
		SetWeaponIdleTime( gpGlobals->curtime + SequenceDuration() );
	}
	else
	{
		SetWeaponIdleTime( gpGlobals->curtime + SequenceDuration() );
	}

	// Check the reload mode and behave appropriately.
	if ( m_bReloadsSingly )
	{
		m_iReloadMode.Set( TF_RELOAD_START );
	}

#ifdef STAGING_ONLY
	// Remove Cond if I attack
	if ( pPlayer->m_Shared.InCond( TF_COND_NO_COMBAT_SPEED_BOOST ) )
	{
		pPlayer->m_Shared.RemoveCond( TF_COND_NO_COMBAT_SPEED_BOOST );
	}
#endif

	m_flLastPrimaryAttackTime = gpGlobals->curtime;

	if ( ShouldRemoveDisguiseOnPrimaryAttack() )
	{
		pPlayer->RemoveDisguise();
	}
}	

bool CTFWeaponBaseGun::ShouldRemoveDisguiseOnPrimaryAttack() const
{
	int iAttr = 0;
	CALL_ATTRIB_HOOK_INT( iAttr, keep_disguise_on_attack );
	if ( iAttr )
		return false;

	return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::SecondaryAttack( void )
{
	// semi-auto behaviour
	if ( m_bInAttack2 )
		return;

	// Get the player owning the weapon.
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	pPlayer->DoClassSpecialSkill();

	m_bInAttack2 = true;

#ifdef STAGING_ONLY
	// Remove Cond if I attack
	if ( pPlayer->m_Shared.InCond( TF_COND_NO_COMBAT_SPEED_BOOST ) )
	{
		pPlayer->m_Shared.RemoveCond( TF_COND_NO_COMBAT_SPEED_BOOST );
	}
#endif

	m_flNextSecondaryAttack = gpGlobals->curtime + 0.5;
}

CBaseEntity *CTFWeaponBaseGun::FireProjectile( CTFPlayer *pPlayer )
{
	// New behavior: allow weapons to have attributes to specify what sort of
	// projectile they fire.
	int iProjectile = 0;
	CALL_ATTRIB_HOOK_INT( iProjectile, override_projectile_type );

	// Previous default behavior: ask the weapon type for what sort of projectile
	// to launch.
	if ( iProjectile == 0 )
	{
		iProjectile = GetWeaponProjectileType();
	}

	CBaseEntity *pProjectile = NULL;

	// Anyone ever hear of a factory? This is a disgrace.
	switch( iProjectile )
	{
	case TF_PROJECTILE_BULLET:
		FireBullet( pPlayer );
		//pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_ROCKET:
		pProjectile = FireRocket( pPlayer, iProjectile );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_SYRINGE:
#ifdef STAGING_ONLY
	case TF_PROJECTILE_TRANQ:
#endif // STAGING_ONLY
		pProjectile = FireNail( pPlayer, iProjectile );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_FLARE:
		pProjectile = FireFlare( pPlayer );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_PIPEBOMB:
	case TF_PROJECTILE_PIPEBOMB_REMOTE:
	case TF_PROJECTILE_PIPEBOMB_PRACTICE:
	case TF_PROJECTILE_CANNONBALL:
		pProjectile = FirePipeBomb( pPlayer, iProjectile );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_JAR:
	case TF_PROJECTILE_JAR_MILK:
	case TF_PROJECTILE_CLEAVER:
	case TF_PROJECTILE_THROWABLE:
	case TF_PROJECTILE_FESTIVE_JAR:
	case TF_PROJECTILE_BREADMONSTER_JARATE:
	case TF_PROJECTILE_BREADMONSTER_MADMILK:
		pProjectile = FireJar( pPlayer );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;
	case TF_PROJECTILE_ARROW:
	case TF_PROJECTILE_HEALING_BOLT:
	case TF_PROJECTILE_BUILDING_REPAIR_BOLT:
	case TF_PROJECTILE_FESTIVE_ARROW:
	case TF_PROJECTILE_FESTIVE_HEALING_BOLT:
#ifdef STAGING_ONLY
	case TF_PROJECTILE_SNIPERBULLET:
	case TF_PROJECTILE_MILK_BOLT:
#endif
	case TF_PROJECTILE_GRAPPLINGHOOK:
		pProjectile = FireArrow( pPlayer, ProjectileType_t( iProjectile ) );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		break;

	case TF_PROJECTILE_FLAME_ROCKET:
		pProjectile = FireFlameRocket( pPlayer );
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_SECONDARY );
		break;

	case TF_PROJECTILE_ENERGY_BALL:
		pProjectile = FireEnergyBall( pPlayer );
		if ( ShouldPlayFireAnim() )
		{
			pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		}
		break;

	case TF_PROJECTILE_ENERGY_RING:
		pProjectile = FireEnergyBall( pPlayer, true );
		if ( ShouldPlayFireAnim() )
		{
			pPlayer->DoAnimationEvent( PLAYERANIMEVENT_ATTACK_PRIMARY );
		}
		break;

	case TF_PROJECTILE_NONE:
	default:
		// do nothing!
		DevMsg( "Weapon does not have a projectile type set\n" );
		break;
	}

	RemoveProjectileAmmo( pPlayer );

	m_flLastFireTime = gpGlobals->curtime;

	DoFireEffects();

	UpdatePunchAngles( pPlayer );

#ifdef GAME_DLL
	// Some game modes may allow any class to have stealth.  Continuous-fire weapons like the
	// minigun might be firing when stealth is applied, so we try removing it from here, too.
	if ( pPlayer->m_Shared.IsStealthed() && ShouldRemoveInvisibilityOnPrimaryAttack() )
	{
		pPlayer->RemoveInvisibility();
	}
#endif // GAME_DLL

	return pProjectile;
}

//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::RemoveProjectileAmmo( CTFPlayer *pPlayer )
{

	if ( m_iClip1 != -1 )
	{
		m_iClip1 -= GetAmmoPerShot();
	}
	else
	{
		if ( m_iWeaponMode == TF_WEAPON_PRIMARY_MODE )
		{
			pPlayer->RemoveAmmo( GetAmmoPerShot(), m_iPrimaryAmmoType );

#ifndef CLIENT_DLL
			// delayed ammo adding for the onhit attribute
			if ( m_iAmmoToAdd > 0 )
			{
				pPlayer->GiveAmmo( m_iAmmoToAdd, m_iPrimaryAmmoType );
				m_iAmmoToAdd = 0;
			}
#endif
		}
		else
		{
			pPlayer->RemoveAmmo( GetAmmoPerShot(), m_iSecondaryAmmoType );

#ifndef CLIENT_DLL
			// delayed ammo adding for the onhit attribute
			if ( m_iAmmoToAdd > 0 )
			{
				pPlayer->GiveAmmo( m_iAmmoToAdd, m_iSecondaryAmmoType );
				m_iAmmoToAdd = 0;
			}
#endif
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFWeaponBaseGun::HasPrimaryAmmo( void )
{
	if ( m_iPrimaryAmmoType == TF_AMMO_METAL )
	{
		if ( GetOwner() && ( GetOwner()->GetAmmoCount( m_iPrimaryAmmoType ) < GetAmmoPerShot() ) )
			return false;
	}

	return BaseClass::HasPrimaryAmmo();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFWeaponBaseGun::CanDeploy( void )
{
	if ( m_iPrimaryAmmoType == TF_AMMO_METAL )
	{
		if ( GetOwner() && ( GetOwner()->GetAmmoCount( m_iPrimaryAmmoType ) < GetAmmoPerShot() ) )
			return false;
	}

	return BaseClass::CanDeploy();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFWeaponBaseGun::CanBeSelected( void )
{
	if ( m_iPrimaryAmmoType == TF_AMMO_METAL )
	{
		if ( GetOwner() && ( GetOwner()->GetAmmoCount( m_iPrimaryAmmoType ) < GetAmmoPerShot() ) )
			return false;
	}

	return BaseClass::CanBeSelected();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTFWeaponBaseGun::GetAmmoPerShot( void )
{
	if ( IsEnergyWeapon() )
		return 0;
	else
	{
		int iAmmoPerShot = 0;
		CALL_ATTRIB_HOOK_INT( iAmmoPerShot, mod_ammo_per_shot );
		if ( iAmmoPerShot > 0 )
			return iAmmoPerShot;

		return m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_iAmmoPerShot;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::UpdatePunchAngles( CTFPlayer *pPlayer )
{
	// Update the player's punch angle.
	QAngle angle = pPlayer->GetPunchAngle();
	float flPunchAngle = m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_flPunchAngle;

	if ( flPunchAngle > 0 )
	{
		angle.x -= SharedRandomInt( "ShotgunPunchAngle", ( flPunchAngle - 1 ), ( flPunchAngle + 1 ) );
		pPlayer->SetPunchAngle( angle );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Fire a bullet!
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::FireBullet( CTFPlayer *pPlayer )
{
	PlayWeaponShootSound();

	FX_FireBullets(
		this,
		pPlayer->entindex(),
		pPlayer->Weapon_ShootPosition(),
		pPlayer->EyeAngles() + pPlayer->GetPunchAngle(),
		GetWeaponID(),
		m_iWeaponMode,
		CBaseEntity::GetPredictionRandomSeed( UseServerRandomSeed() ) & 255,
		GetWeaponSpread(),
		GetProjectileDamage(),
		IsCurrentAttackACrit() );
}

//-----------------------------------------------------------------------------
// Purpose: Fire a rocket
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireRocket( CTFPlayer *pPlayer, int iRocketType )
{
	PlayWeaponShootSound();

	// Server only - create the rocket.
#ifdef GAME_DLL

	Vector vecSrc;
	QAngle angForward;
	Vector vecOffset( 23.5f, 12.0f, -3.0f );
	if ( pPlayer->GetFlags() & FL_DUCKING )
	{
		vecOffset.z = 8.0f;
	}
	GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward, false );

	trace_t trace;	
	Vector vecEye = pPlayer->EyePosition();
	CTraceFilterSimple traceFilter( this, COLLISION_GROUP_NONE );
	UTIL_TraceLine( vecEye, vecSrc, MASK_SOLID_BRUSHONLY, &traceFilter, &trace );

	CTFProjectile_Rocket *pProjectile = CTFProjectile_Rocket::Create( this, trace.endpos, angForward, pPlayer, pPlayer );

	if ( pProjectile )
	{
		pProjectile->SetCritical( IsCurrentAttackACrit() );
		pProjectile->SetDamage( GetProjectileDamage() );
	}

	return pProjectile;

#endif

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Fire an energy ball
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireEnergyBall( CTFPlayer *pPlayer, bool bRing )
{
	PlayWeaponShootSound();

	Vector vecSrc;
	QAngle angForward;
	Vector vecOffset( 23.5f, -8.0f, -3.0f );
	if ( pPlayer->GetFlags() & FL_DUCKING )
	{
		vecOffset.z = 8.0f;
	}
	GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward, false );

	trace_t trace;
	Vector vecEye = pPlayer->EyePosition();
	CTraceFilterSimple traceFilter( this, COLLISION_GROUP_NONE );
	UTIL_TraceLine( vecEye, vecSrc, MASK_SOLID_BRUSHONLY, &traceFilter, &trace );

	if ( bRing )
	{
		CTFProjectile_EnergyRing* pProjectile = CTFProjectile_EnergyRing::Create( this, trace.endpos, angForward, 
			GetProjectileSpeed(), GetProjectileGravity(), pPlayer, pPlayer, GetParticleColor(1), GetParticleColor(2), IsCurrentAttackACrit() );
		if ( pProjectile )
		{
			pProjectile->SetWeaponID( GetWeaponID() );
			pProjectile->SetCritical( IsCurrentAttackACrit() );
#ifdef GAME_DLL
			pProjectile->SetDamage( GetProjectileDamage() );
#endif
		}
		return pProjectile;
	}
	else
	{
#ifdef GAME_DLL
		CTFProjectile_EnergyBall* pProjectile = CTFProjectile_EnergyBall::Create( trace.endpos, angForward, GetProjectileSpeed(), GetProjectileGravity(), pPlayer, pPlayer );
		if ( pProjectile )
		{
			pProjectile->SetLauncher( this );
			pProjectile->SetCritical( IsCurrentAttackACrit() );
			pProjectile->SetDamage( GetProjectileDamage() );
		}
		return pProjectile;
#endif
	}


	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Fire a projectile nail
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireNail( CTFPlayer *pPlayer, int iSpecificNail )
{
	PlayWeaponShootSound();

	Vector vecSrc;
	QAngle angForward;
	
	// Add some spread
	float flSpread = 1.5;
	flSpread += GetProjectileSpread();

	CTFBaseProjectile *pProjectile = NULL;
	switch( iSpecificNail )
	{
	case TF_PROJECTILE_SYRINGE:
		{
			Vector vecOffset( 16, 6, -8 );
			GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward );
			angForward.x += RandomFloat( -flSpread, flSpread );
			angForward.y += RandomFloat( -flSpread, flSpread );
			pProjectile = CTFProjectile_Syringe::Create( vecSrc, angForward, this, pPlayer, pPlayer, IsCurrentAttackACrit() );
		}
		break;
#ifdef STAGING_ONLY
	case TF_PROJECTILE_TRANQ:
		{
			Vector vecOffset( 16, 6, 0 );
			GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward );
			pProjectile = CTFProjectile_Tranq::Create( vecSrc, angForward, this, pPlayer, pPlayer, IsCurrentAttackACrit() );
		}
		break;
#endif // STAGING_ONLY
	default:
		Assert(0);
	}

	if ( pProjectile )
	{
		pProjectile->SetWeaponID( GetWeaponID() );
		pProjectile->SetCritical( IsCurrentAttackACrit() );
#ifdef GAME_DLL
		pProjectile->SetLauncher( this );
		pProjectile->SetDamage( GetProjectileDamage() );
#endif
	}
	
	return pProjectile;
}

//-----------------------------------------------------------------------------
// Purpose: Fire a pipe bomb
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FirePipeBomb( CTFPlayer *pPlayer, int iPipeBombType )
{
	PlayWeaponShootSound();

#ifdef GAME_DLL
	QAngle angEyes = pPlayer->EyeAngles();

	float flSpreadAngle = 0.0f; 
	CALL_ATTRIB_HOOK_FLOAT( flSpreadAngle, projectile_spread_angle );
	if ( flSpreadAngle > 0.0f )
	{
		QAngle angSpread = RandomAngle( -flSpreadAngle, flSpreadAngle );
		angSpread.z = 0.0f;
		angEyes += angSpread;
		DevMsg( "Fire bomb at %f %f %f\n", XYZ(angEyes) );
	}

	Vector vecForward, vecRight, vecUp;
	AngleVectors( angEyes, &vecForward, &vecRight, &vecUp );

	// Create grenades here!!
	float fRight = 8.f;
	if ( IsViewModelFlipped() )
	{
		fRight *= -1;
	}
	Vector vecSrc = pPlayer->Weapon_ShootPosition();
	vecSrc +=  vecForward * 16.0f + vecRight * fRight + vecUp * -6.0f;

	trace_t trace;	
	Vector vecEye = pPlayer->EyePosition();
	CTraceFilterSimple traceFilter( this, COLLISION_GROUP_NONE );
	UTIL_TraceHull( vecEye, vecSrc, -Vector(8,8,8), Vector(8,8,8), MASK_SOLID_BRUSHONLY, &traceFilter, &trace );

	// If we started in solid, don't let them fire at all
	if ( trace.startsolid )
		return NULL;

	float flLaunchSpeed = GetProjectileSpeed();
	CALL_ATTRIB_HOOK_FLOAT( flLaunchSpeed, mult_projectile_range );
	Vector vecVelocity = ( vecForward * flLaunchSpeed ) + ( vecUp * 200.0f ) + ( random->RandomFloat( -10.0f, 10.0f ) * vecRight ) +		
		( random->RandomFloat( -10.0f, 10.0f ) * vecUp );

	float flMultDmg = 1.f;
	CALL_ATTRIB_HOOK_FLOAT( flMultDmg, mult_dmg );
	
	// no spin for loch-n-load
	Vector angImpulse = AngularImpulse( 600, random->RandomInt( -1200, 1200 ), 0 );
	int iNoSpin = 0;
	CALL_ATTRIB_HOOK_INT( iNoSpin, grenade_no_spin );
	if ( iNoSpin )
	{
		angImpulse.Zero();
	}

	CTFGrenadePipebombProjectile *pProjectile = CTFGrenadePipebombProjectile::Create( trace.endpos, angEyes, vecVelocity, angImpulse, pPlayer, GetTFWpnData(), iPipeBombType, flMultDmg );

	if ( pProjectile )
	{
		pProjectile->SetCritical( IsCurrentAttackACrit() );
		pProjectile->SetLauncher( this );

		//float flFizzle = 0;
		//CALL_ATTRIB_HOOK_FLOAT( flFizzle, stickybomb_fizzle_time );
		//if ( flFizzle )
		//{
		//	pProjectile->SetDetonateTimerLength( flFizzle );
		//}
		CAttribute_String attrCustomModelName;
		GetCustomProjectileModel( &attrCustomModelName );
		if ( attrCustomModelName.has_value() )
		{
			pProjectile->SetModel( attrCustomModelName.value().c_str() );
		}

	}

	return pProjectile;

#endif

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Fire a flare
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireFlare( CTFPlayer *pPlayer )
{
	PlayWeaponShootSound();

	// Server only - create the flare.
#ifdef GAME_DLL

	Vector vecSrc;
	QAngle angForward;
	Vector vecOffset( 23.5f, 12.0f, -3.0f );
	if ( pPlayer->GetFlags() & FL_DUCKING )
	{
		vecOffset.z = 8.0f;
	}
	GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward, false );

	CTFProjectile_Flare *pProjectile = CTFProjectile_Flare::Create( this, vecSrc, angForward, pPlayer, pPlayer );
	if ( pProjectile )
	{
		pProjectile->SetLauncher( this );
		pProjectile->SetCritical( IsCurrentAttackACrit() );
		pProjectile->SetDamage( GetProjectileDamage() );
		CTFFlareGun *pFlareGun = dynamic_cast<CTFFlareGun *>( this );
		if ( pFlareGun && pFlareGun->GetFlareGunType() == FLAREGUN_DETONATE )
		{
			pFlareGun->AddFlare( pProjectile );
		}
	}
	return pProjectile;

#endif

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Fire an arrow
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireArrow( CTFPlayer *pPlayer, ProjectileType_t projectileType )
{
	PlayWeaponShootSound();

	// Server only - create the rocket.
#ifdef GAME_DLL

	Vector vecSrc;
	QAngle angForward;
	Vector vecOffset( 23.5f, -8.0f, -3.0f );
#ifdef STAGING_ONLY
	if ( projectileType == TF_PROJECTILE_SNIPERBULLET )
	{
		// Center the bullet while zoomed, otherwise flip the arrow cause arrows are dumb
		if ( pPlayer->m_Shared.InCond( TF_COND_ZOOMED ) )
		{
			vecOffset = Vector( 32, 0, -2.0f );
		}
		else
		{
			vecOffset.y = 8.0f;
		}
	}
#endif // STAGING_ONLY

	GetProjectileFireSetup( pPlayer, vecOffset, &vecSrc, &angForward, false );

	CTFProjectile_Arrow *pProjectile = CTFProjectile_Arrow::Create( vecSrc, angForward, GetProjectileSpeed(), GetProjectileGravity(), projectileType, pPlayer, pPlayer );
	if ( pProjectile )
	{
		pProjectile->SetLauncher( this );
		pProjectile->SetCritical( IsCurrentAttackACrit() );
		pProjectile->SetDamage( GetProjectileDamage() );

		int iPenetrate = 0;
		CALL_ATTRIB_HOOK_INT( iPenetrate, projectile_penetration );
		if ( iPenetrate == 1 )
		{
			pProjectile->SetPenetrate( true );
		}
		pProjectile->SetCollisionGroup( TFCOLLISION_GROUP_ROCKET_BUT_NOT_WITH_OTHER_ROCKETS );
	}
	return pProjectile;

#endif

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Toss a Jar...of something...
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireJar( CTFPlayer *pPlayer )
{
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CBaseEntity *CTFWeaponBaseGun::FireFlameRocket( CTFPlayer *pPlayer )
{
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::PlayWeaponShootSound( void )
{
	if ( IsCurrentAttackACrit() )
	{
		WeaponSound( BURST );
	}
	else
	{
		WeaponSound( SINGLE );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CTFWeaponBaseGun::GetWeaponSpread( void )
{
	float fSpread = m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_flSpread;
	CALL_ATTRIB_HOOK_FLOAT( fSpread, mult_spread_scale );

	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() ); 
	
	if ( pPlayer )
	{
		if ( pPlayer->m_Shared.GetCarryingRuneType() == RUNE_PRECISION )
		{
			if ( GetWeaponID() == TF_WEAPON_MINIGUN )
			{
				fSpread *= 0.4f;
			}
			else
			{
				fSpread *= 0.1f;
			}
		}

		// Some weapons change fire delay based on player's health
		float flReducedHealthBonus = 1.0f;
		CALL_ATTRIB_HOOK_FLOAT( flReducedHealthBonus, panic_attack_negative );
		if ( flReducedHealthBonus != 1.0f )
		{
			flReducedHealthBonus = RemapValClamped( pPlayer->HealthFraction(), 0.2f, 0.9f, flReducedHealthBonus, 1.0f );
			fSpread *= flReducedHealthBonus;
		}
	}

	return fSpread;
}

//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::GetCustomProjectileModel( CAttribute_String *attrCustomProjModel )
{
	// Must still add these to a precache somewhere
	// ie CTFGrenadePipebombProjectile::Precache()
	static CSchemaAttributeDefHandle pAttrDef_ProjectileEntityName( "custom projectile model" );
	CEconItemView *pItem = GetAttributeContainer()->GetItem();
	if ( pAttrDef_ProjectileEntityName && pItem )
	{
		pItem->FindAttribute( pAttrDef_ProjectileEntityName, attrCustomProjModel );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Accessor for damage, so sniper etc can modify damage
//-----------------------------------------------------------------------------
float CTFWeaponBaseGun::GetProjectileDamage( void )
{
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );

	float flDamage = (float)m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_nDamage;
	CALL_ATTRIB_HOOK_FLOAT( flDamage, mult_dmg );

	// Some weapons mod dmg when not disguised
	bool bDisguised = pPlayer && pPlayer->m_Shared.InCond( TF_COND_DISGUISED );
	if ( bDisguised )
	{
		CALL_ATTRIB_HOOK_FLOAT( flDamage, mult_dmg_disguised );
	}

	if ( pPlayer && ( pPlayer->IsPlayerClass( TF_CLASS_SOLDIER ) || pPlayer->IsPlayerClass( TF_CLASS_PYRO ) ) )
	{	
		float flRageDamage = 1.f;
		CALL_ATTRIB_HOOK_FLOAT( flRageDamage, rage_damage );

		if ( flRageDamage > 1.f )
		{
			float flRageRatio = pPlayer->m_Shared.GetRageMeter() / 100.f;
			flRageDamage = (flRageDamage - 1.f) * flRageRatio;
			flDamage *= 1.f + flRageDamage;
		}
	}

#ifdef GAME_DLL
	float flMedicHealDamageBonus = 1.f;
	CALL_ATTRIB_HOOK_FLOAT( flMedicHealDamageBonus, medic_healed_damage_bonus );

	if ( flMedicHealDamageBonus > 1.f )
	{
		if ( pPlayer )
		{
			int numHealers = pPlayer->m_Shared.GetNumHealers();
			bool bHealedByMedic = false;
			for ( int i=0; i<numHealers; i++ )
			{
				if ( ToTFPlayer( pPlayer->m_Shared.GetHealerByIndex( i ) ) != NULL )
				{
					bHealedByMedic = true;
				}
			}

			if ( bHealedByMedic )
			{
				flDamage *= flMedicHealDamageBonus;
			}
		}
	}

	if ( GetWeaponProjectileType() == TF_PROJECTILE_BULLET )
	{
		float flScaleDamage = 1.f;
		CALL_ATTRIB_HOOK_FLOAT( flScaleDamage, accuracy_scales_damage );
		if ( flScaleDamage > 1.f )
		{
			// Bullets fired vs hit ratio over last x.x second(s)
			if ( gpGlobals->curtime < GetLastHitTime() + 0.7f )
			{
				float flRatio = (float)m_iHitsInTime / (float)m_iFiredInTime;
				float flDmgMod = RemapValClamped( flRatio, 0.f, 1.f, 1.f, flScaleDamage );

// 				DevMsg( "A: %f - D: %f\n", flRatio, flDmgMod );
// 				DevMsg( "H: %d - F: %d\n", m_iHitsInTime, m_iFiredInTime );

				flDamage *= flDmgMod;
			}
			else
			{
				m_iHitsInTime = 1;
				m_iFiredInTime = 1;
			}
		}
	}

#endif

	return flDamage;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CTFWeaponBaseGun::Holster( CBaseCombatWeapon *pSwitchingTo )
{
// Server specific.
#if !defined( CLIENT_DLL )

	// Make sure to zoom out before we holster the weapon.
	ZoomOut();
	SetContextThink( NULL, 0, ZOOM_CONTEXT );

#endif

	return BaseClass::Holster( pSwitchingTo );
}

//-----------------------------------------------------------------------------
// Purpose:
// NOTE: Should this be put into fire gun
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::DoFireEffects()
{
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	if ( ShouldDoMuzzleFlash() )
	{
		pPlayer->DoMuzzleFlash();
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::ToggleZoom( void )
{
	// Toggle the zoom.
	CBasePlayer *pPlayer = GetPlayerOwner();
	if ( pPlayer )
	{
		if( pPlayer->GetFOV() >= 75 )
		{
			ZoomIn();
		}
		else
		{
			ZoomOut();
		}
	}

	// Get the zoom animation time.
	m_flNextSecondaryAttack = gpGlobals->curtime + 1.2;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::ZoomIn( void )
{
	// The the owning player.
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	// Set the weapon zoom.
	// TODO: The weapon fov should be gotten from the script file.
	float fBaseZoom = TF_WEAPON_ZOOM_FOV;

	// Disabled this for now, because we have no attributes using it
	//CALL_ATTRIB_HOOK_FLOAT( fBaseZoom, mult_zoom_fov );

	pPlayer->SetFOV( pPlayer, fBaseZoom, 0.1f );
	pPlayer->m_Shared.AddCond( TF_COND_ZOOMED );

#if defined( CLIENT_DLL )
	// Doing this allows us to show/hide the player viewmodel/localmodel
	pPlayer->UpdateVisibility();
#endif
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::ZoomOut( void )
{
	// The the owning player.
	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	if ( pPlayer->m_Shared.InCond( TF_COND_ZOOMED ) )
	{
		// Set the FOV to 0 set the default FOV.
		pPlayer->SetFOV( pPlayer, 0, 0.1f );
		pPlayer->m_Shared.RemoveCond( TF_COND_ZOOMED );
	}

#if defined( CLIENT_DLL )
	// Doing this allows us to show/hide the player viewmodel/localmodel
	pPlayer->UpdateVisibility();
#endif
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWeaponBaseGun::ZoomOutIn( void )
{
	//Zoom out, set think to zoom back in.
	ZoomOut();
	SetContextThink( &CTFWeaponBaseGun::ZoomIn, gpGlobals->curtime + ZOOM_REZOOM_TIME, ZOOM_CONTEXT );
}

//-----------------------------------------------------------------------------
bool CTFWeaponBaseGun::HasLastShotCritical( void )
{
	if ( m_iClip1 == 1 )
	{
		int iAttr = 0;
		CALL_ATTRIB_HOOK_INT( iAttr, last_shot_crits );
		if ( iAttr )
		{
			return true;
		}
	}
	return false;
}