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.

221 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CTF Regenerate Zone.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "tf_player.h"
  8. #include "tf_item.h"
  9. #include "tf_team.h"
  10. #include "func_regenerate.h"
  11. #include "tf_gamerules.h"
  12. #include "eventqueue.h"
  13. LINK_ENTITY_TO_CLASS( func_regenerate, CRegenerateZone );
  14. #define TF_REGENERATE_SOUND "Regenerate.Touch"
  15. #define TF_REGENERATE_NEXT_USE_TIME 3.0f
  16. //=============================================================================
  17. //
  18. // CTF Regenerate Zone tables.
  19. //
  20. BEGIN_DATADESC( CRegenerateZone )
  21. DEFINE_FIELD( m_hAssociatedModel, FIELD_EHANDLE ),
  22. DEFINE_KEYFIELD( m_iszAssociatedModel, FIELD_STRING, "associatedmodel" ),
  23. // Functions.
  24. DEFINE_FUNCTION( Touch ),
  25. END_DATADESC();
  26. //=============================================================================
  27. //
  28. // CTF Regenerate Zone functions.
  29. //
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. //-----------------------------------------------------------------------------
  33. CRegenerateZone::CRegenerateZone()
  34. {
  35. m_bDisabled = false;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Spawn function for the entity
  39. //-----------------------------------------------------------------------------
  40. void CRegenerateZone::Spawn( void )
  41. {
  42. Precache();
  43. InitTrigger();
  44. SetTouch( &CRegenerateZone::Touch );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Precache function for the entity
  48. //-----------------------------------------------------------------------------
  49. void CRegenerateZone::Precache( void )
  50. {
  51. PrecacheScriptSound( TF_REGENERATE_SOUND );
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Precache function for the entity
  55. //-----------------------------------------------------------------------------
  56. void CRegenerateZone::Activate( void )
  57. {
  58. BaseClass::Activate();
  59. if ( m_iszAssociatedModel != NULL_STRING )
  60. {
  61. CBaseEntity *pEnt = gEntList.FindEntityByName( NULL, STRING(m_iszAssociatedModel) );
  62. if ( !pEnt )
  63. {
  64. Warning("%s(%s) unable to find associated model named '%s'.\n", GetClassname(), GetDebugName(), STRING(m_iszAssociatedModel) );
  65. }
  66. else
  67. {
  68. m_hAssociatedModel = dynamic_cast<CDynamicProp*>(pEnt);
  69. if ( !m_hAssociatedModel )
  70. {
  71. Warning("%s(%s) tried to use associated model named '%s', but it isn't a dynamic prop.\n", GetClassname(), GetDebugName(), STRING(m_iszAssociatedModel) );
  72. }
  73. }
  74. }
  75. else
  76. {
  77. Warning("%s(%s) has no associated model.\n", GetClassname(), GetDebugName() );
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. void CRegenerateZone::Touch( CBaseEntity *pOther )
  84. {
  85. if ( !IsDisabled() )
  86. {
  87. CTFPlayer *pPlayer = ToTFPlayer( pOther );
  88. if ( pPlayer )
  89. {
  90. if ( pPlayer->GetNextRegenTime() > gpGlobals->curtime )
  91. return;
  92. if ( pPlayer->IsTaunting() )
  93. return;
  94. int iTeam = GetTeamNumber();
  95. if ( TFGameRules()->State_Get() != GR_STATE_TEAM_WIN )
  96. {
  97. if ( iTeam && ( pPlayer->GetTeamNumber() != iTeam ) )
  98. return;
  99. }
  100. else
  101. {
  102. // no health for the losing team, but all zones work for the winning team
  103. if ( TFGameRules()->GetWinningTeam() != pPlayer->GetTeamNumber() )
  104. return;
  105. }
  106. if ( TFGameRules()->InStalemate() )
  107. return;
  108. Regenerate( pPlayer );
  109. #ifdef STAGING_ONLY
  110. if ( TFGameRules() && TFGameRules()->IsBountyMode() )
  111. {
  112. pPlayer->m_Shared.SetInUpgradeZone( true );
  113. }
  114. #endif // STAGING_ONLY
  115. }
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. void CRegenerateZone::EndTouch( CBaseEntity *pOther )
  122. {
  123. if ( pOther->IsPlayer() )
  124. {
  125. CTFPlayer *pTFPlayer = ToTFPlayer( pOther );
  126. if ( pTFPlayer )
  127. {
  128. pTFPlayer->m_Shared.SetInUpgradeZone( false );
  129. }
  130. }
  131. BaseClass::EndTouch( pOther );
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose:
  135. //-----------------------------------------------------------------------------
  136. void CRegenerateZone::InputEnable( inputdata_t &inputdata )
  137. {
  138. SetDisabled( false );
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CRegenerateZone::InputDisable( inputdata_t &inputdata )
  144. {
  145. SetDisabled( true );
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose:
  149. //-----------------------------------------------------------------------------
  150. bool CRegenerateZone::IsDisabled( void )
  151. {
  152. return m_bDisabled;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. //-----------------------------------------------------------------------------
  157. void CRegenerateZone::InputToggle( inputdata_t &inputdata )
  158. {
  159. if ( m_bDisabled )
  160. {
  161. SetDisabled( false );
  162. }
  163. else
  164. {
  165. SetDisabled( true );
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose:
  170. //-----------------------------------------------------------------------------
  171. void CRegenerateZone::SetDisabled( bool bDisabled )
  172. {
  173. m_bDisabled = bDisabled;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose:
  177. //-----------------------------------------------------------------------------
  178. void CRegenerateZone::Regenerate( CTFPlayer *pPlayer )
  179. {
  180. pPlayer->Regenerate();
  181. pPlayer->SetNextRegenTime( gpGlobals->curtime + TF_REGENERATE_NEXT_USE_TIME );
  182. CSingleUserRecipientFilter filter( pPlayer );
  183. EmitSound( filter, pPlayer->entindex(), TF_REGENERATE_SOUND );
  184. if ( m_hAssociatedModel )
  185. {
  186. variant_t tmpVar;
  187. tmpVar.SetString( MAKE_STRING("open") );
  188. m_hAssociatedModel->AcceptInput( "SetAnimation", this, this, tmpVar, 0 );
  189. tmpVar.SetString( MAKE_STRING("close") );
  190. g_EventQueue.AddEvent( m_hAssociatedModel, "SetAnimation", tmpVar, TF_REGENERATE_NEXT_USE_TIME - 1.0, this, this );
  191. }
  192. }