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.

270 lines
8.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Entity to control screen overlays on a player
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "shareddefs.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. #define ENV_PROJECTEDTEXTURE_STARTON (1<<0)
  11. //-----------------------------------------------------------------------------
  12. // Purpose:
  13. //-----------------------------------------------------------------------------
  14. class CEnvProjectedTexture : public CPointEntity
  15. {
  16. DECLARE_CLASS( CEnvProjectedTexture, CPointEntity );
  17. public:
  18. DECLARE_DATADESC();
  19. DECLARE_SERVERCLASS();
  20. CEnvProjectedTexture();
  21. bool KeyValue( const char *szKeyName, const char *szValue );
  22. // Always transmit to clients
  23. virtual int UpdateTransmitState();
  24. virtual void Activate( void );
  25. void InputTurnOn( inputdata_t &inputdata );
  26. void InputTurnOff( inputdata_t &inputdata );
  27. void InputSetFOV( inputdata_t &inputdata );
  28. void InputSetTarget( inputdata_t &inputdata );
  29. void InputSetCameraSpace( inputdata_t &inputdata );
  30. void InputSetLightOnlyTarget( inputdata_t &inputdata );
  31. void InputSetLightWorld( inputdata_t &inputdata );
  32. void InputSetEnableShadows( inputdata_t &inputdata );
  33. // void InputSetLightColor( inputdata_t &inputdata );
  34. void InputSetSpotlightTexture( inputdata_t &inputdata );
  35. void InputSetAmbient( inputdata_t &inputdata );
  36. void InitialThink( void );
  37. CNetworkHandle( CBaseEntity, m_hTargetEntity );
  38. private:
  39. CNetworkVar( bool, m_bState );
  40. CNetworkVar( float, m_flLightFOV );
  41. CNetworkVar( bool, m_bEnableShadows );
  42. CNetworkVar( bool, m_bLightOnlyTarget );
  43. CNetworkVar( bool, m_bLightWorld );
  44. CNetworkVar( bool, m_bCameraSpace );
  45. CNetworkVector( m_LinearFloatLightColor );
  46. CNetworkVar( float, m_flAmbient );
  47. CNetworkString( m_SpotlightTextureName, MAX_PATH );
  48. CNetworkVar( int, m_nSpotlightTextureFrame );
  49. CNetworkVar( float, m_flNearZ );
  50. CNetworkVar( float, m_flFarZ );
  51. CNetworkVar( int, m_nShadowQuality );
  52. };
  53. LINK_ENTITY_TO_CLASS( env_projectedtexture, CEnvProjectedTexture );
  54. BEGIN_DATADESC( CEnvProjectedTexture )
  55. DEFINE_FIELD( m_hTargetEntity, FIELD_EHANDLE ),
  56. DEFINE_FIELD( m_bState, FIELD_BOOLEAN ),
  57. DEFINE_KEYFIELD( m_flLightFOV, FIELD_FLOAT, "lightfov" ),
  58. DEFINE_KEYFIELD( m_bEnableShadows, FIELD_BOOLEAN, "enableshadows" ),
  59. DEFINE_KEYFIELD( m_bLightOnlyTarget, FIELD_BOOLEAN, "lightonlytarget" ),
  60. DEFINE_KEYFIELD( m_bLightWorld, FIELD_BOOLEAN, "lightworld" ),
  61. DEFINE_KEYFIELD( m_bCameraSpace, FIELD_BOOLEAN, "cameraspace" ),
  62. DEFINE_KEYFIELD( m_flAmbient, FIELD_FLOAT, "ambient" ),
  63. DEFINE_AUTO_ARRAY_KEYFIELD( m_SpotlightTextureName, FIELD_CHARACTER, "texturename" ),
  64. DEFINE_KEYFIELD( m_nSpotlightTextureFrame, FIELD_INTEGER, "textureframe" ),
  65. DEFINE_KEYFIELD( m_flNearZ, FIELD_FLOAT, "nearz" ),
  66. DEFINE_KEYFIELD( m_flFarZ, FIELD_FLOAT, "farz" ),
  67. DEFINE_KEYFIELD( m_nShadowQuality, FIELD_INTEGER, "shadowquality" ),
  68. DEFINE_FIELD( m_LinearFloatLightColor, FIELD_VECTOR ),
  69. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
  70. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff ),
  71. DEFINE_INPUTFUNC( FIELD_FLOAT, "FOV", InputSetFOV ),
  72. DEFINE_INPUTFUNC( FIELD_EHANDLE, "Target", InputSetTarget ),
  73. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "CameraSpace", InputSetCameraSpace ),
  74. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "LightOnlyTarget", InputSetLightOnlyTarget ),
  75. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "LightWorld", InputSetLightWorld ),
  76. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "EnableShadows", InputSetEnableShadows ),
  77. // this is broken . . need to be able to set color and intensity like light_dynamic
  78. // DEFINE_INPUTFUNC( FIELD_COLOR32, "LightColor", InputSetLightColor ),
  79. DEFINE_INPUTFUNC( FIELD_FLOAT, "Ambient", InputSetAmbient ),
  80. DEFINE_INPUTFUNC( FIELD_STRING, "SpotlightTexture", InputSetSpotlightTexture ),
  81. DEFINE_THINKFUNC( InitialThink ),
  82. END_DATADESC()
  83. IMPLEMENT_SERVERCLASS_ST( CEnvProjectedTexture, DT_EnvProjectedTexture )
  84. SendPropEHandle( SENDINFO( m_hTargetEntity ) ),
  85. SendPropBool( SENDINFO( m_bState ) ),
  86. SendPropFloat( SENDINFO( m_flLightFOV ) ),
  87. SendPropBool( SENDINFO( m_bEnableShadows ) ),
  88. SendPropBool( SENDINFO( m_bLightOnlyTarget ) ),
  89. SendPropBool( SENDINFO( m_bLightWorld ) ),
  90. SendPropBool( SENDINFO( m_bCameraSpace ) ),
  91. SendPropVector( SENDINFO( m_LinearFloatLightColor ) ),
  92. SendPropFloat( SENDINFO( m_flAmbient ) ),
  93. SendPropString( SENDINFO( m_SpotlightTextureName ) ),
  94. SendPropInt( SENDINFO( m_nSpotlightTextureFrame ) ),
  95. SendPropFloat( SENDINFO( m_flNearZ ), 16, SPROP_ROUNDDOWN, 0.0f, 500.0f ),
  96. SendPropFloat( SENDINFO( m_flFarZ ), 18, SPROP_ROUNDDOWN, 0.0f, 1500.0f ),
  97. SendPropInt( SENDINFO( m_nShadowQuality ), 1, SPROP_UNSIGNED ), // Just one bit for now
  98. END_SEND_TABLE()
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. CEnvProjectedTexture::CEnvProjectedTexture( void )
  103. {
  104. m_bState = true;
  105. m_flLightFOV = 45.0f;
  106. m_bEnableShadows = false;
  107. m_bLightOnlyTarget = false;
  108. m_bLightWorld = true;
  109. m_bCameraSpace = false;
  110. // if ( g_pHardwareConfig->SupportsBorderColor() )
  111. #if defined( _X360 )
  112. Q_strcpy( m_SpotlightTextureName.GetForModify(), "effects/flashlight_border" );
  113. #else
  114. Q_strcpy( m_SpotlightTextureName.GetForModify(), "effects/flashlight001" );
  115. #endif
  116. m_nSpotlightTextureFrame = 0;
  117. m_LinearFloatLightColor.Init( 1.0f, 1.0f, 1.0f );
  118. m_flAmbient = 0.0f;
  119. m_flNearZ = 4.0f;
  120. m_flFarZ = 750.0f;
  121. m_nShadowQuality = 0;
  122. }
  123. void UTIL_ColorStringToLinearFloatColor( Vector &color, const char *pString )
  124. {
  125. float tmp[4];
  126. UTIL_StringToFloatArray( tmp, 4, pString );
  127. if( tmp[3] <= 0.0f )
  128. {
  129. tmp[3] = 255.0f;
  130. }
  131. tmp[3] *= ( 1.0f / 255.0f );
  132. color.x = GammaToLinear( tmp[0] * ( 1.0f / 255.0f ) ) * tmp[3];
  133. color.y = GammaToLinear( tmp[1] * ( 1.0f / 255.0f ) ) * tmp[3];
  134. color.z = GammaToLinear( tmp[2] * ( 1.0f / 255.0f ) ) * tmp[3];
  135. }
  136. bool CEnvProjectedTexture::KeyValue( const char *szKeyName, const char *szValue )
  137. {
  138. if ( FStrEq( szKeyName, "lightcolor" ) )
  139. {
  140. Vector tmp;
  141. UTIL_ColorStringToLinearFloatColor( tmp, szValue );
  142. m_LinearFloatLightColor = tmp;
  143. }
  144. else
  145. {
  146. return BaseClass::KeyValue( szKeyName, szValue );
  147. }
  148. return true;
  149. }
  150. void CEnvProjectedTexture::InputTurnOn( inputdata_t &inputdata )
  151. {
  152. m_bState = true;
  153. }
  154. void CEnvProjectedTexture::InputTurnOff( inputdata_t &inputdata )
  155. {
  156. m_bState = false;
  157. }
  158. void CEnvProjectedTexture::InputSetFOV( inputdata_t &inputdata )
  159. {
  160. m_flLightFOV = inputdata.value.Float();
  161. }
  162. void CEnvProjectedTexture::InputSetTarget( inputdata_t &inputdata )
  163. {
  164. m_hTargetEntity = inputdata.value.Entity();
  165. }
  166. void CEnvProjectedTexture::InputSetCameraSpace( inputdata_t &inputdata )
  167. {
  168. m_bCameraSpace = inputdata.value.Bool();
  169. }
  170. void CEnvProjectedTexture::InputSetLightOnlyTarget( inputdata_t &inputdata )
  171. {
  172. m_bLightOnlyTarget = inputdata.value.Bool();
  173. }
  174. void CEnvProjectedTexture::InputSetLightWorld( inputdata_t &inputdata )
  175. {
  176. m_bLightWorld = inputdata.value.Bool();
  177. }
  178. void CEnvProjectedTexture::InputSetEnableShadows( inputdata_t &inputdata )
  179. {
  180. m_bEnableShadows = inputdata.value.Bool();
  181. }
  182. //void CEnvProjectedTexture::InputSetLightColor( inputdata_t &inputdata )
  183. //{
  184. // m_cLightColor = inputdata.value.Color32();
  185. //}
  186. void CEnvProjectedTexture::InputSetAmbient( inputdata_t &inputdata )
  187. {
  188. m_flAmbient = inputdata.value.Float();
  189. }
  190. void CEnvProjectedTexture::InputSetSpotlightTexture( inputdata_t &inputdata )
  191. {
  192. Q_strcpy( m_SpotlightTextureName.GetForModify(), inputdata.value.String() );
  193. }
  194. void CEnvProjectedTexture::Activate( void )
  195. {
  196. if ( GetSpawnFlags() & ENV_PROJECTEDTEXTURE_STARTON )
  197. {
  198. m_bState = true;
  199. }
  200. SetThink( &CEnvProjectedTexture::InitialThink );
  201. SetNextThink( gpGlobals->curtime + 0.1f );
  202. BaseClass::Activate();
  203. }
  204. void CEnvProjectedTexture::InitialThink( void )
  205. {
  206. m_hTargetEntity = gEntList.FindEntityByName( NULL, m_target );
  207. }
  208. int CEnvProjectedTexture::UpdateTransmitState()
  209. {
  210. return SetTransmitState( FL_EDICT_ALWAYS );
  211. }
  212. // Console command for creating env_projectedtexture entities
  213. void CC_CreateFlashlight( const CCommand &args )
  214. {
  215. CBasePlayer *pPlayer = UTIL_GetCommandClient();
  216. if( !pPlayer )
  217. return;
  218. QAngle angles = pPlayer->EyeAngles();
  219. Vector origin = pPlayer->EyePosition();
  220. CEnvProjectedTexture *pFlashlight = dynamic_cast< CEnvProjectedTexture * >( CreateEntityByName("env_projectedtexture") );
  221. if( args.ArgC() > 1 )
  222. {
  223. pFlashlight->SetName( AllocPooledString( args[1] ) );
  224. }
  225. pFlashlight->Teleport( &origin, &angles, NULL );
  226. }
  227. static ConCommand create_flashlight("create_flashlight", CC_CreateFlashlight, 0, FCVAR_CHEAT);