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.

189 lines
5.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "smokegrenade_projectile.h"
  8. #include "sendproxy.h"
  9. #include "particle_smokegrenade.h"
  10. #include "cs_player.h"
  11. #include "KeyValues.h"
  12. #include "bot_manager.h"
  13. #include "weapon_csbase.h"
  14. #define GRENADE_MODEL "models/Weapons/w_eq_smokegrenade_thrown.mdl"
  15. LINK_ENTITY_TO_CLASS( smokegrenade_projectile, CSmokeGrenadeProjectile );
  16. PRECACHE_WEAPON_REGISTER( smokegrenade_projectile );
  17. BEGIN_DATADESC( CSmokeGrenadeProjectile )
  18. DEFINE_THINKFUNC( Think_Detonate ),
  19. DEFINE_THINKFUNC( Think_Fade ),
  20. DEFINE_THINKFUNC( Think_Remove )
  21. END_DATADESC()
  22. CSmokeGrenadeProjectile* CSmokeGrenadeProjectile::Create(
  23. const Vector &position,
  24. const QAngle &angles,
  25. const Vector &velocity,
  26. const AngularImpulse &angVelocity,
  27. CBaseCombatCharacter *pOwner )
  28. {
  29. CSmokeGrenadeProjectile *pGrenade = (CSmokeGrenadeProjectile*)CBaseEntity::Create( "smokegrenade_projectile", position, angles, pOwner );
  30. // Set the timer for 1 second less than requested. We're going to issue a SOUND_DANGER
  31. // one second before detonation.
  32. pGrenade->SetTimer( 1.5 );
  33. pGrenade->SetAbsVelocity( velocity );
  34. pGrenade->SetupInitialTransmittedGrenadeVelocity( velocity );
  35. pGrenade->SetThrower( pOwner );
  36. pGrenade->SetGravity( 0.55 );
  37. pGrenade->SetFriction( 0.7 );
  38. pGrenade->m_flDamage = 100;
  39. pGrenade->ChangeTeam( pOwner->GetTeamNumber() );
  40. pGrenade->ApplyLocalAngularVelocityImpulse( angVelocity );
  41. pGrenade->SetTouch( &CBaseGrenade::BounceTouch );
  42. pGrenade->SetGravity( BaseClass::GetGrenadeGravity() );
  43. pGrenade->SetFriction( BaseClass::GetGrenadeFriction() );
  44. pGrenade->SetElasticity( BaseClass::GetGrenadeElasticity() );
  45. pGrenade->m_bDidSmokeEffect = false;
  46. pGrenade->m_pWeaponInfo = GetWeaponInfo( WEAPON_SMOKEGRENADE );
  47. return pGrenade;
  48. }
  49. void CSmokeGrenadeProjectile::SetTimer( float timer )
  50. {
  51. SetThink( &CSmokeGrenadeProjectile::Think_Detonate );
  52. SetNextThink( gpGlobals->curtime + timer );
  53. TheBots->SetGrenadeRadius( this, 0.0f );
  54. }
  55. void CSmokeGrenadeProjectile::Think_Detonate()
  56. {
  57. if ( GetAbsVelocity().Length() > 0.1 )
  58. {
  59. // Still moving. Don't detonate yet.
  60. SetNextThink( gpGlobals->curtime + 0.2 );
  61. return;
  62. }
  63. TheBots->SetGrenadeRadius( this, SmokeGrenadeRadius );
  64. // Ok, we've stopped rolling or whatever. Now detonate.
  65. ParticleSmokeGrenade *pGren = (ParticleSmokeGrenade*)CBaseEntity::Create( PARTICLESMOKEGRENADE_ENTITYNAME, GetAbsOrigin(), QAngle(0,0,0), NULL );
  66. if ( pGren )
  67. {
  68. pGren->FillVolume();
  69. pGren->SetFadeTime( 15, 20 );
  70. pGren->SetAbsOrigin( GetAbsOrigin() );
  71. //tell the hostages about the smoke!
  72. CBaseEntity *pEntity = NULL;
  73. variant_t var; //send the location of the smoke?
  74. var.SetVector3D( GetAbsOrigin() );
  75. while ( ( pEntity = gEntList.FindEntityByClassname( pEntity, "hostage_entity" ) ) != NULL)
  76. {
  77. //send to hostages that have a resonable chance of being in it while its still smoking
  78. if( (GetAbsOrigin() - pEntity->GetAbsOrigin()).Length() < 1000 )
  79. pEntity->AcceptInput( "smokegrenade", this, this, var, 0 );
  80. }
  81. // tell the bots a smoke grenade has exploded
  82. CCSPlayer *player = ToCSPlayer(GetThrower());
  83. if ( player )
  84. {
  85. IGameEvent * event = gameeventmanager->CreateEvent( "smokegrenade_detonate" );
  86. if ( event )
  87. {
  88. event->SetInt( "userid", player->GetUserID() );
  89. event->SetFloat( "x", GetAbsOrigin().x );
  90. event->SetFloat( "y", GetAbsOrigin().y );
  91. event->SetFloat( "z", GetAbsOrigin().z );
  92. gameeventmanager->FireEvent( event );
  93. }
  94. }
  95. }
  96. m_hSmokeEffect = pGren;
  97. m_bDidSmokeEffect = true;
  98. EmitSound( "BaseSmokeEffect.Sound" );
  99. m_nRenderMode = kRenderTransColor;
  100. SetNextThink( gpGlobals->curtime + 5 );
  101. SetThink( &CSmokeGrenadeProjectile::Think_Fade );
  102. }
  103. // Fade the projectile out over time before making it disappear
  104. void CSmokeGrenadeProjectile::Think_Fade()
  105. {
  106. SetNextThink( gpGlobals->curtime );
  107. color32 c = GetRenderColor();
  108. c.a -= 1;
  109. SetRenderColor( c.r, c.b, c.g, c.a );
  110. if ( !c.a )
  111. {
  112. TheBots->RemoveGrenade( this );
  113. SetModelName( NULL_STRING );//invisible
  114. SetNextThink( gpGlobals->curtime + 20 );
  115. SetThink( &CSmokeGrenadeProjectile::Think_Remove ); // Spit out smoke for 10 seconds.
  116. SetSolid( SOLID_NONE );
  117. }
  118. }
  119. void CSmokeGrenadeProjectile::Think_Remove()
  120. {
  121. if ( m_hSmokeEffect.Get() )
  122. UTIL_Remove( m_hSmokeEffect );
  123. TheBots->RemoveGrenade( this );
  124. SetModelName( NULL_STRING );//invisible
  125. SetSolid( SOLID_NONE );
  126. SetMoveType( MOVETYPE_NONE );
  127. }
  128. //Implement this so we never call the base class,
  129. //but this should never be called either.
  130. void CSmokeGrenadeProjectile::Detonate( void )
  131. {
  132. Assert(!"Smoke grenade handles its own detonation");
  133. }
  134. void CSmokeGrenadeProjectile::Spawn()
  135. {
  136. SetModel( GRENADE_MODEL );
  137. BaseClass::Spawn();
  138. }
  139. void CSmokeGrenadeProjectile::Precache()
  140. {
  141. PrecacheModel( GRENADE_MODEL );
  142. PrecacheScriptSound( "BaseSmokeEffect.Sound" );
  143. PrecacheScriptSound( "SmokeGrenade.Bounce" );
  144. BaseClass::Precache();
  145. }
  146. void CSmokeGrenadeProjectile::BounceSound( void )
  147. {
  148. if ( !m_bDidSmokeEffect )
  149. {
  150. EmitSound( "SmokeGrenade.Bounce" );
  151. }
  152. }