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.

258 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef SERVERNETWORKPROPERTY_H
  8. #define SERVERNETWORKPROPERTY_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "iservernetworkable.h"
  13. #include "server_class.h"
  14. #include "edict.h"
  15. #include "timedeventmgr.h"
  16. //
  17. // Lightweight base class for networkable data on the server.
  18. //
  19. class CServerNetworkProperty : public IServerNetworkable, public IEventRegisterCallback
  20. {
  21. public:
  22. DECLARE_CLASS_NOBASE( CServerNetworkProperty );
  23. DECLARE_DATADESC();
  24. public:
  25. CServerNetworkProperty();
  26. virtual ~CServerNetworkProperty();
  27. public:
  28. // IServerNetworkable implementation.
  29. virtual IHandleEntity *GetEntityHandle( );
  30. virtual edict_t *GetEdict() const;
  31. virtual CBaseNetworkable* GetBaseNetworkable();
  32. virtual CBaseEntity* GetBaseEntity();
  33. virtual ServerClass* GetServerClass();
  34. virtual const char* GetClassName() const;
  35. virtual void Release();
  36. virtual int AreaNum() const;
  37. virtual PVSInfo_t* GetPVSInfo();
  38. public:
  39. // Other public methods
  40. void Init( CBaseEntity *pEntity );
  41. void AttachEdict( edict_t *pRequiredEdict = NULL );
  42. // Methods to get the entindex + edict
  43. int entindex() const;
  44. edict_t *edict();
  45. const edict_t *edict() const;
  46. // Sets the edict pointer (for swapping edicts)
  47. void SetEdict( edict_t *pEdict );
  48. // All these functions call through to CNetStateMgr.
  49. // See CNetStateMgr for details about these functions.
  50. void NetworkStateForceUpdate();
  51. void NetworkStateChanged();
  52. void NetworkStateChanged( unsigned short offset );
  53. // Marks the PVS information dirty
  54. void MarkPVSInformationDirty();
  55. // Marks for deletion
  56. void MarkForDeletion();
  57. bool IsMarkedForDeletion() const;
  58. // Sets the network parent
  59. void SetNetworkParent( EHANDLE hParent );
  60. CServerNetworkProperty* GetNetworkParent();
  61. // This is useful for entities that don't change frequently or that the client
  62. // doesn't need updates on very often. If you use this mode, the server will only try to
  63. // detect state changes every N seconds, so it will save CPU cycles and bandwidth.
  64. //
  65. // Note: N must be less than AUTOUPDATE_MAX_TIME_LENGTH.
  66. //
  67. // Set back to zero to disable the feature.
  68. //
  69. // This feature works on top of manual mode.
  70. // - If you turn it on and manual mode is off, it will autodetect changes every N seconds.
  71. // - If you turn it on and manual mode is on, then every N seconds it will only say there
  72. // is a change if you've called NetworkStateChanged.
  73. void SetUpdateInterval( float N );
  74. // You can use this to override any entity's ShouldTransmit behavior.
  75. // void SetTransmitProxy( CBaseTransmitProxy *pProxy );
  76. // This version does a PVS check which also checks for connected areas
  77. bool IsInPVS( const CCheckTransmitInfo *pInfo );
  78. // This version doesn't do the area check
  79. bool IsInPVS( const edict_t *pRecipient, const void *pvs, int pvssize );
  80. // Called by the timed event manager when it's time to detect a state change.
  81. virtual void FireEvent();
  82. // Recomputes PVS information
  83. void RecomputePVSInformation();
  84. private:
  85. // Detaches the edict.. should only be called by CBaseNetworkable's destructor.
  86. void DetachEdict();
  87. CBaseEntity *GetOuter();
  88. // Marks the networkable that it will should transmit
  89. void SetTransmit( CCheckTransmitInfo *pInfo );
  90. private:
  91. CBaseEntity *m_pOuter;
  92. // CBaseTransmitProxy *m_pTransmitProxy;
  93. edict_t *m_pPev;
  94. PVSInfo_t m_PVSInfo;
  95. ServerClass *m_pServerClass;
  96. // NOTE: This state is 'owned' by the entity. It's only copied here
  97. // also to help improve cache performance in networking code.
  98. EHANDLE m_hParent;
  99. // Counters for SetUpdateInterval.
  100. CEventRegister m_TimerEvent;
  101. bool m_bPendingStateChange : 1;
  102. // friend class CBaseTransmitProxy;
  103. };
  104. //-----------------------------------------------------------------------------
  105. // inline methods // TODOMO does inline work on virtual functions ?
  106. //-----------------------------------------------------------------------------
  107. inline CBaseNetworkable* CServerNetworkProperty::GetBaseNetworkable()
  108. {
  109. return NULL;
  110. }
  111. inline CBaseEntity* CServerNetworkProperty::GetBaseEntity()
  112. {
  113. return m_pOuter;
  114. }
  115. inline CBaseEntity *CServerNetworkProperty::GetOuter()
  116. {
  117. return m_pOuter;
  118. }
  119. inline PVSInfo_t *CServerNetworkProperty::GetPVSInfo()
  120. {
  121. return &m_PVSInfo;
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Marks the PVS information dirty
  125. //-----------------------------------------------------------------------------
  126. inline void CServerNetworkProperty::MarkPVSInformationDirty()
  127. {
  128. if ( m_pPev )
  129. {
  130. m_pPev->m_fStateFlags |= FL_EDICT_DIRTY_PVS_INFORMATION;
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Sets/gets the network parent
  135. //-----------------------------------------------------------------------------
  136. inline void CServerNetworkProperty::SetNetworkParent( EHANDLE hParent )
  137. {
  138. m_hParent = hParent;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Methods related to the net state mgr
  142. //-----------------------------------------------------------------------------
  143. inline void CServerNetworkProperty::NetworkStateForceUpdate()
  144. {
  145. if ( m_pPev )
  146. m_pPev->StateChanged();
  147. }
  148. inline void CServerNetworkProperty::NetworkStateChanged()
  149. {
  150. // If we're using the timer, then ignore this call.
  151. if ( m_TimerEvent.IsRegistered() )
  152. {
  153. // If we're waiting for a timer event, then queue the change so it happens
  154. // when the timer goes off.
  155. m_bPendingStateChange = true;
  156. }
  157. else
  158. {
  159. if ( m_pPev )
  160. m_pPev->StateChanged();
  161. }
  162. }
  163. inline void CServerNetworkProperty::NetworkStateChanged( unsigned short varOffset )
  164. {
  165. // If we're using the timer, then ignore this call.
  166. if ( m_TimerEvent.IsRegistered() )
  167. {
  168. // If we're waiting for a timer event, then queue the change so it happens
  169. // when the timer goes off.
  170. m_bPendingStateChange = true;
  171. }
  172. else
  173. {
  174. if ( m_pPev )
  175. m_pPev->StateChanged( varOffset );
  176. }
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Methods to get the entindex + edict
  180. //-----------------------------------------------------------------------------
  181. inline int CServerNetworkProperty::entindex() const
  182. {
  183. return ENTINDEX( m_pPev );
  184. }
  185. inline edict_t* CServerNetworkProperty::GetEdict() const
  186. {
  187. // This one's virtual, that's why we have to two other versions
  188. return m_pPev;
  189. }
  190. inline edict_t *CServerNetworkProperty::edict()
  191. {
  192. return m_pPev;
  193. }
  194. inline const edict_t *CServerNetworkProperty::edict() const
  195. {
  196. return m_pPev;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Sets the edict pointer (for swapping edicts)
  200. //-----------------------------------------------------------------------------
  201. inline void CServerNetworkProperty::SetEdict( edict_t *pEdict )
  202. {
  203. m_pPev = pEdict;
  204. }
  205. inline int CServerNetworkProperty::AreaNum() const
  206. {
  207. const_cast<CServerNetworkProperty*>(this)->RecomputePVSInformation();
  208. return m_PVSInfo.m_nAreaNum;
  209. }
  210. #endif // SERVERNETWORKPROPERTY_H