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.

136 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CTF AmmoPack.
  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_ammopack.h"
  14. #include "tf_gamestats.h"
  15. //=============================================================================
  16. //
  17. // CTF AmmoPack defines.
  18. //
  19. #define TF_AMMOPACK_PICKUP_SOUND "AmmoPack.Touch"
  20. LINK_ENTITY_TO_CLASS( item_ammopack_full, CAmmoPack );
  21. LINK_ENTITY_TO_CLASS( item_ammopack_small, CAmmoPackSmall );
  22. LINK_ENTITY_TO_CLASS( item_ammopack_medium, CAmmoPackMedium );
  23. //=============================================================================
  24. //
  25. // CTF AmmoPack functions.
  26. //
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Spawn function for the ammopack
  29. //-----------------------------------------------------------------------------
  30. void CAmmoPack::Spawn( void )
  31. {
  32. BaseClass::Spawn();
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose: Precache function for the ammopack
  36. //-----------------------------------------------------------------------------
  37. void CAmmoPack::Precache( void )
  38. {
  39. PrecacheScriptSound( TF_AMMOPACK_PICKUP_SOUND );
  40. PrecacheModel( TF_AMMOPACK_LARGE_BDAY ); // always precache this for PyroVision
  41. BaseClass::Precache();
  42. UpdateModelIndexOverrides();
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Purpose: MyTouch function for the ammopack
  46. //-----------------------------------------------------------------------------
  47. bool CAmmoPack::MyTouch( CBasePlayer *pPlayer )
  48. {
  49. bool bSuccess = false;
  50. if ( ValidTouch( pPlayer ) )
  51. {
  52. CTFPlayer *pTFPlayer = ToTFPlayer( pPlayer );
  53. if ( !pTFPlayer )
  54. return false;
  55. float flPackRatio = PackRatios[GetPowerupSize()];
  56. int iMaxPrimary = pTFPlayer->GetMaxAmmo(TF_AMMO_PRIMARY);
  57. if ( pTFPlayer->GiveAmmo( ceil(iMaxPrimary * flPackRatio), TF_AMMO_PRIMARY, true, kAmmoSource_Pickup ) )
  58. {
  59. bSuccess = true;
  60. }
  61. int iMaxSecondary = pTFPlayer->GetMaxAmmo(TF_AMMO_SECONDARY);
  62. if ( pTFPlayer->GiveAmmo( ceil(iMaxSecondary * flPackRatio), TF_AMMO_SECONDARY, true, kAmmoSource_Pickup ) )
  63. {
  64. bSuccess = true;
  65. }
  66. int iMaxMetal = pTFPlayer->GetMaxAmmo(TF_AMMO_METAL);
  67. if ( pTFPlayer->GiveAmmo( ceil(iMaxMetal * flPackRatio), TF_AMMO_METAL, true, kAmmoSource_Pickup ) )
  68. {
  69. bSuccess = true;
  70. }
  71. if ( pTFPlayer->m_Shared.AddToSpyCloakMeter( 100.0f * flPackRatio ) )
  72. {
  73. bSuccess = true;
  74. }
  75. if ( pTFPlayer->AddToSpyKnife( 100.0f * flPackRatio, false ) )
  76. {
  77. bSuccess = true;
  78. }
  79. int iAmmoIsCharge = 0;
  80. CALL_ATTRIB_HOOK_INT_ON_OTHER( pTFPlayer, iAmmoIsCharge, ammo_gives_charge );
  81. if ( iAmmoIsCharge )
  82. {
  83. float flCurrentCharge = pTFPlayer->m_Shared.GetDemomanChargeMeter();
  84. if ( flCurrentCharge < 100.0f )
  85. {
  86. pTFPlayer->m_Shared.SetDemomanChargeMeter( flCurrentCharge + flPackRatio * 100.0f );
  87. bSuccess = true;
  88. }
  89. }
  90. if ( pTFPlayer->IsPlayerClass( TF_CLASS_ENGINEER ) )
  91. {
  92. int iMaxGrenades1 = pTFPlayer->GetMaxAmmo(TF_AMMO_GRENADES1);
  93. if ( pTFPlayer->GiveAmmo( ceil(iMaxGrenades1 * flPackRatio), TF_AMMO_GRENADES1, true, kAmmoSource_Pickup ) )
  94. {
  95. bSuccess = true;
  96. }
  97. }
  98. // did we give them anything?
  99. if ( bSuccess )
  100. {
  101. CSingleUserRecipientFilter filter( pPlayer );
  102. EmitSound( filter, entindex(), TF_AMMOPACK_PICKUP_SOUND );
  103. CTF_GameStats.Event_PlayerAmmokitPickup( pTFPlayer );
  104. IGameEvent * event = gameeventmanager->CreateEvent( "item_pickup" );
  105. if( event )
  106. {
  107. event->SetInt( "userid", pPlayer->GetUserID() );
  108. event->SetString( "item", GetAmmoPackName() );
  109. gameeventmanager->FireEvent( event );
  110. }
  111. }
  112. }
  113. return bSuccess;
  114. }