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.

485 lines
14 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "bone_setup.h"
  10. #include "physics_bone_follower.h"
  11. #include "vcollide_parse.h"
  12. #include "saverestore_utlvector.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. BEGIN_SIMPLE_DATADESC( physfollower_t )
  16. DEFINE_FIELD( boneIndex, FIELD_INTEGER ),
  17. DEFINE_FIELD( hFollower, FIELD_EHANDLE ),
  18. END_DATADESC()
  19. BEGIN_SIMPLE_DATADESC( CBoneFollowerManager )
  20. DEFINE_GLOBAL_FIELD( m_iNumBones, FIELD_INTEGER ),
  21. DEFINE_GLOBAL_UTLVECTOR( m_physBones, FIELD_EMBEDDED ),
  22. END_DATADESC()
  23. //================================================================================================================
  24. // BONE FOLLOWER MANAGER
  25. //================================================================================================================
  26. CBoneFollowerManager::CBoneFollowerManager()
  27. {
  28. m_iNumBones = 0;
  29. }
  30. CBoneFollowerManager::~CBoneFollowerManager()
  31. {
  32. // if this fires then someone isn't destroying their bonefollowers in UpdateOnRemove
  33. Assert(m_iNumBones==0);
  34. DestroyBoneFollowers();
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. // Input : *pEntity -
  39. // iNumBones -
  40. // **pFollowerBoneNames -
  41. //-----------------------------------------------------------------------------
  42. void CBoneFollowerManager::InitBoneFollowers( CBaseAnimating *pParentEntity, int iNumBones, const char **pFollowerBoneNames )
  43. {
  44. m_iNumBones = iNumBones;
  45. m_physBones.EnsureCount( iNumBones );
  46. // Now init all the bones
  47. for ( int i = 0; i < iNumBones; i++ )
  48. {
  49. CreatePhysicsFollower( pParentEntity, m_physBones[i], pFollowerBoneNames[i], NULL );
  50. }
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. //-----------------------------------------------------------------------------
  55. void CBoneFollowerManager::AddBoneFollower( CBaseAnimating *pParentEntity, const char *pFollowerBoneName, solid_t *pSolid )
  56. {
  57. m_iNumBones++;
  58. int iIndex = m_physBones.AddToTail();
  59. CreatePhysicsFollower( pParentEntity, m_physBones[iIndex], pFollowerBoneName, pSolid );
  60. }
  61. // walk the hitboxes and find the first one that is attached to the physics bone in question
  62. // return the hitgroup of that box
  63. static int HitGroupFromPhysicsBone( CBaseAnimating *pAnim, int physicsBone )
  64. {
  65. CStudioHdr *pStudioHdr = pAnim->GetModelPtr( );
  66. mstudiohitboxset_t *set = pStudioHdr->pHitboxSet( pAnim->m_nHitboxSet );
  67. for ( int i = 0; i < set->numhitboxes; i++ )
  68. {
  69. if ( pStudioHdr->pBone( set->pHitbox(i)->bone )->physicsbone == physicsBone )
  70. {
  71. return set->pHitbox(i)->group;
  72. }
  73. }
  74. return 0;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose:
  78. // Input : &follow -
  79. // *pBoneName -
  80. // Output : Returns true on success, false on failure.
  81. //-----------------------------------------------------------------------------
  82. bool CBoneFollowerManager::CreatePhysicsFollower( CBaseAnimating *pParentEntity, physfollower_t &follow, const char *pBoneName, solid_t *pSolid )
  83. {
  84. CStudioHdr *pStudioHdr = pParentEntity->GetModelPtr();
  85. matrix3x4_t boneToWorld;
  86. solid_t solidTmp;
  87. Vector bonePosition;
  88. QAngle boneAngles;
  89. int boneIndex = Studio_BoneIndexByName( pStudioHdr, pBoneName );
  90. if ( boneIndex >= 0 )
  91. {
  92. mstudiobone_t *pBone = pStudioHdr->pBone( boneIndex );
  93. int physicsBone = pBone->physicsbone;
  94. if ( !pSolid )
  95. {
  96. if ( !PhysModelParseSolidByIndex( solidTmp, pParentEntity, pParentEntity->GetModelIndex(), physicsBone ) )
  97. return false;
  98. pSolid = &solidTmp;
  99. }
  100. // fixup in case ragdoll is assigned to a parent of the requested follower bone
  101. follow.boneIndex = Studio_BoneIndexByName( pStudioHdr, pSolid->name );
  102. if ( follow.boneIndex < 0 )
  103. {
  104. follow.boneIndex = boneIndex;
  105. }
  106. pParentEntity->GetBoneTransform( follow.boneIndex, boneToWorld );
  107. MatrixAngles( boneToWorld, boneAngles, bonePosition );
  108. follow.hFollower = CBoneFollower::Create( pParentEntity, STRING(pParentEntity->GetModelName()), *pSolid, bonePosition, boneAngles );
  109. follow.hFollower->SetTraceData( physicsBone, HitGroupFromPhysicsBone( pParentEntity, physicsBone ) );
  110. follow.hFollower->SetBlocksLOS( pParentEntity->BlocksLOS() );
  111. return true;
  112. }
  113. else
  114. {
  115. Warning( "ERROR: Tried to create bone follower on invalid bone %s\n", pBoneName );
  116. }
  117. return false;
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. //-----------------------------------------------------------------------------
  122. void CBoneFollowerManager::UpdateBoneFollowers( CBaseAnimating *pParentEntity )
  123. {
  124. if ( m_iNumBones )
  125. {
  126. matrix3x4_t boneToWorld;
  127. Vector bonePosition;
  128. QAngle boneAngles;
  129. for ( int i = 0; i < m_iNumBones; i++ )
  130. {
  131. if ( !m_physBones[i].hFollower )
  132. continue;
  133. pParentEntity->GetBoneTransform( m_physBones[i].boneIndex, boneToWorld );
  134. MatrixAngles( boneToWorld, boneAngles, bonePosition );
  135. m_physBones[i].hFollower->UpdateFollower( bonePosition, boneAngles, 0.1 );
  136. }
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose:
  141. //-----------------------------------------------------------------------------
  142. void CBoneFollowerManager::DestroyBoneFollowers( void )
  143. {
  144. for ( int i = 0; i < m_iNumBones; i++ )
  145. {
  146. if ( !m_physBones[i].hFollower )
  147. continue;
  148. UTIL_Remove( m_physBones[i].hFollower );
  149. m_physBones[i].hFollower = NULL;
  150. }
  151. m_physBones.Purge();
  152. m_iNumBones = 0;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. //-----------------------------------------------------------------------------
  157. physfollower_t *CBoneFollowerManager::GetBoneFollower( int iFollowerIndex )
  158. {
  159. Assert( iFollowerIndex >= 0 && iFollowerIndex < m_iNumBones );
  160. if ( iFollowerIndex >= 0 && iFollowerIndex < m_iNumBones )
  161. return &m_physBones[iFollowerIndex];
  162. return NULL;
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: Retrieve the index for a supplied bone follower
  166. // Input : *pFollower - Bone follower to look up
  167. // Output : -1 if not found, otherwise the index of the bone follower
  168. //-----------------------------------------------------------------------------
  169. int CBoneFollowerManager::GetBoneFollowerIndex( CBoneFollower *pFollower )
  170. {
  171. if ( pFollower == NULL )
  172. return -1;
  173. for ( int i = 0; i < m_iNumBones; i++ )
  174. {
  175. if ( !m_physBones[i].hFollower )
  176. continue;
  177. if ( m_physBones[i].hFollower == pFollower )
  178. return i;
  179. }
  180. return -1;
  181. }
  182. //================================================================================================================
  183. // BONE FOLLOWER
  184. //================================================================================================================
  185. //---------------------------------------------------------
  186. // Save/Restore
  187. //---------------------------------------------------------
  188. BEGIN_DATADESC( CBoneFollower )
  189. DEFINE_FIELD( m_modelIndex, FIELD_MODELINDEX ),
  190. DEFINE_FIELD( m_solidIndex, FIELD_INTEGER ),
  191. DEFINE_FIELD( m_physicsBone, FIELD_INTEGER ),
  192. DEFINE_FIELD( m_hitGroup, FIELD_INTEGER ),
  193. END_DATADESC()
  194. IMPLEMENT_SERVERCLASS_ST( CBoneFollower, DT_BoneFollower )
  195. SendPropModelIndex(SENDINFO(m_modelIndex)),
  196. SendPropInt(SENDINFO(m_solidIndex), 6, SPROP_UNSIGNED ),
  197. END_SEND_TABLE()
  198. bool CBoneFollower::Init( CBaseEntity *pOwner, const char *pModelName, solid_t &solid, const Vector &position, const QAngle &orientation )
  199. {
  200. SetOwnerEntity( pOwner );
  201. UTIL_SetModel( this, pModelName );
  202. AddEffects( EF_NODRAW ); // invisible
  203. m_modelIndex = modelinfo->GetModelIndex( pModelName );
  204. m_solidIndex = solid.index;
  205. SetAbsOrigin( position );
  206. SetAbsAngles( orientation );
  207. SetMoveType( MOVETYPE_PUSH );
  208. SetSolid( SOLID_VPHYSICS );
  209. SetCollisionGroup( pOwner->GetCollisionGroup() );
  210. AddSolidFlags( FSOLID_CUSTOMRAYTEST | FSOLID_CUSTOMBOXTEST );
  211. solid.params.pGameData = (void *)this;
  212. IPhysicsObject *pPhysics = VPhysicsInitShadow( false, false, &solid );
  213. if ( !pPhysics )
  214. return false;
  215. // we can't use the default model bounds because each entity is only one bone of the model
  216. // so compute the OBB of the physics model and use that.
  217. Vector mins, maxs;
  218. physcollision->CollideGetAABB( &mins, &maxs, pPhysics->GetCollide(), vec3_origin, vec3_angle );
  219. SetCollisionBounds( mins, maxs );
  220. pPhysics->SetCallbackFlags( pPhysics->GetCallbackFlags() | CALLBACK_GLOBAL_TOUCH );
  221. pPhysics->EnableGravity( false );
  222. // This is not a normal shadow controller that is trying to go to a space occupied by an entity in the game physics
  223. // This entity is not running PhysicsPusher(), so Vphysics is supposed to move it
  224. // This line of code informs vphysics of that fact
  225. if ( pOwner->IsNPC() )
  226. {
  227. pPhysics->GetShadowController()->SetPhysicallyControlled( true );
  228. }
  229. return true;
  230. }
  231. int CBoneFollower::UpdateTransmitState()
  232. {
  233. // Send to the client for client-side collisions and visualization
  234. return SetTransmitState( FL_EDICT_PVSCHECK );
  235. }
  236. void CBoneFollower::VPhysicsUpdate( IPhysicsObject *pPhysics )
  237. {
  238. Vector origin;
  239. QAngle angles;
  240. pPhysics->GetPosition( &origin, &angles );
  241. SetAbsOrigin( origin );
  242. SetAbsAngles( angles );
  243. }
  244. // a little helper class to temporarily change the physics object
  245. // for an entity - and change it back when it goes out of scope.
  246. class CPhysicsSwapTemp
  247. {
  248. public:
  249. CPhysicsSwapTemp( CBaseEntity *pEntity, IPhysicsObject *pTmpPhysics )
  250. {
  251. Assert(pEntity);
  252. Assert(pTmpPhysics);
  253. m_pEntity = pEntity;
  254. m_pPhysics = m_pEntity->VPhysicsGetObject();
  255. if ( m_pPhysics )
  256. {
  257. m_pEntity->VPhysicsSwapObject( pTmpPhysics );
  258. }
  259. else
  260. {
  261. m_pEntity->VPhysicsSetObject( pTmpPhysics );
  262. }
  263. }
  264. ~CPhysicsSwapTemp()
  265. {
  266. m_pEntity->VPhysicsSwapObject( m_pPhysics );
  267. }
  268. private:
  269. CBaseEntity *m_pEntity;
  270. IPhysicsObject *m_pPhysics;
  271. };
  272. void CBoneFollower::VPhysicsCollision( int index, gamevcollisionevent_t *pEvent )
  273. {
  274. CBaseEntity *pOwner = GetOwnerEntity();
  275. if ( pOwner )
  276. {
  277. CPhysicsSwapTemp tmp(pOwner, pEvent->pObjects[index] );
  278. pOwner->VPhysicsCollision( index, pEvent );
  279. }
  280. }
  281. void CBoneFollower::VPhysicsShadowCollision( int index, gamevcollisionevent_t *pEvent )
  282. {
  283. CBaseEntity *pOwner = GetOwnerEntity();
  284. if ( pOwner )
  285. {
  286. CPhysicsSwapTemp tmp(pOwner, pEvent->pObjects[index] );
  287. pOwner->VPhysicsShadowCollision( index, pEvent );
  288. }
  289. }
  290. void CBoneFollower::VPhysicsFriction( IPhysicsObject *pObject, float energy, int surfaceProps, int surfacePropsHit )
  291. {
  292. CBaseEntity *pOwner = GetOwnerEntity();
  293. if ( pOwner )
  294. {
  295. CPhysicsSwapTemp tmp(pOwner, pObject );
  296. pOwner->VPhysicsFriction( pObject, energy, surfaceProps, surfacePropsHit );
  297. }
  298. }
  299. bool CBoneFollower::TestCollision( const Ray_t &ray, unsigned int mask, trace_t& trace )
  300. {
  301. vcollide_t *pCollide = modelinfo->GetVCollide( GetModelIndex() );
  302. Assert( pCollide && pCollide->solidCount > m_solidIndex );
  303. UTIL_ClearTrace( trace );
  304. physcollision->TraceBox( ray, pCollide->solids[m_solidIndex], GetAbsOrigin(), GetAbsAngles(), &trace );
  305. if ( trace.fraction >= 1 )
  306. return false;
  307. // return owner as trace hit
  308. trace.m_pEnt = GetOwnerEntity();
  309. trace.hitgroup = m_hitGroup;
  310. trace.physicsbone = m_physicsBone;
  311. return true;
  312. }
  313. void CBoneFollower::UpdateFollower( const Vector &position, const QAngle &orientation, float flInterval )
  314. {
  315. // UNDONE: Shadow update needs timing info?
  316. VPhysicsGetObject()->UpdateShadow( position, orientation, false, flInterval );
  317. }
  318. void CBoneFollower::SetTraceData( int physicsBone, int hitGroup )
  319. {
  320. m_hitGroup = hitGroup;
  321. m_physicsBone = physicsBone;
  322. }
  323. CBoneFollower *CBoneFollower::Create( CBaseEntity *pOwner, const char *pModelName, solid_t &solid, const Vector &position, const QAngle &orientation )
  324. {
  325. CBoneFollower *pFollower = (CBoneFollower *)CreateEntityByName( "phys_bone_follower" );
  326. if ( pFollower )
  327. {
  328. pFollower->Init( pOwner, pModelName, solid, position, orientation );
  329. }
  330. return pFollower;
  331. }
  332. //-----------------------------------------------------------------------------
  333. // Purpose:
  334. //-----------------------------------------------------------------------------
  335. int CBoneFollower::ObjectCaps()
  336. {
  337. CBaseEntity *pOwner = GetOwnerEntity();
  338. if ( pOwner )
  339. {
  340. if( pOwner->m_iGlobalname != NULL_STRING )
  341. {
  342. int caps = BaseClass::ObjectCaps() | pOwner->ObjectCaps();
  343. caps &= ~FCAP_ACROSS_TRANSITION;
  344. return caps;
  345. }
  346. }
  347. return BaseClass::ObjectCaps();
  348. }
  349. //-----------------------------------------------------------------------------
  350. // Purpose:
  351. //-----------------------------------------------------------------------------
  352. void CBoneFollower::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  353. {
  354. CBaseEntity *pOwner = GetOwnerEntity();
  355. if ( pOwner )
  356. {
  357. pOwner->Use( pActivator, pCaller, useType, value );
  358. return;
  359. }
  360. BaseClass::Use( pActivator, pCaller, useType, value );
  361. }
  362. //-----------------------------------------------------------------------------
  363. // Purpose: Pass on Touch calls to the entity we're following
  364. //-----------------------------------------------------------------------------
  365. void CBoneFollower::Touch( CBaseEntity *pOther )
  366. {
  367. CBaseEntity *pOwner = GetOwnerEntity();
  368. if ( pOwner )
  369. {
  370. //TODO: fill in the touch trace with the hitbox number associated with this bone
  371. pOwner->Touch( pOther );
  372. return;
  373. }
  374. BaseClass::Touch( pOther );
  375. }
  376. //-----------------------------------------------------------------------------
  377. // Purpose: Pass on trace attack calls to the entity we're following
  378. //-----------------------------------------------------------------------------
  379. void CBoneFollower::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
  380. {
  381. CBaseEntity *pOwner = GetOwnerEntity();
  382. if ( pOwner )
  383. {
  384. pOwner->DispatchTraceAttack( info, vecDir, ptr, pAccumulator );
  385. return;
  386. }
  387. BaseClass::TraceAttack( info, vecDir, ptr, pAccumulator );
  388. }
  389. LINK_ENTITY_TO_CLASS( phys_bone_follower, CBoneFollower );
  390. // create a manager and a list of followers directly from a ragdoll
  391. void CreateBoneFollowersFromRagdoll( CBaseAnimating *pEntity, CBoneFollowerManager *pManager, vcollide_t *pCollide )
  392. {
  393. IVPhysicsKeyParser *pParse = physcollision->VPhysicsKeyParserCreate( pCollide->pKeyValues );
  394. while ( !pParse->Finished() )
  395. {
  396. const char *pBlock = pParse->GetCurrentBlockName();
  397. if ( !strcmpi( pBlock, "solid" ) )
  398. {
  399. solid_t solid;
  400. pParse->ParseSolid( &solid, NULL );
  401. // collisions are off by default, turn them on
  402. solid.params.enableCollisions = true;
  403. solid.params.pName = STRING(pEntity->GetModelName());
  404. pManager->AddBoneFollower( pEntity, solid.name, &solid );
  405. }
  406. else
  407. {
  408. pParse->SkipBlock();
  409. }
  410. }
  411. }