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.

289 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: 357 - hand gun
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "npcevent.h"
  9. #include "hl1mp_basecombatweapon_shared.h"
  10. #ifdef CLIENT_DLL
  11. #include "c_baseplayer.h"
  12. #else
  13. #include "player.h"
  14. #endif
  15. #include "gamerules.h"
  16. #include "in_buttons.h"
  17. #ifndef CLIENT_DLL
  18. #include "soundent.h"
  19. #include "game.h"
  20. #endif
  21. #include "vstdlib/random.h"
  22. #include "engine/IEngineSound.h"
  23. #ifdef CLIENT_DLL
  24. #define CWeapon357 C_Weapon357
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // CWeapon357
  28. //-----------------------------------------------------------------------------
  29. class CWeapon357 : public CBaseHL1MPCombatWeapon
  30. {
  31. DECLARE_CLASS( CWeapon357, CBaseHL1CombatWeapon );
  32. public:
  33. DECLARE_NETWORKCLASS();
  34. DECLARE_PREDICTABLE();
  35. CWeapon357( void );
  36. void Precache( void );
  37. bool Deploy( void );
  38. void PrimaryAttack( void );
  39. void SecondaryAttack( void );
  40. bool Reload( void );
  41. void WeaponIdle( void );
  42. bool Holster( CBaseCombatWeapon *pSwitchingTo = NULL );
  43. // DECLARE_SERVERCLASS();
  44. // DECLARE_DATADESC();
  45. private:
  46. void ToggleZoom( void );
  47. private:
  48. CNetworkVar( float, m_fInZoom );
  49. };
  50. IMPLEMENT_NETWORKCLASS_ALIASED( Weapon357, DT_Weapon357 );
  51. BEGIN_NETWORK_TABLE( CWeapon357, DT_Weapon357 )
  52. #ifdef CLIENT_DLL
  53. RecvPropFloat( RECVINFO( m_fInZoom ) ),
  54. #else
  55. SendPropFloat( SENDINFO( m_fInZoom ) ),
  56. #endif
  57. END_NETWORK_TABLE()
  58. BEGIN_PREDICTION_DATA( CWeapon357 )
  59. #ifdef CLIENT_DLL
  60. DEFINE_PRED_FIELD( m_fInZoom, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  61. #endif
  62. END_PREDICTION_DATA()
  63. LINK_ENTITY_TO_CLASS( weapon_357, CWeapon357 );
  64. PRECACHE_WEAPON_REGISTER( weapon_357 );
  65. //IMPLEMENT_SERVERCLASS_ST( CWeapon357, DT_Weapon357 )
  66. //END_SEND_TABLE()
  67. //BEGIN_DATADESC( CWeapon357 )
  68. //END_DATADESC()
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Constructor
  71. //-----------------------------------------------------------------------------
  72. CWeapon357::CWeapon357( void )
  73. {
  74. m_bReloadsSingly = false;
  75. m_bFiresUnderwater = false;
  76. m_fInZoom = false;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. //-----------------------------------------------------------------------------
  81. void CWeapon357::Precache( void )
  82. {
  83. BaseClass::Precache();
  84. #ifndef CLIENT_DLL
  85. UTIL_PrecacheOther( "ammo_357" );
  86. #endif
  87. }
  88. bool CWeapon357::Deploy( void )
  89. {
  90. // Bodygroup stuff not currently working correctly
  91. // if ( g_pGameRules->IsMultiplayer() )
  92. // {
  93. // enable laser sight geometry.
  94. // SetBodygroup( 4, 1 );
  95. // }
  96. // else
  97. // {
  98. // SetBodygroup( 4, 0 );
  99. // }
  100. return BaseClass::Deploy();
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. void CWeapon357::PrimaryAttack( void )
  106. {
  107. // Only the player fires this way so we can cast
  108. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  109. if ( !pPlayer )
  110. {
  111. return;
  112. }
  113. if ( m_iClip1 <= 0 )
  114. {
  115. if ( !m_bFireOnEmpty )
  116. {
  117. Reload();
  118. }
  119. else
  120. {
  121. WeaponSound( EMPTY );
  122. m_flNextPrimaryAttack = 0.15;
  123. }
  124. return;
  125. }
  126. WeaponSound( SINGLE );
  127. pPlayer->DoMuzzleFlash();
  128. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  129. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  130. m_flNextPrimaryAttack = gpGlobals->curtime + 0.75;
  131. m_flNextSecondaryAttack = gpGlobals->curtime + 0.75;
  132. m_iClip1--;
  133. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  134. Vector vecAiming = pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
  135. // pPlayer->FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_1DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 0 );
  136. FireBulletsInfo_t info( 1, vecSrc, vecAiming, VECTOR_CONE_1DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType );
  137. info.m_pAttacker = pPlayer;
  138. pPlayer->FireBullets( info );
  139. #ifndef CLIENT_DLL
  140. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  141. #endif
  142. pPlayer->ViewPunch( QAngle( -10, 0, 0 ) );
  143. #ifndef CLIENT_DLL
  144. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 600, 0.2 );
  145. #endif
  146. if ( !m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
  147. {
  148. // HEV suit - indicate out of ammo condition
  149. pPlayer->SetSuitUpdate( "!HEV_AMO0", FALSE, 0 );
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. //-----------------------------------------------------------------------------
  155. void CWeapon357::SecondaryAttack( void )
  156. {
  157. // only in multiplayer
  158. if ( !g_pGameRules->IsMultiplayer() )
  159. {
  160. #ifndef CLIENT_DLL
  161. // unless we have cheats on
  162. if ( !sv_cheats->GetBool() )
  163. {
  164. return;
  165. }
  166. #endif
  167. }
  168. ToggleZoom();
  169. m_flNextSecondaryAttack = gpGlobals->curtime + 0.5;
  170. }
  171. bool CWeapon357::Reload( void )
  172. {
  173. bool fRet;
  174. fRet = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
  175. if ( fRet )
  176. {
  177. if ( m_fInZoom )
  178. {
  179. ToggleZoom();
  180. }
  181. }
  182. return fRet;
  183. }
  184. void CWeapon357::WeaponIdle( void )
  185. {
  186. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  187. if ( pPlayer )
  188. {
  189. pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
  190. }
  191. if ( !HasWeaponIdleTimeElapsed() )
  192. return;
  193. if ( random->RandomFloat( 0, 1 ) <= 0.9 )
  194. {
  195. SendWeaponAnim( ACT_VM_IDLE );
  196. }
  197. else
  198. {
  199. SendWeaponAnim( ACT_VM_FIDGET );
  200. }
  201. }
  202. bool CWeapon357::Holster( CBaseCombatWeapon *pSwitchingTo )
  203. {
  204. if ( m_fInZoom )
  205. {
  206. ToggleZoom();
  207. }
  208. return BaseClass::Holster( pSwitchingTo );
  209. }
  210. void CWeapon357::ToggleZoom( void )
  211. {
  212. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  213. if ( !pPlayer )
  214. {
  215. return;
  216. }
  217. #ifndef CLIENT_DLL
  218. if ( m_fInZoom )
  219. {
  220. if ( pPlayer->SetFOV( this, 0 ) )
  221. {
  222. m_fInZoom = false;
  223. pPlayer->ShowViewModel( true );
  224. }
  225. }
  226. else
  227. {
  228. if ( pPlayer->SetFOV( this, 40 ) )
  229. {
  230. m_fInZoom = true;
  231. pPlayer->ShowViewModel( false );
  232. }
  233. }
  234. #endif
  235. }