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.

247 lines
8.0 KiB

  1. //===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Spatial entity.
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "cbase.h"
  8. #include "spatialentity.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. #define SPATIAL_ENT_THINK_RATE TICK_INTERVAL
  12. static const char *s_pFadeInContextThink = "SpatialEntityFadeInThink";
  13. static const char *s_pFadeOutContextThink = "SpatialEntityFadeOutThink";
  14. BEGIN_DATADESC( CSpatialEntity )
  15. DEFINE_THINKFUNC( FadeInThink ),
  16. DEFINE_THINKFUNC( FadeOutThink ),
  17. DEFINE_FIELD( m_flCurWeight, FIELD_FLOAT ),
  18. DEFINE_FIELD( m_flTimeStartFadeIn, FIELD_FLOAT ),
  19. DEFINE_FIELD( m_flTimeStartFadeOut, FIELD_FLOAT ),
  20. DEFINE_FIELD( m_flStartFadeInWeight, FIELD_FLOAT ),
  21. DEFINE_FIELD( m_flStartFadeOutWeight, FIELD_FLOAT ),
  22. DEFINE_KEYFIELD( m_MinFalloff, FIELD_FLOAT, "minfalloff" ),
  23. DEFINE_KEYFIELD( m_MaxFalloff, FIELD_FLOAT, "maxfalloff" ),
  24. DEFINE_KEYFIELD( m_flMaxWeight, FIELD_FLOAT, "maxweight" ),
  25. DEFINE_KEYFIELD( m_flFadeInDuration, FIELD_FLOAT, "fadeInDuration" ),
  26. DEFINE_KEYFIELD( m_flFadeOutDuration, FIELD_FLOAT, "fadeOutDuration" ),
  27. DEFINE_KEYFIELD( m_lookupFilename, FIELD_STRING, "filename" ),
  28. DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" ),
  29. DEFINE_KEYFIELD( m_bStartDisabled, FIELD_BOOLEAN, "StartDisabled" ),
  30. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  31. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  32. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetFadeInDuration", InputSetFadeInDuration ),
  33. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetFadeOutDuration", InputSetFadeOutDuration ),
  34. END_DATADESC()
  35. extern void SendProxy_Origin( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID );
  36. IMPLEMENT_SERVERCLASS_ST_NOBASE(CSpatialEntity, DT_SpatialEntity)
  37. SendPropVector( SENDINFO(m_vecOrigin), -1, SPROP_NOSCALE, 0.0f, HIGH_DEFAULT, SendProxy_Origin ),
  38. SendPropFloat( SENDINFO(m_MinFalloff) ),
  39. SendPropFloat( SENDINFO(m_MaxFalloff) ),
  40. SendPropFloat( SENDINFO(m_flCurWeight) ),
  41. SendPropBool( SENDINFO(m_bEnabled) ),
  42. END_SEND_TABLE()
  43. CSpatialEntity::CSpatialEntity() : BaseClass()
  44. {
  45. m_bEnabled = true;
  46. m_MinFalloff = 0.0f;
  47. m_MaxFalloff = 1000.0f;
  48. m_flMaxWeight = 1.0f;
  49. m_flCurWeight.Set( 0.0f );
  50. m_flFadeInDuration = 0.0f;
  51. m_flFadeOutDuration = 0.0f;
  52. m_flStartFadeInWeight = 0.0f;
  53. m_flStartFadeOutWeight = 0.0f;
  54. m_flTimeStartFadeIn = 0.0f;
  55. m_flTimeStartFadeOut = 0.0f;
  56. m_lookupFilename = NULL_STRING;
  57. }
  58. //------------------------------------------------------------------------------
  59. // Purpose : Send even though we don't have a model
  60. //------------------------------------------------------------------------------
  61. int CSpatialEntity::UpdateTransmitState()
  62. {
  63. // ALWAYS transmit to all clients.
  64. return SetTransmitState( FL_EDICT_ALWAYS );
  65. }
  66. //------------------------------------------------------------------------------
  67. // Purpose :
  68. //------------------------------------------------------------------------------
  69. void CSpatialEntity::Spawn( void )
  70. {
  71. AddEFlags( EFL_FORCE_CHECK_TRANSMIT | EFL_DIRTY_ABSTRANSFORM );
  72. Precache();
  73. SetSolid( SOLID_NONE );
  74. // To fade in/out the weight.
  75. SetContextThink( &CSpatialEntity::FadeInThink, TICK_NEVER_THINK, s_pFadeInContextThink );
  76. SetContextThink( &CSpatialEntity::FadeOutThink, TICK_NEVER_THINK, s_pFadeOutContextThink );
  77. if( m_bStartDisabled )
  78. {
  79. m_bEnabled = false;
  80. m_flCurWeight.Set ( 0.0f );
  81. }
  82. else
  83. {
  84. m_bEnabled = true;
  85. m_flCurWeight.Set ( 1.0f );
  86. }
  87. BaseClass::Spawn();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: Sets up internal vars needed for fade in lerping
  91. //-----------------------------------------------------------------------------
  92. void CSpatialEntity::FadeIn ( void )
  93. {
  94. m_bEnabled = true;
  95. m_flTimeStartFadeIn = gpGlobals->curtime;
  96. m_flStartFadeInWeight = m_flCurWeight;
  97. SetNextThink ( gpGlobals->curtime + SPATIAL_ENT_THINK_RATE, s_pFadeInContextThink );
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose: Sets up internal vars needed for fade out lerping
  101. //-----------------------------------------------------------------------------
  102. void CSpatialEntity::FadeOut ( void )
  103. {
  104. m_bEnabled = false;
  105. m_flTimeStartFadeOut = gpGlobals->curtime;
  106. m_flStartFadeOutWeight = m_flCurWeight;
  107. SetNextThink ( gpGlobals->curtime + SPATIAL_ENT_THINK_RATE, s_pFadeOutContextThink );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose: Fades lookup weight from CurWeight->MaxWeight
  111. //-----------------------------------------------------------------------------
  112. void CSpatialEntity::FadeInThink( void )
  113. {
  114. // Check for conditions where we shouldnt fade in
  115. if ( m_flFadeInDuration <= 0 || // not set to fade in
  116. m_flCurWeight >= m_flMaxWeight || // already past max weight
  117. !m_bEnabled || // fade in/out mutex
  118. m_flMaxWeight == 0.0f || // min==max
  119. m_flStartFadeInWeight >= m_flMaxWeight ) // already at max weight
  120. {
  121. SetNextThink ( TICK_NEVER_THINK, s_pFadeInContextThink );
  122. return;
  123. }
  124. // If we started fading in without fully fading out, use a truncated duration
  125. float flTimeToFade = m_flFadeInDuration;
  126. if ( m_flStartFadeInWeight > 0.0f )
  127. {
  128. float flWeightRatio = m_flStartFadeInWeight / m_flMaxWeight;
  129. flWeightRatio = clamp ( flWeightRatio, 0.0f, 0.99f );
  130. flTimeToFade = m_flFadeInDuration * (1.0 - flWeightRatio);
  131. }
  132. Assert ( flTimeToFade > 0.0f );
  133. float flFadeRatio = (gpGlobals->curtime - m_flTimeStartFadeIn) / flTimeToFade;
  134. flFadeRatio = clamp ( flFadeRatio, 0.0f, 1.0f );
  135. m_flStartFadeInWeight = clamp ( m_flStartFadeInWeight, 0.0f, 1.0f );
  136. m_flCurWeight = Lerp( flFadeRatio, m_flStartFadeInWeight, m_flMaxWeight );
  137. SetNextThink( gpGlobals->curtime + SPATIAL_ENT_THINK_RATE, s_pFadeInContextThink );
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Fades lookup weight from CurWeight->0.0
  141. //-----------------------------------------------------------------------------
  142. void CSpatialEntity::FadeOutThink( void )
  143. {
  144. // Check for conditions where we shouldn't fade out
  145. if ( m_flFadeOutDuration <= 0 || // not set to fade out
  146. m_flCurWeight <= 0.0f || // already faded out
  147. m_bEnabled || // fade in/out mutex
  148. m_flMaxWeight == 0.0f || // min==max
  149. m_flStartFadeOutWeight <= 0.0f )// already at min weight
  150. {
  151. SetNextThink ( TICK_NEVER_THINK, s_pFadeOutContextThink );
  152. return;
  153. }
  154. // If we started fading out without fully fading in, use a truncated duration
  155. float flTimeToFade = m_flFadeOutDuration;
  156. if ( m_flStartFadeOutWeight < m_flMaxWeight )
  157. {
  158. float flWeightRatio = m_flStartFadeOutWeight / m_flMaxWeight;
  159. flWeightRatio = clamp ( flWeightRatio, 0.01f, 1.0f );
  160. flTimeToFade = m_flFadeOutDuration * flWeightRatio;
  161. }
  162. Assert ( flTimeToFade > 0.0f );
  163. float flFadeRatio = (gpGlobals->curtime - m_flTimeStartFadeOut) / flTimeToFade;
  164. flFadeRatio = clamp ( flFadeRatio, 0.0f, 1.0f );
  165. m_flStartFadeOutWeight = clamp ( m_flStartFadeOutWeight, 0.0f, 1.0f );
  166. m_flCurWeight = Lerp( 1.0f - flFadeRatio, 0.0f, m_flStartFadeOutWeight );
  167. SetNextThink( gpGlobals->curtime + SPATIAL_ENT_THINK_RATE, s_pFadeOutContextThink );
  168. }
  169. //------------------------------------------------------------------------------
  170. // Purpose : Input handlers
  171. //------------------------------------------------------------------------------
  172. void CSpatialEntity::InputEnable( inputdata_t &inputdata )
  173. {
  174. m_bEnabled = true;
  175. if ( m_flFadeInDuration > 0.0f )
  176. {
  177. FadeIn();
  178. }
  179. else
  180. {
  181. m_flCurWeight = m_flMaxWeight;
  182. }
  183. }
  184. void CSpatialEntity::InputDisable( inputdata_t &inputdata )
  185. {
  186. m_bEnabled = false;
  187. if ( m_flFadeOutDuration > 0.0f )
  188. {
  189. FadeOut();
  190. }
  191. else
  192. {
  193. m_flCurWeight = 0.0f;
  194. }
  195. }
  196. void CSpatialEntity::InputSetFadeInDuration( inputdata_t& inputdata )
  197. {
  198. m_flFadeInDuration = inputdata.value.Float();
  199. }
  200. void CSpatialEntity::InputSetFadeOutDuration( inputdata_t& inputdata )
  201. {
  202. m_flFadeOutDuration = inputdata.value.Float();
  203. }