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.

273 lines
7.2 KiB

  1. //========= Copyright � 1996-2009, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: base class for belt items, eg pills and adrenaline
  4. //
  5. // $NoKeywords: $
  6. //=====================================================================================//
  7. #include "cbase.h"
  8. #include "weapon_baseitem.h"
  9. #include "cs_gamerules.h"
  10. #if defined( CLIENT_DLL )
  11. #include "c_cs_player.h"
  12. #include "HUD/sfweaponselection.h"
  13. #else
  14. #include "cs_player.h"
  15. #endif // CLIENT_DLL
  16. // NOTE: This has to be the last file included!
  17. #include "tier0/memdbgon.h"
  18. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponBaseItem, DT_WeaponBaseItem )
  19. BEGIN_NETWORK_TABLE( CWeaponBaseItem, DT_WeaponBaseItem )
  20. #ifndef CLIENT_DLL
  21. SendPropBool( SENDINFO( m_bRedraw ) ),
  22. #else
  23. RecvPropBool( RECVINFO( m_bRedraw ) ),
  24. #endif
  25. END_NETWORK_TABLE()
  26. #if defined CLIENT_DLL
  27. BEGIN_PREDICTION_DATA( CWeaponBaseItem )
  28. DEFINE_PRED_FIELD( m_bRedraw, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
  29. END_PREDICTION_DATA()
  30. #endif
  31. #ifndef CLIENT_DLL
  32. BEGIN_DATADESC( CWeaponBaseItem )
  33. DEFINE_FIELD( m_bRedraw, FIELD_BOOLEAN ),
  34. END_DATADESC()
  35. #endif
  36. CWeaponBaseItem::CWeaponBaseItem()
  37. {
  38. m_bRedraw = false;
  39. }
  40. //--------------------------------------------------------------------------------------------------------
  41. void CWeaponBaseItem::Spawn( void )
  42. {
  43. m_UseTimer.Invalidate();
  44. BaseClass::Spawn();
  45. SetCollisionGroup( COLLISION_GROUP_WEAPON );
  46. }
  47. //--------------------------------------------------------------------------------------------------------
  48. bool CWeaponBaseItem::Deploy( void )
  49. {
  50. m_bRedraw = false;
  51. m_UseTimer.Invalidate();
  52. return BaseClass::Deploy();
  53. }
  54. //--------------------------------------------------------------------------------------------------------
  55. bool CWeaponBaseItem::Holster( CBaseCombatWeapon *pSwitchingTo )
  56. {
  57. m_bRedraw = false;
  58. m_UseTimer.Invalidate();
  59. return BaseClass::Holster( pSwitchingTo );
  60. }
  61. //--------------------------------------------------------------------------------------------------
  62. // bool CWeaponBaseItem::CanExtendHelpingHand( void ) const
  63. // {
  64. // return !m_UseTimer.HasStarted() && BaseClass::CanExtendHelpingHand();
  65. // }
  66. //--------------------------------------------------------------------------------------------------------
  67. void CWeaponBaseItem::PrimaryAttack( void )
  68. {
  69. CCSPlayer *pPlayer = ToCSPlayer( GetPlayerOwner() );
  70. if (pPlayer == NULL)
  71. return;
  72. if ( m_UseTimer.HasStarted() )
  73. {
  74. return;
  75. }
  76. // if ( HelpingHandPrimaryAttack() )
  77. // {
  78. // return;
  79. // }
  80. if ( !CanUseOnSelf( pPlayer ) )
  81. return;
  82. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  83. pPlayer->DoAnimationEvent( PLAYERANIMEVENT_FIRE_GUN_PRIMARY );
  84. OnStartUse( pPlayer );
  85. m_UseTimer.Start( GetUseTimerDuration() );
  86. }
  87. float CWeaponBaseItem::GetUseTimerDuration( void )
  88. {
  89. return SequenceDuration();
  90. }
  91. extern ConVar z_use_belt_item_tolerance;
  92. //--------------------------------------------------------------------------------------------------------
  93. void CWeaponBaseItem::SecondaryAttack( void )
  94. {
  95. CCSPlayer *pPlayer = ToCSPlayer( GetPlayerOwner() );
  96. if (pPlayer == NULL)
  97. return;
  98. if ( m_UseTimer.HasStarted() )
  99. return;
  100. // static const float GiveRange = 256.0f;
  101. // CCSPlayer *target = ToCSPlayer( pPlayer->FindUseEntity( GiveRange, 0.0f, z_use_belt_item_tolerance.GetFloat(), NULL, true ) ); // Prefer to hit players
  102. // if ( target && target->IsOnASurvivorTeam() && !target->IsIncapacitated() )
  103. // {
  104. // #ifdef GAME_DLL
  105. // pPlayer->GiveActiveWeapon( target );
  106. // #endif
  107. // return;
  108. // }
  109. BaseClass::SecondaryAttack();
  110. }
  111. //--------------------------------------------------------------------------------------------------------
  112. /**
  113. * Called when no buttons are pressed
  114. */
  115. void CWeaponBaseItem::WeaponIdle( void )
  116. {
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. // Output : Returns true on success, false on failure.
  121. //-----------------------------------------------------------------------------
  122. bool CWeaponBaseItem::Reload()
  123. {
  124. if ( ( m_bRedraw ) && ( m_flNextPrimaryAttack <= gpGlobals->curtime ) && ( m_flNextSecondaryAttack <= gpGlobals->curtime ) )
  125. {
  126. //Redraw the weapon
  127. SendWeaponAnim( ACT_VM_DRAW );
  128. //Update our times
  129. m_flNextPrimaryAttack = gpGlobals->curtime + SequenceDuration();
  130. m_flNextSecondaryAttack = gpGlobals->curtime + SequenceDuration();
  131. SetWeaponIdleTime( gpGlobals->curtime + SequenceDuration() );
  132. //Mark this as done
  133. // m_bRedraw = false;
  134. }
  135. return true;
  136. }
  137. //--------------------------------------------------------------------------------------------------------
  138. /**
  139. * Called each frame by the player PostThink
  140. */
  141. void CWeaponBaseItem::ItemPostFrame( void )
  142. {
  143. CCSPlayer *pPlayer = ToCSPlayer( GetPlayerOwner() );
  144. if ( !pPlayer )
  145. return;
  146. #ifndef CLIENT_DLL
  147. int buttons = pPlayer->m_nButtons;
  148. #endif
  149. BaseClass::ItemPostFrame();
  150. if ( m_UseTimer.HasStarted() && m_UseTimer.IsElapsed() )
  151. {
  152. // pills can only help you so much
  153. CCSPlayer *pPlayer = ToCSPlayer( GetPlayerOwner() );
  154. if ( !pPlayer || !pPlayer->IsAlive() )
  155. {
  156. m_UseTimer.Invalidate();
  157. return;
  158. }
  159. #ifndef CLIENT_DLL
  160. CompleteUse( pPlayer );
  161. // BaseClass::ItemPostFrame() clears IN_ATTACK2, so we restore it here to prevent the next weapon from bashing immediately
  162. pPlayer->m_nButtons = buttons;
  163. m_bRedraw = true;
  164. #endif
  165. m_UseTimer.Invalidate();
  166. // remove the ammo
  167. pPlayer->RemoveAmmo( 1, m_iPrimaryAmmoType );
  168. if ( pPlayer->GetAmmoCount(m_iPrimaryAmmoType) <= 0 )
  169. {
  170. pPlayer->Weapon_Drop( this, NULL, NULL );
  171. #ifndef CLIENT_DLL
  172. UTIL_Remove( this );
  173. #endif
  174. }
  175. else
  176. {
  177. pPlayer->SwitchToNextBestWeapon( this );
  178. }
  179. #if defined (CLIENT_DLL)
  180. // when an item is removed, force the local player to update their inventory screen
  181. C_CSPlayer *pLocalPlayer = C_CSPlayer::GetLocalCSPlayer();
  182. if ( pLocalPlayer && pLocalPlayer == pPlayer )
  183. {
  184. SFWeaponSelection *pHudWS = GET_HUDELEMENT( SFWeaponSelection );
  185. if ( pHudWS )
  186. {
  187. int nAmmoCount = pPlayer->GetAmmoCount( m_iPrimaryAmmoType );
  188. if ( nAmmoCount <= 0 )
  189. {
  190. pHudWS->ShowAndUpdateSelection( WEPSELECT_DROP, this );
  191. }
  192. else
  193. {
  194. // we need to tell the hud that this weapon still exists and then update the selected weapon
  195. pHudWS->ShowAndUpdateSelection( WEPSELECT_PICKUP, this );
  196. }
  197. }
  198. }
  199. #endif
  200. }
  201. }
  202. // //--------------------------------------------------------------------------------------------------------
  203. // bool CWeaponBaseItem::OnHit( trace_t &trace, const Vector &swingVector, bool firstTime )
  204. // {
  205. // if ( trace.m_pEnt && trace.m_pEnt->IsPlayer() && IsASurvivorTeam( trace.m_pEnt->GetTeamNumber() ) )
  206. // return false; // don't hit survivors who are outside of heal range if we're trying to get close and heal them.
  207. //
  208. // return BaseClass::OnHit( trace, swingVector, firstTime );
  209. // }
  210. //--------------------------------------------------------------------------------------------------------
  211. bool CWeaponBaseItem::SendWeaponAnim( int iActivity )
  212. {
  213. //iActivity = TranslateViewmodelActivity( (Activity)iActivity );
  214. return BaseClass::SendWeaponAnim( iActivity );
  215. }
  216. //--------------------------------------------------------------------------------------------------------
  217. bool CWeaponBaseItem::CanFidget( void )
  218. {
  219. return false;
  220. }
  221. //--------------------------------------------------------------------------------------------------------