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.

275 lines
9.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Game-specific impact effect hooks
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "fx_impact.h"
  8. #include "fx.h"
  9. #include "decals.h"
  10. #include "fx_quad.h"
  11. #include "fx_sparks.h"
  12. #include "tier0/vprof.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Handle jeep impacts
  17. //-----------------------------------------------------------------------------
  18. void ImpactJeepCallback( const CEffectData &data )
  19. {
  20. trace_t tr;
  21. Vector vecOrigin, vecStart, vecShotDir;
  22. int iMaterial, iDamageType, iHitbox;
  23. short nSurfaceProp;
  24. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  25. if ( !pEntity )
  26. {
  27. // This happens for impacts that occur on an object that's then destroyed.
  28. // Clear out the fraction so it uses the server's data
  29. tr.fraction = 1.0;
  30. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  31. return;
  32. }
  33. // If we hit, perform our custom effects and play the sound
  34. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr ) )
  35. {
  36. // Check for custom effects based on the Decal index
  37. PerformCustomEffects( vecOrigin, tr, vecShotDir, iMaterial, 2 );
  38. }
  39. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  40. }
  41. DECLARE_CLIENT_EFFECT( "ImpactJeep", ImpactJeepCallback );
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Handle gauss impacts
  44. //-----------------------------------------------------------------------------
  45. void ImpactGaussCallback( const CEffectData &data )
  46. {
  47. trace_t tr;
  48. Vector vecOrigin, vecStart, vecShotDir;
  49. int iMaterial, iDamageType, iHitbox;
  50. short nSurfaceProp;
  51. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  52. if ( !pEntity )
  53. {
  54. // This happens for impacts that occur on an object that's then destroyed.
  55. // Clear out the fraction so it uses the server's data
  56. tr.fraction = 1.0;
  57. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  58. return;
  59. }
  60. // If we hit, perform our custom effects and play the sound
  61. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr ) )
  62. {
  63. // Check for custom effects based on the Decal index
  64. PerformCustomEffects( vecOrigin, tr, vecShotDir, iMaterial, 2 );
  65. }
  66. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  67. }
  68. DECLARE_CLIENT_EFFECT( "ImpactGauss", ImpactGaussCallback );
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Handle weapon impacts
  71. //-----------------------------------------------------------------------------
  72. void ImpactCallback( const CEffectData &data )
  73. {
  74. VPROF_BUDGET( "ImpactCallback", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  75. trace_t tr;
  76. Vector vecOrigin, vecStart, vecShotDir;
  77. int iMaterial, iDamageType, iHitbox;
  78. short nSurfaceProp;
  79. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  80. if ( !pEntity )
  81. {
  82. // This happens for impacts that occur on an object that's then destroyed.
  83. // Clear out the fraction so it uses the server's data
  84. tr.fraction = 1.0;
  85. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  86. return;
  87. }
  88. // If we hit, perform our custom effects and play the sound
  89. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr ) )
  90. {
  91. // Check for custom effects based on the Decal index
  92. PerformCustomEffects( vecOrigin, tr, vecShotDir, iMaterial, 1.0 );
  93. }
  94. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  95. }
  96. DECLARE_CLIENT_EFFECT( "Impact", ImpactCallback );
  97. //-----------------------------------------------------------------------------
  98. // Purpose:
  99. // Input : &origin -
  100. // &normal -
  101. // scale -
  102. //-----------------------------------------------------------------------------
  103. void FX_AirboatGunImpact( const Vector &origin, const Vector &normal, float scale )
  104. {
  105. #ifdef _XBOX
  106. Vector offset = origin + ( normal * 1.0f );
  107. CSmartPtr<CTrailParticles> sparkEmitter = CTrailParticles::Create( "FX_MetalSpark 1" );
  108. if ( sparkEmitter == NULL )
  109. return;
  110. //Setup our information
  111. sparkEmitter->SetSortOrigin( offset );
  112. sparkEmitter->SetFlag( bitsPARTICLE_TRAIL_VELOCITY_DAMPEN );
  113. sparkEmitter->SetVelocityDampen( 8.0f );
  114. sparkEmitter->SetGravity( 800.0f );
  115. sparkEmitter->SetCollisionDamped( 0.25f );
  116. sparkEmitter->GetBinding().SetBBox( offset - Vector( 32, 32, 32 ), offset + Vector( 32, 32, 32 ) );
  117. int numSparks = random->RandomInt( 4, 8 );
  118. TrailParticle *pParticle;
  119. PMaterialHandle hMaterial = sparkEmitter->GetPMaterial( "effects/spark" );
  120. Vector dir;
  121. float length = 0.1f;
  122. //Dump out sparks
  123. for ( int i = 0; i < numSparks; i++ )
  124. {
  125. pParticle = (TrailParticle *) sparkEmitter->AddParticle( sizeof(TrailParticle), hMaterial, offset );
  126. if ( pParticle == NULL )
  127. return;
  128. pParticle->m_flLifetime = 0.0f;
  129. pParticle->m_flDieTime = random->RandomFloat( 0.05f, 0.1f );
  130. float spreadOfs = random->RandomFloat( 0.0f, 2.0f );
  131. dir[0] = normal[0] + random->RandomFloat( -(0.5f*spreadOfs), (0.5f*spreadOfs) );
  132. dir[1] = normal[1] + random->RandomFloat( -(0.5f*spreadOfs), (0.5f*spreadOfs) );
  133. dir[2] = normal[2] + random->RandomFloat( -(0.5f*spreadOfs), (0.5f*spreadOfs) );
  134. VectorNormalize( dir );
  135. pParticle->m_flWidth = random->RandomFloat( 1.0f, 4.0f );
  136. pParticle->m_flLength = random->RandomFloat( length*0.25f, length );
  137. pParticle->m_vecVelocity = dir * random->RandomFloat( (128.0f*(2.0f-spreadOfs)), (512.0f*(2.0f-spreadOfs)) );
  138. Color32Init( pParticle->m_color, 255, 255, 255, 255 );
  139. }
  140. #else
  141. // Normal metal spark
  142. FX_MetalSpark( origin, normal, normal, (int) scale );
  143. #endif // _XBOX
  144. // Add a quad to highlite the hit point
  145. FX_AddQuad( origin,
  146. normal,
  147. random->RandomFloat( 16, 32 ),
  148. random->RandomFloat( 32, 48 ),
  149. 0.75f,
  150. 1.0f,
  151. 0.0f,
  152. 0.4f,
  153. random->RandomInt( 0, 360 ),
  154. 0,
  155. Vector( 1.0f, 1.0f, 1.0f ),
  156. 0.05f,
  157. "effects/combinemuzzle2_nocull",
  158. (FXQUAD_BIAS_SCALE|FXQUAD_BIAS_ALPHA) );
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Purpose: Handle weapon impacts from the airboat gun shooting (cheaper versions)
  162. //-----------------------------------------------------------------------------
  163. void ImpactAirboatGunCallback( const CEffectData &data )
  164. {
  165. VPROF_BUDGET( "ImpactAirboatGunCallback", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  166. trace_t tr;
  167. Vector vecOrigin, vecStart, vecShotDir;
  168. int iMaterial, iDamageType, iHitbox;
  169. short nSurfaceProp;
  170. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  171. if ( !pEntity )
  172. {
  173. // This happens for impacts that occur on an object that's then destroyed.
  174. // Clear out the fraction so it uses the server's data
  175. tr.fraction = 1.0;
  176. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  177. return;
  178. }
  179. #if !defined( _XBOX )
  180. // If we hit, perform our custom effects and play the sound. Don't create decals
  181. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr, IMPACT_NODECAL | IMPACT_REPORT_RAGDOLL_IMPACTS ) )
  182. {
  183. FX_AirboatGunImpact( vecOrigin, tr.plane.normal, 2 );
  184. }
  185. #else
  186. FX_AirboatGunImpact( vecOrigin, tr.plane.normal, 1 );
  187. #endif
  188. }
  189. DECLARE_CLIENT_EFFECT( "AirboatGunImpact", ImpactAirboatGunCallback );
  190. //-----------------------------------------------------------------------------
  191. // Purpose: Handle weapon impacts from the helicopter shooting (cheaper versions)
  192. //-----------------------------------------------------------------------------
  193. void ImpactHelicopterCallback( const CEffectData &data )
  194. {
  195. VPROF_BUDGET( "ImpactHelicopterCallback", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  196. trace_t tr;
  197. Vector vecOrigin, vecStart, vecShotDir;
  198. int iMaterial, iDamageType, iHitbox;
  199. short nSurfaceProp;
  200. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  201. if ( !pEntity )
  202. {
  203. // This happens for impacts that occur on an object that's then destroyed.
  204. // Clear out the fraction so it uses the server's data
  205. tr.fraction = 1.0;
  206. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  207. return;
  208. }
  209. // If we hit, perform our custom effects and play the sound. Don't create decals
  210. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr, IMPACT_NODECAL | IMPACT_REPORT_RAGDOLL_IMPACTS ) )
  211. {
  212. FX_AirboatGunImpact( vecOrigin, tr.plane.normal, IsXbox() ? 1 : 2 );
  213. // Only do metal + computer custom effects
  214. if ( (iMaterial == CHAR_TEX_METAL) || (iMaterial == CHAR_TEX_COMPUTER) )
  215. {
  216. PerformCustomEffects( vecOrigin, tr, vecShotDir, iMaterial, 1.0, FLAGS_CUSTIOM_EFFECTS_NOFLECKS );
  217. }
  218. }
  219. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  220. }
  221. DECLARE_CLIENT_EFFECT( "HelicopterImpact", ImpactHelicopterCallback );