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.

140 lines
2.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "dod_ammo_box.h"
  8. #include "dod_player.h"
  9. #include "dod_gamerules.h"
  10. BEGIN_DATADESC( CAmmoBox )
  11. DEFINE_THINKFUNC( FlyThink ),
  12. DEFINE_ENTITYFUNC( BoxTouch ),
  13. END_DATADESC();
  14. LINK_ENTITY_TO_CLASS( dod_ammo_box, CAmmoBox );
  15. void CAmmoBox::Spawn( void )
  16. {
  17. Precache( );
  18. SetModel( "models/ammo/ammo_us.mdl" );
  19. BaseClass::Spawn();
  20. SetNextThink( gpGlobals->curtime + 0.75f );
  21. SetThink( &CAmmoBox::FlyThink );
  22. SetTouch( &CAmmoBox::BoxTouch );
  23. m_hOldOwner = GetOwnerEntity();
  24. }
  25. void CAmmoBox::Precache( void )
  26. {
  27. PrecacheModel( "models/ammo/ammo_axis.mdl" );
  28. PrecacheModel( "models/ammo/ammo_us.mdl" );
  29. }
  30. CAmmoBox *CAmmoBox::Create( const Vector &vecOrigin, const QAngle &vecAngles, CBaseEntity *pOwner, int team )
  31. {
  32. CAmmoBox *p = static_cast<CAmmoBox *> ( CBaseAnimating::Create( "dod_ammo_box", vecOrigin, vecAngles, pOwner ) );
  33. p->SetAmmoTeam( team );
  34. return p;
  35. }
  36. void CAmmoBox::SetAmmoTeam( int team )
  37. {
  38. switch( team )
  39. {
  40. case TEAM_ALLIES:
  41. {
  42. SetModel( "models/ammo/ammo_us.mdl" );
  43. }
  44. break;
  45. case TEAM_AXIS:
  46. {
  47. SetModel( "models/ammo/ammo_axis.mdl" );
  48. }
  49. break;
  50. default:
  51. Assert(0);
  52. break;
  53. }
  54. m_iAmmoTeam = team;
  55. }
  56. void CAmmoBox::FlyThink( void )
  57. {
  58. SetOwnerEntity( NULL ); //so our owner can pick it back up
  59. }
  60. void CAmmoBox::BoxTouch( CBaseEntity *pOther )
  61. {
  62. Assert( pOther );
  63. if( !pOther->IsPlayer() )
  64. return;
  65. if( !pOther->IsAlive() )
  66. return;
  67. //Don't let the person who threw this ammo pick it up until it hits the ground.
  68. //This way we can throw ammo to people, but not touch it as soon as we throw it ourselves
  69. if( GetOwnerEntity() == pOther )
  70. return;
  71. CDODPlayer *pPlayer = ToDODPlayer( pOther );
  72. Assert( pPlayer );
  73. if( pPlayer->GetTeamNumber() != m_iAmmoTeam )
  74. return;
  75. if( pPlayer == m_hOldOwner )
  76. {
  77. //don't give ammo, just give him his drop again
  78. pPlayer->ReturnGenericAmmo();
  79. UTIL_Remove(this);
  80. }
  81. else
  82. {
  83. //See if they can use some ammo, if so, remove the box
  84. if( pPlayer->GiveGenericAmmo() )
  85. UTIL_Remove(this);
  86. }
  87. }
  88. bool CAmmoBox::MyTouch( CBasePlayer *pBasePlayer )
  89. {
  90. if ( !pBasePlayer )
  91. {
  92. Assert( false );
  93. return false;
  94. }
  95. if( !pBasePlayer->IsAlive() )
  96. return false;
  97. if( pBasePlayer->GetTeamNumber() != m_iAmmoTeam )
  98. return false;
  99. CDODPlayer *pPlayer = ToDODPlayer( pBasePlayer );
  100. if( pPlayer == m_hOldOwner )
  101. {
  102. //don't give ammo, just give him his drop again
  103. pPlayer->ReturnGenericAmmo();
  104. UTIL_Remove(this);
  105. }
  106. else
  107. {
  108. //See if they can use some ammo, if so, remove the box
  109. if( pPlayer->GiveGenericAmmo() )
  110. UTIL_Remove(this);
  111. }
  112. return true;
  113. }