Team Fortress 2 Source Code as on 22/4/2020
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.

339 lines
9.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "npcevent.h"
  8. #include "in_buttons.h"
  9. #ifdef CLIENT_DLL
  10. #include "c_hl2mp_player.h"
  11. #else
  12. #include "hl2mp_player.h"
  13. #endif
  14. #include "weapon_hl2mpbasehlmpcombatweapon.h"
  15. #define PISTOL_FASTEST_REFIRE_TIME 0.1f
  16. #define PISTOL_FASTEST_DRY_REFIRE_TIME 0.2f
  17. #define PISTOL_ACCURACY_SHOT_PENALTY_TIME 0.2f // Applied amount of time each shot adds to the time we must recover from
  18. #define PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME 1.5f // Maximum penalty to deal out
  19. #ifdef CLIENT_DLL
  20. #define CWeaponPistol C_WeaponPistol
  21. #endif
  22. //-----------------------------------------------------------------------------
  23. // CWeaponPistol
  24. //-----------------------------------------------------------------------------
  25. class CWeaponPistol : public CBaseHL2MPCombatWeapon
  26. {
  27. public:
  28. DECLARE_CLASS( CWeaponPistol, CBaseHL2MPCombatWeapon );
  29. CWeaponPistol(void);
  30. DECLARE_NETWORKCLASS();
  31. DECLARE_PREDICTABLE();
  32. void Precache( void );
  33. void ItemPostFrame( void );
  34. void ItemPreFrame( void );
  35. void ItemBusyFrame( void );
  36. void PrimaryAttack( void );
  37. void AddViewKick( void );
  38. void DryFire( void );
  39. void UpdatePenaltyTime( void );
  40. Activity GetPrimaryAttackActivity( void );
  41. virtual bool Reload( void );
  42. virtual const Vector& GetBulletSpread( void )
  43. {
  44. static Vector cone;
  45. float ramp = RemapValClamped( m_flAccuracyPenalty,
  46. 0.0f,
  47. PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME,
  48. 0.0f,
  49. 1.0f );
  50. // We lerp from very accurate to inaccurate over time
  51. VectorLerp( VECTOR_CONE_1DEGREES, VECTOR_CONE_6DEGREES, ramp, cone );
  52. return cone;
  53. }
  54. virtual int GetMinBurst()
  55. {
  56. return 1;
  57. }
  58. virtual int GetMaxBurst()
  59. {
  60. return 3;
  61. }
  62. virtual float GetFireRate( void )
  63. {
  64. return 0.5f;
  65. }
  66. #ifndef CLIENT_DLL
  67. DECLARE_ACTTABLE();
  68. #endif
  69. private:
  70. CNetworkVar( float, m_flSoonestPrimaryAttack );
  71. CNetworkVar( float, m_flLastAttackTime );
  72. CNetworkVar( float, m_flAccuracyPenalty );
  73. CNetworkVar( int, m_nNumShotsFired );
  74. private:
  75. CWeaponPistol( const CWeaponPistol & );
  76. };
  77. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponPistol, DT_WeaponPistol )
  78. BEGIN_NETWORK_TABLE( CWeaponPistol, DT_WeaponPistol )
  79. #ifdef CLIENT_DLL
  80. RecvPropTime( RECVINFO( m_flSoonestPrimaryAttack ) ),
  81. RecvPropTime( RECVINFO( m_flLastAttackTime ) ),
  82. RecvPropFloat( RECVINFO( m_flAccuracyPenalty ) ),
  83. RecvPropInt( RECVINFO( m_nNumShotsFired ) ),
  84. #else
  85. SendPropTime( SENDINFO( m_flSoonestPrimaryAttack ) ),
  86. SendPropTime( SENDINFO( m_flLastAttackTime ) ),
  87. SendPropFloat( SENDINFO( m_flAccuracyPenalty ) ),
  88. SendPropInt( SENDINFO( m_nNumShotsFired ) ),
  89. #endif
  90. END_NETWORK_TABLE()
  91. #ifdef CLIENT_DLL
  92. BEGIN_PREDICTION_DATA( CWeaponPistol )
  93. DEFINE_PRED_FIELD( m_flSoonestPrimaryAttack, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  94. DEFINE_PRED_FIELD( m_flLastAttackTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  95. DEFINE_PRED_FIELD( m_flAccuracyPenalty, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  96. DEFINE_PRED_FIELD( m_nNumShotsFired, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
  97. END_PREDICTION_DATA()
  98. #endif
  99. LINK_ENTITY_TO_CLASS( weapon_pistol, CWeaponPistol );
  100. PRECACHE_WEAPON_REGISTER( weapon_pistol );
  101. #ifndef CLIENT_DLL
  102. acttable_t CWeaponPistol::m_acttable[] =
  103. {
  104. { ACT_HL2MP_IDLE, ACT_HL2MP_IDLE_PISTOL, false },
  105. { ACT_HL2MP_RUN, ACT_HL2MP_RUN_PISTOL, false },
  106. { ACT_HL2MP_IDLE_CROUCH, ACT_HL2MP_IDLE_CROUCH_PISTOL, false },
  107. { ACT_HL2MP_WALK_CROUCH, ACT_HL2MP_WALK_CROUCH_PISTOL, false },
  108. { ACT_HL2MP_GESTURE_RANGE_ATTACK, ACT_HL2MP_GESTURE_RANGE_ATTACK_PISTOL, false },
  109. { ACT_HL2MP_GESTURE_RELOAD, ACT_HL2MP_GESTURE_RELOAD_PISTOL, false },
  110. { ACT_HL2MP_JUMP, ACT_HL2MP_JUMP_PISTOL, false },
  111. { ACT_RANGE_ATTACK1, ACT_RANGE_ATTACK_PISTOL, false },
  112. };
  113. IMPLEMENT_ACTTABLE( CWeaponPistol );
  114. #endif
  115. //-----------------------------------------------------------------------------
  116. // Purpose: Constructor
  117. //-----------------------------------------------------------------------------
  118. CWeaponPistol::CWeaponPistol( void )
  119. {
  120. m_flSoonestPrimaryAttack = gpGlobals->curtime;
  121. m_flAccuracyPenalty = 0.0f;
  122. m_fMinRange1 = 24;
  123. m_fMaxRange1 = 1500;
  124. m_fMinRange2 = 24;
  125. m_fMaxRange2 = 200;
  126. m_bFiresUnderwater = true;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose:
  130. //-----------------------------------------------------------------------------
  131. void CWeaponPistol::Precache( void )
  132. {
  133. BaseClass::Precache();
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. //-----------------------------------------------------------------------------
  138. void CWeaponPistol::DryFire( void )
  139. {
  140. WeaponSound( EMPTY );
  141. SendWeaponAnim( ACT_VM_DRYFIRE );
  142. m_flSoonestPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_DRY_REFIRE_TIME;
  143. m_flNextPrimaryAttack = gpGlobals->curtime + SequenceDuration();
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose:
  147. //-----------------------------------------------------------------------------
  148. void CWeaponPistol::PrimaryAttack( void )
  149. {
  150. if ( ( gpGlobals->curtime - m_flLastAttackTime ) > 0.5f )
  151. {
  152. m_nNumShotsFired = 0;
  153. }
  154. else
  155. {
  156. m_nNumShotsFired++;
  157. }
  158. m_flLastAttackTime = gpGlobals->curtime;
  159. m_flSoonestPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_REFIRE_TIME;
  160. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  161. if( pOwner )
  162. {
  163. // Each time the player fires the pistol, reset the view punch. This prevents
  164. // the aim from 'drifting off' when the player fires very quickly. This may
  165. // not be the ideal way to achieve this, but it's cheap and it works, which is
  166. // great for a feature we're evaluating. (sjb)
  167. pOwner->ViewPunchReset();
  168. }
  169. BaseClass::PrimaryAttack();
  170. // Add an accuracy penalty which can move past our maximum penalty time if we're really spastic
  171. m_flAccuracyPenalty += PISTOL_ACCURACY_SHOT_PENALTY_TIME;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. //-----------------------------------------------------------------------------
  176. void CWeaponPistol::UpdatePenaltyTime( void )
  177. {
  178. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  179. if ( pOwner == NULL )
  180. return;
  181. // Check our penalty time decay
  182. if ( ( ( pOwner->m_nButtons & IN_ATTACK ) == false ) && ( m_flSoonestPrimaryAttack < gpGlobals->curtime ) )
  183. {
  184. m_flAccuracyPenalty -= gpGlobals->frametime;
  185. m_flAccuracyPenalty = clamp( m_flAccuracyPenalty, 0.0f, PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME );
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Purpose:
  190. //-----------------------------------------------------------------------------
  191. void CWeaponPistol::ItemPreFrame( void )
  192. {
  193. UpdatePenaltyTime();
  194. BaseClass::ItemPreFrame();
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Purpose:
  198. //-----------------------------------------------------------------------------
  199. void CWeaponPistol::ItemBusyFrame( void )
  200. {
  201. UpdatePenaltyTime();
  202. BaseClass::ItemBusyFrame();
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Purpose: Allows firing as fast as button is pressed
  206. //-----------------------------------------------------------------------------
  207. void CWeaponPistol::ItemPostFrame( void )
  208. {
  209. BaseClass::ItemPostFrame();
  210. if ( m_bInReload )
  211. return;
  212. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  213. if ( pOwner == NULL )
  214. return;
  215. if ( pOwner->m_nButtons & IN_ATTACK2 )
  216. {
  217. m_flLastAttackTime = gpGlobals->curtime + PISTOL_FASTEST_REFIRE_TIME;
  218. m_flSoonestPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_REFIRE_TIME;
  219. m_flNextPrimaryAttack = gpGlobals->curtime + PISTOL_FASTEST_REFIRE_TIME;
  220. }
  221. //Allow a refire as fast as the player can click
  222. if ( ( ( pOwner->m_nButtons & IN_ATTACK ) == false ) && ( m_flSoonestPrimaryAttack < gpGlobals->curtime ) )
  223. {
  224. m_flNextPrimaryAttack = gpGlobals->curtime - 0.1f;
  225. }
  226. else if ( ( pOwner->m_nButtons & IN_ATTACK ) && ( m_flNextPrimaryAttack < gpGlobals->curtime ) && ( m_iClip1 <= 0 ) )
  227. {
  228. DryFire();
  229. }
  230. }
  231. //-----------------------------------------------------------------------------
  232. // Purpose:
  233. // Output : int
  234. //-----------------------------------------------------------------------------
  235. Activity CWeaponPistol::GetPrimaryAttackActivity( void )
  236. {
  237. if ( m_nNumShotsFired < 1 )
  238. return ACT_VM_PRIMARYATTACK;
  239. if ( m_nNumShotsFired < 2 )
  240. return ACT_VM_RECOIL1;
  241. if ( m_nNumShotsFired < 3 )
  242. return ACT_VM_RECOIL2;
  243. return ACT_VM_RECOIL3;
  244. }
  245. //-----------------------------------------------------------------------------
  246. //-----------------------------------------------------------------------------
  247. bool CWeaponPistol::Reload( void )
  248. {
  249. bool fRet = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
  250. if ( fRet )
  251. {
  252. WeaponSound( RELOAD );
  253. m_flAccuracyPenalty = 0.0f;
  254. }
  255. return fRet;
  256. }
  257. //-----------------------------------------------------------------------------
  258. // Purpose:
  259. //-----------------------------------------------------------------------------
  260. void CWeaponPistol::AddViewKick( void )
  261. {
  262. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  263. if ( pPlayer == NULL )
  264. return;
  265. QAngle viewPunch;
  266. viewPunch.x = SharedRandomFloat( "pistolpax", 0.25f, 0.5f );
  267. viewPunch.y = SharedRandomFloat( "pistolpay", -.6f, .6f );
  268. viewPunch.z = 0.0f;
  269. //Add it to the view punch
  270. pPlayer->ViewPunch( viewPunch );
  271. }