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.

211 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #if !defined( PACKED_ENTITY_H )
  10. #define PACKED_ENTITY_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include <const.h>
  15. #include <basetypes.h>
  16. #include <mempool.h>
  17. #include <utlvector.h>
  18. #include <tier0/dbg.h>
  19. #include "common.h"
  20. // Matched with the memdbgoff at end of header
  21. #include "memdbgon.h"
  22. // This is extra spew to the files cltrace.txt + svtrace.txt
  23. // #define DEBUG_NETWORKING 1
  24. #if defined( DEBUG_NETWORKING )
  25. #include "convar.h"
  26. void SpewToFile( PRINTF_FORMAT_STRING char const* pFmt, ... );
  27. extern ConVar sv_packettrace;
  28. #define TRACE_PACKET( text ) if ( sv_packettrace.GetInt() ) { SpewToFile text ; };
  29. #else
  30. #define TRACE_PACKET( text )
  31. #endif
  32. enum
  33. {
  34. ENTITY_SENTINEL = 9999 // larger number than any real entity number
  35. };
  36. #define FLAG_IS_COMPRESSED (1<<31)
  37. class CSendProxyRecipients;
  38. class SendTable;
  39. class RecvTable;
  40. class ServerClass;
  41. class ClientClass;
  42. class IChangeFrameList;
  43. // Replaces entity_state_t.
  44. // This is what we send to clients.
  45. class PackedEntity
  46. {
  47. public:
  48. PackedEntity();
  49. ~PackedEntity();
  50. void SetNumBits( int nBits );
  51. int GetNumBits() const;
  52. int GetNumBytes() const;
  53. void SetCompressed();
  54. bool IsCompressed() const;
  55. // Access the data in the entity.
  56. void* GetData();
  57. void FreeData();
  58. // Copy the data into the PackedEntity's data and make sure the # bytes allocated is
  59. // an integer multiple of 4.
  60. bool AllocAndCopyPadded( const void *pData, unsigned long size );
  61. // These are like Get/Set, except SnagChangeFrameList clears out the
  62. // PackedEntity's pointer since the usage model in sv_main is to keep
  63. // the same CChangeFrameList in the most recent PackedEntity for the
  64. // lifetime of an edict.
  65. //
  66. // When the PackedEntity is deleted, it deletes its current CChangeFrameList if it exists.
  67. void SetChangeFrameList( IChangeFrameList *pList );
  68. IChangeFrameList* GetChangeFrameList();
  69. IChangeFrameList* SnagChangeFrameList();
  70. // If this PackedEntity has a ChangeFrameList, then this calls through. If not, it returns all props
  71. int GetPropsChangedAfterTick( int iTick, int *iOutProps, int nMaxOutProps );
  72. // Access the recipients array.
  73. const CSendProxyRecipients* GetRecipients() const;
  74. int GetNumRecipients() const;
  75. void SetRecipients( const CUtlMemory<CSendProxyRecipients> &recipients );
  76. bool CompareRecipients( const CUtlMemory<CSendProxyRecipients> &recipients );
  77. void SetSnapshotCreationTick( int nTick );
  78. int GetSnapshotCreationTick() const;
  79. void SetShouldCheckCreationTick( bool bState );
  80. bool ShouldCheckCreationTick() const;
  81. void SetServerAndClientClass( ServerClass *pServerClass, ClientClass *pClientClass );
  82. public:
  83. ServerClass *m_pServerClass; // Valid on the server
  84. ClientClass *m_pClientClass; // Valid on the client
  85. int m_nEntityIndex; // Entity index.
  86. int m_ReferenceCount; // reference count;
  87. private:
  88. CUtlVector<CSendProxyRecipients> m_Recipients;
  89. void *m_pData; // Packed data.
  90. int m_nBits; // Number of bits used to encode.
  91. IChangeFrameList *m_pChangeFrameList; // Only the most current
  92. // This is the tick this PackedEntity was created on
  93. unsigned int m_nSnapshotCreationTick : 31;
  94. unsigned int m_nShouldCheckCreationTick : 1;
  95. };
  96. inline void PackedEntity::SetNumBits( int nBits )
  97. {
  98. Assert( !( nBits & 31 ) );
  99. m_nBits = nBits;
  100. }
  101. inline void PackedEntity::SetCompressed()
  102. {
  103. m_nBits |= FLAG_IS_COMPRESSED;
  104. }
  105. inline bool PackedEntity::IsCompressed() const
  106. {
  107. return (m_nBits & FLAG_IS_COMPRESSED) != 0;
  108. }
  109. inline int PackedEntity::GetNumBits() const
  110. {
  111. Assert( !( m_nBits & 31 ) );
  112. return m_nBits & ~(FLAG_IS_COMPRESSED);
  113. }
  114. inline int PackedEntity::GetNumBytes() const
  115. {
  116. return Bits2Bytes( m_nBits );
  117. }
  118. inline void* PackedEntity::GetData()
  119. {
  120. return m_pData;
  121. }
  122. inline void PackedEntity::FreeData()
  123. {
  124. if ( m_pData )
  125. {
  126. free(m_pData);
  127. m_pData = NULL;
  128. }
  129. }
  130. inline void PackedEntity::SetChangeFrameList( IChangeFrameList *pList )
  131. {
  132. Assert( !m_pChangeFrameList );
  133. m_pChangeFrameList = pList;
  134. }
  135. inline IChangeFrameList* PackedEntity::GetChangeFrameList()
  136. {
  137. return m_pChangeFrameList;
  138. }
  139. inline IChangeFrameList* PackedEntity::SnagChangeFrameList()
  140. {
  141. IChangeFrameList *pRet = m_pChangeFrameList;
  142. m_pChangeFrameList = NULL;
  143. return pRet;
  144. }
  145. inline void PackedEntity::SetSnapshotCreationTick( int nTick )
  146. {
  147. m_nSnapshotCreationTick = (unsigned int)nTick;
  148. }
  149. inline int PackedEntity::GetSnapshotCreationTick() const
  150. {
  151. return (int)m_nSnapshotCreationTick;
  152. }
  153. inline void PackedEntity::SetShouldCheckCreationTick( bool bState )
  154. {
  155. m_nShouldCheckCreationTick = bState ? 1 : 0;
  156. }
  157. inline bool PackedEntity::ShouldCheckCreationTick() const
  158. {
  159. return m_nShouldCheckCreationTick == 1 ? true : false;
  160. }
  161. #include "memdbgoff.h"
  162. #endif // PACKED_ENTITY_H