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.

476 lines
12 KiB

  1. //========= Copyright 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. class CBeam : public CBaseEntity
  39. {
  40. DECLARE_CLASS( CBeam, CBaseEntity );
  41. public:
  42. DECLARE_NETWORKCLASS();
  43. DECLARE_PREDICTABLE();
  44. #if !defined( CLIENT_DLL )
  45. DECLARE_DATADESC();
  46. #endif
  47. CBeam();
  48. virtual void SetModel( const char *szModelName );
  49. void Spawn( void );
  50. void Precache( void );
  51. #if !defined( CLIENT_DLL )
  52. int ObjectCaps( void );
  53. void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways );
  54. int UpdateTransmitState( void );
  55. int ShouldTransmit( const CCheckTransmitInfo *pInfo );
  56. #endif
  57. virtual int DrawDebugTextOverlays(void);
  58. // These functions are here to show the way beams are encoded as entities.
  59. // Encoding beams as entities simplifies their management in the client/server architecture
  60. void SetType( int type );
  61. void SetBeamFlags( int flags );
  62. void SetBeamFlag( int flag );
  63. // NOTE: Start + End Pos are specified in *relative* coordinates
  64. void SetStartPos( const Vector &pos );
  65. void SetEndPos( const Vector &pos );
  66. // This will change things so the abs position matches the requested spot
  67. void SetAbsStartPos( const Vector &pos );
  68. void SetAbsEndPos( const Vector &pos );
  69. const Vector &GetAbsStartPos( void ) const;
  70. const Vector &GetAbsEndPos( void ) const;
  71. void SetStartEntity( CBaseEntity *pEntity );
  72. void SetEndEntity( CBaseEntity *pEntity );
  73. void SetStartAttachment( int attachment );
  74. void SetEndAttachment( int attachment );
  75. void SetTexture( int spriteIndex );
  76. void SetHaloTexture( int spriteIndex );
  77. void SetHaloScale( float haloScale );
  78. void SetWidth( float width );
  79. void SetEndWidth( float endWidth );
  80. void SetFadeLength( float fadeLength );
  81. void SetNoise( float amplitude );
  82. void SetColor( int r, int g, int b );
  83. void SetBrightness( int brightness );
  84. void SetFrame( float frame );
  85. void SetScrollRate( int speed );
  86. void SetFireTime( float flFireTime );
  87. void SetFrameRate( float flFrameRate ) { m_flFrameRate = flFrameRate; }
  88. void SetMinDXLevel( int nMinDXLevel ) { m_nMinDXLevel = nMinDXLevel; }
  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. float GetWidth( void ) const;
  102. float GetEndWidth( void ) const;
  103. float GetFadeLength( void ) const;
  104. float GetNoise( void ) const;
  105. int GetBrightness( void ) const;
  106. float GetFrame( void ) const;
  107. float GetScrollRate( void ) const;
  108. float GetHDRColorScale( void ) const;
  109. void SetHDRColorScale( float flScale ) { m_flHDRColorScale = flScale; }
  110. // Call after you change start/end positions
  111. void RelinkBeam( void );
  112. void DoSparks( const Vector &start, const Vector &end );
  113. CBaseEntity *RandomTargetname( const char *szName );
  114. void BeamDamage( trace_t *ptr );
  115. // Init after BeamCreate()
  116. void BeamInit( const char *pSpriteName, float width );
  117. void PointsInit( const Vector &start, const Vector &end );
  118. void PointEntInit( const Vector &start, CBaseEntity *pEndEntity );
  119. void EntsInit( CBaseEntity *pStartEntity, CBaseEntity *pEndEntity );
  120. void LaserInit( CBaseEntity *pStartEntity, CBaseEntity *pEndEntity );
  121. void HoseInit( const Vector &start, const Vector &direction );
  122. void SplineInit( int nNumEnts, CBaseEntity** pEntList, int *attachment );
  123. // Input handlers
  124. static CBeam *BeamCreate( const char *pSpriteName, float width );
  125. static CBeam *BeamCreatePredictable( const char *module, int line, bool persist, const char *pSpriteName, float width, CBasePlayer *pOwner );
  126. void LiveForTime( float time );
  127. void BeamDamageInstant( trace_t *ptr, float damage );
  128. // Only supported in TF2 right now
  129. #if defined( INVASION_CLIENT_DLL )
  130. virtual bool ShouldPredict( void )
  131. {
  132. return true;
  133. }
  134. #endif
  135. virtual const char *GetDecalName( void ) { return "BigShot"; }
  136. #if defined( CLIENT_DLL )
  137. // IClientEntity overrides.
  138. public:
  139. virtual int DrawModel( int flags );
  140. virtual bool IsTransparent( void );
  141. virtual bool ShouldDraw();
  142. virtual bool IgnoresZBuffer( void ) const { return true; }
  143. virtual void OnDataChanged( DataUpdateType_t updateType );
  144. virtual bool OnPredictedEntityRemove( bool isbeingremoved, C_BaseEntity *predicted );
  145. // Add beam to visible entities list?
  146. virtual void AddEntity( void );
  147. virtual bool ShouldReceiveProjectedTextures( int flags )
  148. {
  149. return false;
  150. }
  151. // Beam Data Elements
  152. private:
  153. // Computes the bounding box of a beam local to the origin of the beam
  154. void ComputeBounds( Vector& mins, Vector& maxs );
  155. friend void RecvProxy_Beam_ScrollSpeed( const CRecvProxyData *pData, void *pStruct, void *pOut );
  156. friend class CViewRenderBeams;
  157. #endif
  158. protected:
  159. CNetworkVar( float, m_flFrameRate );
  160. CNetworkVar( float, m_flHDRColorScale );
  161. float m_flFireTime;
  162. float m_flDamage; // Damage per second to touchers.
  163. CNetworkVar( int, m_nNumBeamEnts );
  164. #if defined( CLIENT_DLL )
  165. pixelvis_handle_t m_queryHandleHalo;
  166. #endif
  167. private:
  168. #if !defined( CLIENT_DLL )
  169. void InputNoise( inputdata_t &inputdata );
  170. void InputWidth( inputdata_t &inputdata );
  171. void InputColorRedValue( inputdata_t &inputdata );
  172. void InputColorBlueValue( inputdata_t &inputdata );
  173. void InputColorGreenValue( inputdata_t &inputdata );
  174. #endif
  175. // Beam Data Elements
  176. CNetworkVar( int, m_nHaloIndex );
  177. CNetworkVar( int, m_nBeamType );
  178. CNetworkVar( int, m_nBeamFlags );
  179. CNetworkArray( EHANDLE, m_hAttachEntity, MAX_BEAM_ENTS );
  180. CNetworkArray( int, m_nAttachIndex, MAX_BEAM_ENTS );
  181. CNetworkVar( float, m_fWidth );
  182. CNetworkVar( float, m_fEndWidth );
  183. CNetworkVar( float, m_fFadeLength );
  184. CNetworkVar( float, m_fHaloScale );
  185. CNetworkVar( float, m_fAmplitude );
  186. CNetworkVar( float, m_fStartFrame );
  187. CNetworkVar( float, m_fSpeed );
  188. CNetworkVar( int, m_nMinDXLevel );
  189. CNetworkVar( float, m_flFrame );
  190. CNetworkVector( m_vecEndPos );
  191. EHANDLE m_hEndEntity;
  192. #if !defined( CLIENT_DLL )
  193. int m_nDissolveType;
  194. #endif
  195. public:
  196. #ifdef PORTAL
  197. CNetworkVar( bool, m_bDrawInMainRender );
  198. CNetworkVar( bool, m_bDrawInPortalRender );
  199. #endif //#ifdef PORTAL
  200. };
  201. #if !defined( CLIENT_DLL )
  202. //-----------------------------------------------------------------------------
  203. // Inline methods
  204. //-----------------------------------------------------------------------------
  205. inline int CBeam::ObjectCaps( void )
  206. {
  207. int flags = 0;
  208. if ( HasSpawnFlags( SF_BEAM_TEMPORARY ) )
  209. flags = FCAP_DONT_SAVE;
  210. return (BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | flags;
  211. }
  212. #endif
  213. inline void CBeam::SetFireTime( float flFireTime )
  214. {
  215. m_flFireTime = flFireTime;
  216. }
  217. //-----------------------------------------------------------------------------
  218. // NOTE: Start + End Pos are specified in *relative* coordinates
  219. //-----------------------------------------------------------------------------
  220. inline void CBeam::SetStartPos( const Vector &pos )
  221. {
  222. #if defined( CLIENT_DLL )
  223. SetNetworkOrigin( pos );
  224. #endif
  225. SetLocalOrigin( pos );
  226. }
  227. inline void CBeam::SetEndPos( const Vector &pos )
  228. {
  229. m_vecEndPos = pos;
  230. }
  231. // center point of beam
  232. inline const Vector &CBeam::WorldSpaceCenter( void ) const
  233. {
  234. Vector &vecResult = AllocTempVector();
  235. VectorAdd( GetAbsStartPos(), GetAbsEndPos(), vecResult );
  236. vecResult *= 0.5f;
  237. return vecResult;
  238. }
  239. inline void CBeam::SetStartAttachment( int attachment )
  240. {
  241. Assert( (attachment & ~ATTACHMENT_INDEX_MASK) == 0 );
  242. m_nAttachIndex.Set( 0, attachment );
  243. }
  244. inline void CBeam::SetEndAttachment( int attachment )
  245. {
  246. Assert( (attachment & ~ATTACHMENT_INDEX_MASK) == 0 );
  247. m_nAttachIndex.Set( m_nNumBeamEnts-1, attachment );
  248. }
  249. inline void CBeam::SetTexture( int spriteIndex )
  250. {
  251. SetModelIndex( spriteIndex );
  252. }
  253. inline void CBeam::SetHaloTexture( int spriteIndex )
  254. {
  255. m_nHaloIndex = spriteIndex;
  256. }
  257. inline void CBeam::SetHaloScale( float haloScale )
  258. {
  259. m_fHaloScale = haloScale;
  260. }
  261. inline void CBeam::SetWidth( float width )
  262. {
  263. Assert( width <= MAX_BEAM_WIDTH );
  264. m_fWidth = MIN( MAX_BEAM_WIDTH, width );
  265. }
  266. inline void CBeam::SetEndWidth( float endWidth )
  267. {
  268. Assert( endWidth <= MAX_BEAM_WIDTH );
  269. m_fEndWidth = MIN( MAX_BEAM_WIDTH, endWidth );
  270. }
  271. inline void CBeam::SetFadeLength( float fadeLength )
  272. {
  273. m_fFadeLength = fadeLength;
  274. }
  275. inline void CBeam::SetNoise( float amplitude )
  276. {
  277. m_fAmplitude = amplitude;
  278. }
  279. inline void CBeam::SetColor( int r, int g, int b )
  280. {
  281. SetRenderColor( r, g, b, GetRenderColor().a );
  282. }
  283. inline void CBeam::SetBrightness( int brightness )
  284. {
  285. SetRenderColorA( brightness );
  286. }
  287. inline void CBeam::SetFrame( float frame )
  288. {
  289. m_fStartFrame = frame;
  290. }
  291. inline void CBeam::SetScrollRate( int speed )
  292. {
  293. m_fSpeed = speed;
  294. }
  295. inline CBaseEntity* CBeam::GetStartEntityPtr( void ) const
  296. {
  297. return m_hAttachEntity[0].Get();
  298. }
  299. inline int CBeam::GetStartEntity( void ) const
  300. {
  301. CBaseEntity *pEntity = m_hAttachEntity[0].Get();
  302. return pEntity ? pEntity->entindex() : 0;
  303. }
  304. inline CBaseEntity* CBeam::GetEndEntityPtr( void ) const
  305. {
  306. return m_hAttachEntity[1].Get();
  307. }
  308. inline int CBeam::GetEndEntity( void ) const
  309. {
  310. CBaseEntity *pEntity = m_hAttachEntity[m_nNumBeamEnts-1].Get();
  311. return pEntity ? pEntity->entindex() : 0;
  312. }
  313. inline int CBeam::GetStartAttachment() const
  314. {
  315. return m_nAttachIndex[0] & ATTACHMENT_INDEX_MASK;
  316. }
  317. inline int CBeam::GetEndAttachment() const
  318. {
  319. return m_nAttachIndex[m_nNumBeamEnts-1] & ATTACHMENT_INDEX_MASK;
  320. }
  321. inline int CBeam::GetTexture( void )
  322. {
  323. return GetModelIndex();
  324. }
  325. inline float CBeam::GetWidth( void ) const
  326. {
  327. return m_fWidth;
  328. }
  329. inline float CBeam::GetEndWidth( void ) const
  330. {
  331. return m_fEndWidth;
  332. }
  333. inline float CBeam::GetFadeLength( void ) const
  334. {
  335. return m_fFadeLength;
  336. }
  337. inline float CBeam::GetNoise( void ) const
  338. {
  339. return m_fAmplitude;
  340. }
  341. inline int CBeam::GetBrightness( void ) const
  342. {
  343. return GetRenderColor().a;
  344. }
  345. inline float CBeam::GetFrame( void ) const
  346. {
  347. return m_fStartFrame;
  348. }
  349. inline float CBeam::GetScrollRate( void ) const
  350. {
  351. return m_fSpeed;
  352. }
  353. inline float CBeam::GetHDRColorScale( void ) const
  354. {
  355. return m_flHDRColorScale;
  356. }
  357. inline void CBeam::LiveForTime( float time )
  358. {
  359. SetThink(&CBeam::SUB_Remove);
  360. SetNextThink( gpGlobals->curtime + time );
  361. }
  362. inline void CBeam::BeamDamageInstant( trace_t *ptr, float damage )
  363. {
  364. m_flDamage = damage;
  365. m_flFireTime = gpGlobals->curtime - 1;
  366. BeamDamage(ptr);
  367. }
  368. bool IsStaticPointEntity( CBaseEntity *pEnt );
  369. // Macro to wrap creation
  370. #define BEAM_CREATE_PREDICTABLE( name, width, player ) \
  371. CBeam::BeamCreatePredictable( __FILE__, __LINE__, false, name, width, player )
  372. #define BEAM_CREATE_PREDICTABLE_PERSIST( name, width, player ) \
  373. CBeam::BeamCreatePredictable( __FILE__, __LINE__, true, name, width, player )
  374. // Start/End Entity is encoded as 12 bits of entity index, and 4 bits of attachment (4:12)
  375. #define BEAMENT_ENTITY(x) ((x)&0xFFF)
  376. #define BEAMENT_ATTACHMENT(x) (((x)>>12)&0xF)
  377. // Beam types, encoded as a byte
  378. enum
  379. {
  380. BEAM_POINTS = 0,
  381. BEAM_ENTPOINT,
  382. BEAM_ENTS,
  383. BEAM_HOSE,
  384. BEAM_SPLINE,
  385. BEAM_LASER,
  386. NUM_BEAM_TYPES
  387. };
  388. #endif // BEAM_H