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.

300 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CTF HealthKit.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "items.h"
  8. #include "tf_gamerules.h"
  9. #include "tf_shareddefs.h"
  10. #include "tf_player.h"
  11. #include "tf_team.h"
  12. #include "engine/IEngineSound.h"
  13. #include "entity_healthkit.h"
  14. #include "tf_weapon_lunchbox.h"
  15. #include "tf_gamestats.h"
  16. //=============================================================================
  17. //
  18. // CTF HealthKit defines.
  19. //
  20. #define TF_HEALTHKIT_MODEL "models/items/healthkit.mdl"
  21. #define TF_HEALTHKIT_PICKUP_SOUND "HealthKit.Touch"
  22. #define TF_AMMOPACK_PICKUP_SOUND "AmmoPack.Touch"
  23. LINK_ENTITY_TO_CLASS( item_healthkit_full, CHealthKit );
  24. LINK_ENTITY_TO_CLASS( item_healthkit_small, CHealthKitSmall );
  25. LINK_ENTITY_TO_CLASS( item_healthkit_medium, CHealthKitMedium );
  26. LINK_ENTITY_TO_CLASS( item_healthammokit, CHealthAmmoKit );
  27. IMPLEMENT_AUTO_LIST( IHealthKitAutoList );
  28. //=============================================================================
  29. //
  30. // CTF HealthKit functions.
  31. //
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Spawn function for the healthkit
  34. //-----------------------------------------------------------------------------
  35. void CHealthKit::Spawn( void )
  36. {
  37. BaseClass::Spawn();
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose: Precache function for the healthkit
  41. //-----------------------------------------------------------------------------
  42. void CHealthKit::Precache( void )
  43. {
  44. PrecacheScriptSound( TF_HEALTHKIT_PICKUP_SOUND );
  45. PrecacheModel( TF_MEDKIT_LARGE_BDAY ); // always precache this for PyroVision
  46. PrecacheModel( TF_MEDKIT_LARGE_HALLOWEEN ); // always precache this for Halloween
  47. BaseClass::Precache();
  48. UpdateModelIndexOverrides();
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: MyTouch function for the healthkit
  52. //-----------------------------------------------------------------------------
  53. bool CHealthKit::MyTouch( CBasePlayer *pPlayer )
  54. {
  55. bool bSuccess = false;
  56. if ( ValidTouch( pPlayer ) )
  57. {
  58. CTFPlayer *pTFPlayer = ToTFPlayer( pPlayer );
  59. Assert( pTFPlayer );
  60. const bool bIsAnyHeavyWithSandvichEquippedPickingUp = pTFPlayer->Weapon_OwnsThisID( TF_WEAPON_LUNCHBOX ) && pTFPlayer->IsPlayerClass( TF_CLASS_HEAVYWEAPONS );
  61. bool bPerformPickup = false;
  62. // In the case of sandvich's owner, only restore ammo
  63. if ( GetOwnerEntity() == pPlayer && bIsAnyHeavyWithSandvichEquippedPickingUp )
  64. {
  65. if ( pPlayer->GiveAmmo( 1, TF_AMMO_GRENADES1, false ) )
  66. {
  67. bSuccess = true;
  68. bPerformPickup = true;
  69. }
  70. }
  71. else
  72. {
  73. float flHealth = ceil( ( pPlayer->GetMaxHealth() - pTFPlayer->GetRuneHealthBonus() ) * PackRatios[GetPowerupSize()] );
  74. CALL_ATTRIB_HOOK_FLOAT_ON_OTHER( pPlayer, flHealth, mult_health_frompacks );
  75. int nHealthGiven = pPlayer->TakeHealth( flHealth, DMG_GENERIC );
  76. if ( nHealthGiven > 0 )
  77. {
  78. IGameEvent *event = gameeventmanager->CreateEvent( "player_healed" );
  79. if ( event )
  80. {
  81. CTFPlayer *pOwner = ToTFPlayer( GetOwnerEntity() );
  82. int nHealerID = pOwner ? pOwner->GetUserID() : 0;
  83. event->SetInt( "priority", 1 ); // HLTV event priority
  84. event->SetInt( "patient", pPlayer->GetUserID() );
  85. event->SetInt( "healer", nHealerID );
  86. event->SetInt( "amount", nHealthGiven );
  87. gameeventmanager->FireEvent( event );
  88. }
  89. }
  90. if ( pTFPlayer->m_Shared.InCond( TF_COND_DISGUISED ) && pTFPlayer->m_Shared.GetCarryingRuneType() != RUNE_PLAGUE )
  91. {
  92. float flDisguiseHealth = pTFPlayer->m_Shared.GetDisguiseHealth();
  93. float flDisguiseMaxHealth = pTFPlayer->m_Shared.GetDisguiseMaxHealth();
  94. float flHealthToAdd = ceil(flDisguiseMaxHealth * PackRatios[GetPowerupSize()]);
  95. // don't want to add more than we're allowed to have
  96. if ( flHealthToAdd > flDisguiseMaxHealth - flDisguiseHealth )
  97. {
  98. flHealthToAdd = flDisguiseMaxHealth - flDisguiseHealth;
  99. }
  100. pTFPlayer->m_Shared.SetDisguiseHealth( flDisguiseHealth + flHealthToAdd );
  101. bSuccess = true;
  102. }
  103. if ( nHealthGiven > 0 || pTFPlayer->m_Shared.InCond( TF_COND_BLEEDING ) || pTFPlayer->m_Shared.InCond( TF_COND_BURNING ) || pTFPlayer->m_Shared.InCond( TF_COND_PLAGUE ) )
  104. {
  105. bPerformPickup = true;
  106. bSuccess = true;
  107. // subtract this from the drowndmg in case they're drowning and being healed at the same time
  108. pPlayer->AdjustDrownDmg( -1.0 * flHealth );
  109. CSingleUserRecipientFilter user( pPlayer );
  110. EmitSound( user, entindex(), TF_HEALTHKIT_PICKUP_SOUND );
  111. CTFPlayer *pOwner = ToTFPlayer( GetOwnerEntity() );
  112. if ( pOwner && ( pOwner != pTFPlayer ) )
  113. {
  114. if ( pOwner->GetTeamNumber() == pTFPlayer->GetTeamNumber() )
  115. {
  116. if ( pTFPlayer->GetLastEntityDamaged() != pTFPlayer )
  117. {
  118. CTF_GameStats.Event_PlayerAwardBonusPoints( pOwner, pTFPlayer, 10 );
  119. CTF_GameStats.Event_PlayerHealedOtherAssist( pOwner, nHealthGiven );
  120. }
  121. if ( pOwner->Weapon_OwnsThisID( TF_WEAPON_LUNCHBOX ) && pOwner->IsPlayerClass( TF_CLASS_HEAVYWEAPONS ) )
  122. {
  123. CEconEntity *pEconItem = dynamic_cast<CEconEntity *>( pOwner->GetEntityForLoadoutSlot( LOADOUT_POSITION_SECONDARY ) );
  124. if ( pEconItem )
  125. {
  126. EconEntity_OnOwnerKillEaterEvent( pEconItem, pOwner, pTFPlayer, kKillEaterEvent_AllyHealingDone, nHealthGiven );
  127. if ( pTFPlayer->m_Shared.InCond( TF_COND_BURNING ) )
  128. {
  129. EconEntity_OnOwnerKillEaterEvent( pEconItem, pOwner, pTFPlayer, kKillEaterEvent_BurningAllyExtinguished );
  130. }
  131. }
  132. }
  133. }
  134. }
  135. if ( pTFPlayer->IsPlayerClass( TF_CLASS_HEAVYWEAPONS ) )
  136. {
  137. UserMessageBegin( user, "UpdateAchievement" );
  138. WRITE_SHORT( ACHIEVEMENT_TF_HEAVY_HEAL_MEDIKITS );
  139. WRITE_SHORT( nHealthGiven );
  140. MessageEnd();
  141. }
  142. pTFPlayer->m_Shared.HealthKitPickupEffects( nHealthGiven );
  143. CTF_GameStats.Event_PlayerHealthkitPickup( pTFPlayer );
  144. }
  145. else if ( !m_bThrownSingleInstance )
  146. {
  147. if ( bIsAnyHeavyWithSandvichEquippedPickingUp )
  148. {
  149. if ( pPlayer->GiveAmmo( 1, TF_AMMO_GRENADES1, false ) )
  150. {
  151. bPerformPickup = true;
  152. bSuccess = true;
  153. }
  154. }
  155. }
  156. }
  157. if ( bPerformPickup )
  158. {
  159. CSingleUserRecipientFilter user( pPlayer );
  160. user.MakeReliable();
  161. UserMessageBegin( user, "ItemPickup" );
  162. WRITE_STRING( GetClassname() );
  163. MessageEnd();
  164. IGameEvent * event = gameeventmanager->CreateEvent( "item_pickup" );
  165. if( event )
  166. {
  167. event->SetInt( "userid", pPlayer->GetUserID() );
  168. event->SetString( "item", GetHealthKitName() );
  169. gameeventmanager->FireEvent( event );
  170. }
  171. }
  172. }
  173. return bSuccess;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose:
  177. //-----------------------------------------------------------------------------
  178. float CHealthKit::GetRespawnDelay( void )
  179. {
  180. return g_pGameRules->FlItemRespawnTime( this );
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Purpose: MyTouch function for the health-ammo kit
  184. //-----------------------------------------------------------------------------
  185. bool CHealthAmmoKit::MyTouch( CBasePlayer *pPlayer )
  186. {
  187. // First do regular health-kit behavior.
  188. bool bHealthSuccess = BaseClass::MyTouch( pPlayer );
  189. // Now do ammo-kit behavior (essentially a dupe of the logic in CAmmmoPack::MyTouch - no easy way to put in one spot
  190. // without larger refactoring). Filtering out heavies picking up their own sandvich.
  191. bool bAmmoSuccess = false;
  192. if ( ValidTouch( pPlayer ) )
  193. {
  194. CTFPlayer *pTFPlayer = ToTFPlayer( pPlayer );
  195. if ( pTFPlayer )
  196. {
  197. const bool bIsAnyHeavyWithSandvichEquippedPickingUp = pTFPlayer->Weapon_OwnsThisID( TF_WEAPON_LUNCHBOX ) && pTFPlayer->IsPlayerClass( TF_CLASS_HEAVYWEAPONS );
  198. if ( !bIsAnyHeavyWithSandvichEquippedPickingUp || GetOwnerEntity() != pPlayer )
  199. {
  200. float flPackRatio = PackRatios[GetPowerupSize()];
  201. int iMaxPrimary = pTFPlayer->GetMaxAmmo( TF_AMMO_PRIMARY );
  202. if ( pTFPlayer->GiveAmmo( ceil( iMaxPrimary * flPackRatio ), TF_AMMO_PRIMARY, true, kAmmoSource_Pickup ) )
  203. {
  204. bAmmoSuccess = true;
  205. }
  206. int iMaxSecondary = pTFPlayer->GetMaxAmmo( TF_AMMO_SECONDARY );
  207. if ( pTFPlayer->GiveAmmo( ceil( iMaxSecondary * flPackRatio ), TF_AMMO_SECONDARY, true, kAmmoSource_Pickup ) )
  208. {
  209. bAmmoSuccess = true;
  210. }
  211. int iMaxMetal = pTFPlayer->GetMaxAmmo( TF_AMMO_METAL );
  212. if ( pTFPlayer->GiveAmmo( ceil( iMaxMetal * flPackRatio ), TF_AMMO_METAL, true, kAmmoSource_Pickup ) )
  213. {
  214. bAmmoSuccess = true;
  215. }
  216. if ( pTFPlayer->m_Shared.AddToSpyCloakMeter( 100.0f * flPackRatio ) )
  217. {
  218. bAmmoSuccess = true;
  219. }
  220. if ( pTFPlayer->AddToSpyKnife( 100.0f * flPackRatio, false ) )
  221. {
  222. bAmmoSuccess = true;
  223. }
  224. if ( pTFPlayer->IsPlayerClass( TF_CLASS_ENGINEER ) )
  225. {
  226. int iMaxGrenades1 = pTFPlayer->GetMaxAmmo( TF_AMMO_GRENADES1 );
  227. if ( pTFPlayer->GiveAmmo( ceil( iMaxGrenades1 * flPackRatio ), TF_AMMO_GRENADES1, true, kAmmoSource_Pickup ) )
  228. {
  229. bAmmoSuccess = true;
  230. }
  231. }
  232. }
  233. }
  234. // If we didn't give them health already, but did give them ammo, do the ammo pickup flow.
  235. if ( !bHealthSuccess && bAmmoSuccess )
  236. {
  237. CSingleUserRecipientFilter filter( pPlayer );
  238. EmitSound( filter, entindex(), TF_AMMOPACK_PICKUP_SOUND );
  239. CTF_GameStats.Event_PlayerAmmokitPickup( pTFPlayer );
  240. IGameEvent * event = gameeventmanager->CreateEvent( "item_pickup" );
  241. if ( event )
  242. {
  243. event->SetInt( "userid", pPlayer->GetUserID() );
  244. event->SetString( "item", GetHealthKitName() );
  245. gameeventmanager->FireEvent( event );
  246. }
  247. }
  248. }
  249. return bAmmoSuccess | bHealthSuccess;
  250. }