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.

259 lines
7.0 KiB

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