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.

151 lines
4.3 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.h"
  8. #include "c_te_effect_dispatch.h"
  9. #include "tier0/vprof.h"
  10. #include "clientsideeffects.h"
  11. #include "clienteffectprecachesystem.h"
  12. #include "view.h"
  13. #include "collisionutils.h"
  14. #include "SoundEmitterSystem/isoundemittersystembase.h"
  15. #include "engine/IEngineSound.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. CLIENTEFFECT_REGISTER_BEGIN( PrecacheDodTracers )
  19. CLIENTEFFECT_MATERIAL( "effects/fainttracer" )
  20. CLIENTEFFECT_MATERIAL( "effects/spark" )
  21. CLIENTEFFECT_REGISTER_END()
  22. #define LISTENER_HEIGHT 24
  23. #define TRACER_TYPE_FAINT 4
  24. void FX_DoDTracerSound( const Vector &start, const Vector &end, int iTracerType )
  25. {
  26. // don't play on very short hits
  27. if ( ( start - end ).Length() < 200 )
  28. return;
  29. const char *pszSoundName = "Bullets.DefaultNearmiss";
  30. float flWhizDist = 64;
  31. Vector vecListenOrigin = MainViewOrigin();
  32. switch( iTracerType )
  33. {
  34. case TRACER_TYPE_DEFAULT:
  35. flWhizDist = 96;
  36. // fall through !
  37. default:
  38. {
  39. Ray_t bullet, listener;
  40. bullet.Init( start, end );
  41. Vector vecLower = vecListenOrigin;
  42. vecLower.z -= LISTENER_HEIGHT;
  43. listener.Init( vecListenOrigin, vecLower );
  44. float s, t;
  45. IntersectRayWithRay( bullet, listener, s, t );
  46. t = clamp( t, 0, 1 );
  47. vecListenOrigin.z -= t * LISTENER_HEIGHT;
  48. }
  49. break;
  50. }
  51. static float flNextWhizTime = 0;
  52. // Is it time yet?
  53. float dt = flNextWhizTime - gpGlobals->curtime;
  54. if ( dt > 0 )
  55. return;
  56. // Did the thing pass close enough to our head?
  57. float vDist = CalcDistanceSqrToLineSegment( vecListenOrigin, start, end );
  58. if ( vDist >= (flWhizDist * flWhizDist) )
  59. return;
  60. CSoundParameters params;
  61. if( C_BaseEntity::GetParametersForSound( pszSoundName, params, NULL ) )
  62. {
  63. // Get shot direction
  64. Vector shotDir;
  65. VectorSubtract( end, start, shotDir );
  66. VectorNormalize( shotDir );
  67. CLocalPlayerFilter filter;
  68. enginesound->EmitSound( filter, SOUND_FROM_WORLD, CHAN_STATIC, params.soundname,
  69. params.volume, SNDLVL_TO_ATTN(params.soundlevel), 0, params.pitch, 0, &start, &shotDir, NULL);
  70. }
  71. // Don't play another bullet whiz for this client until this time has run out
  72. flNextWhizTime = gpGlobals->curtime + 0.1f;
  73. }
  74. void FX_FaintTracer( Vector& start, Vector& end )
  75. {
  76. if ( random->RandomInt( 0, 2 ) == 0 )
  77. {
  78. //Don't make small tracers
  79. float dist;
  80. Vector dir;
  81. int velocity = 5000;
  82. VectorSubtract( end, start, dir );
  83. dist = VectorNormalize( dir );
  84. float length = random->RandomFloat( 32.0f, 64.0f );
  85. float life = ( dist + length ) / velocity; //NOTENOTE: We want the tail to finish its run as well
  86. //Add it
  87. FX_AddDiscreetLine( start, dir, velocity, length, dist, random->RandomFloat( 0.75f, 0.9f ), life, "effects/fainttracer" );
  88. }
  89. FX_DoDTracerSound( start, end, TRACER_TYPE_DEFAULT+1 ); //not default!
  90. }
  91. void FX_BrightTracer( Vector& start, Vector& end )
  92. {
  93. //Don't make small tracers
  94. float dist;
  95. Vector dir;
  96. int velocity = 5000;
  97. VectorSubtract( end, start, dir );
  98. dist = VectorNormalize( dir );
  99. // Don't make short tracers.
  100. float length = random->RandomFloat( 64.0f, 128.0f );
  101. float life = ( dist + length ) / velocity; //NOTENOTE: We want the tail to finish its run as well
  102. //Add it
  103. FX_AddDiscreetLine( start, dir, velocity, length, dist, random->RandomFloat( 0.75f, 0.9f ), life, "effects/spark" );
  104. FX_DoDTracerSound( start, end, TRACER_TYPE_DEFAULT );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Faint Tracer for most dod weapons
  108. //-----------------------------------------------------------------------------
  109. void FaintTracerCallback( const CEffectData &data )
  110. {
  111. FX_FaintTracer( (Vector&)data.m_vStart, (Vector&)data.m_vOrigin );
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose: Bright Tracer for Machine Guns
  115. //-----------------------------------------------------------------------------
  116. void BrightTracerCallback( const CEffectData &data )
  117. {
  118. FX_BrightTracer( (Vector&)data.m_vStart, (Vector&)data.m_vOrigin );
  119. }
  120. DECLARE_CLIENT_EFFECT( "FaintTracer", FaintTracerCallback );
  121. DECLARE_CLIENT_EFFECT( "BrightTracer", BrightTracerCallback );