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.

150 lines
4.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //----------------------------------------------------------------------------------------
  4. #ifndef REPLAY_RAGDOLL_H
  5. #define REPLAY_RAGDOLL_H
  6. #ifdef _WIN32
  7. #pragma once
  8. #endif
  9. //--------------------------------------------------------------------------------
  10. class C_BaseAnimating;
  11. //--------------------------------------------------------------------------------
  12. struct RagdollSimulationFrame_t
  13. {
  14. RagdollSimulationFrame_t() : pPositions(NULL), pAngles(NULL), nTick(-1) {}
  15. static RagdollSimulationFrame_t* Alloc( int nNumBones );
  16. int nTick;
  17. Vector* pPositions;
  18. QAngle* pAngles;
  19. Vector vRootPosition;
  20. QAngle angRootAngles;
  21. };
  22. //--------------------------------------------------------------------------------
  23. struct RagdollSimulationData_t
  24. {
  25. RagdollSimulationData_t( C_BaseAnimating* pEntity = NULL, int nStartTick = 0, int nNumBones = 0 );
  26. void Record();
  27. int m_nEntityIndex;
  28. int m_nStartTick;
  29. int m_nDuration;
  30. int m_nNumBones;
  31. typedef unsigned short Iterator_t;
  32. CUtlLinkedList< RagdollSimulationFrame_t*, Iterator_t > m_lstFrames;
  33. C_BaseAnimating* m_pEntity;
  34. };
  35. //--------------------------------------------------------------------------------
  36. // TODO: Refactor this into an interface and hide implementation in cpp file
  37. class CReplayRagdollRecorder
  38. {
  39. private:
  40. CReplayRagdollRecorder();
  41. ~CReplayRagdollRecorder();
  42. public:
  43. static CReplayRagdollRecorder& Instance();
  44. void Init();
  45. void Shutdown();
  46. void Think();
  47. void AddEntry( C_BaseAnimating* pEntity, int nStartTick, int nNumBones );
  48. void StopRecordingRagdoll( C_BaseAnimating* pEntity );
  49. void CleanupStartupTicksAndDurations( int nStartTick );
  50. bool DumpRagdollsToDisk( char const* pszFilename ) const;
  51. bool IsRecording() const { return m_bIsRecording; }
  52. private:
  53. typedef unsigned short Iterator_t;
  54. void StopRecordingRagdollAtIndex( Iterator_t nIndex );
  55. void StopRecordingSleepingRagdolls();
  56. void Record();
  57. bool FindEntryInRecordingList( C_BaseAnimating* pEntity, Iterator_t& nOutIndex );
  58. void PrintDebug();
  59. CUtlLinkedList< RagdollSimulationData_t*, Iterator_t > m_lstRagdolls;
  60. CUtlLinkedList< RagdollSimulationData_t*, Iterator_t > m_lstRagdollsToRecord; // Contains some of the elements from m_lstRagdolls -
  61. // the ones which are still recording
  62. bool m_bIsRecording;
  63. };
  64. //--------------------------------------------------------------------------------
  65. class CReplayRagdollCache
  66. {
  67. private:
  68. CReplayRagdollCache();
  69. public:
  70. static CReplayRagdollCache& Instance();
  71. bool Init( char const* pszFilename );
  72. void Shutdown();
  73. void Think();
  74. bool IsInitialized() const { return m_bInit; }
  75. //
  76. // Returns false is no frame exists for the given entity at the given tick.
  77. // Otherwise, returns a
  78. //
  79. bool GetFrame( C_BaseAnimating* pEntity, int nTick, bool* pBoneSimulated, CBoneAccessor* pBoneAccessor ) const;
  80. private:
  81. RagdollSimulationData_t* FindRagdollEntry( C_BaseAnimating* pEntity, int nTick );
  82. const RagdollSimulationData_t* FindRagdollEntry( C_BaseAnimating* pEntity, int nTick ) const;
  83. bool FindFrame( RagdollSimulationFrame_t*& pFrameOut, RagdollSimulationFrame_t*& pNextFrameOut,
  84. const RagdollSimulationData_t* pRagdollEntry, int nTick );
  85. bool FindFrame( RagdollSimulationFrame_t*& pFrameOut, RagdollSimulationFrame_t*& pNextFrameOut,
  86. const RagdollSimulationData_t* pRagdollEntry, int nTick ) const;
  87. typedef unsigned short Iterator_t;
  88. bool m_bInit;
  89. CUtlLinkedList< RagdollSimulationData_t*, Iterator_t > m_lstRagdolls;
  90. };
  91. //--------------------------------------------------------------------------------
  92. bool Replay_CacheRagdolls( const char* pFilename, int nStartTick );
  93. //--------------------------------------------------------------------------------
  94. inline const RagdollSimulationData_t* CReplayRagdollCache::FindRagdollEntry( C_BaseAnimating* pEntity, int nTick ) const
  95. {
  96. return const_cast< CReplayRagdollCache* >( this )->FindRagdollEntry( pEntity, nTick );
  97. }
  98. inline bool CReplayRagdollCache::FindFrame( RagdollSimulationFrame_t*& pFrameOut, RagdollSimulationFrame_t*& pNextFrameOut,
  99. const RagdollSimulationData_t* pRagdollEntry, int nTick ) const
  100. {
  101. return const_cast< CReplayRagdollCache* >( this )->FindFrame( pFrameOut, pNextFrameOut, pRagdollEntry, nTick );
  102. }
  103. //--------------------------------------------------------------------------------
  104. #endif // REPLAY_RAGDOLL_H