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.

319 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "fx_impact.h"
  8. #include "engine/IEngineSound.h"
  9. #include "c_tf_player.h"
  10. #include "view.h"
  11. #include "tf_gamerules.h"
  12. #include "player_vs_environment/c_tf_tank_boss.h"
  13. #include "decals.h"
  14. #include "clientsideeffects.h"
  15. #include "fx_quad.h"
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Handle weapon impacts
  18. //-----------------------------------------------------------------------------
  19. static int g_MvMRobotImpactCount = 0;
  20. static int g_MvMTankImpactCount = 0;
  21. void ImpactCallback( const CEffectData &data )
  22. {
  23. trace_t tr;
  24. Vector vecOrigin, vecStart, vecShotDir;
  25. int iMaterial, iDamageType, iHitbox;
  26. short nSurfaceProp;
  27. C_BaseEntity *pEntity = ParseImpactData( data, &vecOrigin, &vecStart, &vecShotDir, nSurfaceProp, iMaterial, iDamageType, iHitbox );
  28. if ( !pEntity )
  29. return;
  30. bool bImpact = ( data.m_nDamageType != pEntity->GetTeamNumber() || pEntity->GetTeamNumber() < FIRST_GAME_TEAM );
  31. if ( data.m_nDamageType != pEntity->GetTeamNumber() && pEntity->IsPlayer() )
  32. {
  33. C_TFPlayer *pPlayer = ToTFPlayer( pEntity );
  34. // Don't impact spies disguised as the same team as this syringe/projectile
  35. if ( pPlayer && pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) )
  36. {
  37. if ( pPlayer->m_Shared.GetDisguiseTeam() == data.m_nDamageType )
  38. {
  39. bImpact = false;
  40. }
  41. }
  42. }
  43. if ( bImpact )
  44. {
  45. // We create lots of impact sounds, especially from Heavy's firing miniguns. To cut down on the number
  46. // of active sounds, we precull impact sounds that are too far from the current viewpoint.
  47. bool bIsMVM = TFGameRules() && TFGameRules()->IsMannVsMachineMode();
  48. int nApparentTeam = pEntity->GetTeamNumber();
  49. C_TFPlayer *pPlayer = ToTFPlayer( pEntity );
  50. if ( pPlayer && pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) )
  51. {
  52. nApparentTeam = pPlayer->m_Shared.GetDisguiseTeam();
  53. }
  54. bool bPlaySound = true;
  55. bool bIsRobotImpact = false;
  56. if ( bIsMVM && pPlayer && nApparentTeam == TF_TEAM_PVE_INVADERS )
  57. {
  58. bPlaySound = true;
  59. bIsRobotImpact = true;
  60. }
  61. else
  62. {
  63. bPlaySound = (MainViewOrigin() - vecOrigin).LengthSqr() < (1024*1024);
  64. }
  65. // If we hit, perform our custom effects and play the sound
  66. if ( Impact( vecOrigin, vecStart, iMaterial, iDamageType, iHitbox, pEntity, tr ) )
  67. {
  68. // Check for custom effects based on the Decal index
  69. PerformCustomEffects( vecOrigin, tr, vecShotDir, iMaterial, 1 );
  70. //Play a ricochet sound some of the time
  71. if ( bPlaySound && random->RandomInt(1,10) <= 3 && (iDamageType == DMG_BULLET) )
  72. {
  73. CLocalPlayerFilter filter;
  74. C_BaseEntity::EmitSound( filter, SOUND_FROM_WORLD, "Bounce.Shrapnel", &vecOrigin );
  75. }
  76. }
  77. if ( bPlaySound )
  78. {
  79. // every other one of the mvm impacts are emitted from the world to allow for ~2 impacts playing at a time
  80. if ( bIsRobotImpact )
  81. {
  82. //pEntity->EmitSound( "MVM_Robot.BulletImpact" );
  83. CLocalPlayerFilter filter;
  84. if ( g_MvMRobotImpactCount % 4 == 0 )
  85. {
  86. if( pPlayer->IsMiniBoss() )
  87. {
  88. C_BaseEntity::EmitSound( filter, pEntity->entindex(), "MVM_Giant.BulletImpact", &vecOrigin );
  89. }
  90. else
  91. {
  92. C_BaseEntity::EmitSound( filter, pEntity->entindex(), "MVM_Robot.BulletImpact", &vecOrigin );
  93. }
  94. }
  95. else
  96. {
  97. C_BaseEntity::EmitSound( filter, SOUND_FROM_WORLD, "MVM_Robot.BulletImpact", &vecOrigin );
  98. }
  99. g_MvMRobotImpactCount++;
  100. }
  101. else if ( bIsMVM && dynamic_cast< C_TFTankBoss* >( pEntity ) )
  102. {
  103. //pEntity->EmitSound( "MVM_Robot.BulletImpact" );
  104. CLocalPlayerFilter filter;
  105. if ( g_MvMTankImpactCount % 4 == 0 )
  106. {
  107. C_BaseEntity::EmitSound( filter, pEntity->entindex(), "MVM_Tank.BulletImpact", &vecOrigin );
  108. }
  109. else
  110. {
  111. C_BaseEntity::EmitSound( filter, SOUND_FROM_WORLD, "MVM_Tank.BulletImpact", &vecOrigin );
  112. }
  113. g_MvMTankImpactCount++;
  114. }
  115. else
  116. {
  117. PlayImpactSound( pEntity, tr, vecOrigin, nSurfaceProp );
  118. }
  119. }
  120. }
  121. }
  122. DECLARE_CLIENT_EFFECT( "Impact", ImpactCallback );
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. //-----------------------------------------------------------------------------
  126. void TFSplashCallbackHelper( const CEffectData &data, const char *pszEffectName )
  127. {
  128. Vector normal;
  129. AngleVectors( data.m_vAngles, &normal );
  130. CSmartPtr<CNewParticleEffect> pEffect = CNewParticleEffect::Create( NULL, pszEffectName );
  131. if ( pEffect->IsValid() )
  132. {
  133. pEffect->SetSortOrigin( data.m_vOrigin );
  134. pEffect->SetControlPoint( 0, data.m_vOrigin );
  135. pEffect->SetControlPointOrientation( 0, Vector(1,0,0), Vector(0,1,0), Vector(0,0,1) );
  136. }
  137. }
  138. void TFSplashCallback( const CEffectData &data )
  139. {
  140. TFSplashCallbackHelper( data, "water_bulletsplash01" );
  141. }
  142. DECLARE_CLIENT_EFFECT( "tf_gunshotsplash", TFSplashCallback );
  143. void TFSplashCallbackMinigun( const CEffectData &data )
  144. {
  145. TFSplashCallbackHelper( data, "water_bulletsplash01_minigun" );
  146. }
  147. DECLARE_CLIENT_EFFECT( "tf_gunshotsplash_minigun", TFSplashCallbackMinigun );
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. //-----------------------------------------------------------------------------
  151. void TFPaintBombSplashCallback( const CEffectData &data )
  152. {
  153. C_BaseEntity *pEntity = data.GetEntity();
  154. if ( !pEntity )
  155. return;
  156. trace_t tr;
  157. // Get a color if it exists
  158. Color cColor;
  159. if ( data.m_bCustomColors )
  160. {
  161. cColor.SetColor( data.m_CustomColors.m_vecColor1.x * 255, data.m_CustomColors.m_vecColor1.y * 255, data.m_CustomColors.m_vecColor1.z * 255, 255 );
  162. }
  163. else
  164. {
  165. cColor.SetColor( 255, 255, 255, 255 );
  166. }
  167. CTFPlayer *pPlayer = ToTFPlayer( pEntity );
  168. // Build out the decal name from nMaterial
  169. int nPaintIndex = data.m_nMaterial;
  170. // Decals
  171. // Special case for world entity with hitbox:
  172. if ( (pEntity->index == 0) && (data.m_nHitBox != 0) )
  173. {
  174. int nMaterial = decalsystem->GetDecalIndexForName( VarArgs( "%s%d%s", "TF_Paint_", nPaintIndex, "_Prop" ) );
  175. staticpropmgr->AddColorDecalToStaticProp( data.m_vStart, data.m_vOrigin, data.m_nHitBox - 1, nMaterial, false, tr, true, cColor );
  176. }
  177. else
  178. {
  179. int nMaterial = 0;
  180. if ( pEntity->index == 0 )
  181. {
  182. nMaterial = decalsystem->GetDecalIndexForName( VarArgs( "%s%d%s", "TF_Paint_", nPaintIndex, "_World" ) );
  183. }
  184. else if ( pPlayer )
  185. {
  186. nMaterial = decalsystem->GetDecalIndexForName( VarArgs( "%s%d%s", "TF_Paint_", nPaintIndex, "_Player" ) );
  187. }
  188. else
  189. {
  190. nMaterial = decalsystem->GetDecalIndexForName( VarArgs( "%s%d%s", "TF_Paint_", nPaintIndex, "_Prop" ) );
  191. }
  192. pEntity->AddColoredDecal( data.m_vStart, data.m_vOrigin, data.m_vOrigin, data.m_nHitBox, nMaterial, false, tr, cColor );
  193. }
  194. }
  195. DECLARE_CLIENT_EFFECT( "tf_paint_bomb", TFPaintBombSplashCallback );
  196. //-----------------------------------------------------------------------------
  197. // Purpose:
  198. //-----------------------------------------------------------------------------
  199. void TFEnergyShieldImpactCallback( const CEffectData &data )
  200. {
  201. // Customized EnergySplash
  202. C_BaseEntity *pEntity = data.GetEntity();
  203. if ( !pEntity )
  204. return;
  205. Vector vecPos = data.m_vOrigin;
  206. Vector vecNormal = data.m_vNormal;
  207. Vector vecOffset = vecPos + ( vecNormal * 2.f );
  208. float flMagnitude = data.m_flMagnitude;
  209. float flFlashAlpha = 0.25f * flMagnitude;
  210. float flLingerAlpha = 0.2f * flMagnitude;
  211. float flFlashLife = 0.1f * flMagnitude;
  212. float flLingerLife = 0.2f * flMagnitude;
  213. float flRed = random->RandomFloat( 0.7f, 1.f );
  214. float flGreen = random->RandomFloat( 0.25f, 0.45f );
  215. float flBlue = random->RandomFloat( 0.25f, 0.45f );
  216. // Impact
  217. FX_AddQuad( vecPos, // Origin
  218. vecNormal, // Normal
  219. 16.f * flMagnitude, // Start size
  220. 0.f, // End size
  221. 0.5f, // Size bias
  222. flFlashAlpha, // Start alpha
  223. 0.f, // End alpha
  224. 0.1f, // Alpha bias
  225. random->RandomInt( 0, 360 ), // Yaw
  226. 0.f, // Delta Yaw
  227. Vector( flRed, flGreen, flBlue ), // Color
  228. flFlashLife, // Lifetime
  229. "effects/circle_nocull",
  230. (FXQUAD_BIAS_SCALE|FXQUAD_BIAS_ALPHA) );
  231. // Burn
  232. FX_AddQuad( vecPos, // Origin
  233. vecNormal, // Normal
  234. 6.f * flMagnitude, // Start size
  235. 18.f * flMagnitude, // End size
  236. 0.75f, // Size bias
  237. flLingerAlpha, // Start alpha
  238. 0.f, // End alpha
  239. 0.4f, // Alpha bias
  240. random->RandomInt( 0, 360 ), // Yaw
  241. 0.f, // Delta Yaw
  242. Vector( flRed, flGreen, flBlue ), // Color
  243. flLingerLife, // Lifetime
  244. "effects/circle_nocull",
  245. (FXQUAD_BIAS_SCALE|FXQUAD_BIAS_ALPHA) );
  246. CSmartPtr< CSimpleEmitter > pEmitter;
  247. pEmitter = CSimpleEmitter::Create( "C_EntityDissolve" );
  248. pEmitter->SetSortOrigin( vecPos );
  249. PMaterialHandle pMaterialSpark = pEmitter->GetPMaterial( "effects/spark" );
  250. if ( !pMaterialSpark )
  251. return;
  252. SimpleParticle *sParticle;
  253. for ( int j = 0; j < 6; j++ )
  254. {
  255. vecOffset.x = random->RandomFloat( -8.f, 8.f );
  256. vecOffset.y = random->RandomFloat( -8.f, 8.f );
  257. vecOffset.z = random->RandomFloat( 0.f, 4.f );
  258. vecOffset += vecPos;
  259. sParticle = (SimpleParticle *) pEmitter->AddParticle( sizeof(SimpleParticle), pMaterialSpark, vecOffset );
  260. if ( !sParticle )
  261. return;
  262. sParticle->m_vecVelocity = Vector( random->RandomFloat( -8.f, 8.f ), random->RandomFloat( -8.f, 8.f ), random->RandomFloat( 16.f, 64.f ) );
  263. sParticle->m_uchStartSize = random->RandomFloat( 1.5f, 3.f );
  264. sParticle->m_flDieTime = random->RandomFloat( 0.2f, 0.4f );
  265. sParticle->m_flLifetime = 0.f;
  266. sParticle->m_flRoll = random->RandomInt( 0, 360 );
  267. float flAlpha = 255.f;
  268. sParticle->m_flRollDelta = random->RandomFloat( -16.f, 16.f );
  269. sParticle->m_uchColor[0] = random->RandomInt( 120, 150 );
  270. sParticle->m_uchColor[1] = random->RandomInt( 40, 70 );
  271. sParticle->m_uchColor[2] = random->RandomInt( 40, 70 );
  272. sParticle->m_uchStartAlpha = flAlpha;
  273. sParticle->m_uchEndAlpha = 0;
  274. sParticle->m_uchEndSize = 0;
  275. }
  276. }
  277. DECLARE_CLIENT_EFFECT( "EnergyShieldImpact", TFEnergyShieldImpactCallback );