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.

320 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "cbase.h"
  9. #include "c_basetempentity.h"
  10. #include "particle_simple3d.h"
  11. #include "tier1/KeyValues.h"
  12. #include "toolframework_client.h"
  13. #include "fx.h"
  14. #include "tier0/vprof.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. #define PI 3.14159265359
  18. #define GLASS_SHARD_MIN_LIFE 2
  19. #define GLASS_SHARD_MAX_LIFE 5
  20. #define GLASS_SHARD_NOISE 0.3
  21. #define GLASS_SHARD_GRAVITY 500
  22. #define GLASS_SHARD_DAMPING 0.3
  23. #include "clienteffectprecachesystem.h"
  24. CLIENTEFFECT_REGISTER_BEGIN( PrecacheEffectGlassShatter )
  25. CLIENTEFFECT_MATERIAL( "effects/fleck_glass1" )
  26. CLIENTEFFECT_MATERIAL( "effects/fleck_glass2" )
  27. CLIENTEFFECT_MATERIAL( "effects/fleck_tile1" )
  28. CLIENTEFFECT_MATERIAL( "effects/fleck_tile2" )
  29. CLIENTEFFECT_REGISTER_END()
  30. //###################################################
  31. // > C_TEShatterSurface
  32. //###################################################
  33. class C_TEShatterSurface : public C_BaseTempEntity
  34. {
  35. public:
  36. DECLARE_CLASS( C_TEShatterSurface, C_BaseTempEntity );
  37. DECLARE_CLIENTCLASS();
  38. C_TEShatterSurface( void );
  39. ~C_TEShatterSurface( void );
  40. virtual void PostDataUpdate( DataUpdateType_t updateType );
  41. private:
  42. // Recording
  43. void RecordShatterSurface( );
  44. public:
  45. Vector m_vecOrigin;
  46. QAngle m_vecAngles;
  47. Vector m_vecForce;
  48. Vector m_vecForcePos;
  49. float m_flWidth;
  50. float m_flHeight;
  51. float m_flShardSize;
  52. PMaterialHandle m_pMaterialHandle;
  53. int m_nSurfaceType;
  54. byte m_uchFrontColor[3];
  55. byte m_uchBackColor[3];
  56. };
  57. //------------------------------------------------------------------------------
  58. // Networking
  59. //------------------------------------------------------------------------------
  60. IMPLEMENT_CLIENTCLASS_EVENT_DT(C_TEShatterSurface, DT_TEShatterSurface, CTEShatterSurface)
  61. RecvPropVector( RECVINFO(m_vecOrigin)),
  62. RecvPropVector( RECVINFO(m_vecAngles)),
  63. RecvPropVector( RECVINFO(m_vecForce)),
  64. RecvPropVector( RECVINFO(m_vecForcePos)),
  65. RecvPropFloat( RECVINFO(m_flWidth)),
  66. RecvPropFloat( RECVINFO(m_flHeight)),
  67. RecvPropFloat( RECVINFO(m_flShardSize)),
  68. RecvPropInt( RECVINFO(m_nSurfaceType)),
  69. RecvPropInt( RECVINFO(m_uchFrontColor[0])),
  70. RecvPropInt( RECVINFO(m_uchFrontColor[1])),
  71. RecvPropInt( RECVINFO(m_uchFrontColor[2])),
  72. RecvPropInt( RECVINFO(m_uchBackColor[0])),
  73. RecvPropInt( RECVINFO(m_uchBackColor[1])),
  74. RecvPropInt( RECVINFO(m_uchBackColor[2])),
  75. END_RECV_TABLE()
  76. //------------------------------------------------------------------------------
  77. // Constructor, destructor
  78. //------------------------------------------------------------------------------
  79. C_TEShatterSurface::C_TEShatterSurface( void )
  80. {
  81. m_vecOrigin.Init();
  82. m_vecAngles.Init();
  83. m_vecForce.Init();
  84. m_vecForcePos.Init();
  85. m_flWidth = 16.0;
  86. m_flHeight = 16.0;
  87. m_flShardSize = 3;
  88. m_nSurfaceType = SHATTERSURFACE_GLASS;
  89. m_uchFrontColor[0] = 255;
  90. m_uchFrontColor[1] = 255;
  91. m_uchFrontColor[2] = 255;
  92. m_uchBackColor[0] = 255;
  93. m_uchBackColor[1] = 255;
  94. m_uchBackColor[2] = 255;
  95. }
  96. C_TEShatterSurface::~C_TEShatterSurface()
  97. {
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Recording
  101. //-----------------------------------------------------------------------------
  102. void C_TEShatterSurface::RecordShatterSurface( )
  103. {
  104. if ( !ToolsEnabled() )
  105. return;
  106. if ( clienttools->IsInRecordingMode() )
  107. {
  108. Color front( m_uchFrontColor[0], m_uchFrontColor[1], m_uchFrontColor[2], 255 );
  109. Color back( m_uchBackColor[0], m_uchBackColor[1], m_uchBackColor[2], 255 );
  110. KeyValues *msg = new KeyValues( "TempEntity" );
  111. msg->SetInt( "te", TE_SHATTER_SURFACE );
  112. msg->SetString( "name", "TE_ShatterSurface" );
  113. msg->SetFloat( "time", gpGlobals->curtime );
  114. msg->SetFloat( "originx", m_vecOrigin.x );
  115. msg->SetFloat( "originy", m_vecOrigin.y );
  116. msg->SetFloat( "originz", m_vecOrigin.z );
  117. msg->SetFloat( "anglesx", m_vecAngles.x );
  118. msg->SetFloat( "anglesy", m_vecAngles.y );
  119. msg->SetFloat( "anglesz", m_vecAngles.z );
  120. msg->SetFloat( "forcex", m_vecForce.x );
  121. msg->SetFloat( "forcey", m_vecForce.y );
  122. msg->SetFloat( "forcez", m_vecForce.z );
  123. msg->SetFloat( "forceposx", m_vecForcePos.x );
  124. msg->SetFloat( "forceposy", m_vecForcePos.y );
  125. msg->SetFloat( "forceposz", m_vecForcePos.z );
  126. msg->SetColor( "frontcolor", front );
  127. msg->SetColor( "backcolor", back );
  128. msg->SetFloat( "width", m_flWidth );
  129. msg->SetFloat( "height", m_flHeight );
  130. msg->SetFloat( "size", m_flShardSize );
  131. msg->SetInt( "surfacetype", m_nSurfaceType );
  132. ToolFramework_PostToolMessage( HTOOLHANDLE_INVALID, msg );
  133. msg->deleteThis();
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose:
  138. //-----------------------------------------------------------------------------
  139. void C_TEShatterSurface::PostDataUpdate( DataUpdateType_t updateType )
  140. {
  141. VPROF( "C_TEShatterSurface::PostDataUpdate" );
  142. RecordShatterSurface();
  143. CSmartPtr<CSimple3DEmitter> pGlassEmitter = CSimple3DEmitter::Create( "C_TEShatterSurface 1" );
  144. pGlassEmitter->SetSortOrigin( m_vecOrigin );
  145. Vector vecColor;
  146. engine->ComputeLighting( m_vecOrigin, NULL, true, vecColor );
  147. // HACK: Blend a little toward white to match the materials...
  148. VectorLerp( vecColor, Vector( 1, 1, 1 ), 0.3, vecColor );
  149. PMaterialHandle *hMaterial;
  150. if (m_nSurfaceType == SHATTERSURFACE_GLASS)
  151. {
  152. hMaterial = g_Mat_Fleck_Glass;
  153. }
  154. else
  155. {
  156. hMaterial = g_Mat_Fleck_Tile;
  157. }
  158. // ---------------------------------------------------
  159. // Figure out number of particles required to fill space
  160. // ---------------------------------------------------
  161. int nNumWide = m_flWidth / m_flShardSize;
  162. int nNumHigh = m_flHeight / m_flShardSize;
  163. Vector vWidthStep,vHeightStep;
  164. AngleVectors(m_vecAngles,NULL,&vWidthStep,&vHeightStep);
  165. vWidthStep *= m_flShardSize;
  166. vHeightStep *= m_flShardSize;
  167. // ---------------------
  168. // Create glass shards
  169. // ----------------------
  170. Vector vCurPos = m_vecOrigin;
  171. vCurPos.x += 0.5*m_flShardSize;
  172. vCurPos.z += 0.5*m_flShardSize;
  173. float flMinSpeed = 9999999999.0f;
  174. float flMaxSpeed = 0;
  175. Particle3D *pParticle = NULL;
  176. for (int width=0;width<nNumWide;width++)
  177. {
  178. for (int height=0;height<nNumHigh;height++)
  179. {
  180. pParticle = (Particle3D *) pGlassEmitter->AddParticle( sizeof(Particle3D), hMaterial[random->RandomInt(0,1)], vCurPos );
  181. Vector vForceVel = Vector(0,0,0);
  182. if (random->RandomInt(0, 3) != 0)
  183. {
  184. float flForceDistSqr = (vCurPos - m_vecForcePos).LengthSqr();
  185. vForceVel = m_vecForce;
  186. if (flForceDistSqr > 0 )
  187. {
  188. vForceVel *= ( 40.0f / flForceDistSqr );
  189. }
  190. }
  191. if (pParticle)
  192. {
  193. pParticle->m_flLifeRemaining = random->RandomFloat(GLASS_SHARD_MIN_LIFE,GLASS_SHARD_MAX_LIFE);
  194. pParticle->m_vecVelocity = vForceVel;
  195. pParticle->m_vecVelocity += RandomVector(-25,25);
  196. pParticle->m_uchSize = m_flShardSize + random->RandomFloat(-0.5*m_flShardSize,0.5*m_flShardSize);
  197. pParticle->m_vAngles = m_vecAngles;
  198. pParticle->m_flAngSpeed = random->RandomFloat(-400,400);
  199. pParticle->m_uchFrontColor[0] = (byte)(m_uchFrontColor[0] * vecColor.x );
  200. pParticle->m_uchFrontColor[1] = (byte)(m_uchFrontColor[1] * vecColor.y );
  201. pParticle->m_uchFrontColor[2] = (byte)(m_uchFrontColor[2] * vecColor.z );
  202. pParticle->m_uchBackColor[0] = (byte)(m_uchBackColor[0] * vecColor.x );
  203. pParticle->m_uchBackColor[1] = (byte)(m_uchBackColor[1] * vecColor.y );
  204. pParticle->m_uchBackColor[2] = (byte)(m_uchBackColor[2] * vecColor.z );
  205. }
  206. // Keep track of min and max speed for collision detection
  207. float flForceSpeed = vForceVel.Length();
  208. if (flForceSpeed > flMaxSpeed)
  209. {
  210. flMaxSpeed = flForceSpeed;
  211. }
  212. if (flForceSpeed < flMinSpeed)
  213. {
  214. flMinSpeed = flForceSpeed;
  215. }
  216. vCurPos += vHeightStep;
  217. }
  218. vCurPos -= nNumHigh*vHeightStep;
  219. vCurPos += vWidthStep;
  220. }
  221. // --------------------------------------------------
  222. // Set collision parameters
  223. // --------------------------------------------------
  224. Vector vMoveDir = m_vecForce;
  225. VectorNormalize(vMoveDir);
  226. pGlassEmitter->m_ParticleCollision.Setup( m_vecOrigin, &vMoveDir, GLASS_SHARD_NOISE,
  227. flMinSpeed, flMaxSpeed, GLASS_SHARD_GRAVITY, GLASS_SHARD_DAMPING );
  228. }
  229. void TE_ShatterSurface( IRecipientFilter& filter, float delay,
  230. const Vector* pos, const QAngle* angle, const Vector* vForce, const Vector* vForcePos,
  231. float width, float height, float shardsize, ShatterSurface_t surfacetype,
  232. int front_r, int front_g, int front_b, int back_r, int back_g, int back_b)
  233. {
  234. // Major hack to simulate receiving network message
  235. __g_C_TEShatterSurface.m_vecOrigin = *pos;
  236. __g_C_TEShatterSurface.m_vecAngles = *angle;
  237. __g_C_TEShatterSurface.m_vecForce = *vForce;
  238. __g_C_TEShatterSurface.m_vecForcePos = *vForcePos;
  239. __g_C_TEShatterSurface.m_flWidth = width;
  240. __g_C_TEShatterSurface.m_flHeight = height;
  241. __g_C_TEShatterSurface.m_flShardSize = shardsize;
  242. __g_C_TEShatterSurface.m_nSurfaceType = surfacetype;
  243. __g_C_TEShatterSurface.m_uchFrontColor[0] = front_r;
  244. __g_C_TEShatterSurface.m_uchFrontColor[1] = front_g;
  245. __g_C_TEShatterSurface.m_uchFrontColor[2] = front_b;
  246. __g_C_TEShatterSurface.m_uchBackColor[0] = back_r;
  247. __g_C_TEShatterSurface.m_uchBackColor[1] = back_g;
  248. __g_C_TEShatterSurface.m_uchBackColor[2] = back_b;
  249. __g_C_TEShatterSurface.PostDataUpdate( DATA_UPDATE_CREATED );
  250. }
  251. void TE_ShatterSurface( IRecipientFilter& filter, float delay, KeyValues *pKeyValues )
  252. {
  253. Vector vecOrigin, vecForce, vecForcePos;
  254. QAngle angles;
  255. vecOrigin.x = pKeyValues->GetFloat( "originx" );
  256. vecOrigin.y = pKeyValues->GetFloat( "originy" );
  257. vecOrigin.z = pKeyValues->GetFloat( "originz" );
  258. angles.x = pKeyValues->GetFloat( "anglesx" );
  259. angles.y = pKeyValues->GetFloat( "anglesy" );
  260. angles.z = pKeyValues->GetFloat( "anglesz" );
  261. vecForce.x = pKeyValues->GetFloat( "forcex" );
  262. vecForce.y = pKeyValues->GetFloat( "forcey" );
  263. vecForce.z = pKeyValues->GetFloat( "forcez" );
  264. vecForcePos.x = pKeyValues->GetFloat( "forceposx" );
  265. vecForcePos.y = pKeyValues->GetFloat( "forceposy" );
  266. vecForcePos.z = pKeyValues->GetFloat( "forceposz" );
  267. Color front = pKeyValues->GetColor( "frontcolor" );
  268. Color back = pKeyValues->GetColor( "backcolor" );
  269. float flWidth = pKeyValues->GetFloat( "width" );
  270. float flHeight = pKeyValues->GetFloat( "height" );
  271. float flSize = pKeyValues->GetFloat( "size" );
  272. ShatterSurface_t nSurfaceType = (ShatterSurface_t)pKeyValues->GetInt( "surfacetype" );
  273. TE_ShatterSurface( filter, 0.0f, &vecOrigin, &angles, &vecForce, &vecForcePos,
  274. flWidth, flHeight, flSize, nSurfaceType, front.r(), front.g(), front.b(),
  275. back.r(), back.g(), back.b() );
  276. }