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.

188 lines
5.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CTF HealthKit.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "entity_bonuspack.h"
  8. #ifdef GAME_DLL
  9. #include "tf_logic_robot_destruction.h"
  10. #include "tf_player.h"
  11. #include "particle_parse.h"
  12. #include "tf_fx.h"
  13. #endif
  14. #define TF_POWERCORE_RED_PICKUP "powercore_embers_red"
  15. #define TF_POWERCORE_BLUE_PICKUP "powercore_embers_blue"
  16. #define BONUS_PACK_BLINK_CONTEXT "blink_think"
  17. ConVar tf_bonuspack_score( "tf_bonuspack_score", "1", FCVAR_REPLICATED | FCVAR_DEVELOPMENTONLY );
  18. #define BLINK_TIME 5.f
  19. #define REMOVE_TIME 20.f
  20. #define PICKUP_TIME 0.5f
  21. IMPLEMENT_NETWORKCLASS_ALIASED( BonusPack, DT_CBonusPack )
  22. BEGIN_NETWORK_TABLE( CBonusPack, DT_CBonusPack )
  23. END_NETWORK_TABLE()
  24. BEGIN_DATADESC( CBonusPack )
  25. END_DATADESC()
  26. LINK_ENTITY_TO_CLASS( item_bonuspack, CBonusPack );
  27. IMPLEMENT_AUTO_LIST( IBonusPackAutoList );
  28. //-----------------------------------------------------------------------------
  29. // Purpose:
  30. //-----------------------------------------------------------------------------
  31. CBonusPack::CBonusPack()
  32. {
  33. #ifdef GAME_DLL
  34. m_bAutoMaterialize = false;
  35. #else
  36. SetCycle( RandomFloat() );
  37. #endif
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose:
  41. //-----------------------------------------------------------------------------
  42. void CBonusPack::Spawn( void )
  43. {
  44. Precache();
  45. BaseClass::BaseClass::Spawn();
  46. #ifdef GAME_DLL
  47. const char *pszParticleName = GetTeamNumber() == TF_TEAM_RED ? "powercore_alert_blue" : "powercore_alert_red";
  48. DispatchParticleEffect( pszParticleName, PATTACH_POINT_FOLLOW, this, "particle_spawn" );
  49. SetModel( GetPowerupModel() );
  50. CollisionProp()->UseTriggerBounds( true, 64 );
  51. m_flCanPickupTime = gpGlobals->curtime + PICKUP_TIME;
  52. m_nBlinkCount = 0;
  53. m_flKillTime = gpGlobals->curtime + REMOVE_TIME + BLINK_TIME;
  54. SetContextThink( &CBonusPack::BlinkThink, gpGlobals->curtime + REMOVE_TIME, BONUS_PACK_BLINK_CONTEXT );
  55. SetContextThink( &CBonusPack::SUB_Remove, m_flKillTime, "RemoveThink" );
  56. #endif
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. //-----------------------------------------------------------------------------
  61. void CBonusPack::Precache( void )
  62. {
  63. // We deliberately allow late precaches here
  64. bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
  65. CBaseEntity::SetAllowPrecache( true );
  66. PrecacheParticleSystem( TF_POWERCORE_RED_PICKUP );
  67. PrecacheParticleSystem( TF_POWERCORE_BLUE_PICKUP );
  68. BaseClass::Precache();
  69. CBaseEntity::SetAllowPrecache( bAllowPrecache );
  70. }
  71. #ifdef GAME_DLL
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. bool CBonusPack::MyTouch( CBasePlayer *pPlayer )
  76. {
  77. if ( ValidTouch( pPlayer ) && gpGlobals->curtime >= m_flCanPickupTime )
  78. {
  79. CTFPlayer *pTFPlayer = ToTFPlayer( pPlayer );
  80. if ( !pTFPlayer )
  81. return true;
  82. // Play a particle colored the color of the player that picked it up
  83. Vector vecOrigin = GetAbsOrigin() + Vector( 0,0,5 );
  84. CPVSFilter pvsfilter( vecOrigin );
  85. const char *pszParticleName = pPlayer->GetTeamNumber() == TF_TEAM_RED ? TF_POWERCORE_RED_PICKUP : TF_POWERCORE_BLUE_PICKUP;
  86. TE_TFParticleEffect( pvsfilter, 0.f, pszParticleName, vecOrigin, vec3_angle );
  87. if ( CTFRobotDestructionLogic::GetRobotDestructionLogic() )
  88. {
  89. CTFRobotDestructionLogic::GetRobotDestructionLogic()->ScorePoints( GetTeamNumber()
  90. , tf_bonuspack_score.GetInt()
  91. , SCORE_CORES_COLLECTED
  92. , ToTFPlayer( pPlayer ) );
  93. }
  94. int iBoostMax = pTFPlayer->m_Shared.GetMaxBuffedHealth();
  95. // Cap it to the max we'll boost a player's health
  96. int nHealthToAdd = clamp( 5, 0, iBoostMax - pTFPlayer->GetHealth() );
  97. // Give health
  98. pPlayer->TakeHealth( nHealthToAdd, DMG_GENERIC | DMG_IGNORE_MAXHEALTH );
  99. for ( int i=0;i<TF_AMMO_COUNT;i++ )
  100. {
  101. pPlayer->GiveAmmo( 5, i );
  102. }
  103. pPlayer->SetLastObjectiveTime( gpGlobals->curtime );
  104. return true;
  105. }
  106. return false;
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose:
  110. //-----------------------------------------------------------------------------
  111. bool CBonusPack::ValidTouch( CBasePlayer *pPlayer )
  112. {
  113. if( pPlayer->GetTeamNumber() != GetTeamNumber() )
  114. return false;
  115. CTFPlayer *pTFPlayer = ToTFPlayer( pPlayer );
  116. if ( !pTFPlayer )
  117. return false;
  118. // No invis spies
  119. if ( pTFPlayer->m_Shared.InCond( TF_COND_STEALTHED ) || pTFPlayer->m_Shared.GetPercentInvisible() > 0.25f )
  120. return false;
  121. // No disguised spies
  122. if ( pTFPlayer->m_Shared.InCond( TF_COND_DISGUISED ) || pTFPlayer->m_Shared.InCond( TF_COND_DISGUISING ) )
  123. return false;
  124. // No bonk'd scouts
  125. if ( pTFPlayer->m_Shared.InCond( TF_COND_PHASE ) || pTFPlayer->m_Shared.InCond( TF_COND_PASSTIME_INTERCEPTION ) )
  126. return false;
  127. // No teleporting players
  128. if ( pTFPlayer->m_Shared.InCond( TF_COND_SELECTED_TO_TELEPORT ) )
  129. return false;
  130. // No invulns
  131. if ( pTFPlayer->m_Shared.IsInvulnerable() )
  132. return false;
  133. return BaseClass::ValidTouch( pPlayer );
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. //-----------------------------------------------------------------------------
  138. void CBonusPack::BlinkThink()
  139. {
  140. float flTimeToKill = m_flKillTime - gpGlobals->curtime;
  141. float flNextBlink = RemapValClamped( flTimeToKill, BLINK_TIME, 0.f, 0.5f, 0.1f );
  142. SetContextThink( &CBonusPack::BlinkThink, gpGlobals->curtime + flNextBlink, BONUS_PACK_BLINK_CONTEXT );
  143. SetRenderMode( kRenderTransAlpha );
  144. ++m_nBlinkCount;
  145. if ( m_nBlinkCount % 2 == 0 )
  146. {
  147. SetRenderColorA( 25 );
  148. }
  149. else
  150. {
  151. SetRenderColorA( 255 );
  152. }
  153. }
  154. #endif