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.

219 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: TF Caltrop Grenade.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "tf_weaponbase.h"
  8. #include "tf_gamerules.h"
  9. #include "npcevent.h"
  10. #include "engine/IEngineSound.h"
  11. #include "tf_weapon_grenade_caltrop.h"
  12. // Server specific.
  13. #ifdef GAME_DLL
  14. #include "tf_player.h"
  15. #include "items.h"
  16. #include "tf_weaponbase_grenadeproj.h"
  17. #include "soundent.h"
  18. #include "KeyValues.h"
  19. #endif
  20. #define GRENADE_CALTROP_TIMER 3.0f //Seconds
  21. #define GRENADE_CALTROP_RELEASE_COUNT 6
  22. #define GRENADE_CALTROP_DAMAGE 10
  23. //=============================================================================
  24. //
  25. // TF Caltrop Grenade tables.
  26. //
  27. IMPLEMENT_NETWORKCLASS_ALIASED( TFGrenadeCaltrop, DT_TFGrenadeCaltrop )
  28. BEGIN_NETWORK_TABLE( CTFGrenadeCaltrop, DT_TFGrenadeCaltrop )
  29. END_NETWORK_TABLE()
  30. BEGIN_PREDICTION_DATA( CTFGrenadeCaltrop )
  31. END_PREDICTION_DATA()
  32. LINK_ENTITY_TO_CLASS( tf_weapon_grenade_caltrop, CTFGrenadeCaltrop );
  33. PRECACHE_WEAPON_REGISTER( tf_weapon_grenade_caltrop );
  34. //=============================================================================
  35. //
  36. // TF Caltrop Grenade functions.
  37. //
  38. // Server specific.
  39. #ifdef GAME_DLL
  40. BEGIN_DATADESC( CTFGrenadeCaltrop )
  41. END_DATADESC()
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. CTFWeaponBaseGrenadeProj *CTFGrenadeCaltrop::EmitGrenade( Vector vecSrc, QAngle vecAngles, Vector vecVel,
  46. AngularImpulse angImpulse, CBasePlayer *pPlayer, float flTime, int iflags )
  47. {
  48. // Release several at a time (different directions, angles, speeds, etc.)
  49. for ( int i = 0 ; i < GRENADE_CALTROP_RELEASE_COUNT ; i++ )
  50. {
  51. Vector velocity( random->RandomFloat(-100,100), random->RandomFloat(-100,100), random->RandomFloat(150,200) );
  52. CTFGrenadeCaltropProjectile::Create( vecSrc, vecAngles, velocity, angImpulse,
  53. pPlayer, GetTFWpnData(), random->RandomFloat( 10.0f, 15.0f ) );
  54. }
  55. return NULL;
  56. }
  57. #endif
  58. //=============================================================================
  59. //
  60. // TF Caltrop Grenade Projectile functions (Server specific).
  61. //
  62. LINK_ENTITY_TO_CLASS( tf_weapon_grenade_caltrop_projectile, CTFGrenadeCaltropProjectile );
  63. PRECACHE_WEAPON_REGISTER( tf_weapon_grenade_caltrop_projectile );
  64. IMPLEMENT_NETWORKCLASS_ALIASED( TFGrenadeCaltropProjectile, DT_TFGrenadeCaltropProjectile )
  65. BEGIN_NETWORK_TABLE( CTFGrenadeCaltropProjectile, DT_TFGrenadeCaltropProjectile )
  66. END_NETWORK_TABLE()
  67. #ifdef GAME_DLL
  68. #define GRENADE_MODEL "models/weapons/w_models/w_grenade_beartrap.mdl"
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. CTFGrenadeCaltropProjectile* CTFGrenadeCaltropProjectile::Create( const Vector &position, const QAngle &angles,
  73. const Vector &velocity, const AngularImpulse &angVelocity,
  74. CBaseCombatCharacter *pOwner, const CTFWeaponInfo &weaponInfo,
  75. float timer, int iFlags )
  76. {
  77. CTFGrenadeCaltropProjectile *pGrenade = static_cast<CTFGrenadeCaltropProjectile*>( CTFWeaponBaseGrenadeProj::Create( "tf_weapon_grenade_caltrop_projectile", position, angles, velocity, angVelocity, pOwner, weaponInfo, timer, iFlags ) );
  78. if ( pGrenade )
  79. {
  80. pGrenade->ApplyLocalAngularVelocityImpulse( angVelocity );
  81. pGrenade->SetTouch( &CTFGrenadeCaltropProjectile::Touch );
  82. }
  83. return pGrenade;
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. //-----------------------------------------------------------------------------
  88. void CTFGrenadeCaltropProjectile::Spawn()
  89. {
  90. SetModel( GRENADE_MODEL );
  91. BaseClass::Spawn();
  92. // We want to get touch functions called so we can damage enemy players
  93. AddSolidFlags( FSOLID_TRIGGER );
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose:
  97. //-----------------------------------------------------------------------------
  98. void CTFGrenadeCaltropProjectile::Precache()
  99. {
  100. PrecacheModel( GRENADE_MODEL );
  101. BaseClass::Precache();
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. void CTFGrenadeCaltropProjectile::BounceSound( void )
  107. {
  108. EmitSound( "Weapon_Grenade_Caltrop.Bounce" );
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose:
  112. //-----------------------------------------------------------------------------
  113. void CTFGrenadeCaltropProjectile::Detonate()
  114. {
  115. if ( ShouldNotDetonate() )
  116. {
  117. RemoveGrenade();
  118. return;
  119. }
  120. // have the caltrop disappear
  121. UTIL_Remove( this );
  122. #if 0
  123. // Tell the bots an HE grenade has exploded
  124. CTFPlayer *pPlayer = ToTFPlayer( GetThrower() );
  125. if ( pPlayer )
  126. {
  127. KeyValues *pEvent = new KeyValues( "tf_weapon_grenade_detonate" );
  128. pEvent->SetInt( "userid", pPlayer->GetUserID() );
  129. gameeventmanager->FireEventServerOnly( pEvent );
  130. }
  131. #endif
  132. }
  133. #endif
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. //-----------------------------------------------------------------------------
  137. void CTFGrenadeCaltropProjectile::Touch( CBaseEntity *pOther )
  138. {
  139. if ( !pOther->IsPlayer() || !( pOther->GetFlags() & FL_ONGROUND ) || !pOther->IsAlive() )
  140. return;
  141. // Don't hurt friendlies
  142. if ( GetTeamNumber() == pOther->GetTeamNumber() )
  143. return;
  144. // Caltrops need to be on the ground. Check to see if we're still moving.
  145. Vector vecVelocity;
  146. VPhysicsGetObject()->GetVelocity( &vecVelocity, NULL );
  147. if ( vecVelocity.LengthSqr() > (1*1) )
  148. return;
  149. #ifdef GAME_DLL
  150. // Do the leg damage to the player
  151. CTakeDamageInfo info( this, GetThrower(), GRENADE_CALTROP_DAMAGE, DMG_LEG_DAMAGE | DMG_PREVENT_PHYSICS_FORCE );
  152. pOther->TakeDamage( info );
  153. // have the caltrop disappear
  154. UTIL_Remove( this );
  155. #endif
  156. }
  157. #ifdef CLIENT_DLL
  158. //-----------------------------------------------------------------------------
  159. // Purpose:
  160. //-----------------------------------------------------------------------------
  161. void CTFGrenadeCaltropProjectile::OnDataChanged(DataUpdateType_t updateType)
  162. {
  163. BaseClass::OnDataChanged(updateType);
  164. if ( updateType == DATA_UPDATE_CREATED )
  165. {
  166. /*
  167. SetSolidFlags( FSOLID_NOT_STANDABLE );
  168. SetSolid( SOLID_BBOX );
  169. SetCollisionBounds( Vector( -2.0f, -2.0f, -2.0f ), Vector( 2.0f, 2.0f, 2.0f ) );
  170. // We want touch calls on the client.
  171. // So override the collision group, but make it a trigger
  172. SetCollisionGroup( COLLISION_GROUP_NONE );
  173. AddSolidFlags( FSOLID_TRIGGER );
  174. UpdatePartitionListEntry();
  175. CollisionProp()->UpdatePartition();
  176. */
  177. }
  178. }
  179. #endif