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.

157 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef RAGDOLL_SHARED_H
  7. #define RAGDOLL_SHARED_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. class IPhysicsObject;
  12. class IPhysicsConstraint;
  13. class IPhysicsConstraintGroup;
  14. class IPhysicsCollision;
  15. class IPhysicsEnvironment;
  16. class IPhysicsSurfaceProps;
  17. struct matrix3x4_t;
  18. struct vcollide_t;
  19. struct studiohdr_t;
  20. class CStudioHdr;
  21. class CBoneAccessor;
  22. #include "mathlib/vector.h"
  23. #include "bone_accessor.h"
  24. // UNDONE: Remove and make dynamic?
  25. #define RAGDOLL_MAX_ELEMENTS 24
  26. #define RAGDOLL_INDEX_BITS 5 // NOTE 1<<RAGDOLL_INDEX_BITS >= RAGDOLL_MAX_ELEMENTS
  27. #define CORE_DISSOLVE_FADE_START 0.2f
  28. #define CORE_DISSOLVE_MODEL_FADE_START 0.1f
  29. #define CORE_DISSOLVE_MODEL_FADE_LENGTH 0.05f
  30. #define CORE_DISSOLVE_FADEIN_LENGTH 0.1f
  31. struct ragdollelement_t
  32. {
  33. Vector originParentSpace;
  34. IPhysicsObject *pObject; // all valid elements have an object
  35. IPhysicsConstraint *pConstraint; // all valid elements have a constraint (except the root)
  36. int parentIndex;
  37. };
  38. struct ragdollanimatedfriction_t
  39. {
  40. float flFrictionTimeIn;
  41. float flFrictionTimeOut;
  42. float flFrictionTimeHold;
  43. int iMinAnimatedFriction;
  44. int iMaxAnimatedFriction;
  45. };
  46. struct ragdoll_t
  47. {
  48. int listCount;
  49. bool allowStretch;
  50. bool unused;
  51. IPhysicsConstraintGroup *pGroup;
  52. // store these in separate arrays for save/load
  53. ragdollelement_t list[RAGDOLL_MAX_ELEMENTS];
  54. int boneIndex[RAGDOLL_MAX_ELEMENTS];
  55. ragdollanimatedfriction_t animfriction;
  56. };
  57. struct ragdollparams_t
  58. {
  59. void *pGameData;
  60. vcollide_t *pCollide;
  61. CStudioHdr *pStudioHdr;
  62. int modelIndex;
  63. Vector forcePosition;
  64. Vector forceVector;
  65. int forceBoneIndex;
  66. const matrix3x4_t *pCurrentBones;
  67. float jointFrictionScale;
  68. bool allowStretch;
  69. bool fixedConstraints;
  70. };
  71. //-----------------------------------------------------------------------------
  72. // This hooks the main game systems callbacks to allow the AI system to manage memory
  73. //-----------------------------------------------------------------------------
  74. class CRagdollLRURetirement : public CAutoGameSystemPerFrame
  75. {
  76. public:
  77. CRagdollLRURetirement( char const *name ) : CAutoGameSystemPerFrame( name )
  78. {
  79. }
  80. // Methods of IGameSystem
  81. virtual void Update( float frametime );
  82. virtual void FrameUpdatePostEntityThink( void );
  83. // Move it to the top of the LRU
  84. void MoveToTopOfLRU( CBaseAnimating *pRagdoll, bool bImportant = false );
  85. void SetMaxRagdollCount( int iMaxCount ){ m_iMaxRagdolls = iMaxCount; }
  86. virtual void LevelInitPreEntity( void );
  87. int CountRagdolls( bool bOnlySimulatingRagdolls ) { return bOnlySimulatingRagdolls ? m_iSimulatedRagdollCount : m_iRagdollCount; }
  88. private:
  89. typedef CHandle<CBaseAnimating> CRagdollHandle;
  90. CUtlLinkedList< CRagdollHandle > m_LRU;
  91. CUtlLinkedList< CRagdollHandle > m_LRUImportantRagdolls;
  92. int m_iMaxRagdolls;
  93. int m_iSimulatedRagdollCount;
  94. int m_iRagdollCount;
  95. };
  96. extern CRagdollLRURetirement s_RagdollLRU;
  97. // Manages ragdolls fading for the low violence versions
  98. class CRagdollLowViolenceManager
  99. {
  100. public:
  101. CRagdollLowViolenceManager(){ m_bLowViolence = false; }
  102. // Turn the low violence ragdoll stuff off if we're in the HL2 Citadel maps because
  103. // the player has the super gravity gun and fading ragdolls will break things.
  104. void SetLowViolence( const char *pMapName );
  105. bool IsLowViolence( void ){ return m_bLowViolence; }
  106. private:
  107. bool m_bLowViolence;
  108. };
  109. extern CRagdollLowViolenceManager g_RagdollLVManager;
  110. bool RagdollCreate( ragdoll_t &ragdoll, const ragdollparams_t &params, IPhysicsEnvironment *pPhysEnv );
  111. void RagdollActivate( ragdoll_t &ragdoll, vcollide_t *pCollide, int modelIndex, bool bForceWake = true );
  112. void RagdollSetupCollisions( ragdoll_t &ragdoll, vcollide_t *pCollide, int modelIndex );
  113. void RagdollDestroy( ragdoll_t &ragdoll );
  114. // Gets the bone matrix for a ragdoll object
  115. // NOTE: This is different than the object's position because it is
  116. // forced to be rigidly attached in parent space
  117. bool RagdollGetBoneMatrix( const ragdoll_t &ragdoll, CBoneAccessor &pBoneToWorld, int objectIndex );
  118. // Parse the ragdoll and obtain the mapping from each physics element index to a bone index
  119. // returns num phys elements
  120. int RagdollExtractBoneIndices( int *boneIndexOut, CStudioHdr *pStudioHdr, vcollide_t *pCollide );
  121. // computes an exact bbox of the ragdoll's physics objects
  122. void RagdollComputeExactBbox( const ragdoll_t &ragdoll, const Vector &origin, Vector &outMins, Vector &outMaxs );
  123. bool RagdollIsAsleep( const ragdoll_t &ragdoll );
  124. void RagdollSetupAnimatedFriction( IPhysicsEnvironment *pPhysEnv, ragdoll_t *ragdoll, int iModelIndex );
  125. void RagdollApplyAnimationAsVelocity( ragdoll_t &ragdoll, const matrix3x4_t *pBoneToWorld );
  126. void RagdollApplyAnimationAsVelocity( ragdoll_t &ragdoll, const matrix3x4_t *pPrevBones, const matrix3x4_t *pCurrentBones, float dt );
  127. void RagdollSolveSeparation( ragdoll_t &ragdoll, CBaseEntity *pEntity );
  128. #endif // RAGDOLL_SHARED_H