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.

187 lines
5.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #if !defined( FRAMESNAPSHOT_H )
  8. #define FRAMESNAPSHOT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <mempool.h>
  13. #include <utllinkedlist.h>
  14. class PackedEntity;
  15. class HLTVEntityData;
  16. class ReplayEntityData;
  17. class ServerClass;
  18. class CEventInfo;
  19. #define INVALID_PACKED_ENTITY_HANDLE (0)
  20. typedef intptr_t PackedEntityHandle_t;
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Individual entity data, did the entity exist and what was it's serial number
  23. //-----------------------------------------------------------------------------
  24. class CFrameSnapshotEntry
  25. {
  26. public:
  27. ServerClass* m_pClass;
  28. int m_nSerialNumber;
  29. // Keeps track of the fullpack info for this frame for all entities in any pvs:
  30. PackedEntityHandle_t m_pPackedData;
  31. };
  32. // HLTV needs some more data per entity
  33. class CHLTVEntityData
  34. {
  35. public:
  36. vec_t origin[3]; // entity position
  37. unsigned int m_nNodeCluster; // if (1<<31) is set it's a node, otherwise a cluster
  38. };
  39. // Replay needs some more data per entity
  40. class CReplayEntityData
  41. {
  42. public:
  43. vec_t origin[3]; // entity position
  44. unsigned int m_nNodeCluster; // if (1<<31) is set it's a node, otherwise a cluster
  45. };
  46. typedef struct
  47. {
  48. PackedEntity *pEntity; // original packed entity
  49. int counter; // increaseing counter to find LRU entries
  50. int bits; // uncompressed data length in bits
  51. char data[MAX_PACKEDENTITY_DATA]; // uncompressed data cache
  52. } UnpackedDataCache_t;
  53. //-----------------------------------------------------------------------------
  54. // Purpose: For all entities, stores whether the entity existed and what frame the
  55. // snapshot is for. Also tracks whether the snapshot is still referenced. When no
  56. // longer referenced, it's freed
  57. //-----------------------------------------------------------------------------
  58. class CFrameSnapshot
  59. {
  60. DECLARE_FIXEDSIZE_ALLOCATOR( CFrameSnapshot );
  61. public:
  62. CFrameSnapshot();
  63. ~CFrameSnapshot();
  64. // Reference-counting.
  65. void AddReference();
  66. void ReleaseReference();
  67. CFrameSnapshot* NextSnapshot() const;
  68. public:
  69. CInterlockedInt m_ListIndex; // Index info CFrameSnapshotManager::m_FrameSnapshots.
  70. // Associated frame.
  71. int m_nTickCount; // = sv.tickcount
  72. // State information
  73. CFrameSnapshotEntry *m_pEntities;
  74. int m_nNumEntities; // = sv.num_edicts
  75. // This list holds the entities that are in use and that also aren't entities for inactive clients.
  76. unsigned short *m_pValidEntities;
  77. int m_nValidEntities;
  78. // Additional HLTV info
  79. CHLTVEntityData *m_pHLTVEntityData; // is NULL if not in HLTV mode or array of m_pValidEntities entries
  80. CReplayEntityData *m_pReplayEntityData; // is NULL if not in replay mode or array of m_pValidEntities entries
  81. CEventInfo **m_pTempEntities; // temp entities
  82. int m_nTempEntities;
  83. CUtlVector<int> m_iExplicitDeleteSlots;
  84. private:
  85. // Snapshots auto-delete themselves when their refcount goes to zero.
  86. CInterlockedInt m_nReferences;
  87. };
  88. //-----------------------------------------------------------------------------
  89. // Purpose: snapshot manager class
  90. //-----------------------------------------------------------------------------
  91. class CFrameSnapshotManager
  92. {
  93. friend class CFrameSnapshot;
  94. public:
  95. CFrameSnapshotManager( void );
  96. virtual ~CFrameSnapshotManager( void );
  97. // IFrameSnapshot implementation.
  98. public:
  99. // Called when a level change happens
  100. virtual void LevelChanged();
  101. // Called once per frame after simulation to store off all entities.
  102. // Note: the returned snapshot has a recount of 1 so you MUST call ReleaseReference on it.
  103. CFrameSnapshot* CreateEmptySnapshot( int ticknumber, int maxEntities );
  104. CFrameSnapshot* TakeTickSnapshot( int ticknumber );
  105. CFrameSnapshot* NextSnapshot( const CFrameSnapshot *pSnapshot );
  106. // Creates pack data for a particular entity for a particular snapshot
  107. PackedEntity* CreatePackedEntity( CFrameSnapshot* pSnapshot, int entity );
  108. // Returns the pack data for a particular entity for a particular snapshot
  109. PackedEntity* GetPackedEntity( CFrameSnapshot* pSnapshot, int entity );
  110. // if we are copying a Packed Entity, we have to increase the reference counter
  111. void AddEntityReference( PackedEntityHandle_t handle );
  112. // if we are removeing a Packed Entity, we have to decrease the reference counter
  113. void RemoveEntityReference( PackedEntityHandle_t handle );
  114. // Uses a previously sent packet
  115. bool UsePreviouslySentPacket( CFrameSnapshot* pSnapshot, int entity, int entSerialNumber );
  116. bool ShouldForceRepack( CFrameSnapshot* pSnapshot, int entity, PackedEntityHandle_t handle );
  117. PackedEntity* GetPreviouslySentPacket( int iEntity, int iSerialNumber );
  118. // Return the entity sitting in iEntity's slot if iSerialNumber matches its number.
  119. UnpackedDataCache_t *GetCachedUncompressedEntity( PackedEntity *pPackedEntity );
  120. CThreadFastMutex &GetMutex();
  121. // List of entities to explicitly delete
  122. void AddExplicitDelete( int iSlot );
  123. private:
  124. void DeleteFrameSnapshot( CFrameSnapshot* pSnapshot );
  125. CUtlLinkedList<CFrameSnapshot*, unsigned short> m_FrameSnapshots;
  126. CClassMemoryPool< PackedEntity > m_PackedEntitiesPool;
  127. int m_nPackedEntityCacheCounter; // increase with every cache access
  128. CUtlVector<UnpackedDataCache_t> m_PackedEntityCache; // cache for uncompressed packed entities
  129. // The most recently sent packets for each entity
  130. PackedEntityHandle_t m_pPackedData[ MAX_EDICTS ];
  131. int m_pSerialNumber[ MAX_EDICTS ];
  132. CThreadFastMutex m_WriteMutex;
  133. CUtlVector<int> m_iExplicitDeleteSlots;
  134. };
  135. extern CFrameSnapshotManager *framesnapshotmanager;
  136. #endif // FRAMESNAPSHOT_H