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.

203 lines
5.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "baseentity.h"
  9. #include "sendproxy.h"
  10. #include "sun_shared.h"
  11. #include "map_utils.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. class CSun : public CBaseEntity
  15. {
  16. public:
  17. DECLARE_CLASS( CSun, CBaseEntity );
  18. DECLARE_SERVERCLASS();
  19. DECLARE_DATADESC();
  20. CSun();
  21. virtual void Activate();
  22. // Input handlers
  23. void InputTurnOn( inputdata_t &inputdata );
  24. void InputTurnOff( inputdata_t &inputdata );
  25. void InputSetColor( inputdata_t &inputdata );
  26. virtual int UpdateTransmitState();
  27. public:
  28. CNetworkVector( m_vDirection );
  29. string_t m_strMaterial;
  30. string_t m_strOverlayMaterial;
  31. int m_bUseAngles;
  32. float m_flPitch;
  33. float m_flYaw;
  34. CNetworkVar( int, m_nSize ); // Size of the main core image
  35. CNetworkVar( int, m_nOverlaySize ); // Size for the glow overlay
  36. CNetworkVar( color32, m_clrOverlay );
  37. CNetworkVar( bool, m_bOn );
  38. CNetworkVar( int, m_nMaterial );
  39. CNetworkVar( int, m_nOverlayMaterial );
  40. CNetworkVar( float, m_flHDRColorScale );
  41. };
  42. IMPLEMENT_SERVERCLASS_ST_NOBASE( CSun, DT_Sun )
  43. SendPropInt( SENDINFO(m_clrRender), 32, SPROP_UNSIGNED, SendProxy_Color32ToInt ),
  44. SendPropInt( SENDINFO(m_clrOverlay), 32, SPROP_UNSIGNED, SendProxy_Color32ToInt ),
  45. SendPropVector( SENDINFO(m_vDirection), 0, SPROP_NORMAL ),
  46. SendPropInt( SENDINFO(m_bOn), 1, SPROP_UNSIGNED ),
  47. SendPropInt( SENDINFO(m_nSize), 10, SPROP_UNSIGNED ),
  48. SendPropInt( SENDINFO(m_nOverlaySize), 10, SPROP_UNSIGNED ),
  49. SendPropInt( SENDINFO(m_nMaterial), 32, SPROP_UNSIGNED ),
  50. SendPropInt( SENDINFO(m_nOverlayMaterial), 32, SPROP_UNSIGNED ),
  51. SendPropFloat( SENDINFO_NAME( m_flHDRColorScale, HDRColorScale ), 0, SPROP_NOSCALE, 0.0f, 100.0f ),
  52. END_SEND_TABLE()
  53. LINK_ENTITY_TO_CLASS( env_sun, CSun );
  54. BEGIN_DATADESC( CSun )
  55. DEFINE_FIELD( m_vDirection, FIELD_VECTOR ),
  56. DEFINE_KEYFIELD( m_bUseAngles, FIELD_INTEGER, "use_angles" ),
  57. DEFINE_KEYFIELD( m_flPitch, FIELD_FLOAT, "pitch" ),
  58. DEFINE_KEYFIELD( m_flYaw, FIELD_FLOAT, "angle" ),
  59. DEFINE_KEYFIELD( m_nSize, FIELD_INTEGER, "size" ),
  60. DEFINE_KEYFIELD( m_clrOverlay, FIELD_COLOR32, "overlaycolor" ),
  61. DEFINE_KEYFIELD( m_nOverlaySize, FIELD_INTEGER, "overlaysize" ),
  62. DEFINE_KEYFIELD( m_strMaterial, FIELD_STRING, "material" ),
  63. DEFINE_KEYFIELD( m_strOverlayMaterial, FIELD_STRING, "overlaymaterial" ),
  64. // NOT SAVED
  65. // m_nOverlayMaterial
  66. // m_nMaterial
  67. DEFINE_FIELD( m_bOn, FIELD_BOOLEAN ),
  68. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
  69. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff ),
  70. DEFINE_INPUTFUNC( FIELD_COLOR32, "SetColor", InputSetColor ),
  71. DEFINE_KEYFIELD( m_flHDRColorScale, FIELD_FLOAT, "HDRColorScale" ),
  72. END_DATADESC()
  73. CSun::CSun()
  74. {
  75. m_vDirection.Init( 0, 0, 1 );
  76. m_bUseAngles = false;
  77. m_flPitch = 0;
  78. m_flYaw = 0;
  79. m_nSize = 16;
  80. m_bOn = true;
  81. AddEFlags( EFL_FORCE_CHECK_TRANSMIT );
  82. m_strMaterial = NULL_STRING;
  83. m_strOverlayMaterial = NULL_STRING;
  84. m_nOverlaySize = -1;
  85. }
  86. void CSun::Activate()
  87. {
  88. BaseClass::Activate();
  89. // Find our target.
  90. if ( m_bUseAngles )
  91. {
  92. SetupLightNormalFromProps( GetAbsAngles(), m_flYaw, m_flPitch, m_vDirection.GetForModify() );
  93. m_vDirection = -m_vDirection.Get();
  94. }
  95. else
  96. {
  97. CBaseEntity *pEnt = gEntList.FindEntityByName( 0, m_target );
  98. if( pEnt )
  99. {
  100. Vector vDirection = GetAbsOrigin() - pEnt->GetAbsOrigin();
  101. VectorNormalize( vDirection );
  102. m_vDirection = vDirection;
  103. }
  104. }
  105. // Default behavior
  106. if ( m_nOverlaySize == -1 )
  107. {
  108. m_nOverlaySize = m_nSize;
  109. }
  110. // Cache off our image indices
  111. if ( m_strMaterial == NULL_STRING )
  112. {
  113. m_strMaterial = AllocPooledString( "sprites/light_glow02_add_noz.vmt" );
  114. }
  115. else
  116. {
  117. const char *pExtension = V_GetFileExtension( STRING( m_strMaterial ) );
  118. if ( !pExtension )
  119. {
  120. char szFixedString[MAX_PATH];
  121. V_strncpy( szFixedString, STRING( m_strMaterial ), sizeof( szFixedString ) );
  122. V_strncat( szFixedString, ".vmt", sizeof( szFixedString ) );
  123. m_strMaterial = AllocPooledString( szFixedString );
  124. }
  125. }
  126. if ( m_strOverlayMaterial == NULL_STRING )
  127. {
  128. m_strOverlayMaterial = AllocPooledString( "sprites/light_glow02_add_noz.vmt" );
  129. }
  130. else
  131. {
  132. const char *pExtension = V_GetFileExtension( STRING( m_strOverlayMaterial ) );
  133. if ( !pExtension )
  134. {
  135. char szFixedString[MAX_PATH];
  136. V_strncpy( szFixedString, STRING( m_strOverlayMaterial ), sizeof( szFixedString ) );
  137. V_strncat( szFixedString, ".vmt", sizeof( szFixedString ) );
  138. m_strOverlayMaterial = AllocPooledString( szFixedString );
  139. }
  140. }
  141. m_nMaterial = PrecacheModel( STRING( m_strMaterial ) );
  142. m_nOverlayMaterial = PrecacheModel( STRING( m_strOverlayMaterial ) );
  143. }
  144. void CSun::InputTurnOn( inputdata_t &inputdata )
  145. {
  146. if( !m_bOn )
  147. {
  148. m_bOn = true;
  149. }
  150. }
  151. void CSun::InputTurnOff( inputdata_t &inputdata )
  152. {
  153. if ( m_bOn )
  154. {
  155. m_bOn = false;
  156. }
  157. }
  158. void CSun::InputSetColor( inputdata_t &inputdata )
  159. {
  160. m_clrRender = inputdata.value.Color32();
  161. }
  162. int CSun::UpdateTransmitState()
  163. {
  164. return SetTransmitState( FL_EDICT_ALWAYS );
  165. }