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.

251 lines
7.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Sunlight shadow control entity.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. //------------------------------------------------------------------------------
  11. // FIXME: This really should inherit from something more lightweight
  12. //------------------------------------------------------------------------------
  13. //------------------------------------------------------------------------------
  14. // Purpose : Sunlight shadow control entity
  15. //------------------------------------------------------------------------------
  16. class CSunlightShadowControl : public CBaseEntity
  17. {
  18. public:
  19. DECLARE_CLASS( CSunlightShadowControl, CBaseEntity );
  20. CSunlightShadowControl();
  21. void Spawn( void );
  22. bool KeyValue( const char *szKeyName, const char *szValue );
  23. virtual bool GetKeyValue( const char *szKeyName, char *szValue, int iMaxLen );
  24. int UpdateTransmitState();
  25. // Inputs
  26. void InputSetAngles( inputdata_t &inputdata );
  27. void InputEnable( inputdata_t &inputdata );
  28. void InputDisable( inputdata_t &inputdata );
  29. void InputSetTexture( inputdata_t &inputdata );
  30. void InputSetEnableShadows( inputdata_t &inputdata );
  31. void InputSetLightColor( inputdata_t &inputdata );
  32. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
  33. DECLARE_SERVERCLASS();
  34. DECLARE_DATADESC();
  35. private:
  36. CNetworkVector( m_shadowDirection );
  37. CNetworkVar( bool, m_bEnabled );
  38. bool m_bStartDisabled;
  39. CNetworkString( m_TextureName, MAX_PATH );
  40. CNetworkColor32( m_LightColor );
  41. CNetworkVar( float, m_flColorTransitionTime );
  42. CNetworkVar( float, m_flSunDistance );
  43. CNetworkVar( float, m_flFOV );
  44. CNetworkVar( float, m_flNearZ );
  45. CNetworkVar( float, m_flNorthOffset );
  46. CNetworkVar( bool, m_bEnableShadows );
  47. };
  48. LINK_ENTITY_TO_CLASS(sunlight_shadow_control, CSunlightShadowControl);
  49. BEGIN_DATADESC( CSunlightShadowControl )
  50. DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" ),
  51. DEFINE_KEYFIELD( m_bStartDisabled, FIELD_BOOLEAN, "StartDisabled" ),
  52. DEFINE_AUTO_ARRAY_KEYFIELD( m_TextureName, FIELD_CHARACTER, "texturename" ),
  53. DEFINE_KEYFIELD( m_flSunDistance, FIELD_FLOAT, "distance" ),
  54. DEFINE_KEYFIELD( m_flFOV, FIELD_FLOAT, "fov" ),
  55. DEFINE_KEYFIELD( m_flNearZ, FIELD_FLOAT, "nearz" ),
  56. DEFINE_KEYFIELD( m_flNorthOffset, FIELD_FLOAT, "northoffset" ),
  57. DEFINE_KEYFIELD( m_bEnableShadows, FIELD_BOOLEAN, "enableshadows" ),
  58. DEFINE_FIELD( m_LightColor, FIELD_COLOR32 ),
  59. DEFINE_KEYFIELD( m_flColorTransitionTime, FIELD_FLOAT, "colortransitiontime" ),
  60. // Inputs
  61. DEFINE_INPUT( m_flSunDistance, FIELD_FLOAT, "SetDistance" ),
  62. DEFINE_INPUT( m_flFOV, FIELD_FLOAT, "SetFOV" ),
  63. DEFINE_INPUT( m_flNearZ, FIELD_FLOAT, "SetNearZDistance" ),
  64. DEFINE_INPUT( m_flNorthOffset, FIELD_FLOAT, "SetNorthOffset" ),
  65. DEFINE_INPUTFUNC( FIELD_COLOR32, "LightColor", InputSetLightColor ),
  66. DEFINE_INPUTFUNC( FIELD_STRING, "SetAngles", InputSetAngles ),
  67. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  68. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  69. DEFINE_INPUTFUNC( FIELD_STRING, "SetTexture", InputSetTexture ),
  70. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "EnableShadows", InputSetEnableShadows ),
  71. END_DATADESC()
  72. IMPLEMENT_SERVERCLASS_ST_NOBASE(CSunlightShadowControl, DT_SunlightShadowControl)
  73. SendPropVector(SENDINFO(m_shadowDirection), -1, SPROP_NOSCALE ),
  74. SendPropBool(SENDINFO(m_bEnabled) ),
  75. SendPropString(SENDINFO(m_TextureName)),
  76. SendPropInt(SENDINFO (m_LightColor ), 32, SPROP_UNSIGNED, SendProxy_Color32ToInt32 ),
  77. SendPropFloat( SENDINFO( m_flColorTransitionTime ) ),
  78. SendPropFloat(SENDINFO(m_flSunDistance), 0, SPROP_NOSCALE ),
  79. SendPropFloat(SENDINFO(m_flFOV), 0, SPROP_NOSCALE ),
  80. SendPropFloat(SENDINFO(m_flNearZ), 0, SPROP_NOSCALE ),
  81. SendPropFloat(SENDINFO(m_flNorthOffset), 0, SPROP_NOSCALE ),
  82. SendPropBool( SENDINFO( m_bEnableShadows ) ),
  83. END_SEND_TABLE()
  84. CSunlightShadowControl::CSunlightShadowControl()
  85. {
  86. #if defined( _GAMECONSOLE )
  87. Q_strcpy( m_TextureName.GetForModify(), "effects/flashlight_border" );
  88. #else
  89. Q_strcpy( m_TextureName.GetForModify(), "effects/flashlight001" );
  90. #endif
  91. m_LightColor.Init( 255, 255, 255, 1 );
  92. m_flColorTransitionTime = 0.5f;
  93. m_flSunDistance = 10000.0f;
  94. m_flFOV = 5.0f;
  95. m_bEnableShadows = false;
  96. }
  97. //------------------------------------------------------------------------------
  98. // Purpose : Send even though we don't have a model
  99. //------------------------------------------------------------------------------
  100. int CSunlightShadowControl::UpdateTransmitState()
  101. {
  102. // ALWAYS transmit to all clients.
  103. return SetTransmitState( FL_EDICT_ALWAYS );
  104. }
  105. bool CSunlightShadowControl::KeyValue( const char *szKeyName, const char *szValue )
  106. {
  107. if ( FStrEq( szKeyName, "color" ) )
  108. {
  109. float tmp[4];
  110. UTIL_StringToFloatArray( tmp, 4, szValue );
  111. m_LightColor.SetR( tmp[0] );
  112. m_LightColor.SetG( tmp[1] );
  113. m_LightColor.SetB( tmp[2] );
  114. m_LightColor.SetA( tmp[3] );
  115. }
  116. else if ( FStrEq( szKeyName, "angles" ) )
  117. {
  118. QAngle angles;
  119. UTIL_StringToVector( angles.Base(), szValue );
  120. if (angles == vec3_angle)
  121. {
  122. angles.Init( 80, 30, 0 );
  123. }
  124. Vector vForward;
  125. AngleVectors( angles, &vForward );
  126. m_shadowDirection = vForward;
  127. return true;
  128. }
  129. else if ( FStrEq( szKeyName, "texturename" ) )
  130. {
  131. #if defined( _GAMECONSOLE )
  132. if ( Q_strcmp( szValue, "effects/flashlight001" ) == 0 )
  133. {
  134. // Use this as the default for Xbox
  135. Q_strcpy( m_TextureName.GetForModify(), "effects/flashlight_border" );
  136. }
  137. else
  138. {
  139. Q_strcpy( m_TextureName.GetForModify(), szValue );
  140. }
  141. #else
  142. Q_strcpy( m_TextureName.GetForModify(), szValue );
  143. #endif
  144. }
  145. return BaseClass::KeyValue( szKeyName, szValue );
  146. }
  147. bool CSunlightShadowControl::GetKeyValue( const char *szKeyName, char *szValue, int iMaxLen )
  148. {
  149. if ( FStrEq( szKeyName, "color" ) )
  150. {
  151. Q_snprintf( szValue, iMaxLen, "%d %d %d %d", m_LightColor.GetR(), m_LightColor.GetG(), m_LightColor.GetB(), m_LightColor.GetA() );
  152. return true;
  153. }
  154. else if ( FStrEq( szKeyName, "texturename" ) )
  155. {
  156. Q_snprintf( szValue, iMaxLen, "%s", m_TextureName.Get() );
  157. return true;
  158. }
  159. return BaseClass::GetKeyValue( szKeyName, szValue, iMaxLen );
  160. }
  161. //------------------------------------------------------------------------------
  162. // Purpose :
  163. //------------------------------------------------------------------------------
  164. void CSunlightShadowControl::Spawn( void )
  165. {
  166. Precache();
  167. SetSolid( SOLID_NONE );
  168. if( m_bStartDisabled )
  169. {
  170. m_bEnabled = false;
  171. }
  172. else
  173. {
  174. m_bEnabled = true;
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. // Input values
  179. //------------------------------------------------------------------------------
  180. void CSunlightShadowControl::InputSetAngles( inputdata_t &inputdata )
  181. {
  182. const char *pAngles = inputdata.value.String();
  183. QAngle angles;
  184. UTIL_StringToVector( angles.Base(), pAngles );
  185. Vector vTemp;
  186. AngleVectors( angles, &vTemp );
  187. m_shadowDirection = vTemp;
  188. }
  189. //------------------------------------------------------------------------------
  190. // Purpose : Input handlers
  191. //------------------------------------------------------------------------------
  192. void CSunlightShadowControl::InputEnable( inputdata_t &inputdata )
  193. {
  194. m_bEnabled = true;
  195. }
  196. void CSunlightShadowControl::InputDisable( inputdata_t &inputdata )
  197. {
  198. m_bEnabled = false;
  199. }
  200. void CSunlightShadowControl::InputSetTexture( inputdata_t &inputdata )
  201. {
  202. Q_strcpy( m_TextureName.GetForModify(), inputdata.value.String() );
  203. }
  204. void CSunlightShadowControl::InputSetEnableShadows( inputdata_t &inputdata )
  205. {
  206. m_bEnableShadows = inputdata.value.Bool();
  207. }
  208. void CSunlightShadowControl::InputSetLightColor( inputdata_t &inputdata )
  209. {
  210. m_LightColor = inputdata.value.Color32();
  211. }