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.

489 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef BEAM_H
  8. #define BEAM_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "baseentity_shared.h"
  13. #include "baseplayer_shared.h"
  14. #if !defined( CLIENT_DLL )
  15. #include "entityoutput.h"
  16. #endif
  17. #include "beam_flags.h"
  18. #define MAX_BEAM_WIDTH 102.3f
  19. #define MAX_BEAM_SCROLLSPEED 100.0f
  20. #define MAX_BEAM_NOISEAMPLITUDE 64
  21. #define SF_BEAM_STARTON 0x0001
  22. #define SF_BEAM_TOGGLE 0x0002
  23. #define SF_BEAM_RANDOM 0x0004
  24. #define SF_BEAM_RING 0x0008
  25. #define SF_BEAM_SPARKSTART 0x0010
  26. #define SF_BEAM_SPARKEND 0x0020
  27. #define SF_BEAM_DECALS 0x0040
  28. #define SF_BEAM_SHADEIN 0x0080
  29. #define SF_BEAM_SHADEOUT 0x0100
  30. #define SF_BEAM_TAPEROUT 0x0200 // Tapers to zero
  31. #define SF_BEAM_TEMPORARY 0x8000
  32. #define ATTACHMENT_INDEX_BITS 5
  33. #define ATTACHMENT_INDEX_MASK ((1 << ATTACHMENT_INDEX_BITS) - 1)
  34. #if defined( CLIENT_DLL )
  35. #define CBeam C_Beam
  36. #include "c_pixel_visibility.h"
  37. #endif
  38. // I've seen CBeams glitter in the dark near Tannhauser gate...
  39. class CBeam : public CBaseEntity
  40. {
  41. DECLARE_CLASS( CBeam, CBaseEntity );
  42. public:
  43. DECLARE_NETWORKCLASS();
  44. DECLARE_PREDICTABLE();
  45. #if !defined( CLIENT_DLL )
  46. DECLARE_DATADESC();
  47. #endif
  48. CBeam();
  49. virtual void SetModel( const char *szModelName );
  50. void Spawn( void );
  51. void Precache( void );
  52. #if !defined( CLIENT_DLL )
  53. int ObjectCaps( void );
  54. void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways );
  55. int UpdateTransmitState( void );
  56. int ShouldTransmit( const CCheckTransmitInfo *pInfo );
  57. #endif
  58. virtual int DrawDebugTextOverlays(void);
  59. // These functions are here to show the way beams are encoded as entities.
  60. // Encoding beams as entities simplifies their management in the client/server architecture
  61. void SetType( int type );
  62. void SetBeamFlags( int flags );
  63. void SetBeamFlag( int flag );
  64. // NOTE: Start + End Pos are specified in *relative* coordinates
  65. void SetStartPos( const Vector &pos );
  66. void SetEndPos( const Vector &pos );
  67. // This will change things so the abs position matches the requested spot
  68. void SetAbsStartPos( const Vector &pos );
  69. void SetAbsEndPos( const Vector &pos );
  70. const Vector &GetAbsStartPos( void ) const;
  71. const Vector &GetAbsEndPos( void ) const;
  72. void SetStartEntity( CBaseEntity *pEntity );
  73. void SetEndEntity( CBaseEntity *pEntity );
  74. void SetStartAttachment( int attachment );
  75. void SetEndAttachment( int attachment );
  76. void SetTexture( int spriteIndex );
  77. void SetHaloTexture( int spriteIndex );
  78. void SetHaloScale( float haloScale );
  79. void SetWidth( float width );
  80. void SetEndWidth( float endWidth );
  81. void SetFadeLength( float fadeLength );
  82. void SetNoise( float amplitude );
  83. void SetColor( int r, int g, int b );
  84. void SetBrightness( int brightness );
  85. void SetFrame( float frame );
  86. void SetScrollRate( int speed );
  87. void SetFireTime( float flFireTime );
  88. void SetFrameRate( float flFrameRate ) { m_flFrameRate = flFrameRate; }
  89. void TurnOn( void );
  90. void TurnOff( void );
  91. int GetType( void ) const;
  92. int GetBeamFlags( void ) const;
  93. CBaseEntity* GetStartEntityPtr( void ) const;
  94. int GetStartEntity( void ) const;
  95. CBaseEntity* GetEndEntityPtr( void ) const;
  96. int GetEndEntity( void ) const;
  97. int GetStartAttachment() const;
  98. int GetEndAttachment() const;
  99. virtual const Vector &WorldSpaceCenter( void ) const;
  100. int GetTexture( void );
  101. int GetHaloTexture( void ) const;
  102. float GetWidth( void ) const;
  103. float GetEndWidth( void ) const;
  104. float GetFadeLength( void ) const;
  105. float GetNoise( void ) const;
  106. float GetHaloScale( void ) const;
  107. int GetBrightness( void ) const;
  108. float GetFrame( void ) const;
  109. float GetScrollRate( void ) const;
  110. float GetHDRColorScale( void ) const;
  111. void SetHDRColorScale( float flScale ) { m_flHDRColorScale = flScale; }
  112. // Call after you change start/end positions
  113. void RelinkBeam( void );
  114. void DoSparks( const Vector &start, const Vector &end );
  115. CBaseEntity *RandomTargetname( const char *szName );
  116. void BeamDamage( trace_t *ptr );
  117. // Init after BeamCreate()
  118. void BeamInit( const char *pSpriteName, float width );
  119. void PointsInit( const Vector &start, const Vector &end );
  120. void PointEntInit( const Vector &start, CBaseEntity *pEndEntity );
  121. void EntsInit( CBaseEntity *pStartEntity, CBaseEntity *pEndEntity );
  122. void LaserInit( CBaseEntity *pStartEntity, CBaseEntity *pEndEntity );
  123. void HoseInit( const Vector &start, const Vector &direction );
  124. void SplineInit( int nNumEnts, CBaseEntity** pEntList, int *attachment );
  125. // Input handlers
  126. static CBeam *BeamCreate( const char *pSpriteName, float width );
  127. static CBeam *BeamCreatePredictable( const char *module, int line, bool persist, const char *pSpriteName, float width, CBasePlayer *pOwner );
  128. void LiveForTime( float time );
  129. void BeamDamageInstant( trace_t *ptr, float damage );
  130. virtual const char *GetDecalName( void ) { return "BigShot"; }
  131. // specify whether the beam should always go all the way to
  132. // the end point, or clip against geometry, or clip against
  133. // geometry and NPCs. This is only used by env_beams at present, but
  134. // need to be in this CBeam because of the way it affects drawing.
  135. enum BeamClipStyle_t
  136. {
  137. kNOCLIP = 0, // don't clip (default)
  138. kGEOCLIP = 1,
  139. kMODELCLIP = 2,
  140. kBEAMCLIPSTYLE_NUMBITS = 2, //< number of bits needed to represent this object
  141. };
  142. inline BeamClipStyle_t GetClipStyle() const { return m_nClipStyle; }
  143. #if defined( CLIENT_DLL )
  144. // IClientEntity overrides.
  145. public:
  146. virtual int DrawModel( int flags, const RenderableInstance_t &instance );
  147. virtual RenderableTranslucencyType_t ComputeTranslucencyType();
  148. virtual void OnDataChanged( DataUpdateType_t updateType );
  149. virtual bool OnPredictedEntityRemove( bool isbeingremoved, C_BaseEntity *predicted );
  150. // Add beam to visible entities list?
  151. virtual bool Simulate( void );
  152. virtual bool ShouldReceiveProjectedTextures( int flags )
  153. {
  154. return false;
  155. }
  156. virtual void GetToolRecordingState( KeyValues *msg );
  157. void RestoreToToolRecordedState( KeyValues *pKV );
  158. // Beam Data Elements
  159. private:
  160. // Computes the bounding box of a beam local to the origin of the beam
  161. void ComputeBounds( Vector& mins, Vector& maxs );
  162. friend void RecvProxy_Beam_ScrollSpeed( const CRecvProxyData *pData, void *pStruct, void *pOut );
  163. friend class CViewRenderBeams;
  164. #endif
  165. protected:
  166. CNetworkVar( float, m_flFrameRate );
  167. CNetworkVar( float, m_flHDRColorScale );
  168. float m_flFireTime;
  169. float m_flDamage; // Damage per second to touchers.
  170. CNetworkVar( int, m_nNumBeamEnts );
  171. #if defined( CLIENT_DLL )
  172. pixelvis_handle_t m_queryHandleHalo;
  173. #endif
  174. private:
  175. #if !defined( CLIENT_DLL )
  176. void InputNoise( inputdata_t &inputdata );
  177. void InputWidth( inputdata_t &inputdata );
  178. void InputColorRedValue( inputdata_t &inputdata );
  179. void InputColorBlueValue( inputdata_t &inputdata );
  180. void InputColorGreenValue( inputdata_t &inputdata );
  181. #endif
  182. // Beam Data Elements
  183. CNetworkVar( int, m_nHaloIndex );
  184. CNetworkVar( int, m_nBeamType );
  185. CNetworkVar( int, m_nBeamFlags );
  186. CNetworkArray( EHANDLE, m_hAttachEntity, MAX_BEAM_ENTS );
  187. CNetworkArray( int, m_nAttachIndex, MAX_BEAM_ENTS );
  188. CNetworkVar( float, m_fWidth );
  189. CNetworkVar( float, m_fEndWidth );
  190. CNetworkVar( float, m_fFadeLength );
  191. CNetworkVar( float, m_fHaloScale );
  192. CNetworkVar( float, m_fAmplitude );
  193. CNetworkVar( float, m_fStartFrame );
  194. CNetworkVar( float, m_fSpeed );
  195. CNetworkVar( float, m_flFrame );
  196. CNetworkVar( BeamClipStyle_t, m_nClipStyle );
  197. CNetworkVector( m_vecEndPos );
  198. EHANDLE m_hEndEntity;
  199. #if !defined( CLIENT_DLL )
  200. int m_nDissolveType;
  201. #endif
  202. };
  203. #if !defined( CLIENT_DLL )
  204. //-----------------------------------------------------------------------------
  205. // Inline methods
  206. //-----------------------------------------------------------------------------
  207. inline int CBeam::ObjectCaps( void )
  208. {
  209. int flags = 0;
  210. if ( HasSpawnFlags( SF_BEAM_TEMPORARY ) )
  211. flags = FCAP_DONT_SAVE;
  212. return (BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | flags;
  213. }
  214. #endif
  215. inline void CBeam::SetFireTime( float flFireTime )
  216. {
  217. m_flFireTime = flFireTime;
  218. }
  219. //-----------------------------------------------------------------------------
  220. // NOTE: Start + End Pos are specified in *relative* coordinates
  221. //-----------------------------------------------------------------------------
  222. inline void CBeam::SetStartPos( const Vector &pos )
  223. {
  224. #if defined( CLIENT_DLL )
  225. SetNetworkOrigin( pos );
  226. #endif
  227. SetLocalOrigin( pos );
  228. }
  229. inline void CBeam::SetEndPos( const Vector &pos )
  230. {
  231. m_vecEndPos = pos;
  232. }
  233. // center point of beam
  234. inline const Vector &CBeam::WorldSpaceCenter( void ) const
  235. {
  236. Vector &vecResult = AllocTempVector();
  237. VectorAdd( GetAbsStartPos(), GetAbsEndPos(), vecResult );
  238. vecResult *= 0.5f;
  239. return vecResult;
  240. }
  241. inline void CBeam::SetStartAttachment( int attachment )
  242. {
  243. Assert( (attachment & ~ATTACHMENT_INDEX_MASK) == 0 );
  244. m_nAttachIndex.Set( 0, attachment );
  245. }
  246. inline void CBeam::SetEndAttachment( int attachment )
  247. {
  248. Assert( (attachment & ~ATTACHMENT_INDEX_MASK) == 0 );
  249. m_nAttachIndex.Set( m_nNumBeamEnts-1, attachment );
  250. }
  251. inline void CBeam::SetTexture( int spriteIndex )
  252. {
  253. SetModelIndex( spriteIndex );
  254. }
  255. inline void CBeam::SetHaloTexture( int spriteIndex )
  256. {
  257. m_nHaloIndex = spriteIndex;
  258. }
  259. inline void CBeam::SetHaloScale( float haloScale )
  260. {
  261. m_fHaloScale = haloScale;
  262. }
  263. inline void CBeam::SetWidth( float width )
  264. {
  265. Assert( width <= MAX_BEAM_WIDTH );
  266. m_fWidth = MIN( MAX_BEAM_WIDTH, width );
  267. }
  268. inline void CBeam::SetEndWidth( float endWidth )
  269. {
  270. Assert( endWidth <= MAX_BEAM_WIDTH );
  271. m_fEndWidth = MIN( MAX_BEAM_WIDTH, endWidth );
  272. }
  273. inline void CBeam::SetFadeLength( float fadeLength )
  274. {
  275. m_fFadeLength = fadeLength;
  276. }
  277. inline void CBeam::SetNoise( float amplitude )
  278. {
  279. m_fAmplitude = amplitude;
  280. }
  281. inline void CBeam::SetColor( int r, int g, int b )
  282. {
  283. SetRenderColor( r, g, b );
  284. }
  285. inline void CBeam::SetBrightness( int brightness )
  286. {
  287. SetRenderAlpha( brightness );
  288. }
  289. inline void CBeam::SetFrame( float frame )
  290. {
  291. m_fStartFrame = frame;
  292. }
  293. inline void CBeam::SetScrollRate( int speed )
  294. {
  295. m_fSpeed = speed;
  296. }
  297. inline CBaseEntity* CBeam::GetStartEntityPtr( void ) const
  298. {
  299. return m_hAttachEntity[0].Get();
  300. }
  301. inline int CBeam::GetStartEntity( void ) const
  302. {
  303. CBaseEntity *pEntity = m_hAttachEntity[0].Get();
  304. return pEntity ? pEntity->entindex() : 0;
  305. }
  306. inline CBaseEntity* CBeam::GetEndEntityPtr( void ) const
  307. {
  308. return m_hAttachEntity[1].Get();
  309. }
  310. inline int CBeam::GetEndEntity( void ) const
  311. {
  312. CBaseEntity *pEntity = m_hAttachEntity[m_nNumBeamEnts-1].Get();
  313. return pEntity ? pEntity->entindex() : 0;
  314. }
  315. inline int CBeam::GetStartAttachment() const
  316. {
  317. return m_nAttachIndex[0] & ATTACHMENT_INDEX_MASK;
  318. }
  319. inline int CBeam::GetEndAttachment() const
  320. {
  321. return m_nAttachIndex[m_nNumBeamEnts-1] & ATTACHMENT_INDEX_MASK;
  322. }
  323. inline int CBeam::GetTexture( void )
  324. {
  325. return GetModelIndex();
  326. }
  327. inline int CBeam::GetHaloTexture( void ) const
  328. {
  329. return m_nHaloIndex;
  330. }
  331. inline float CBeam::GetWidth( void ) const
  332. {
  333. return m_fWidth;
  334. }
  335. inline float CBeam::GetEndWidth( void ) const
  336. {
  337. return m_fEndWidth;
  338. }
  339. inline float CBeam::GetFadeLength( void ) const
  340. {
  341. return m_fFadeLength;
  342. }
  343. inline float CBeam::GetNoise( void ) const
  344. {
  345. return m_fAmplitude;
  346. }
  347. inline float CBeam::GetHaloScale( void ) const
  348. {
  349. return m_fHaloScale;
  350. }
  351. inline int CBeam::GetBrightness( void ) const
  352. {
  353. return GetRenderAlpha();
  354. }
  355. inline float CBeam::GetFrame( void ) const
  356. {
  357. return m_fStartFrame;
  358. }
  359. inline float CBeam::GetScrollRate( void ) const
  360. {
  361. return m_fSpeed;
  362. }
  363. inline float CBeam::GetHDRColorScale( void ) const
  364. {
  365. return m_flHDRColorScale;
  366. }
  367. inline void CBeam::LiveForTime( float time )
  368. {
  369. SetThink(&CBeam::SUB_Remove);
  370. SetNextThink( gpGlobals->curtime + time );
  371. }
  372. inline void CBeam::BeamDamageInstant( trace_t *ptr, float damage )
  373. {
  374. m_flDamage = damage;
  375. m_flFireTime = gpGlobals->curtime - 1;
  376. BeamDamage(ptr);
  377. }
  378. bool IsStaticPointEntity( CBaseEntity *pEnt );
  379. // Macro to wrap creation
  380. #define BEAM_CREATE_PREDICTABLE( name, width, player ) \
  381. CBeam::BeamCreatePredictable( __FILE__, __LINE__, false, name, width, player )
  382. #define BEAM_CREATE_PREDICTABLE_PERSIST( name, width, player ) \
  383. CBeam::BeamCreatePredictable( __FILE__, __LINE__, true, name, width, player )
  384. // Start/End Entity is encoded as 12 bits of entity index, and 4 bits of attachment (4:12)
  385. #define BEAMENT_ENTITY(x) ((x)&0xFFF)
  386. #define BEAMENT_ATTACHMENT(x) (((x)>>12)&0xF)
  387. // Beam types, encoded as a byte
  388. enum
  389. {
  390. BEAM_POINTS = 0,
  391. BEAM_ENTPOINT,
  392. BEAM_ENTS,
  393. BEAM_HOSE,
  394. BEAM_SPLINE,
  395. BEAM_LASER,
  396. NUM_BEAM_TYPES
  397. };
  398. #endif // BEAM_H