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.

281 lines
7.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: spawn and think functions for editor-placed lights
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "lights.h"
  9. #include "world.h"
  10. #include "env_cascade_light.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. LINK_ENTITY_TO_CLASS( light, CLight );
  14. BEGIN_DATADESC( CLight )
  15. DEFINE_FIELD( m_iCurrentFade, FIELD_CHARACTER),
  16. DEFINE_FIELD( m_iTargetFade, FIELD_CHARACTER),
  17. DEFINE_KEYFIELD( m_iStyle, FIELD_INTEGER, "style" ),
  18. DEFINE_KEYFIELD( m_iDefaultStyle, FIELD_INTEGER, "defaultstyle" ),
  19. DEFINE_KEYFIELD( m_iszPattern, FIELD_STRING, "pattern" ),
  20. // Fuctions
  21. DEFINE_FUNCTION( FadeThink ),
  22. // Inputs
  23. DEFINE_INPUTFUNC( FIELD_STRING, "SetPattern", InputSetPattern ),
  24. DEFINE_INPUTFUNC( FIELD_STRING, "FadeToPattern", InputFadeToPattern ),
  25. DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
  26. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
  27. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff ),
  28. END_DATADESC()
  29. //
  30. // Cache user-entity-field values until spawn is called.
  31. //
  32. bool CLight::KeyValue( const char *szKeyName, const char *szValue )
  33. {
  34. if (FStrEq(szKeyName, "pitch"))
  35. {
  36. QAngle angles = GetAbsAngles();
  37. angles.x = atof(szValue);
  38. SetAbsAngles( angles );
  39. }
  40. else
  41. {
  42. return BaseClass::KeyValue( szKeyName, szValue );
  43. }
  44. return true;
  45. }
  46. // Light entity
  47. // If targeted, it will toggle between on or off.
  48. void CLight::Spawn( void )
  49. {
  50. if (!GetEntityName())
  51. { // inert light
  52. UTIL_Remove( this );
  53. return;
  54. }
  55. if (m_iStyle >= 32)
  56. {
  57. if ( m_iszPattern == NULL_STRING && m_iDefaultStyle > 0 )
  58. {
  59. m_iszPattern = MAKE_STRING(GetDefaultLightstyleString(m_iDefaultStyle));
  60. }
  61. if (FBitSet(m_spawnflags, SF_LIGHT_START_OFF))
  62. engine->LightStyle(m_iStyle, "a");
  63. else if (m_iszPattern != NULL_STRING)
  64. engine->LightStyle(m_iStyle, (char *)STRING( m_iszPattern ));
  65. else
  66. engine->LightStyle(m_iStyle, "m");
  67. }
  68. }
  69. void CLight::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  70. {
  71. if (m_iStyle >= 32)
  72. {
  73. if ( !ShouldToggle( useType, !FBitSet(m_spawnflags, SF_LIGHT_START_OFF) ) )
  74. return;
  75. Toggle();
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: Turn the light on
  80. //-----------------------------------------------------------------------------
  81. void CLight::TurnOn( void )
  82. {
  83. if ( m_iszPattern != NULL_STRING )
  84. {
  85. engine->LightStyle( m_iStyle, (char *) STRING( m_iszPattern ) );
  86. }
  87. else
  88. {
  89. engine->LightStyle( m_iStyle, "m" );
  90. }
  91. CLEARBITS( m_spawnflags, SF_LIGHT_START_OFF );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose: Turn the light off
  95. //-----------------------------------------------------------------------------
  96. void CLight::TurnOff( void )
  97. {
  98. engine->LightStyle( m_iStyle, "a" );
  99. SETBITS( m_spawnflags, SF_LIGHT_START_OFF );
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose: Toggle the light on/off
  103. //-----------------------------------------------------------------------------
  104. void CLight::Toggle( void )
  105. {
  106. //Toggle it
  107. if ( FBitSet( m_spawnflags, SF_LIGHT_START_OFF ) )
  108. {
  109. TurnOn();
  110. }
  111. else
  112. {
  113. TurnOff();
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose: Handle the "turnon" input handler
  118. // Input : &inputdata -
  119. //-----------------------------------------------------------------------------
  120. void CLight::InputTurnOn( inputdata_t &inputdata )
  121. {
  122. TurnOn();
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose: Handle the "turnoff" input handler
  126. // Input : &inputdata -
  127. //-----------------------------------------------------------------------------
  128. void CLight::InputTurnOff( inputdata_t &inputdata )
  129. {
  130. TurnOff();
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose: Handle the "toggle" input handler
  134. // Input : &inputdata -
  135. //-----------------------------------------------------------------------------
  136. void CLight::InputToggle( inputdata_t &inputdata )
  137. {
  138. Toggle();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: Input handler for setting a light pattern
  142. //-----------------------------------------------------------------------------
  143. void CLight::InputSetPattern( inputdata_t &inputdata )
  144. {
  145. m_iszPattern = inputdata.value.StringID();
  146. engine->LightStyle(m_iStyle, (char *)STRING( m_iszPattern ));
  147. // Light is on if pattern is set
  148. CLEARBITS(m_spawnflags, SF_LIGHT_START_OFF);
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose: Input handler for fading from first value in old pattern to
  152. // first value in new pattern
  153. //-----------------------------------------------------------------------------
  154. void CLight::InputFadeToPattern( inputdata_t &inputdata )
  155. {
  156. m_iCurrentFade = (STRING(m_iszPattern))[0];
  157. m_iTargetFade = inputdata.value.String()[0];
  158. m_iszPattern = inputdata.value.StringID();
  159. SetThink(&CLight::FadeThink);
  160. SetNextThink( gpGlobals->curtime );
  161. // Light is on if pattern is set
  162. CLEARBITS(m_spawnflags, SF_LIGHT_START_OFF);
  163. }
  164. //------------------------------------------------------------------------------
  165. // Purpose : Fade light to new starting pattern value then stop thinking
  166. //------------------------------------------------------------------------------
  167. void CLight::FadeThink(void)
  168. {
  169. if (m_iCurrentFade < m_iTargetFade)
  170. {
  171. m_iCurrentFade++;
  172. }
  173. else if (m_iCurrentFade > m_iTargetFade)
  174. {
  175. m_iCurrentFade--;
  176. }
  177. // If we're done fading instantiate our light pattern and stop thinking
  178. if (m_iCurrentFade == m_iTargetFade)
  179. {
  180. engine->LightStyle(m_iStyle, (char *)STRING( m_iszPattern ));
  181. SetNextThink( TICK_NEVER_THINK );
  182. }
  183. // Otherwise instantiate our current fade value and keep thinking
  184. else
  185. {
  186. char sCurString[2];
  187. sCurString[0] = m_iCurrentFade;
  188. sCurString[1] = NULL;
  189. engine->LightStyle(m_iStyle, sCurString);
  190. // UNDONE: Consider making this settable war to control fade speed
  191. SetNextThink( gpGlobals->curtime + 0.1f );
  192. }
  193. }
  194. //
  195. // shut up spawn functions for new spotlights
  196. //
  197. LINK_ENTITY_TO_CLASS( light_spot, CLight );
  198. LINK_ENTITY_TO_CLASS( light_glspot, CLight );
  199. LINK_ENTITY_TO_CLASS( light_directional, CLight );
  200. class CEnvLight : public CLight
  201. {
  202. public:
  203. DECLARE_CLASS( CEnvLight, CLight );
  204. bool KeyValue( const char *szKeyName, const char *szValue );
  205. void Spawn( void );
  206. };
  207. LINK_ENTITY_TO_CLASS( light_environment, CEnvLight );
  208. bool CEnvLight::KeyValue( const char *szKeyName, const char *szValue )
  209. {
  210. bool bSuccess = true;
  211. if (FStrEq(szKeyName, "_light"))
  212. {
  213. int tmp[4];
  214. V_StringToIntArray( tmp, 4, szValue );
  215. CCascadeLight::SetLightColor( tmp[0], tmp[1], tmp[2], tmp[3] );
  216. }
  217. else if ( FStrEq( szKeyName, "_ambient" ) )
  218. {
  219. // nothing
  220. }
  221. else
  222. {
  223. bSuccess = BaseClass::KeyValue( szKeyName, szValue );
  224. }
  225. if ( FStrEq( szKeyName, "pitch" ) )
  226. {
  227. CCascadeLight::SetEnvLightShadowPitch( atof( szValue ) );
  228. }
  229. CCascadeLight::SetEnvLightShadowAngles( GetAbsAngles() );
  230. return bSuccess;
  231. }
  232. void CEnvLight::Spawn( void )
  233. {
  234. BaseClass::Spawn( );
  235. CCascadeLight::SetEnvLightShadowAngles( GetAbsAngles() );
  236. }