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.

253 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: An entity that spawns and controls a particle system
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "particles/particles.h"
  8. #include "networkstringtable_gamedll.h"
  9. #include "particle_system.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. extern void SendProxy_Origin( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID );
  13. extern void SendProxy_Angles( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID );
  14. // Stripped down CBaseEntity send table
  15. IMPLEMENT_SERVERCLASS_ST_NOBASE(CParticleSystem, DT_ParticleSystem)
  16. SendPropVector (SENDINFO(m_vecOrigin), -1, SPROP_COORD|SPROP_CHANGES_OFTEN, 0.0f, HIGH_DEFAULT, SendProxy_Origin ),
  17. SendPropEHandle (SENDINFO(m_hOwnerEntity)),
  18. SendPropEHandle (SENDINFO_NAME(m_hMoveParent, moveparent)),
  19. SendPropInt (SENDINFO(m_iParentAttachment), NUM_PARENTATTACHMENT_BITS, SPROP_UNSIGNED),
  20. SendPropQAngles (SENDINFO(m_angRotation), 13, SPROP_CHANGES_OFTEN, SendProxy_Angles ),
  21. SendPropInt( SENDINFO(m_iEffectIndex), MAX_PARTICLESYSTEMS_STRING_BITS, SPROP_UNSIGNED ),
  22. SendPropBool( SENDINFO(m_bActive) ),
  23. SendPropFloat( SENDINFO(m_flStartTime) ),
  24. SendPropArray3( SENDINFO_ARRAY3(m_hControlPointEnts), SendPropEHandle( SENDINFO_ARRAY(m_hControlPointEnts) ) ),
  25. SendPropArray3( SENDINFO_ARRAY3(m_iControlPointParents), SendPropInt( SENDINFO_ARRAY(m_iControlPointParents), 3, SPROP_UNSIGNED ) ),
  26. SendPropBool( SENDINFO(m_bWeatherEffect) ),
  27. END_SEND_TABLE()
  28. BEGIN_DATADESC( CParticleSystem )
  29. DEFINE_KEYFIELD( m_bStartActive, FIELD_BOOLEAN, "start_active" ),
  30. DEFINE_KEYFIELD( m_bWeatherEffect, FIELD_BOOLEAN, "flag_as_weather" ),
  31. DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
  32. DEFINE_FIELD( m_flStartTime, FIELD_TIME ),
  33. DEFINE_KEYFIELD( m_iszEffectName, FIELD_STRING, "effect_name" ),
  34. //DEFINE_FIELD( m_iEffectIndex, FIELD_INTEGER ), // Don't save. Refind after loading.
  35. DEFINE_KEYFIELD( m_iszControlPointNames[0], FIELD_STRING, "cpoint1" ),
  36. DEFINE_KEYFIELD( m_iszControlPointNames[1], FIELD_STRING, "cpoint2" ),
  37. DEFINE_KEYFIELD( m_iszControlPointNames[2], FIELD_STRING, "cpoint3" ),
  38. DEFINE_KEYFIELD( m_iszControlPointNames[3], FIELD_STRING, "cpoint4" ),
  39. DEFINE_KEYFIELD( m_iszControlPointNames[4], FIELD_STRING, "cpoint5" ),
  40. DEFINE_KEYFIELD( m_iszControlPointNames[5], FIELD_STRING, "cpoint6" ),
  41. DEFINE_KEYFIELD( m_iszControlPointNames[6], FIELD_STRING, "cpoint7" ),
  42. DEFINE_KEYFIELD( m_iszControlPointNames[7], FIELD_STRING, "cpoint8" ),
  43. DEFINE_KEYFIELD( m_iszControlPointNames[8], FIELD_STRING, "cpoint9" ),
  44. DEFINE_KEYFIELD( m_iszControlPointNames[9], FIELD_STRING, "cpoint10" ),
  45. DEFINE_KEYFIELD( m_iszControlPointNames[10], FIELD_STRING, "cpoint11" ),
  46. DEFINE_KEYFIELD( m_iszControlPointNames[11], FIELD_STRING, "cpoint12" ),
  47. DEFINE_KEYFIELD( m_iszControlPointNames[12], FIELD_STRING, "cpoint13" ),
  48. DEFINE_KEYFIELD( m_iszControlPointNames[13], FIELD_STRING, "cpoint14" ),
  49. DEFINE_KEYFIELD( m_iszControlPointNames[14], FIELD_STRING, "cpoint15" ),
  50. DEFINE_KEYFIELD( m_iszControlPointNames[15], FIELD_STRING, "cpoint16" ),
  51. DEFINE_KEYFIELD( m_iszControlPointNames[16], FIELD_STRING, "cpoint17" ),
  52. DEFINE_KEYFIELD( m_iszControlPointNames[17], FIELD_STRING, "cpoint18" ),
  53. DEFINE_KEYFIELD( m_iszControlPointNames[18], FIELD_STRING, "cpoint19" ),
  54. DEFINE_KEYFIELD( m_iszControlPointNames[19], FIELD_STRING, "cpoint20" ),
  55. DEFINE_KEYFIELD( m_iszControlPointNames[20], FIELD_STRING, "cpoint21" ),
  56. DEFINE_KEYFIELD( m_iszControlPointNames[21], FIELD_STRING, "cpoint22" ),
  57. DEFINE_KEYFIELD( m_iszControlPointNames[22], FIELD_STRING, "cpoint23" ),
  58. DEFINE_KEYFIELD( m_iszControlPointNames[23], FIELD_STRING, "cpoint24" ),
  59. DEFINE_KEYFIELD( m_iszControlPointNames[24], FIELD_STRING, "cpoint25" ),
  60. DEFINE_KEYFIELD( m_iszControlPointNames[25], FIELD_STRING, "cpoint26" ),
  61. DEFINE_KEYFIELD( m_iszControlPointNames[26], FIELD_STRING, "cpoint27" ),
  62. DEFINE_KEYFIELD( m_iszControlPointNames[27], FIELD_STRING, "cpoint28" ),
  63. DEFINE_KEYFIELD( m_iszControlPointNames[28], FIELD_STRING, "cpoint29" ),
  64. DEFINE_KEYFIELD( m_iszControlPointNames[29], FIELD_STRING, "cpoint30" ),
  65. DEFINE_KEYFIELD( m_iszControlPointNames[30], FIELD_STRING, "cpoint31" ),
  66. DEFINE_KEYFIELD( m_iszControlPointNames[31], FIELD_STRING, "cpoint32" ),
  67. DEFINE_KEYFIELD( m_iszControlPointNames[32], FIELD_STRING, "cpoint33" ),
  68. DEFINE_KEYFIELD( m_iszControlPointNames[33], FIELD_STRING, "cpoint34" ),
  69. DEFINE_KEYFIELD( m_iszControlPointNames[34], FIELD_STRING, "cpoint35" ),
  70. DEFINE_KEYFIELD( m_iszControlPointNames[35], FIELD_STRING, "cpoint36" ),
  71. DEFINE_KEYFIELD( m_iszControlPointNames[36], FIELD_STRING, "cpoint37" ),
  72. DEFINE_KEYFIELD( m_iszControlPointNames[37], FIELD_STRING, "cpoint38" ),
  73. DEFINE_KEYFIELD( m_iszControlPointNames[38], FIELD_STRING, "cpoint39" ),
  74. DEFINE_KEYFIELD( m_iszControlPointNames[39], FIELD_STRING, "cpoint40" ),
  75. DEFINE_KEYFIELD( m_iszControlPointNames[40], FIELD_STRING, "cpoint41" ),
  76. DEFINE_KEYFIELD( m_iszControlPointNames[41], FIELD_STRING, "cpoint42" ),
  77. DEFINE_KEYFIELD( m_iszControlPointNames[42], FIELD_STRING, "cpoint43" ),
  78. DEFINE_KEYFIELD( m_iszControlPointNames[43], FIELD_STRING, "cpoint44" ),
  79. DEFINE_KEYFIELD( m_iszControlPointNames[44], FIELD_STRING, "cpoint45" ),
  80. DEFINE_KEYFIELD( m_iszControlPointNames[45], FIELD_STRING, "cpoint46" ),
  81. DEFINE_KEYFIELD( m_iszControlPointNames[46], FIELD_STRING, "cpoint47" ),
  82. DEFINE_KEYFIELD( m_iszControlPointNames[47], FIELD_STRING, "cpoint48" ),
  83. DEFINE_KEYFIELD( m_iszControlPointNames[48], FIELD_STRING, "cpoint49" ),
  84. DEFINE_KEYFIELD( m_iszControlPointNames[49], FIELD_STRING, "cpoint50" ),
  85. DEFINE_KEYFIELD( m_iszControlPointNames[50], FIELD_STRING, "cpoint51" ),
  86. DEFINE_KEYFIELD( m_iszControlPointNames[51], FIELD_STRING, "cpoint52" ),
  87. DEFINE_KEYFIELD( m_iszControlPointNames[52], FIELD_STRING, "cpoint53" ),
  88. DEFINE_KEYFIELD( m_iszControlPointNames[53], FIELD_STRING, "cpoint54" ),
  89. DEFINE_KEYFIELD( m_iszControlPointNames[54], FIELD_STRING, "cpoint55" ),
  90. DEFINE_KEYFIELD( m_iszControlPointNames[55], FIELD_STRING, "cpoint56" ),
  91. DEFINE_KEYFIELD( m_iszControlPointNames[56], FIELD_STRING, "cpoint57" ),
  92. DEFINE_KEYFIELD( m_iszControlPointNames[57], FIELD_STRING, "cpoint58" ),
  93. DEFINE_KEYFIELD( m_iszControlPointNames[58], FIELD_STRING, "cpoint59" ),
  94. DEFINE_KEYFIELD( m_iszControlPointNames[59], FIELD_STRING, "cpoint60" ),
  95. DEFINE_KEYFIELD( m_iszControlPointNames[60], FIELD_STRING, "cpoint61" ),
  96. DEFINE_KEYFIELD( m_iszControlPointNames[61], FIELD_STRING, "cpoint62" ),
  97. DEFINE_KEYFIELD( m_iszControlPointNames[62], FIELD_STRING, "cpoint63" ),
  98. DEFINE_KEYFIELD( m_iControlPointParents[0], FIELD_CHARACTER, "cpoint1_parent" ),
  99. DEFINE_KEYFIELD( m_iControlPointParents[1], FIELD_CHARACTER, "cpoint2_parent" ),
  100. DEFINE_KEYFIELD( m_iControlPointParents[2], FIELD_CHARACTER, "cpoint3_parent" ),
  101. DEFINE_KEYFIELD( m_iControlPointParents[3], FIELD_CHARACTER, "cpoint4_parent" ),
  102. DEFINE_KEYFIELD( m_iControlPointParents[4], FIELD_CHARACTER, "cpoint5_parent" ),
  103. DEFINE_KEYFIELD( m_iControlPointParents[5], FIELD_CHARACTER, "cpoint6_parent" ),
  104. DEFINE_KEYFIELD( m_iControlPointParents[6], FIELD_CHARACTER, "cpoint7_parent" ),
  105. DEFINE_AUTO_ARRAY( m_hControlPointEnts, FIELD_EHANDLE ),
  106. DEFINE_INPUTFUNC( FIELD_VOID, "Start", InputStart ),
  107. DEFINE_INPUTFUNC( FIELD_VOID, "Stop", InputStop ),
  108. DEFINE_THINKFUNC( StartParticleSystemThink ),
  109. END_DATADESC()
  110. LINK_ENTITY_TO_CLASS( info_particle_system, CParticleSystem );
  111. //-----------------------------------------------------------------------------
  112. // Purpose:
  113. //-----------------------------------------------------------------------------
  114. CParticleSystem::CParticleSystem()
  115. {
  116. m_bWeatherEffect = false;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Precache
  120. //-----------------------------------------------------------------------------
  121. void CParticleSystem::Precache( void )
  122. {
  123. const char *pParticleSystemName = STRING( m_iszEffectName );
  124. if ( pParticleSystemName == NULL || pParticleSystemName[0] == '\0' )
  125. {
  126. Warning( "info_particle_system (%s) has no particle system name specified!\n", GetEntityName().ToCStr() );
  127. }
  128. PrecacheParticleSystem( pParticleSystemName );
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose:
  132. //-----------------------------------------------------------------------------
  133. void CParticleSystem::Spawn( void )
  134. {
  135. BaseClass::Spawn();
  136. Precache();
  137. m_iEffectIndex = -1;
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose:
  141. //-----------------------------------------------------------------------------
  142. void CParticleSystem::Activate( void )
  143. {
  144. BaseClass::Activate();
  145. // Find our particle effect index
  146. m_iEffectIndex = GetParticleSystemIndex( STRING(m_iszEffectName) );
  147. if ( m_bStartActive )
  148. {
  149. m_bStartActive = false;
  150. StartParticleSystem();
  151. }
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose:
  155. //-----------------------------------------------------------------------------
  156. void CParticleSystem::StartParticleSystemThink( void )
  157. {
  158. StartParticleSystem();
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Purpose: Always transmitted to clients
  162. //-----------------------------------------------------------------------------
  163. int CParticleSystem::UpdateTransmitState()
  164. {
  165. return SetTransmitState( FL_EDICT_ALWAYS );
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose:
  169. //-----------------------------------------------------------------------------
  170. void CParticleSystem::StartParticleSystem( void )
  171. {
  172. if ( m_bActive == false )
  173. {
  174. m_flStartTime = gpGlobals->curtime;
  175. m_bActive = true;
  176. // Setup our control points at this time (in case our targets weren't around at spawn time)
  177. ReadControlPointEnts();
  178. }
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void CParticleSystem::StopParticleSystem( void )
  184. {
  185. m_bActive = false;
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose:
  189. //-----------------------------------------------------------------------------
  190. void CParticleSystem::InputStart( inputdata_t &inputdata )
  191. {
  192. StartParticleSystem();
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose:
  196. //-----------------------------------------------------------------------------
  197. void CParticleSystem::InputStop( inputdata_t &inputdata )
  198. {
  199. StopParticleSystem();
  200. }
  201. //-----------------------------------------------------------------------------
  202. // Purpose: Find each entity referred to by m_iszControlPointNames and
  203. // resolve it into the corresponding slot in m_hControlPointEnts
  204. //-----------------------------------------------------------------------------
  205. void CParticleSystem::ReadControlPointEnts( void )
  206. {
  207. for ( int i = 0 ; i < kMAXCONTROLPOINTS; ++i )
  208. {
  209. if ( m_iszControlPointNames[i] == NULL_STRING )
  210. continue;
  211. CBaseEntity *pPointEnt = gEntList.FindEntityGeneric( NULL, STRING( m_iszControlPointNames[i] ), this );
  212. Assert( pPointEnt != NULL );
  213. if ( pPointEnt == NULL )
  214. {
  215. Warning("Particle system %s could not find control point entity (%s)\n", GetEntityName().ToCStr(), m_iszControlPointNames[i].ToCStr() );
  216. continue;
  217. }
  218. m_hControlPointEnts.Set( i, pPointEnt );
  219. }
  220. }