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.

238 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. ===== h_battery.cpp ========================================================
  10. battery-related code
  11. */
  12. #include "cbase.h"
  13. #include "gamerules.h"
  14. #include "player.h"
  15. #include "engine/IEngineSound.h"
  16. #include "in_buttons.h"
  17. ConVar sk_suitcharger( "sk_suitcharger","0" );
  18. #define HL1_MAX_ARMOR 100
  19. class CRecharge : public CBaseToggle
  20. {
  21. public:
  22. DECLARE_CLASS( CRecharge, CBaseToggle );
  23. void Spawn( );
  24. virtual void Precache();
  25. bool CreateVPhysics();
  26. void Off(void);
  27. void Recharge(void);
  28. bool KeyValue( const char *szKeyName, const char *szValue );
  29. void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  30. virtual int ObjectCaps( void ) { return (BaseClass::ObjectCaps() | m_iCaps ); }
  31. DECLARE_DATADESC();
  32. float m_flNextCharge;
  33. int m_iReactivate ; // DeathMatch Delay until reactvated
  34. int m_iJuice;
  35. int m_iOn; // 0 = off, 1 = startup, 2 = going
  36. float m_flSoundTime;
  37. int m_iCaps;
  38. COutputFloat m_OutRemainingCharge;
  39. };
  40. BEGIN_DATADESC( CRecharge )
  41. DEFINE_FIELD( m_flNextCharge, FIELD_TIME ),
  42. DEFINE_FIELD( m_iReactivate, FIELD_INTEGER),
  43. DEFINE_FIELD( m_iJuice, FIELD_INTEGER),
  44. DEFINE_FIELD( m_iOn, FIELD_INTEGER),
  45. DEFINE_FIELD( m_flSoundTime, FIELD_TIME ),
  46. DEFINE_FIELD( m_iCaps, FIELD_INTEGER ),
  47. // Function Pointers
  48. DEFINE_FUNCTION( Off ),
  49. DEFINE_FUNCTION( Recharge ),
  50. DEFINE_OUTPUT(m_OutRemainingCharge, "OutRemainingCharge"),
  51. END_DATADESC()
  52. LINK_ENTITY_TO_CLASS(func_recharge, CRecharge);
  53. bool CRecharge::KeyValue( const char *szKeyName, const char *szValue )
  54. {
  55. if ( FStrEq(szKeyName, "style") ||
  56. FStrEq(szKeyName, "height") ||
  57. FStrEq(szKeyName, "value1") ||
  58. FStrEq(szKeyName, "value2") ||
  59. FStrEq(szKeyName, "value3"))
  60. {
  61. }
  62. else if (FStrEq(szKeyName, "dmdelay"))
  63. {
  64. m_iReactivate = atoi(szValue);
  65. }
  66. else
  67. return BaseClass::KeyValue( szKeyName, szValue );
  68. return true;
  69. }
  70. void CRecharge::Spawn()
  71. {
  72. Precache( );
  73. SetSolid( SOLID_BSP );
  74. SetMoveType( MOVETYPE_PUSH );
  75. SetModel( STRING( GetModelName() ) );
  76. m_iJuice = sk_suitcharger.GetFloat();
  77. SetTextureFrameIndex( 0 );
  78. m_iCaps = FCAP_CONTINUOUS_USE;
  79. CreateVPhysics();
  80. }
  81. void CRecharge::Precache()
  82. {
  83. BaseClass::Precache();
  84. PrecacheScriptSound( "SuitRecharge.Deny" );
  85. PrecacheScriptSound( "SuitRecharge.Start" );
  86. PrecacheScriptSound( "SuitRecharge.ChargingLoop" );
  87. }
  88. bool CRecharge::CreateVPhysics()
  89. {
  90. VPhysicsInitStatic();
  91. return true;
  92. }
  93. void CRecharge::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  94. {
  95. // Make sure that we have a caller
  96. if (!pActivator)
  97. return;
  98. // if it's not a player, ignore
  99. if ( !pActivator->IsPlayer() )
  100. return;
  101. CBasePlayer *pPlayer = dynamic_cast<CBasePlayer *>( pActivator );
  102. if ( pPlayer == NULL )
  103. return;
  104. // Reset to a state of continuous use.
  105. m_iCaps = FCAP_CONTINUOUS_USE;
  106. // if there is no juice left, turn it off
  107. if (m_iJuice <= 0)
  108. {
  109. SetTextureFrameIndex( 1 );
  110. Off();
  111. }
  112. // if the player doesn't have the suit, or there is no juice left, make the deny noise
  113. if ( m_iJuice <= 0 )
  114. {
  115. if (m_flSoundTime <= gpGlobals->curtime)
  116. {
  117. m_flSoundTime = gpGlobals->curtime + 0.62;
  118. EmitSound( "SuitRecharge.Deny" );
  119. }
  120. return;
  121. }
  122. // If we're over our limit, debounce our keys
  123. if ( pPlayer->ArmorValue() >= HL1_MAX_ARMOR)
  124. {
  125. // Make the user re-use me to get started drawing health.
  126. pPlayer->m_afButtonPressed &= ~IN_USE;
  127. m_iCaps = FCAP_IMPULSE_USE;
  128. EmitSound( "SuitRecharge.Deny" );
  129. return;
  130. }
  131. SetNextThink( gpGlobals->curtime + 0.25 );
  132. SetThink(&CRecharge::Off);
  133. // Time to recharge yet?
  134. if (m_flNextCharge >= gpGlobals->curtime)
  135. return;
  136. m_hActivator = pActivator;
  137. // Play the on sound or the looping charging sound
  138. if (!m_iOn)
  139. {
  140. m_iOn++;
  141. EmitSound( "SuitRecharge.Start" );
  142. m_flSoundTime = 0.56 + gpGlobals->curtime;
  143. }
  144. if ((m_iOn == 1) && (m_flSoundTime <= gpGlobals->curtime))
  145. {
  146. m_iOn++;
  147. CPASAttenuationFilter filter( this, "SuitRecharge.ChargingLoop" );
  148. filter.MakeReliable();
  149. EmitSound( filter, entindex(), "SuitRecharge.ChargingLoop" );
  150. }
  151. CBasePlayer *pl = (CBasePlayer *) m_hActivator.Get();
  152. // charge the player
  153. if (pl->ArmorValue() < HL1_MAX_ARMOR)
  154. {
  155. m_iJuice--;
  156. pl->IncrementArmorValue( 1, HL1_MAX_ARMOR );
  157. }
  158. // Send the output.
  159. float flRemaining = m_iJuice / sk_suitcharger.GetFloat();
  160. m_OutRemainingCharge.Set(flRemaining, pActivator, this);
  161. // govern the rate of charge
  162. m_flNextCharge = gpGlobals->curtime + 0.1;
  163. }
  164. void CRecharge::Recharge(void)
  165. {
  166. m_iJuice = sk_suitcharger.GetFloat();
  167. SetTextureFrameIndex( 0 );
  168. SetThink( &CBaseEntity::SUB_DoNothing );
  169. }
  170. void CRecharge::Off(void)
  171. {
  172. // Stop looping sound.
  173. if (m_iOn > 1)
  174. {
  175. StopSound( "SuitRecharge.ChargingLoop" );
  176. }
  177. m_iOn = 0;
  178. if ((!m_iJuice) && ( ( m_iReactivate = g_pGameRules->FlHEVChargerRechargeTime() ) > 0) )
  179. {
  180. SetNextThink( gpGlobals->curtime + m_iReactivate );
  181. SetThink(&CRecharge::Recharge);
  182. }
  183. else
  184. SetThink( NULL );
  185. }