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.

265 lines
6.7 KiB

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