-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbasecombatweapon_shared.cpp
2869 lines (2454 loc) · 84.2 KB
/
basecombatweapon_shared.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "in_buttons.h"
#include "engine/IEngineSound.h"
#include "ammodef.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "physics_saverestore.h"
#include "datacache/imdlcache.h"
#include "activitylist.h"
// NVNT start extra includes
#include "haptics/haptic_utils.h"
#ifdef CLIENT_DLL
#include "prediction.h"
#endif
// NVNT end extra includes
#if defined ( TF_DLL ) || defined ( TF_CLIENT_DLL )
#include "tf_shareddefs.h"
#endif
#if !defined( CLIENT_DLL )
// Game DLL Headers
#include "soundent.h"
#include "eventqueue.h"
#include "fmtstr.h"
#include "gameweaponmanager.h"
#ifdef HL2MP
#include "hl2mp_gamerules.h"
#endif
#endif
#include "vprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// The minimum time a hud hint for a weapon should be on screen. If we switch away before
// this, then teh hud hint counter will be deremented so the hint will be shown again, as
// if it had never been seen. The total display time for a hud hint is specified in client
// script HudAnimations.txt (which I can't read here).
#define MIN_HUDHINT_DISPLAY_TIME 7.0f
#define HIDEWEAPON_THINK_CONTEXT "BaseCombatWeapon_HideThink"
extern bool UTIL_ItemCanBeTouchedByPlayer( CBaseEntity *pItem, CBasePlayer *pPlayer );
#if defined ( TF_CLIENT_DLL ) || defined ( TF_DLL )
#ifdef _DEBUG
ConVar tf_weapon_criticals_force_random( "tf_weapon_criticals_force_random", "0", FCVAR_REPLICATED | FCVAR_CHEAT );
#endif // _DEBUG
ConVar tf_weapon_criticals_bucket_cap( "tf_weapon_criticals_bucket_cap", "1000.0", FCVAR_REPLICATED | FCVAR_CHEAT );
ConVar tf_weapon_criticals_bucket_bottom( "tf_weapon_criticals_bucket_bottom", "-250.0", FCVAR_REPLICATED | FCVAR_CHEAT );
ConVar tf_weapon_criticals_bucket_default( "tf_weapon_criticals_bucket_default", "300.0", FCVAR_REPLICATED | FCVAR_CHEAT );
#endif // TF
CBaseCombatWeapon::CBaseCombatWeapon()
{
// Constructor must call this
// CONSTRUCT_PREDICTABLE( CBaseCombatWeapon );
// Some default values. There should be set in the particular weapon classes
m_fMinRange1 = 65;
m_fMinRange2 = 65;
m_fMaxRange1 = 1024;
m_fMaxRange2 = 1024;
m_bReloadsSingly = false;
// Defaults to zero
m_nViewModelIndex = 0;
m_bFlipViewModel = false;
#if defined( CLIENT_DLL )
m_iState = m_iOldState = WEAPON_NOT_CARRIED;
m_iClip1 = -1;
m_iClip2 = -1;
m_iPrimaryAmmoType = -1;
m_iSecondaryAmmoType = -1;
#endif
#if !defined( CLIENT_DLL )
m_pConstraint = NULL;
OnBaseCombatWeaponCreated( this );
#endif
m_hWeaponFileInfo = GetInvalidWeaponInfoHandle();
#if defined( TF_DLL )
UseClientSideAnimation();
#endif
#if defined ( TF_CLIENT_DLL ) || defined ( TF_DLL )
m_flCritTokenBucket = tf_weapon_criticals_bucket_default.GetFloat();
m_nCritChecks = 1;
m_nCritSeedRequests = 0;
#endif // TF
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CBaseCombatWeapon::~CBaseCombatWeapon( void )
{
#if !defined( CLIENT_DLL )
//Remove our constraint, if we have one
if ( m_pConstraint != NULL )
{
physenv->DestroyConstraint( m_pConstraint );
m_pConstraint = NULL;
}
OnBaseCombatWeaponDestroyed( this );
#endif
}
void CBaseCombatWeapon::Activate( void )
{
BaseClass::Activate();
#ifndef CLIENT_DLL
if ( GetOwnerEntity() )
return;
if ( g_pGameRules->IsAllowedToSpawn( this ) == false )
{
UTIL_Remove( this );
return;
}
#endif
}
void CBaseCombatWeapon::GiveDefaultAmmo( void )
{
// If I use clips, set my clips to the default
if ( UsesClipsForAmmo1() )
{
m_iClip1 = AutoFiresFullClip() ? 0 : GetDefaultClip1();
}
else
{
SetPrimaryAmmoCount( GetDefaultClip1() );
m_iClip1 = WEAPON_NOCLIP;
}
if ( UsesClipsForAmmo2() )
{
m_iClip2 = GetDefaultClip2();
}
else
{
SetSecondaryAmmoCount( GetDefaultClip2() );
m_iClip2 = WEAPON_NOCLIP;
}
}
//-----------------------------------------------------------------------------
// Purpose: Set mode to world model and start falling to the ground
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::Spawn( void )
{
bool bPrecacheAllowed = CBaseEntity::IsPrecacheAllowed();
if (!bPrecacheAllowed)
{
tmEnter( TELEMETRY_LEVEL1, TMZF_NONE, "LateWeaponPrecache" );
}
Precache();
if (!bPrecacheAllowed)
{
tmLeave( TELEMETRY_LEVEL1 );
}
BaseClass::Spawn();
SetSolid( SOLID_BBOX );
m_flNextEmptySoundTime = 0.0f;
// Weapons won't show up in trace calls if they are being carried...
RemoveEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
m_iState = WEAPON_NOT_CARRIED;
// Assume
m_nViewModelIndex = 0;
GiveDefaultAmmo();
if ( GetWorldModel() )
{
SetModel( GetWorldModel() );
}
#if !defined( CLIENT_DLL )
if( IsX360() )
{
AddEffects( EF_ITEM_BLINK );
}
FallInit();
SetCollisionGroup( COLLISION_GROUP_WEAPON );
m_takedamage = DAMAGE_EVENTS_ONLY;
SetBlocksLOS( false );
// Default to non-removeable, because we don't want the
// game_weapon_manager entity to remove weapons that have
// been hand-placed by level designers. We only want to remove
// weapons that have been dropped by NPC's.
SetRemoveable( false );
#endif
// Bloat the box for player pickup
CollisionProp()->UseTriggerBounds( true, 36 );
// Use more efficient bbox culling on the client. Otherwise, it'll setup bones for most
// characters even when they're not in the frustum.
AddEffects( EF_BONEMERGE_FASTCULL );
m_iReloadHudHintCount = 0;
m_iAltFireHudHintCount = 0;
m_flHudHintMinDisplayTime = 0;
}
//-----------------------------------------------------------------------------
// Purpose: get this game's encryption key for decoding weapon kv files
// Output : virtual const unsigned char
//-----------------------------------------------------------------------------
const unsigned char *CBaseCombatWeapon::GetEncryptionKey( void )
{
return g_pGameRules->GetEncryptionKey();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::Precache( void )
{
#if defined( CLIENT_DLL )
Assert( Q_strlen( GetClassname() ) > 0 );
// Msg( "Client got %s\n", GetClassname() );
#endif
m_iPrimaryAmmoType = m_iSecondaryAmmoType = -1;
// Add this weapon to the weapon registry, and get our index into it
// Get weapon data from script file
if ( ReadWeaponDataFromFileForSlot( filesystem, GetClassname(), &m_hWeaponFileInfo, GetEncryptionKey() ) )
{
// Get the ammo indexes for the ammo's specified in the data file
if ( GetWpnData().szAmmo1[0] )
{
m_iPrimaryAmmoType = GetAmmoDef()->Index( GetWpnData().szAmmo1 );
if (m_iPrimaryAmmoType == -1)
{
Msg("ERROR: Weapon (%s) using undefined primary ammo type (%s)\n",GetClassname(), GetWpnData().szAmmo1);
}
#if defined ( TF_DLL ) || defined ( TF_CLIENT_DLL )
// Ammo override
int iModUseMetalOverride = 0;
CALL_ATTRIB_HOOK_INT( iModUseMetalOverride, mod_use_metal_ammo_type );
if ( iModUseMetalOverride )
{
m_iPrimaryAmmoType = (int)TF_AMMO_METAL;
}
#endif
}
if ( GetWpnData().szAmmo2[0] )
{
m_iSecondaryAmmoType = GetAmmoDef()->Index( GetWpnData().szAmmo2 );
if (m_iSecondaryAmmoType == -1)
{
Msg("ERROR: Weapon (%s) using undefined secondary ammo type (%s)\n",GetClassname(),GetWpnData().szAmmo2);
}
}
#if defined( CLIENT_DLL )
gWR.LoadWeaponSprites( GetWeaponFileInfoHandle() );
#endif
// Precache models (preload to avoid hitch)
m_iViewModelIndex = 0;
m_iWorldModelIndex = 0;
if ( GetViewModel() && GetViewModel()[0] )
{
m_iViewModelIndex = CBaseEntity::PrecacheModel( GetViewModel() );
}
if ( GetWorldModel() && GetWorldModel()[0] )
{
m_iWorldModelIndex = CBaseEntity::PrecacheModel( GetWorldModel() );
}
// Precache sounds, too
for ( int i = 0; i < NUM_SHOOT_SOUND_TYPES; ++i )
{
const char *shootsound = GetShootSound( i );
if ( shootsound && shootsound[0] )
{
CBaseEntity::PrecacheScriptSound( shootsound );
}
}
}
else
{
// Couldn't read data file, remove myself
Warning( "Error reading weapon data file for: %s\n", GetClassname() );
// Remove( ); //don't remove, this gets released soon!
}
}
//-----------------------------------------------------------------------------
// Purpose: Get my data in the file weapon info array
//-----------------------------------------------------------------------------
const FileWeaponInfo_t &CBaseCombatWeapon::GetWpnData( void ) const
{
return *GetFileWeaponInfoFromHandle( m_hWeaponFileInfo );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetViewModel( int /*viewmodelindex = 0 -- this is ignored in the base class here*/ ) const
{
return GetWpnData().szViewModel;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetWorldModel( void ) const
{
return GetWpnData().szWorldModel;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetAnimPrefix( void ) const
{
return GetWpnData().szAnimationPrefix;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : char const
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetPrintName( void ) const
{
return GetWpnData().szPrintName;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetMaxClip1( void ) const
{
#if defined ( TF_DLL ) || defined ( TF_CLIENT_DLL )
int iModMaxClipOverride = 0;
CALL_ATTRIB_HOOK_INT( iModMaxClipOverride, mod_max_primary_clip_override );
if ( iModMaxClipOverride != 0 )
return iModMaxClipOverride;
#endif
return GetWpnData().iMaxClip1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetMaxClip2( void ) const
{
return GetWpnData().iMaxClip2;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetDefaultClip1( void ) const
{
return GetWpnData().iDefaultClip1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetDefaultClip2( void ) const
{
return GetWpnData().iDefaultClip2;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::UsesClipsForAmmo1( void ) const
{
return ( GetMaxClip1() != WEAPON_NOCLIP );
}
bool CBaseCombatWeapon::IsMeleeWeapon() const
{
return GetWpnData().m_bMeleeWeapon;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::UsesClipsForAmmo2( void ) const
{
return ( GetMaxClip2() != WEAPON_NOCLIP );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetWeight( void ) const
{
return GetWpnData().iWeight;
}
//-----------------------------------------------------------------------------
// Purpose: Whether this weapon can be autoswitched to when the player runs out
// of ammo in their current weapon or they pick this weapon up.
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::AllowsAutoSwitchTo( void ) const
{
return GetWpnData().bAutoSwitchTo;
}
//-----------------------------------------------------------------------------
// Purpose: Whether this weapon can be autoswitched away from when the player
// runs out of ammo in this weapon or picks up another weapon or ammo.
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::AllowsAutoSwitchFrom( void ) const
{
return GetWpnData().bAutoSwitchFrom;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetWeaponFlags( void ) const
{
return GetWpnData().iFlags;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetSlot( void ) const
{
return GetWpnData().iSlot;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetPosition( void ) const
{
return GetWpnData().iPosition;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetName( void ) const
{
return GetWpnData().szClassName;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteActive( void ) const
{
return GetWpnData().iconActive;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteInactive( void ) const
{
return GetWpnData().iconInactive;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteAmmo( void ) const
{
return GetWpnData().iconAmmo;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteAmmo2( void ) const
{
return GetWpnData().iconAmmo2;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteCrosshair( void ) const
{
return GetWpnData().iconCrosshair;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteAutoaim( void ) const
{
return GetWpnData().iconAutoaim;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteZoomedCrosshair( void ) const
{
return GetWpnData().iconZoomedCrosshair;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture const *CBaseCombatWeapon::GetSpriteZoomedAutoaim( void ) const
{
return GetWpnData().iconZoomedAutoaim;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char *CBaseCombatWeapon::GetShootSound( int iIndex ) const
{
return GetWpnData().aShootSounds[ iIndex ];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatWeapon::GetRumbleEffect() const
{
return GetWpnData().iRumbleEffect;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseCombatCharacter *CBaseCombatWeapon::GetOwner() const
{
return ToBaseCombatCharacter( m_hOwner.Get() );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : BaseCombatCharacter -
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::SetOwner( CBaseCombatCharacter *owner )
{
if ( !owner )
{
#ifndef CLIENT_DLL
// Make sure the weapon updates its state when it's removed from the player
// We have to force an active state change, because it's being dropped and won't call UpdateClientData()
int iOldState = m_iState;
m_iState = WEAPON_NOT_CARRIED;
OnActiveStateChanged( iOldState );
#endif
// make sure we clear out our HideThink if we have one pending
SetContextThink( NULL, 0, HIDEWEAPON_THINK_CONTEXT );
}
m_hOwner = owner;
#ifndef CLIENT_DLL
DispatchUpdateTransmitState();
#else
UpdateVisibility();
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Return false if this weapon won't let the player switch away from it
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::IsAllowedToSwitch( void )
{
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this weapon can be selected via the weapon selection
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::CanBeSelected( void )
{
if ( !VisibleInWeaponSelection() )
return false;
return HasAmmo();
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this weapon has some ammo
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::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;
CBasePlayer *player = ToBasePlayer( GetOwner() );
if ( !player )
return false;
return ( m_iClip1 > 0 || player->GetAmmoCount( m_iPrimaryAmmoType ) || m_iClip2 > 0 || player->GetAmmoCount( m_iSecondaryAmmoType ) );
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this weapon should be seen, and hence be selectable, in the weapon selection
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::VisibleInWeaponSelection( void )
{
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::HasWeaponIdleTimeElapsed( void )
{
if ( gpGlobals->curtime > m_flTimeWeaponIdle )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : time -
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::SetWeaponIdleTime( float time )
{
m_flTimeWeaponIdle = time;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : float
//-----------------------------------------------------------------------------
float CBaseCombatWeapon::GetWeaponIdleTime( void )
{
return m_flTimeWeaponIdle;
}
//-----------------------------------------------------------------------------
// Purpose: Drop/throw the weapon with the given velocity.
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::Drop( const Vector &vecVelocity )
{
#if !defined( CLIENT_DLL )
// Once somebody drops a gun, it's fair game for removal when/if
// a game_weapon_manager does a cleanup on surplus weapons in the
// world.
SetRemoveable( true );
WeaponManager_AmmoMod( this );
//If it was dropped then there's no need to respawn it.
AddSpawnFlags( SF_NORESPAWN );
StopAnimation();
StopFollowingEntity( );
SetMoveType( MOVETYPE_FLYGRAVITY );
// clear follow stuff, setup for collision
SetGravity(1.0);
m_iState = WEAPON_NOT_CARRIED;
RemoveEffects( EF_NODRAW );
FallInit();
SetGroundEntity( NULL );
SetThink( &CBaseCombatWeapon::SetPickupTouch );
SetTouch(NULL);
if( hl2_episodic.GetBool() )
{
RemoveSpawnFlags( SF_WEAPON_NO_PLAYER_PICKUP );
}
IPhysicsObject *pObj = VPhysicsGetObject();
if ( pObj != NULL )
{
AngularImpulse angImp( 200, 200, 200 );
pObj->AddVelocity( &vecVelocity, &angImp );
}
else
{
SetAbsVelocity( vecVelocity );
}
CBaseEntity *pOwner = GetOwnerEntity();
SetNextThink( gpGlobals->curtime + 1.0f );
SetOwnerEntity( NULL );
SetOwner( NULL );
// If we're not allowing to spawn due to the gamerules,
// remove myself when I'm dropped by an NPC.
if ( pOwner && pOwner->IsNPC() )
{
if ( g_pGameRules->IsAllowedToSpawn( this ) == false )
{
UTIL_Remove( this );
return;
}
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pPicker -
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::OnPickedUp( CBaseCombatCharacter *pNewOwner )
{
#if !defined( CLIENT_DLL )
RemoveEffects( EF_ITEM_BLINK );
if( pNewOwner->IsPlayer() )
{
m_OnPlayerPickup.FireOutput(pNewOwner, this);
// Play the pickup sound for 1st-person observers
CRecipientFilter filter;
for ( int i=1; i <= gpGlobals->maxClients; ++i )
{
CBasePlayer *player = UTIL_PlayerByIndex(i);
if ( player && !player->IsAlive() && player->GetObserverMode() == OBS_MODE_IN_EYE )
{
filter.AddRecipient( player );
}
}
if ( filter.GetRecipientCount() )
{
CBaseEntity::EmitSound( filter, pNewOwner->entindex(), "Player.PickupWeapon" );
}
// Robin: We don't want to delete weapons the player has picked up, so
// clear the name of the weapon. This prevents wildcards that are meant
// to find NPCs finding weapons dropped by the NPCs as well.
SetName( NULL_STRING );
}
else
{
m_OnNPCPickup.FireOutput(pNewOwner, this);
}
#ifdef HL2MP
HL2MPRules()->RemoveLevelDesignerPlacedObject( this );
#endif
// Someone picked me up, so make it so that I can't be removed.
SetRemoveable( false );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : &vecTracerSrc -
// &tr -
// iTracerType -
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::MakeTracer( const Vector &vecTracerSrc, const trace_t &tr, int iTracerType )
{
CBaseEntity *pOwner = GetOwner();
if ( pOwner == NULL )
{
BaseClass::MakeTracer( vecTracerSrc, tr, iTracerType );
return;
}
const char *pszTracerName = GetTracerType();
Vector vNewSrc = vecTracerSrc;
int iEntIndex = pOwner->entindex();
if ( g_pGameRules->IsMultiplayer() )
{
iEntIndex = entindex();
}
int iAttachment = GetTracerAttachment();
switch ( iTracerType )
{
case TRACER_LINE:
UTIL_Tracer( vNewSrc, tr.endpos, iEntIndex, iAttachment, 0.0f, true, pszTracerName );
break;
case TRACER_LINE_AND_WHIZ:
UTIL_Tracer( vNewSrc, tr.endpos, iEntIndex, iAttachment, 0.0f, true, pszTracerName );
break;
}
}
void CBaseCombatWeapon::GiveTo( CBaseEntity *pOther )
{
DefaultTouch( pOther );
}
//-----------------------------------------------------------------------------
// Purpose: Default Touch function for player picking up a weapon (not AI)
// Input : pOther - the entity that touched me
// Output :
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::DefaultTouch( CBaseEntity *pOther )
{
#if !defined( CLIENT_DLL )
// Can't pick up dissolving weapons
if ( IsDissolving() )
return;
// if it's not a player, ignore
CBasePlayer *pPlayer = ToBasePlayer(pOther);
if ( !pPlayer )
return;
if( UTIL_ItemCanBeTouchedByPlayer(this, pPlayer) )
{
// This makes sure the player could potentially take the object
// before firing the cache interaction output. That doesn't mean
// the player WILL end up taking the object, but cache interactions
// are fired as soon as you prove you have found the object, not
// when you finally acquire it.
m_OnCacheInteraction.FireOutput( pOther, this );
}
if( HasSpawnFlags(SF_WEAPON_NO_PLAYER_PICKUP) )
return;
if (pPlayer->BumpWeapon(this))
{
OnPickedUp( pPlayer );
}
#endif
}
//---------------------------------------------------------
// It's OK for base classes to override this completely
// without calling up. (sjb)
//---------------------------------------------------------
bool CBaseCombatWeapon::ShouldDisplayAltFireHUDHint()
{
if( m_iAltFireHudHintCount >= WEAPON_RELOAD_HUD_HINT_COUNT )
return false;
if( UsesSecondaryAmmo() && HasSecondaryAmmo() )
{
return true;
}
if( !UsesSecondaryAmmo() && HasPrimaryAmmo() )
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::DisplayAltFireHudHint()
{
#if !defined( CLIENT_DLL )
CFmtStr hint;
hint.sprintf( "#valve_hint_alt_%s", GetClassname() );
UTIL_HudHintText( GetOwner(), hint.Access() );
m_iAltFireHudHintCount++;
m_bAltFireHudHintDisplayed = true;
m_flHudHintMinDisplayTime = gpGlobals->curtime + MIN_HUDHINT_DISPLAY_TIME;
#endif//CLIENT_DLL
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::RescindAltFireHudHint()
{
#if !defined( CLIENT_DLL )
Assert(m_bAltFireHudHintDisplayed);
UTIL_HudHintText( GetOwner(), "" );
--m_iAltFireHudHintCount;
m_bAltFireHudHintDisplayed = false;
#endif//CLIENT_DLL
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CBaseCombatWeapon::ShouldDisplayReloadHUDHint()
{
if( m_iReloadHudHintCount >= WEAPON_RELOAD_HUD_HINT_COUNT )
return false;
CBaseCombatCharacter *pOwner = GetOwner();
if( pOwner != NULL && pOwner->IsPlayer() && UsesClipsForAmmo1() && m_iClip1 < (GetMaxClip1() / 2) )
{
// I'm owned by a player, I use clips, I have less then half a clip loaded. Now, does the player have more ammo?
if ( pOwner )
{
if ( pOwner->GetAmmoCount( m_iPrimaryAmmoType ) > 0 )
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::DisplayReloadHudHint()
{
#if !defined( CLIENT_DLL )
UTIL_HudHintText( GetOwner(), "valve_hint_reload" );
m_iReloadHudHintCount++;
m_bReloadHudHintDisplayed = true;
m_flHudHintMinDisplayTime = gpGlobals->curtime + MIN_HUDHINT_DISPLAY_TIME;
#endif//CLIENT_DLL
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::RescindReloadHudHint()
{
#if !defined( CLIENT_DLL )
Assert(m_bReloadHudHintDisplayed);
UTIL_HudHintText( GetOwner(), "" );
--m_iReloadHudHintCount;
m_bReloadHudHintDisplayed = false;
#endif//CLIENT_DLL
}
void CBaseCombatWeapon::SetPickupTouch( void )
{
#if !defined( CLIENT_DLL )
SetTouch(&CBaseCombatWeapon::DefaultTouch);
if ( gpGlobals->maxClients > 1 )
{
if ( GetSpawnFlags() & SF_NORESPAWN )
{
SetThink( &CBaseEntity::SUB_Remove );
SetNextThink( gpGlobals->curtime + 30.0f );
}
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Become a child of the owner (MOVETYPE_FOLLOW)
// disables collisions, touch functions, thinking
// Input : *pOwner - new owner/operator
//-----------------------------------------------------------------------------
void CBaseCombatWeapon::Equip( CBaseCombatCharacter *pOwner )
{
// Attach the weapon to an owner
SetAbsVelocity( vec3_origin );
RemoveSolidFlags( FSOLID_TRIGGER );
FollowEntity( pOwner );
SetOwner( pOwner );
SetOwnerEntity( pOwner );
// Break any constraint I might have to the world.
RemoveEffects( EF_ITEM_BLINK );
#if !defined( CLIENT_DLL )
if ( m_pConstraint != NULL )
{
RemoveSpawnFlags( SF_WEAPON_START_CONSTRAINED );
physenv->DestroyConstraint( m_pConstraint );
m_pConstraint = NULL;
}
#endif