Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
18 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef WEAPON_CSBASE_H
  7. #define WEAPON_CSBASE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #if !defined (_GAMECONSOLE)
  12. #include "econ_item_view.h"
  13. #endif
  14. #include "cs_playeranimstate.h"
  15. #include "cs_weapon_parse.h"
  16. #include "cs_shareddefs.h"
  17. #ifdef CLIENT_DLL
  18. #include "glow_outline_effect.h"
  19. #endif
  20. #ifdef IRONSIGHT
  21. #include "weapon_ironsightcontroller.h"
  22. #endif //IRONSIGHT
  23. #if defined( CLIENT_DLL )
  24. #define CWeaponCSBase C_WeaponCSBase
  25. #endif
  26. // extern int ClassnameToWeaponID( const char *classname );
  27. extern CSWeaponID AliasToWeaponID( const char *alias );
  28. extern const char *WeaponIDToAlias( int id );
  29. extern const char *GetTranslatedWeaponAlias( const char *alias);
  30. extern const char * GetWeaponAliasFromTranslated(const char *translatedAlias);
  31. extern bool IsPrimaryWeapon( CSWeaponID id );
  32. extern bool IsSecondaryWeapon( CSWeaponID id );
  33. extern bool IsGrenadeWeapon( CSWeaponID id );
  34. extern bool IsArmor( CSWeaponID id );
  35. extern int GetShellForAmmoType( const char *ammoname );
  36. #define SHIELD_VIEW_MODEL "models/weapons/v_shield.mdl"
  37. #define SHIELD_WORLD_MODEL "models/weapons/w_shield.mdl"
  38. class CCSPlayer;
  39. // These are the names of the ammo types that go in the CAmmoDefs and that the
  40. // weapon script files reference.
  41. #define BULLET_PLAYER_50AE "BULLET_PLAYER_50AE"
  42. #define BULLET_PLAYER_762MM "BULLET_PLAYER_762MM"
  43. #define BULLET_PLAYER_556MM "BULLET_PLAYER_556MM"
  44. #define BULLET_PLAYER_556MM_SMALL "BULLET_PLAYER_556MM_SMALL"
  45. #define BULLET_PLAYER_556MM_BOX "BULLET_PLAYER_556MM_BOX"
  46. #define BULLET_PLAYER_338MAG "BULLET_PLAYER_338MAG"
  47. #define BULLET_PLAYER_9MM "BULLET_PLAYER_9MM"
  48. #define BULLET_PLAYER_BUCKSHOT "BULLET_PLAYER_BUCKSHOT"
  49. #define BULLET_PLAYER_45ACP "BULLET_PLAYER_45ACP"
  50. #define BULLET_PLAYER_357SIG "BULLET_PLAYER_357SIG"
  51. #define BULLET_PLAYER_357SIG_P250 "BULLET_PLAYER_357SIG_P250"
  52. #define BULLET_PLAYER_357SIG_SMALL "BULLET_PLAYER_357SIG_SMALL"
  53. #define BULLET_PLAYER_357SIG_MIN "BULLET_PLAYER_357SIG_MIN"
  54. #define BULLET_PLAYER_57MM "BULLET_PLAYER_57MM"
  55. #define AMMO_TYPE_HEGRENADE "AMMO_TYPE_HEGRENADE"
  56. #define AMMO_TYPE_FLASHBANG "AMMO_TYPE_FLASHBANG"
  57. #define AMMO_TYPE_SMOKEGRENADE "AMMO_TYPE_SMOKEGRENADE"
  58. #define AMMO_TYPE_DECOY "AMMO_TYPE_DECOY"
  59. #define AMMO_TYPE_MOLOTOV "AMMO_TYPE_MOLOTOV"
  60. #define AMMO_TYPE_TASERCHARGE "AMMO_TYPE_TASERCHARGE"
  61. #define AMMO_TYPE_HEALTHSHOT "AMMO_TYPE_HEALTHSHOT"
  62. #define AMMO_TYPE_TAGRENADE "AMMO_TYPE_TAGRENADE"
  63. #define CROSSHAIR_CONTRACT_PIXELS_PER_SECOND 7.0f
  64. // Given an ammo type (like from a weapon's GetPrimaryAmmoType()), this compares it
  65. // against the ammo name you specify.
  66. // MIKETODO: this should use indexing instead of searching and strcmp()'ing all the time.
  67. bool IsAmmoType( int iAmmoType, const char *pAmmoName );
  68. enum CSWeaponMode
  69. {
  70. Primary_Mode = 0,
  71. Secondary_Mode,
  72. WeaponMode_MAX
  73. };
  74. // structure to encapsulate state of head bob
  75. struct BobState_t
  76. {
  77. BobState_t()
  78. {
  79. m_flBobTime = 0;
  80. m_flLastBobTime = 0;
  81. m_flLastSpeed = 0;
  82. m_flVerticalBob = 0;
  83. m_flLateralBob = 0;
  84. m_flRawVerticalBob = 0;
  85. m_flRawLateralBob = 0;
  86. }
  87. float m_flBobTime;
  88. float m_flLastBobTime;
  89. float m_flLastSpeed;
  90. float m_flVerticalBob;
  91. float m_flLateralBob;
  92. float m_flRawVerticalBob;
  93. float m_flRawLateralBob;
  94. };
  95. #ifdef CLIENT_DLL
  96. float CalcViewModelBobHelper( CBasePlayer *player, BobState_t *pBobState, int nVMIndex = 0 );
  97. void AddViewModelBobHelper( Vector &origin, QAngle &angles, BobState_t *pBobState );
  98. class CCSWeaponVisualsDataProcessor;
  99. #endif
  100. class CWeaponCSBase : public CBaseCombatWeapon
  101. {
  102. public:
  103. DECLARE_CLASS( CWeaponCSBase, CBaseCombatWeapon );
  104. DECLARE_NETWORKCLASS();
  105. DECLARE_PREDICTABLE();
  106. CWeaponCSBase();
  107. virtual ~CWeaponCSBase();
  108. #ifdef GAME_DLL
  109. DECLARE_DATADESC();
  110. virtual void CheckRespawn();
  111. virtual CBaseEntity* Respawn();
  112. virtual const Vector& GetBulletSpread();
  113. virtual float GetDefaultAnimSpeed();
  114. virtual void BulletWasFired( const Vector &vecStart, const Vector &vecEnd );
  115. virtual bool ShouldRemoveOnRoundRestart();
  116. virtual void OnRoundRestart();
  117. virtual bool DefaultReload( int iClipSize1, int iClipSize2, int iActivity );
  118. void SendActivityEvents( int nActEvents = ACT_VM_RELOAD );
  119. void Materialize();
  120. void AttemptToMaterialize();
  121. virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  122. virtual bool IsRemoveable();
  123. virtual void RemoveUnownedWeaponThink();
  124. #endif
  125. virtual int GetShotgunReloadState( void ) { return 0; } // only shotguns use this for multi-stage reloads
  126. virtual bool Holster( CBaseCombatWeapon *pSwitchingTo );
  127. virtual void AddViewmodelBob( CBaseViewModel *viewmodel, Vector &origin, QAngle &angles );
  128. virtual float CalcViewmodelBob( void );
  129. BobState_t *GetBobState();
  130. // All predicted weapons need to implement and return true
  131. virtual bool IsPredicted() const;
  132. bool IsPistol() const;
  133. virtual int GetWeaponPrice() const { return GetCSWpnData(). GetWeaponPrice( GetEconItemView() ); }
  134. virtual bool IsFullAuto() const { return GetCSWpnData(). IsFullAuto( GetEconItemView() ); }
  135. virtual int GetDamage() const { return GetCSWpnData(). GetDamage( GetEconItemView() ); }
  136. virtual int GetKillAward() const { return GetCSWpnData(). GetKillAward( GetEconItemView() ); }
  137. virtual float GetCycleTime( int mode = Primary_Mode ) const { return GetCSWpnData(). GetCycleTime( GetEconItemView(), mode ); }
  138. virtual float GetArmorRatio() const { return GetCSWpnData(). GetArmorRatio( GetEconItemView() ); }
  139. virtual bool HasTraditionalScope() const { return GetCSWpnData(). HasTraditionalScope( GetEconItemView() ); }
  140. virtual float GetInaccuracyStand( int mode = Primary_Mode ) const { return GetCSWpnData(). GetInaccuracyStand( GetEconItemView(), mode ); }
  141. virtual float GetInaccuracyCrouch( int mode = Primary_Mode ) const { return GetCSWpnData(). GetInaccuracyCrouch( GetEconItemView(), mode ); }
  142. virtual bool CannotShootUnderwater() const { return GetCSWpnData(). CannotShootUnderwater( GetEconItemView() ); }
  143. virtual int GetRecoilMagnitude( int mode ) const { return GetCSWpnData(). GetRecoilMagnitude( GetEconItemView(), mode ); }
  144. virtual int GetRecoilMagnitudeVariance( int mode ) const { return GetCSWpnData(). GetRecoilMagnitudeVariance( GetEconItemView(), mode ); }
  145. virtual int GetRecoilAngle( int mode ) const { return GetCSWpnData(). GetRecoilAngle( GetEconItemView(), mode ); }
  146. virtual int GetRecoilAngleVariance( int mode ) const { return GetCSWpnData(). GetRecoilAngleVariance( GetEconItemView(), mode ); }
  147. virtual float GetMaxSpeed() const { return GetCSWpnData(). GetMaxSpeed( GetEconItemView(), m_weaponMode ); } // What's the player's max speed while holding this weapon.
  148. virtual int GetZoomLevels() const { return GetCSWpnData(). GetZoomLevels( GetEconItemView() ); }
  149. virtual bool CanBeUsedWithShield() const { return GetCSWpnData(). CanBeUsedWithShield(); }
  150. virtual const char* GetZoomInSound() const { return GetCSWpnData(). GetZoomInSound(); }
  151. virtual const char* GetZoomOutSound() const { return GetCSWpnData(). GetZoomOutSound(); }
  152. virtual float GetBotAudibleRange() const { return GetCSWpnData(). GetBotAudibleRange(); }
  153. virtual const char* GetWrongTeamMsg() const { return GetCSWpnData(). GetWrongTeamMsg(); }
  154. virtual const char* GetAnimExtension() const { return GetCSWpnData(). GetAnimExtension(); }
  155. virtual const char* GetShieldViewModel() const { return GetCSWpnData(). GetShieldViewModel(); }
  156. virtual const char* GetSilencerModel() const { return GetCSWpnData(). GetSilencerModel(); }
  157. virtual float GetAddonScale() const { return GetCSWpnData(). GetAddonScale(); }
  158. virtual float GetThrowVelocity() const { return GetCSWpnData(). GetThrowVelocity(); }
  159. virtual int GetZoomFOV( int nZoomLevel ) const;
  160. virtual float GetZoomTime( int nZoomLevel ) const;
  161. virtual CSWeaponType GetWeaponType( void ) const { return GetCSWpnData().GetWeaponType( GetEconItemView() ); }
  162. virtual const char *GetDefinitionName( void ) const { return GetEconItemView()->GetStaticData()->GetDefinitionName(); }
  163. CCSPlayer* GetPlayerOwner() const;
  164. #ifdef CLIENT_DLL
  165. C_BaseEntity *GetWeaponForEffect();
  166. void UpdateOutlineGlow( void );
  167. CGlowObject m_GlowObject;
  168. #endif
  169. virtual int GetRecoilSeed( void ) const;
  170. // Get CS-specific weapon data.
  171. virtual CCSWeaponInfo const &GetCSWpnData() const;
  172. virtual int GetCSZoomLevel() { return 0; }
  173. // Get specific CS weapon ID (ie: WEAPON_AK47, etc)
  174. virtual CSWeaponID GetCSWeaponID( void ) const { return m_nWeaponID; }
  175. // return true if this weapon is an instance of the given weapon type (ie: "IsA" WEAPON_GLOCK)
  176. bool IsA( CSWeaponID id ) const { return GetCSWeaponID() == id; }
  177. // return true if this weapon is a kinf of the given weapon type (ie: "IsKindOf" WEAPONTYPE_RIFLE )
  178. bool IsKindOf( CSWeaponType type ) const { return GetCSWpnData().GetWeaponType() == type; }
  179. const char *GetTracerType( void ) { return GetCSWpnData().GetTracerEffectName( GetEconItemView() ); }
  180. loadout_positions_t GetDefaultLoadoutSlot( void ) { return (loadout_positions_t)( GetEconItemView() ? GetEconItemView()->GetItemDefinition()->GetLoadoutSlot( 0 ) : 0 ); }
  181. // return true if this weapon has a silencer equipped
  182. virtual bool IsSilenced( void ) const { return m_bSilencerOn; }
  183. // return true is this weapon is capable of being silenced
  184. // TODO: allow weapons to define this in the weapon script file and have it live in WpnData
  185. virtual bool HasSilencer( void ) const
  186. {
  187. return GetCSWpnData().HasSilencer( GetEconItemView() );
  188. }
  189. virtual void SetWeaponModelIndex( const char *pName );
  190. virtual void OnPickedUp( CBaseCombatCharacter *pNewOwner );
  191. virtual void OnJump( float fImpulse );
  192. virtual void OnLand( float fVelocity );
  193. float GetRecoveryTime( void );
  194. virtual bool HasZoom() { return false; }
  195. void CallSecondaryAttack();
  196. public:
  197. #if defined( CLIENT_DLL )
  198. virtual void ProcessMuzzleFlashEvent();
  199. virtual bool OnFireEvent( C_BaseViewModel *pViewModel, const Vector& origin, const QAngle& angles, int event, const char *options );
  200. virtual bool ShouldPredict();
  201. virtual void DrawCrosshair();
  202. virtual void OnDataChanged( DataUpdateType_t type );
  203. virtual int GetMuzzleAttachmentIndex_1stPerson( C_BaseViewModel *pViewModel );
  204. virtual int GetMuzzleAttachmentIndex_3rdPerson( void );
  205. virtual int GetEjectBrassAttachmentIndex_1stPerson( C_BaseViewModel *pViewModel );
  206. virtual int GetEjectBrassAttachmentIndex_3rdPerson( void );
  207. virtual bool DoesHideViewModelWhenZoomed( void ) { return GetCSWpnData(). DoesHideViewModelWhenZoomed( GetEconItemView() ); }
  208. float m_flCrosshairDistance;
  209. int m_iAmmoLastCheck;
  210. int m_iAlpha;
  211. int m_iScopeTextureID;
  212. int m_iCrosshairTextureID; // for white additive texture
  213. float m_flGunAccuracyPosition;
  214. virtual const char* GetMuzzleFlashEffectName_1stPerson( void );
  215. virtual const char* GetMuzzleFlashEffectName_3rdPerson( void );
  216. virtual const char* GetEjectBrassEffectName( void );
  217. virtual const char* GetHeatEffectName( void );
  218. int GetReticleWeaponSpread( void );
  219. int GetReticleCrosshairGap( void );
  220. bool WantReticleShown( void );
  221. #else
  222. virtual void Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator );
  223. virtual bool Reload();
  224. virtual void Spawn();
  225. virtual bool KeyValue( const char *szKeyName, const char *szValue );
  226. virtual bool PhysicsSplash( const Vector &centerPoint, const Vector &normal, float rawSpeed, float scaledSpeed );
  227. #endif
  228. bool IsUseable();
  229. virtual bool CanDeploy( void );
  230. virtual void UpdateShieldState( void );
  231. virtual bool SendWeaponAnim( int iActivity );
  232. virtual void SendViewModelAnim( int nSequence );
  233. virtual void SecondaryAttack( void );
  234. virtual bool CanPrimaryAttack( void ) {return true;}
  235. virtual void Precache( void );
  236. virtual bool CanBeSelected( void );
  237. virtual Activity GetDeployActivity( void );
  238. virtual bool DefaultDeploy( char *szViewModel, char *szWeaponModel, int iActivity, char *szAnimExt );
  239. virtual void DefaultTouch( CBaseEntity *pOther ); // default weapon touch
  240. virtual bool DefaultPistolReload();
  241. virtual bool Deploy();
  242. virtual void Drop( const Vector &vecVelocity );
  243. bool PlayEmptySound();
  244. virtual const char *GetShootSound( int iIndex ) const;
  245. virtual const char *GetPlayerAnimationExtension( void ) const;
  246. virtual const char *GetAddonModel( void ) const;
  247. virtual void ItemPostFrame();
  248. virtual void ItemBusyFrame();
  249. virtual const char *GetViewModel( int viewmodelindex = 0 ) const;
  250. virtual void WeaponReset( void ) {}
  251. virtual bool WeaponHasBurst() const { return false; }
  252. virtual bool IsInBurstMode() { return m_bBurstMode; }
  253. virtual bool IsRevolver() const { return false; }
  254. void ItemPostFrame_ProcessPrimaryAttack( CCSPlayer *pPlayer );
  255. bool ItemPostFrame_ProcessZoomAction( CCSPlayer *pPlayer );
  256. bool ItemPostFrame_ProcessSecondaryAttack( CCSPlayer *pPlayer );
  257. void ItemPostFrame_ProcessReloadAction( CCSPlayer *pPlayer );
  258. void ItemPostFrame_ProcessIdleNoAction( CCSPlayer *pPlayer );
  259. void ItemPostFrame_RevolverResetHaulback();
  260. CNetworkVar( CSWeaponMode, m_weaponMode);
  261. virtual float GetInaccuracy() const;
  262. #if defined( WEAPON_FIRE_BULLETS_ACCURACY_FISHTAIL_FEATURE )
  263. virtual float GetAccuracyFishtail() const { return m_fAccuracyFishtail; }
  264. virtual void SetAccuracyFishtail( float fFishtail ) { m_fAccuracyFishtail = fFishtail; }
  265. #endif
  266. virtual float GetSpread() const { return GetCSWpnData().GetSpread( GetEconItemView(), m_weaponMode.Get() ); }
  267. virtual void UpdateAccuracyPenalty();
  268. CNetworkVar( float, m_fAccuracyPenalty );
  269. #if defined( WEAPON_FIRE_BULLETS_ACCURACY_FISHTAIL_FEATURE )
  270. CNetworkVar( float, m_fAccuracyFishtail );
  271. #endif
  272. float m_fAccuracySmoothedForZoom;
  273. float m_fScopeZoomEndTime;
  274. CNetworkVar( int, m_iRecoilIndex ); // DEPRECATED. Kept for old demo compatibility.
  275. CNetworkVar( float, m_flRecoilIndex );
  276. CNetworkVar( bool, m_bBurstMode );
  277. CNetworkVar( float, m_flPostponeFireReadyTime );
  278. void ResetPostponeFireReadyTime( void ) { m_flPostponeFireReadyTime = FLT_MAX; }
  279. void SetPostponeFireReadyTime( float flFutureTime ) { m_flPostponeFireReadyTime = flFutureTime; }
  280. bool IsPostponFireReadyTimeElapsed( void ) { return (m_flPostponeFireReadyTime < gpGlobals->curtime); }
  281. virtual bool IsReloadVisuallyComplete() {return m_bReloadVisuallyComplete; }
  282. CNetworkVar( bool, m_bReloadVisuallyComplete );
  283. CNetworkVar( bool, m_bSilencerOn );
  284. CNetworkVar( float, m_flDoneSwitchingSilencer ); // soonest time switching the silencer will be complete
  285. CNetworkVar( float, m_flDroppedAtTime ); // when was this weapon last dropped
  286. bool IsSwitchingSilencer( void ) { return ( m_flDoneSwitchingSilencer >= gpGlobals->curtime ); }
  287. void SetExtraAmmoCount( int count ) { m_iExtraPrimaryAmmo = count; }
  288. int GetExtraAmmoCount( void ) const { return m_iExtraPrimaryAmmo; }
  289. void SetPreviousOwner( CCSPlayer* player ) { m_hPrevOwner = ( CBasePlayer * )player; }
  290. CCSPlayer* GetPreviousOwner() const { return ( CCSPlayer* )m_hPrevOwner.Get(); }
  291. // [tj] Accessors for the donor system
  292. void SetDonor( CCSPlayer* player ) { m_donor.Set( player ); }
  293. CCSPlayer* GetDonor() const { return m_donor.Get(); }
  294. void SetDonated(bool donated) { m_donated = true;}
  295. bool GetDonated() const { return m_donated; }
  296. //[dwenger] Accessors for the prior owner list
  297. void AddToPriorOwnerList( CCSPlayer* pPlayer );
  298. bool IsAPriorOwner( CCSPlayer* pPlayer ) const;
  299. CCSPlayer * GetOriginalOwner();
  300. CNetworkVar( int, m_iOriginalTeamNumber );
  301. int GetOriginalTeamNumber() { return m_iOriginalTeamNumber; }
  302. void AddPriorOwner( CCSPlayer* pPlayer );
  303. bool WasOwnedByTeam( int teamNumber );
  304. bool CanBePickedUp( void ) { return m_bCanBePickedUp; }
  305. #ifdef CLIENT_DLL
  306. virtual void SaveCustomMaterialsTextures( void );
  307. #endif
  308. protected:
  309. float CalculateNextAttackTime( float flCycleTime );
  310. void Recoil( CSWeaponMode weaponMode );
  311. bool m_bCanBePickedUp;
  312. private:
  313. CWeaponCSBase( const CWeaponCSBase & );
  314. virtual int GetWeaponID( void ) const { return GetCSWeaponID(); }
  315. #ifdef CLIENT_DLL
  316. void UpdateCustomMaterial( void );
  317. void CheckCustomMaterial( void );
  318. bool m_bVisualsDataSet;
  319. bool m_bOldFirstPersonSpectatedState;
  320. #endif
  321. CSWeaponID m_nWeaponID;
  322. int m_iExtraPrimaryAmmo;
  323. float m_nextOwnerTouchTime;
  324. float m_nextPrevOwnerTouchTime;
  325. CNetworkHandle( CBasePlayer, m_hPrevOwner );
  326. int m_iDefaultExtraAmmo;
  327. // [dwenger] track all prior owners of this weapon
  328. CUtlVector< CCSPlayer* > m_PriorOwners;
  329. // [tj] To keep track of people who drop weapons for teammates during the buy round
  330. CHandle<CCSPlayer> m_donor;
  331. bool m_donated;
  332. void ResetGunHeat( void );
  333. void UpdateGunHeat( float heat, int iAttachmentIndex );
  334. CNetworkVar( float, m_fLastShotTime );
  335. bool m_bWasOwnedByCT;
  336. bool m_bWasOwnedByTerrorist;
  337. #ifdef CLIENT_DLL
  338. int GetViewModelSplitScreenSlot( C_BaseViewModel *pViewModel );
  339. // Smoke effect variables.
  340. float m_gunHeat;
  341. unsigned int m_smokeAttachments;
  342. float m_lastSmokeTime;
  343. #else
  344. bool m_bFiredOutOfAmmoEvent;
  345. int m_numRemoveUnownedWeaponThink;
  346. #endif
  347. public:
  348. #ifdef CLIENT_DLL
  349. float m_flLastClientFireBulletTime;
  350. #endif
  351. #ifdef IRONSIGHT
  352. CIronSightController *GetIronSightController( void );
  353. void UpdateIronSightController( void );
  354. CIronSightController *m_IronSightController;
  355. CNetworkVar( int, m_iIronSightMode );
  356. #endif //IRONSIGHT
  357. };
  358. extern ConVar weapon_recoil_decay1;
  359. extern ConVar weapon_recoil_decay2_exp;
  360. extern ConVar weapon_recoil_decay2_lin;
  361. extern ConVar weapon_recoil_vel_decay;
  362. extern ConVar weapon_recoil_extra;
  363. extern ConVar weapon_recoil_scale;
  364. extern ConVar weapon_recoil_scale_motion_controller;
  365. #endif // WEAPON_CSBASE_H