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.

237 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. //#include "basecombatweapon.h"
  9. #include "hl1mp_basecombatweapon_shared.h"
  10. #include "npcevent.h"
  11. //#include "basecombatcharacter.h"
  12. //#include "AI_BaseNPC.h"
  13. #ifdef CLIENT_DLL
  14. #include "hl1/hl1_c_player.h"
  15. #else
  16. #include "player.h"
  17. #endif
  18. #include "hl1mp_weapon_mp5.h"
  19. //#include "hl1_weapon_mp5.h"
  20. #ifdef CLIENT_DLL
  21. #else
  22. #include "hl1_grenade_mp5.h"
  23. #endif
  24. #include "gamerules.h"
  25. #ifdef CLIENT_DLL
  26. #else
  27. #include "soundent.h"
  28. #include "game.h"
  29. #endif
  30. #include "in_buttons.h"
  31. #include "engine/IEngineSound.h"
  32. extern ConVar sk_plr_dmg_mp5_grenade;
  33. extern ConVar sk_max_mp5_grenade;
  34. extern ConVar sk_mp5_grenade_radius;
  35. //=========================================================
  36. //=========================================================
  37. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponMP5, DT_WeaponMP5 );
  38. BEGIN_NETWORK_TABLE( CWeaponMP5, DT_WeaponMP5 )
  39. END_NETWORK_TABLE()
  40. BEGIN_PREDICTION_DATA( CWeaponMP5 )
  41. END_PREDICTION_DATA()
  42. LINK_ENTITY_TO_CLASS( weapon_mp5, CWeaponMP5 );
  43. PRECACHE_WEAPON_REGISTER(weapon_mp5);
  44. //IMPLEMENT_SERVERCLASS_ST( CWeaponMP5, DT_WeaponMP5 )
  45. //END_SEND_TABLE()
  46. BEGIN_DATADESC( CWeaponMP5 )
  47. END_DATADESC()
  48. CWeaponMP5::CWeaponMP5( )
  49. {
  50. m_bReloadsSingly = false;
  51. m_bFiresUnderwater = false;
  52. }
  53. void CWeaponMP5::Precache( void )
  54. {
  55. BaseClass::Precache();
  56. #ifndef CLIENT_DLL
  57. UTIL_PrecacheOther( "grenade_mp5" );
  58. #endif
  59. }
  60. void CWeaponMP5::PrimaryAttack( void )
  61. {
  62. // Only the player fires this way so we can cast
  63. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  64. if ( !pPlayer )
  65. {
  66. return;
  67. }
  68. if ( m_iClip1 <= 0 )
  69. {
  70. DryFire();
  71. return;
  72. }
  73. WeaponSound( SINGLE );
  74. pPlayer->DoMuzzleFlash();
  75. m_iClip1--;
  76. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  77. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  78. m_flNextPrimaryAttack = gpGlobals->curtime + 0.1;
  79. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  80. Vector vecAiming = pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
  81. if ( !g_pGameRules->IsMultiplayer() )
  82. {
  83. // optimized multiplayer. Widened to make it easier to hit a moving player
  84. // pPlayer->FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_6DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 2 );
  85. FireBulletsInfo_t info( 1, vecSrc, vecAiming, VECTOR_CONE_6DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType );
  86. info.m_pAttacker = pPlayer;
  87. info.m_iTracerFreq = 2;
  88. pPlayer->FireBullets( info );
  89. }
  90. else
  91. {
  92. // single player spread
  93. // pPlayer->FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_3DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 2 );
  94. FireBulletsInfo_t info( 1, vecSrc, vecAiming, VECTOR_CONE_3DEGREES, MAX_TRACE_LENGTH, m_iPrimaryAmmoType );
  95. info.m_pAttacker = pPlayer;
  96. info.m_iTracerFreq = 2;
  97. pPlayer->FireBullets( info );
  98. }
  99. EjectShell( pPlayer, 0 );
  100. pPlayer->ViewPunch( QAngle( random->RandomFloat( -2, 2 ), 0, 0 ) );
  101. #ifdef CLIENT_DLL
  102. pPlayer->DoMuzzleFlash();
  103. #else
  104. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  105. #endif
  106. #ifdef CLIENT_DLL
  107. #else
  108. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 600, 0.2 );
  109. #endif
  110. if ( !m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
  111. {
  112. // HEV suit - indicate out of ammo condition
  113. pPlayer->SetSuitUpdate("!HEV_AMO0", FALSE, 0);
  114. }
  115. SetWeaponIdleTime( gpGlobals->curtime + random->RandomFloat( 10, 15 ) );
  116. }
  117. void CWeaponMP5::SecondaryAttack( void )
  118. {
  119. // Only the player fires this way so we can cast
  120. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  121. if (!pPlayer)
  122. {
  123. return;
  124. }
  125. if ( pPlayer->GetAmmoCount( m_iSecondaryAmmoType ) <= 0 )
  126. {
  127. DryFire( );
  128. return;
  129. }
  130. WeaponSound(WPN_DOUBLE);
  131. pPlayer->DoMuzzleFlash();
  132. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  133. Vector vecThrow = pPlayer->GetAutoaimVector( 0 ) * 800;
  134. QAngle angGrenAngle;
  135. VectorAngles( vecThrow, angGrenAngle );
  136. #ifdef CLIENT_DLL
  137. #else
  138. CGrenadeMP5 * m_pMyGrenade = (CGrenadeMP5*)Create( "grenade_mp5", vecSrc, angGrenAngle, GetOwner() );
  139. m_pMyGrenade->SetAbsVelocity( vecThrow );
  140. m_pMyGrenade->SetLocalAngularVelocity( QAngle( random->RandomFloat( -100, -500 ), 0, 0 ) );
  141. m_pMyGrenade->SetMoveType( MOVETYPE_FLYGRAVITY );
  142. m_pMyGrenade->SetThrower( GetOwner() );
  143. m_pMyGrenade->SetDamage( sk_plr_dmg_mp5_grenade.GetFloat() * g_pGameRules->GetDamageMultiplier() );
  144. #endif
  145. SendWeaponAnim( ACT_VM_SECONDARYATTACK );
  146. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  147. pPlayer->ViewPunch( QAngle( -10, 0, 0 ) );
  148. // Register a muzzleflash for the AI.
  149. #if !defined(CLIENT_DLL)
  150. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  151. #endif
  152. #ifdef CLIENT_DLL
  153. #else
  154. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 600, 0.2 );
  155. #endif
  156. // Decrease ammo
  157. pPlayer->RemoveAmmo( 1, m_iSecondaryAmmoType );
  158. if ( pPlayer->GetAmmoCount( m_iSecondaryAmmoType ) <= 0 )
  159. {
  160. // HEV suit - indicate out of ammo condition
  161. pPlayer->SetSuitUpdate("!HEV_AMO0", FALSE, 0);
  162. }
  163. m_flNextPrimaryAttack = gpGlobals->curtime + 1.0;
  164. m_flNextSecondaryAttack = gpGlobals->curtime + 1.0;
  165. SetWeaponIdleTime( gpGlobals->curtime + 5.0 );
  166. }
  167. void CWeaponMP5::DryFire( void )
  168. {
  169. WeaponSound( EMPTY );
  170. m_flNextPrimaryAttack = gpGlobals->curtime + 0.15;
  171. m_flNextSecondaryAttack = gpGlobals->curtime + 0.15;
  172. }
  173. void CWeaponMP5::WeaponIdle( void )
  174. {
  175. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  176. if ( pPlayer )
  177. {
  178. pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
  179. }
  180. bool bElapsed = HasWeaponIdleTimeElapsed();
  181. BaseClass::WeaponIdle();
  182. if( bElapsed )
  183. SetWeaponIdleTime( gpGlobals->curtime + random->RandomInt( 3, 5 ) );
  184. }