Counter Strike : Global Offensive Source Code
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.

1974 lines
59 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "cbase.h"
  9. #include "c_smoke_trail.h"
  10. #include "fx.h"
  11. #include "engine/ivdebugoverlay.h"
  12. #include "engine/IEngineSound.h"
  13. #include "c_te_effect_dispatch.h"
  14. #include "glow_overlay.h"
  15. #include "fx_explosion.h"
  16. #include "tier1/keyvalues.h"
  17. #include "toolframework_client.h"
  18. #include "view.h"
  19. #include "precache_register.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. //
  23. // CRocketTrailParticle
  24. //
  25. class CRocketTrailParticle : public CSimpleEmitter
  26. {
  27. public:
  28. CRocketTrailParticle( const char *pDebugName ) : CSimpleEmitter( pDebugName ) {}
  29. //Create
  30. static CRocketTrailParticle *Create( const char *pDebugName )
  31. {
  32. return new CRocketTrailParticle( pDebugName );
  33. }
  34. //Roll
  35. virtual float UpdateRoll( SimpleParticle *pParticle, float timeDelta )
  36. {
  37. pParticle->m_flRoll += pParticle->m_flRollDelta * timeDelta;
  38. pParticle->m_flRollDelta += pParticle->m_flRollDelta * ( timeDelta * -8.0f );
  39. //Cap the minimum roll
  40. if ( fabs( pParticle->m_flRollDelta ) < 0.5f )
  41. {
  42. pParticle->m_flRollDelta = ( pParticle->m_flRollDelta > 0.0f ) ? 0.5f : -0.5f;
  43. }
  44. return pParticle->m_flRoll;
  45. }
  46. //Alpha
  47. virtual float UpdateAlpha( const SimpleParticle *pParticle )
  48. {
  49. return ( ((float)pParticle->m_uchStartAlpha/255.0f) * sin( M_PI * (pParticle->m_flLifetime / pParticle->m_flDieTime) ) );
  50. }
  51. private:
  52. CRocketTrailParticle( const CRocketTrailParticle & );
  53. };
  54. //
  55. // CSmokeParticle
  56. //
  57. class CSmokeParticle : public CSimpleEmitter
  58. {
  59. public:
  60. CSmokeParticle( const char *pDebugName ) : CSimpleEmitter( pDebugName ) {}
  61. //Create
  62. static CSmokeParticle *Create( const char *pDebugName )
  63. {
  64. return new CSmokeParticle( pDebugName );
  65. }
  66. //Alpha
  67. virtual float UpdateAlpha( const SimpleParticle *pParticle )
  68. {
  69. return ( ((float)pParticle->m_uchStartAlpha/255.0f) * sin( M_PI * (pParticle->m_flLifetime / pParticle->m_flDieTime) ) );
  70. }
  71. //Color
  72. virtual Vector UpdateColor( const SimpleParticle *pParticle )
  73. {
  74. Vector color;
  75. float tLifetime = pParticle->m_flLifetime / pParticle->m_flDieTime;
  76. float ramp = 1.0f - tLifetime;
  77. color[0] = ( (float) pParticle->m_uchColor[0] * ramp ) / 255.0f;
  78. color[1] = ( (float) pParticle->m_uchColor[1] * ramp ) / 255.0f;
  79. color[2] = ( (float) pParticle->m_uchColor[2] * ramp ) / 255.0f;
  80. return color;
  81. }
  82. //Roll
  83. virtual float UpdateRoll( SimpleParticle *pParticle, float timeDelta )
  84. {
  85. pParticle->m_flRoll += pParticle->m_flRollDelta * timeDelta;
  86. pParticle->m_flRollDelta += pParticle->m_flRollDelta * ( timeDelta * -8.0f );
  87. //Cap the minimum roll
  88. if ( fabs( pParticle->m_flRollDelta ) < 0.5f )
  89. {
  90. pParticle->m_flRollDelta = ( pParticle->m_flRollDelta > 0.0f ) ? 0.5f : -0.5f;
  91. }
  92. return pParticle->m_flRoll;
  93. }
  94. private:
  95. CSmokeParticle( const CSmokeParticle & );
  96. };
  97. // Datatable.. this can have all the smoketrail parameters when we need it to.
  98. IMPLEMENT_CLIENTCLASS_DT(C_SmokeTrail, DT_SmokeTrail, SmokeTrail)
  99. RecvPropFloat(RECVINFO(m_SpawnRate)),
  100. RecvPropVector(RECVINFO(m_StartColor)),
  101. RecvPropVector(RECVINFO(m_EndColor)),
  102. RecvPropFloat(RECVINFO(m_ParticleLifetime)),
  103. RecvPropFloat(RECVINFO(m_StopEmitTime)),
  104. RecvPropFloat(RECVINFO(m_MinSpeed)),
  105. RecvPropFloat(RECVINFO(m_MaxSpeed)),
  106. RecvPropFloat(RECVINFO(m_MinDirectedSpeed)),
  107. RecvPropFloat(RECVINFO(m_MaxDirectedSpeed)),
  108. RecvPropFloat(RECVINFO(m_StartSize)),
  109. RecvPropFloat(RECVINFO(m_EndSize)),
  110. RecvPropFloat(RECVINFO(m_SpawnRadius)),
  111. RecvPropInt(RECVINFO(m_bEmit)),
  112. RecvPropInt(RECVINFO(m_nAttachment)),
  113. RecvPropFloat(RECVINFO(m_Opacity)),
  114. END_RECV_TABLE()
  115. // ------------------------------------------------------------------------- //
  116. // ParticleMovieExplosion
  117. // ------------------------------------------------------------------------- //
  118. C_SmokeTrail::C_SmokeTrail()
  119. {
  120. m_MaterialHandle[0] = NULL;
  121. m_MaterialHandle[1] = NULL;
  122. m_SpawnRate = 10;
  123. m_ParticleSpawn.Init(10);
  124. m_StartColor.Init(0.5, 0.5, 0.5);
  125. m_EndColor.Init(0,0,0);
  126. m_ParticleLifetime = 5;
  127. m_StopEmitTime = 0; // No end time
  128. m_MinSpeed = 2;
  129. m_MaxSpeed = 4;
  130. m_MinDirectedSpeed = m_MaxDirectedSpeed = 0;
  131. m_StartSize = 35;
  132. m_EndSize = 55;
  133. m_SpawnRadius = 2;
  134. m_VelocityOffset.Init();
  135. m_Opacity = 0.5f;
  136. m_bEmit = true;
  137. m_nAttachment = -1;
  138. m_pSmokeEmitter = NULL;
  139. m_pParticleMgr = NULL;
  140. }
  141. C_SmokeTrail::~C_SmokeTrail()
  142. {
  143. if ( ToolsEnabled() && clienttools->IsInRecordingMode() && m_pSmokeEmitter.IsValid() && m_pSmokeEmitter->GetToolParticleEffectId() != TOOLPARTICLESYSTEMID_INVALID )
  144. {
  145. KeyValues *msg = new KeyValues( "OldParticleSystem_ActivateEmitter" );
  146. msg->SetInt( "id", m_pSmokeEmitter->GetToolParticleEffectId() );
  147. msg->SetInt( "emitter", 0 );
  148. msg->SetInt( "active", false );
  149. msg->SetFloat( "time", gpGlobals->curtime );
  150. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  151. msg->deleteThis();
  152. }
  153. if ( m_pParticleMgr )
  154. {
  155. m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose:
  160. //-----------------------------------------------------------------------------
  161. void C_SmokeTrail::GetAimEntOrigin( IClientEntity *pAttachedTo, Vector *pAbsOrigin, QAngle *pAbsAngles )
  162. {
  163. C_BaseEntity *pEnt = pAttachedTo->GetBaseEntity();
  164. if (pEnt && (m_nAttachment > 0))
  165. {
  166. pEnt->GetAttachment( m_nAttachment, *pAbsOrigin, *pAbsAngles );
  167. }
  168. else
  169. {
  170. BaseClass::GetAimEntOrigin( pAttachedTo, pAbsOrigin, pAbsAngles );
  171. }
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. // Input : bEmit -
  176. //-----------------------------------------------------------------------------
  177. void C_SmokeTrail::SetEmit(bool bEmit)
  178. {
  179. m_bEmit = bEmit;
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Purpose:
  183. // Input : rate -
  184. //-----------------------------------------------------------------------------
  185. void C_SmokeTrail::SetSpawnRate(float rate)
  186. {
  187. m_SpawnRate = rate;
  188. m_ParticleSpawn.Init(rate);
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. // Input : bnewentity -
  193. //-----------------------------------------------------------------------------
  194. void C_SmokeTrail::OnDataChanged(DataUpdateType_t updateType)
  195. {
  196. C_BaseEntity::OnDataChanged(updateType);
  197. if ( updateType == DATA_UPDATE_CREATED )
  198. {
  199. Start( ParticleMgr(), NULL );
  200. }
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. // Input : *pParticleMgr -
  205. // *pArgs -
  206. //-----------------------------------------------------------------------------
  207. void C_SmokeTrail::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  208. {
  209. if(!pParticleMgr->AddEffect( &m_ParticleEffect, this ))
  210. return;
  211. m_pParticleMgr = pParticleMgr;
  212. m_pSmokeEmitter = CSmokeParticle::Create("smokeTrail");
  213. if ( !m_pSmokeEmitter )
  214. {
  215. Assert( false );
  216. return;
  217. }
  218. m_pSmokeEmitter->SetSortOrigin( GetAbsOrigin() );
  219. m_pSmokeEmitter->SetNearClip( 64.0f, 128.0f );
  220. m_MaterialHandle[0] = g_Mat_DustPuff[0];
  221. m_MaterialHandle[1] = g_Mat_DustPuff[1];
  222. m_ParticleSpawn.Init( m_SpawnRate );
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Purpose:
  226. // Input : fTimeDelta -
  227. //-----------------------------------------------------------------------------
  228. void C_SmokeTrail::Update( float fTimeDelta )
  229. {
  230. if ( !m_pSmokeEmitter )
  231. return;
  232. Vector offsetColor;
  233. // Add new particles
  234. if ( !m_bEmit )
  235. return;
  236. if ( ( m_StopEmitTime != 0 ) && ( m_StopEmitTime <= gpGlobals->curtime ) )
  237. return;
  238. float tempDelta = fTimeDelta;
  239. SimpleParticle *pParticle;
  240. Vector offset;
  241. Vector vecOrigin;
  242. VectorMA( GetAbsOrigin(), -fTimeDelta, GetAbsVelocity(), vecOrigin );
  243. Vector vecForward;
  244. GetVectors( &vecForward, NULL, NULL );
  245. while( m_ParticleSpawn.NextEvent( tempDelta ) )
  246. {
  247. float fldt = fTimeDelta - tempDelta;
  248. offset.Random( -m_SpawnRadius, m_SpawnRadius );
  249. offset += vecOrigin;
  250. VectorMA( offset, fldt, GetAbsVelocity(), offset );
  251. pParticle = (SimpleParticle *) m_pSmokeEmitter->AddParticle( sizeof( SimpleParticle ), m_MaterialHandle[random->RandomInt(0,1)], offset );
  252. if ( pParticle == NULL )
  253. continue;
  254. pParticle->m_flLifetime = 0.0f;
  255. pParticle->m_flDieTime = m_ParticleLifetime;
  256. pParticle->m_vecVelocity.Random( -1.0f, 1.0f );
  257. pParticle->m_vecVelocity *= random->RandomFloat( m_MinSpeed, m_MaxSpeed );
  258. pParticle->m_vecVelocity = pParticle->m_vecVelocity + GetAbsVelocity();
  259. float flDirectedVel = random->RandomFloat( m_MinDirectedSpeed, m_MaxDirectedSpeed );
  260. VectorMA( pParticle->m_vecVelocity, flDirectedVel, vecForward, pParticle->m_vecVelocity );
  261. offsetColor = m_StartColor;
  262. float flMaxVal = MAX( m_StartColor[0], m_StartColor[1] );
  263. if ( flMaxVal < m_StartColor[2] )
  264. {
  265. flMaxVal = m_StartColor[2];
  266. }
  267. offsetColor /= flMaxVal;
  268. offsetColor *= random->RandomFloat( -0.2f, 0.2f );
  269. offsetColor += m_StartColor;
  270. offsetColor[0] = clamp( offsetColor[0], 0.0f, 1.0f );
  271. offsetColor[1] = clamp( offsetColor[1], 0.0f, 1.0f );
  272. offsetColor[2] = clamp( offsetColor[2], 0.0f, 1.0f );
  273. pParticle->m_uchColor[0] = offsetColor[0]*255.0f;
  274. pParticle->m_uchColor[1] = offsetColor[1]*255.0f;
  275. pParticle->m_uchColor[2] = offsetColor[2]*255.0f;
  276. pParticle->m_uchStartSize = m_StartSize;
  277. pParticle->m_uchEndSize = m_EndSize;
  278. float alpha = random->RandomFloat( m_Opacity*0.75f, m_Opacity*1.25f );
  279. alpha = clamp( alpha, 0.0f, 1.0f );
  280. pParticle->m_uchStartAlpha = alpha * 255;
  281. pParticle->m_uchEndAlpha = 0;
  282. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  283. pParticle->m_flRollDelta = random->RandomFloat( -1.0f, 1.0f );
  284. }
  285. }
  286. void C_SmokeTrail::RenderParticles( CParticleRenderIterator *pIterator )
  287. {
  288. }
  289. void C_SmokeTrail::SimulateParticles( CParticleSimulateIterator *pIterator )
  290. {
  291. }
  292. //-----------------------------------------------------------------------------
  293. // This is called after sending this entity's recording state
  294. //-----------------------------------------------------------------------------
  295. void C_SmokeTrail::CleanupToolRecordingState( KeyValues *msg )
  296. {
  297. if ( !ToolsEnabled() )
  298. return;
  299. BaseClass::CleanupToolRecordingState( msg );
  300. // Generally, this is used to allow the entity to clean up
  301. // allocated state it put into the message, but here we're going
  302. // to use it to send particle system messages because we
  303. // know the grenade has been recorded at this point
  304. if ( !clienttools->IsInRecordingMode() || !m_pSmokeEmitter.IsValid() )
  305. return;
  306. // For now, we can't record smoketrails that don't have a moveparent
  307. C_BaseEntity *pEnt = GetMoveParent();
  308. if ( !pEnt )
  309. return;
  310. bool bEmitterActive = m_bEmit && ( ( m_StopEmitTime == 0 ) || ( m_StopEmitTime > gpGlobals->curtime ) );
  311. // NOTE: Particle system destruction message will be sent by the particle effect itself.
  312. if ( m_pSmokeEmitter->GetToolParticleEffectId() == TOOLPARTICLESYSTEMID_INVALID )
  313. {
  314. int nId = m_pSmokeEmitter->AllocateToolParticleEffectId();
  315. KeyValues *msg = new KeyValues( "OldParticleSystem_Create" );
  316. msg->SetString( "name", "C_SmokeTrail" );
  317. msg->SetInt( "id", nId );
  318. msg->SetFloat( "time", gpGlobals->curtime );
  319. KeyValues *pRandomEmitter = msg->FindKey( "DmeRandomEmitter", true );
  320. pRandomEmitter->SetInt( "count", m_SpawnRate ); // particles per second, when duration is < 0
  321. pRandomEmitter->SetFloat( "duration", -1 );
  322. pRandomEmitter->SetInt( "active", bEmitterActive );
  323. KeyValues *pEmitterParent1 = pRandomEmitter->FindKey( "emitter1", true );
  324. pEmitterParent1->SetFloat( "randomamount", 0.5f );
  325. KeyValues *pEmitterParent2 = pRandomEmitter->FindKey( "emitter2", true );
  326. pEmitterParent2->SetFloat( "randomamount", 0.5f );
  327. KeyValues *pEmitter = pEmitterParent1->FindKey( "DmeSpriteEmitter", true );
  328. pEmitter->SetString( "material", "particle/particle_smokegrenade" );
  329. KeyValues *pInitializers = pEmitter->FindKey( "initializers", true );
  330. // FIXME: Until we can interpolate ent logs during emission, this can't work
  331. KeyValues *pPosition = pInitializers->FindKey( "DmePositionPointToEntityInitializer", true );
  332. pPosition->SetPtr( "entindex", (void*)pEnt->entindex() );
  333. pPosition->SetInt( "attachmentIndex", m_nAttachment );
  334. pPosition->SetFloat( "randomDist", m_SpawnRadius );
  335. pPosition->SetFloat( "startx", pEnt->GetAbsOrigin().x );
  336. pPosition->SetFloat( "starty", pEnt->GetAbsOrigin().y );
  337. pPosition->SetFloat( "startz", pEnt->GetAbsOrigin().z );
  338. KeyValues *pLifetime = pInitializers->FindKey( "DmeRandomLifetimeInitializer", true );
  339. pLifetime->SetFloat( "minLifetime", m_ParticleLifetime );
  340. pLifetime->SetFloat( "maxLifetime", m_ParticleLifetime );
  341. KeyValues *pVelocity = pInitializers->FindKey( "DmeAttachmentVelocityInitializer", true );
  342. pVelocity->SetPtr( "entindex", (void*)entindex() );
  343. pVelocity->SetFloat( "minAttachmentSpeed", m_MinDirectedSpeed );
  344. pVelocity->SetFloat( "maxAttachmentSpeed", m_MaxDirectedSpeed );
  345. pVelocity->SetFloat( "minRandomSpeed", m_MinSpeed );
  346. pVelocity->SetFloat( "maxRandomSpeed", m_MaxSpeed );
  347. KeyValues *pRoll = pInitializers->FindKey( "DmeRandomRollInitializer", true );
  348. pRoll->SetFloat( "minRoll", 0.0f );
  349. pRoll->SetFloat( "maxRoll", 360.0f );
  350. KeyValues *pRollSpeed = pInitializers->FindKey( "DmeRandomRollSpeedInitializer", true );
  351. pRollSpeed->SetFloat( "minRollSpeed", -1.0f );
  352. pRollSpeed->SetFloat( "maxRollSpeed", 1.0f );
  353. KeyValues *pColor = pInitializers->FindKey( "DmeRandomValueColorInitializer", true );
  354. Color c(
  355. clamp( m_StartColor.x * 255.0f, 0, 255 ),
  356. clamp( m_StartColor.y * 255.0f, 0, 255 ),
  357. clamp( m_StartColor.z * 255.0f, 0, 255 ), 255 );
  358. pColor->SetColor( "startColor", c );
  359. pColor->SetFloat( "minStartValueDelta", -0.2f );
  360. pColor->SetFloat( "maxStartValueDelta", 0.2f );
  361. pColor->SetColor( "endColor", Color( 0, 0, 0, 255 ) );
  362. KeyValues *pAlpha = pInitializers->FindKey( "DmeRandomAlphaInitializer", true );
  363. int nMinAlpha = 255 * m_Opacity * 0.75f;
  364. int nMaxAlpha = 255 * m_Opacity * 1.25f;
  365. pAlpha->SetInt( "minStartAlpha", 0 );
  366. pAlpha->SetInt( "maxStartAlpha", 0 );
  367. pAlpha->SetInt( "minEndAlpha", clamp( nMinAlpha, 0, 255 ) );
  368. pAlpha->SetInt( "maxEndAlpha", clamp( nMaxAlpha, 0, 255 ) );
  369. KeyValues *pSize = pInitializers->FindKey( "DmeRandomSizeInitializer", true );
  370. pSize->SetFloat( "minStartSize", m_StartSize );
  371. pSize->SetFloat( "maxStartSize", m_StartSize );
  372. pSize->SetFloat( "minEndSize", m_EndSize );
  373. pSize->SetFloat( "maxEndSize", m_EndSize );
  374. KeyValues *pUpdaters = pEmitter->FindKey( "updaters", true );
  375. pUpdaters->FindKey( "DmePositionVelocityUpdater", true );
  376. pUpdaters->FindKey( "DmeRollUpdater", true );
  377. KeyValues *pRollSpeedUpdater = pUpdaters->FindKey( "DmeRollSpeedAttenuateUpdater", true );
  378. pRollSpeedUpdater->SetFloat( "attenuation", 1.0f - 8.0f / 30.0f );
  379. pRollSpeedUpdater->SetFloat( "attenuationTme", 1.0f / 30.0f );
  380. pRollSpeedUpdater->SetFloat( "minRollSpeed", 0.5f );
  381. pUpdaters->FindKey( "DmeAlphaSineUpdater", true );
  382. pUpdaters->FindKey( "DmeColorUpdater", true );
  383. pUpdaters->FindKey( "DmeSizeUpdater", true );
  384. KeyValues *pEmitter2 = pEmitter->MakeCopy();
  385. pEmitter2->SetString( "material", "particle/particle_noisesphere" );
  386. pEmitterParent2->AddSubKey( pEmitter2 );
  387. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  388. msg->deleteThis();
  389. }
  390. else
  391. {
  392. KeyValues *msg = new KeyValues( "OldParticleSystem_ActivateEmitter" );
  393. msg->SetInt( "id", m_pSmokeEmitter->GetToolParticleEffectId() );
  394. msg->SetInt( "emitter", 0 );
  395. msg->SetInt( "active", bEmitterActive );
  396. msg->SetFloat( "time", gpGlobals->curtime );
  397. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  398. msg->deleteThis();
  399. }
  400. }
  401. //==================================================
  402. // RocketTrail
  403. //==================================================
  404. // Expose to the particle app.
  405. EXPOSE_PROTOTYPE_EFFECT(RocketTrail, C_RocketTrail);
  406. // Datatable.. this can have all the smoketrail parameters when we need it to.
  407. IMPLEMENT_CLIENTCLASS_DT(C_RocketTrail, DT_RocketTrail, RocketTrail)
  408. RecvPropFloat(RECVINFO(m_SpawnRate)),
  409. RecvPropVector(RECVINFO(m_StartColor)),
  410. RecvPropVector(RECVINFO(m_EndColor)),
  411. RecvPropFloat(RECVINFO(m_ParticleLifetime)),
  412. RecvPropFloat(RECVINFO(m_StopEmitTime)),
  413. RecvPropFloat(RECVINFO(m_MinSpeed)),
  414. RecvPropFloat(RECVINFO(m_MaxSpeed)),
  415. RecvPropFloat(RECVINFO(m_StartSize)),
  416. RecvPropFloat(RECVINFO(m_EndSize)),
  417. RecvPropFloat(RECVINFO(m_SpawnRadius)),
  418. RecvPropInt(RECVINFO(m_bEmit)),
  419. RecvPropInt(RECVINFO(m_nAttachment)),
  420. RecvPropFloat(RECVINFO(m_Opacity)),
  421. RecvPropInt(RECVINFO(m_bDamaged)),
  422. RecvPropFloat(RECVINFO(m_flFlareScale)),
  423. END_RECV_TABLE()
  424. // ------------------------------------------------------------------------- //
  425. // ParticleMovieExplosion
  426. // ------------------------------------------------------------------------- //
  427. C_RocketTrail::C_RocketTrail()
  428. {
  429. m_MaterialHandle[0] = NULL;
  430. m_MaterialHandle[1] = NULL;
  431. m_SpawnRate = 10;
  432. m_ParticleSpawn.Init(10);
  433. m_StartColor.Init(0.5, 0.5, 0.5);
  434. m_EndColor.Init(0,0,0);
  435. m_ParticleLifetime = 5;
  436. m_StopEmitTime = 0; // No end time
  437. m_MinSpeed = 2;
  438. m_MaxSpeed = 4;
  439. m_StartSize = 35;
  440. m_EndSize = 55;
  441. m_SpawnRadius = 2;
  442. m_VelocityOffset.Init();
  443. m_Opacity = 0.5f;
  444. m_bEmit = true;
  445. m_bDamaged = false;
  446. m_nAttachment = -1;
  447. m_pRocketEmitter = NULL;
  448. m_pParticleMgr = NULL;
  449. }
  450. C_RocketTrail::~C_RocketTrail()
  451. {
  452. if ( m_pParticleMgr )
  453. {
  454. m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
  455. }
  456. }
  457. //-----------------------------------------------------------------------------
  458. // Purpose:
  459. //-----------------------------------------------------------------------------
  460. void C_RocketTrail::GetAimEntOrigin( IClientEntity *pAttachedTo, Vector *pAbsOrigin, QAngle *pAbsAngles )
  461. {
  462. C_BaseEntity *pEnt = pAttachedTo->GetBaseEntity();
  463. if (pEnt && (m_nAttachment > 0))
  464. {
  465. pEnt->GetAttachment( m_nAttachment, *pAbsOrigin, *pAbsAngles );
  466. }
  467. else
  468. {
  469. BaseClass::GetAimEntOrigin( pAttachedTo, pAbsOrigin, pAbsAngles );
  470. }
  471. }
  472. //-----------------------------------------------------------------------------
  473. // Purpose:
  474. // Input : bEmit -
  475. //-----------------------------------------------------------------------------
  476. void C_RocketTrail::SetEmit(bool bEmit)
  477. {
  478. m_bEmit = bEmit;
  479. }
  480. //-----------------------------------------------------------------------------
  481. // Purpose:
  482. // Input : rate -
  483. //-----------------------------------------------------------------------------
  484. void C_RocketTrail::SetSpawnRate(float rate)
  485. {
  486. m_SpawnRate = rate;
  487. m_ParticleSpawn.Init(rate);
  488. }
  489. //-----------------------------------------------------------------------------
  490. // Purpose:
  491. // Input : bnewentity -
  492. //-----------------------------------------------------------------------------
  493. void C_RocketTrail::OnDataChanged(DataUpdateType_t updateType)
  494. {
  495. C_BaseEntity::OnDataChanged(updateType);
  496. if ( updateType == DATA_UPDATE_CREATED )
  497. {
  498. Start( ParticleMgr(), NULL );
  499. }
  500. }
  501. //-----------------------------------------------------------------------------
  502. // Purpose:
  503. // Input : *pParticleMgr -
  504. // *pArgs -
  505. //-----------------------------------------------------------------------------
  506. void C_RocketTrail::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  507. {
  508. if(!pParticleMgr->AddEffect( &m_ParticleEffect, this ))
  509. return;
  510. m_pParticleMgr = pParticleMgr;
  511. m_pRocketEmitter = CRocketTrailParticle::Create("smokeTrail");
  512. if ( !m_pRocketEmitter )
  513. {
  514. Assert( false );
  515. return;
  516. }
  517. m_pRocketEmitter->SetSortOrigin( GetAbsOrigin() );
  518. m_pRocketEmitter->SetNearClip( 64.0f, 128.0f );
  519. m_MaterialHandle[0] = g_Mat_DustPuff[0];
  520. m_MaterialHandle[1] = g_Mat_DustPuff[1];
  521. m_ParticleSpawn.Init( m_SpawnRate );
  522. m_vecLastPosition = GetAbsOrigin();
  523. }
  524. //-----------------------------------------------------------------------------
  525. // Purpose:
  526. // Input : fTimeDelta -
  527. //-----------------------------------------------------------------------------
  528. void C_RocketTrail::Update( float fTimeDelta )
  529. {
  530. if ( !m_pRocketEmitter )
  531. return;
  532. if ( gpGlobals->frametime == 0.0f )
  533. return;
  534. CSmartPtr<CSimpleEmitter> pSimple = CSimpleEmitter::Create( "MuzzleFlash" );
  535. pSimple->SetSortOrigin( GetAbsOrigin() );
  536. SimpleParticle *pParticle;
  537. Vector forward, offset;
  538. AngleVectors( GetAbsAngles(), &forward );
  539. forward.Negate();
  540. float flScale = random->RandomFloat( m_flFlareScale-0.5f, m_flFlareScale+0.5f );
  541. //
  542. // Flash
  543. //
  544. int i;
  545. for ( i = 1; i < 9; i++ )
  546. {
  547. offset = GetAbsOrigin() + (forward * (i*2.0f*m_flFlareScale));
  548. pParticle = (SimpleParticle *) pSimple->AddParticle( sizeof( SimpleParticle ), pSimple->GetPMaterial( VarArgs( "effects/muzzleflash%d", random->RandomInt(1,4) ) ), offset );
  549. if ( pParticle == NULL )
  550. return;
  551. pParticle->m_flLifetime = 0.0f;
  552. pParticle->m_flDieTime = 0.01f;
  553. pParticle->m_vecVelocity.Init();
  554. pParticle->m_uchColor[0] = 255;
  555. pParticle->m_uchColor[1] = 255;
  556. pParticle->m_uchColor[2] = 255;
  557. pParticle->m_uchStartAlpha = 255;
  558. pParticle->m_uchEndAlpha = 128;
  559. pParticle->m_uchStartSize = (random->RandomFloat( 5.0f, 6.0f ) * (12-(i))/9) * flScale;
  560. pParticle->m_uchEndSize = pParticle->m_uchStartSize;
  561. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  562. pParticle->m_flRollDelta = 0.0f;
  563. }
  564. // Add new particles (undamaged version)
  565. if ( m_bEmit )
  566. {
  567. Vector moveDiff = GetAbsOrigin() - m_vecLastPosition;
  568. float moveLength = VectorNormalize( moveDiff );
  569. int numPuffs = moveLength / ( m_StartSize / 2.0f );
  570. //debugoverlay->AddLineOverlay( m_vecLastPosition, GetAbsOrigin(), 255, 0, 0, true, 2.0f );
  571. //FIXME: More rational cap here, perhaps
  572. if ( numPuffs > 50 )
  573. numPuffs = 50;
  574. Vector offsetColor;
  575. float step = moveLength / numPuffs;
  576. //Fill in the gaps
  577. for ( i = 1; i < numPuffs+1; i++ )
  578. {
  579. offset = m_vecLastPosition + ( moveDiff * step * i );
  580. //debugoverlay->AddBoxOverlay( offset, -Vector(2,2,2), Vector(2,2,2), vec3_angle, i*4, i*4, i*4, true, 4.0f );
  581. pParticle = (SimpleParticle *) m_pRocketEmitter->AddParticle( sizeof( SimpleParticle ), m_MaterialHandle[random->RandomInt(0,1)], offset );
  582. if ( pParticle != NULL )
  583. {
  584. pParticle->m_flLifetime = 0.0f;
  585. pParticle->m_flDieTime = m_ParticleLifetime + random->RandomFloat(m_ParticleLifetime*0.9f,m_ParticleLifetime*1.1f);
  586. pParticle->m_vecVelocity.Random( -1.0f, 1.0f );
  587. pParticle->m_vecVelocity *= random->RandomFloat( m_MinSpeed, m_MaxSpeed );
  588. offsetColor = m_StartColor * random->RandomFloat( 0.75f, 1.25f );
  589. offsetColor[0] = clamp( offsetColor[0], 0.0f, 1.0f );
  590. offsetColor[1] = clamp( offsetColor[1], 0.0f, 1.0f );
  591. offsetColor[2] = clamp( offsetColor[2], 0.0f, 1.0f );
  592. pParticle->m_uchColor[0] = offsetColor[0]*255.0f;
  593. pParticle->m_uchColor[1] = offsetColor[1]*255.0f;
  594. pParticle->m_uchColor[2] = offsetColor[2]*255.0f;
  595. pParticle->m_uchStartSize = m_StartSize * random->RandomFloat( 0.75f, 1.25f );
  596. pParticle->m_uchEndSize = m_EndSize * random->RandomFloat( 1.0f, 1.25f );
  597. float alpha = random->RandomFloat( m_Opacity*0.75f, m_Opacity*1.25f );
  598. if ( alpha > 1.0f )
  599. alpha = 1.0f;
  600. if ( alpha < 0.0f )
  601. alpha = 0.0f;
  602. pParticle->m_uchStartAlpha = alpha * 255;
  603. pParticle->m_uchEndAlpha = 0;
  604. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  605. pParticle->m_flRollDelta = random->RandomFloat( -8.0f, 8.0f );
  606. }
  607. }
  608. }
  609. if ( m_bDamaged )
  610. {
  611. SimpleParticle *pParticle;
  612. Vector offset;
  613. Vector offsetColor;
  614. CSmartPtr<CEmberEffect> pEmitter = CEmberEffect::Create("C_RocketTrail::damaged");
  615. pEmitter->SetSortOrigin( GetAbsOrigin() );
  616. PMaterialHandle flameMaterial = m_pRocketEmitter->GetPMaterial( VarArgs( "sprites/flamelet%d", random->RandomInt( 1, 4 ) ) );
  617. // Flames from the rocket
  618. for ( i = 0; i < 8; i++ )
  619. {
  620. offset = RandomVector( -8, 8 ) + GetAbsOrigin();
  621. pParticle = (SimpleParticle *) pEmitter->AddParticle( sizeof( SimpleParticle ), flameMaterial, offset );
  622. if ( pParticle != NULL )
  623. {
  624. pParticle->m_flLifetime = 0.0f;
  625. pParticle->m_flDieTime = 0.25f;
  626. pParticle->m_vecVelocity.Random( -1.0f, 1.0f );
  627. pParticle->m_vecVelocity *= random->RandomFloat( 32, 128 );
  628. offsetColor = m_StartColor * random->RandomFloat( 0.75f, 1.25f );
  629. offsetColor[0] = clamp( offsetColor[0], 0.0f, 1.0f );
  630. offsetColor[1] = clamp( offsetColor[1], 0.0f, 1.0f );
  631. offsetColor[2] = clamp( offsetColor[2], 0.0f, 1.0f );
  632. pParticle->m_uchColor[0] = offsetColor[0]*255.0f;
  633. pParticle->m_uchColor[1] = offsetColor[1]*255.0f;
  634. pParticle->m_uchColor[2] = offsetColor[2]*255.0f;
  635. pParticle->m_uchStartSize = 8.0f;
  636. pParticle->m_uchEndSize = 32.0f;
  637. pParticle->m_uchStartAlpha = 255;
  638. pParticle->m_uchEndAlpha = 0;
  639. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  640. pParticle->m_flRollDelta = random->RandomFloat( -8.0f, 8.0f );
  641. }
  642. }
  643. }
  644. m_vecLastPosition = GetAbsOrigin();
  645. }
  646. void C_RocketTrail::RenderParticles( CParticleRenderIterator *pIterator )
  647. {
  648. }
  649. void C_RocketTrail::SimulateParticles( CParticleSimulateIterator *pIterator )
  650. {
  651. }
  652. SporeEffect::SporeEffect( const char *pDebugName ) : CSimpleEmitter( pDebugName )
  653. {
  654. }
  655. SporeEffect* SporeEffect::Create( const char *pDebugName )
  656. {
  657. return new SporeEffect( pDebugName );
  658. }
  659. //-----------------------------------------------------------------------------
  660. // Purpose:
  661. // Input : fTimeDelta -
  662. // Output : Vector
  663. //-----------------------------------------------------------------------------
  664. void SporeEffect::UpdateVelocity( SimpleParticle *pParticle, float timeDelta )
  665. {
  666. float speed = VectorNormalize( pParticle->m_vecVelocity );
  667. Vector offset;
  668. speed -= ( 64.0f * timeDelta );
  669. offset.Random( -0.5f, 0.5f );
  670. pParticle->m_vecVelocity += offset;
  671. VectorNormalize( pParticle->m_vecVelocity );
  672. pParticle->m_vecVelocity *= speed;
  673. }
  674. //-----------------------------------------------------------------------------
  675. // Purpose:
  676. // Input : *pParticle -
  677. // timeDelta -
  678. //-----------------------------------------------------------------------------
  679. Vector SporeEffect::UpdateColor( const SimpleParticle *pParticle )
  680. {
  681. Vector color;
  682. float ramp = ((float)pParticle->m_uchStartAlpha/255.0f) * sin( M_PI * (pParticle->m_flLifetime / pParticle->m_flDieTime) );//1.0f - ( pParticle->m_flLifetime / pParticle->m_flDieTime );
  683. color[0] = ( (float) pParticle->m_uchColor[0] * ramp ) / 255.0f;
  684. color[1] = ( (float) pParticle->m_uchColor[1] * ramp ) / 255.0f;
  685. color[2] = ( (float) pParticle->m_uchColor[2] * ramp ) / 255.0f;
  686. return color;
  687. }
  688. //-----------------------------------------------------------------------------
  689. // Purpose:
  690. // Input : *pParticle -
  691. // timeDelta -
  692. // Output : float
  693. //-----------------------------------------------------------------------------
  694. float SporeEffect::UpdateAlpha( const SimpleParticle *pParticle )
  695. {
  696. return ( ((float)pParticle->m_uchStartAlpha/255.0f) * sin( M_PI * (pParticle->m_flLifetime / pParticle->m_flDieTime) ) );
  697. }
  698. //==================================================
  699. // C_SporeExplosion
  700. //==================================================
  701. EXPOSE_PROTOTYPE_EFFECT( SporeExplosion, C_SporeExplosion );
  702. IMPLEMENT_CLIENTCLASS_DT( C_SporeExplosion, DT_SporeExplosion, SporeExplosion )
  703. RecvPropFloat(RECVINFO(m_flSpawnRate)),
  704. RecvPropFloat(RECVINFO(m_flParticleLifetime)),
  705. RecvPropFloat(RECVINFO(m_flStartSize)),
  706. RecvPropFloat(RECVINFO(m_flEndSize)),
  707. RecvPropFloat(RECVINFO(m_flSpawnRadius)),
  708. RecvPropBool(RECVINFO(m_bEmit)),
  709. RecvPropBool(RECVINFO(m_bDontRemove)),
  710. END_RECV_TABLE()
  711. C_SporeExplosion::C_SporeExplosion( void )
  712. {
  713. m_pParticleMgr = NULL;
  714. m_flSpawnRate = 32;
  715. m_flParticleLifetime = 5;
  716. m_flStartSize = 32;
  717. m_flEndSize = 64;
  718. m_flSpawnRadius = 32;
  719. m_pSporeEffect = NULL;
  720. m_teParticleSpawn.Init( 32 );
  721. m_bEmit = true;
  722. m_bDontRemove = false;
  723. }
  724. C_SporeExplosion::~C_SporeExplosion()
  725. {
  726. if ( m_pParticleMgr != NULL )
  727. {
  728. m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
  729. }
  730. }
  731. //-----------------------------------------------------------------------------
  732. // Purpose:
  733. // Input : bnewentity -
  734. //-----------------------------------------------------------------------------
  735. void C_SporeExplosion::OnDataChanged( DataUpdateType_t updateType )
  736. {
  737. C_BaseEntity::OnDataChanged( updateType );
  738. if ( updateType == DATA_UPDATE_CREATED )
  739. {
  740. m_flPreviousSpawnRate = m_flSpawnRate;
  741. m_teParticleSpawn.Init( m_flSpawnRate );
  742. Start( ParticleMgr(), NULL );
  743. }
  744. else if( m_bEmit )
  745. {
  746. // Just been turned on by the server.
  747. m_flPreviousSpawnRate = m_flSpawnRate;
  748. m_teParticleSpawn.Init( m_flSpawnRate );
  749. }
  750. m_pSporeEffect->SetDontRemove( m_bDontRemove );
  751. }
  752. //-----------------------------------------------------------------------------
  753. // Purpose:
  754. // Input : *pParticleMgr -
  755. // *pArgs -
  756. //-----------------------------------------------------------------------------
  757. void C_SporeExplosion::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  758. {
  759. //Add us into the effect manager
  760. if( pParticleMgr->AddEffect( &m_ParticleEffect, this ) == false )
  761. return;
  762. //Create our main effect
  763. m_pSporeEffect = SporeEffect::Create( "C_SporeExplosion" );
  764. if ( m_pSporeEffect == NULL )
  765. return;
  766. m_hMaterial = m_pSporeEffect->GetPMaterial( "particle/fire" );
  767. m_pSporeEffect->SetSortOrigin( GetAbsOrigin() );
  768. m_pSporeEffect->SetNearClip( 64, 128 );
  769. }
  770. //-----------------------------------------------------------------------------
  771. // Purpose:
  772. //-----------------------------------------------------------------------------
  773. void C_SporeExplosion::AddParticles( void )
  774. {
  775. //Spores
  776. Vector offset;
  777. Vector dir;
  778. //Get our direction
  779. AngleVectors( GetAbsAngles(), &dir );
  780. SimpleParticle *sParticle;
  781. for ( int i = 0; i < 4; i++ )
  782. {
  783. //Add small particle to the effect's origin
  784. offset.Random( -m_flSpawnRadius, m_flSpawnRadius );
  785. sParticle = (SimpleParticle *) m_pSporeEffect->AddParticle( sizeof(SimpleParticle), m_hMaterial, GetAbsOrigin()+offset );
  786. if ( sParticle == NULL )
  787. return;
  788. sParticle->m_flLifetime = 0.0f;
  789. sParticle->m_flDieTime = 2.0f;
  790. sParticle->m_flRoll = 0;
  791. sParticle->m_flRollDelta = 0;
  792. sParticle->m_uchColor[0] = 225;
  793. sParticle->m_uchColor[1] = 140;
  794. sParticle->m_uchColor[2] = 64;
  795. sParticle->m_uchStartAlpha = Helper_RandomInt( 128, 255 );
  796. sParticle->m_uchEndAlpha = 0;
  797. sParticle->m_uchStartSize = Helper_RandomInt( 1, 2 );
  798. sParticle->m_uchEndSize = 1;
  799. sParticle->m_vecVelocity = dir * Helper_RandomFloat( 128.0f, 256.0f );
  800. }
  801. //Add smokey bits
  802. offset.Random( -(m_flSpawnRadius * 0.5), (m_flSpawnRadius * 0.5) );
  803. sParticle = (SimpleParticle *) m_pSporeEffect->AddParticle( sizeof(SimpleParticle), g_Mat_DustPuff[1], GetAbsOrigin()+offset );
  804. if ( sParticle == NULL )
  805. return;
  806. sParticle->m_flLifetime = 0.0f;
  807. sParticle->m_flDieTime = 1.0f;
  808. sParticle->m_flRoll = Helper_RandomFloat( 0, 360 );
  809. sParticle->m_flRollDelta = Helper_RandomFloat( -2.0f, 2.0f );
  810. sParticle->m_uchColor[0] = 225;
  811. sParticle->m_uchColor[1] = 140;
  812. sParticle->m_uchColor[2] = 64;
  813. sParticle->m_uchStartAlpha = Helper_RandomInt( 32, 64 );
  814. sParticle->m_uchEndAlpha = 0;
  815. sParticle->m_uchStartSize = m_flStartSize;
  816. sParticle->m_uchEndSize = m_flEndSize;
  817. sParticle->m_vecVelocity = dir * Helper_RandomFloat( 64.0f, 128.0f );
  818. }
  819. ConVar cl_sporeclipdistance( "cl_sporeclipdistance", "512", FCVAR_CHEAT | FCVAR_CLIENTDLL );
  820. //-----------------------------------------------------------------------------
  821. // Purpose:
  822. // Input : fTimeDelta -
  823. //-----------------------------------------------------------------------------
  824. void C_SporeExplosion::Update( float fTimeDelta )
  825. {
  826. if( m_bEmit )
  827. {
  828. int nSlot = GET_ACTIVE_SPLITSCREEN_SLOT();
  829. float tempDelta = fTimeDelta;
  830. float flDist = (MainViewOrigin(nSlot) - GetAbsOrigin()).Length();
  831. //Lower the spawnrate by half if we're far away from it.
  832. if ( cl_sporeclipdistance.GetFloat() <= flDist )
  833. {
  834. if ( m_flSpawnRate == m_flPreviousSpawnRate )
  835. {
  836. m_flPreviousSpawnRate = m_flSpawnRate * 0.5f;
  837. m_teParticleSpawn.ResetRate( m_flPreviousSpawnRate );
  838. }
  839. }
  840. else
  841. {
  842. if ( m_flSpawnRate != m_flPreviousSpawnRate )
  843. {
  844. m_flPreviousSpawnRate = m_flSpawnRate;
  845. m_teParticleSpawn.ResetRate( m_flPreviousSpawnRate );
  846. }
  847. }
  848. while ( m_teParticleSpawn.NextEvent( tempDelta ) )
  849. {
  850. AddParticles();
  851. }
  852. }
  853. }
  854. void C_SporeExplosion::SimulateParticles( CParticleSimulateIterator *pIterator )
  855. {
  856. StandardParticle_t *pParticle = (StandardParticle_t*)pIterator->GetFirst();
  857. while ( pParticle )
  858. {
  859. pParticle->m_Lifetime += pIterator->GetTimeDelta();
  860. if( pParticle->m_Lifetime > m_flParticleLifetime )
  861. {
  862. pIterator->RemoveParticle( pParticle );
  863. }
  864. pParticle = (StandardParticle_t*)pIterator->GetNext();
  865. }
  866. }
  867. void C_SporeExplosion::RenderParticles( CParticleRenderIterator *pIterator )
  868. {
  869. }
  870. //-----------------------------------------------------------------------------
  871. // Purpose:
  872. //-----------------------------------------------------------------------------
  873. void RPGShotDownCallback( const CEffectData &data )
  874. {
  875. CLocalPlayerFilter filter;
  876. C_BaseEntity::EmitSound( filter, SOUND_FROM_WORLD, "Missile.ShotDown", &data.m_vOrigin );
  877. if ( CExplosionOverlay *pOverlay = new CExplosionOverlay )
  878. {
  879. pOverlay->m_flLifetime = 0;
  880. pOverlay->m_vPos = data.m_vOrigin;
  881. pOverlay->m_nSprites = 1;
  882. pOverlay->m_vBaseColors[0].Init( 1.0f, 0.9f, 0.7f );
  883. pOverlay->m_Sprites[0].m_flHorzSize = 0.01f;
  884. pOverlay->m_Sprites[0].m_flVertSize = pOverlay->m_Sprites[0].m_flHorzSize*0.5f;
  885. pOverlay->Activate();
  886. }
  887. }
  888. DECLARE_CLIENT_EFFECT_BEGIN( RPGShotDown, RPGShotDownCallback )
  889. PRECACHE( GAMESOUND, "Missile.ShotDown" )
  890. DECLARE_CLIENT_EFFECT_END()
  891. //==================================================
  892. // C_SporeTrail
  893. //==================================================
  894. class C_SporeTrail : public C_BaseParticleEntity
  895. {
  896. public:
  897. DECLARE_CLASS( C_SporeTrail, C_BaseParticleEntity );
  898. DECLARE_CLIENTCLASS();
  899. C_SporeTrail( void );
  900. virtual ~C_SporeTrail( void );
  901. public:
  902. void SetEmit( bool bEmit );
  903. // C_BaseEntity
  904. public:
  905. virtual void OnDataChanged( DataUpdateType_t updateType );
  906. virtual void GetAimEntOrigin( IClientEntity *pAttachedTo, Vector *pAbsOrigin, QAngle *pAbsAngles );
  907. // IPrototypeAppEffect
  908. public:
  909. virtual void Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs );
  910. // IParticleEffect
  911. public:
  912. virtual void Update( float fTimeDelta );
  913. virtual void RenderParticles( CParticleRenderIterator *pIterator );
  914. virtual void SimulateParticles( CParticleSimulateIterator *pIterator );
  915. virtual void StartRender( VMatrix &effectMatrix );
  916. public:
  917. Vector m_vecEndColor;
  918. float m_flSpawnRate;
  919. float m_flParticleLifetime;
  920. float m_flStartSize;
  921. float m_flEndSize;
  922. float m_flSpawnRadius;
  923. Vector m_vecVelocityOffset;
  924. bool m_bEmit;
  925. private:
  926. C_SporeTrail( const C_SporeTrail & );
  927. void AddParticles( void );
  928. PMaterialHandle m_hMaterial;
  929. TimedEvent m_teParticleSpawn;
  930. //CSmartPtr<SporeSmokeEffect> m_pSmokeEffect;
  931. Vector m_vecPos;
  932. Vector m_vecLastPos; // This is stored so we can spawn particles in between the previous and new position
  933. // to eliminate holes in the trail.
  934. VMatrix m_mAttachmentMatrix;
  935. CParticleMgr *m_pParticleMgr;
  936. };
  937. //==================================================
  938. // C_SporeTrail
  939. //==================================================
  940. IMPLEMENT_CLIENTCLASS_DT( C_SporeTrail, DT_SporeTrail, SporeTrail )
  941. RecvPropFloat(RECVINFO(m_flSpawnRate)),
  942. RecvPropVector(RECVINFO(m_vecEndColor)),
  943. RecvPropFloat(RECVINFO(m_flParticleLifetime)),
  944. RecvPropFloat(RECVINFO(m_flStartSize)),
  945. RecvPropFloat(RECVINFO(m_flEndSize)),
  946. RecvPropFloat(RECVINFO(m_flSpawnRadius)),
  947. RecvPropInt(RECVINFO(m_bEmit)),
  948. END_RECV_TABLE()
  949. C_SporeTrail::C_SporeTrail( void )
  950. {
  951. m_pParticleMgr = NULL;
  952. //m_pSmokeEffect = SporeSmokeEffect::Create( "C_SporeTrail" );
  953. m_flSpawnRate = 10;
  954. m_flParticleLifetime = 5;
  955. m_flStartSize = 35;
  956. m_flEndSize = 55;
  957. m_flSpawnRadius = 2;
  958. m_teParticleSpawn.Init( 5 );
  959. m_vecEndColor.Init();
  960. m_vecPos.Init();
  961. m_vecLastPos.Init();
  962. m_vecVelocityOffset.Init();
  963. m_bEmit = true;
  964. }
  965. C_SporeTrail::~C_SporeTrail()
  966. {
  967. if( m_pParticleMgr )
  968. {
  969. m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
  970. }
  971. }
  972. //-----------------------------------------------------------------------------
  973. // Purpose:
  974. // Input : bEmit -
  975. //-----------------------------------------------------------------------------
  976. void C_SporeTrail::SetEmit( bool bEmit )
  977. {
  978. m_bEmit = bEmit;
  979. }
  980. //-----------------------------------------------------------------------------
  981. // Purpose:
  982. // Input : bnewentity -
  983. //-----------------------------------------------------------------------------
  984. void C_SporeTrail::OnDataChanged( DataUpdateType_t updateType )
  985. {
  986. C_BaseEntity::OnDataChanged( updateType );
  987. if ( updateType == DATA_UPDATE_CREATED )
  988. {
  989. Start( ParticleMgr(), NULL );
  990. }
  991. }
  992. //-----------------------------------------------------------------------------
  993. // Purpose:
  994. // Input : *pParticleMgr -
  995. // *pArgs -
  996. //-----------------------------------------------------------------------------
  997. void C_SporeTrail::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  998. {
  999. if( pParticleMgr->AddEffect( &m_ParticleEffect, this ) == false )
  1000. return;
  1001. m_hMaterial = g_Mat_DustPuff[1];
  1002. m_pParticleMgr = pParticleMgr;
  1003. m_teParticleSpawn.Init( 64 );
  1004. }
  1005. //-----------------------------------------------------------------------------
  1006. // Purpose:
  1007. //-----------------------------------------------------------------------------
  1008. void C_SporeTrail::AddParticles( void )
  1009. {
  1010. Vector offset = RandomVector( -4.0f, 4.0f );
  1011. //Make a new particle
  1012. SimpleParticle *sParticle = (SimpleParticle *) m_ParticleEffect.AddParticle( sizeof(SimpleParticle), m_hMaterial );//m_pSmokeEffect->AddParticle( sizeof(SimpleParticle), m_hMaterial, GetAbsOrigin()+offset );
  1013. if ( sParticle == NULL )
  1014. return;
  1015. sParticle->m_Pos = offset;
  1016. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  1017. sParticle->m_flRollDelta = Helper_RandomFloat( -2.0f, 2.0f );
  1018. sParticle->m_flLifetime = 0.0f;
  1019. sParticle->m_flDieTime = 0.5f;
  1020. sParticle->m_uchColor[0] = 225;
  1021. sParticle->m_uchColor[1] = 140;
  1022. sParticle->m_uchColor[2] = 64;
  1023. sParticle->m_uchStartAlpha = Helper_RandomInt( 64, 128 );
  1024. sParticle->m_uchEndAlpha = 0;
  1025. sParticle->m_uchStartSize = 1.0f;
  1026. sParticle->m_uchEndSize = 1.0f;
  1027. sParticle->m_vecVelocity = RandomVector( -8.0f, 8.0f );
  1028. }
  1029. //-----------------------------------------------------------------------------
  1030. // Purpose:
  1031. // Input : fTimeDelta -
  1032. //-----------------------------------------------------------------------------
  1033. void C_SporeTrail::Update( float fTimeDelta )
  1034. {
  1035. if ( m_pParticleMgr == NULL )
  1036. return;
  1037. //Add new particles
  1038. if ( m_bEmit )
  1039. {
  1040. float tempDelta = fTimeDelta;
  1041. while ( m_teParticleSpawn.NextEvent( tempDelta ) )
  1042. {
  1043. AddParticles();
  1044. }
  1045. }
  1046. }
  1047. //-----------------------------------------------------------------------------
  1048. // Purpose:
  1049. // Input : &effectMatrix -
  1050. //-----------------------------------------------------------------------------
  1051. void C_SporeTrail::StartRender( VMatrix &effectMatrix )
  1052. {
  1053. effectMatrix = effectMatrix * m_mAttachmentMatrix;
  1054. }
  1055. void C_SporeTrail::RenderParticles( CParticleRenderIterator *pIterator )
  1056. {
  1057. if ( m_bEmit == false )
  1058. return;
  1059. const SimpleParticle *pParticle = (const SimpleParticle*)pIterator->GetFirst();
  1060. while ( pParticle )
  1061. {
  1062. //Render
  1063. Vector tPos;
  1064. TransformParticle( m_pParticleMgr->GetModelView(), pParticle->m_Pos, tPos );
  1065. float sortKey = tPos.z;
  1066. Vector color = Vector( 1.0f, 1.0f, 1.0f );
  1067. //Render it
  1068. RenderParticle_ColorSize(
  1069. pIterator->GetParticleDraw(),
  1070. tPos,
  1071. color,
  1072. 1.0f,
  1073. 4 );
  1074. pParticle = (const SimpleParticle*)pIterator->GetNext( sortKey );
  1075. }
  1076. }
  1077. void C_SporeTrail::SimulateParticles( CParticleSimulateIterator *pIterator )
  1078. {
  1079. if ( m_bEmit == false )
  1080. return;
  1081. SimpleParticle *pParticle = (SimpleParticle*)pIterator->GetFirst();
  1082. while ( pParticle )
  1083. {
  1084. //UpdateVelocity( pParticle, timeDelta );
  1085. pParticle->m_Pos += pParticle->m_vecVelocity * pIterator->GetTimeDelta();
  1086. //Should this particle die?
  1087. pParticle->m_flLifetime += pIterator->GetTimeDelta();
  1088. if ( pParticle->m_flLifetime >= pParticle->m_flDieTime )
  1089. {
  1090. pIterator->RemoveParticle( pParticle );
  1091. }
  1092. pParticle = (SimpleParticle*)pIterator->GetNext();
  1093. }
  1094. }
  1095. //-----------------------------------------------------------------------------
  1096. // Purpose:
  1097. //-----------------------------------------------------------------------------
  1098. void C_SporeTrail::GetAimEntOrigin( IClientEntity *pAttachedTo, Vector *pAbsOrigin, QAngle *pAbsAngles )
  1099. {
  1100. C_BaseEntity *pEnt = pAttachedTo->GetBaseEntity();
  1101. pEnt->GetAttachment( 1, *pAbsOrigin, *pAbsAngles );
  1102. matrix3x4_t matrix;
  1103. AngleMatrix( *pAbsAngles, *pAbsOrigin, matrix );
  1104. m_mAttachmentMatrix.Init( matrix );
  1105. }
  1106. //==================================================
  1107. // FireTrailhou
  1108. //==================================================
  1109. // Datatable.. this can have all the smoketrail parameters when we need it to.
  1110. IMPLEMENT_CLIENTCLASS_DT(C_FireTrail, DT_FireTrail, CFireTrail)
  1111. RecvPropInt(RECVINFO(m_nAttachment)),
  1112. RecvPropFloat(RECVINFO(m_flLifetime)),
  1113. END_RECV_TABLE()
  1114. // ------------------------------------------------------------------------- //
  1115. // ParticleMovieExplosion
  1116. // ------------------------------------------------------------------------- //
  1117. C_FireTrail::C_FireTrail()
  1118. {
  1119. }
  1120. //-----------------------------------------------------------------------------
  1121. // Purpose:
  1122. //-----------------------------------------------------------------------------
  1123. C_FireTrail::~C_FireTrail( void )
  1124. {
  1125. }
  1126. //-----------------------------------------------------------------------------
  1127. // Purpose:
  1128. // Input : *pParticleMgr -
  1129. // *pArgs -
  1130. //-----------------------------------------------------------------------------
  1131. void C_FireTrail::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  1132. {
  1133. BaseClass::Start( pParticleMgr, pArgs );
  1134. m_pTrailEmitter = CSimpleEmitter::Create( "FireTrail" );
  1135. if ( !m_pTrailEmitter )
  1136. {
  1137. Assert( false );
  1138. return;
  1139. }
  1140. m_pTrailEmitter->SetSortOrigin( GetAbsOrigin() );
  1141. // Setup our materials
  1142. m_hMaterial[FTRAIL_SMOKE1] = g_Mat_DustPuff[0];
  1143. m_hMaterial[FTRAIL_SMOKE2] = g_Mat_DustPuff[1];
  1144. m_hMaterial[FTRAIL_FLAME1] = m_pTrailEmitter->GetPMaterial( "sprites/flamelet1" );
  1145. m_hMaterial[FTRAIL_FLAME2] = m_pTrailEmitter->GetPMaterial( "sprites/flamelet2" );
  1146. m_hMaterial[FTRAIL_FLAME3] = m_pTrailEmitter->GetPMaterial( "sprites/flamelet3" );
  1147. m_hMaterial[FTRAIL_FLAME4] = m_pTrailEmitter->GetPMaterial( "sprites/flamelet4" );
  1148. m_hMaterial[FTRAIL_FLAME5] = m_pTrailEmitter->GetPMaterial( "sprites/flamelet5" );
  1149. // Setup our smoke emitter
  1150. m_pSmokeEmitter = CSmokeParticle::Create( "FireTrail_Smoke" );
  1151. m_pSmokeEmitter->SetSortOrigin( GetAbsOrigin() );
  1152. m_pSmokeEmitter->SetNearClip( 64.0f, 128.0f );
  1153. if ( !m_pSmokeEmitter )
  1154. {
  1155. Assert( false );
  1156. return;
  1157. }
  1158. // Seed our first position as the last known one
  1159. m_vecLastPosition = GetAbsOrigin();
  1160. }
  1161. //-----------------------------------------------------------------------------
  1162. // Purpose:
  1163. // Input : fTimeDelta -
  1164. //-----------------------------------------------------------------------------
  1165. void C_FireTrail::Update( float fTimeDelta )
  1166. {
  1167. if ( !m_pTrailEmitter )
  1168. return;
  1169. if ( ( m_flLifetime != 0 ) && ( m_flLifetime <= gpGlobals->curtime ) )
  1170. return;
  1171. CSmartPtr<CSimpleEmitter> pSimple = CSimpleEmitter::Create( "FireTrail" );
  1172. pSimple->SetSortOrigin( GetAbsOrigin() );
  1173. Vector offset;
  1174. #define STARTSIZE 8
  1175. #define ENDSIZE 16
  1176. #define PARTICLE_LIFETIME 0.075f
  1177. #define MIN_SPEED 32
  1178. #define MAX_SPEED 64
  1179. // Add new particles
  1180. //if ( ShouldEmit() )
  1181. {
  1182. Vector moveDiff = GetAbsOrigin() - m_vecLastPosition;
  1183. float moveLength = VectorNormalize( moveDiff );
  1184. int numPuffs = moveLength / ( STARTSIZE / 2.0f );
  1185. //FIXME: More rational cap here, perhaps
  1186. numPuffs = clamp( numPuffs, 1, 32 );
  1187. SimpleParticle *pParticle;
  1188. Vector offset;
  1189. Vector offsetColor;
  1190. float step = moveLength / numPuffs;
  1191. //Fill in the gaps
  1192. for ( int i = 1; i < numPuffs+1; i++ )
  1193. {
  1194. offset = m_vecLastPosition + ( moveDiff * step * i ) + RandomVector( -4.0f, 4.0f );
  1195. //debugoverlay->AddBoxOverlay( offset, -Vector(2,2,2), Vector(2,2,2), vec3_angle, i*4, i*4, i*4, true, 1.0f );
  1196. pParticle = (SimpleParticle *) m_pSmokeEmitter->AddParticle( sizeof( SimpleParticle ), m_hMaterial[random->RandomInt( FTRAIL_FLAME1,FTRAIL_FLAME5 )], offset );
  1197. if ( pParticle != NULL )
  1198. {
  1199. pParticle->m_flLifetime = 0.0f;
  1200. pParticle->m_flDieTime = /*PARTICLE_LIFETIME*/ 0.5f;// + random->RandomFloat(PARTICLE_LIFETIME*0.75f, PARTICLE_LIFETIME*1.25f);
  1201. pParticle->m_vecVelocity.Random( 0.0f, 1.0f );
  1202. pParticle->m_vecVelocity *= random->RandomFloat( MIN_SPEED, MAX_SPEED );
  1203. pParticle->m_vecVelocity[2] += 50;//random->RandomFloat( 32, 64 );
  1204. pParticle->m_uchColor[0] = 255.0f;
  1205. pParticle->m_uchColor[1] = 255.0f;
  1206. pParticle->m_uchColor[2] = 255.0f;
  1207. pParticle->m_uchStartSize = STARTSIZE * 2.0f;
  1208. pParticle->m_uchEndSize = STARTSIZE * 0.5f;
  1209. pParticle->m_uchStartAlpha = 255;
  1210. pParticle->m_uchEndAlpha = 0;
  1211. pParticle->m_flRoll = 0.0f;//random->RandomInt( 0, 360 );
  1212. pParticle->m_flRollDelta = random->RandomFloat( -16.0f, 16.0f );
  1213. }
  1214. }
  1215. //
  1216. // Smoke
  1217. //
  1218. offset = RandomVector( -STARTSIZE*0.5f, STARTSIZE*0.5f ) + GetAbsOrigin();
  1219. pParticle = (SimpleParticle *) m_pSmokeEmitter->AddParticle( sizeof( SimpleParticle ), m_hMaterial[random->RandomInt( FTRAIL_SMOKE1, FTRAIL_SMOKE2 )], offset );
  1220. if ( pParticle != NULL )
  1221. {
  1222. pParticle->m_flLifetime = 0.0f;
  1223. pParticle->m_flDieTime = ( PARTICLE_LIFETIME * 10.0f ) + random->RandomFloat(PARTICLE_LIFETIME*0.75f, PARTICLE_LIFETIME*1.25f);
  1224. pParticle->m_vecVelocity.Random( 0.0f, 1.0f );
  1225. pParticle->m_vecVelocity *= random->RandomFloat( MIN_SPEED, MAX_SPEED );
  1226. pParticle->m_vecVelocity[2] += random->RandomFloat( 50, 100 );
  1227. pParticle->m_uchColor[0] = 255.0f * 0.5f;
  1228. pParticle->m_uchColor[1] = 245.0f * 0.5f;
  1229. pParticle->m_uchColor[2] = 205.0f * 0.5f;
  1230. pParticle->m_uchStartSize = 16 * random->RandomFloat( 0.75f, 1.25f );
  1231. pParticle->m_uchEndSize = pParticle->m_uchStartSize * 2.5f;
  1232. pParticle->m_uchStartAlpha = 64;
  1233. pParticle->m_uchEndAlpha = 0;
  1234. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  1235. pParticle->m_flRollDelta = random->RandomFloat( -16.0f, 16.0f );
  1236. }
  1237. }
  1238. // Save off this position
  1239. m_vecLastPosition = GetAbsOrigin();
  1240. }
  1241. // Datatable.. this can have all the smoketrail parameters when we need it to.
  1242. IMPLEMENT_CLIENTCLASS_DT(C_DustTrail, DT_DustTrail, DustTrail)
  1243. RecvPropFloat(RECVINFO(m_SpawnRate)),
  1244. RecvPropVector(RECVINFO(m_Color)),
  1245. RecvPropFloat(RECVINFO(m_ParticleLifetime)),
  1246. RecvPropFloat(RECVINFO(m_StopEmitTime)),
  1247. RecvPropFloat(RECVINFO(m_MinSpeed)),
  1248. RecvPropFloat(RECVINFO(m_MaxSpeed)),
  1249. RecvPropFloat(RECVINFO(m_MinDirectedSpeed)),
  1250. RecvPropFloat(RECVINFO(m_MaxDirectedSpeed)),
  1251. RecvPropFloat(RECVINFO(m_StartSize)),
  1252. RecvPropFloat(RECVINFO(m_EndSize)),
  1253. RecvPropFloat(RECVINFO(m_SpawnRadius)),
  1254. RecvPropInt(RECVINFO(m_bEmit)),
  1255. RecvPropFloat(RECVINFO(m_Opacity)),
  1256. END_RECV_TABLE()
  1257. // ------------------------------------------------------------------------- //
  1258. // ParticleMovieExplosion
  1259. // ------------------------------------------------------------------------- //
  1260. C_DustTrail::C_DustTrail()
  1261. {
  1262. for (int i = 0; i < DUSTTRAIL_MATERIALS; i++)
  1263. {
  1264. m_MaterialHandle[i] = NULL;
  1265. }
  1266. m_SpawnRate = 10;
  1267. m_ParticleSpawn.Init(10);
  1268. m_Color.Init(0.5, 0.5, 0.5);
  1269. m_ParticleLifetime = 5;
  1270. m_StartEmitTime = gpGlobals->curtime;
  1271. m_StopEmitTime = 0; // No end time
  1272. m_MinSpeed = 2;
  1273. m_MaxSpeed = 4;
  1274. m_MinDirectedSpeed = m_MaxDirectedSpeed = 0;
  1275. m_StartSize = 35;
  1276. m_EndSize = 55;
  1277. m_SpawnRadius = 2;
  1278. m_VelocityOffset.Init();
  1279. m_Opacity = 0.5f;
  1280. m_bEmit = true;
  1281. m_pDustEmitter = NULL;
  1282. m_pParticleMgr = NULL;
  1283. }
  1284. C_DustTrail::~C_DustTrail()
  1285. {
  1286. if ( ToolsEnabled() && clienttools->IsInRecordingMode() && m_pDustEmitter.IsValid() && m_pDustEmitter->GetToolParticleEffectId() != TOOLPARTICLESYSTEMID_INVALID )
  1287. {
  1288. KeyValues *msg = new KeyValues( "OldParticleSystem_ActivateEmitter" );
  1289. msg->SetInt( "id", m_pDustEmitter->GetToolParticleEffectId() );
  1290. msg->SetInt( "emitter", 0 );
  1291. msg->SetInt( "active", false );
  1292. msg->SetFloat( "time", gpGlobals->curtime );
  1293. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  1294. msg->deleteThis();
  1295. }
  1296. if ( m_pParticleMgr )
  1297. {
  1298. m_pParticleMgr->RemoveEffect( &m_ParticleEffect );
  1299. }
  1300. }
  1301. //-----------------------------------------------------------------------------
  1302. // Purpose:
  1303. // Input : bEmit -
  1304. //-----------------------------------------------------------------------------
  1305. void C_DustTrail::SetEmit(bool bEmit)
  1306. {
  1307. m_bEmit = bEmit;
  1308. }
  1309. //-----------------------------------------------------------------------------
  1310. // Purpose:
  1311. // Input : rate -
  1312. //-----------------------------------------------------------------------------
  1313. void C_DustTrail::SetSpawnRate(float rate)
  1314. {
  1315. m_SpawnRate = rate;
  1316. m_ParticleSpawn.Init(rate);
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. // Purpose:
  1320. // Input : bnewentity -
  1321. //-----------------------------------------------------------------------------
  1322. void C_DustTrail::OnDataChanged(DataUpdateType_t updateType)
  1323. {
  1324. C_BaseEntity::OnDataChanged(updateType);
  1325. if ( updateType == DATA_UPDATE_CREATED )
  1326. {
  1327. Start( ParticleMgr(), NULL );
  1328. }
  1329. }
  1330. // FIXME: These all have to be moved out of this old system and into the new to leverage art assets!
  1331. PRECACHE_REGISTER_BEGIN( GLOBAL, PrecacheEffectDusttrail )
  1332. PRECACHE( MATERIAL, "particle/smokesprites_0001" )
  1333. /*
  1334. PRECACHE( MATERIAL, "particle/smokesprites_0002" )
  1335. PRECACHE( MATERIAL, "particle/smokesprites_0003" )
  1336. PRECACHE( MATERIAL, "particle/smokesprites_0004" )
  1337. PRECACHE( MATERIAL, "particle/smokesprites_0005" )
  1338. PRECACHE( MATERIAL, "particle/smokesprites_0006" )
  1339. PRECACHE( MATERIAL, "particle/smokesprites_0007" )
  1340. PRECACHE( MATERIAL, "particle/smokesprites_0008" )
  1341. PRECACHE( MATERIAL, "particle/smokesprites_0009" )
  1342. PRECACHE( MATERIAL, "particle/smokesprites_0010" )
  1343. PRECACHE( MATERIAL, "particle/smokesprites_0011" )
  1344. PRECACHE( MATERIAL, "particle/smokesprites_0012" )
  1345. PRECACHE( MATERIAL, "particle/smokesprites_0013" )
  1346. PRECACHE( MATERIAL, "particle/smokesprites_0014" )
  1347. PRECACHE( MATERIAL, "particle/smokesprites_0015" )
  1348. PRECACHE( MATERIAL, "particle/smokesprites_0016" )
  1349. */
  1350. PRECACHE_REGISTER_END()
  1351. //-----------------------------------------------------------------------------
  1352. // Purpose:
  1353. // Input : *pParticleMgr -
  1354. // *pArgs -
  1355. //-----------------------------------------------------------------------------
  1356. void C_DustTrail::Start( CParticleMgr *pParticleMgr, IPrototypeArgAccess *pArgs )
  1357. {
  1358. if(!pParticleMgr->AddEffect( &m_ParticleEffect, this ))
  1359. return;
  1360. m_pParticleMgr = pParticleMgr;
  1361. m_pDustEmitter = CDustFollower::Create("DustTrail");
  1362. if ( !m_pDustEmitter )
  1363. {
  1364. Assert( false );
  1365. return;
  1366. }
  1367. m_pDustEmitter->SetSortOrigin( GetAbsOrigin() );
  1368. m_pDustEmitter->SetNearClip( 64.0f, 128.0f );
  1369. for (int i = 0; i < DUSTTRAIL_MATERIALS; i++)
  1370. {
  1371. //char name[256];
  1372. //Q_snprintf( name, sizeof( name ), "particle/smokesprites_%04d", i + 1 );
  1373. m_MaterialHandle[i] = m_pDustEmitter->GetPMaterial( "particle/smokesprites_0001" );
  1374. }
  1375. m_ParticleSpawn.Init( m_SpawnRate );
  1376. }
  1377. //-----------------------------------------------------------------------------
  1378. // Purpose:
  1379. // Input : fTimeDelta -
  1380. //-----------------------------------------------------------------------------
  1381. void C_DustTrail::Update( float fTimeDelta )
  1382. {
  1383. if ( !m_pDustEmitter )
  1384. return;
  1385. Vector offsetColor;
  1386. // Add new particles
  1387. if ( !m_bEmit )
  1388. return;
  1389. if ( ( m_StopEmitTime != 0 ) && ( m_StopEmitTime <= gpGlobals->curtime ) )
  1390. return;
  1391. float tempDelta = fTimeDelta;
  1392. SimpleParticle *pParticle;
  1393. Vector offset;
  1394. Vector vecOrigin;
  1395. VectorMA( GetAbsOrigin(), -fTimeDelta, GetAbsVelocity(), vecOrigin );
  1396. Vector vecForward;
  1397. GetVectors( &vecForward, NULL, NULL );
  1398. while( m_ParticleSpawn.NextEvent( tempDelta ) )
  1399. {
  1400. float fldt = fTimeDelta - tempDelta;
  1401. offset.Random( -m_SpawnRadius, m_SpawnRadius );
  1402. offset += vecOrigin;
  1403. VectorMA( offset, fldt, GetAbsVelocity(), offset );
  1404. //if ( random->RandomFloat( 0.f, 5.0f ) > GetAbsVelocity().Length())
  1405. // continue;
  1406. pParticle = (SimpleParticle *) m_pDustEmitter->AddParticle( sizeof( SimpleParticle ), m_MaterialHandle[random->RandomInt(0,0)], offset ); // FIXME: the other sprites look bad
  1407. if ( pParticle == NULL )
  1408. continue;
  1409. pParticle->m_flLifetime = 0.0f;
  1410. pParticle->m_flDieTime = m_ParticleLifetime;
  1411. pParticle->m_vecVelocity.Random( -1.0f, 1.0f );
  1412. pParticle->m_vecVelocity *= random->RandomFloat( m_MinSpeed, m_MaxSpeed );
  1413. pParticle->m_vecVelocity = pParticle->m_vecVelocity + GetAbsVelocity();
  1414. float flDirectedVel = random->RandomFloat( m_MinDirectedSpeed, m_MaxDirectedSpeed );
  1415. VectorMA( pParticle->m_vecVelocity, flDirectedVel, vecForward, pParticle->m_vecVelocity );
  1416. offsetColor = m_Color;
  1417. float flMaxVal = MAX( m_Color[0], m_Color[1] );
  1418. if ( flMaxVal < m_Color[2] )
  1419. {
  1420. flMaxVal = m_Color[2];
  1421. }
  1422. offsetColor /= flMaxVal;
  1423. offsetColor *= random->RandomFloat( -0.2f, 0.2f );
  1424. offsetColor += m_Color;
  1425. offsetColor[0] = clamp( offsetColor[0], 0.0f, 1.0f );
  1426. offsetColor[1] = clamp( offsetColor[1], 0.0f, 1.0f );
  1427. offsetColor[2] = clamp( offsetColor[2], 0.0f, 1.0f );
  1428. pParticle->m_uchColor[0] = offsetColor[0]*255.0f;
  1429. pParticle->m_uchColor[1] = offsetColor[1]*255.0f;
  1430. pParticle->m_uchColor[2] = offsetColor[2]*255.0f;
  1431. pParticle->m_uchStartSize = m_StartSize;
  1432. pParticle->m_uchEndSize = m_EndSize;
  1433. float alpha = random->RandomFloat( m_Opacity*0.75f, m_Opacity*1.25f );
  1434. alpha = clamp( alpha, 0.0f, 1.0f );
  1435. if ( m_StopEmitTime != 0 && m_StopEmitTime > m_StartEmitTime )
  1436. {
  1437. alpha *= sqrt( (m_StopEmitTime - gpGlobals->curtime) /(m_StopEmitTime - m_StartEmitTime) );
  1438. }
  1439. pParticle->m_uchStartAlpha = alpha * 255;
  1440. pParticle->m_uchEndAlpha = 0;
  1441. pParticle->m_flRoll = random->RandomInt( 0, 360 );
  1442. pParticle->m_flRollDelta = random->RandomFloat( -1.0f, 1.0f );
  1443. }
  1444. }
  1445. void C_DustTrail::RenderParticles( CParticleRenderIterator *pIterator )
  1446. {
  1447. }
  1448. void C_DustTrail::SimulateParticles( CParticleSimulateIterator *pIterator )
  1449. {
  1450. }
  1451. //-----------------------------------------------------------------------------
  1452. // This is called after sending this entity's recording state
  1453. //-----------------------------------------------------------------------------
  1454. void C_DustTrail::CleanupToolRecordingState( KeyValues *msg )
  1455. {
  1456. if ( !ToolsEnabled() )
  1457. return;
  1458. BaseClass::CleanupToolRecordingState( msg );
  1459. // Generally, this is used to allow the entity to clean up
  1460. // allocated state it put into the message, but here we're going
  1461. // to use it to send particle system messages because we
  1462. // know the grenade has been recorded at this point
  1463. if ( !clienttools->IsInRecordingMode() || !m_pDustEmitter.IsValid() )
  1464. return;
  1465. // For now, we can't record Dusttrails that don't have a moveparent
  1466. C_BaseEntity *pEnt = GetMoveParent();
  1467. if ( !pEnt )
  1468. return;
  1469. bool bEmitterActive = m_bEmit && ( ( m_StopEmitTime == 0 ) || ( m_StopEmitTime > gpGlobals->curtime ) );
  1470. // NOTE: Particle system destruction message will be sent by the particle effect itself.
  1471. if ( m_pDustEmitter->GetToolParticleEffectId() == TOOLPARTICLESYSTEMID_INVALID )
  1472. {
  1473. int nId = m_pDustEmitter->AllocateToolParticleEffectId();
  1474. KeyValues *msg = new KeyValues( "OldParticleSystem_Create" );
  1475. msg->SetString( "name", "C_DustTrail" );
  1476. msg->SetInt( "id", nId );
  1477. msg->SetFloat( "time", gpGlobals->curtime );
  1478. KeyValues *pEmitter = msg->FindKey( "DmeSpriteEmitter", true );
  1479. pEmitter->SetString( "material", "particle/smokesprites_0001" );
  1480. pEmitter->SetInt( "count", m_SpawnRate ); // particles per second, when duration is < 0
  1481. pEmitter->SetFloat( "duration", -1 ); // FIXME
  1482. pEmitter->SetInt( "active", bEmitterActive );
  1483. KeyValues *pInitializers = pEmitter->FindKey( "initializers", true );
  1484. // FIXME: Until we can interpolate ent logs during emission, this can't work
  1485. KeyValues *pPosition = pInitializers->FindKey( "DmePositionPointToEntityInitializer", true );
  1486. pPosition->SetPtr( "entindex", (void*)pEnt->entindex() );
  1487. pPosition->SetInt( "attachmentIndex", GetParentAttachment() );
  1488. pPosition->SetFloat( "randomDist", m_SpawnRadius );
  1489. pPosition->SetFloat( "startx", pEnt->GetAbsOrigin().x );
  1490. pPosition->SetFloat( "starty", pEnt->GetAbsOrigin().y );
  1491. pPosition->SetFloat( "startz", pEnt->GetAbsOrigin().z );
  1492. KeyValues *pVelocity = pInitializers->FindKey( "DmeDecayVelocityInitializer", true );
  1493. pVelocity->SetFloat( "velocityX", pEnt->GetAbsVelocity().x );
  1494. pVelocity->SetFloat( "velocityY", pEnt->GetAbsVelocity().y );
  1495. pVelocity->SetFloat( "velocityZ", pEnt->GetAbsVelocity().z );
  1496. pVelocity->SetFloat( "decayto", 0.5 );
  1497. pVelocity->SetFloat( "decaytime", 0.3 );
  1498. KeyValues *pLifetime = pInitializers->FindKey( "DmeRandomLifetimeInitializer", true );
  1499. pLifetime->SetFloat( "minLifetime", m_ParticleLifetime );
  1500. pLifetime->SetFloat( "maxLifetime", m_ParticleLifetime );
  1501. KeyValues *pRoll = pInitializers->FindKey( "DmeRandomRollInitializer", true );
  1502. pRoll->SetFloat( "minRoll", 0.0f );
  1503. pRoll->SetFloat( "maxRoll", 360.0f );
  1504. KeyValues *pRollSpeed = pInitializers->FindKey( "DmeRandomRollSpeedInitializer", true );
  1505. pRollSpeed->SetFloat( "minRollSpeed", -1.0f );
  1506. pRollSpeed->SetFloat( "maxRollSpeed", 1.0f );
  1507. KeyValues *pColor = pInitializers->FindKey( "DmeRandomValueColorInitializer", true );
  1508. Color c(
  1509. clamp( m_Color.x * 255.0f, 0, 255 ),
  1510. clamp( m_Color.y * 255.0f, 0, 255 ),
  1511. clamp( m_Color.z * 255.0f, 0, 255 ), 255 );
  1512. pColor->SetColor( "startColor", c );
  1513. pColor->SetFloat( "minStartValueDelta", 0.0f );
  1514. pColor->SetFloat( "maxStartValueDelta", 0.0f );
  1515. pColor->SetColor( "endColor", c );
  1516. KeyValues *pAlpha = pInitializers->FindKey( "DmeRandomAlphaInitializer", true );
  1517. int nMinAlpha = 255 * m_Opacity * 0.75f;
  1518. int nMaxAlpha = 255 * m_Opacity * 1.25f;
  1519. pAlpha->SetInt( "minStartAlpha", clamp( nMinAlpha, 0, 255 ) );
  1520. pAlpha->SetInt( "maxStartAlpha", clamp( nMaxAlpha, 0, 255 ) );
  1521. pAlpha->SetInt( "minEndAlpha", clamp( nMinAlpha, 0, 255 ) );
  1522. pAlpha->SetInt( "maxEndAlpha", clamp( nMaxAlpha, 0, 255 ) );
  1523. KeyValues *pSize = pInitializers->FindKey( "DmeRandomSizeInitializer", true );
  1524. pSize->SetFloat( "minStartSize", m_StartSize );
  1525. pSize->SetFloat( "maxStartSize", m_StartSize );
  1526. pSize->SetFloat( "minEndSize", m_EndSize );
  1527. pSize->SetFloat( "maxEndSize", m_EndSize );
  1528. KeyValues *pUpdaters = pEmitter->FindKey( "updaters", true );
  1529. pUpdaters->FindKey( "DmePositionVelocityDecayUpdater", true );
  1530. pUpdaters->FindKey( "DmeRollUpdater", true );
  1531. KeyValues *pRollSpeedUpdater = pUpdaters->FindKey( "DmeRollSpeedAttenuateUpdater", true );
  1532. pRollSpeedUpdater->SetFloat( "attenuation", 1.0f - 8.0f / 30.0f );
  1533. pRollSpeedUpdater->SetFloat( "attenuationTme", 1.0f / 30.0f );
  1534. pRollSpeedUpdater->SetFloat( "minRollSpeed", 0.5f );
  1535. pUpdaters->FindKey( "DmeAlphaSineRampUpdater", true );
  1536. pUpdaters->FindKey( "DmeColorUpdater", true );
  1537. pUpdaters->FindKey( "DmeSizeUpdater", true );
  1538. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  1539. msg->deleteThis();
  1540. }
  1541. else
  1542. {
  1543. KeyValues *msg = new KeyValues( "OldParticleSystem_ActivateEmitter" );
  1544. msg->SetInt( "id", m_pDustEmitter->GetToolParticleEffectId() );
  1545. msg->SetInt( "emitter", 0 );
  1546. msg->SetInt( "active", bEmitterActive );
  1547. msg->SetFloat( "time", gpGlobals->curtime );
  1548. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  1549. msg->deleteThis();
  1550. }
  1551. }