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.

316 lines
8.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "engine/IEngineSound.h"
  10. #include "view.h"
  11. #include "fx_line.h"
  12. #include "fx_discreetline.h"
  13. #include "fx_quad.h"
  14. #include "clientsideeffects.h"
  15. #include "SoundEmitterSystem/isoundemittersystembase.h"
  16. #include "tier0/vprof.h"
  17. #include "collisionutils.h"
  18. #include "clienteffectprecachesystem.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. //FIXME: All these functions will be moved out to FX_Main.CPP or a individual folder
  22. CLIENTEFFECT_REGISTER_BEGIN( PrecacheEffectsTest )
  23. CLIENTEFFECT_MATERIAL( "effects/spark" )
  24. CLIENTEFFECT_MATERIAL( "effects/gunshiptracer" )
  25. CLIENTEFFECT_MATERIAL( "effects/bluespark" )
  26. CLIENTEFFECT_REGISTER_END()
  27. //-----------------------------------------------------------------------------
  28. // Purpose:
  29. // Input : &data -
  30. //-----------------------------------------------------------------------------
  31. void FX_AddLine( const FXLineData_t &data )
  32. {
  33. CFXLine *t = new CFXLine( "Line", data );
  34. assert( t );
  35. //Throw it into the list
  36. clienteffects->AddEffect( t );
  37. }
  38. /*
  39. ==================================================
  40. FX_AddStaticLine
  41. ==================================================
  42. */
  43. void FX_AddStaticLine( const Vector& start, const Vector& end, float scale, float life, const char *materialName, unsigned char flags )
  44. {
  45. CFXStaticLine *t = new CFXStaticLine( "StaticLine", start, end, scale, life, materialName, flags );
  46. assert( t );
  47. //Throw it into the list
  48. clienteffects->AddEffect( t );
  49. }
  50. /*
  51. ==================================================
  52. FX_AddDiscreetLine
  53. ==================================================
  54. */
  55. void FX_AddDiscreetLine( const Vector& start, const Vector& direction, float velocity, float length, float clipLength, float scale, float life, const char *shader )
  56. {
  57. CFXDiscreetLine *t = new CFXDiscreetLine( "Line", start, direction, velocity, length, clipLength, scale, life, shader );
  58. assert( t );
  59. //Throw it into the list
  60. clienteffects->AddEffect( t );
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. void FX_AddQuad( const FXQuadData_t &data )
  66. {
  67. CFXQuad *quad = new CFXQuad( data );
  68. Assert( quad != NULL );
  69. clienteffects->AddEffect( quad );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: Parameter heavy version
  73. //-----------------------------------------------------------------------------
  74. void FX_AddQuad( const Vector &origin,
  75. const Vector &normal,
  76. float startSize,
  77. float endSize,
  78. float sizeBias,
  79. float startAlpha,
  80. float endAlpha,
  81. float alphaBias,
  82. float yaw,
  83. float deltaYaw,
  84. const Vector &color,
  85. float lifeTime,
  86. const char *shader,
  87. unsigned int flags )
  88. {
  89. FXQuadData_t data;
  90. //Setup the data
  91. data.SetAlpha( startAlpha, endAlpha );
  92. data.SetScale( startSize, endSize );
  93. data.SetFlags( flags );
  94. data.SetMaterial( shader );
  95. data.SetNormal( normal );
  96. data.SetOrigin( origin );
  97. data.SetLifeTime( lifeTime );
  98. data.SetColor( color[0], color[1], color[2] );
  99. data.SetScaleBias( sizeBias );
  100. data.SetAlphaBias( alphaBias );
  101. data.SetYaw( yaw, deltaYaw );
  102. //Output it
  103. FX_AddQuad( data );
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose: Creates a test effect
  107. //-----------------------------------------------------------------------------
  108. void CreateTestEffect( void )
  109. {
  110. //NOTENOTE: Add any test effects here
  111. //FX_BulletPass( NULL, NULL );
  112. }
  113. /*
  114. ==================================================
  115. FX_PlayerTracer
  116. ==================================================
  117. */
  118. #define TRACER_BASE_OFFSET 8
  119. void FX_PlayerTracer( Vector& start, Vector& end )
  120. {
  121. VPROF_BUDGET( "FX_PlayerTracer", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  122. Vector shotDir, dStart, dEnd;
  123. float length;
  124. //Find the direction of the tracer
  125. VectorSubtract( end, start, shotDir );
  126. length = VectorNormalize( shotDir );
  127. //We don't want to draw them if they're too close to us
  128. if ( length < 256 )
  129. return;
  130. //Randomly place the tracer along this line, with a random length
  131. VectorMA( start, TRACER_BASE_OFFSET + random->RandomFloat( -24.0f, 64.0f ), shotDir, dStart );
  132. VectorMA( dStart, ( length * random->RandomFloat( 0.1f, 0.6f ) ), shotDir, dEnd );
  133. //Create the line
  134. CFXStaticLine *t;
  135. const char *materialName;
  136. //materialName = ( random->RandomInt( 0, 1 ) ) ? "effects/tracer_middle" : "effects/tracer_middle2";
  137. materialName = "effects/spark";
  138. t = new CFXStaticLine( "Tracer", dStart, dEnd, random->RandomFloat( 0.5f, 0.75f ), 0.01f, materialName, 0 );
  139. assert( t );
  140. //Throw it into the list
  141. clienteffects->AddEffect( t );
  142. }
  143. /*
  144. ==================================================
  145. FX_Tracer
  146. ==================================================
  147. */
  148. // Tracer must be this close to be considered for hearing
  149. #define TRACER_MAX_HEAR_DIST (6*12)
  150. #define TRACER_SOUND_TIME_MIN 0.1f
  151. #define TRACER_SOUND_TIME_MAX 0.1f
  152. class CBulletWhizTimer : public CAutoGameSystem
  153. {
  154. public:
  155. CBulletWhizTimer( char const *name ) : CAutoGameSystem( name )
  156. {
  157. }
  158. void LevelInitPreEntity()
  159. {
  160. m_nextWhizTime = 0;
  161. }
  162. float m_nextWhizTime;
  163. };
  164. // Client-side tracking for whiz noises
  165. CBulletWhizTimer g_BulletWhiz( "CBulletWhizTimer" );
  166. //-----------------------------------------------------------------------------
  167. // Purpose:
  168. // Input : &vecStart -
  169. // &vecDir -
  170. // iTracerType -
  171. //-----------------------------------------------------------------------------
  172. #define LISTENER_HEIGHT 24
  173. void FX_TracerSound( const Vector &start, const Vector &end, int iTracerType )
  174. {
  175. const char *pszSoundName = NULL;
  176. float flWhizDist = TRACER_MAX_HEAR_DIST;
  177. float flMinWhizTime = TRACER_SOUND_TIME_MIN;
  178. float flMaxWhizTime = TRACER_SOUND_TIME_MAX;
  179. Vector vecListenOrigin = MainViewOrigin();
  180. switch( iTracerType )
  181. {
  182. case TRACER_TYPE_DEFAULT:
  183. {
  184. pszSoundName = "Bullets.DefaultNearmiss";
  185. flWhizDist = 24;
  186. Ray_t bullet, listener;
  187. bullet.Init( start, end );
  188. Vector vecLower = vecListenOrigin;
  189. vecLower.z -= LISTENER_HEIGHT;
  190. listener.Init( vecListenOrigin, vecLower );
  191. float s, t;
  192. IntersectRayWithRay( bullet, listener, s, t );
  193. t = clamp( t, 0.f, 1.f );
  194. vecListenOrigin.z -= t * LISTENER_HEIGHT;
  195. }
  196. break;
  197. case TRACER_TYPE_GUNSHIP:
  198. pszSoundName = "Bullets.GunshipNearmiss";
  199. break;
  200. case TRACER_TYPE_STRIDER:
  201. pszSoundName = "Bullets.StriderNearmiss";
  202. break;
  203. case TRACER_TYPE_WATERBULLET:
  204. pszSoundName = "Underwater.BulletImpact";
  205. flWhizDist = 48;
  206. flMinWhizTime = 0.3f;
  207. flMaxWhizTime = 0.6f;
  208. break;
  209. default:
  210. return;
  211. }
  212. if( !pszSoundName )
  213. return;
  214. // Is it time yet?
  215. float dt = g_BulletWhiz.m_nextWhizTime - gpGlobals->curtime;
  216. if ( dt > 0 )
  217. return;
  218. // Did the thing pass close enough to our head?
  219. float vDist = CalcDistanceSqrToLineSegment( vecListenOrigin, start, end );
  220. if ( vDist >= (flWhizDist * flWhizDist) )
  221. return;
  222. CSoundParameters params;
  223. if( C_BaseEntity::GetParametersForSound( pszSoundName, params, NULL ) )
  224. {
  225. // Get shot direction
  226. Vector shotDir;
  227. VectorSubtract( end, start, shotDir );
  228. VectorNormalize( shotDir );
  229. CLocalPlayerFilter filter;
  230. enginesound->EmitSound( filter, SOUND_FROM_WORLD, CHAN_STATIC, params.soundname,
  231. params.volume, SNDLVL_TO_ATTN(params.soundlevel), 0, params.pitch, 0, &start, &shotDir, NULL);
  232. }
  233. // FIXME: This has a bad behavior when both bullet + strider shots are whizzing by at the same time
  234. // Could use different timers for the different types.
  235. // Don't play another bullet whiz for this client until this time has run out
  236. g_BulletWhiz.m_nextWhizTime = gpGlobals->curtime + random->RandomFloat( flMinWhizTime, flMaxWhizTime );
  237. }
  238. void FX_Tracer( Vector& start, Vector& end, int velocity, bool makeWhiz )
  239. {
  240. VPROF_BUDGET( "FX_Tracer", VPROF_BUDGETGROUP_PARTICLE_RENDERING );
  241. //Don't make small tracers
  242. float dist;
  243. Vector dir;
  244. VectorSubtract( end, start, dir );
  245. dist = VectorNormalize( dir );
  246. // Don't make short tracers.
  247. if ( dist >= 256 )
  248. {
  249. float length = random->RandomFloat( 64.0f, 128.0f );
  250. float life = ( dist + length ) / velocity; //NOTENOTE: We want the tail to finish its run as well
  251. //Add it
  252. FX_AddDiscreetLine( start, dir, velocity, length, dist, random->RandomFloat( 0.75f, 0.9f ), life, "effects/spark" );
  253. }
  254. if( makeWhiz )
  255. {
  256. FX_TracerSound( start, end, TRACER_TYPE_DEFAULT );
  257. }
  258. }