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.

6710 lines
186 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "cbase.h"
  8. #include "c_baseanimating.h"
  9. #include "c_sprite.h"
  10. #include "model_types.h"
  11. #include "bone_setup.h"
  12. #include "ivrenderview.h"
  13. #include "r_efx.h"
  14. #include "dlight.h"
  15. #include "beamdraw.h"
  16. #include "cl_animevent.h"
  17. #include "engine/IEngineSound.h"
  18. #include "c_te_legacytempents.h"
  19. #include "activitylist.h"
  20. #include "animation.h"
  21. #include "tier0/vprof.h"
  22. #include "clienteffectprecachesystem.h"
  23. #include "IEffects.h"
  24. #include "engine/ivmodelinfo.h"
  25. #include "engine/ivdebugoverlay.h"
  26. #include "c_te_effect_dispatch.h"
  27. #include <KeyValues.h>
  28. #include "c_rope.h"
  29. #include "isaverestore.h"
  30. #include "datacache/imdlcache.h"
  31. #include "eventlist.h"
  32. #include "saverestore.h"
  33. #include "physics_saverestore.h"
  34. #include "vphysics/constraints.h"
  35. #include "ragdoll_shared.h"
  36. #include "view.h"
  37. #include "c_ai_basenpc.h"
  38. #include "c_entitydissolve.h"
  39. #include "saverestoretypes.h"
  40. #include "c_fire_smoke.h"
  41. #include "input.h"
  42. #include "soundinfo.h"
  43. #include "tools/bonelist.h"
  44. #include "toolframework/itoolframework.h"
  45. #include "datacache/idatacache.h"
  46. #include "gamestringpool.h"
  47. #include "jigglebones.h"
  48. #include "toolframework_client.h"
  49. #include "vstdlib/jobthread.h"
  50. #include "bonetoworldarray.h"
  51. #include "posedebugger.h"
  52. #include "tier0/icommandline.h"
  53. #include "prediction.h"
  54. #include "replay/replay_ragdoll.h"
  55. #include "studio_stats.h"
  56. #include "tier1/callqueue.h"
  57. #ifdef TF_CLIENT_DLL
  58. #include "c_tf_player.h"
  59. #include "c_baseobject.h"
  60. #include "tf_gamerules.h"
  61. #endif
  62. // memdbgon must be the last include file in a .cpp file!!!
  63. #include "tier0/memdbgon.h"
  64. static ConVar cl_SetupAllBones( "cl_SetupAllBones", "0" );
  65. ConVar r_sequence_debug( "r_sequence_debug", "" );
  66. // If an NPC is moving faster than this, he should play the running footstep sound
  67. const float RUN_SPEED_ESTIMATE_SQR = 150.0f * 150.0f;
  68. // Removed macro used by shared code stuff
  69. #if defined( CBaseAnimating )
  70. #undef CBaseAnimating
  71. #endif
  72. #ifdef DEBUG
  73. static ConVar dbganimmodel( "dbganimmodel", "" );
  74. #endif
  75. #if defined( STAGING_ONLY )
  76. static ConVar dbg_bonestack_perturb( "dbg_bonestack_perturb", "0", 0);
  77. static CInterlockedInt dbg_bonestack_reentrant_count = 0;
  78. #endif // STAGING_ONLY
  79. mstudioevent_t *GetEventIndexForSequence( mstudioseqdesc_t &seqdesc );
  80. C_EntityDissolve *DissolveEffect( C_BaseEntity *pTarget, float flTime );
  81. C_EntityFlame *FireEffect( C_BaseAnimating *pTarget, C_BaseEntity *pServerFire, float *flScaleEnd, float *flTimeStart, float *flTimeEnd );
  82. bool NPC_IsImportantNPC( C_BaseAnimating *pAnimating );
  83. void VCollideWireframe_ChangeCallback( IConVar *pConVar, char const *pOldString, float flOldValue );
  84. ConVar vcollide_wireframe( "vcollide_wireframe", "0", FCVAR_CHEAT, "Render physics collision models in wireframe", VCollideWireframe_ChangeCallback );
  85. bool C_AnimationLayer::IsActive( void )
  86. {
  87. return (m_nOrder != C_BaseAnimatingOverlay::MAX_OVERLAYS);
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Relative lighting entity
  91. //-----------------------------------------------------------------------------
  92. class C_InfoLightingRelative : public C_BaseEntity
  93. {
  94. public:
  95. DECLARE_CLASS( C_InfoLightingRelative, C_BaseEntity );
  96. DECLARE_CLIENTCLASS();
  97. void GetLightingOffset( matrix3x4_t &offset );
  98. private:
  99. EHANDLE m_hLightingLandmark;
  100. };
  101. IMPLEMENT_CLIENTCLASS_DT(C_InfoLightingRelative, DT_InfoLightingRelative, CInfoLightingRelative)
  102. RecvPropEHandle(RECVINFO(m_hLightingLandmark)),
  103. END_RECV_TABLE()
  104. //-----------------------------------------------------------------------------
  105. // Relative lighting entity
  106. //-----------------------------------------------------------------------------
  107. void C_InfoLightingRelative::GetLightingOffset( matrix3x4_t &offset )
  108. {
  109. if ( m_hLightingLandmark.Get() )
  110. {
  111. matrix3x4_t matWorldToLandmark;
  112. MatrixInvert( m_hLightingLandmark->EntityToWorldTransform(), matWorldToLandmark );
  113. ConcatTransforms( EntityToWorldTransform(), matWorldToLandmark, offset );
  114. }
  115. else
  116. {
  117. SetIdentityMatrix( offset );
  118. }
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Base Animating
  122. //-----------------------------------------------------------------------------
  123. struct clientanimating_t
  124. {
  125. C_BaseAnimating *pAnimating;
  126. unsigned int flags;
  127. clientanimating_t(C_BaseAnimating *_pAnim, unsigned int _flags ) : pAnimating(_pAnim), flags(_flags) {}
  128. };
  129. const unsigned int FCLIENTANIM_SEQUENCE_CYCLE = 0x00000001;
  130. static CUtlVector< clientanimating_t > g_ClientSideAnimationList;
  131. BEGIN_RECV_TABLE_NOBASE( C_BaseAnimating, DT_ServerAnimationData )
  132. RecvPropFloat(RECVINFO(m_flCycle)),
  133. END_RECV_TABLE()
  134. void RecvProxy_Sequence( const CRecvProxyData *pData, void *pStruct, void *pOut )
  135. {
  136. // Have the regular proxy store the data.
  137. RecvProxy_Int32ToInt32( pData, pStruct, pOut );
  138. C_BaseAnimating *pAnimating = (C_BaseAnimating *)pStruct;
  139. if ( !pAnimating )
  140. return;
  141. pAnimating->SetReceivedSequence();
  142. // render bounds may have changed
  143. pAnimating->UpdateVisibility();
  144. }
  145. IMPLEMENT_CLIENTCLASS_DT(C_BaseAnimating, DT_BaseAnimating, CBaseAnimating)
  146. RecvPropInt(RECVINFO(m_nSequence), 0, RecvProxy_Sequence),
  147. RecvPropInt(RECVINFO(m_nForceBone)),
  148. RecvPropVector(RECVINFO(m_vecForce)),
  149. RecvPropInt(RECVINFO(m_nSkin)),
  150. RecvPropInt(RECVINFO(m_nBody)),
  151. RecvPropInt(RECVINFO(m_nHitboxSet)),
  152. RecvPropFloat(RECVINFO(m_flModelScale)),
  153. RecvPropFloat(RECVINFO_NAME(m_flModelScale, m_flModelWidthScale)), // for demo compatibility only
  154. // RecvPropArray(RecvPropFloat(RECVINFO(m_flPoseParameter[0])), m_flPoseParameter),
  155. RecvPropArray3(RECVINFO_ARRAY(m_flPoseParameter), RecvPropFloat(RECVINFO(m_flPoseParameter[0])) ),
  156. RecvPropFloat(RECVINFO(m_flPlaybackRate)),
  157. RecvPropArray3( RECVINFO_ARRAY(m_flEncodedController), RecvPropFloat(RECVINFO(m_flEncodedController[0]))),
  158. RecvPropInt( RECVINFO( m_bClientSideAnimation )),
  159. RecvPropInt( RECVINFO( m_bClientSideFrameReset )),
  160. RecvPropInt( RECVINFO( m_nNewSequenceParity )),
  161. RecvPropInt( RECVINFO( m_nResetEventsParity )),
  162. RecvPropInt( RECVINFO( m_nMuzzleFlashParity ) ),
  163. RecvPropEHandle(RECVINFO(m_hLightingOrigin)),
  164. RecvPropEHandle(RECVINFO(m_hLightingOriginRelative)),
  165. RecvPropDataTable( "serveranimdata", 0, 0, &REFERENCE_RECV_TABLE( DT_ServerAnimationData ) ),
  166. RecvPropFloat( RECVINFO( m_fadeMinDist ) ),
  167. RecvPropFloat( RECVINFO( m_fadeMaxDist ) ),
  168. RecvPropFloat( RECVINFO( m_flFadeScale ) ),
  169. END_RECV_TABLE()
  170. BEGIN_PREDICTION_DATA( C_BaseAnimating )
  171. DEFINE_PRED_FIELD( m_nSkin, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
  172. DEFINE_PRED_FIELD( m_nBody, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
  173. // DEFINE_PRED_FIELD( m_nHitboxSet, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
  174. // DEFINE_PRED_FIELD( m_flModelScale, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  175. DEFINE_PRED_FIELD( m_nSequence, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
  176. DEFINE_PRED_FIELD( m_flPlaybackRate, FIELD_FLOAT, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
  177. DEFINE_PRED_FIELD( m_flCycle, FIELD_FLOAT, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
  178. // DEFINE_PRED_ARRAY( m_flPoseParameter, FIELD_FLOAT, MAXSTUDIOPOSEPARAM, FTYPEDESC_INSENDTABLE ),
  179. DEFINE_PRED_ARRAY_TOL( m_flEncodedController, FIELD_FLOAT, MAXSTUDIOBONECTRLS, FTYPEDESC_INSENDTABLE, 0.02f ),
  180. DEFINE_FIELD( m_nPrevSequence, FIELD_INTEGER ),
  181. //DEFINE_FIELD( m_flPrevEventCycle, FIELD_FLOAT ),
  182. //DEFINE_FIELD( m_flEventCycle, FIELD_FLOAT ),
  183. //DEFINE_FIELD( m_nEventSequence, FIELD_INTEGER ),
  184. DEFINE_PRED_FIELD( m_nNewSequenceParity, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
  185. DEFINE_PRED_FIELD( m_nResetEventsParity, FIELD_INTEGER, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
  186. // DEFINE_PRED_FIELD( m_nPrevResetEventsParity, FIELD_INTEGER, 0 ),
  187. DEFINE_PRED_FIELD( m_nMuzzleFlashParity, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
  188. //DEFINE_FIELD( m_nOldMuzzleFlashParity, FIELD_CHARACTER ),
  189. //DEFINE_FIELD( m_nPrevNewSequenceParity, FIELD_INTEGER ),
  190. //DEFINE_FIELD( m_nPrevResetEventsParity, FIELD_INTEGER ),
  191. // DEFINE_PRED_FIELD( m_vecForce, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
  192. // DEFINE_PRED_FIELD( m_nForceBone, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
  193. // DEFINE_PRED_FIELD( m_bClientSideAnimation, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
  194. // DEFINE_PRED_FIELD( m_bClientSideFrameReset, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
  195. // DEFINE_FIELD( m_pRagdollInfo, RagdollInfo_t ),
  196. // DEFINE_FIELD( m_CachedBones, CUtlVector < CBoneCacheEntry > ),
  197. // DEFINE_FIELD( m_pActualAttachmentAngles, FIELD_VECTOR ),
  198. // DEFINE_FIELD( m_pActualAttachmentOrigin, FIELD_VECTOR ),
  199. // DEFINE_FIELD( m_animationQueue, CUtlVector < C_AnimationLayer > ),
  200. // DEFINE_FIELD( m_pIk, CIKContext ),
  201. // DEFINE_FIELD( m_bLastClientSideFrameReset, FIELD_BOOLEAN ),
  202. // DEFINE_FIELD( hdr, studiohdr_t ),
  203. // DEFINE_FIELD( m_pRagdoll, IRagdoll ),
  204. // DEFINE_FIELD( m_bStoreRagdollInfo, FIELD_BOOLEAN ),
  205. // DEFINE_FIELD( C_BaseFlex, m_iEyeAttachment, FIELD_INTEGER ),
  206. END_PREDICTION_DATA()
  207. LINK_ENTITY_TO_CLASS( client_ragdoll, C_ClientRagdoll );
  208. BEGIN_DATADESC( C_ClientRagdoll )
  209. DEFINE_FIELD( m_bFadeOut, FIELD_BOOLEAN ),
  210. DEFINE_FIELD( m_bImportant, FIELD_BOOLEAN ),
  211. DEFINE_FIELD( m_iCurrentFriction, FIELD_INTEGER ),
  212. DEFINE_FIELD( m_iMinFriction, FIELD_INTEGER ),
  213. DEFINE_FIELD( m_iMaxFriction, FIELD_INTEGER ),
  214. DEFINE_FIELD( m_flFrictionModTime, FIELD_FLOAT ),
  215. DEFINE_FIELD( m_flFrictionTime, FIELD_TIME ),
  216. DEFINE_FIELD( m_iFrictionAnimState, FIELD_INTEGER ),
  217. DEFINE_FIELD( m_bReleaseRagdoll, FIELD_BOOLEAN ),
  218. DEFINE_FIELD( m_nBody, FIELD_INTEGER ),
  219. DEFINE_FIELD( m_nSkin, FIELD_INTEGER ),
  220. DEFINE_FIELD( m_nRenderFX, FIELD_CHARACTER ),
  221. DEFINE_FIELD( m_nRenderMode, FIELD_CHARACTER ),
  222. DEFINE_FIELD( m_clrRender, FIELD_COLOR32 ),
  223. DEFINE_FIELD( m_flEffectTime, FIELD_TIME ),
  224. DEFINE_FIELD( m_bFadingOut, FIELD_BOOLEAN ),
  225. DEFINE_AUTO_ARRAY( m_flScaleEnd, FIELD_FLOAT ),
  226. DEFINE_AUTO_ARRAY( m_flScaleTimeStart, FIELD_FLOAT ),
  227. DEFINE_AUTO_ARRAY( m_flScaleTimeEnd, FIELD_FLOAT ),
  228. DEFINE_EMBEDDEDBYREF( m_pRagdoll ),
  229. END_DATADESC()
  230. C_ClientRagdoll::C_ClientRagdoll( bool bRestoring )
  231. {
  232. m_iCurrentFriction = 0;
  233. m_iFrictionAnimState = RAGDOLL_FRICTION_NONE;
  234. m_bReleaseRagdoll = false;
  235. m_bFadeOut = false;
  236. m_bFadingOut = false;
  237. m_bImportant = false;
  238. m_bNoModelParticles = false;
  239. SetClassname("client_ragdoll");
  240. if ( bRestoring == true )
  241. {
  242. m_pRagdoll = new CRagdoll;
  243. }
  244. }
  245. void C_ClientRagdoll::OnSave( void )
  246. {
  247. }
  248. void C_ClientRagdoll::OnRestore( void )
  249. {
  250. CStudioHdr *hdr = GetModelPtr();
  251. if ( hdr == NULL )
  252. {
  253. const char *pModelName = STRING( GetModelName() );
  254. SetModel( pModelName );
  255. hdr = GetModelPtr();
  256. if ( hdr == NULL )
  257. return;
  258. }
  259. if ( m_pRagdoll == NULL )
  260. return;
  261. ragdoll_t *pRagdollT = m_pRagdoll->GetRagdoll();
  262. if ( pRagdollT == NULL || pRagdollT->list[0].pObject == NULL )
  263. {
  264. m_bReleaseRagdoll = true;
  265. m_pRagdoll = NULL;
  266. Assert( !"Attempted to restore a ragdoll without physobjects!" );
  267. return;
  268. }
  269. if ( GetFlags() & FL_DISSOLVING )
  270. {
  271. DissolveEffect( this, m_flEffectTime );
  272. }
  273. else if ( GetFlags() & FL_ONFIRE )
  274. {
  275. C_EntityFlame *pFireChild = dynamic_cast<C_EntityFlame *>( GetEffectEntity() );
  276. C_EntityFlame *pNewFireChild = FireEffect( this, pFireChild, m_flScaleEnd, m_flScaleTimeStart, m_flScaleTimeEnd );
  277. //Set the new fire child as the new effect entity.
  278. SetEffectEntity( pNewFireChild );
  279. }
  280. VPhysicsSetObject( NULL );
  281. VPhysicsSetObject( pRagdollT->list[0].pObject );
  282. SetupBones( NULL, -1, BONE_USED_BY_ANYTHING, gpGlobals->curtime );
  283. pRagdollT->list[0].parentIndex = -1;
  284. pRagdollT->list[0].originParentSpace.Init();
  285. RagdollActivate( *pRagdollT, modelinfo->GetVCollide( GetModelIndex() ), GetModelIndex(), true );
  286. RagdollSetupAnimatedFriction( physenv, pRagdollT, GetModelIndex() );
  287. m_pRagdoll->BuildRagdollBounds( this );
  288. // UNDONE: The shadow & leaf system cleanup should probably be in C_BaseEntity::OnRestore()
  289. // this must be recomputed because the model was NULL when this was set up
  290. RemoveFromLeafSystem();
  291. AddToLeafSystem( RENDER_GROUP_OPAQUE_ENTITY );
  292. DestroyShadow();
  293. CreateShadow();
  294. SetNextClientThink( CLIENT_THINK_ALWAYS );
  295. if ( m_bFadeOut == true )
  296. {
  297. s_RagdollLRU.MoveToTopOfLRU( this, m_bImportant );
  298. }
  299. NoteRagdollCreationTick( this );
  300. BaseClass::OnRestore();
  301. RagdollMoved();
  302. }
  303. void C_ClientRagdoll::ImpactTrace( trace_t *pTrace, int iDamageType, const char *pCustomImpactName )
  304. {
  305. VPROF( "C_ClientRagdoll::ImpactTrace" );
  306. IPhysicsObject *pPhysicsObject = VPhysicsGetObject();
  307. if( !pPhysicsObject )
  308. return;
  309. Vector dir = pTrace->endpos - pTrace->startpos;
  310. if ( iDamageType == DMG_BLAST )
  311. {
  312. dir *= 500; // adjust impact strenght
  313. // apply force at object mass center
  314. pPhysicsObject->ApplyForceCenter( dir );
  315. }
  316. else
  317. {
  318. Vector hitpos;
  319. VectorMA( pTrace->startpos, pTrace->fraction, dir, hitpos );
  320. VectorNormalize( dir );
  321. dir *= 4000; // adjust impact strenght
  322. // apply force where we hit it
  323. pPhysicsObject->ApplyForceOffset( dir, hitpos );
  324. }
  325. m_pRagdoll->ResetRagdollSleepAfterTime();
  326. }
  327. ConVar g_debug_ragdoll_visualize( "g_debug_ragdoll_visualize", "0", FCVAR_CHEAT );
  328. void C_ClientRagdoll::HandleAnimatedFriction( void )
  329. {
  330. if ( m_iFrictionAnimState == RAGDOLL_FRICTION_OFF )
  331. return;
  332. ragdoll_t *pRagdollT = NULL;
  333. int iBoneCount = 0;
  334. if ( m_pRagdoll )
  335. {
  336. pRagdollT = m_pRagdoll->GetRagdoll();
  337. iBoneCount = m_pRagdoll->RagdollBoneCount();
  338. }
  339. if ( pRagdollT == NULL )
  340. return;
  341. switch ( m_iFrictionAnimState )
  342. {
  343. case RAGDOLL_FRICTION_NONE:
  344. {
  345. m_iMinFriction = pRagdollT->animfriction.iMinAnimatedFriction;
  346. m_iMaxFriction = pRagdollT->animfriction.iMaxAnimatedFriction;
  347. if ( m_iMinFriction != 0 || m_iMaxFriction != 0 )
  348. {
  349. m_iFrictionAnimState = RAGDOLL_FRICTION_IN;
  350. m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeIn;
  351. m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
  352. m_iCurrentFriction = m_iMinFriction;
  353. }
  354. else
  355. {
  356. m_iFrictionAnimState = RAGDOLL_FRICTION_OFF;
  357. }
  358. break;
  359. }
  360. case RAGDOLL_FRICTION_IN:
  361. {
  362. float flDeltaTime = (m_flFrictionTime - gpGlobals->curtime);
  363. m_iCurrentFriction = RemapValClamped( flDeltaTime , m_flFrictionModTime, 0, m_iMinFriction, m_iMaxFriction );
  364. if ( flDeltaTime <= 0.0f )
  365. {
  366. m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeHold;
  367. m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
  368. m_iFrictionAnimState = RAGDOLL_FRICTION_HOLD;
  369. }
  370. break;
  371. }
  372. case RAGDOLL_FRICTION_HOLD:
  373. {
  374. if ( m_flFrictionTime < gpGlobals->curtime )
  375. {
  376. m_flFrictionModTime = pRagdollT->animfriction.flFrictionTimeOut;
  377. m_flFrictionTime = gpGlobals->curtime + m_flFrictionModTime;
  378. m_iFrictionAnimState = RAGDOLL_FRICTION_OUT;
  379. }
  380. break;
  381. }
  382. case RAGDOLL_FRICTION_OUT:
  383. {
  384. float flDeltaTime = (m_flFrictionTime - gpGlobals->curtime);
  385. m_iCurrentFriction = RemapValClamped( flDeltaTime , 0, m_flFrictionModTime, m_iMinFriction, m_iMaxFriction );
  386. if ( flDeltaTime <= 0.0f )
  387. {
  388. m_iFrictionAnimState = RAGDOLL_FRICTION_OFF;
  389. }
  390. break;
  391. }
  392. }
  393. for ( int i = 0; i < iBoneCount; i++ )
  394. {
  395. if ( pRagdollT->list[i].pConstraint )
  396. pRagdollT->list[i].pConstraint->SetAngularMotor( 0, m_iCurrentFriction );
  397. }
  398. IPhysicsObject *pPhysicsObject = VPhysicsGetObject();
  399. if ( pPhysicsObject )
  400. {
  401. pPhysicsObject->Wake();
  402. }
  403. }
  404. ConVar g_ragdoll_fadespeed( "g_ragdoll_fadespeed", "600" );
  405. ConVar g_ragdoll_lvfadespeed( "g_ragdoll_lvfadespeed", "100" );
  406. void C_ClientRagdoll::OnPVSStatusChanged( bool bInPVS )
  407. {
  408. if ( bInPVS )
  409. {
  410. CreateShadow();
  411. }
  412. else
  413. {
  414. DestroyShadow();
  415. }
  416. }
  417. void C_ClientRagdoll::FadeOut( void )
  418. {
  419. if ( m_bFadingOut == false )
  420. {
  421. return;
  422. }
  423. int iAlpha = GetRenderColor().a;
  424. int iFadeSpeed = ( g_RagdollLVManager.IsLowViolence() ) ? g_ragdoll_lvfadespeed.GetInt() : g_ragdoll_fadespeed.GetInt();
  425. iAlpha = MAX( iAlpha - ( iFadeSpeed * gpGlobals->frametime ), 0 );
  426. SetRenderMode( kRenderTransAlpha );
  427. SetRenderColorA( iAlpha );
  428. if ( iAlpha == 0 )
  429. {
  430. m_bReleaseRagdoll = true;
  431. }
  432. }
  433. void C_ClientRagdoll::SUB_Remove( void )
  434. {
  435. m_bFadingOut = true;
  436. SetNextClientThink( CLIENT_THINK_ALWAYS );
  437. }
  438. void C_ClientRagdoll::ClientThink( void )
  439. {
  440. if ( m_bReleaseRagdoll == true )
  441. {
  442. DestroyBoneAttachments();
  443. Release();
  444. return;
  445. }
  446. if ( g_debug_ragdoll_visualize.GetBool() )
  447. {
  448. Vector vMins, vMaxs;
  449. Vector origin = m_pRagdoll->GetRagdollOrigin();
  450. m_pRagdoll->GetRagdollBounds( vMins, vMaxs );
  451. if ( debugoverlay )
  452. {
  453. debugoverlay->AddBoxOverlay( origin, vMins, vMaxs, QAngle( 0, 0, 0 ), 0, 255, 0, 16, 0 );
  454. }
  455. }
  456. HandleAnimatedFriction();
  457. FadeOut();
  458. }
  459. //-----------------------------------------------------------------------------
  460. // Purpose: clear out any face/eye values stored in the material system
  461. //-----------------------------------------------------------------------------
  462. float C_ClientRagdoll::LastBoneChangedTime()
  463. {
  464. // When did this last change?
  465. return m_pRagdoll ? m_pRagdoll->GetLastVPhysicsUpdateTime() : -FLT_MAX;
  466. }
  467. //-----------------------------------------------------------------------------
  468. // Purpose: clear out any face/eye values stored in the material system
  469. //-----------------------------------------------------------------------------
  470. void C_ClientRagdoll::SetupWeights( const matrix3x4_t *pBoneToWorld, int nFlexWeightCount, float *pFlexWeights, float *pFlexDelayedWeights )
  471. {
  472. BaseClass::SetupWeights( pBoneToWorld, nFlexWeightCount, pFlexWeights, pFlexDelayedWeights );
  473. CStudioHdr *hdr = GetModelPtr();
  474. if ( !hdr )
  475. return;
  476. int nFlexDescCount = hdr->numflexdesc();
  477. if ( nFlexDescCount )
  478. {
  479. Assert( !pFlexDelayedWeights );
  480. memset( pFlexWeights, 0, nFlexWeightCount * sizeof(float) );
  481. }
  482. if ( m_iEyeAttachment > 0 )
  483. {
  484. matrix3x4_t attToWorld;
  485. if ( GetAttachment( m_iEyeAttachment, attToWorld ) )
  486. {
  487. Vector local, tmp;
  488. local.Init( 1000.0f, 0.0f, 0.0f );
  489. VectorTransform( local, attToWorld, tmp );
  490. modelrender->SetViewTarget( GetModelPtr(), GetBody(), tmp );
  491. }
  492. }
  493. }
  494. void C_ClientRagdoll::Release( void )
  495. {
  496. C_BaseEntity *pChild = GetEffectEntity();
  497. if ( pChild && pChild->IsMarkedForDeletion() == false )
  498. {
  499. pChild->Release();
  500. }
  501. if ( GetThinkHandle() != INVALID_THINK_HANDLE )
  502. {
  503. ClientThinkList()->RemoveThinkable( GetClientHandle() );
  504. }
  505. ClientEntityList().RemoveEntity( GetClientHandle() );
  506. if ( CollisionProp()->GetPartitionHandle() != PARTITION_INVALID_HANDLE )
  507. {
  508. ::partition->Remove( PARTITION_CLIENT_SOLID_EDICTS | PARTITION_CLIENT_RESPONSIVE_EDICTS | PARTITION_CLIENT_NON_STATIC_EDICTS, CollisionProp()->GetPartitionHandle() );
  509. }
  510. RemoveFromLeafSystem();
  511. BaseClass::Release();
  512. }
  513. //-----------------------------------------------------------------------------
  514. // Incremented each frame in InvalidateModelBones. Models compare this value to what it
  515. // was last time they setup their bones to determine if they need to re-setup their bones.
  516. static unsigned long g_iModelBoneCounter = 0;
  517. CUtlVector<C_BaseAnimating *> g_PreviousBoneSetups;
  518. static unsigned long g_iPreviousBoneCounter = (unsigned)-1;
  519. class C_BaseAnimatingGameSystem : public CAutoGameSystem
  520. {
  521. void LevelShutdownPostEntity()
  522. {
  523. g_iPreviousBoneCounter = (unsigned)-1;
  524. if ( g_PreviousBoneSetups.Count() != 0 )
  525. {
  526. Msg( "%d entities in bone setup array. Should have been cleaned up by now\n", g_PreviousBoneSetups.Count() );
  527. g_PreviousBoneSetups.RemoveAll();
  528. }
  529. }
  530. } g_BaseAnimatingGameSystem;
  531. //-----------------------------------------------------------------------------
  532. // Purpose: convert axis rotations to a quaternion
  533. //-----------------------------------------------------------------------------
  534. C_BaseAnimating::C_BaseAnimating() :
  535. m_iv_flCycle( "C_BaseAnimating::m_iv_flCycle" ),
  536. m_iv_flPoseParameter( "C_BaseAnimating::m_iv_flPoseParameter" ),
  537. m_iv_flEncodedController("C_BaseAnimating::m_iv_flEncodedController")
  538. {
  539. m_vecForce.Init();
  540. m_nForceBone = -1;
  541. m_ClientSideAnimationListHandle = INVALID_CLIENTSIDEANIMATION_LIST_HANDLE;
  542. m_nPrevSequence = -1;
  543. m_nRestoreSequence = -1;
  544. m_pRagdoll = NULL;
  545. m_builtRagdoll = false;
  546. m_hitboxBoneCacheHandle = 0;
  547. int i;
  548. for ( i = 0; i < ARRAYSIZE( m_flEncodedController ); i++ )
  549. {
  550. m_flEncodedController[ i ] = 0.0f;
  551. }
  552. AddBaseAnimatingInterpolatedVars();
  553. m_iMostRecentModelBoneCounter = 0xFFFFFFFF;
  554. m_iMostRecentBoneSetupRequest = g_iPreviousBoneCounter - 1;
  555. m_flLastBoneSetupTime = -FLT_MAX;
  556. m_vecPreRagdollMins = vec3_origin;
  557. m_vecPreRagdollMaxs = vec3_origin;
  558. m_bStoreRagdollInfo = false;
  559. m_pRagdollInfo = NULL;
  560. m_flPlaybackRate = 1.0f;
  561. m_nEventSequence = -1;
  562. m_pIk = NULL;
  563. // Assume false. Derived classes might fill in a receive table entry
  564. // and in that case this would show up as true
  565. m_bClientSideAnimation = false;
  566. m_nPrevNewSequenceParity = -1;
  567. m_nPrevResetEventsParity = -1;
  568. m_nOldMuzzleFlashParity = 0;
  569. m_nMuzzleFlashParity = 0;
  570. m_flModelScale = 1.0f;
  571. m_iEyeAttachment = 0;
  572. #ifdef _XBOX
  573. m_iAccumulatedBoneMask = 0;
  574. #endif
  575. m_pStudioHdr = NULL;
  576. m_hStudioHdr = MDLHANDLE_INVALID;
  577. m_bReceivedSequence = false;
  578. m_boneIndexAttached = -1;
  579. m_flOldModelScale = 0.0f;
  580. m_pAttachedTo = NULL;
  581. m_bDynamicModelAllowed = false;
  582. m_bDynamicModelPending = false;
  583. m_bResetSequenceInfoOnLoad = false;
  584. m_bInitModelEffects = false;
  585. m_bDelayInitModelEffects = false;
  586. Q_memset(&m_mouth, 0, sizeof(m_mouth));
  587. m_flCycle = 0;
  588. m_flOldCycle = 0;
  589. }
  590. //-----------------------------------------------------------------------------
  591. // Purpose: cleanup
  592. //-----------------------------------------------------------------------------
  593. C_BaseAnimating::~C_BaseAnimating()
  594. {
  595. int i = g_PreviousBoneSetups.Find( this );
  596. if ( i != -1 )
  597. g_PreviousBoneSetups.FastRemove( i );
  598. TermRopes();
  599. Assert( !m_pRagdoll );
  600. delete m_pRagdollInfo;
  601. m_pRagdollInfo = NULL;
  602. delete m_pIk;
  603. m_pIk = NULL;
  604. delete m_pBoneMergeCache;
  605. m_pBoneMergeCache = NULL;
  606. Studio_DestroyBoneCache( m_hitboxBoneCacheHandle );
  607. delete m_pJiggleBones;
  608. m_pJiggleBones = NULL;
  609. InvalidateMdlCache();
  610. // Kill off anything bone attached to us.
  611. DestroyBoneAttachments();
  612. // If we are bone attached to something, remove us from the list.
  613. if ( m_pAttachedTo )
  614. {
  615. m_pAttachedTo->RemoveBoneAttachment( this );
  616. m_pAttachedTo = NULL;
  617. }
  618. }
  619. bool C_BaseAnimating::UsesPowerOfTwoFrameBufferTexture( void )
  620. {
  621. return modelinfo->IsUsingFBTexture( GetModel(), GetSkin(), GetBody(), GetClientRenderable() );
  622. }
  623. //-----------------------------------------------------------------------------
  624. // VPhysics object
  625. //-----------------------------------------------------------------------------
  626. int C_BaseAnimating::VPhysicsGetObjectList( IPhysicsObject **pList, int listMax )
  627. {
  628. if ( IsRagdoll() )
  629. {
  630. int i;
  631. for ( i = 0; i < m_pRagdoll->RagdollBoneCount(); ++i )
  632. {
  633. if ( i >= listMax )
  634. break;
  635. pList[i] = m_pRagdoll->GetElement(i);
  636. }
  637. return i;
  638. }
  639. return BaseClass::VPhysicsGetObjectList( pList, listMax );
  640. }
  641. //-----------------------------------------------------------------------------
  642. // Should this object cast render-to-texture shadows?
  643. //-----------------------------------------------------------------------------
  644. ShadowType_t C_BaseAnimating::ShadowCastType()
  645. {
  646. CStudioHdr *pStudioHdr = GetModelPtr();
  647. if ( !pStudioHdr || !pStudioHdr->SequencesAvailable() )
  648. return SHADOWS_NONE;
  649. if ( IsEffectActive(EF_NODRAW | EF_NOSHADOW) )
  650. return SHADOWS_NONE;
  651. if (pStudioHdr->GetNumSeq() == 0)
  652. return SHADOWS_RENDER_TO_TEXTURE;
  653. if ( !IsRagdoll() )
  654. {
  655. // If we have pose parameters, always update
  656. if ( pStudioHdr->GetNumPoseParameters() > 0 )
  657. return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
  658. // If we have bone controllers, always update
  659. if ( pStudioHdr->numbonecontrollers() > 0 )
  660. return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
  661. // If we use IK, always update
  662. if ( pStudioHdr->numikchains() > 0 )
  663. return SHADOWS_RENDER_TO_TEXTURE_DYNAMIC;
  664. }
  665. // FIXME: Do something to check to see how many frames the current animation has
  666. // If we do this, we have to be able to handle the case of changing ShadowCastTypes
  667. // at the moment, they are assumed to be constant.
  668. return SHADOWS_RENDER_TO_TEXTURE;
  669. }
  670. //-----------------------------------------------------------------------------
  671. // Purpose: convert axis rotations to a quaternion
  672. //-----------------------------------------------------------------------------
  673. void C_BaseAnimating::SetPredictable( bool state )
  674. {
  675. BaseClass::SetPredictable( state );
  676. UpdateRelevantInterpolatedVars();
  677. }
  678. //-----------------------------------------------------------------------------
  679. // Purpose: sets client side animation
  680. //-----------------------------------------------------------------------------
  681. void C_BaseAnimating::UseClientSideAnimation()
  682. {
  683. m_bClientSideAnimation = true;
  684. }
  685. void C_BaseAnimating::UpdateRelevantInterpolatedVars()
  686. {
  687. MDLCACHE_CRITICAL_SECTION();
  688. // Remove any interpolated vars that need to be removed.
  689. if ( !IsMarkedForDeletion() && !GetPredictable() && !IsClientCreated() && GetModelPtr() && GetModelPtr()->SequencesAvailable() )
  690. {
  691. AddBaseAnimatingInterpolatedVars();
  692. }
  693. else
  694. {
  695. RemoveBaseAnimatingInterpolatedVars();
  696. }
  697. }
  698. void C_BaseAnimating::AddBaseAnimatingInterpolatedVars()
  699. {
  700. AddVar( m_flEncodedController, &m_iv_flEncodedController, LATCH_ANIMATION_VAR, true );
  701. AddVar( m_flPoseParameter, &m_iv_flPoseParameter, LATCH_ANIMATION_VAR, true );
  702. int flags = LATCH_ANIMATION_VAR;
  703. if ( m_bClientSideAnimation )
  704. flags |= EXCLUDE_AUTO_INTERPOLATE;
  705. AddVar( &m_flCycle, &m_iv_flCycle, flags, true );
  706. }
  707. void C_BaseAnimating::RemoveBaseAnimatingInterpolatedVars()
  708. {
  709. RemoveVar( m_flEncodedController, false );
  710. RemoveVar( m_flPoseParameter, false );
  711. #ifdef HL2MP
  712. // HACK: Don't want to remove interpolation for predictables in hl2dm, though
  713. // The animation state stuff sets the pose parameters -- so they should interp
  714. // but m_flCycle is not touched, so it's only set during prediction (which occurs on tick boundaries)
  715. // and so needs to continue to be interpolated for smooth rendering of the lower body of the local player in third person, etc.
  716. if ( !GetPredictable() )
  717. #endif
  718. {
  719. RemoveVar( &m_flCycle, false );
  720. }
  721. }
  722. /*
  723. From Ken: Lock() and Unlock() are render frame only, its just so the mdlcache
  724. doesnt toss the memory when it reshuffles the data, or at least used to. I
  725. don't have any idea if mdlcache even does that anymore, but at one point it would
  726. happily throw away the animation data if you ran out of memory on the
  727. consoles. Jay adds: Ken is correct and the pointer should be valid until the end
  728. of the frame lock (provided you are within a MDLCACHE_LOCK() block or whatever
  729. Jay also recommends running with a forced small cache size (1MB) to put maximum
  730. pressure on the cache when testing changes. Look for datacache ConVar in datacache.cpp.
  731. */
  732. void C_BaseAnimating::LockStudioHdr()
  733. {
  734. Assert( m_hStudioHdr == MDLHANDLE_INVALID && m_pStudioHdr == NULL );
  735. AUTO_LOCK( m_StudioHdrInitLock );
  736. if ( m_hStudioHdr != MDLHANDLE_INVALID || m_pStudioHdr != NULL )
  737. {
  738. Assert( m_pStudioHdr ? m_pStudioHdr->GetRenderHdr() == mdlcache->GetStudioHdr(m_hStudioHdr) : m_hStudioHdr == MDLHANDLE_INVALID );
  739. return;
  740. }
  741. const model_t *mdl = GetModel();
  742. if ( !mdl )
  743. return;
  744. m_hStudioHdr = modelinfo->GetCacheHandle( mdl );
  745. if ( m_hStudioHdr == MDLHANDLE_INVALID )
  746. return;
  747. const studiohdr_t *pStudioHdr = mdlcache->LockStudioHdr( m_hStudioHdr );
  748. if ( !pStudioHdr )
  749. {
  750. m_hStudioHdr = MDLHANDLE_INVALID;
  751. return;
  752. }
  753. CStudioHdr *pNewWrapper = new CStudioHdr;
  754. pNewWrapper->Init( pStudioHdr, mdlcache );
  755. Assert( pNewWrapper->IsValid() );
  756. if ( pNewWrapper->GetVirtualModel() )
  757. {
  758. MDLHandle_t hVirtualModel = (MDLHandle_t)(int)(pStudioHdr->virtualModel)&0xffff;
  759. mdlcache->LockStudioHdr( hVirtualModel );
  760. }
  761. m_pStudioHdr = pNewWrapper; // must be last to ensure virtual model correctly set up
  762. }
  763. void C_BaseAnimating::UnlockStudioHdr()
  764. {
  765. if ( m_hStudioHdr != MDLHANDLE_INVALID )
  766. {
  767. studiohdr_t *pStudioHdr = mdlcache->GetStudioHdr( m_hStudioHdr );
  768. Assert( m_pStudioHdr && m_pStudioHdr->GetRenderHdr() == pStudioHdr );
  769. #if 0
  770. // XXX need to figure out where to flush the queue on map change to not crash
  771. if ( ICallQueue *pCallQueue = materials->GetRenderContext()->GetCallQueue() )
  772. {
  773. // Parallel rendering: don't unlock model data until end of rendering
  774. if ( pStudioHdr->GetVirtualModel() )
  775. {
  776. MDLHandle_t hVirtualModel = (MDLHandle_t)(int)pStudioHdr->virtualModel&0xffff;
  777. pCallQueue->QueueCall( mdlcache, &IMDLCache::UnlockStudioHdr, hVirtualModel );
  778. }
  779. pCallQueue->QueueCall( mdlcache, &IMDLCache::UnlockStudioHdr, m_hStudioHdr );
  780. }
  781. else
  782. #endif
  783. {
  784. // Immediate-mode rendering, can unlock immediately
  785. if ( pStudioHdr->GetVirtualModel() )
  786. {
  787. MDLHandle_t hVirtualModel = (MDLHandle_t)(int)pStudioHdr->virtualModel&0xffff;
  788. mdlcache->UnlockStudioHdr( hVirtualModel );
  789. }
  790. mdlcache->UnlockStudioHdr( m_hStudioHdr );
  791. }
  792. m_hStudioHdr = MDLHANDLE_INVALID;
  793. delete m_pStudioHdr;
  794. m_pStudioHdr = NULL;
  795. }
  796. }
  797. void C_BaseAnimating::OnModelLoadComplete( const model_t* pModel )
  798. {
  799. Assert( m_bDynamicModelPending && pModel == GetModel() );
  800. if ( m_bDynamicModelPending && pModel == GetModel() )
  801. {
  802. m_bDynamicModelPending = false;
  803. OnNewModel();
  804. UpdateVisibility();
  805. }
  806. }
  807. void C_BaseAnimating::ValidateModelIndex()
  808. {
  809. BaseClass::ValidateModelIndex();
  810. Assert( m_nModelIndex == 0 || m_AutoRefModelIndex.Get() );
  811. }
  812. CStudioHdr *C_BaseAnimating::OnNewModel()
  813. {
  814. InvalidateMdlCache();
  815. // remove transition animations playback
  816. m_SequenceTransitioner.RemoveAll();
  817. if (m_pJiggleBones)
  818. {
  819. delete m_pJiggleBones;
  820. m_pJiggleBones = NULL;
  821. }
  822. if ( m_bDynamicModelPending )
  823. {
  824. modelinfo->UnregisterModelLoadCallback( -1, this );
  825. m_bDynamicModelPending = false;
  826. }
  827. m_AutoRefModelIndex.Clear();
  828. if ( !GetModel() || modelinfo->GetModelType( GetModel() ) != mod_studio )
  829. return NULL;
  830. // Reference (and thus start loading) dynamic model
  831. int nNewIndex = m_nModelIndex;
  832. if ( modelinfo->GetModel( nNewIndex ) != GetModel() )
  833. {
  834. // XXX what's authoritative? the model pointer or the model index? what a mess.
  835. nNewIndex = modelinfo->GetModelIndex( modelinfo->GetModelName( GetModel() ) );
  836. Assert( nNewIndex < 0 || modelinfo->GetModel( nNewIndex ) == GetModel() );
  837. if ( nNewIndex < 0 )
  838. nNewIndex = m_nModelIndex;
  839. }
  840. m_AutoRefModelIndex = nNewIndex;
  841. if ( IsDynamicModelIndex( nNewIndex ) && modelinfo->IsDynamicModelLoading( nNewIndex ) )
  842. {
  843. m_bDynamicModelPending = true;
  844. modelinfo->RegisterModelLoadCallback( nNewIndex, this );
  845. }
  846. if ( IsDynamicModelLoading() )
  847. {
  848. // Called while dynamic model still loading -> new model, clear deferred state
  849. m_bResetSequenceInfoOnLoad = false;
  850. return NULL;
  851. }
  852. CStudioHdr *hdr = GetModelPtr();
  853. if (hdr == NULL)
  854. return NULL;
  855. InvalidateBoneCache();
  856. if ( m_pBoneMergeCache )
  857. {
  858. delete m_pBoneMergeCache;
  859. m_pBoneMergeCache = NULL;
  860. // recreated in BuildTransformations
  861. }
  862. Studio_DestroyBoneCache( m_hitboxBoneCacheHandle );
  863. m_hitboxBoneCacheHandle = 0;
  864. // Make sure m_CachedBones has space.
  865. if ( m_CachedBoneData.Count() != hdr->numbones() )
  866. {
  867. m_CachedBoneData.SetSize( hdr->numbones() );
  868. for ( int i=0; i < hdr->numbones(); i++ )
  869. {
  870. SetIdentityMatrix( m_CachedBoneData[i] );
  871. }
  872. }
  873. m_BoneAccessor.Init( this, m_CachedBoneData.Base() ); // Always call this in case the studiohdr_t has changed.
  874. // Free any IK data
  875. if (m_pIk)
  876. {
  877. delete m_pIk;
  878. m_pIk = NULL;
  879. }
  880. // Don't reallocate unless a different size.
  881. if ( m_Attachments.Count() != hdr->GetNumAttachments() )
  882. {
  883. m_Attachments.SetSize( hdr->GetNumAttachments() );
  884. // This is to make sure we don't use the attachment before its been set up
  885. for ( int i=0; i < m_Attachments.Count(); i++ )
  886. {
  887. m_Attachments[i].m_bAnglesComputed = false;
  888. m_Attachments[i].m_nLastFramecount = 0;
  889. #ifdef _DEBUG
  890. m_Attachments[i].m_AttachmentToWorld.Invalidate();
  891. m_Attachments[i].m_angRotation.Init( VEC_T_NAN, VEC_T_NAN, VEC_T_NAN );
  892. m_Attachments[i].m_vOriginVelocity.Init( VEC_T_NAN, VEC_T_NAN, VEC_T_NAN );
  893. #endif
  894. }
  895. }
  896. Assert( hdr->GetNumPoseParameters() <= ARRAYSIZE( m_flPoseParameter ) );
  897. m_iv_flPoseParameter.SetMaxCount( hdr->GetNumPoseParameters() );
  898. int i;
  899. for ( i = 0; i < hdr->GetNumPoseParameters() ; i++ )
  900. {
  901. const mstudioposeparamdesc_t &Pose = hdr->pPoseParameter( i );
  902. m_iv_flPoseParameter.SetLooping( Pose.loop != 0.0f, i );
  903. // Note: We can't do this since if we get a DATA_UPDATE_CREATED (i.e., new entity) with both a new model and some valid pose parameters this will slam the
  904. // pose parameters to zero and if the model goes dormant the pose parameter field will never be set to the true value. We shouldn't have to zero these out
  905. // as they are under the control of the server and should be properly set
  906. if ( !IsServerEntity() )
  907. {
  908. SetPoseParameter( hdr, i, 0.0 );
  909. }
  910. }
  911. int boneControllerCount = MIN( hdr->numbonecontrollers(), ARRAYSIZE( m_flEncodedController ) );
  912. m_iv_flEncodedController.SetMaxCount( boneControllerCount );
  913. for ( i = 0; i < boneControllerCount ; i++ )
  914. {
  915. bool loop = (hdr->pBonecontroller( i )->type & (STUDIO_XR | STUDIO_YR | STUDIO_ZR)) != 0;
  916. m_iv_flEncodedController.SetLooping( loop, i );
  917. SetBoneController( i, 0.0 );
  918. }
  919. InitModelEffects();
  920. // lookup generic eye attachment, if exists
  921. m_iEyeAttachment = LookupAttachment( "eyes" );
  922. // If we didn't have a model before, then we might need to go in the interpolation list now.
  923. if ( ShouldInterpolate() )
  924. AddToInterpolationList();
  925. // objects with attachment points need to be queryable even if they're not solid
  926. if ( hdr->GetNumAttachments() != 0 )
  927. {
  928. AddEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
  929. }
  930. // Most entities clear out their sequences when they change models on the server, but
  931. // not all entities network down their m_nSequence (like multiplayer game player entities),
  932. // so we may need to clear it out here. Force a SetSequence call no matter what, though.
  933. int forceSequence = ShouldResetSequenceOnNewModel() ? 0 : m_nSequence;
  934. if ( GetSequence() >= hdr->GetNumSeq() )
  935. {
  936. forceSequence = 0;
  937. }
  938. m_nSequence = -1;
  939. SetSequence( forceSequence );
  940. if ( m_bResetSequenceInfoOnLoad )
  941. {
  942. m_bResetSequenceInfoOnLoad = false;
  943. ResetSequenceInfo();
  944. }
  945. UpdateRelevantInterpolatedVars();
  946. return hdr;
  947. }
  948. //-----------------------------------------------------------------------------
  949. // Purpose: Returns index number of a given named bone
  950. // Input : name of a bone
  951. // Output : Bone index number or -1 if bone not found
  952. //-----------------------------------------------------------------------------
  953. int C_BaseAnimating::LookupBone( const char *szName )
  954. {
  955. Assert( GetModelPtr() );
  956. return Studio_BoneIndexByName( GetModelPtr(), szName );
  957. }
  958. //=========================================================
  959. //=========================================================
  960. void C_BaseAnimating::GetBonePosition ( int iBone, Vector &origin, QAngle &angles )
  961. {
  962. matrix3x4_t bonetoworld;
  963. GetBoneTransform( iBone, bonetoworld );
  964. MatrixAngles( bonetoworld, angles, origin );
  965. }
  966. void C_BaseAnimating::GetBoneTransform( int iBone, matrix3x4_t &pBoneToWorld )
  967. {
  968. Assert( GetModelPtr() && iBone >= 0 && iBone < GetModelPtr()->numbones() );
  969. CBoneCache *pcache = GetBoneCache( NULL );
  970. matrix3x4_t *pmatrix = pcache->GetCachedBone( iBone );
  971. if ( !pmatrix )
  972. {
  973. MatrixCopy( EntityToWorldTransform(), pBoneToWorld );
  974. return;
  975. }
  976. Assert( pmatrix );
  977. // FIXME
  978. MatrixCopy( *pmatrix, pBoneToWorld );
  979. }
  980. //=============================================================================
  981. // HPE_BEGIN:
  982. // [menglish] Finds the bone associated with the given hitbox
  983. //=============================================================================
  984. int C_BaseAnimating::GetHitboxBone( int hitboxIndex )
  985. {
  986. CStudioHdr *pStudioHdr = GetModelPtr();
  987. if ( pStudioHdr )
  988. {
  989. mstudiohitboxset_t *set =pStudioHdr->pHitboxSet( m_nHitboxSet );
  990. if ( set && hitboxIndex < set->numhitboxes )
  991. {
  992. return set->pHitbox( hitboxIndex )->bone;
  993. }
  994. }
  995. return 0;
  996. }
  997. //=============================================================================
  998. // HPE_END
  999. //=============================================================================
  1000. //-----------------------------------------------------------------------------
  1001. // Purpose: Setup to initialize our model effects once the model's loaded
  1002. //-----------------------------------------------------------------------------
  1003. void C_BaseAnimating::InitModelEffects( void )
  1004. {
  1005. m_bInitModelEffects = true;
  1006. m_bDelayInitModelEffects = true;
  1007. TermRopes();
  1008. ParticleProp()->StopParticlesInvolving( this );
  1009. m_bHasAttachedParticles = false;
  1010. }
  1011. //-----------------------------------------------------------------------------
  1012. // Purpose: Load the model's keyvalues section and create effects listed inside it
  1013. //-----------------------------------------------------------------------------
  1014. void C_BaseAnimating::DelayedInitModelEffects( void )
  1015. {
  1016. // don't create the effect if we're not visible
  1017. if ( !ShouldDraw() )
  1018. return;
  1019. m_bDelayInitModelEffects = false;
  1020. // Parse the keyvalues and see if they want to make ropes on this model.
  1021. KeyValues * modelKeyValues = new KeyValues("");
  1022. if ( modelKeyValues->LoadFromBuffer( modelinfo->GetModelName( GetModel() ), modelinfo->GetModelKeyValueText( GetModel() ) ) )
  1023. {
  1024. // Do we have a cables section?
  1025. KeyValues *pkvAllCables = modelKeyValues->FindKey("Cables");
  1026. if ( pkvAllCables )
  1027. {
  1028. // Start grabbing the sounds and slotting them in
  1029. for ( KeyValues *pSingleCable = pkvAllCables->GetFirstSubKey(); pSingleCable; pSingleCable = pSingleCable->GetNextKey() )
  1030. {
  1031. C_RopeKeyframe *pRope = C_RopeKeyframe::CreateFromKeyValues( this, pSingleCable );
  1032. m_Ropes.AddToTail( pRope );
  1033. }
  1034. }
  1035. if ( !m_bNoModelParticles )
  1036. {
  1037. // Do we have a particles section?
  1038. KeyValues *pkvAllParticleEffects = modelKeyValues->FindKey("Particles");
  1039. if ( pkvAllParticleEffects )
  1040. {
  1041. // Start grabbing the sounds and slotting them in
  1042. for ( KeyValues *pSingleEffect = pkvAllParticleEffects->GetFirstSubKey(); pSingleEffect; pSingleEffect = pSingleEffect->GetNextKey() )
  1043. {
  1044. const char *pszParticleEffect = pSingleEffect->GetString( "name", "" );
  1045. const char *pszAttachment = pSingleEffect->GetString( "attachment_point", "" );
  1046. const char *pszAttachType = pSingleEffect->GetString( "attachment_type", "" );
  1047. // Convert attach type
  1048. int iAttachType = GetAttachTypeFromString( pszAttachType );
  1049. if ( iAttachType == -1 )
  1050. {
  1051. Warning("Invalid attach type specified for particle effect in model '%s' keyvalues section. Trying to spawn effect '%s' with attach type of '%s'\n", GetModelName(), pszParticleEffect, pszAttachType );
  1052. return;
  1053. }
  1054. // Convert attachment point
  1055. int iAttachment = atoi(pszAttachment);
  1056. // See if we can find any attachment points matching the name
  1057. if ( pszAttachment[0] != '0' && iAttachment == 0 )
  1058. {
  1059. iAttachment = LookupAttachment( pszAttachment );
  1060. if ( iAttachment <= 0 )
  1061. {
  1062. Warning("Failed to find attachment point specified for particle effect in model '%s' keyvalues section. Trying to spawn effect '%s' on attachment named '%s'\n", GetModelName(), pszParticleEffect, pszAttachment );
  1063. return;
  1064. }
  1065. }
  1066. #ifdef TF_CLIENT_DLL
  1067. // Halloween Hack for Sentry Rockets
  1068. if ( !V_strcmp( "sentry_rocket", pszParticleEffect ) )
  1069. {
  1070. // Halloween Spell Effect Check
  1071. int iHalloweenSpell = 0;
  1072. if ( TF_IsHolidayActive( kHoliday_HalloweenOrFullMoon ) )
  1073. {
  1074. // if the owner is a Sentry, Check its owner
  1075. if ( GetOwnerEntity() && GetOwnerEntity()->IsBaseObject() )
  1076. {
  1077. CBaseObject *pSentry = assert_cast< CBaseObject* >( GetOwnerEntity() );
  1078. CALL_ATTRIB_HOOK_INT_ON_OTHER( pSentry->GetOwner(), iHalloweenSpell, halloween_pumpkin_explosions );
  1079. }
  1080. else
  1081. {
  1082. CALL_ATTRIB_HOOK_INT_ON_OTHER( GetOwnerEntity(), iHalloweenSpell, halloween_pumpkin_explosions );
  1083. }
  1084. }
  1085. if ( iHalloweenSpell > 0 )
  1086. {
  1087. pszParticleEffect = "halloween_rockettrail";
  1088. }
  1089. }
  1090. #endif // TF_CLIENT_DLL
  1091. // Spawn the particle effect
  1092. ParticleProp()->Create( pszParticleEffect, (ParticleAttachment_t)iAttachType, iAttachment );
  1093. m_bHasAttachedParticles = true;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. modelKeyValues->deleteThis();
  1099. }
  1100. void C_BaseAnimating::TermRopes()
  1101. {
  1102. FOR_EACH_LL( m_Ropes, i )
  1103. m_Ropes[i]->Release();
  1104. m_Ropes.Purge();
  1105. }
  1106. // FIXME: redundant?
  1107. void C_BaseAnimating::GetBoneControllers(float controllers[MAXSTUDIOBONECTRLS])
  1108. {
  1109. // interpolate two 0..1 encoded controllers to a single 0..1 controller
  1110. int i;
  1111. for( i=0; i < MAXSTUDIOBONECTRLS; i++)
  1112. {
  1113. controllers[ i ] = m_flEncodedController[ i ];
  1114. }
  1115. }
  1116. float C_BaseAnimating::GetPoseParameter( int iPoseParameter )
  1117. {
  1118. CStudioHdr *pStudioHdr = GetModelPtr();
  1119. if ( pStudioHdr == NULL )
  1120. return 0.0f;
  1121. if ( pStudioHdr->GetNumPoseParameters() < iPoseParameter )
  1122. return 0.0f;
  1123. if ( iPoseParameter < 0 )
  1124. return 0.0f;
  1125. return m_flPoseParameter[iPoseParameter];
  1126. }
  1127. // FIXME: redundant?
  1128. void C_BaseAnimating::GetPoseParameters( CStudioHdr *pStudioHdr, float poseParameter[MAXSTUDIOPOSEPARAM])
  1129. {
  1130. if ( !pStudioHdr )
  1131. return;
  1132. // interpolate pose parameters
  1133. int i;
  1134. for( i=0; i < pStudioHdr->GetNumPoseParameters(); i++)
  1135. {
  1136. poseParameter[i] = m_flPoseParameter[i];
  1137. }
  1138. #if 0 // _DEBUG
  1139. if (/* Q_stristr( pStudioHdr->pszName(), r_sequence_debug.GetString()) != NULL || */ r_sequence_debug.GetInt() == entindex())
  1140. {
  1141. DevMsgRT( "%s\n", pStudioHdr->pszName() );
  1142. DevMsgRT( "%6.2f : ", gpGlobals->curtime );
  1143. for( i=0; i < pStudioHdr->GetNumPoseParameters(); i++)
  1144. {
  1145. const mstudioposeparamdesc_t &Pose = pStudioHdr->pPoseParameter( i );
  1146. DevMsgRT( "%s %6.2f ", Pose.pszName(), poseParameter[i] * Pose.end + (1 - poseParameter[i]) * Pose.start );
  1147. }
  1148. DevMsgRT( "\n" );
  1149. }
  1150. #endif
  1151. }
  1152. float C_BaseAnimating::ClampCycle( float flCycle, bool isLooping )
  1153. {
  1154. if (isLooping)
  1155. {
  1156. // FIXME: does this work with negative framerate?
  1157. flCycle -= (int)flCycle;
  1158. if (flCycle < 0.0f)
  1159. {
  1160. flCycle += 1.0f;
  1161. }
  1162. }
  1163. else
  1164. {
  1165. flCycle = clamp( flCycle, 0.0f, 0.999f );
  1166. }
  1167. return flCycle;
  1168. }
  1169. void C_BaseAnimating::GetCachedBoneMatrix( int boneIndex, matrix3x4_t &out )
  1170. {
  1171. MatrixCopy( GetBone( boneIndex ), out );
  1172. }
  1173. //-----------------------------------------------------------------------------
  1174. // Purpose: move position and rotation transforms into global matrices
  1175. //-----------------------------------------------------------------------------
  1176. void C_BaseAnimating::BuildTransformations( CStudioHdr *hdr, Vector *pos, Quaternion *q, const matrix3x4_t &cameraTransform, int boneMask, CBoneBitList &boneComputed )
  1177. {
  1178. VPROF_BUDGET( "C_BaseAnimating::BuildTransformations", VPROF_BUDGETGROUP_CLIENT_ANIMATION );
  1179. if ( !hdr )
  1180. return;
  1181. matrix3x4_t bonematrix;
  1182. bool boneSimulated[MAXSTUDIOBONES];
  1183. // no bones have been simulated
  1184. memset( boneSimulated, 0, sizeof(boneSimulated) );
  1185. mstudiobone_t *pbones = hdr->pBone( 0 );
  1186. if ( m_pRagdoll )
  1187. {
  1188. // simulate bones and update flags
  1189. int oldWritableBones = m_BoneAccessor.GetWritableBones();
  1190. int oldReadableBones = m_BoneAccessor.GetReadableBones();
  1191. m_BoneAccessor.SetWritableBones( BONE_USED_BY_ANYTHING );
  1192. m_BoneAccessor.SetReadableBones( BONE_USED_BY_ANYTHING );
  1193. #if defined( REPLAY_ENABLED )
  1194. // If we're playing back a demo, override the ragdoll bones with cached version if available - otherwise, simulate.
  1195. if ( ( !engine->IsPlayingDemo() && !engine->IsPlayingTimeDemo() ) ||
  1196. !CReplayRagdollCache::Instance().IsInitialized() ||
  1197. !CReplayRagdollCache::Instance().GetFrame( this, engine->GetDemoPlaybackTick(), boneSimulated, &m_BoneAccessor ) )
  1198. #endif
  1199. {
  1200. m_pRagdoll->RagdollBone( this, pbones, hdr->numbones(), boneSimulated, m_BoneAccessor );
  1201. }
  1202. m_BoneAccessor.SetWritableBones( oldWritableBones );
  1203. m_BoneAccessor.SetReadableBones( oldReadableBones );
  1204. }
  1205. // For EF_BONEMERGE entities, copy the bone matrices for any bones that have matching names.
  1206. bool boneMerge = IsEffectActive(EF_BONEMERGE);
  1207. if ( boneMerge || m_pBoneMergeCache )
  1208. {
  1209. if ( boneMerge )
  1210. {
  1211. if ( !m_pBoneMergeCache )
  1212. {
  1213. m_pBoneMergeCache = new CBoneMergeCache;
  1214. m_pBoneMergeCache->Init( this );
  1215. }
  1216. m_pBoneMergeCache->MergeMatchingBones( boneMask );
  1217. }
  1218. else
  1219. {
  1220. delete m_pBoneMergeCache;
  1221. m_pBoneMergeCache = NULL;
  1222. }
  1223. }
  1224. for (int i = 0; i < hdr->numbones(); i++)
  1225. {
  1226. // Only update bones reference by the bone mask.
  1227. if ( !( hdr->boneFlags( i ) & boneMask ) )
  1228. {
  1229. continue;
  1230. }
  1231. if ( m_pBoneMergeCache && m_pBoneMergeCache->IsBoneMerged( i ) )
  1232. continue;
  1233. // animate all non-simulated bones
  1234. if ( boneSimulated[i] || CalcProceduralBone( hdr, i, m_BoneAccessor ))
  1235. {
  1236. continue;
  1237. }
  1238. // skip bones that the IK has already setup
  1239. else if (boneComputed.IsBoneMarked( i ))
  1240. {
  1241. // dummy operation, just used to verify in debug that this should have happened
  1242. GetBoneForWrite( i );
  1243. }
  1244. else
  1245. {
  1246. QuaternionMatrix( q[i], pos[i], bonematrix );
  1247. Assert( fabs( pos[i].x ) < 100000 );
  1248. Assert( fabs( pos[i].y ) < 100000 );
  1249. Assert( fabs( pos[i].z ) < 100000 );
  1250. if ( (hdr->boneFlags( i ) & BONE_ALWAYS_PROCEDURAL) &&
  1251. (hdr->pBone( i )->proctype & STUDIO_PROC_JIGGLE) )
  1252. {
  1253. //
  1254. // Physics-based "jiggle" bone
  1255. // Bone is assumed to be along the Z axis
  1256. // Pitch around X, yaw around Y
  1257. //
  1258. // compute desired bone orientation
  1259. matrix3x4_t goalMX;
  1260. if (pbones[i].parent == -1)
  1261. {
  1262. ConcatTransforms( cameraTransform, bonematrix, goalMX );
  1263. }
  1264. else
  1265. {
  1266. // If the parent bone has been scaled (like with BuildBigHeadTransformations)
  1267. // scale it back down so the jiggly bones show up non-scaled in the correct location.
  1268. matrix3x4_t parentMX = GetBone( pbones[i].parent );
  1269. float fScale = Square( parentMX[0][0] ) + Square( parentMX[1][0] ) + Square( parentMX[2][0] );
  1270. if ( fScale > Square( 1.0001f ) )
  1271. {
  1272. fScale = 1.0f / FastSqrt( fScale );
  1273. MatrixScaleBy( fScale, parentMX );
  1274. }
  1275. ConcatTransforms( parentMX, bonematrix, goalMX );
  1276. }
  1277. // get jiggle properties from QC data
  1278. mstudiojigglebone_t *jiggleInfo = (mstudiojigglebone_t *)pbones[i].pProcedure( );
  1279. if (!m_pJiggleBones)
  1280. {
  1281. m_pJiggleBones = new CJiggleBones;
  1282. }
  1283. // do jiggle physics
  1284. m_pJiggleBones->BuildJiggleTransformations( i, gpGlobals->realtime, jiggleInfo, goalMX, GetBoneForWrite( i ) );
  1285. }
  1286. else if (hdr->boneParent(i) == -1)
  1287. {
  1288. ConcatTransforms( cameraTransform, bonematrix, GetBoneForWrite( i ) );
  1289. }
  1290. else
  1291. {
  1292. ConcatTransforms( GetBone( hdr->boneParent(i) ), bonematrix, GetBoneForWrite( i ) );
  1293. }
  1294. }
  1295. if (hdr->boneParent(i) == -1)
  1296. {
  1297. // Apply client-side effects to the transformation matrix
  1298. ApplyBoneMatrixTransform( GetBoneForWrite( i ) );
  1299. }
  1300. }
  1301. }
  1302. //-----------------------------------------------------------------------------
  1303. // Purpose: Special effects
  1304. // Input : transform -
  1305. //-----------------------------------------------------------------------------
  1306. void C_BaseAnimating::ApplyBoneMatrixTransform( matrix3x4_t& transform )
  1307. {
  1308. switch( m_nRenderFX )
  1309. {
  1310. case kRenderFxDistort:
  1311. case kRenderFxHologram:
  1312. if ( RandomInt(0,49) == 0 )
  1313. {
  1314. int axis = RandomInt(0,1);
  1315. if ( axis == 1 ) // Choose between x & z
  1316. axis = 2;
  1317. VectorScale( transform[axis], RandomFloat(1,1.484), transform[axis] );
  1318. }
  1319. else if ( RandomInt(0,49) == 0 )
  1320. {
  1321. float offset;
  1322. int axis = RandomInt(0,1);
  1323. if ( axis == 1 ) // Choose between x & z
  1324. axis = 2;
  1325. offset = RandomFloat(-10,10);
  1326. transform[RandomInt(0,2)][3] += offset;
  1327. }
  1328. break;
  1329. case kRenderFxExplode:
  1330. {
  1331. float scale;
  1332. scale = 1.0 + (gpGlobals->curtime - m_flAnimTime) * 10.0;
  1333. if ( scale > 2 ) // Don't blow up more than 200%
  1334. scale = 2;
  1335. transform[0][1] *= scale;
  1336. transform[1][1] *= scale;
  1337. transform[2][1] *= scale;
  1338. }
  1339. break;
  1340. default:
  1341. break;
  1342. }
  1343. if ( IsModelScaled() )
  1344. {
  1345. // The bone transform is in worldspace, so to scale this, we need to translate it back
  1346. float scale = GetModelScale();
  1347. Vector pos;
  1348. MatrixGetColumn( transform, 3, pos );
  1349. pos -= GetRenderOrigin();
  1350. pos *= scale;
  1351. pos += GetRenderOrigin();
  1352. MatrixSetColumn( pos, 3, transform );
  1353. VectorScale( transform[0], scale, transform[0] );
  1354. VectorScale( transform[1], scale, transform[1] );
  1355. VectorScale( transform[2], scale, transform[2] );
  1356. }
  1357. }
  1358. void C_BaseAnimating::CreateUnragdollInfo( C_BaseAnimating *pRagdoll )
  1359. {
  1360. CStudioHdr *hdr = GetModelPtr();
  1361. if ( !hdr )
  1362. {
  1363. return;
  1364. }
  1365. // It's already an active ragdoll, sigh
  1366. if ( m_pRagdollInfo && m_pRagdollInfo->m_bActive )
  1367. {
  1368. Assert( 0 );
  1369. return;
  1370. }
  1371. // Now do the current bone setup
  1372. pRagdoll->SetupBones( NULL, -1, BONE_USED_BY_ANYTHING, gpGlobals->curtime );
  1373. matrix3x4_t parentTransform;
  1374. QAngle newAngles( 0, pRagdoll->GetAbsAngles()[YAW], 0 );
  1375. AngleMatrix( GetAbsAngles(), GetAbsOrigin(), parentTransform );
  1376. // pRagdoll->SaveRagdollInfo( hdr->numbones, parentTransform, m_BoneAccessor );
  1377. if ( !m_pRagdollInfo )
  1378. {
  1379. m_pRagdollInfo = new RagdollInfo_t;
  1380. Assert( m_pRagdollInfo );
  1381. if ( !m_pRagdollInfo )
  1382. {
  1383. Msg( "Memory allocation of RagdollInfo_t failed!\n" );
  1384. return;
  1385. }
  1386. }
  1387. Q_memset( m_pRagdollInfo, 0, sizeof( *m_pRagdollInfo ) );
  1388. int numbones = hdr->numbones();
  1389. m_pRagdollInfo->m_bActive = true;
  1390. m_pRagdollInfo->m_flSaveTime = gpGlobals->curtime;
  1391. m_pRagdollInfo->m_nNumBones = numbones;
  1392. for ( int i = 0; i < numbones; i++ )
  1393. {
  1394. matrix3x4_t inverted;
  1395. matrix3x4_t output;
  1396. if ( hdr->boneParent(i) == -1 )
  1397. {
  1398. // Decompose into parent space
  1399. MatrixInvert( parentTransform, inverted );
  1400. }
  1401. else
  1402. {
  1403. MatrixInvert( pRagdoll->m_BoneAccessor.GetBone( hdr->boneParent(i) ), inverted );
  1404. }
  1405. ConcatTransforms( inverted, pRagdoll->m_BoneAccessor.GetBone( i ), output );
  1406. MatrixAngles( output,
  1407. m_pRagdollInfo->m_rgBoneQuaternion[ i ],
  1408. m_pRagdollInfo->m_rgBonePos[ i ] );
  1409. }
  1410. }
  1411. void C_BaseAnimating::SaveRagdollInfo( int numbones, const matrix3x4_t &cameraTransform, CBoneAccessor &pBoneToWorld )
  1412. {
  1413. CStudioHdr *hdr = GetModelPtr();
  1414. if ( !hdr )
  1415. {
  1416. return;
  1417. }
  1418. if ( !m_pRagdollInfo )
  1419. {
  1420. m_pRagdollInfo = new RagdollInfo_t;
  1421. Assert( m_pRagdollInfo );
  1422. if ( !m_pRagdollInfo )
  1423. {
  1424. Msg( "Memory allocation of RagdollInfo_t failed!\n" );
  1425. return;
  1426. }
  1427. memset( m_pRagdollInfo, 0, sizeof( *m_pRagdollInfo ) );
  1428. }
  1429. mstudiobone_t *pbones = hdr->pBone( 0 );
  1430. m_pRagdollInfo->m_bActive = true;
  1431. m_pRagdollInfo->m_flSaveTime = gpGlobals->curtime;
  1432. m_pRagdollInfo->m_nNumBones = numbones;
  1433. for ( int i = 0; i < numbones; i++ )
  1434. {
  1435. matrix3x4_t inverted;
  1436. matrix3x4_t output;
  1437. if ( pbones[i].parent == -1 )
  1438. {
  1439. // Decompose into parent space
  1440. MatrixInvert( cameraTransform, inverted );
  1441. }
  1442. else
  1443. {
  1444. MatrixInvert( pBoneToWorld.GetBone( pbones[ i ].parent ), inverted );
  1445. }
  1446. ConcatTransforms( inverted, pBoneToWorld.GetBone( i ), output );
  1447. MatrixAngles( output,
  1448. m_pRagdollInfo->m_rgBoneQuaternion[ i ],
  1449. m_pRagdollInfo->m_rgBonePos[ i ] );
  1450. }
  1451. }
  1452. bool C_BaseAnimating::RetrieveRagdollInfo( Vector *pos, Quaternion *q )
  1453. {
  1454. if ( !m_bStoreRagdollInfo || !m_pRagdollInfo || !m_pRagdollInfo->m_bActive )
  1455. return false;
  1456. for ( int i = 0; i < m_pRagdollInfo->m_nNumBones; i++ )
  1457. {
  1458. pos[ i ] = m_pRagdollInfo->m_rgBonePos[ i ];
  1459. q[ i ] = m_pRagdollInfo->m_rgBoneQuaternion[ i ];
  1460. }
  1461. return true;
  1462. }
  1463. //-----------------------------------------------------------------------------
  1464. // Should we collide?
  1465. //-----------------------------------------------------------------------------
  1466. CollideType_t C_BaseAnimating::GetCollideType( void )
  1467. {
  1468. if ( IsRagdoll() )
  1469. return ENTITY_SHOULD_RESPOND;
  1470. return BaseClass::GetCollideType();
  1471. }
  1472. //-----------------------------------------------------------------------------
  1473. // Purpose: if the active sequence changes, keep track of the previous ones and decay them based on their decay rate
  1474. //-----------------------------------------------------------------------------
  1475. void C_BaseAnimating::MaintainSequenceTransitions( IBoneSetup &boneSetup, float flCycle, Vector pos[], Quaternion q[] )
  1476. {
  1477. VPROF( "C_BaseAnimating::MaintainSequenceTransitions" );
  1478. if ( !boneSetup.GetStudioHdr() )
  1479. return;
  1480. if ( prediction->InPrediction() )
  1481. {
  1482. m_nPrevNewSequenceParity = m_nNewSequenceParity;
  1483. return;
  1484. }
  1485. m_SequenceTransitioner.CheckForSequenceChange(
  1486. boneSetup.GetStudioHdr(),
  1487. GetSequence(),
  1488. m_nNewSequenceParity != m_nPrevNewSequenceParity,
  1489. !IsNoInterpolationFrame()
  1490. );
  1491. m_nPrevNewSequenceParity = m_nNewSequenceParity;
  1492. // Update the transition sequence list.
  1493. m_SequenceTransitioner.UpdateCurrent(
  1494. boneSetup.GetStudioHdr(),
  1495. GetSequence(),
  1496. flCycle,
  1497. m_flPlaybackRate,
  1498. gpGlobals->curtime
  1499. );
  1500. // process previous sequences
  1501. for (int i = m_SequenceTransitioner.m_animationQueue.Count() - 2; i >= 0; i--)
  1502. {
  1503. C_AnimationLayer *blend = &m_SequenceTransitioner.m_animationQueue[i];
  1504. float dt = (gpGlobals->curtime - blend->m_flLayerAnimtime);
  1505. flCycle = blend->m_flCycle + dt * blend->m_flPlaybackRate * GetSequenceCycleRate( boneSetup.GetStudioHdr(), blend->m_nSequence );
  1506. flCycle = ClampCycle( flCycle, IsSequenceLooping( boneSetup.GetStudioHdr(), blend->m_nSequence ) );
  1507. #if 1 // _DEBUG
  1508. if (/*Q_stristr( hdr->pszName(), r_sequence_debug.GetString()) != NULL || */ r_sequence_debug.GetInt() == entindex())
  1509. {
  1510. DevMsgRT( "%8.4f : %30s : %5.3f : %4.2f +\n", gpGlobals->curtime, boneSetup.GetStudioHdr()->pSeqdesc( blend->m_nSequence ).pszLabel(), flCycle, (float)blend->m_flWeight );
  1511. }
  1512. #endif
  1513. boneSetup.AccumulatePose( pos, q, blend->m_nSequence, flCycle, blend->m_flWeight, gpGlobals->curtime, m_pIk );
  1514. }
  1515. }
  1516. //-----------------------------------------------------------------------------
  1517. // Purpose:
  1518. // Input : *hdr -
  1519. // pos[] -
  1520. // q[] -
  1521. //-----------------------------------------------------------------------------
  1522. void C_BaseAnimating::UnragdollBlend( CStudioHdr *hdr, Vector pos[], Quaternion q[], float currentTime )
  1523. {
  1524. if ( !hdr )
  1525. {
  1526. return;
  1527. }
  1528. if ( !m_pRagdollInfo || !m_pRagdollInfo->m_bActive )
  1529. return;
  1530. float dt = currentTime - m_pRagdollInfo->m_flSaveTime;
  1531. if ( dt > 0.2f )
  1532. {
  1533. m_pRagdollInfo->m_bActive = false;
  1534. return;
  1535. }
  1536. // Slerp bone sets together
  1537. float frac = dt / 0.2f;
  1538. frac = clamp( frac, 0.0f, 1.0f );
  1539. int i;
  1540. for ( i = 0; i < hdr->numbones(); i++ )
  1541. {
  1542. VectorLerp( m_pRagdollInfo->m_rgBonePos[ i ], pos[ i ], frac, pos[ i ] );
  1543. QuaternionSlerp( m_pRagdollInfo->m_rgBoneQuaternion[ i ], q[ i ], frac, q[ i ] );
  1544. }
  1545. }
  1546. void C_BaseAnimating::AccumulateLayers( IBoneSetup &boneSetup, Vector pos[], Quaternion q[], float currentTime )
  1547. {
  1548. // Nothing here
  1549. }
  1550. void C_BaseAnimating::ChildLayerBlend( Vector pos[], Quaternion q[], float currentTime, int boneMask )
  1551. {
  1552. return;
  1553. Vector childPos[MAXSTUDIOBONES];
  1554. Quaternion childQ[MAXSTUDIOBONES];
  1555. float childPoseparam[MAXSTUDIOPOSEPARAM];
  1556. // go through all children
  1557. for ( C_BaseEntity *pChild = FirstMoveChild(); pChild; pChild = pChild->NextMovePeer() )
  1558. {
  1559. C_BaseAnimating *pChildAnimating = pChild->GetBaseAnimating();
  1560. if ( pChildAnimating )
  1561. {
  1562. CStudioHdr *pChildHdr = pChildAnimating->GetModelPtr();
  1563. // FIXME: needs a new type of EF_BONEMERGE (EF_CHILDMERGE?)
  1564. if ( pChildHdr && pChild->IsEffectActive( EF_BONEMERGE ) && pChildHdr->SequencesAvailable() && pChildAnimating->m_pBoneMergeCache )
  1565. {
  1566. // FIXME: these should Inherit from the parent
  1567. GetPoseParameters( pChildHdr, childPoseparam );
  1568. IBoneSetup childBoneSetup( pChildHdr, boneMask, childPoseparam );
  1569. childBoneSetup.InitPose( childPos, childQ );
  1570. // set up the child into the parent's current pose
  1571. pChildAnimating->m_pBoneMergeCache->CopyParentToChild( pos, q, childPos, childQ, boneMask );
  1572. // FIXME: needs some kind of sequence
  1573. // merge over whatever bones the childs sequence modifies
  1574. childBoneSetup.AccumulatePose( childPos, childQ, 0, GetCycle(), 1.0, currentTime, NULL );
  1575. // copy the result back into the parents bones
  1576. pChildAnimating->m_pBoneMergeCache->CopyChildToParent( childPos, childQ, pos, q, boneMask );
  1577. // probably needs an IK merge system of some sort =(
  1578. }
  1579. }
  1580. }
  1581. }
  1582. //-----------------------------------------------------------------------------
  1583. // Purpose: Do the default sequence blending rules as done in HL1
  1584. //-----------------------------------------------------------------------------
  1585. void C_BaseAnimating::StandardBlendingRules( CStudioHdr *hdr, Vector pos[], Quaternion q[], float currentTime, int boneMask )
  1586. {
  1587. VPROF( "C_BaseAnimating::StandardBlendingRules" );
  1588. float poseparam[MAXSTUDIOPOSEPARAM];
  1589. if ( !hdr )
  1590. return;
  1591. if ( !hdr->SequencesAvailable() )
  1592. {
  1593. return;
  1594. }
  1595. if (GetSequence() >= hdr->GetNumSeq() || GetSequence() == -1 )
  1596. {
  1597. SetSequence( 0 );
  1598. }
  1599. GetPoseParameters( hdr, poseparam );
  1600. // build root animation
  1601. float fCycle = GetCycle();
  1602. #if 1 //_DEBUG
  1603. if (/* Q_stristr( hdr->pszName(), r_sequence_debug.GetString()) != NULL || */ r_sequence_debug.GetInt() == entindex())
  1604. {
  1605. DevMsgRT( "%8.4f : %30s : %5.3f : %4.2f\n", currentTime, hdr->pSeqdesc( GetSequence() ).pszLabel(), fCycle, 1.0 );
  1606. }
  1607. #endif
  1608. IBoneSetup boneSetup( hdr, boneMask, poseparam );
  1609. boneSetup.InitPose( pos, q );
  1610. boneSetup.AccumulatePose( pos, q, GetSequence(), fCycle, 1.0, currentTime, m_pIk );
  1611. // debugoverlay->AddTextOverlay( GetAbsOrigin() + Vector( 0, 0, 64 ), 0, 0, "%30s %6.2f : %6.2f", hdr->pSeqdesc( GetSequence() )->pszLabel( ), fCycle, 1.0 );
  1612. MaintainSequenceTransitions( boneSetup, fCycle, pos, q );
  1613. AccumulateLayers( boneSetup, pos, q, currentTime );
  1614. CIKContext auto_ik;
  1615. auto_ik.Init( hdr, GetRenderAngles(), GetRenderOrigin(), currentTime, gpGlobals->framecount, boneMask );
  1616. boneSetup.CalcAutoplaySequences( pos, q, currentTime, &auto_ik );
  1617. if ( hdr->numbonecontrollers() )
  1618. {
  1619. float controllers[MAXSTUDIOBONECTRLS];
  1620. GetBoneControllers(controllers);
  1621. boneSetup.CalcBoneAdj( pos, q, controllers );
  1622. }
  1623. ChildLayerBlend( pos, q, currentTime, boneMask );
  1624. UnragdollBlend( hdr, pos, q, currentTime );
  1625. #ifdef STUDIO_ENABLE_PERF_COUNTERS
  1626. #if _DEBUG
  1627. if (Q_stristr( hdr->pszName(), r_sequence_debug.GetString()) != NULL)
  1628. {
  1629. DevMsgRT( "layers %4d : bones %4d : animated %4d\n", hdr->m_nPerfAnimationLayers, hdr->m_nPerfUsedBones, hdr->m_nPerfAnimatedBones );
  1630. }
  1631. #endif
  1632. #endif
  1633. }
  1634. //-----------------------------------------------------------------------------
  1635. // Purpose: Put a value into an attachment point by index
  1636. // Input : number - which point
  1637. // Output : float * - the attachment point
  1638. //-----------------------------------------------------------------------------
  1639. bool C_BaseAnimating::PutAttachment( int number, const matrix3x4_t &attachmentToWorld )
  1640. {
  1641. if ( number < 1 || number > m_Attachments.Count() )
  1642. return false;
  1643. CAttachmentData *pAtt = &m_Attachments[number-1];
  1644. if ( gpGlobals->frametime > 0 && pAtt->m_nLastFramecount > 0 && pAtt->m_nLastFramecount == gpGlobals->framecount - 1 )
  1645. {
  1646. Vector vecPreviousOrigin, vecOrigin;
  1647. MatrixPosition( pAtt->m_AttachmentToWorld, vecPreviousOrigin );
  1648. MatrixPosition( attachmentToWorld, vecOrigin );
  1649. pAtt->m_vOriginVelocity = (vecOrigin - vecPreviousOrigin) / gpGlobals->frametime;
  1650. }
  1651. else
  1652. {
  1653. pAtt->m_vOriginVelocity.Init();
  1654. }
  1655. pAtt->m_nLastFramecount = gpGlobals->framecount;
  1656. pAtt->m_bAnglesComputed = false;
  1657. pAtt->m_AttachmentToWorld = attachmentToWorld;
  1658. #ifdef _DEBUG
  1659. pAtt->m_angRotation.Init( VEC_T_NAN, VEC_T_NAN, VEC_T_NAN );
  1660. #endif
  1661. return true;
  1662. }
  1663. bool C_BaseAnimating::SetupBones_AttachmentHelper( CStudioHdr *hdr )
  1664. {
  1665. if ( !hdr )
  1666. return false;
  1667. // calculate attachment points
  1668. matrix3x4_t world;
  1669. for (int i = 0; i < hdr->GetNumAttachments(); i++)
  1670. {
  1671. const mstudioattachment_t &pattachment = hdr->pAttachment( i );
  1672. int iBone = hdr->GetAttachmentBone( i );
  1673. if ( (pattachment.flags & ATTACHMENT_FLAG_WORLD_ALIGN) == 0 )
  1674. {
  1675. ConcatTransforms( GetBone( iBone ), pattachment.local, world );
  1676. }
  1677. else
  1678. {
  1679. Vector vecLocalBonePos, vecWorldBonePos;
  1680. MatrixGetColumn( pattachment.local, 3, vecLocalBonePos );
  1681. VectorTransform( vecLocalBonePos, GetBone( iBone ), vecWorldBonePos );
  1682. SetIdentityMatrix( world );
  1683. MatrixSetColumn( vecWorldBonePos, 3, world );
  1684. }
  1685. // FIXME: this shouldn't be here, it should client side on-demand only and hooked into the bone cache!!
  1686. FormatViewModelAttachment( i, world );
  1687. PutAttachment( i + 1, world );
  1688. }
  1689. return true;
  1690. }
  1691. bool C_BaseAnimating::CalcAttachments()
  1692. {
  1693. VPROF( "C_BaseAnimating::CalcAttachments" );
  1694. // Make sure m_CachedBones is valid.
  1695. return SetupBones( NULL, -1, BONE_USED_BY_ATTACHMENT, gpGlobals->curtime );
  1696. }
  1697. //-----------------------------------------------------------------------------
  1698. // Purpose: Returns the world location and world angles of an attachment
  1699. // Input : attachment name
  1700. // Output : location and angles
  1701. //-----------------------------------------------------------------------------
  1702. bool C_BaseAnimating::GetAttachment( const char *szName, Vector &absOrigin, QAngle &absAngles )
  1703. {
  1704. return GetAttachment( LookupAttachment( szName ), absOrigin, absAngles );
  1705. }
  1706. //-----------------------------------------------------------------------------
  1707. // Purpose: Get attachment point by index
  1708. // Input : number - which point
  1709. // Output : float * - the attachment point
  1710. //-----------------------------------------------------------------------------
  1711. bool C_BaseAnimating::GetAttachment( int number, Vector &origin, QAngle &angles )
  1712. {
  1713. // Note: this could be more efficient, but we want the matrix3x4_t version of GetAttachment to be the origin of
  1714. // attachment generation, so a derived class that wants to fudge attachments only
  1715. // has to reimplement that version. This also makes it work like the server in that regard.
  1716. if ( number < 1 || number > m_Attachments.Count() || !CalcAttachments() )
  1717. {
  1718. // Set this to the model origin/angles so that we don't have stack fungus in origin and angles.
  1719. origin = GetAbsOrigin();
  1720. angles = GetAbsAngles();
  1721. return false;
  1722. }
  1723. CAttachmentData *pData = &m_Attachments[number-1];
  1724. if ( pData->m_bAnglesComputed == 0 )
  1725. {
  1726. MatrixAngles( pData->m_AttachmentToWorld, pData->m_angRotation );
  1727. pData->m_bAnglesComputed = -1;
  1728. }
  1729. angles = pData->m_angRotation;
  1730. MatrixPosition( pData->m_AttachmentToWorld, origin );
  1731. return true;
  1732. }
  1733. bool C_BaseAnimating::GetAttachment( int number, matrix3x4_t& matrix )
  1734. {
  1735. if ( number < 1 || number > m_Attachments.Count() )
  1736. return false;
  1737. if ( !CalcAttachments() )
  1738. return false;
  1739. matrix = m_Attachments[number-1].m_AttachmentToWorld;
  1740. return true;
  1741. }
  1742. //-----------------------------------------------------------------------------
  1743. // Purpose: Get attachment point by index (position only)
  1744. // Input : number - which point
  1745. //-----------------------------------------------------------------------------
  1746. bool C_BaseAnimating::GetAttachment( int number, Vector &origin )
  1747. {
  1748. // Note: this could be more efficient, but we want the matrix3x4_t version of GetAttachment to be the origin of
  1749. // attachment generation, so a derived class that wants to fudge attachments only
  1750. // has to reimplement that version. This also makes it work like the server in that regard.
  1751. matrix3x4_t attachmentToWorld;
  1752. if ( !GetAttachment( number, attachmentToWorld ) )
  1753. {
  1754. // Set this to the model origin/angles so that we don't have stack fungus in origin and angles.
  1755. origin = GetAbsOrigin();
  1756. return false;
  1757. }
  1758. MatrixPosition( attachmentToWorld, origin );
  1759. return true;
  1760. }
  1761. bool C_BaseAnimating::GetAttachment( const char *szName, Vector &absOrigin )
  1762. {
  1763. return GetAttachment( LookupAttachment( szName ), absOrigin );
  1764. }
  1765. bool C_BaseAnimating::GetAttachmentVelocity( int number, Vector &originVel, Quaternion &angleVel )
  1766. {
  1767. if ( number < 1 || number > m_Attachments.Count() )
  1768. {
  1769. return false;
  1770. }
  1771. if ( !CalcAttachments() )
  1772. return false;
  1773. originVel = m_Attachments[number-1].m_vOriginVelocity;
  1774. angleVel.Init();
  1775. return true;
  1776. }
  1777. //-----------------------------------------------------------------------------
  1778. // Returns the attachment in local space
  1779. //-----------------------------------------------------------------------------
  1780. bool C_BaseAnimating::GetAttachmentLocal( int iAttachment, matrix3x4_t &attachmentToLocal )
  1781. {
  1782. matrix3x4_t attachmentToWorld;
  1783. if (!GetAttachment(iAttachment, attachmentToWorld))
  1784. return false;
  1785. matrix3x4_t worldToEntity;
  1786. MatrixInvert( EntityToWorldTransform(), worldToEntity );
  1787. ConcatTransforms( worldToEntity, attachmentToWorld, attachmentToLocal );
  1788. return true;
  1789. }
  1790. bool C_BaseAnimating::GetAttachmentLocal( int iAttachment, Vector &origin, QAngle &angles )
  1791. {
  1792. matrix3x4_t attachmentToEntity;
  1793. if ( GetAttachmentLocal( iAttachment, attachmentToEntity ) )
  1794. {
  1795. origin.Init( attachmentToEntity[0][3], attachmentToEntity[1][3], attachmentToEntity[2][3] );
  1796. MatrixAngles( attachmentToEntity, angles );
  1797. return true;
  1798. }
  1799. return false;
  1800. }
  1801. bool C_BaseAnimating::GetAttachmentLocal( int iAttachment, Vector &origin )
  1802. {
  1803. matrix3x4_t attachmentToEntity;
  1804. if ( GetAttachmentLocal( iAttachment, attachmentToEntity ) )
  1805. {
  1806. MatrixPosition( attachmentToEntity, origin );
  1807. return true;
  1808. }
  1809. return false;
  1810. }
  1811. //-----------------------------------------------------------------------------
  1812. // Purpose:
  1813. //-----------------------------------------------------------------------------
  1814. bool C_BaseAnimating::GetRootBone( matrix3x4_t &rootBone )
  1815. {
  1816. Assert( !IsDynamicModelLoading() );
  1817. if ( IsEffectActive( EF_BONEMERGE ) && GetMoveParent() && m_pBoneMergeCache )
  1818. return m_pBoneMergeCache->GetRootBone( rootBone );
  1819. GetBoneTransform( 0, rootBone );
  1820. return true;
  1821. }
  1822. //-----------------------------------------------------------------------------
  1823. // Purpose: Move sound location to center of body
  1824. //-----------------------------------------------------------------------------
  1825. bool C_BaseAnimating::GetSoundSpatialization( SpatializationInfo_t& info )
  1826. {
  1827. {
  1828. C_BaseAnimating::AutoAllowBoneAccess boneaccess( true, false );
  1829. if ( !BaseClass::GetSoundSpatialization( info ) )
  1830. return false;
  1831. }
  1832. // move sound origin to center if npc has IK
  1833. if ( info.pOrigin && IsNPC() && m_pIk)
  1834. {
  1835. *info.pOrigin = GetAbsOrigin();
  1836. Vector mins, maxs, center;
  1837. modelinfo->GetModelBounds( GetModel(), mins, maxs );
  1838. VectorAdd( mins, maxs, center );
  1839. VectorScale( center, 0.5f, center );
  1840. (*info.pOrigin) += center;
  1841. }
  1842. return true;
  1843. }
  1844. //-----------------------------------------------------------------------------
  1845. // Purpose:
  1846. //-----------------------------------------------------------------------------
  1847. bool C_BaseAnimating::IsViewModel() const
  1848. {
  1849. return false;
  1850. }
  1851. //-----------------------------------------------------------------------------
  1852. // Purpose:
  1853. //-----------------------------------------------------------------------------
  1854. void C_BaseAnimating::UpdateOnRemove( void )
  1855. {
  1856. RemoveFromClientSideAnimationList( true );
  1857. BaseClass::UpdateOnRemove();
  1858. }
  1859. //-----------------------------------------------------------------------------
  1860. // Purpose:
  1861. //-----------------------------------------------------------------------------
  1862. bool C_BaseAnimating::IsMenuModel() const
  1863. {
  1864. return false;
  1865. }
  1866. // UNDONE: Seems kind of silly to have this when we also have the cached bones in C_BaseAnimating
  1867. //-----------------------------------------------------------------------------
  1868. // Purpose:
  1869. //-----------------------------------------------------------------------------
  1870. CBoneCache *C_BaseAnimating::GetBoneCache( CStudioHdr *pStudioHdr )
  1871. {
  1872. int boneMask = BONE_USED_BY_HITBOX;
  1873. CBoneCache *pcache = Studio_GetBoneCache( m_hitboxBoneCacheHandle );
  1874. if ( pcache )
  1875. {
  1876. if ( pcache->IsValid( gpGlobals->curtime, 0.0 ) )
  1877. {
  1878. // in memory and still valid, use it!
  1879. return pcache;
  1880. }
  1881. // in memory, but not the same bone set, destroy & rebuild
  1882. if ( (pcache->m_boneMask & boneMask) != boneMask )
  1883. {
  1884. Studio_DestroyBoneCache( m_hitboxBoneCacheHandle );
  1885. m_hitboxBoneCacheHandle = 0;
  1886. pcache = NULL;
  1887. }
  1888. }
  1889. if ( !pStudioHdr )
  1890. pStudioHdr = GetModelPtr( );
  1891. Assert(pStudioHdr);
  1892. C_BaseAnimating::PushAllowBoneAccess( true, false, "GetBoneCache" );
  1893. SetupBones( NULL, -1, boneMask, gpGlobals->curtime );
  1894. C_BaseAnimating::PopBoneAccess( "GetBoneCache" );
  1895. if ( pcache )
  1896. {
  1897. // still in memory but out of date, refresh the bones.
  1898. pcache->UpdateBones( m_CachedBoneData.Base(), pStudioHdr->numbones(), gpGlobals->curtime );
  1899. }
  1900. else
  1901. {
  1902. bonecacheparams_t params;
  1903. params.pStudioHdr = pStudioHdr;
  1904. // HACKHACK: We need the pointer to all bones here
  1905. params.pBoneToWorld = m_CachedBoneData.Base();
  1906. params.curtime = gpGlobals->curtime;
  1907. params.boneMask = boneMask;
  1908. m_hitboxBoneCacheHandle = Studio_CreateBoneCache( params );
  1909. pcache = Studio_GetBoneCache( m_hitboxBoneCacheHandle );
  1910. }
  1911. Assert(pcache);
  1912. return pcache;
  1913. }
  1914. class CTraceFilterSkipNPCsAndPlayers : public CTraceFilterSimple
  1915. {
  1916. public:
  1917. CTraceFilterSkipNPCsAndPlayers( const IHandleEntity *passentity, int collisionGroup )
  1918. : CTraceFilterSimple( passentity, collisionGroup )
  1919. {
  1920. }
  1921. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  1922. {
  1923. if ( CTraceFilterSimple::ShouldHitEntity(pServerEntity, contentsMask) )
  1924. {
  1925. C_BaseEntity *pEntity = EntityFromEntityHandle( pServerEntity );
  1926. if ( !pEntity )
  1927. return true;
  1928. if ( pEntity->IsNPC() || pEntity->IsPlayer() )
  1929. return false;
  1930. return true;
  1931. }
  1932. return false;
  1933. }
  1934. };
  1935. /*
  1936. void drawLine(const Vector& origin, const Vector& dest, int r, int g, int b, bool noDepthTest, float duration)
  1937. {
  1938. debugoverlay->AddLineOverlay( origin, dest, r, g, b, noDepthTest, duration );
  1939. }
  1940. */
  1941. //-----------------------------------------------------------------------------
  1942. // Purpose: update latched IK contacts if they're in a moving reference frame.
  1943. //-----------------------------------------------------------------------------
  1944. void C_BaseAnimating::UpdateIKLocks( float currentTime )
  1945. {
  1946. if (!m_pIk)
  1947. return;
  1948. int targetCount = m_pIk->m_target.Count();
  1949. if ( targetCount == 0 )
  1950. return;
  1951. for (int i = 0; i < targetCount; i++)
  1952. {
  1953. CIKTarget *pTarget = &m_pIk->m_target[i];
  1954. if (!pTarget->IsActive())
  1955. continue;
  1956. if (pTarget->GetOwner() != -1)
  1957. {
  1958. C_BaseEntity *pOwner = cl_entitylist->GetEnt( pTarget->GetOwner() );
  1959. if (pOwner != NULL)
  1960. {
  1961. pTarget->UpdateOwner( pOwner->entindex(), pOwner->GetAbsOrigin(), pOwner->GetAbsAngles() );
  1962. }
  1963. }
  1964. }
  1965. }
  1966. //-----------------------------------------------------------------------------
  1967. // Purpose: Find the ground or external attachment points needed by IK rules
  1968. //-----------------------------------------------------------------------------
  1969. void C_BaseAnimating::CalculateIKLocks( float currentTime )
  1970. {
  1971. if (!m_pIk)
  1972. return;
  1973. int targetCount = m_pIk->m_target.Count();
  1974. if ( targetCount == 0 )
  1975. return;
  1976. // In TF, we might be attaching a player's view to a walking model that's using IK. If we are, it can
  1977. // get in here during the view setup code, and it's not normally supposed to be able to access the spatial
  1978. // partition that early in the rendering loop. So we allow access right here for that special case.
  1979. SpatialPartitionListMask_t curSuppressed = ::partition->GetSuppressedLists();
  1980. ::partition->SuppressLists( PARTITION_ALL_CLIENT_EDICTS, false );
  1981. CBaseEntity::PushEnableAbsRecomputations( false );
  1982. Ray_t ray;
  1983. CTraceFilterSkipNPCsAndPlayers traceFilter( this, GetCollisionGroup() );
  1984. // FIXME: trace based on gravity or trace based on angles?
  1985. Vector up;
  1986. AngleVectors( GetRenderAngles(), NULL, NULL, &up );
  1987. // FIXME: check number of slots?
  1988. float minHeight = FLT_MAX;
  1989. float maxHeight = -FLT_MAX;
  1990. for (int i = 0; i < targetCount; i++)
  1991. {
  1992. trace_t trace;
  1993. CIKTarget *pTarget = &m_pIk->m_target[i];
  1994. if (!pTarget->IsActive())
  1995. continue;
  1996. switch( pTarget->type)
  1997. {
  1998. case IK_GROUND:
  1999. {
  2000. Vector estGround;
  2001. Vector p1, p2;
  2002. // adjust ground to original ground position
  2003. estGround = (pTarget->est.pos - GetRenderOrigin());
  2004. estGround = estGround - (estGround * up) * up;
  2005. estGround = GetAbsOrigin() + estGround + pTarget->est.floor * up;
  2006. VectorMA( estGround, pTarget->est.height, up, p1 );
  2007. VectorMA( estGround, -pTarget->est.height, up, p2 );
  2008. float r = MAX( pTarget->est.radius, 1);
  2009. // don't IK to other characters
  2010. ray.Init( p1, p2, Vector(-r,-r,0), Vector(r,r,r*2) );
  2011. enginetrace->TraceRay( ray, PhysicsSolidMaskForEntity(), &traceFilter, &trace );
  2012. if ( trace.m_pEnt != NULL && trace.m_pEnt->GetMoveType() == MOVETYPE_PUSH )
  2013. {
  2014. pTarget->SetOwner( trace.m_pEnt->entindex(), trace.m_pEnt->GetAbsOrigin(), trace.m_pEnt->GetAbsAngles() );
  2015. }
  2016. else
  2017. {
  2018. pTarget->ClearOwner( );
  2019. }
  2020. if (trace.startsolid)
  2021. {
  2022. // trace from back towards hip
  2023. Vector tmp = estGround - pTarget->trace.closest;
  2024. tmp.NormalizeInPlace();
  2025. ray.Init( estGround - tmp * pTarget->est.height, estGround, Vector(-r,-r,0), Vector(r,r,1) );
  2026. // debugoverlay->AddLineOverlay( ray.m_Start, ray.m_Start + ray.m_Delta, 255, 0, 0, 0, 0 );
  2027. enginetrace->TraceRay( ray, MASK_SOLID, &traceFilter, &trace );
  2028. if (!trace.startsolid)
  2029. {
  2030. p1 = trace.endpos;
  2031. VectorMA( p1, - pTarget->est.height, up, p2 );
  2032. ray.Init( p1, p2, Vector(-r,-r,0), Vector(r,r,1) );
  2033. enginetrace->TraceRay( ray, MASK_SOLID, &traceFilter, &trace );
  2034. }
  2035. // debugoverlay->AddLineOverlay( ray.m_Start, ray.m_Start + ray.m_Delta, 0, 255, 0, 0, 0 );
  2036. }
  2037. if (!trace.startsolid)
  2038. {
  2039. if (trace.DidHitWorld())
  2040. {
  2041. // clamp normal to 33 degrees
  2042. const float limit = 0.832;
  2043. float dot = DotProduct(trace.plane.normal, up);
  2044. if (dot < limit)
  2045. {
  2046. Assert( dot >= 0 );
  2047. // subtract out up component
  2048. Vector diff = trace.plane.normal - up * dot;
  2049. // scale remainder such that it and the up vector are a unit vector
  2050. float d = sqrt( (1 - limit * limit) / DotProduct( diff, diff ) );
  2051. trace.plane.normal = up * limit + d * diff;
  2052. }
  2053. // FIXME: this is wrong with respect to contact position and actual ankle offset
  2054. pTarget->SetPosWithNormalOffset( trace.endpos, trace.plane.normal );
  2055. pTarget->SetNormal( trace.plane.normal );
  2056. pTarget->SetOnWorld( true );
  2057. // only do this on forward tracking or commited IK ground rules
  2058. if (pTarget->est.release < 0.1)
  2059. {
  2060. // keep track of ground height
  2061. float offset = DotProduct( pTarget->est.pos, up );
  2062. if (minHeight > offset )
  2063. minHeight = offset;
  2064. if (maxHeight < offset )
  2065. maxHeight = offset;
  2066. }
  2067. // FIXME: if we don't drop legs, running down hills looks horrible
  2068. /*
  2069. if (DotProduct( pTarget->est.pos, up ) < DotProduct( estGround, up ))
  2070. {
  2071. pTarget->est.pos = estGround;
  2072. }
  2073. */
  2074. }
  2075. else if (trace.DidHitNonWorldEntity())
  2076. {
  2077. pTarget->SetPos( trace.endpos );
  2078. pTarget->SetAngles( GetRenderAngles() );
  2079. // only do this on forward tracking or commited IK ground rules
  2080. if (pTarget->est.release < 0.1)
  2081. {
  2082. float offset = DotProduct( pTarget->est.pos, up );
  2083. if (minHeight > offset )
  2084. minHeight = offset;
  2085. if (maxHeight < offset )
  2086. maxHeight = offset;
  2087. }
  2088. // FIXME: if we don't drop legs, running down hills looks horrible
  2089. /*
  2090. if (DotProduct( pTarget->est.pos, up ) < DotProduct( estGround, up ))
  2091. {
  2092. pTarget->est.pos = estGround;
  2093. }
  2094. */
  2095. }
  2096. else
  2097. {
  2098. pTarget->IKFailed( );
  2099. }
  2100. }
  2101. else
  2102. {
  2103. if (!trace.DidHitWorld())
  2104. {
  2105. pTarget->IKFailed( );
  2106. }
  2107. else
  2108. {
  2109. pTarget->SetPos( trace.endpos );
  2110. pTarget->SetAngles( GetRenderAngles() );
  2111. pTarget->SetOnWorld( true );
  2112. }
  2113. }
  2114. /*
  2115. debugoverlay->AddTextOverlay( p1, i, 0, "%d %.1f %.1f %.1f ", i,
  2116. pTarget->latched.deltaPos.x, pTarget->latched.deltaPos.y, pTarget->latched.deltaPos.z );
  2117. debugoverlay->AddBoxOverlay( pTarget->est.pos, Vector( -r, -r, -1 ), Vector( r, r, 1), QAngle( 0, 0, 0 ), 255, 0, 0, 0, 0 );
  2118. */
  2119. // debugoverlay->AddBoxOverlay( pTarget->latched.pos, Vector( -2, -2, 2 ), Vector( 2, 2, 6), QAngle( 0, 0, 0 ), 0, 255, 0, 0, 0 );
  2120. }
  2121. break;
  2122. case IK_ATTACHMENT:
  2123. {
  2124. C_BaseEntity *pEntity = NULL;
  2125. float flDist = pTarget->est.radius;
  2126. // FIXME: make entity finding sticky!
  2127. // FIXME: what should the radius check be?
  2128. for ( CEntitySphereQuery sphere( pTarget->est.pos, 64 ); ( pEntity = sphere.GetCurrentEntity() ) != NULL; sphere.NextEntity() )
  2129. {
  2130. C_BaseAnimating *pAnim = pEntity->GetBaseAnimating( );
  2131. if (!pAnim)
  2132. continue;
  2133. int iAttachment = pAnim->LookupAttachment( pTarget->offset.pAttachmentName );
  2134. if (iAttachment <= 0)
  2135. continue;
  2136. Vector origin;
  2137. QAngle angles;
  2138. pAnim->GetAttachment( iAttachment, origin, angles );
  2139. // debugoverlay->AddBoxOverlay( origin, Vector( -1, -1, -1 ), Vector( 1, 1, 1 ), QAngle( 0, 0, 0 ), 255, 0, 0, 0, 0 );
  2140. float d = (pTarget->est.pos - origin).Length();
  2141. if ( d >= flDist)
  2142. continue;
  2143. flDist = d;
  2144. pTarget->SetPos( origin );
  2145. pTarget->SetAngles( angles );
  2146. // debugoverlay->AddBoxOverlay( pTarget->est.pos, Vector( -pTarget->est.radius, -pTarget->est.radius, -pTarget->est.radius ), Vector( pTarget->est.radius, pTarget->est.radius, pTarget->est.radius), QAngle( 0, 0, 0 ), 0, 255, 0, 0, 0 );
  2147. }
  2148. if (flDist >= pTarget->est.radius)
  2149. {
  2150. // debugoverlay->AddBoxOverlay( pTarget->est.pos, Vector( -pTarget->est.radius, -pTarget->est.radius, -pTarget->est.radius ), Vector( pTarget->est.radius, pTarget->est.radius, pTarget->est.radius), QAngle( 0, 0, 0 ), 0, 0, 255, 0, 0 );
  2151. // no solution, disable ik rule
  2152. pTarget->IKFailed( );
  2153. }
  2154. }
  2155. break;
  2156. }
  2157. }
  2158. #if defined( HL2_CLIENT_DLL )
  2159. if (minHeight < FLT_MAX)
  2160. {
  2161. input->AddIKGroundContactInfo( entindex(), minHeight, maxHeight );
  2162. }
  2163. #endif
  2164. CBaseEntity::PopEnableAbsRecomputations();
  2165. ::partition->SuppressLists( curSuppressed, true );
  2166. }
  2167. bool C_BaseAnimating::GetPoseParameterRange( int index_, float &minValue, float &maxValue )
  2168. {
  2169. CStudioHdr *pStudioHdr = GetModelPtr();
  2170. if (pStudioHdr)
  2171. {
  2172. if ( index_ >= 0 && index_ < pStudioHdr->GetNumPoseParameters())
  2173. {
  2174. const mstudioposeparamdesc_t &pose = pStudioHdr->pPoseParameter( index_ );
  2175. minValue = pose.start;
  2176. maxValue = pose.end;
  2177. return true;
  2178. }
  2179. }
  2180. minValue = 0.0f;
  2181. maxValue = 1.0f;
  2182. return false;
  2183. }
  2184. //-----------------------------------------------------------------------------
  2185. // Purpose: Do HL1 style lipsynch
  2186. //-----------------------------------------------------------------------------
  2187. void C_BaseAnimating::ControlMouth( CStudioHdr *pstudiohdr )
  2188. {
  2189. if ( !MouthInfo().NeedsEnvelope() )
  2190. return;
  2191. if ( !pstudiohdr )
  2192. return;
  2193. int index_ = LookupPoseParameter( pstudiohdr, LIPSYNC_POSEPARAM_NAME );
  2194. if ( index_ != -1 )
  2195. {
  2196. float value = GetMouth()->mouthopen / 64.0;
  2197. float raw = value;
  2198. if ( value > 1.0 )
  2199. value = 1.0;
  2200. float start, end;
  2201. GetPoseParameterRange( index_, start, end );
  2202. value = (1.0 - value) * start + value * end;
  2203. //Adrian - Set the pose parameter value.
  2204. //It has to be called "mouth".
  2205. SetPoseParameter( pstudiohdr, index_, value );
  2206. // Reset interpolation here since the client is controlling this rather than the server...
  2207. m_iv_flPoseParameter.SetHistoryValuesForItem( index_, raw );
  2208. }
  2209. }
  2210. CMouthInfo *C_BaseAnimating::GetMouth( void )
  2211. {
  2212. return &m_mouth;
  2213. }
  2214. #ifdef DEBUG_BONE_SETUP_THREADING
  2215. ConVar cl_warn_thread_contested_bone_setup("cl_warn_thread_contested_bone_setup", "0" );
  2216. #endif
  2217. // Marked this developmentonly because it currently crashes, and users are enabling it and complaining because of
  2218. // course. Once this actually works it should just be FCVAR_INTERNAL_USE.
  2219. ConVar cl_threaded_bone_setup("cl_threaded_bone_setup", "0", FCVAR_DEVELOPMENTONLY | FCVAR_INTERNAL_USE,
  2220. "Enable parallel processing of C_BaseAnimating::SetupBones()" );
  2221. //-----------------------------------------------------------------------------
  2222. // Purpose: Do the default sequence blending rules as done in HL1
  2223. //-----------------------------------------------------------------------------
  2224. static void SetupBonesOnBaseAnimating( C_BaseAnimating *&pBaseAnimating )
  2225. {
  2226. if ( !pBaseAnimating->GetMoveParent() )
  2227. pBaseAnimating->SetupBones( NULL, -1, -1, gpGlobals->curtime );
  2228. }
  2229. static void PreThreadedBoneSetup()
  2230. {
  2231. mdlcache->BeginLock();
  2232. }
  2233. static void PostThreadedBoneSetup()
  2234. {
  2235. mdlcache->EndLock();
  2236. }
  2237. static bool g_bInThreadedBoneSetup;
  2238. static bool g_bDoThreadedBoneSetup;
  2239. void C_BaseAnimating::InitBoneSetupThreadPool()
  2240. {
  2241. }
  2242. void C_BaseAnimating::ShutdownBoneSetupThreadPool()
  2243. {
  2244. }
  2245. void C_BaseAnimating::ThreadedBoneSetup()
  2246. {
  2247. g_bDoThreadedBoneSetup = cl_threaded_bone_setup.GetBool();
  2248. if ( g_bDoThreadedBoneSetup )
  2249. {
  2250. int nCount = g_PreviousBoneSetups.Count();
  2251. if ( nCount > 1 )
  2252. {
  2253. g_bInThreadedBoneSetup = true;
  2254. ParallelProcess( "C_BaseAnimating::ThreadedBoneSetup", g_PreviousBoneSetups.Base(), nCount, &SetupBonesOnBaseAnimating, &PreThreadedBoneSetup, &PostThreadedBoneSetup );
  2255. g_bInThreadedBoneSetup = false;
  2256. }
  2257. }
  2258. g_iPreviousBoneCounter++;
  2259. g_PreviousBoneSetups.RemoveAll();
  2260. }
  2261. bool C_BaseAnimating::SetupBones( matrix3x4_t *pBoneToWorldOut, int nMaxBones, int boneMask, float currentTime )
  2262. {
  2263. VPROF_BUDGET( "C_BaseAnimating::SetupBones", VPROF_BUDGETGROUP_CLIENT_ANIMATION );
  2264. //=============================================================================
  2265. // HPE_BEGIN:
  2266. // [pfreese] Added the check for pBoneToWorldOut != NULL in this debug warning
  2267. // code. SetupBones is called in the CSS anytime an attachment wants its
  2268. // parent's transform, hence this warning is hit extremely frequently.
  2269. // I'm not actually sure if this is the right "fix" for this, as the bones are
  2270. // actually accessed as part of the setup process, but since I'm not clear on the
  2271. // purpose of this dev warning, I'm including this comment block.
  2272. //=============================================================================
  2273. if ( pBoneToWorldOut != NULL && !IsBoneAccessAllowed() )
  2274. {
  2275. static float lastWarning = 0.0f;
  2276. // Prevent spammage!!!
  2277. if ( gpGlobals->realtime >= lastWarning + 1.0f )
  2278. {
  2279. DevMsgRT( "*** ERROR: Bone access not allowed (entity %i:%s)\n", index, GetClassname() );
  2280. lastWarning = gpGlobals->realtime;
  2281. }
  2282. }
  2283. //boneMask = BONE_USED_BY_ANYTHING; // HACK HACK - this is a temp fix until we have accessors for bones to find out where problems are.
  2284. if ( GetSequence() == -1 )
  2285. return false;
  2286. if ( boneMask == -1 )
  2287. {
  2288. boneMask = m_iPrevBoneMask;
  2289. }
  2290. // We should get rid of this someday when we have solutions for the odd cases where a bone doesn't
  2291. // get setup and its transform is asked for later.
  2292. if ( cl_SetupAllBones.GetInt() )
  2293. {
  2294. boneMask |= BONE_USED_BY_ANYTHING;
  2295. }
  2296. // Set up all bones if recording, too
  2297. if ( IsToolRecording() )
  2298. {
  2299. boneMask |= BONE_USED_BY_ANYTHING;
  2300. }
  2301. if ( g_bInThreadedBoneSetup )
  2302. {
  2303. if ( !m_BoneSetupLock.TryLock() )
  2304. {
  2305. return false;
  2306. }
  2307. }
  2308. #ifdef DEBUG_BONE_SETUP_THREADING
  2309. if ( cl_warn_thread_contested_bone_setup.GetBool() )
  2310. {
  2311. if ( !m_BoneSetupLock.TryLock() )
  2312. {
  2313. Msg( "Contested bone setup in frame %d!\n", gpGlobals->framecount );
  2314. }
  2315. else
  2316. {
  2317. m_BoneSetupLock.Unlock();
  2318. }
  2319. }
  2320. #endif
  2321. AUTO_LOCK( m_BoneSetupLock );
  2322. if ( g_bInThreadedBoneSetup )
  2323. {
  2324. m_BoneSetupLock.Unlock();
  2325. }
  2326. if ( m_iMostRecentModelBoneCounter != g_iModelBoneCounter )
  2327. {
  2328. // Clear out which bones we've touched this frame if this is
  2329. // the first time we've seen this object this frame.
  2330. if ( LastBoneChangedTime() >= m_flLastBoneSetupTime )
  2331. {
  2332. m_BoneAccessor.SetReadableBones( 0 );
  2333. m_BoneAccessor.SetWritableBones( 0 );
  2334. m_flLastBoneSetupTime = currentTime;
  2335. }
  2336. m_iPrevBoneMask = m_iAccumulatedBoneMask;
  2337. m_iAccumulatedBoneMask = 0;
  2338. #ifdef STUDIO_ENABLE_PERF_COUNTERS
  2339. CStudioHdr *hdr = GetModelPtr();
  2340. if (hdr)
  2341. {
  2342. hdr->ClearPerfCounters();
  2343. }
  2344. #endif
  2345. }
  2346. int nBoneCount = m_CachedBoneData.Count();
  2347. if ( g_bDoThreadedBoneSetup && !g_bInThreadedBoneSetup && ( nBoneCount >= 16 ) && !GetMoveParent() && m_iMostRecentBoneSetupRequest != g_iPreviousBoneCounter )
  2348. {
  2349. m_iMostRecentBoneSetupRequest = g_iPreviousBoneCounter;
  2350. Assert( g_PreviousBoneSetups.Find( this ) == -1 );
  2351. g_PreviousBoneSetups.AddToTail( this );
  2352. }
  2353. // Keep track of everthing asked for over the entire frame
  2354. m_iAccumulatedBoneMask |= boneMask;
  2355. // Make sure that we know that we've already calculated some bone stuff this time around.
  2356. m_iMostRecentModelBoneCounter = g_iModelBoneCounter;
  2357. // Have we cached off all bones meeting the flag set?
  2358. if( ( m_BoneAccessor.GetReadableBones() & boneMask ) != boneMask )
  2359. {
  2360. MDLCACHE_CRITICAL_SECTION();
  2361. CStudioHdr *hdr = GetModelPtr();
  2362. if ( !hdr || !hdr->SequencesAvailable() )
  2363. return false;
  2364. // Setup our transform based on render angles and origin.
  2365. matrix3x4_t parentTransform;
  2366. AngleMatrix( GetRenderAngles(), GetRenderOrigin(), parentTransform );
  2367. // Load the boneMask with the total of what was asked for last frame.
  2368. boneMask |= m_iPrevBoneMask;
  2369. // Allow access to the bones we're setting up so we don't get asserts in here.
  2370. int oldReadableBones = m_BoneAccessor.GetReadableBones();
  2371. m_BoneAccessor.SetWritableBones( m_BoneAccessor.GetReadableBones() | boneMask );
  2372. m_BoneAccessor.SetReadableBones( m_BoneAccessor.GetWritableBones() );
  2373. if (hdr->flags() & STUDIOHDR_FLAGS_STATIC_PROP)
  2374. {
  2375. MatrixCopy( parentTransform, GetBoneForWrite( 0 ) );
  2376. }
  2377. else
  2378. {
  2379. TrackBoneSetupEnt( this );
  2380. // This is necessary because it's possible that CalculateIKLocks will trigger our move children
  2381. // to call GetAbsOrigin(), and they'll use our OLD bone transforms to get their attachments
  2382. // since we're right in the middle of setting up our new transforms.
  2383. //
  2384. // Setting this flag forces move children to keep their abs transform invalidated.
  2385. AddFlag( EFL_SETTING_UP_BONES );
  2386. // NOTE: For model scaling, we need to opt out of IK because it will mark the bones as already being calculated
  2387. if ( !IsModelScaled() )
  2388. {
  2389. // only allocate an ik block if the npc can use it
  2390. if ( !m_pIk && hdr->numikchains() > 0 && !(m_EntClientFlags & ENTCLIENTFLAG_DONTUSEIK) )
  2391. {
  2392. m_pIk = new CIKContext;
  2393. }
  2394. }
  2395. else
  2396. {
  2397. // Reset the IK
  2398. if ( m_pIk )
  2399. {
  2400. delete m_pIk;
  2401. m_pIk = NULL;
  2402. }
  2403. }
  2404. Vector pos[MAXSTUDIOBONES];
  2405. Quaternion q[MAXSTUDIOBONES];
  2406. #if defined(FP_EXCEPTIONS_ENABLED) || defined(DBGFLAG_ASSERT)
  2407. // Having these uninitialized means that some bugs are very hard
  2408. // to reproduce. A memset of 0xFF is a simple way of getting NaNs.
  2409. memset( pos, 0xFF, sizeof(pos) );
  2410. memset( q, 0xFF, sizeof(q) );
  2411. #endif
  2412. int bonesMaskNeedRecalc = boneMask | oldReadableBones; // Hack to always recalc bones, to fix the arm jitter in the new CS player anims until Ken makes the real fix
  2413. if ( m_pIk )
  2414. {
  2415. if (Teleported() || IsNoInterpolationFrame())
  2416. m_pIk->ClearTargets();
  2417. m_pIk->Init( hdr, GetRenderAngles(), GetRenderOrigin(), currentTime, gpGlobals->framecount, bonesMaskNeedRecalc );
  2418. }
  2419. // Let pose debugger know that we are blending
  2420. g_pPoseDebugger->StartBlending( this, hdr );
  2421. StandardBlendingRules( hdr, pos, q, currentTime, bonesMaskNeedRecalc );
  2422. CBoneBitList boneComputed;
  2423. // don't calculate IK on ragdolls
  2424. if ( m_pIk && !IsRagdoll() )
  2425. {
  2426. UpdateIKLocks( currentTime );
  2427. m_pIk->UpdateTargets( pos, q, m_BoneAccessor.GetBoneArrayForWrite(), boneComputed );
  2428. CalculateIKLocks( currentTime );
  2429. m_pIk->SolveDependencies( pos, q, m_BoneAccessor.GetBoneArrayForWrite(), boneComputed );
  2430. }
  2431. BuildTransformations( hdr, pos, q, parentTransform, bonesMaskNeedRecalc, boneComputed );
  2432. RemoveFlag( EFL_SETTING_UP_BONES );
  2433. ControlMouth( hdr );
  2434. }
  2435. if( !( oldReadableBones & BONE_USED_BY_ATTACHMENT ) && ( boneMask & BONE_USED_BY_ATTACHMENT ) )
  2436. {
  2437. if ( !SetupBones_AttachmentHelper( hdr ) )
  2438. {
  2439. DevWarning( 2, "SetupBones: SetupBones_AttachmentHelper failed.\n" );
  2440. return false;
  2441. }
  2442. }
  2443. }
  2444. // Do they want to get at the bone transforms? If it's just making sure an aiment has
  2445. // its bones setup, it doesn't need the transforms yet.
  2446. if ( pBoneToWorldOut )
  2447. {
  2448. if ( nMaxBones >= m_CachedBoneData.Count() )
  2449. {
  2450. memcpy( pBoneToWorldOut, m_CachedBoneData.Base(), sizeof( matrix3x4_t ) * m_CachedBoneData.Count() );
  2451. }
  2452. else
  2453. {
  2454. ExecuteNTimes( 25, Warning( "SetupBones: invalid bone array size (%d - needs %d)\n", nMaxBones, m_CachedBoneData.Count() ) );
  2455. return false;
  2456. }
  2457. }
  2458. return true;
  2459. }
  2460. C_BaseAnimating* C_BaseAnimating::FindFollowedEntity()
  2461. {
  2462. C_BaseEntity *follow = GetFollowedEntity();
  2463. if ( !follow )
  2464. return NULL;
  2465. if ( follow->IsDormant() )
  2466. return NULL;
  2467. if ( !follow->GetModel() )
  2468. {
  2469. Warning( "mod_studio: MOVETYPE_FOLLOW with no model.\n" );
  2470. return NULL;
  2471. }
  2472. if ( modelinfo->GetModelType( follow->GetModel() ) != mod_studio )
  2473. {
  2474. Warning( "Attached %s (mod_studio) to %s (%d)\n",
  2475. modelinfo->GetModelName( GetModel() ),
  2476. modelinfo->GetModelName( follow->GetModel() ),
  2477. modelinfo->GetModelType( follow->GetModel() ) );
  2478. return NULL;
  2479. }
  2480. return assert_cast< C_BaseAnimating* >( follow );
  2481. }
  2482. void C_BaseAnimating::InvalidateBoneCache()
  2483. {
  2484. m_iMostRecentModelBoneCounter = g_iModelBoneCounter - 1;
  2485. m_flLastBoneSetupTime = -FLT_MAX;
  2486. }
  2487. bool C_BaseAnimating::IsBoneCacheValid() const
  2488. {
  2489. return m_iMostRecentModelBoneCounter == g_iModelBoneCounter;
  2490. }
  2491. // Causes an assert to happen if bones or attachments are used while this is false.
  2492. struct BoneAccess
  2493. {
  2494. BoneAccess()
  2495. {
  2496. bAllowBoneAccessForNormalModels = false;
  2497. bAllowBoneAccessForViewModels = false;
  2498. tag = NULL;
  2499. }
  2500. bool bAllowBoneAccessForNormalModels;
  2501. bool bAllowBoneAccessForViewModels;
  2502. char const *tag;
  2503. };
  2504. // the modelcache critical section is insufficient for preventing us from getting into the bone cache at the same time.
  2505. // The bonecache itself is protected by a mutex, but the actual bone access stack needs to be protected separately.
  2506. static CThreadFastMutex g_BoneAccessMutex;
  2507. static CUtlVector< BoneAccess > g_BoneAccessStack;
  2508. static BoneAccess g_BoneAcessBase;
  2509. bool C_BaseAnimating::IsBoneAccessAllowed() const
  2510. {
  2511. if ( IsViewModel() )
  2512. return g_BoneAcessBase.bAllowBoneAccessForViewModels;
  2513. else
  2514. return g_BoneAcessBase.bAllowBoneAccessForNormalModels;
  2515. }
  2516. // (static function)
  2517. void C_BaseAnimating::PushAllowBoneAccess( bool bAllowForNormalModels, bool bAllowForViewModels, char const *tagPush )
  2518. {
  2519. AUTO_LOCK( g_BoneAccessMutex );
  2520. STAGING_ONLY_EXEC( ReentrancyVerifier rv( &dbg_bonestack_reentrant_count, dbg_bonestack_perturb.GetInt() ) );
  2521. BoneAccess save = g_BoneAcessBase;
  2522. g_BoneAccessStack.AddToTail( save );
  2523. Assert( g_BoneAccessStack.Count() < 32 ); // Most likely we are leaking "PushAllowBoneAccess" calls if PopBoneAccess is never called. Consider using AutoAllowBoneAccess.
  2524. g_BoneAcessBase.bAllowBoneAccessForNormalModels = bAllowForNormalModels;
  2525. g_BoneAcessBase.bAllowBoneAccessForViewModels = bAllowForViewModels;
  2526. g_BoneAcessBase.tag = tagPush;
  2527. }
  2528. void C_BaseAnimating::PopBoneAccess( char const *tagPop )
  2529. {
  2530. AUTO_LOCK( g_BoneAccessMutex );
  2531. STAGING_ONLY_EXEC( ReentrancyVerifier rv( &dbg_bonestack_reentrant_count, dbg_bonestack_perturb.GetInt() ) );
  2532. // Validate that pop matches the push
  2533. Assert( ( g_BoneAcessBase.tag == tagPop ) || ( g_BoneAcessBase.tag && g_BoneAcessBase.tag != ( char const * ) 1 && tagPop && tagPop != ( char const * ) 1 && !strcmp( g_BoneAcessBase.tag, tagPop ) ) );
  2534. int lastIndex = g_BoneAccessStack.Count() - 1;
  2535. if ( lastIndex < 0 )
  2536. {
  2537. Assert( !"C_BaseAnimating::PopBoneAccess: Stack is empty!!!" );
  2538. return;
  2539. }
  2540. g_BoneAcessBase = g_BoneAccessStack[lastIndex ];
  2541. g_BoneAccessStack.Remove( lastIndex );
  2542. }
  2543. C_BaseAnimating::AutoAllowBoneAccess::AutoAllowBoneAccess( bool bAllowForNormalModels, bool bAllowForViewModels )
  2544. {
  2545. C_BaseAnimating::PushAllowBoneAccess( bAllowForNormalModels, bAllowForViewModels, ( char const * ) 1 );
  2546. }
  2547. C_BaseAnimating::AutoAllowBoneAccess::~AutoAllowBoneAccess( )
  2548. {
  2549. C_BaseAnimating::PopBoneAccess( ( char const * ) 1 );
  2550. }
  2551. // (static function)
  2552. void C_BaseAnimating::InvalidateBoneCaches()
  2553. {
  2554. g_iModelBoneCounter++;
  2555. }
  2556. bool C_BaseAnimating::ShouldDraw()
  2557. {
  2558. return !IsDynamicModelLoading() && BaseClass::ShouldDraw();
  2559. }
  2560. void C_BaseAnimating::UpdateVisibility()
  2561. {
  2562. BaseClass::UpdateVisibility();
  2563. if ( ShouldDraw() )
  2564. {
  2565. if ( !m_bInitModelEffects )
  2566. {
  2567. InitModelEffects();
  2568. }
  2569. }
  2570. else if ( m_bHasAttachedParticles )
  2571. {
  2572. ParticleProp()->StopParticlesInvolving( this );
  2573. m_bHasAttachedParticles = false;
  2574. m_bInitModelEffects = false;
  2575. }
  2576. }
  2577. ConVar r_drawothermodels( "r_drawothermodels", "1", FCVAR_CHEAT, "0=Off, 1=Normal, 2=Wireframe" );
  2578. //-----------------------------------------------------------------------------
  2579. // Purpose: Draws the object
  2580. // Input : flags -
  2581. //-----------------------------------------------------------------------------
  2582. int C_BaseAnimating::DrawModel( int flags )
  2583. {
  2584. VPROF_BUDGET( "C_BaseAnimating::DrawModel", VPROF_BUDGETGROUP_MODEL_RENDERING );
  2585. if ( !m_bReadyToDraw )
  2586. return 0;
  2587. int drawn = 0;
  2588. #ifdef TF_CLIENT_DLL
  2589. ValidateModelIndex();
  2590. #endif
  2591. if ( r_drawothermodels.GetInt() )
  2592. {
  2593. MDLCACHE_CRITICAL_SECTION();
  2594. int extraFlags = 0;
  2595. if ( r_drawothermodels.GetInt() == 2 )
  2596. {
  2597. extraFlags |= STUDIO_WIREFRAME;
  2598. }
  2599. if ( flags & STUDIO_SHADOWDEPTHTEXTURE )
  2600. {
  2601. extraFlags |= STUDIO_SHADOWDEPTHTEXTURE;
  2602. }
  2603. if ( flags & STUDIO_SSAODEPTHTEXTURE )
  2604. {
  2605. extraFlags |= STUDIO_SSAODEPTHTEXTURE;
  2606. }
  2607. if ( ( flags & ( STUDIO_SSAODEPTHTEXTURE | STUDIO_SHADOWDEPTHTEXTURE ) ) == 0 &&
  2608. g_pStudioStatsEntity != NULL && g_pStudioStatsEntity == GetClientRenderable() )
  2609. {
  2610. extraFlags |= STUDIO_GENERATE_STATS;
  2611. }
  2612. if ( flags & ( STUDIO_NO_OVERRIDE_FOR_ATTACH ) )
  2613. {
  2614. extraFlags |= STUDIO_NO_OVERRIDE_FOR_ATTACH;
  2615. }
  2616. // Necessary for lighting blending
  2617. CreateModelInstance();
  2618. if ( !IsFollowingEntity() )
  2619. {
  2620. drawn = InternalDrawModel( flags|extraFlags );
  2621. }
  2622. else
  2623. {
  2624. // this doesn't draw unless master entity is visible and it's a studio model!!!
  2625. C_BaseAnimating *follow = FindFollowedEntity();
  2626. if ( follow )
  2627. {
  2628. // recompute master entity bone structure
  2629. int baseDrawn = follow->DrawModel( 0 );
  2630. // draw entity
  2631. // FIXME: Currently only draws if aiment is drawn.
  2632. // BUGBUG: Fixup bbox and do a separate cull for follow object
  2633. if ( baseDrawn )
  2634. {
  2635. drawn = InternalDrawModel( STUDIO_RENDER|extraFlags );
  2636. }
  2637. }
  2638. }
  2639. }
  2640. // If we're visualizing our bboxes, draw them
  2641. DrawBBoxVisualizations();
  2642. return drawn;
  2643. }
  2644. //-----------------------------------------------------------------------------
  2645. // Gets the hitbox-to-world transforms, returns false if there was a problem
  2646. //-----------------------------------------------------------------------------
  2647. bool C_BaseAnimating::HitboxToWorldTransforms( matrix3x4_t *pHitboxToWorld[MAXSTUDIOBONES] )
  2648. {
  2649. MDLCACHE_CRITICAL_SECTION();
  2650. if ( !GetModel() )
  2651. return false;
  2652. CStudioHdr *pStudioHdr = GetModelPtr();
  2653. if (!pStudioHdr)
  2654. return false;
  2655. mstudiohitboxset_t *set = pStudioHdr->pHitboxSet( GetHitboxSet() );
  2656. if ( !set )
  2657. return false;
  2658. if ( !set->numhitboxes )
  2659. return false;
  2660. CBoneCache *pCache = GetBoneCache( pStudioHdr );
  2661. pCache->ReadCachedBonePointers( pHitboxToWorld, pStudioHdr->numbones() );
  2662. return true;
  2663. }
  2664. //-----------------------------------------------------------------------------
  2665. //
  2666. //-----------------------------------------------------------------------------
  2667. bool C_BaseAnimating::OnPostInternalDrawModel( ClientModelRenderInfo_t *pInfo )
  2668. {
  2669. return true;
  2670. }
  2671. //-----------------------------------------------------------------------------
  2672. //
  2673. //-----------------------------------------------------------------------------
  2674. bool C_BaseAnimating::OnInternalDrawModel( ClientModelRenderInfo_t *pInfo )
  2675. {
  2676. if ( m_hLightingOriginRelative.Get() )
  2677. {
  2678. C_InfoLightingRelative *pInfoLighting = assert_cast<C_InfoLightingRelative*>( m_hLightingOriginRelative.Get() );
  2679. pInfoLighting->GetLightingOffset( pInfo->lightingOffset );
  2680. pInfo->pLightingOffset = &pInfo->lightingOffset;
  2681. }
  2682. if ( m_hLightingOrigin )
  2683. {
  2684. pInfo->pLightingOrigin = &(m_hLightingOrigin->GetAbsOrigin());
  2685. }
  2686. return true;
  2687. }
  2688. //-----------------------------------------------------------------------------
  2689. //
  2690. //-----------------------------------------------------------------------------
  2691. void C_BaseAnimating::DoInternalDrawModel( ClientModelRenderInfo_t *pInfo, DrawModelState_t *pState, matrix3x4_t *pBoneToWorldArray )
  2692. {
  2693. if ( pState)
  2694. {
  2695. modelrender->DrawModelExecute( *pState, *pInfo, pBoneToWorldArray );
  2696. }
  2697. if ( vcollide_wireframe.GetBool() )
  2698. {
  2699. if ( IsRagdoll() )
  2700. {
  2701. m_pRagdoll->DrawWireframe();
  2702. }
  2703. else if ( IsSolid() && CollisionProp()->GetSolid() == SOLID_VPHYSICS )
  2704. {
  2705. vcollide_t *pCollide = modelinfo->GetVCollide( GetModelIndex() );
  2706. if ( pCollide && pCollide->solidCount == 1 )
  2707. {
  2708. static color32 debugColor = {0,255,255,0};
  2709. matrix3x4_t matrix;
  2710. AngleMatrix( GetAbsAngles(), GetAbsOrigin(), matrix );
  2711. engine->DebugDrawPhysCollide( pCollide->solids[0], NULL, matrix, debugColor );
  2712. if ( VPhysicsGetObject() )
  2713. {
  2714. static color32 debugColorPhys = {255,0,0,0};
  2715. VPhysicsGetObject()->GetPositionMatrix( &matrix );
  2716. engine->DebugDrawPhysCollide( pCollide->solids[0], NULL, matrix, debugColorPhys );
  2717. }
  2718. }
  2719. }
  2720. }
  2721. }
  2722. #ifdef TF_CLIENT_DLL
  2723. // Move this elsewhere if we ever need it again.
  2724. class MaterialOverrideRestore
  2725. {
  2726. public:
  2727. MaterialOverrideRestore()
  2728. : m_bRestore( false )
  2729. , m_pOverrideMaterial( NULL )
  2730. , m_nOverrideType( OVERRIDE_NORMAL )
  2731. { }
  2732. ~MaterialOverrideRestore()
  2733. {
  2734. if ( m_bRestore )
  2735. {
  2736. modelrender->ForcedMaterialOverride( m_pOverrideMaterial, m_nOverrideType );
  2737. }
  2738. }
  2739. void RestoreOverride( IMaterial* pRestoreOverride, OverrideType_t overType )
  2740. {
  2741. m_bRestore = true;
  2742. m_pOverrideMaterial = pRestoreOverride;
  2743. m_nOverrideType = overType;
  2744. }
  2745. private:
  2746. bool m_bRestore;
  2747. IMaterial* m_pOverrideMaterial;
  2748. OverrideType_t m_nOverrideType;
  2749. };
  2750. #endif
  2751. //-----------------------------------------------------------------------------
  2752. // Purpose: Draws the object
  2753. // Input : flags -
  2754. //-----------------------------------------------------------------------------
  2755. int C_BaseAnimating::InternalDrawModel( int flags )
  2756. {
  2757. VPROF( "C_BaseAnimating::InternalDrawModel" );
  2758. #ifdef TF_CLIENT_DLL
  2759. MaterialOverrideRestore overrideRestore;
  2760. // TODO: We should listen for TF_COND_TAUNTING changing and then just do this then,
  2761. // rather than every frame.
  2762. bool bIgnoreOverride = false;
  2763. C_TFPlayer *pOwner = ToTFPlayer( GetOwnerEntity() );
  2764. if ( pOwner )
  2765. {
  2766. CTFPlayerInventory *pInv = pOwner->Inventory();
  2767. if ( pInv )
  2768. {
  2769. if ( pOwner->m_Shared.InCond( TF_COND_TAUNTING ) )
  2770. {
  2771. int iClass = pOwner->GetPlayerClass()->GetClassIndex();
  2772. CEconItemView *pMiscItemView = pInv->GetItemInLoadout( iClass, pOwner->GetActiveTauntSlot() );
  2773. if ( pMiscItemView && pMiscItemView->IsValid() )
  2774. {
  2775. if ( pMiscItemView->GetStaticData()->GetTauntData() )
  2776. {
  2777. bIgnoreOverride = pMiscItemView->GetStaticData()->GetTauntData()->GetProp( iClass ) != NULL;
  2778. }
  2779. }
  2780. }
  2781. }
  2782. }
  2783. if ( !bIgnoreOverride )
  2784. {
  2785. // If there is some other material override, it's probably the client asking for us to render invuln or the
  2786. // spy cloaking. Those are way more important than ours, so do them instead.
  2787. IMaterial* pOverrideMaterial = NULL;
  2788. OverrideType_t nDontcare = OVERRIDE_NORMAL;
  2789. modelrender->GetMaterialOverride( &pOverrideMaterial, &nDontcare );
  2790. bIgnoreOverride = ( pOverrideMaterial != NULL );
  2791. }
  2792. IMaterial* pOverrideMaterial = GetEconWeaponMaterialOverride( GetTeamNumber() );
  2793. bool bUseOverride = !bIgnoreOverride && pOverrideMaterial != NULL;
  2794. if ( bUseOverride && ( flags & STUDIO_RENDER ) )
  2795. {
  2796. // Set us up to restore properly on exit
  2797. overrideRestore.RestoreOverride( NULL, OVERRIDE_NORMAL );
  2798. modelrender->ForcedMaterialOverride( pOverrideMaterial );
  2799. flags |= STUDIO_NO_OVERRIDE_FOR_ATTACH; // Don't apply override materials to attachments.
  2800. }
  2801. #endif
  2802. if ( !GetModel() )
  2803. return 0;
  2804. // This should never happen, but if the server class hierarchy has bmodel entities derived from CBaseAnimating or does a
  2805. // SetModel with the wrong type of model, this could occur.
  2806. if ( modelinfo->GetModelType( GetModel() ) != mod_studio )
  2807. {
  2808. return BaseClass::DrawModel( flags );
  2809. }
  2810. // Make sure hdr is valid for drawing
  2811. if ( !GetModelPtr() )
  2812. return 0;
  2813. UpdateBoneAttachments( );
  2814. if ( IsEffectActive( EF_ITEM_BLINK ) )
  2815. {
  2816. flags |= STUDIO_ITEM_BLINK;
  2817. }
  2818. ClientModelRenderInfo_t info;
  2819. ClientModelRenderInfo_t *pInfo;
  2820. pInfo = &info;
  2821. pInfo->flags = flags;
  2822. pInfo->pRenderable = this;
  2823. pInfo->instance = GetModelInstance();
  2824. pInfo->entity_index = index;
  2825. pInfo->pModel = GetModel();
  2826. pInfo->origin = GetRenderOrigin();
  2827. pInfo->angles = GetRenderAngles();
  2828. pInfo->skin = GetSkin();
  2829. pInfo->body = GetBody();
  2830. pInfo->hitboxset = m_nHitboxSet;
  2831. if ( !OnInternalDrawModel( pInfo ) )
  2832. {
  2833. return 0;
  2834. }
  2835. Assert( !pInfo->pModelToWorld);
  2836. if ( !pInfo->pModelToWorld )
  2837. {
  2838. pInfo->pModelToWorld = &pInfo->modelToWorld;
  2839. // Turns the origin + angles into a matrix
  2840. AngleMatrix( pInfo->angles, pInfo->origin, pInfo->modelToWorld );
  2841. }
  2842. DrawModelState_t state;
  2843. matrix3x4_t *pBoneToWorld = NULL;
  2844. bool bMarkAsDrawn = modelrender->DrawModelSetup( *pInfo, &state, NULL, &pBoneToWorld );
  2845. // Scale the base transform if we don't have a bone hierarchy
  2846. if ( IsModelScaled() )
  2847. {
  2848. CStudioHdr *pHdr = GetModelPtr();
  2849. if ( pHdr && pBoneToWorld && pHdr->numbones() == 1 )
  2850. {
  2851. // Scale the bone to world at this point
  2852. const float flScale = GetModelScale();
  2853. VectorScale( (*pBoneToWorld)[0], flScale, (*pBoneToWorld)[0] );
  2854. VectorScale( (*pBoneToWorld)[1], flScale, (*pBoneToWorld)[1] );
  2855. VectorScale( (*pBoneToWorld)[2], flScale, (*pBoneToWorld)[2] );
  2856. }
  2857. }
  2858. DoInternalDrawModel( pInfo, ( bMarkAsDrawn && ( pInfo->flags & STUDIO_RENDER ) ) ? &state : NULL, pBoneToWorld );
  2859. OnPostInternalDrawModel( pInfo );
  2860. return bMarkAsDrawn;
  2861. }
  2862. extern ConVar muzzleflash_light;
  2863. void C_BaseAnimating::ProcessMuzzleFlashEvent()
  2864. {
  2865. // If we have an attachment, then stick a light on it.
  2866. if ( muzzleflash_light.GetBool() )
  2867. {
  2868. //FIXME: We should really use a named attachment for this
  2869. if ( m_Attachments.Count() > 0 )
  2870. {
  2871. Vector vAttachment;
  2872. QAngle dummyAngles;
  2873. GetAttachment( 1, vAttachment, dummyAngles );
  2874. // Make an elight
  2875. dlight_t *el = effects->CL_AllocElight( LIGHT_INDEX_MUZZLEFLASH + index );
  2876. el->origin = vAttachment;
  2877. el->radius = random->RandomInt( 32, 64 );
  2878. el->decay = el->radius / 0.05f;
  2879. el->die = gpGlobals->curtime + 0.05f;
  2880. el->color.r = 255;
  2881. el->color.g = 192;
  2882. el->color.b = 64;
  2883. el->color.exponent = 5;
  2884. }
  2885. }
  2886. }
  2887. //-----------------------------------------------------------------------------
  2888. // Internal routine to process animation events for studiomodels
  2889. //-----------------------------------------------------------------------------
  2890. void C_BaseAnimating::DoAnimationEvents( CStudioHdr *pStudioHdr )
  2891. {
  2892. if ( !pStudioHdr )
  2893. return;
  2894. #ifdef DEBUG
  2895. bool watch = dbganimmodel.GetString()[0] && V_stristr( pStudioHdr->pszName(), dbganimmodel.GetString() );
  2896. #else
  2897. bool watch = false; // Q_strstr( hdr->name, "rifle" ) ? true : false;
  2898. #endif
  2899. //Adrian: eh? This should never happen.
  2900. if ( GetSequence() == -1 )
  2901. return;
  2902. // build root animation
  2903. float flEventCycle = GetCycle();
  2904. // If we're invisible, don't draw the muzzle flash
  2905. bool bIsInvisible = !IsVisible() && !IsViewModel() && !IsMenuModel();
  2906. if ( bIsInvisible && !clienttools->IsInRecordingMode() )
  2907. return;
  2908. // add in muzzleflash effect
  2909. if ( ShouldMuzzleFlash() )
  2910. {
  2911. DisableMuzzleFlash();
  2912. ProcessMuzzleFlashEvent();
  2913. }
  2914. // If we're invisible, don't process animation events.
  2915. if ( bIsInvisible )
  2916. return;
  2917. // If we don't have any sequences, don't do anything
  2918. int nStudioNumSeq = pStudioHdr->GetNumSeq();
  2919. if ( nStudioNumSeq < 1 )
  2920. {
  2921. Warning( "%s[%d]: no sequences?\n", GetDebugName(), entindex() );
  2922. Assert( nStudioNumSeq >= 1 );
  2923. return;
  2924. }
  2925. int nSeqNum = GetSequence();
  2926. if ( nSeqNum >= nStudioNumSeq )
  2927. {
  2928. // This can happen e.g. while reloading Heavy's shotgun, switch to the minigun.
  2929. Warning( "%s[%d]: Playing sequence %d but there's only %d in total?\n", GetDebugName(), entindex(), nSeqNum, nStudioNumSeq );
  2930. return;
  2931. }
  2932. mstudioseqdesc_t &seqdesc = pStudioHdr->pSeqdesc( nSeqNum );
  2933. if (seqdesc.numevents == 0)
  2934. return;
  2935. // Forces anim event indices to get set and returns pEvent(0);
  2936. mstudioevent_t *pevent = GetEventIndexForSequence( seqdesc );
  2937. if ( watch )
  2938. {
  2939. Msg( "%i cycle %f\n", gpGlobals->tickcount, GetCycle() );
  2940. }
  2941. bool resetEvents = m_nResetEventsParity != m_nPrevResetEventsParity;
  2942. m_nPrevResetEventsParity = m_nResetEventsParity;
  2943. if (m_nEventSequence != GetSequence() || resetEvents )
  2944. {
  2945. if ( watch )
  2946. {
  2947. Msg( "new seq: %i - old seq: %i - reset: %s - m_flCycle %f - Model Name: %s - (time %.3f)\n",
  2948. GetSequence(), m_nEventSequence,
  2949. resetEvents ? "true" : "false",
  2950. GetCycle(), pStudioHdr->pszName(),
  2951. gpGlobals->curtime);
  2952. }
  2953. m_nEventSequence = GetSequence();
  2954. flEventCycle = 0.0f;
  2955. m_flPrevEventCycle = -0.01; // back up to get 0'th frame animations
  2956. }
  2957. // stalled?
  2958. if (flEventCycle == m_flPrevEventCycle)
  2959. return;
  2960. if ( watch )
  2961. {
  2962. Msg( "%i (seq %d cycle %.3f ) evcycle %.3f prevevcycle %.3f (time %.3f)\n",
  2963. gpGlobals->tickcount,
  2964. GetSequence(),
  2965. GetCycle(),
  2966. flEventCycle,
  2967. m_flPrevEventCycle,
  2968. gpGlobals->curtime );
  2969. }
  2970. // check for looping
  2971. BOOL bLooped = false;
  2972. if (flEventCycle <= m_flPrevEventCycle)
  2973. {
  2974. if (m_flPrevEventCycle - flEventCycle > 0.5)
  2975. {
  2976. bLooped = true;
  2977. }
  2978. else
  2979. {
  2980. // things have backed up, which is bad since it'll probably result in a hitch in the animation playback
  2981. // but, don't play events again for the same time slice
  2982. return;
  2983. }
  2984. }
  2985. // This makes sure events that occur at the end of a sequence occur are
  2986. // sent before events that occur at the beginning of a sequence.
  2987. if (bLooped)
  2988. {
  2989. for (int i = 0; i < (int)seqdesc.numevents; i++)
  2990. {
  2991. // ignore all non-client-side events
  2992. if ( pevent[i].type & AE_TYPE_NEWEVENTSYSTEM )
  2993. {
  2994. if ( !( pevent[i].type & AE_TYPE_CLIENT ) )
  2995. continue;
  2996. }
  2997. else if ( pevent[i].event < 5000 ) //Adrian - Support the old event system
  2998. continue;
  2999. if ( pevent[i].cycle <= m_flPrevEventCycle )
  3000. continue;
  3001. if ( watch )
  3002. {
  3003. Msg( "%i FE %i Looped cycle %f, prev %f ev %f (time %.3f)\n",
  3004. gpGlobals->tickcount,
  3005. pevent[i].event,
  3006. pevent[i].cycle,
  3007. m_flPrevEventCycle,
  3008. flEventCycle,
  3009. gpGlobals->curtime );
  3010. }
  3011. FireEvent( GetAbsOrigin(), GetAbsAngles(), pevent[ i ].event, pevent[ i ].pszOptions() );
  3012. }
  3013. // Necessary to get the next loop working
  3014. m_flPrevEventCycle = flEventCycle - 0.001f;
  3015. }
  3016. for (int i = 0; i < (int)seqdesc.numevents; i++)
  3017. {
  3018. if ( pevent[i].type & AE_TYPE_NEWEVENTSYSTEM )
  3019. {
  3020. if ( !( pevent[i].type & AE_TYPE_CLIENT ) )
  3021. continue;
  3022. }
  3023. else if ( pevent[i].event < 5000 ) //Adrian - Support the old event system
  3024. continue;
  3025. if ( (pevent[i].cycle > m_flPrevEventCycle && pevent[i].cycle <= flEventCycle) )
  3026. {
  3027. if ( watch )
  3028. {
  3029. Msg( "%i (seq: %d) FE %i Normal cycle %f, prev %f ev %f (time %.3f)\n",
  3030. gpGlobals->tickcount,
  3031. GetSequence(),
  3032. pevent[i].event,
  3033. pevent[i].cycle,
  3034. m_flPrevEventCycle,
  3035. flEventCycle,
  3036. gpGlobals->curtime );
  3037. }
  3038. FireEvent( GetAbsOrigin(), GetAbsAngles(), pevent[ i ].event, pevent[ i ].pszOptions() );
  3039. }
  3040. }
  3041. m_flPrevEventCycle = flEventCycle;
  3042. }
  3043. //-----------------------------------------------------------------------------
  3044. // Purpose: Parses a muzzle effect event and sends it out for drawing
  3045. // Input : *options - event parameters in text format
  3046. // isFirstPerson - whether this is coming from an NPC or the player
  3047. // Output : Returns true on success, false on failure.
  3048. //-----------------------------------------------------------------------------
  3049. bool C_BaseAnimating::DispatchMuzzleEffect( const char *options, bool isFirstPerson )
  3050. {
  3051. const char *p = options;
  3052. char token[128];
  3053. int weaponType = 0;
  3054. // Get the first parameter
  3055. p = nexttoken( token, p, ' ' );
  3056. // Find the weapon type
  3057. if ( token[0] )
  3058. {
  3059. //TODO: Parse the type from a list instead
  3060. if ( Q_stricmp( token, "COMBINE" ) == 0 )
  3061. {
  3062. weaponType = MUZZLEFLASH_COMBINE;
  3063. }
  3064. else if ( Q_stricmp( token, "SMG1" ) == 0 )
  3065. {
  3066. weaponType = MUZZLEFLASH_SMG1;
  3067. }
  3068. else if ( Q_stricmp( token, "PISTOL" ) == 0 )
  3069. {
  3070. weaponType = MUZZLEFLASH_PISTOL;
  3071. }
  3072. else if ( Q_stricmp( token, "SHOTGUN" ) == 0 )
  3073. {
  3074. weaponType = MUZZLEFLASH_SHOTGUN;
  3075. }
  3076. else if ( Q_stricmp( token, "357" ) == 0 )
  3077. {
  3078. weaponType = MUZZLEFLASH_357;
  3079. }
  3080. else if ( Q_stricmp( token, "RPG" ) == 0 )
  3081. {
  3082. weaponType = MUZZLEFLASH_RPG;
  3083. }
  3084. else
  3085. {
  3086. //NOTENOTE: This means you specified an invalid muzzleflash type, check your spelling?
  3087. Assert( 0 );
  3088. }
  3089. }
  3090. else
  3091. {
  3092. //NOTENOTE: This means that there wasn't a proper parameter passed into the animevent
  3093. Assert( 0 );
  3094. return false;
  3095. }
  3096. // Get the second parameter
  3097. p = nexttoken( token, p, ' ' );
  3098. int attachmentIndex = -1;
  3099. // Find the attachment name
  3100. if ( token[0] )
  3101. {
  3102. attachmentIndex = LookupAttachment( token );
  3103. // Found an invalid attachment
  3104. if ( attachmentIndex <= 0 )
  3105. {
  3106. //NOTENOTE: This means that the attachment you're trying to use is invalid
  3107. Assert( 0 );
  3108. return false;
  3109. }
  3110. }
  3111. else
  3112. {
  3113. //NOTENOTE: This means that there wasn't a proper parameter passed into the animevent
  3114. Assert( 0 );
  3115. return false;
  3116. }
  3117. // Send it out
  3118. tempents->MuzzleFlash( weaponType, GetRefEHandle(), attachmentIndex, isFirstPerson );
  3119. return true;
  3120. }
  3121. //-----------------------------------------------------------------------------
  3122. //-----------------------------------------------------------------------------
  3123. void MaterialFootstepSound( C_BaseAnimating *pEnt, bool bLeftFoot, float flVolume )
  3124. {
  3125. trace_t tr;
  3126. Vector traceStart;
  3127. QAngle angles;
  3128. int attachment;
  3129. //!!!PERF - These string lookups here aren't the swiftest, but
  3130. // this doesn't get called very frequently unless a lot of NPCs
  3131. // are using this code.
  3132. if( bLeftFoot )
  3133. {
  3134. attachment = pEnt->LookupAttachment( "LeftFoot" );
  3135. }
  3136. else
  3137. {
  3138. attachment = pEnt->LookupAttachment( "RightFoot" );
  3139. }
  3140. if( attachment == -1 )
  3141. {
  3142. // Exit if this NPC doesn't have the proper attachments.
  3143. return;
  3144. }
  3145. pEnt->GetAttachment( attachment, traceStart, angles );
  3146. UTIL_TraceLine( traceStart, traceStart - Vector( 0, 0, 48.0f), MASK_SHOT_HULL, pEnt, COLLISION_GROUP_NONE, &tr );
  3147. if( tr.fraction < 1.0 && tr.m_pEnt )
  3148. {
  3149. surfacedata_t *psurf = physprops->GetSurfaceData( tr.surface.surfaceProps );
  3150. if( psurf )
  3151. {
  3152. EmitSound_t params;
  3153. if( bLeftFoot )
  3154. {
  3155. params.m_pSoundName = physprops->GetString(psurf->sounds.stepleft);
  3156. }
  3157. else
  3158. {
  3159. params.m_pSoundName = physprops->GetString(psurf->sounds.stepright);
  3160. }
  3161. CPASAttenuationFilter filter( pEnt, params.m_pSoundName );
  3162. params.m_bWarnOnDirectWaveReference = true;
  3163. params.m_flVolume = flVolume;
  3164. pEnt->EmitSound( filter, pEnt->entindex(), params );
  3165. }
  3166. }
  3167. }
  3168. //-----------------------------------------------------------------------------
  3169. // Purpose:
  3170. // Input : *origin -
  3171. // *angles -
  3172. // event -
  3173. // *options -
  3174. // numAttachments -
  3175. // attachments[] -
  3176. //-----------------------------------------------------------------------------
  3177. void C_BaseAnimating::FireEvent( const Vector& origin, const QAngle& angles, int event, const char *options )
  3178. {
  3179. switch( event )
  3180. {
  3181. case AE_CL_CREATE_PARTICLE_EFFECT:
  3182. {
  3183. int iAttachment = -1;
  3184. int iAttachType = PATTACH_ABSORIGIN_FOLLOW;
  3185. char szParticleEffect[256];
  3186. CSplitString splitString( options, " " );
  3187. if ( splitString.Count() < 2 )
  3188. {
  3189. Warning( "Invalid options specified for AE_CL_CREATE_PARTICLE_EFFECT event. Must specify \"<particle effect name> <attachment type> <attachment point>\"\n" );
  3190. return;
  3191. }
  3192. // Get the particle effect name
  3193. const char* mtoken = ModifyEventParticles( splitString[0] );
  3194. if ( !mtoken || mtoken[0] == '\0' )
  3195. return;
  3196. V_strncpy( szParticleEffect, mtoken, sizeof(szParticleEffect) );
  3197. const char *pszAttachType = splitString[1];
  3198. iAttachType = GetAttachTypeFromString( pszAttachType );
  3199. if ( iAttachType == -1 )
  3200. {
  3201. Warning( "Invalid attach type specified for AE_CL_CREATE_PARTICLE_EFFECT event. Trying to spawn effect '%s' with attach type of '%s'\n", szParticleEffect, pszAttachType );
  3202. return;
  3203. }
  3204. // Get the attachment point index
  3205. if ( splitString.Count() > 2 )
  3206. {
  3207. const char *pszAttachment = splitString[2];
  3208. iAttachment = atoi( pszAttachment );
  3209. // See if we can find any attachment points matching the name
  3210. if ( pszAttachment[0] != '0' && iAttachment == 0 )
  3211. {
  3212. iAttachment = LookupAttachment( pszAttachment );
  3213. if ( iAttachment <= 0 )
  3214. {
  3215. Warning( "Failed to find attachment point specified for AE_CL_CREATE_PARTICLE_EFFECT event. Trying to spawn effect '%s' on attachment named '%s'\n", szParticleEffect, pszAttachment );
  3216. return;
  3217. }
  3218. }
  3219. }
  3220. // Spawn the particle effect
  3221. ParticleProp()->Create( szParticleEffect, (ParticleAttachment_t)iAttachType, iAttachment );
  3222. }
  3223. break;
  3224. case AE_CL_REMOVE_PARTICLE_EFFECT:
  3225. {
  3226. int iAttachment = -1;
  3227. char szParticleEffect[256];
  3228. CSplitString splitString( options, " " );
  3229. if ( splitString.Count() < 1 )
  3230. {
  3231. Warning( "Invalid options specified for AE_CL_REMOVE_PARTICLE_EFFECT event. Must specify \"<particle effect name> <attachment point>\"\n" );
  3232. return;
  3233. }
  3234. const char* mtoken = ModifyEventParticles( splitString[0] );
  3235. if ( !mtoken || mtoken[0] == '\0' )
  3236. return;
  3237. V_strncpy( szParticleEffect, mtoken, sizeof(szParticleEffect) );
  3238. if ( splitString.Count() > 1 )
  3239. {
  3240. const char *pszAttachment = splitString[1];
  3241. iAttachment = atoi( pszAttachment );
  3242. // See if we can find any attachment points matching the name
  3243. if ( pszAttachment[0] != '0' && iAttachment == 0 )
  3244. {
  3245. iAttachment = LookupAttachment( pszAttachment );
  3246. if ( iAttachment <= 0 )
  3247. {
  3248. Warning( "Failed to find attachment point specified for AE_CL_REMOVE_PARTICLE_EFFECT event. Trying to spawn effect '%s' on attachment named '%s'\n", szParticleEffect, pszAttachment );
  3249. return;
  3250. }
  3251. }
  3252. }
  3253. if ( iAttachment != -1 )
  3254. {
  3255. ParticleProp()->StopParticlesWithNameAndAttachment( szParticleEffect, iAttachment );
  3256. }
  3257. else
  3258. {
  3259. // if no attachment specicied, just remove all particles with specified name from this entity
  3260. ParticleProp()->StopParticlesNamed( szParticleEffect );
  3261. }
  3262. }
  3263. break;
  3264. case AE_CL_PLAYSOUND:
  3265. {
  3266. CLocalPlayerFilter filter;
  3267. Vector attachOrigin;
  3268. QAngle attachAngles;
  3269. if ( m_Attachments.Count() > 0)
  3270. {
  3271. GetAttachment( 1, attachOrigin, attachAngles );
  3272. EmitSound( filter, GetSoundSourceIndex(), options, &attachOrigin );
  3273. }
  3274. else
  3275. {
  3276. EmitSound( filter, GetSoundSourceIndex(), options, &GetAbsOrigin() );
  3277. }
  3278. }
  3279. break;
  3280. case AE_CL_STOPSOUND:
  3281. {
  3282. StopSound( GetSoundSourceIndex(), options );
  3283. }
  3284. break;
  3285. case CL_EVENT_FOOTSTEP_LEFT:
  3286. {
  3287. #ifndef HL2MP
  3288. char pSoundName[256];
  3289. if ( !options || !options[0] )
  3290. {
  3291. options = "NPC_CombineS";
  3292. }
  3293. Vector vel;
  3294. EstimateAbsVelocity( vel );
  3295. // If he's moving fast enough, play the run sound
  3296. if ( vel.Length2DSqr() > RUN_SPEED_ESTIMATE_SQR )
  3297. {
  3298. Q_snprintf( pSoundName, 256, "%s.RunFootstepLeft", options );
  3299. }
  3300. else
  3301. {
  3302. Q_snprintf( pSoundName, 256, "%s.FootstepLeft", options );
  3303. }
  3304. EmitSound( pSoundName );
  3305. #endif
  3306. }
  3307. break;
  3308. case CL_EVENT_FOOTSTEP_RIGHT:
  3309. {
  3310. #ifndef HL2MP
  3311. char pSoundName[256];
  3312. if ( !options || !options[0] )
  3313. {
  3314. options = "NPC_CombineS";
  3315. }
  3316. Vector vel;
  3317. EstimateAbsVelocity( vel );
  3318. // If he's moving fast enough, play the run sound
  3319. if ( vel.Length2DSqr() > RUN_SPEED_ESTIMATE_SQR )
  3320. {
  3321. Q_snprintf( pSoundName, 256, "%s.RunFootstepRight", options );
  3322. }
  3323. else
  3324. {
  3325. Q_snprintf( pSoundName, 256, "%s.FootstepRight", options );
  3326. }
  3327. EmitSound( pSoundName );
  3328. #endif
  3329. }
  3330. break;
  3331. case CL_EVENT_MFOOTSTEP_LEFT:
  3332. {
  3333. MaterialFootstepSound( this, true, VOL_NORM * 0.5f );
  3334. }
  3335. break;
  3336. case CL_EVENT_MFOOTSTEP_RIGHT:
  3337. {
  3338. MaterialFootstepSound( this, false, VOL_NORM * 0.5f );
  3339. }
  3340. break;
  3341. case CL_EVENT_MFOOTSTEP_LEFT_LOUD:
  3342. {
  3343. MaterialFootstepSound( this, true, VOL_NORM );
  3344. }
  3345. break;
  3346. case CL_EVENT_MFOOTSTEP_RIGHT_LOUD:
  3347. {
  3348. MaterialFootstepSound( this, false, VOL_NORM );
  3349. }
  3350. break;
  3351. // Eject brass
  3352. case CL_EVENT_EJECTBRASS1:
  3353. if ( m_Attachments.Count() > 0 )
  3354. {
  3355. if ( MainViewOrigin().DistToSqr( GetAbsOrigin() ) < (256 * 256) )
  3356. {
  3357. Vector attachOrigin;
  3358. QAngle attachAngles;
  3359. if( GetAttachment( 2, attachOrigin, attachAngles ) )
  3360. {
  3361. tempents->EjectBrass( attachOrigin, attachAngles, GetAbsAngles(), atoi( options ) );
  3362. }
  3363. }
  3364. }
  3365. break;
  3366. case AE_MUZZLEFLASH:
  3367. {
  3368. // Send out the effect for a player
  3369. DispatchMuzzleEffect( options, true );
  3370. break;
  3371. }
  3372. case AE_NPC_MUZZLEFLASH:
  3373. {
  3374. // Send out the effect for an NPC
  3375. DispatchMuzzleEffect( options, false );
  3376. break;
  3377. }
  3378. // OBSOLETE EVENTS. REPLACED BY NEWER SYSTEMS.
  3379. // See below in FireObsoleteEvent() for comments on what to use instead.
  3380. case AE_CLIENT_EFFECT_ATTACH:
  3381. case CL_EVENT_DISPATCHEFFECT0:
  3382. case CL_EVENT_DISPATCHEFFECT1:
  3383. case CL_EVENT_DISPATCHEFFECT2:
  3384. case CL_EVENT_DISPATCHEFFECT3:
  3385. case CL_EVENT_DISPATCHEFFECT4:
  3386. case CL_EVENT_DISPATCHEFFECT5:
  3387. case CL_EVENT_DISPATCHEFFECT6:
  3388. case CL_EVENT_DISPATCHEFFECT7:
  3389. case CL_EVENT_DISPATCHEFFECT8:
  3390. case CL_EVENT_DISPATCHEFFECT9:
  3391. case CL_EVENT_MUZZLEFLASH0:
  3392. case CL_EVENT_MUZZLEFLASH1:
  3393. case CL_EVENT_MUZZLEFLASH2:
  3394. case CL_EVENT_MUZZLEFLASH3:
  3395. case CL_EVENT_NPC_MUZZLEFLASH0:
  3396. case CL_EVENT_NPC_MUZZLEFLASH1:
  3397. case CL_EVENT_NPC_MUZZLEFLASH2:
  3398. case CL_EVENT_NPC_MUZZLEFLASH3:
  3399. case CL_EVENT_SPARK0:
  3400. case CL_EVENT_SOUND:
  3401. FireObsoleteEvent( origin, angles, event, options );
  3402. break;
  3403. case AE_CL_ENABLE_BODYGROUP:
  3404. {
  3405. int index_ = FindBodygroupByName( options );
  3406. if ( index_ >= 0 )
  3407. {
  3408. SetBodygroup( index_, 1 );
  3409. }
  3410. }
  3411. break;
  3412. case AE_CL_DISABLE_BODYGROUP:
  3413. {
  3414. int index_ = FindBodygroupByName( options );
  3415. if ( index_ >= 0 )
  3416. {
  3417. SetBodygroup( index_, 0 );
  3418. }
  3419. }
  3420. break;
  3421. case AE_CL_BODYGROUP_SET_VALUE:
  3422. {
  3423. int value;
  3424. char token[256];
  3425. char szBodygroupName[256];
  3426. const char *p = options;
  3427. // Bodygroup Name
  3428. p = nexttoken(token, p, ' ');
  3429. Q_strncpy( szBodygroupName, token, sizeof(szBodygroupName) );
  3430. // Get the desired value
  3431. p = nexttoken(token, p, ' ');
  3432. value = token[0] ? atoi( token ) : 0;
  3433. int index_ = FindBodygroupByName( szBodygroupName );
  3434. if ( index_ >= 0 )
  3435. {
  3436. SetBodygroup( index_, value );
  3437. }
  3438. }
  3439. break;
  3440. default:
  3441. break;
  3442. }
  3443. }
  3444. //-----------------------------------------------------------------------------
  3445. // Purpose: These events are all obsolete events, left here to support old games.
  3446. // Their systems have all been replaced with better ones.
  3447. //-----------------------------------------------------------------------------
  3448. void C_BaseAnimating::FireObsoleteEvent( const Vector& origin, const QAngle& angles, int event, const char *options )
  3449. {
  3450. Vector attachOrigin;
  3451. QAngle attachAngles;
  3452. switch( event )
  3453. {
  3454. // Obsolete. Use the AE_CL_CREATE_PARTICLE_EFFECT event instead, which uses the artist driven particle system & editor.
  3455. case AE_CLIENT_EFFECT_ATTACH:
  3456. {
  3457. int iAttachment;
  3458. int iParam;
  3459. char token[128];
  3460. char effectFunc[128];
  3461. const char *p = options;
  3462. p = nexttoken(token, p, ' ');
  3463. Q_strncpy( effectFunc, token, sizeof(effectFunc) );
  3464. p = nexttoken(token, p, ' ');
  3465. iAttachment = token[0] ? atoi(token) : -1;
  3466. p = nexttoken(token, p, ' ');
  3467. iParam = token[0] ? atoi(token) : 0;
  3468. if ( iAttachment != -1 && m_Attachments.Count() >= iAttachment )
  3469. {
  3470. GetAttachment( iAttachment, attachOrigin, attachAngles );
  3471. // Fill out the generic data
  3472. CEffectData data;
  3473. data.m_vOrigin = attachOrigin;
  3474. data.m_vAngles = attachAngles;
  3475. AngleVectors( attachAngles, &data.m_vNormal );
  3476. data.m_hEntity = GetRefEHandle();
  3477. data.m_nAttachmentIndex = iAttachment + 1;
  3478. data.m_fFlags = iParam;
  3479. DispatchEffect( effectFunc, data );
  3480. }
  3481. }
  3482. break;
  3483. // Obsolete. Use the AE_CL_CREATE_PARTICLE_EFFECT event instead, which uses the artist driven particle system & editor.
  3484. case CL_EVENT_DISPATCHEFFECT0:
  3485. case CL_EVENT_DISPATCHEFFECT1:
  3486. case CL_EVENT_DISPATCHEFFECT2:
  3487. case CL_EVENT_DISPATCHEFFECT3:
  3488. case CL_EVENT_DISPATCHEFFECT4:
  3489. case CL_EVENT_DISPATCHEFFECT5:
  3490. case CL_EVENT_DISPATCHEFFECT6:
  3491. case CL_EVENT_DISPATCHEFFECT7:
  3492. case CL_EVENT_DISPATCHEFFECT8:
  3493. case CL_EVENT_DISPATCHEFFECT9:
  3494. {
  3495. int iAttachment = -1;
  3496. // First person muzzle flashes
  3497. switch (event)
  3498. {
  3499. case CL_EVENT_DISPATCHEFFECT0:
  3500. iAttachment = 0;
  3501. break;
  3502. case CL_EVENT_DISPATCHEFFECT1:
  3503. iAttachment = 1;
  3504. break;
  3505. case CL_EVENT_DISPATCHEFFECT2:
  3506. iAttachment = 2;
  3507. break;
  3508. case CL_EVENT_DISPATCHEFFECT3:
  3509. iAttachment = 3;
  3510. break;
  3511. case CL_EVENT_DISPATCHEFFECT4:
  3512. iAttachment = 4;
  3513. break;
  3514. case CL_EVENT_DISPATCHEFFECT5:
  3515. iAttachment = 5;
  3516. break;
  3517. case CL_EVENT_DISPATCHEFFECT6:
  3518. iAttachment = 6;
  3519. break;
  3520. case CL_EVENT_DISPATCHEFFECT7:
  3521. iAttachment = 7;
  3522. break;
  3523. case CL_EVENT_DISPATCHEFFECT8:
  3524. iAttachment = 8;
  3525. break;
  3526. case CL_EVENT_DISPATCHEFFECT9:
  3527. iAttachment = 9;
  3528. break;
  3529. }
  3530. if ( iAttachment != -1 && m_Attachments.Count() > iAttachment )
  3531. {
  3532. GetAttachment( iAttachment+1, attachOrigin, attachAngles );
  3533. // Fill out the generic data
  3534. CEffectData data;
  3535. data.m_vOrigin = attachOrigin;
  3536. data.m_vAngles = attachAngles;
  3537. AngleVectors( attachAngles, &data.m_vNormal );
  3538. data.m_hEntity = GetRefEHandle();
  3539. data.m_nAttachmentIndex = iAttachment + 1;
  3540. DispatchEffect( options, data );
  3541. }
  3542. }
  3543. break;
  3544. // Obsolete. Use the AE_MUZZLEFLASH / AE_NPC_MUZZLEFLASH events instead.
  3545. case CL_EVENT_MUZZLEFLASH0:
  3546. case CL_EVENT_MUZZLEFLASH1:
  3547. case CL_EVENT_MUZZLEFLASH2:
  3548. case CL_EVENT_MUZZLEFLASH3:
  3549. case CL_EVENT_NPC_MUZZLEFLASH0:
  3550. case CL_EVENT_NPC_MUZZLEFLASH1:
  3551. case CL_EVENT_NPC_MUZZLEFLASH2:
  3552. case CL_EVENT_NPC_MUZZLEFLASH3:
  3553. {
  3554. int iAttachment = -1;
  3555. bool bFirstPerson = true;
  3556. // First person muzzle flashes
  3557. switch (event)
  3558. {
  3559. case CL_EVENT_MUZZLEFLASH0:
  3560. iAttachment = 0;
  3561. break;
  3562. case CL_EVENT_MUZZLEFLASH1:
  3563. iAttachment = 1;
  3564. break;
  3565. case CL_EVENT_MUZZLEFLASH2:
  3566. iAttachment = 2;
  3567. break;
  3568. case CL_EVENT_MUZZLEFLASH3:
  3569. iAttachment = 3;
  3570. break;
  3571. // Third person muzzle flashes
  3572. case CL_EVENT_NPC_MUZZLEFLASH0:
  3573. iAttachment = 0;
  3574. bFirstPerson = false;
  3575. break;
  3576. case CL_EVENT_NPC_MUZZLEFLASH1:
  3577. iAttachment = 1;
  3578. bFirstPerson = false;
  3579. break;
  3580. case CL_EVENT_NPC_MUZZLEFLASH2:
  3581. iAttachment = 2;
  3582. bFirstPerson = false;
  3583. break;
  3584. case CL_EVENT_NPC_MUZZLEFLASH3:
  3585. iAttachment = 3;
  3586. bFirstPerson = false;
  3587. break;
  3588. }
  3589. if ( iAttachment != -1 && m_Attachments.Count() > iAttachment )
  3590. {
  3591. GetAttachment( iAttachment+1, attachOrigin, attachAngles );
  3592. int entId = render->GetViewEntity();
  3593. ClientEntityHandle_t hEntity = ClientEntityList().EntIndexToHandle( entId );
  3594. tempents->MuzzleFlash( attachOrigin, attachAngles, atoi( options ), hEntity, bFirstPerson );
  3595. }
  3596. }
  3597. break;
  3598. // Obsolete: Use the AE_CL_CREATE_PARTICLE_EFFECT event instead, which uses the artist driven particle system & editor.
  3599. case CL_EVENT_SPARK0:
  3600. {
  3601. Vector vecForward;
  3602. GetAttachment( 1, attachOrigin, attachAngles );
  3603. AngleVectors( attachAngles, &vecForward );
  3604. g_pEffects->Sparks( attachOrigin, atoi( options ), 1, &vecForward );
  3605. }
  3606. break;
  3607. // Obsolete: Use the AE_CL_PLAYSOUND event instead, which doesn't rely on a magic number in the .qc
  3608. case CL_EVENT_SOUND:
  3609. {
  3610. CLocalPlayerFilter filter;
  3611. if ( m_Attachments.Count() > 0)
  3612. {
  3613. GetAttachment( 1, attachOrigin, attachAngles );
  3614. EmitSound( filter, GetSoundSourceIndex(), options, &attachOrigin );
  3615. }
  3616. else
  3617. {
  3618. EmitSound( filter, GetSoundSourceIndex(), options );
  3619. }
  3620. }
  3621. break;
  3622. default:
  3623. break;
  3624. }
  3625. }
  3626. //-----------------------------------------------------------------------------
  3627. // Purpose:
  3628. //-----------------------------------------------------------------------------
  3629. bool C_BaseAnimating::IsSelfAnimating()
  3630. {
  3631. if ( m_bClientSideAnimation )
  3632. return true;
  3633. // Yes, we use animtime.
  3634. int iMoveType = GetMoveType();
  3635. if ( iMoveType != MOVETYPE_STEP &&
  3636. iMoveType != MOVETYPE_NONE &&
  3637. iMoveType != MOVETYPE_WALK &&
  3638. iMoveType != MOVETYPE_FLY &&
  3639. iMoveType != MOVETYPE_FLYGRAVITY )
  3640. {
  3641. return true;
  3642. }
  3643. return false;
  3644. }
  3645. //-----------------------------------------------------------------------------
  3646. // Purpose: Called by networking code when an entity is new to the PVS or comes down with the EF_NOINTERP flag set.
  3647. // The position history data is flushed out right after this call, so we need to store off the current data
  3648. // in the latched fields so we try to interpolate
  3649. // Input : *ent -
  3650. // full_reset -
  3651. //-----------------------------------------------------------------------------
  3652. void C_BaseAnimating::ResetLatched( void )
  3653. {
  3654. // Reset the IK
  3655. if ( m_pIk )
  3656. {
  3657. delete m_pIk;
  3658. m_pIk = NULL;
  3659. }
  3660. BaseClass::ResetLatched();
  3661. }
  3662. //-----------------------------------------------------------------------------
  3663. // Purpose:
  3664. // Output : Returns true on success, false on failure.
  3665. //-----------------------------------------------------------------------------
  3666. bool C_BaseAnimating::Interpolate( float flCurrentTime )
  3667. {
  3668. // ragdolls don't need interpolation
  3669. if ( m_pRagdoll )
  3670. return true;
  3671. VPROF( "C_BaseAnimating::Interpolate" );
  3672. Vector oldOrigin;
  3673. QAngle oldAngles;
  3674. Vector oldVel;
  3675. float flOldCycle = GetCycle();
  3676. int nChangeFlags = 0;
  3677. if ( !m_bClientSideAnimation )
  3678. m_iv_flCycle.SetLooping( IsSequenceLooping( GetSequence() ) );
  3679. int bNoMoreChanges;
  3680. int retVal = BaseInterpolatePart1( flCurrentTime, oldOrigin, oldAngles, oldVel, bNoMoreChanges );
  3681. if ( retVal == INTERPOLATE_STOP )
  3682. {
  3683. if ( bNoMoreChanges )
  3684. RemoveFromInterpolationList();
  3685. return true;
  3686. }
  3687. // Did cycle change?
  3688. if( GetCycle() != flOldCycle )
  3689. nChangeFlags |= ANIMATION_CHANGED;
  3690. if ( bNoMoreChanges )
  3691. RemoveFromInterpolationList();
  3692. BaseInterpolatePart2( oldOrigin, oldAngles, oldVel, nChangeFlags );
  3693. return true;
  3694. }
  3695. //-----------------------------------------------------------------------------
  3696. // returns true if we're currently being ragdolled
  3697. //-----------------------------------------------------------------------------
  3698. bool C_BaseAnimating::IsRagdoll() const
  3699. {
  3700. return m_pRagdoll && (m_nRenderFX == kRenderFxRagdoll);
  3701. }
  3702. //-----------------------------------------------------------------------------
  3703. // returns true if we're currently being ragdolled
  3704. //-----------------------------------------------------------------------------
  3705. bool C_BaseAnimating::IsAboutToRagdoll() const
  3706. {
  3707. return (m_nRenderFX == kRenderFxRagdoll);
  3708. }
  3709. //-----------------------------------------------------------------------------
  3710. // Lets us check our sequence number after a network update
  3711. //-----------------------------------------------------------------------------
  3712. int C_BaseAnimating::RestoreData( const char *context, int slot, int type )
  3713. {
  3714. int retVal = BaseClass::RestoreData( context, slot, type );
  3715. CStudioHdr *pHdr = GetModelPtr();
  3716. if( pHdr && m_nSequence >= pHdr->GetNumSeq() )
  3717. {
  3718. // Don't let a network update give us an invalid sequence
  3719. m_nSequence = 0;
  3720. }
  3721. return retVal;
  3722. }
  3723. //-----------------------------------------------------------------------------
  3724. // implements these so ragdolls can handle frustum culling & leaf visibility
  3725. //-----------------------------------------------------------------------------
  3726. void C_BaseAnimating::GetRenderBounds( Vector& theMins, Vector& theMaxs )
  3727. {
  3728. if ( IsRagdoll() )
  3729. {
  3730. m_pRagdoll->GetRagdollBounds( theMins, theMaxs );
  3731. }
  3732. else if ( GetModel() )
  3733. {
  3734. CStudioHdr *pStudioHdr = GetModelPtr();
  3735. if ( !pStudioHdr|| !pStudioHdr->SequencesAvailable() || GetSequence() == -1 )
  3736. {
  3737. theMins = vec3_origin;
  3738. theMaxs = vec3_origin;
  3739. return;
  3740. }
  3741. if (!VectorCompare( vec3_origin, pStudioHdr->view_bbmin() ) || !VectorCompare( vec3_origin, pStudioHdr->view_bbmax() ))
  3742. {
  3743. // clipping bounding box
  3744. VectorCopy ( pStudioHdr->view_bbmin(), theMins);
  3745. VectorCopy ( pStudioHdr->view_bbmax(), theMaxs);
  3746. }
  3747. else
  3748. {
  3749. // movement bounding box
  3750. VectorCopy ( pStudioHdr->hull_min(), theMins);
  3751. VectorCopy ( pStudioHdr->hull_max(), theMaxs);
  3752. }
  3753. mstudioseqdesc_t &seqdesc = pStudioHdr->pSeqdesc( GetSequence() );
  3754. VectorMin( seqdesc.bbmin, theMins, theMins );
  3755. VectorMax( seqdesc.bbmax, theMaxs, theMaxs );
  3756. }
  3757. else
  3758. {
  3759. theMins = vec3_origin;
  3760. theMaxs = vec3_origin;
  3761. }
  3762. // Scale this up depending on if our model is currently scaling
  3763. const float flScale = GetModelScale();
  3764. theMaxs *= flScale;
  3765. theMins *= flScale;
  3766. }
  3767. //-----------------------------------------------------------------------------
  3768. // implements these so ragdolls can handle frustum culling & leaf visibility
  3769. //-----------------------------------------------------------------------------
  3770. const Vector& C_BaseAnimating::GetRenderOrigin( void )
  3771. {
  3772. if ( IsRagdoll() )
  3773. {
  3774. return m_pRagdoll->GetRagdollOrigin();
  3775. }
  3776. else
  3777. {
  3778. return BaseClass::GetRenderOrigin();
  3779. }
  3780. }
  3781. const QAngle& C_BaseAnimating::GetRenderAngles( void )
  3782. {
  3783. if ( IsRagdoll() )
  3784. {
  3785. return vec3_angle;
  3786. }
  3787. else
  3788. {
  3789. return BaseClass::GetRenderAngles();
  3790. }
  3791. }
  3792. void C_BaseAnimating::RagdollMoved( void )
  3793. {
  3794. SetAbsOrigin( m_pRagdoll->GetRagdollOrigin() );
  3795. SetAbsAngles( vec3_angle );
  3796. Vector mins, maxs;
  3797. m_pRagdoll->GetRagdollBounds( mins, maxs );
  3798. SetCollisionBounds( mins, maxs );
  3799. // If the ragdoll moves, its render-to-texture shadow is dirty
  3800. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  3801. }
  3802. //-----------------------------------------------------------------------------
  3803. // Purpose: My physics object has been updated, react or extract data
  3804. //-----------------------------------------------------------------------------
  3805. void C_BaseAnimating::VPhysicsUpdate( IPhysicsObject *pPhysics )
  3806. {
  3807. // FIXME: Should make sure the physics objects being passed in
  3808. // is the ragdoll physics object, but I think it's pretty safe not to check
  3809. if (IsRagdoll())
  3810. {
  3811. m_pRagdoll->VPhysicsUpdate( pPhysics );
  3812. RagdollMoved();
  3813. return;
  3814. }
  3815. BaseClass::VPhysicsUpdate( pPhysics );
  3816. }
  3817. //-----------------------------------------------------------------------------
  3818. // Purpose:
  3819. // Input : updateType -
  3820. //-----------------------------------------------------------------------------
  3821. void C_BaseAnimating::PreDataUpdate( DataUpdateType_t updateType )
  3822. {
  3823. VPROF( "C_BaseAnimating::PreDataUpdate" );
  3824. m_flOldCycle = GetCycle();
  3825. m_nOldSequence = GetSequence();
  3826. m_flOldModelScale = GetModelScale();
  3827. int i;
  3828. for ( i=0;i<MAXSTUDIOBONECTRLS;i++ )
  3829. {
  3830. m_flOldEncodedController[i] = m_flEncodedController[i];
  3831. }
  3832. for ( i=0;i<MAXSTUDIOPOSEPARAM;i++ )
  3833. {
  3834. m_flOldPoseParameters[i] = m_flPoseParameter[i];
  3835. }
  3836. BaseClass::PreDataUpdate( updateType );
  3837. }
  3838. void C_BaseAnimating::NotifyShouldTransmit( ShouldTransmitState_t state )
  3839. {
  3840. BaseClass::NotifyShouldTransmit( state );
  3841. if ( state == SHOULDTRANSMIT_START )
  3842. {
  3843. // If he's been firing a bunch, then he comes back into the PVS, his muzzle flash
  3844. // will show up even if he isn't firing now.
  3845. DisableMuzzleFlash();
  3846. m_nPrevResetEventsParity = m_nResetEventsParity;
  3847. m_nEventSequence = GetSequence();
  3848. }
  3849. }
  3850. //-----------------------------------------------------------------------------
  3851. // Purpose:
  3852. // Input : updateType -
  3853. //-----------------------------------------------------------------------------
  3854. void C_BaseAnimating::PostDataUpdate( DataUpdateType_t updateType )
  3855. {
  3856. BaseClass::PostDataUpdate( updateType );
  3857. if ( m_bClientSideAnimation )
  3858. {
  3859. SetCycle( m_flOldCycle );
  3860. AddToClientSideAnimationList();
  3861. }
  3862. else
  3863. {
  3864. RemoveFromClientSideAnimationList();
  3865. }
  3866. bool bBoneControllersChanged = false;
  3867. int i;
  3868. for ( i=0;i<MAXSTUDIOBONECTRLS && !bBoneControllersChanged;i++ )
  3869. {
  3870. if ( m_flOldEncodedController[i] != m_flEncodedController[i] )
  3871. {
  3872. bBoneControllersChanged = true;
  3873. }
  3874. }
  3875. bool bPoseParametersChanged = false;
  3876. for ( i=0;i<MAXSTUDIOPOSEPARAM && !bPoseParametersChanged;i++ )
  3877. {
  3878. if ( m_flOldPoseParameters[i] != m_flPoseParameter[i] )
  3879. {
  3880. bPoseParametersChanged = true;
  3881. }
  3882. }
  3883. // Cycle change? Then re-render
  3884. bool bAnimationChanged = m_flOldCycle != GetCycle() || bBoneControllersChanged || bPoseParametersChanged;
  3885. bool bSequenceChanged = m_nOldSequence != GetSequence();
  3886. bool bScaleChanged = ( m_flOldModelScale != GetModelScale() );
  3887. if ( bAnimationChanged || bSequenceChanged || bScaleChanged )
  3888. {
  3889. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  3890. }
  3891. if ( bAnimationChanged || bSequenceChanged )
  3892. {
  3893. if ( m_bClientSideAnimation )
  3894. {
  3895. ClientSideAnimationChanged();
  3896. }
  3897. }
  3898. // reset prev cycle if new sequence
  3899. if (m_nNewSequenceParity != m_nPrevNewSequenceParity)
  3900. {
  3901. // It's important not to call Reset() on a static prop, because if we call
  3902. // Reset(), then the entity will stay in the interpolated entities list
  3903. // forever, wasting CPU.
  3904. MDLCACHE_CRITICAL_SECTION();
  3905. CStudioHdr *hdr = GetModelPtr();
  3906. if ( hdr && !( hdr->flags() & STUDIOHDR_FLAGS_STATIC_PROP ) )
  3907. {
  3908. m_iv_flCycle.Reset();
  3909. }
  3910. }
  3911. }
  3912. //-----------------------------------------------------------------------------
  3913. // Purpose:
  3914. // Input : bnewentity -
  3915. //-----------------------------------------------------------------------------
  3916. void C_BaseAnimating::OnPreDataChanged( DataUpdateType_t updateType )
  3917. {
  3918. BaseClass::OnPreDataChanged( updateType );
  3919. m_bLastClientSideFrameReset = m_bClientSideFrameReset;
  3920. }
  3921. bool C_BaseAnimating::ForceSetupBonesAtTime( matrix3x4_t *pBonesOut, float flTime )
  3922. {
  3923. // blow the cached prev bones
  3924. InvalidateBoneCache();
  3925. // reset root position to flTime
  3926. Interpolate( flTime );
  3927. // Setup bone state at the given time
  3928. return SetupBones( pBonesOut, MAXSTUDIOBONES, BONE_USED_BY_ANYTHING, flTime );
  3929. }
  3930. bool C_BaseAnimating::GetRagdollInitBoneArrays( matrix3x4_t *pDeltaBones0, matrix3x4_t *pDeltaBones1, matrix3x4_t *pCurrentBones, float boneDt )
  3931. {
  3932. bool bSuccess = true;
  3933. if ( !ForceSetupBonesAtTime( pDeltaBones0, gpGlobals->curtime - boneDt ) )
  3934. bSuccess = false;
  3935. if ( !ForceSetupBonesAtTime( pDeltaBones1, gpGlobals->curtime ) )
  3936. bSuccess = false;
  3937. float ragdollCreateTime = PhysGetSyncCreateTime();
  3938. if ( ragdollCreateTime != gpGlobals->curtime )
  3939. {
  3940. // The next simulation frame begins before the end of this frame
  3941. // so initialize the ragdoll at that time so that it will reach the current
  3942. // position at curtime. Otherwise the ragdoll will simulate forward from curtime
  3943. // and pop into the future a bit at this point of transition
  3944. if ( !ForceSetupBonesAtTime( pCurrentBones, ragdollCreateTime ) )
  3945. bSuccess = false;
  3946. }
  3947. else
  3948. {
  3949. memcpy( pCurrentBones, m_CachedBoneData.Base(), sizeof( matrix3x4_t ) * m_CachedBoneData.Count() );
  3950. }
  3951. return bSuccess;
  3952. }
  3953. C_BaseAnimating *C_BaseAnimating::CreateRagdollCopy()
  3954. {
  3955. //Adrian: We now create a separate entity that becomes this entity's ragdoll.
  3956. //That way the server side version of this entity can go away.
  3957. //Plus we can hook save/restore code to these ragdolls so they don't fall on restore anymore.
  3958. C_ClientRagdoll *pRagdoll = new C_ClientRagdoll( false );
  3959. if ( pRagdoll == NULL )
  3960. return NULL;
  3961. TermRopes();
  3962. const model_t *pModel = GetModel();
  3963. const char *pModelName = modelinfo->GetModelName( pModel );
  3964. if ( pRagdoll->InitializeAsClientEntity( pModelName, RENDER_GROUP_OPAQUE_ENTITY ) == false )
  3965. {
  3966. pRagdoll->Release();
  3967. return NULL;
  3968. }
  3969. // move my current model instance to the ragdoll's so decals are preserved.
  3970. SnatchModelInstance( pRagdoll );
  3971. // We need to take these from the entity
  3972. pRagdoll->SetAbsOrigin( GetAbsOrigin() );
  3973. pRagdoll->SetAbsAngles( GetAbsAngles() );
  3974. pRagdoll->IgniteRagdoll( this );
  3975. pRagdoll->TransferDissolveFrom( this );
  3976. pRagdoll->InitModelEffects();
  3977. if ( AddRagdollToFadeQueue() == true )
  3978. {
  3979. pRagdoll->m_bImportant = NPC_IsImportantNPC( this );
  3980. s_RagdollLRU.MoveToTopOfLRU( pRagdoll, pRagdoll->m_bImportant );
  3981. pRagdoll->m_bFadeOut = true;
  3982. }
  3983. m_builtRagdoll = true;
  3984. AddEffects( EF_NODRAW );
  3985. if ( IsEffectActive( EF_NOSHADOW ) )
  3986. {
  3987. pRagdoll->AddEffects( EF_NOSHADOW );
  3988. }
  3989. pRagdoll->m_nRenderFX = kRenderFxRagdoll;
  3990. pRagdoll->SetRenderMode( GetRenderMode() );
  3991. pRagdoll->SetRenderColor( GetRenderColor().r, GetRenderColor().g, GetRenderColor().b, GetRenderColor().a );
  3992. pRagdoll->m_nBody = m_nBody;
  3993. pRagdoll->m_nSkin = GetSkin();
  3994. pRagdoll->m_vecForce = m_vecForce;
  3995. pRagdoll->m_nForceBone = m_nForceBone;
  3996. pRagdoll->SetNextClientThink( CLIENT_THINK_ALWAYS );
  3997. pRagdoll->SetModelName( AllocPooledString(pModelName) );
  3998. pRagdoll->SetModelScale( GetModelScale() );
  3999. return pRagdoll;
  4000. }
  4001. C_BaseAnimating *C_BaseAnimating::BecomeRagdollOnClient()
  4002. {
  4003. MoveToLastReceivedPosition( true );
  4004. GetAbsOrigin();
  4005. C_BaseAnimating *pRagdoll = CreateRagdollCopy();
  4006. if ( pRagdoll )
  4007. {
  4008. matrix3x4_t boneDelta0[MAXSTUDIOBONES];
  4009. matrix3x4_t boneDelta1[MAXSTUDIOBONES];
  4010. matrix3x4_t currentBones[MAXSTUDIOBONES];
  4011. const float boneDt = 0.1f;
  4012. bool bInitAsClient = false;
  4013. bool bInitBoneArrays = GetRagdollInitBoneArrays( boneDelta0, boneDelta1, currentBones, boneDt );
  4014. if ( bInitBoneArrays )
  4015. {
  4016. bInitAsClient = pRagdoll->InitAsClientRagdoll( boneDelta0, boneDelta1, currentBones, boneDt );
  4017. }
  4018. if ( !bInitAsClient || !bInitBoneArrays )
  4019. {
  4020. Warning( "C_BaseAnimating::BecomeRagdollOnClient failed. pRagdoll:%p bInitBoneArrays:%d bInitAsClient:%d\n",
  4021. pRagdoll, bInitBoneArrays, bInitAsClient );
  4022. pRagdoll->Release();
  4023. return NULL;
  4024. }
  4025. }
  4026. return pRagdoll;
  4027. }
  4028. bool C_BaseAnimating::InitAsClientRagdoll( const matrix3x4_t *pDeltaBones0, const matrix3x4_t *pDeltaBones1, const matrix3x4_t *pCurrentBonePosition, float boneDt, bool bFixedConstraints )
  4029. {
  4030. CStudioHdr *hdr = GetModelPtr();
  4031. if ( !hdr || m_pRagdoll || m_builtRagdoll )
  4032. return false;
  4033. m_builtRagdoll = true;
  4034. // Store off our old mins & maxs
  4035. m_vecPreRagdollMins = WorldAlignMins();
  4036. m_vecPreRagdollMaxs = WorldAlignMaxs();
  4037. // Force MOVETYPE_STEP interpolation
  4038. MoveType_t savedMovetype = GetMoveType();
  4039. SetMoveType( MOVETYPE_STEP );
  4040. // HACKHACK: force time to last interpolation position
  4041. m_flPlaybackRate = 1;
  4042. m_pRagdoll = CreateRagdoll( this, hdr, m_vecForce, m_nForceBone, pDeltaBones0, pDeltaBones1, pCurrentBonePosition, boneDt, bFixedConstraints );
  4043. // Cause the entity to recompute its shadow type and make a
  4044. // version which only updates when physics state changes
  4045. // NOTE: We have to do this after m_pRagdoll is assigned above
  4046. // because that's what ShadowCastType uses to figure out which type of shadow to use.
  4047. DestroyShadow();
  4048. CreateShadow();
  4049. // Cache off ragdoll bone positions/quaternions
  4050. if ( m_bStoreRagdollInfo && m_pRagdoll )
  4051. {
  4052. matrix3x4_t parentTransform;
  4053. AngleMatrix( GetAbsAngles(), GetAbsOrigin(), parentTransform );
  4054. // FIXME/CHECK: This might be too expensive to do every frame???
  4055. SaveRagdollInfo( hdr->numbones(), parentTransform, m_BoneAccessor );
  4056. }
  4057. SetMoveType( savedMovetype );
  4058. // Now set the dieragdoll sequence to get transforms for all
  4059. // non-simulated bones
  4060. m_nRestoreSequence = GetSequence();
  4061. SetSequence( SelectWeightedSequence( ACT_DIERAGDOLL ) );
  4062. m_nPrevSequence = GetSequence();
  4063. m_flPlaybackRate = 0;
  4064. UpdatePartitionListEntry();
  4065. NoteRagdollCreationTick( this );
  4066. UpdateVisibility();
  4067. #if defined( REPLAY_ENABLED )
  4068. // If Replay is enabled on server, add an entry to the ragdoll recorder for this entity
  4069. ConVar* pReplayEnable = (ConVar*)cvar->FindVar( "replay_enable" );
  4070. if ( m_pRagdoll && pReplayEnable && pReplayEnable->GetInt() && !engine->IsPlayingDemo() && !engine->IsPlayingTimeDemo() )
  4071. {
  4072. CReplayRagdollRecorder& RagdollRecorder = CReplayRagdollRecorder::Instance();
  4073. int nStartTick = TIME_TO_TICKS( engine->GetLastTimeStamp() );
  4074. RagdollRecorder.AddEntry( this, nStartTick, m_pRagdoll->RagdollBoneCount() );
  4075. }
  4076. #endif
  4077. return true;
  4078. }
  4079. //-----------------------------------------------------------------------------
  4080. // Purpose:
  4081. // Input : bnewentity -
  4082. //-----------------------------------------------------------------------------
  4083. void C_BaseAnimating::OnDataChanged( DataUpdateType_t updateType )
  4084. {
  4085. // don't let server change sequences after becoming a ragdoll
  4086. if ( m_pRagdoll && GetSequence() != m_nPrevSequence )
  4087. {
  4088. SetSequence( m_nPrevSequence );
  4089. m_flPlaybackRate = 0;
  4090. }
  4091. if ( !m_pRagdoll && m_nRestoreSequence != -1 )
  4092. {
  4093. SetSequence( m_nRestoreSequence );
  4094. m_nRestoreSequence = -1;
  4095. }
  4096. if (updateType == DATA_UPDATE_CREATED)
  4097. {
  4098. m_nPrevSequence = -1;
  4099. m_nRestoreSequence = -1;
  4100. }
  4101. bool modelchanged = false;
  4102. // UNDONE: The base class does this as well. So this is kind of ugly
  4103. // but getting a model by index is pretty cheap...
  4104. const model_t *pModel = modelinfo->GetModel( GetModelIndex() );
  4105. if ( pModel != GetModel() )
  4106. {
  4107. modelchanged = true;
  4108. }
  4109. BaseClass::OnDataChanged( updateType );
  4110. if ( (updateType == DATA_UPDATE_CREATED) || modelchanged )
  4111. {
  4112. ResetLatched();
  4113. // if you have this pose parameter, activate HL1-style lipsync/wave envelope tracking
  4114. if ( LookupPoseParameter( LIPSYNC_POSEPARAM_NAME ) != -1 )
  4115. {
  4116. MouthInfo().ActivateEnvelope();
  4117. }
  4118. }
  4119. // If there's a significant change, make sure the shadow updates
  4120. if ( modelchanged || (GetSequence() != m_nPrevSequence))
  4121. {
  4122. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  4123. m_nPrevSequence = GetSequence();
  4124. }
  4125. // Only need to think if animating client side
  4126. if ( m_bClientSideAnimation )
  4127. {
  4128. // Check to see if we should reset our frame
  4129. if ( m_bClientSideFrameReset != m_bLastClientSideFrameReset )
  4130. {
  4131. ResetClientsideFrame();
  4132. }
  4133. }
  4134. // build a ragdoll if necessary
  4135. if ( m_nRenderFX == kRenderFxRagdoll && !m_builtRagdoll )
  4136. {
  4137. BecomeRagdollOnClient();
  4138. }
  4139. //HACKHACK!!!
  4140. if ( m_nRenderFX == kRenderFxRagdoll && m_builtRagdoll == true )
  4141. {
  4142. if ( m_pRagdoll == NULL )
  4143. AddEffects( EF_NODRAW );
  4144. }
  4145. if ( m_pRagdoll && m_nRenderFX != kRenderFxRagdoll )
  4146. {
  4147. ClearRagdoll();
  4148. }
  4149. // If ragdolling and get EF_NOINTERP, we probably were dead and are now respawning,
  4150. // don't do blend out of ragdoll at respawn spot.
  4151. if ( IsNoInterpolationFrame() &&
  4152. m_pRagdollInfo &&
  4153. m_pRagdollInfo->m_bActive )
  4154. {
  4155. Msg( "delete ragdoll due to nointerp\n" );
  4156. // Remove ragdoll info
  4157. delete m_pRagdollInfo;
  4158. m_pRagdollInfo = NULL;
  4159. }
  4160. }
  4161. //-----------------------------------------------------------------------------
  4162. // Purpose:
  4163. //-----------------------------------------------------------------------------
  4164. void C_BaseAnimating::AddEntity( void )
  4165. {
  4166. // Server says don't interpolate this frame, so set previous info to new info.
  4167. if ( IsNoInterpolationFrame() )
  4168. {
  4169. ResetLatched();
  4170. }
  4171. BaseClass::AddEntity();
  4172. }
  4173. //-----------------------------------------------------------------------------
  4174. // Purpose: Get the index of the attachment point with the specified name
  4175. //-----------------------------------------------------------------------------
  4176. int C_BaseAnimating::LookupAttachment( const char *pAttachmentName )
  4177. {
  4178. CStudioHdr *hdr = GetModelPtr();
  4179. if ( !hdr )
  4180. {
  4181. return -1;
  4182. }
  4183. // NOTE: Currently, the network uses 0 to mean "no attachment"
  4184. // thus the client must add one to the index of the attachment
  4185. // UNDONE: Make the server do this too to be consistent.
  4186. return Studio_FindAttachment( hdr, pAttachmentName ) + 1;
  4187. }
  4188. //-----------------------------------------------------------------------------
  4189. // Purpose: Get a random index of an attachment point with the specified substring in its name
  4190. //-----------------------------------------------------------------------------
  4191. int C_BaseAnimating::LookupRandomAttachment( const char *pAttachmentNameSubstring )
  4192. {
  4193. CStudioHdr *hdr = GetModelPtr();
  4194. if ( !hdr )
  4195. {
  4196. return -1;
  4197. }
  4198. // NOTE: Currently, the network uses 0 to mean "no attachment"
  4199. // thus the client must add one to the index of the attachment
  4200. // UNDONE: Make the server do this too to be consistent.
  4201. return Studio_FindRandomAttachment( hdr, pAttachmentNameSubstring ) + 1;
  4202. }
  4203. void C_BaseAnimating::ClientSideAnimationChanged()
  4204. {
  4205. if ( !m_bClientSideAnimation || m_ClientSideAnimationListHandle == INVALID_CLIENTSIDEANIMATION_LIST_HANDLE )
  4206. return;
  4207. MDLCACHE_CRITICAL_SECTION();
  4208. clientanimating_t &anim = g_ClientSideAnimationList.Element(m_ClientSideAnimationListHandle);
  4209. Assert(anim.pAnimating == this);
  4210. anim.flags = ComputeClientSideAnimationFlags();
  4211. m_SequenceTransitioner.CheckForSequenceChange(
  4212. GetModelPtr(),
  4213. GetSequence(),
  4214. m_nNewSequenceParity != m_nPrevNewSequenceParity,
  4215. !IsNoInterpolationFrame()
  4216. );
  4217. }
  4218. unsigned int C_BaseAnimating::ComputeClientSideAnimationFlags()
  4219. {
  4220. return FCLIENTANIM_SEQUENCE_CYCLE;
  4221. }
  4222. void C_BaseAnimating::UpdateClientSideAnimation()
  4223. {
  4224. // Update client side animation
  4225. if ( m_bClientSideAnimation )
  4226. {
  4227. Assert( m_ClientSideAnimationListHandle != INVALID_CLIENTSIDEANIMATION_LIST_HANDLE );
  4228. if ( GetSequence() != -1 )
  4229. {
  4230. // latch old values
  4231. OnLatchInterpolatedVariables( LATCH_ANIMATION_VAR );
  4232. // move frame forward
  4233. FrameAdvance( 0.0f ); // 0 means to use the time we last advanced instead of a constant
  4234. }
  4235. }
  4236. else
  4237. {
  4238. Assert( m_ClientSideAnimationListHandle == INVALID_CLIENTSIDEANIMATION_LIST_HANDLE );
  4239. }
  4240. }
  4241. void C_BaseAnimating::Simulate()
  4242. {
  4243. if ( m_bDelayInitModelEffects )
  4244. {
  4245. DelayedInitModelEffects();
  4246. }
  4247. if ( gpGlobals->frametime != 0.0f )
  4248. {
  4249. DoAnimationEvents( GetModelPtr() );
  4250. }
  4251. BaseClass::Simulate();
  4252. if ( IsNoInterpolationFrame() )
  4253. {
  4254. ResetLatched();
  4255. }
  4256. if ( GetSequence() != -1 && m_pRagdoll && ( m_nRenderFX != kRenderFxRagdoll ) )
  4257. {
  4258. ClearRagdoll();
  4259. }
  4260. }
  4261. bool C_BaseAnimating::TestCollision( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
  4262. {
  4263. if ( ray.m_IsRay && IsSolidFlagSet( FSOLID_CUSTOMRAYTEST ))
  4264. {
  4265. if (!TestHitboxes( ray, fContentsMask, tr ))
  4266. return true;
  4267. return tr.DidHit();
  4268. }
  4269. if ( !ray.m_IsRay && IsSolidFlagSet( FSOLID_CUSTOMBOXTEST ))
  4270. {
  4271. if (!TestHitboxes( ray, fContentsMask, tr ))
  4272. return true;
  4273. return true;
  4274. }
  4275. // We shouldn't get here.
  4276. Assert(0);
  4277. return false;
  4278. }
  4279. // UNDONE: This almost works. The client entities have no control over their solid box
  4280. // Also they have no ability to expose FSOLID_ flags to the engine to force the accurate
  4281. // collision tests.
  4282. // Add those and the client hitboxes will be robust
  4283. bool C_BaseAnimating::TestHitboxes( const Ray_t &ray, unsigned int fContentsMask, trace_t& tr )
  4284. {
  4285. VPROF( "C_BaseAnimating::TestHitboxes" );
  4286. MDLCACHE_CRITICAL_SECTION();
  4287. CStudioHdr *pStudioHdr = GetModelPtr();
  4288. if (!pStudioHdr)
  4289. return false;
  4290. mstudiohitboxset_t *set = pStudioHdr->pHitboxSet( m_nHitboxSet );
  4291. if ( !set || !set->numhitboxes )
  4292. return false;
  4293. // Use vcollide for box traces.
  4294. if ( !ray.m_IsRay )
  4295. return false;
  4296. // This *has* to be true for the existing code to function correctly.
  4297. Assert( ray.m_StartOffset == vec3_origin );
  4298. CBoneCache *pCache = GetBoneCache( pStudioHdr );
  4299. matrix3x4_t *hitboxbones[MAXSTUDIOBONES];
  4300. pCache->ReadCachedBonePointers( hitboxbones, pStudioHdr->numbones() );
  4301. if ( TraceToStudio( physprops, ray, pStudioHdr, set, hitboxbones, fContentsMask, GetRenderOrigin(), GetModelScale(), tr ) )
  4302. {
  4303. mstudiobbox_t *pbox = set->pHitbox( tr.hitbox );
  4304. mstudiobone_t *pBone = pStudioHdr->pBone(pbox->bone);
  4305. tr.surface.name = "**studio**";
  4306. tr.surface.flags = SURF_HITBOX;
  4307. tr.surface.surfaceProps = physprops->GetSurfaceIndex( pBone->pszSurfaceProp() );
  4308. if ( IsRagdoll() )
  4309. {
  4310. IPhysicsObject *pReplace = m_pRagdoll->GetElement( tr.physicsbone );
  4311. if ( pReplace )
  4312. {
  4313. VPhysicsSetObject( NULL );
  4314. VPhysicsSetObject( pReplace );
  4315. }
  4316. }
  4317. }
  4318. return true;
  4319. }
  4320. //-----------------------------------------------------------------------------
  4321. // Purpose: Check sequence framerate
  4322. // Input : iSequence -
  4323. // Output : float
  4324. //-----------------------------------------------------------------------------
  4325. float C_BaseAnimating::GetSequenceCycleRate( CStudioHdr *pStudioHdr, int iSequence )
  4326. {
  4327. float t = SequenceDuration( pStudioHdr, iSequence );
  4328. if ( t != 0.0f )
  4329. {
  4330. return 1.0f / t;
  4331. }
  4332. return t;
  4333. }
  4334. float C_BaseAnimating::GetAnimTimeInterval( void ) const
  4335. {
  4336. #define MAX_ANIMTIME_INTERVAL 0.2f
  4337. float flInterval = MIN( gpGlobals->curtime - m_flAnimTime, MAX_ANIMTIME_INTERVAL );
  4338. return flInterval;
  4339. }
  4340. //-----------------------------------------------------------------------------
  4341. // Sets the cycle, marks the entity as being dirty
  4342. //-----------------------------------------------------------------------------
  4343. void C_BaseAnimating::SetCycle( float flCycle )
  4344. {
  4345. if ( m_flCycle != flCycle )
  4346. {
  4347. m_flCycle = flCycle;
  4348. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  4349. }
  4350. }
  4351. //-----------------------------------------------------------------------------
  4352. // Sets the sequence, marks the entity as being dirty
  4353. //-----------------------------------------------------------------------------
  4354. void C_BaseAnimating::SetSequence( int nSequence )
  4355. {
  4356. if ( m_nSequence != nSequence )
  4357. {
  4358. /*
  4359. CStudioHdr *hdr = GetModelPtr();
  4360. // Assert( hdr );
  4361. if ( hdr )
  4362. {
  4363. Assert( nSequence >= 0 && nSequence < hdr->GetNumSeq() );
  4364. }
  4365. */
  4366. m_nSequence = nSequence;
  4367. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  4368. if ( m_bClientSideAnimation )
  4369. {
  4370. ClientSideAnimationChanged();
  4371. }
  4372. }
  4373. }
  4374. //=========================================================
  4375. // StudioFrameAdvance - advance the animation frame up some interval (default 0.1) into the future
  4376. //=========================================================
  4377. void C_BaseAnimating::StudioFrameAdvance()
  4378. {
  4379. if ( m_bClientSideAnimation )
  4380. return;
  4381. CStudioHdr *hdr = GetModelPtr();
  4382. if ( !hdr )
  4383. return;
  4384. #ifdef DEBUG
  4385. bool watch = dbganimmodel.GetString()[0] && V_stristr( hdr->pszName(), dbganimmodel.GetString() );
  4386. #else
  4387. bool watch = false; // Q_strstr( hdr->name, "rifle" ) ? true : false;
  4388. #endif
  4389. //if (!anim.prevanimtime)
  4390. //{
  4391. //anim.prevanimtime = m_flAnimTime = gpGlobals->curtime;
  4392. //}
  4393. // How long since last animtime
  4394. float flInterval = GetAnimTimeInterval();
  4395. if (flInterval <= 0.001)
  4396. {
  4397. // Msg("%s : %s : %5.3f (skip)\n", STRING(pev->classname), GetSequenceName( GetSequence() ), GetCycle() );
  4398. return;
  4399. }
  4400. UpdateModelScale();
  4401. //anim.prevanimtime = m_flAnimTime;
  4402. float cycleAdvance = flInterval * GetSequenceCycleRate( hdr, GetSequence() ) * m_flPlaybackRate;
  4403. float flNewCycle = GetCycle() + cycleAdvance;
  4404. m_flAnimTime = gpGlobals->curtime;
  4405. if ( watch )
  4406. {
  4407. Msg("%s %6.3f : %6.3f (%.3f)\n", GetClassname(), gpGlobals->curtime, m_flAnimTime, flInterval );
  4408. }
  4409. if ( flNewCycle < 0.0f || flNewCycle >= 1.0f )
  4410. {
  4411. if ( IsSequenceLooping( hdr, GetSequence() ) )
  4412. {
  4413. flNewCycle -= (int)(flNewCycle);
  4414. }
  4415. else
  4416. {
  4417. flNewCycle = (flNewCycle < 0.0f) ? 0.0f : 1.0f;
  4418. }
  4419. m_bSequenceFinished = true; // just in case it wasn't caught in GetEvents
  4420. }
  4421. SetCycle( flNewCycle );
  4422. m_flGroundSpeed = GetSequenceGroundSpeed( hdr, GetSequence() ) * GetModelScale();
  4423. #if 0
  4424. // I didn't have a test case for this, but it seems like the right thing to do. Check multi-player!
  4425. // Msg("%s : %s : %5.1f\n", GetClassname(), GetSequenceName( GetSequence() ), GetCycle() );
  4426. InvalidatePhysicsRecursive( ANIMATION_CHANGED );
  4427. #endif
  4428. if ( watch )
  4429. {
  4430. Msg("%s : %s : %5.1f\n", GetClassname(), GetSequenceName( GetSequence() ), GetCycle() );
  4431. }
  4432. }
  4433. float C_BaseAnimating::GetSequenceGroundSpeed( CStudioHdr *pStudioHdr, int iSequence )
  4434. {
  4435. float t = SequenceDuration( pStudioHdr, iSequence );
  4436. if (t > 0)
  4437. {
  4438. return GetSequenceMoveDist( pStudioHdr, iSequence ) / t;
  4439. }
  4440. else
  4441. {
  4442. return 0;
  4443. }
  4444. }
  4445. //-----------------------------------------------------------------------------
  4446. // Purpose:
  4447. //
  4448. // Input : iSequence -
  4449. //
  4450. // Output : float
  4451. //-----------------------------------------------------------------------------
  4452. float C_BaseAnimating::GetSequenceMoveDist( CStudioHdr *pStudioHdr, int iSequence )
  4453. {
  4454. Vector vecReturn;
  4455. ::GetSequenceLinearMotion( pStudioHdr, iSequence, m_flPoseParameter, &vecReturn );
  4456. return vecReturn.Length();
  4457. }
  4458. //-----------------------------------------------------------------------------
  4459. // Purpose:
  4460. //
  4461. // Input : iSequence -
  4462. // *pVec -
  4463. //
  4464. //-----------------------------------------------------------------------------
  4465. void C_BaseAnimating::GetSequenceLinearMotion( int iSequence, Vector *pVec )
  4466. {
  4467. ::GetSequenceLinearMotion( GetModelPtr(), iSequence, m_flPoseParameter, pVec );
  4468. }
  4469. void C_BaseAnimating::GetBlendedLinearVelocity( Vector *pVec )
  4470. {
  4471. Vector vecDist;
  4472. float flDuration;
  4473. GetSequenceLinearMotion( GetSequence(), &vecDist );
  4474. flDuration = SequenceDuration( GetSequence() );
  4475. VectorScale( vecDist, 1.0 / flDuration, *pVec );
  4476. Vector tmp;
  4477. for (int i = m_SequenceTransitioner.m_animationQueue.Count() - 2; i >= 0; i--)
  4478. {
  4479. C_AnimationLayer *blend = &m_SequenceTransitioner.m_animationQueue[i];
  4480. GetSequenceLinearMotion( blend->m_nSequence, &vecDist );
  4481. flDuration = SequenceDuration( blend->m_nSequence );
  4482. VectorScale( vecDist, 1.0 / flDuration, tmp );
  4483. float flWeight = blend->GetFadeout( gpGlobals->curtime );
  4484. *pVec = Lerp( flWeight, *pVec, tmp );
  4485. }
  4486. }
  4487. //-----------------------------------------------------------------------------
  4488. // Purpose:
  4489. // Input : flInterval -
  4490. // Output : float
  4491. //-----------------------------------------------------------------------------
  4492. float C_BaseAnimating::FrameAdvance( float flInterval )
  4493. {
  4494. CStudioHdr *hdr = GetModelPtr();
  4495. if ( !hdr )
  4496. return 0.0f;
  4497. #ifdef DEBUG
  4498. bool bWatch = dbganimmodel.GetString()[0] && V_stristr( hdr->pszName(), dbganimmodel.GetString() );
  4499. #else
  4500. bool bWatch = false; // Q_strstr( hdr->name, "medkit_large" ) ? true : false;
  4501. #endif
  4502. float curtime = gpGlobals->curtime;
  4503. if (flInterval == 0.0f)
  4504. {
  4505. flInterval = ( curtime - m_flAnimTime );
  4506. if (flInterval <= 0.001f)
  4507. {
  4508. return 0.0f;
  4509. }
  4510. }
  4511. if ( !m_flAnimTime )
  4512. {
  4513. flInterval = 0.0f;
  4514. }
  4515. float cyclerate = GetSequenceCycleRate( hdr, GetSequence() );
  4516. float addcycle = flInterval * cyclerate * m_flPlaybackRate;
  4517. if( GetServerIntendedCycle() != -1.0f )
  4518. {
  4519. // The server would like us to ease in a correction so that we will animate the same on the client and server.
  4520. // So we will actually advance the average of what we would have done and what the server wants.
  4521. float serverCycle = GetServerIntendedCycle();
  4522. float serverAdvance = serverCycle - GetCycle();
  4523. bool adjustOkay = serverAdvance > 0.0f;// only want to go forward. backing up looks really jarring, even when slight
  4524. if( serverAdvance < -0.8f )
  4525. {
  4526. // Oh wait, it was just a wraparound from .9 to .1.
  4527. serverAdvance += 1;
  4528. adjustOkay = true;
  4529. }
  4530. if( adjustOkay )
  4531. {
  4532. float originalAdvance = addcycle;
  4533. addcycle = (serverAdvance + addcycle) / 2;
  4534. const float MAX_CYCLE_ADJUSTMENT = 0.1f;
  4535. addcycle = MIN( MAX_CYCLE_ADJUSTMENT, addcycle );// Don't do too big of a jump; it's too jarring as well.
  4536. DevMsg( 2, "(%d): Cycle latch used to correct %.2f in to %.2f instead of %.2f.\n",
  4537. entindex(), GetCycle(), GetCycle() + addcycle, GetCycle() + originalAdvance );
  4538. }
  4539. SetServerIntendedCycle(-1.0f); // Only use a correction once, it isn't valid any time but right now.
  4540. }
  4541. float flNewCycle = GetCycle() + addcycle;
  4542. m_flAnimTime = curtime;
  4543. if ( bWatch )
  4544. {
  4545. Msg("%i CLIENT Time: %6.3f : (Interval %f) : cycle %f rate %f add %f\n",
  4546. gpGlobals->tickcount, gpGlobals->curtime, flInterval, flNewCycle, cyclerate, addcycle );
  4547. }
  4548. if ( (flNewCycle < 0.0f) || (flNewCycle >= 1.0f) )
  4549. {
  4550. if ( IsSequenceLooping( hdr, GetSequence() ) )
  4551. {
  4552. flNewCycle -= (int)(flNewCycle);
  4553. }
  4554. else
  4555. {
  4556. flNewCycle = (flNewCycle < 0.0f) ? 0.0f : 1.0f;
  4557. }
  4558. m_bSequenceFinished = true;
  4559. }
  4560. SetCycle( flNewCycle );
  4561. return flInterval;
  4562. }
  4563. // Stubs for weapon prediction
  4564. void C_BaseAnimating::ResetSequenceInfo( void )
  4565. {
  4566. if ( GetSequence() == -1 )
  4567. {
  4568. // This shouldn't happen. Setting m_nSequence blindly is a horrible coding practice.
  4569. SetSequence( 0 );
  4570. }
  4571. if ( IsDynamicModelLoading() )
  4572. {
  4573. m_bResetSequenceInfoOnLoad = true;
  4574. return;
  4575. }
  4576. CStudioHdr *pStudioHdr = GetModelPtr();
  4577. m_flGroundSpeed = GetSequenceGroundSpeed( pStudioHdr, GetSequence() ) * GetModelScale();
  4578. m_bSequenceLoops = ( ( GetSequenceFlags( pStudioHdr, GetSequence() ) & STUDIO_LOOPING ) != 0 );
  4579. // m_flAnimTime = gpGlobals->time;
  4580. m_flPlaybackRate = 1.0;
  4581. m_bSequenceFinished = false;
  4582. m_flLastEventCheck = 0;
  4583. m_nNewSequenceParity = ( m_nNewSequenceParity + 1 ) & EF_PARITY_MASK;
  4584. m_nResetEventsParity = ( m_nResetEventsParity + 1 ) & EF_PARITY_MASK;
  4585. // FIXME: why is this called here? Nothing should have changed to make this nessesary
  4586. if ( pStudioHdr )
  4587. {
  4588. SetEventIndexForSequence( pStudioHdr->pSeqdesc( GetSequence() ) );
  4589. }
  4590. }
  4591. //=========================================================
  4592. //=========================================================
  4593. bool C_BaseAnimating::IsSequenceLooping( CStudioHdr *pStudioHdr, int iSequence )
  4594. {
  4595. return (::GetSequenceFlags( pStudioHdr, iSequence ) & STUDIO_LOOPING) != 0;
  4596. }
  4597. float C_BaseAnimating::SequenceDuration( CStudioHdr *pStudioHdr, int iSequence )
  4598. {
  4599. if ( !pStudioHdr )
  4600. {
  4601. return 0.1f;
  4602. }
  4603. if ( !pStudioHdr->SequencesAvailable() )
  4604. {
  4605. return 0.1;
  4606. }
  4607. if (iSequence >= pStudioHdr->GetNumSeq() || iSequence < 0 )
  4608. {
  4609. DevWarning( 2, "C_BaseAnimating::SequenceDuration( %d ) out of range\n", iSequence );
  4610. return 0.1;
  4611. }
  4612. return Studio_Duration( pStudioHdr, iSequence, m_flPoseParameter );
  4613. }
  4614. int C_BaseAnimating::FindTransitionSequence( int iCurrentSequence, int iGoalSequence, int *piDir )
  4615. {
  4616. CStudioHdr *hdr = GetModelPtr();
  4617. if ( !hdr )
  4618. {
  4619. return -1;
  4620. }
  4621. if (piDir == NULL)
  4622. {
  4623. int iDir = 1;
  4624. int sequence = ::FindTransitionSequence( hdr, iCurrentSequence, iGoalSequence, &iDir );
  4625. if (iDir != 1)
  4626. return -1;
  4627. else
  4628. return sequence;
  4629. }
  4630. return ::FindTransitionSequence( hdr, iCurrentSequence, iGoalSequence, piDir );
  4631. }
  4632. void C_BaseAnimating::SetBodygroup( int iGroup, int iValue )
  4633. {
  4634. // SetBodygroup is not supported on pending dynamic models. Wait for it to load!
  4635. // XXX TODO we could buffer up the group and value if we really needed to. -henryg
  4636. Assert( GetModelPtr() );
  4637. ::SetBodygroup( GetModelPtr( ), m_nBody, iGroup, iValue );
  4638. }
  4639. int C_BaseAnimating::GetBodygroup( int iGroup )
  4640. {
  4641. Assert( IsDynamicModelLoading() || GetModelPtr() );
  4642. return IsDynamicModelLoading() ? 0 : ::GetBodygroup( GetModelPtr( ), m_nBody, iGroup );
  4643. }
  4644. const char *C_BaseAnimating::GetBodygroupName( int iGroup )
  4645. {
  4646. Assert( IsDynamicModelLoading() || GetModelPtr() );
  4647. return IsDynamicModelLoading() ? "" : ::GetBodygroupName( GetModelPtr( ), iGroup );
  4648. }
  4649. int C_BaseAnimating::FindBodygroupByName( const char *name )
  4650. {
  4651. Assert( IsDynamicModelLoading() || GetModelPtr() );
  4652. return IsDynamicModelLoading() ? -1 : ::FindBodygroupByName( GetModelPtr( ), name );
  4653. }
  4654. int C_BaseAnimating::GetBodygroupCount( int iGroup )
  4655. {
  4656. Assert( IsDynamicModelLoading() || GetModelPtr() );
  4657. return IsDynamicModelLoading() ? 0 : ::GetBodygroupCount( GetModelPtr( ), iGroup );
  4658. }
  4659. int C_BaseAnimating::GetNumBodyGroups( void )
  4660. {
  4661. Assert( IsDynamicModelLoading() || GetModelPtr() );
  4662. return IsDynamicModelLoading() ? 0 : ::GetNumBodyGroups( GetModelPtr( ) );
  4663. }
  4664. //-----------------------------------------------------------------------------
  4665. // Purpose:
  4666. // Input : setnum -
  4667. //-----------------------------------------------------------------------------
  4668. void C_BaseAnimating::SetHitboxSet( int setnum )
  4669. {
  4670. if ( IsDynamicModelLoading() )
  4671. return;
  4672. #ifdef _DEBUG
  4673. CStudioHdr *pStudioHdr = GetModelPtr();
  4674. if ( !pStudioHdr )
  4675. return;
  4676. if (setnum > pStudioHdr->numhitboxsets())
  4677. {
  4678. // Warn if an bogus hitbox set is being used....
  4679. static bool s_bWarned = false;
  4680. if (!s_bWarned)
  4681. {
  4682. Warning("Using bogus hitbox set in entity %s!\n", GetClassname() );
  4683. s_bWarned = true;
  4684. }
  4685. setnum = 0;
  4686. }
  4687. #endif
  4688. m_nHitboxSet = setnum;
  4689. }
  4690. //-----------------------------------------------------------------------------
  4691. // Purpose:
  4692. // Input : *setname -
  4693. //-----------------------------------------------------------------------------
  4694. void C_BaseAnimating::SetHitboxSetByName( const char *setname )
  4695. {
  4696. if ( IsDynamicModelLoading() )
  4697. return;
  4698. m_nHitboxSet = FindHitboxSetByName( GetModelPtr(), setname );
  4699. }
  4700. //-----------------------------------------------------------------------------
  4701. // Purpose:
  4702. // Output : int
  4703. //-----------------------------------------------------------------------------
  4704. int C_BaseAnimating::GetHitboxSet( void )
  4705. {
  4706. return m_nHitboxSet;
  4707. }
  4708. //-----------------------------------------------------------------------------
  4709. // Purpose:
  4710. // Output : char const
  4711. //-----------------------------------------------------------------------------
  4712. const char *C_BaseAnimating::GetHitboxSetName( void )
  4713. {
  4714. if ( IsDynamicModelLoading() )
  4715. return "";
  4716. return ::GetHitboxSetName( GetModelPtr(), m_nHitboxSet );
  4717. }
  4718. //-----------------------------------------------------------------------------
  4719. // Purpose:
  4720. // Output : int
  4721. //-----------------------------------------------------------------------------
  4722. int C_BaseAnimating::GetHitboxSetCount( void )
  4723. {
  4724. if ( IsDynamicModelLoading() )
  4725. return 0;
  4726. return ::GetHitboxSetCount( GetModelPtr() );
  4727. }
  4728. static Vector hullcolor[8] =
  4729. {
  4730. Vector( 1.0, 1.0, 1.0 ),
  4731. Vector( 1.0, 0.5, 0.5 ),
  4732. Vector( 0.5, 1.0, 0.5 ),
  4733. Vector( 1.0, 1.0, 0.5 ),
  4734. Vector( 0.5, 0.5, 1.0 ),
  4735. Vector( 1.0, 0.5, 1.0 ),
  4736. Vector( 0.5, 1.0, 1.0 ),
  4737. Vector( 1.0, 1.0, 1.0 )
  4738. };
  4739. //-----------------------------------------------------------------------------
  4740. // Purpose: Draw the current hitboxes
  4741. //-----------------------------------------------------------------------------
  4742. void C_BaseAnimating::DrawClientHitboxes( float duration /*= 0.0f*/, bool monocolor /*= false*/ )
  4743. {
  4744. CStudioHdr *pStudioHdr = GetModelPtr();
  4745. if ( !pStudioHdr )
  4746. return;
  4747. mstudiohitboxset_t *set =pStudioHdr->pHitboxSet( m_nHitboxSet );
  4748. if ( !set )
  4749. return;
  4750. Vector position;
  4751. QAngle angles;
  4752. int r = 255;
  4753. int g = 0;
  4754. int b = 0;
  4755. for ( int i = 0; i < set->numhitboxes; i++ )
  4756. {
  4757. mstudiobbox_t *pbox = set->pHitbox( i );
  4758. GetBonePosition( pbox->bone, position, angles );
  4759. if ( !monocolor )
  4760. {
  4761. int j = (pbox->group % 8);
  4762. r = ( int ) ( 255.0f * hullcolor[j][0] );
  4763. g = ( int ) ( 255.0f * hullcolor[j][1] );
  4764. b = ( int ) ( 255.0f * hullcolor[j][2] );
  4765. }
  4766. if ( debugoverlay )
  4767. {
  4768. debugoverlay->AddBoxOverlay( position, pbox->bbmin, pbox->bbmax, angles, r, g, b, 0 ,duration );
  4769. }
  4770. }
  4771. }
  4772. //-----------------------------------------------------------------------------
  4773. // Purpose:
  4774. // Input : activity -
  4775. // Output : int C_BaseAnimating::SelectWeightedSequence
  4776. //-----------------------------------------------------------------------------
  4777. int C_BaseAnimating::SelectWeightedSequence ( int activity )
  4778. {
  4779. Assert( activity != ACT_INVALID );
  4780. return ::SelectWeightedSequence( GetModelPtr(), activity );
  4781. }
  4782. int C_BaseAnimating::SelectWeightedSequenceFromModifiers( Activity activity, CUtlSymbol *pActivityModifiers, int iModifierCount )
  4783. {
  4784. Assert( activity != ACT_INVALID );
  4785. Assert( GetModelPtr() );
  4786. return GetModelPtr()->SelectWeightedSequenceFromModifiers( activity, pActivityModifiers, iModifierCount );
  4787. }
  4788. //=========================================================
  4789. //=========================================================
  4790. int C_BaseAnimating::LookupPoseParameter( CStudioHdr *pstudiohdr, const char *szName )
  4791. {
  4792. if ( !pstudiohdr )
  4793. return 0;
  4794. for (int i = 0; i < pstudiohdr->GetNumPoseParameters(); i++)
  4795. {
  4796. if (stricmp( pstudiohdr->pPoseParameter( i ).pszName(), szName ) == 0)
  4797. {
  4798. return i;
  4799. }
  4800. }
  4801. // AssertMsg( 0, UTIL_VarArgs( "poseparameter %s couldn't be mapped!!!\n", szName ) );
  4802. return -1; // Error
  4803. }
  4804. //=========================================================
  4805. //=========================================================
  4806. float C_BaseAnimating::SetPoseParameter( CStudioHdr *pStudioHdr, const char *szName, float flValue )
  4807. {
  4808. return SetPoseParameter( pStudioHdr, LookupPoseParameter( pStudioHdr, szName ), flValue );
  4809. }
  4810. float C_BaseAnimating::SetPoseParameter( CStudioHdr *pStudioHdr, int iParameter, float flValue )
  4811. {
  4812. if ( !pStudioHdr )
  4813. {
  4814. Assert(!"C_BaseAnimating::SetPoseParameter: model missing");
  4815. return flValue;
  4816. }
  4817. if (iParameter >= 0)
  4818. {
  4819. float flNewValue;
  4820. flValue = Studio_SetPoseParameter( pStudioHdr, iParameter, flValue, flNewValue );
  4821. m_flPoseParameter[ iParameter ] = flNewValue;
  4822. }
  4823. return flValue;
  4824. }
  4825. //-----------------------------------------------------------------------------
  4826. // Purpose:
  4827. // Input : *label -
  4828. // Output : int
  4829. //-----------------------------------------------------------------------------
  4830. int C_BaseAnimating::LookupSequence( const char *label )
  4831. {
  4832. Assert( GetModelPtr() );
  4833. return ::LookupSequence( GetModelPtr(), label );
  4834. }
  4835. void C_BaseAnimating::Release()
  4836. {
  4837. ClearRagdoll();
  4838. BaseClass::Release();
  4839. }
  4840. void C_BaseAnimating::Clear( void )
  4841. {
  4842. InvalidateMdlCache();
  4843. Q_memset(&m_mouth, 0, sizeof(m_mouth));
  4844. m_flCycle = 0;
  4845. m_flOldCycle = 0;
  4846. m_bResetSequenceInfoOnLoad = false;
  4847. m_bDynamicModelPending = false;
  4848. m_AutoRefModelIndex.Clear();
  4849. BaseClass::Clear();
  4850. }
  4851. //-----------------------------------------------------------------------------
  4852. // Purpose: Clear current ragdoll
  4853. //-----------------------------------------------------------------------------
  4854. void C_BaseAnimating::ClearRagdoll()
  4855. {
  4856. if ( m_pRagdoll )
  4857. {
  4858. // immediately mark the member ragdoll as being NULL,
  4859. // so that we have no reentrancy problems with the delete
  4860. // (such as the disappearance of the ragdoll physics waking up
  4861. // IVP which causes other objects to move and have a touch
  4862. // callback on the ragdoll entity, which was a crash on TF)
  4863. // That is to say: it is vital that the member be cleared out
  4864. // BEFORE the delete occurs.
  4865. CRagdoll * RESTRICT pDoomed = m_pRagdoll;
  4866. m_pRagdoll = NULL;
  4867. delete pDoomed;
  4868. // Set to null so that the destructor's call to DestroyObject won't destroy
  4869. // m_pObjects[ 0 ] twice since that's the physics object for the prop
  4870. VPhysicsSetObject( NULL );
  4871. // If we have ragdoll mins/maxs, we've just come out of ragdoll, so restore them
  4872. if ( m_vecPreRagdollMins != vec3_origin || m_vecPreRagdollMaxs != vec3_origin )
  4873. {
  4874. SetCollisionBounds( m_vecPreRagdollMins, m_vecPreRagdollMaxs );
  4875. }
  4876. #if defined( REPLAY_ENABLED )
  4877. // Delete entry from ragdoll recorder if Replay is enabled on server
  4878. ConVar* pReplayEnable = (ConVar*)cvar->FindVar( "replay_enable" );
  4879. if ( pReplayEnable && pReplayEnable->GetInt() && !engine->IsPlayingDemo() && !engine->IsPlayingTimeDemo() )
  4880. {
  4881. CReplayRagdollRecorder& RagdollRecorder = CReplayRagdollRecorder::Instance();
  4882. RagdollRecorder.StopRecordingRagdoll( this );
  4883. }
  4884. #endif
  4885. }
  4886. m_builtRagdoll = false;
  4887. }
  4888. //-----------------------------------------------------------------------------
  4889. // Purpose: Looks up an activity by name.
  4890. // Input : label - Name of the activity, ie "ACT_IDLE".
  4891. // Output : Returns the activity ID or ACT_INVALID.
  4892. //-----------------------------------------------------------------------------
  4893. int C_BaseAnimating::LookupActivity( const char *label )
  4894. {
  4895. Assert( GetModelPtr() );
  4896. return ::LookupActivity( GetModelPtr(), label );
  4897. }
  4898. //-----------------------------------------------------------------------------
  4899. // Purpose:
  4900. //
  4901. // Input : iSequence -
  4902. //
  4903. // Output : char
  4904. //-----------------------------------------------------------------------------
  4905. const char *C_BaseAnimating::GetSequenceActivityName( int iSequence )
  4906. {
  4907. if( iSequence == -1 )
  4908. {
  4909. return "Not Found!";
  4910. }
  4911. if ( !GetModelPtr() )
  4912. return "No model!";
  4913. return ::GetSequenceActivityName( GetModelPtr(), iSequence );
  4914. }
  4915. //=========================================================
  4916. //=========================================================
  4917. float C_BaseAnimating::SetBoneController ( int iController, float flValue )
  4918. {
  4919. Assert( GetModelPtr() );
  4920. CStudioHdr *pmodel = GetModelPtr();
  4921. Assert(iController >= 0 && iController < NUM_BONECTRLS);
  4922. float controller = m_flEncodedController[iController];
  4923. float retVal = Studio_SetController( pmodel, iController, flValue, controller );
  4924. m_flEncodedController[iController] = controller;
  4925. return retVal;
  4926. }
  4927. void C_BaseAnimating::GetAimEntOrigin( IClientEntity *pAttachedTo, Vector *pAbsOrigin, QAngle *pAbsAngles )
  4928. {
  4929. CBaseEntity *pMoveParent;
  4930. if ( IsEffectActive( EF_BONEMERGE ) && IsEffectActive( EF_BONEMERGE_FASTCULL ) && (pMoveParent = GetMoveParent()) != NULL )
  4931. {
  4932. // Doing this saves a lot of CPU.
  4933. *pAbsOrigin = pMoveParent->WorldSpaceCenter();
  4934. *pAbsAngles = pMoveParent->GetRenderAngles();
  4935. }
  4936. else
  4937. {
  4938. if ( !m_pBoneMergeCache || !m_pBoneMergeCache->GetAimEntOrigin( pAbsOrigin, pAbsAngles ) )
  4939. BaseClass::GetAimEntOrigin( pAttachedTo, pAbsOrigin, pAbsAngles );
  4940. }
  4941. }
  4942. //-----------------------------------------------------------------------------
  4943. // Purpose:
  4944. //
  4945. // Input : iSequence -
  4946. //
  4947. // Output : char
  4948. //-----------------------------------------------------------------------------
  4949. const char *C_BaseAnimating::GetSequenceName( int iSequence )
  4950. {
  4951. if( iSequence == -1 )
  4952. {
  4953. return "Not Found!";
  4954. }
  4955. if ( !GetModelPtr() )
  4956. return "No model!";
  4957. return ::GetSequenceName( GetModelPtr(), iSequence );
  4958. }
  4959. Activity C_BaseAnimating::GetSequenceActivity( int iSequence )
  4960. {
  4961. if( iSequence == -1 )
  4962. {
  4963. return ACT_INVALID;
  4964. }
  4965. if ( !GetModelPtr() )
  4966. return ACT_INVALID;
  4967. return (Activity)::GetSequenceActivity( GetModelPtr(), iSequence );
  4968. }
  4969. //-----------------------------------------------------------------------------
  4970. // returns the sequence keyvalue text as a KeyValues pointer
  4971. //-----------------------------------------------------------------------------
  4972. KeyValues *C_BaseAnimating::GetSequenceKeyValues( int iSequence )
  4973. {
  4974. const char *szText = Studio_GetKeyValueText( GetModelPtr(), iSequence );
  4975. if (szText)
  4976. {
  4977. KeyValues *seqKeyValues = new KeyValues("");
  4978. if ( seqKeyValues->LoadFromBuffer( modelinfo->GetModelName( GetModel() ), szText ) )
  4979. {
  4980. return seqKeyValues;
  4981. }
  4982. seqKeyValues->deleteThis();
  4983. }
  4984. return NULL;
  4985. }
  4986. //-----------------------------------------------------------------------------
  4987. // Computes a box that surrounds all hitboxes
  4988. //-----------------------------------------------------------------------------
  4989. bool C_BaseAnimating::ComputeHitboxSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs )
  4990. {
  4991. // Note that this currently should not be called during position recomputation because of IK.
  4992. // The code below recomputes bones so as to get at the hitboxes,
  4993. // which causes IK to trigger, which causes raycasts against the other entities to occur,
  4994. // which is illegal to do while in the computeabsposition phase.
  4995. CStudioHdr *pStudioHdr = GetModelPtr();
  4996. if (!pStudioHdr)
  4997. return false;
  4998. mstudiohitboxset_t *set = pStudioHdr->pHitboxSet( m_nHitboxSet );
  4999. if ( !set || !set->numhitboxes )
  5000. return false;
  5001. CBoneCache *pCache = GetBoneCache( pStudioHdr );
  5002. matrix3x4_t *hitboxbones[MAXSTUDIOBONES];
  5003. pCache->ReadCachedBonePointers( hitboxbones, pStudioHdr->numbones() );
  5004. // Compute a box in world space that surrounds this entity
  5005. pVecWorldMins->Init( FLT_MAX, FLT_MAX, FLT_MAX );
  5006. pVecWorldMaxs->Init( -FLT_MAX, -FLT_MAX, -FLT_MAX );
  5007. Vector vecBoxAbsMins, vecBoxAbsMaxs;
  5008. for ( int i = 0; i < set->numhitboxes; i++ )
  5009. {
  5010. mstudiobbox_t *pbox = set->pHitbox(i);
  5011. TransformAABB( *hitboxbones[pbox->bone], pbox->bbmin, pbox->bbmax, vecBoxAbsMins, vecBoxAbsMaxs );
  5012. VectorMin( *pVecWorldMins, vecBoxAbsMins, *pVecWorldMins );
  5013. VectorMax( *pVecWorldMaxs, vecBoxAbsMaxs, *pVecWorldMaxs );
  5014. }
  5015. return true;
  5016. }
  5017. //-----------------------------------------------------------------------------
  5018. // Computes a box that surrounds all hitboxes, in entity space
  5019. //-----------------------------------------------------------------------------
  5020. bool C_BaseAnimating::ComputeEntitySpaceHitboxSurroundingBox( Vector *pVecWorldMins, Vector *pVecWorldMaxs )
  5021. {
  5022. // Note that this currently should not be called during position recomputation because of IK.
  5023. // The code below recomputes bones so as to get at the hitboxes,
  5024. // which causes IK to trigger, which causes raycasts against the other entities to occur,
  5025. // which is illegal to do while in the computeabsposition phase.
  5026. CStudioHdr *pStudioHdr = GetModelPtr();
  5027. if (!pStudioHdr)
  5028. return false;
  5029. mstudiohitboxset_t *set = pStudioHdr->pHitboxSet( m_nHitboxSet );
  5030. if ( !set || !set->numhitboxes )
  5031. return false;
  5032. CBoneCache *pCache = GetBoneCache( pStudioHdr );
  5033. matrix3x4_t *hitboxbones[MAXSTUDIOBONES];
  5034. pCache->ReadCachedBonePointers( hitboxbones, pStudioHdr->numbones() );
  5035. // Compute a box in world space that surrounds this entity
  5036. pVecWorldMins->Init( FLT_MAX, FLT_MAX, FLT_MAX );
  5037. pVecWorldMaxs->Init( -FLT_MAX, -FLT_MAX, -FLT_MAX );
  5038. matrix3x4_t worldToEntity, boneToEntity;
  5039. MatrixInvert( EntityToWorldTransform(), worldToEntity );
  5040. Vector vecBoxAbsMins, vecBoxAbsMaxs;
  5041. for ( int i = 0; i < set->numhitboxes; i++ )
  5042. {
  5043. mstudiobbox_t *pbox = set->pHitbox(i);
  5044. ConcatTransforms( worldToEntity, *hitboxbones[pbox->bone], boneToEntity );
  5045. TransformAABB( boneToEntity, pbox->bbmin, pbox->bbmax, vecBoxAbsMins, vecBoxAbsMaxs );
  5046. VectorMin( *pVecWorldMins, vecBoxAbsMins, *pVecWorldMins );
  5047. VectorMax( *pVecWorldMaxs, vecBoxAbsMaxs, *pVecWorldMaxs );
  5048. }
  5049. return true;
  5050. }
  5051. //-----------------------------------------------------------------------------
  5052. // Purpose:
  5053. // Input : scale -
  5054. //-----------------------------------------------------------------------------
  5055. void C_BaseAnimating::SetModelScale( float scale, float change_duration /*= 0.0f*/ )
  5056. {
  5057. if ( change_duration > 0.0f )
  5058. {
  5059. ModelScale *mvs = ( ModelScale * )CreateDataObject( MODELSCALE );
  5060. mvs->m_flModelScaleStart = m_flModelScale;
  5061. mvs->m_flModelScaleGoal = scale;
  5062. mvs->m_flModelScaleStartTime = gpGlobals->curtime;
  5063. mvs->m_flModelScaleFinishTime = mvs->m_flModelScaleStartTime + change_duration;
  5064. }
  5065. else
  5066. {
  5067. m_flModelScale = scale;
  5068. RefreshCollisionBounds();
  5069. if ( HasDataObjectType( MODELSCALE ) )
  5070. {
  5071. DestroyDataObject( MODELSCALE );
  5072. }
  5073. }
  5074. }
  5075. //-----------------------------------------------------------------------------
  5076. // Purpose:
  5077. //-----------------------------------------------------------------------------
  5078. void C_BaseAnimating::UpdateModelScale()
  5079. {
  5080. ModelScale *mvs = ( ModelScale * )GetDataObject( MODELSCALE );
  5081. if ( !mvs )
  5082. {
  5083. return;
  5084. }
  5085. float dt = mvs->m_flModelScaleFinishTime - mvs->m_flModelScaleStartTime;
  5086. Assert( dt > 0.0f );
  5087. float frac = ( gpGlobals->curtime - mvs->m_flModelScaleStartTime ) / dt;
  5088. frac = clamp( frac, 0.0f, 1.0f );
  5089. if ( gpGlobals->curtime >= mvs->m_flModelScaleFinishTime )
  5090. {
  5091. m_flModelScale = mvs->m_flModelScaleGoal;
  5092. DestroyDataObject( MODELSCALE );
  5093. }
  5094. else
  5095. {
  5096. m_flModelScale = Lerp( frac, mvs->m_flModelScaleStart, mvs->m_flModelScaleGoal );
  5097. }
  5098. RefreshCollisionBounds();
  5099. }
  5100. void C_BaseAnimating::RefreshCollisionBounds( void )
  5101. {
  5102. CollisionProp()->RefreshScaledCollisionBounds();
  5103. }
  5104. //-----------------------------------------------------------------------------
  5105. // Purpose: Clientside bone follower class. Used just to visualize them.
  5106. // Bone followers WON'T be sent to the client if VISUALIZE_FOLLOWERS is
  5107. // undefined in the server's physics_bone_followers.cpp
  5108. //-----------------------------------------------------------------------------
  5109. class C_BoneFollower : public C_BaseEntity
  5110. {
  5111. DECLARE_CLASS( C_BoneFollower, C_BaseEntity );
  5112. DECLARE_CLIENTCLASS();
  5113. public:
  5114. C_BoneFollower( void )
  5115. {
  5116. }
  5117. bool ShouldDraw( void );
  5118. int DrawModel( int flags );
  5119. bool TestCollision( const Ray_t &ray, unsigned int mask, trace_t& trace );
  5120. private:
  5121. int m_modelIndex;
  5122. int m_solidIndex;
  5123. };
  5124. IMPLEMENT_CLIENTCLASS_DT( C_BoneFollower, DT_BoneFollower, CBoneFollower )
  5125. RecvPropInt( RECVINFO( m_modelIndex ) ),
  5126. RecvPropInt( RECVINFO( m_solidIndex ) ),
  5127. END_RECV_TABLE()
  5128. void VCollideWireframe_ChangeCallback( IConVar *pConVar, char const *pOldString, float flOldValue )
  5129. {
  5130. for ( C_BaseEntity *pEntity = ClientEntityList().FirstBaseEntity(); pEntity; pEntity = ClientEntityList().NextBaseEntity(pEntity) )
  5131. {
  5132. pEntity->UpdateVisibility();
  5133. }
  5134. }
  5135. //-----------------------------------------------------------------------------
  5136. // Purpose: Returns whether object should render.
  5137. //-----------------------------------------------------------------------------
  5138. bool C_BoneFollower::ShouldDraw( void )
  5139. {
  5140. return ( vcollide_wireframe.GetBool() ); //MOTODO
  5141. }
  5142. //-----------------------------------------------------------------------------
  5143. // Purpose:
  5144. //-----------------------------------------------------------------------------
  5145. int C_BoneFollower::DrawModel( int flags )
  5146. {
  5147. vcollide_t *pCollide = modelinfo->GetVCollide( m_modelIndex );
  5148. if ( pCollide )
  5149. {
  5150. static color32 debugColor = {0,255,255,0};
  5151. matrix3x4_t matrix;
  5152. AngleMatrix( GetAbsAngles(), GetAbsOrigin(), matrix );
  5153. engine->DebugDrawPhysCollide( pCollide->solids[m_solidIndex], NULL, matrix, debugColor );
  5154. }
  5155. return 1;
  5156. }
  5157. bool C_BoneFollower::TestCollision( const Ray_t &ray, unsigned int mask, trace_t& trace )
  5158. {
  5159. vcollide_t *pCollide = modelinfo->GetVCollide( m_modelIndex );
  5160. Assert( pCollide && pCollide->solidCount > m_solidIndex );
  5161. physcollision->TraceBox( ray, pCollide->solids[m_solidIndex], GetAbsOrigin(), GetAbsAngles(), &trace );
  5162. if ( trace.fraction >= 1 )
  5163. return false;
  5164. // return owner as trace hit
  5165. trace.m_pEnt = GetOwnerEntity();
  5166. trace.hitgroup = 0;//m_hitGroup;
  5167. trace.physicsbone = 0;//m_physicsBone; // UNDONE: Get physics bone index & hitgroup
  5168. return trace.DidHit();
  5169. }
  5170. void C_BaseAnimating::DisableMuzzleFlash()
  5171. {
  5172. m_nOldMuzzleFlashParity = m_nMuzzleFlashParity;
  5173. }
  5174. void C_BaseAnimating::DoMuzzleFlash()
  5175. {
  5176. m_nMuzzleFlashParity = (m_nMuzzleFlashParity+1) & ((1 << EF_MUZZLEFLASH_BITS) - 1);
  5177. }
  5178. //-----------------------------------------------------------------------------
  5179. // Purpose:
  5180. //-----------------------------------------------------------------------------
  5181. void DevMsgRT( char const* pMsg, ... )
  5182. {
  5183. if (gpGlobals->frametime != 0.0f)
  5184. {
  5185. va_list argptr;
  5186. va_start( argptr, pMsg );
  5187. //
  5188. {
  5189. static char string[1024];
  5190. Q_vsnprintf (string, sizeof( string ), pMsg, argptr);
  5191. DevMsg( 1, "%s", string );
  5192. }
  5193. // DevMsg( pMsg, argptr );
  5194. va_end( argptr );
  5195. }
  5196. }
  5197. void C_BaseAnimating::ForceClientSideAnimationOn()
  5198. {
  5199. m_bClientSideAnimation = true;
  5200. AddToClientSideAnimationList();
  5201. }
  5202. void C_BaseAnimating::AddToClientSideAnimationList()
  5203. {
  5204. // Already in list
  5205. if ( m_ClientSideAnimationListHandle != INVALID_CLIENTSIDEANIMATION_LIST_HANDLE )
  5206. return;
  5207. clientanimating_t list( this, 0 );
  5208. m_ClientSideAnimationListHandle = g_ClientSideAnimationList.AddToTail( list );
  5209. ClientSideAnimationChanged();
  5210. UpdateRelevantInterpolatedVars();
  5211. }
  5212. void C_BaseAnimating::RemoveFromClientSideAnimationList( bool bBeingDestroyed /*= false*/ )
  5213. {
  5214. // Not in list yet
  5215. if ( INVALID_CLIENTSIDEANIMATION_LIST_HANDLE == m_ClientSideAnimationListHandle )
  5216. return;
  5217. unsigned int c = g_ClientSideAnimationList.Count();
  5218. Assert( m_ClientSideAnimationListHandle < c );
  5219. unsigned int last = c - 1;
  5220. if ( last == m_ClientSideAnimationListHandle )
  5221. {
  5222. // Just wipe the final entry
  5223. g_ClientSideAnimationList.FastRemove( last );
  5224. }
  5225. else
  5226. {
  5227. clientanimating_t lastEntry = g_ClientSideAnimationList[ last ];
  5228. // Remove the last entry
  5229. g_ClientSideAnimationList.FastRemove( last );
  5230. // And update it's handle to point to this slot.
  5231. lastEntry.pAnimating->m_ClientSideAnimationListHandle = m_ClientSideAnimationListHandle;
  5232. g_ClientSideAnimationList[ m_ClientSideAnimationListHandle ] = lastEntry;
  5233. }
  5234. // Invalidate our handle no matter what.
  5235. m_ClientSideAnimationListHandle = INVALID_CLIENTSIDEANIMATION_LIST_HANDLE;
  5236. if ( !bBeingDestroyed )
  5237. {
  5238. UpdateRelevantInterpolatedVars();
  5239. }
  5240. }
  5241. // static method
  5242. void C_BaseAnimating::UpdateClientSideAnimations()
  5243. {
  5244. VPROF_BUDGET( "UpdateClientSideAnimations", VPROF_BUDGETGROUP_CLIENT_ANIMATION );
  5245. int c = g_ClientSideAnimationList.Count();
  5246. for ( int i = 0; i < c ; ++i )
  5247. {
  5248. clientanimating_t &anim = g_ClientSideAnimationList.Element(i);
  5249. if ( !(anim.flags & FCLIENTANIM_SEQUENCE_CYCLE) )
  5250. continue;
  5251. Assert( anim.pAnimating );
  5252. anim.pAnimating->UpdateClientSideAnimation();
  5253. }
  5254. }
  5255. CBoneList *C_BaseAnimating::RecordBones( CStudioHdr *hdr, matrix3x4_t *pBoneState )
  5256. {
  5257. if ( !ToolsEnabled() )
  5258. return NULL;
  5259. VPROF_BUDGET( "C_BaseAnimating::RecordBones", VPROF_BUDGETGROUP_TOOLS );
  5260. // Possible optimization: Instead of inverting everything while recording, record the pos/q stuff into a structure instead?
  5261. Assert( hdr );
  5262. // Setup our transform based on render angles and origin.
  5263. matrix3x4_t parentTransform;
  5264. AngleMatrix( GetRenderAngles(), GetRenderOrigin(), parentTransform );
  5265. CBoneList *boneList = CBoneList::Alloc();
  5266. Assert( boneList );
  5267. boneList->m_nBones = hdr->numbones();
  5268. for ( int i = 0; i < hdr->numbones(); i++ )
  5269. {
  5270. matrix3x4_t inverted;
  5271. matrix3x4_t output;
  5272. mstudiobone_t *bone = hdr->pBone( i );
  5273. // Only update bones referenced during setup
  5274. if ( !(bone->flags & BONE_USED_BY_ANYTHING ) )
  5275. {
  5276. boneList->m_quatRot[ i ].Init( 0.0f, 0.0f, 0.0f, 1.0f ); // Init by default sets all 0's, which is invalid
  5277. boneList->m_vecPos[ i ].Init();
  5278. continue;
  5279. }
  5280. if ( bone->parent == -1 )
  5281. {
  5282. // Decompose into parent space
  5283. MatrixInvert( parentTransform, inverted );
  5284. }
  5285. else
  5286. {
  5287. MatrixInvert( pBoneState[ bone->parent ], inverted );
  5288. }
  5289. ConcatTransforms( inverted, pBoneState[ i ], output );
  5290. MatrixAngles( output,
  5291. boneList->m_quatRot[ i ],
  5292. boneList->m_vecPos[ i ] );
  5293. }
  5294. return boneList;
  5295. }
  5296. void C_BaseAnimating::GetToolRecordingState( KeyValues *msg )
  5297. {
  5298. if ( !ToolsEnabled() )
  5299. return;
  5300. VPROF_BUDGET( "C_BaseAnimating::GetToolRecordingState", VPROF_BUDGETGROUP_TOOLS );
  5301. // Force the animation to drive bones
  5302. CStudioHdr *hdr = GetModelPtr();
  5303. matrix3x4_t *pBones = (matrix3x4_t*)_alloca( ( hdr ? hdr->numbones() : 1 ) * sizeof(matrix3x4_t) );
  5304. if ( hdr )
  5305. {
  5306. SetupBones( pBones, hdr->numbones(), BONE_USED_BY_ANYTHING, gpGlobals->curtime );
  5307. }
  5308. else
  5309. {
  5310. SetupBones( NULL, -1, BONE_USED_BY_ANYTHING, gpGlobals->curtime );
  5311. }
  5312. BaseClass::GetToolRecordingState( msg );
  5313. static BaseAnimatingRecordingState_t state;
  5314. state.m_nSkin = GetSkin();
  5315. state.m_nBody = m_nBody;
  5316. state.m_nSequence = m_nSequence;
  5317. state.m_pBoneList = NULL;
  5318. msg->SetPtr( "baseanimating", &state );
  5319. msg->SetInt( "viewmodel", IsViewModel() ? 1 : 0 );
  5320. if ( hdr )
  5321. {
  5322. state.m_pBoneList = RecordBones( hdr, pBones );
  5323. }
  5324. }
  5325. void C_BaseAnimating::CleanupToolRecordingState( KeyValues *msg )
  5326. {
  5327. if ( !ToolsEnabled() )
  5328. return;
  5329. BaseAnimatingRecordingState_t *pState = (BaseAnimatingRecordingState_t*)msg->GetPtr( "baseanimating" );
  5330. if ( pState && pState->m_pBoneList )
  5331. {
  5332. pState->m_pBoneList->Release();
  5333. }
  5334. BaseClass::CleanupToolRecordingState( msg );
  5335. }
  5336. LocalFlexController_t C_BaseAnimating::GetNumFlexControllers( void )
  5337. {
  5338. CStudioHdr *pstudiohdr = GetModelPtr( );
  5339. if (! pstudiohdr)
  5340. return LocalFlexController_t(0);
  5341. return pstudiohdr->numflexcontrollers();
  5342. }
  5343. const char *C_BaseAnimating::GetFlexDescFacs( int iFlexDesc )
  5344. {
  5345. CStudioHdr *pstudiohdr = GetModelPtr( );
  5346. if (! pstudiohdr)
  5347. return 0;
  5348. mstudioflexdesc_t *pflexdesc = pstudiohdr->pFlexdesc( iFlexDesc );
  5349. return pflexdesc->pszFACS( );
  5350. }
  5351. const char *C_BaseAnimating::GetFlexControllerName( LocalFlexController_t iFlexController )
  5352. {
  5353. CStudioHdr *pstudiohdr = GetModelPtr( );
  5354. if (! pstudiohdr)
  5355. return 0;
  5356. mstudioflexcontroller_t *pflexcontroller = pstudiohdr->pFlexcontroller( iFlexController );
  5357. return pflexcontroller->pszName( );
  5358. }
  5359. const char *C_BaseAnimating::GetFlexControllerType( LocalFlexController_t iFlexController )
  5360. {
  5361. CStudioHdr *pstudiohdr = GetModelPtr( );
  5362. if (! pstudiohdr)
  5363. return 0;
  5364. mstudioflexcontroller_t *pflexcontroller = pstudiohdr->pFlexcontroller( iFlexController );
  5365. return pflexcontroller->pszType( );
  5366. }
  5367. //-----------------------------------------------------------------------------
  5368. // Purpose: Returns the fade scale of the entity in question
  5369. // Output : unsigned char - 0 - 255 alpha value
  5370. //-----------------------------------------------------------------------------
  5371. unsigned char C_BaseAnimating::GetClientSideFade( void )
  5372. {
  5373. return UTIL_ComputeEntityFade( this, m_fadeMinDist, m_fadeMaxDist, m_flFadeScale );
  5374. }
  5375. //-----------------------------------------------------------------------------
  5376. // Purpose: Note that we've been transmitted a sequence
  5377. //-----------------------------------------------------------------------------
  5378. void C_BaseAnimating::SetReceivedSequence( void )
  5379. {
  5380. m_bReceivedSequence = true;
  5381. }
  5382. //-----------------------------------------------------------------------------
  5383. // Purpose: See if we should force reset our sequence on a new model
  5384. //-----------------------------------------------------------------------------
  5385. bool C_BaseAnimating::ShouldResetSequenceOnNewModel( void )
  5386. {
  5387. return ( m_bReceivedSequence == false );
  5388. }
  5389. //-----------------------------------------------------------------------------
  5390. // Purpose:
  5391. //-----------------------------------------------------------------------------
  5392. void C_BaseAnimating::UpdateBoneAttachments( void )
  5393. {
  5394. if ( !m_pAttachedTo )
  5395. return;
  5396. // Assert( IsFollowingEntity() );
  5397. // Assert( m_boneIndexAttached >= 0 );
  5398. C_BaseAnimating *follow = FindFollowedEntity();
  5399. if ( follow && (m_boneIndexAttached >= 0) )
  5400. {
  5401. matrix3x4_t boneToWorld, localSpace;
  5402. follow->GetCachedBoneMatrix( m_boneIndexAttached, boneToWorld );
  5403. AngleMatrix( m_boneAngles, m_bonePosition, localSpace );
  5404. ConcatTransforms( boneToWorld, localSpace, GetBoneForWrite( 0 ) );
  5405. Vector absOrigin;
  5406. MatrixGetColumn( GetBone( 0 ), 3, absOrigin );
  5407. SetAbsOrigin( absOrigin );
  5408. QAngle absAngle;
  5409. MatrixAngles( GetBone( 0 ), absAngle );
  5410. SetAbsAngles( absAngle);
  5411. }
  5412. }
  5413. //-----------------------------------------------------------------------------
  5414. // Purpose:
  5415. //-----------------------------------------------------------------------------
  5416. void C_BaseAnimating::AttachEntityToBone( C_BaseAnimating* attachTarget, int boneIndexAttached, Vector bonePosition, QAngle boneAngles )
  5417. {
  5418. if ( !attachTarget )
  5419. return;
  5420. SetCollisionGroup( COLLISION_GROUP_DEBRIS );
  5421. FollowEntity( attachTarget );
  5422. SetOwnerEntity( attachTarget );
  5423. // Assert( boneIndexAttached >= 0 ); // We should be attaching to a bone.
  5424. if ( boneIndexAttached >= 0 )
  5425. {
  5426. m_boneIndexAttached = boneIndexAttached;
  5427. m_bonePosition = bonePosition;
  5428. m_boneAngles = boneAngles;
  5429. }
  5430. m_BoneAccessor.SetReadableBones( BONE_USED_BY_ANYTHING );
  5431. m_BoneAccessor.SetWritableBones( BONE_USED_BY_ANYTHING );
  5432. attachTarget->AddBoneAttachment( this );
  5433. NotifyBoneAttached( attachTarget );
  5434. }
  5435. //-----------------------------------------------------------------------------
  5436. // Purpose:
  5437. //-----------------------------------------------------------------------------
  5438. void C_BaseAnimating::NotifyBoneAttached( C_BaseAnimating* attachTarget )
  5439. {
  5440. // If we're already attached to something, remove us from it.
  5441. if ( m_pAttachedTo )
  5442. {
  5443. m_pAttachedTo->RemoveBoneAttachment( this );
  5444. m_pAttachedTo = NULL;
  5445. }
  5446. // Remember the new attach target.
  5447. m_pAttachedTo = attachTarget;
  5448. // Special case: if we just attached to the local player and he is hidden, hide us as well.
  5449. C_BasePlayer *pPlayer = dynamic_cast<C_BasePlayer*>(attachTarget);
  5450. if ( pPlayer && pPlayer->IsLocalPlayer() )
  5451. {
  5452. if ( !C_BasePlayer::ShouldDrawLocalPlayer() )
  5453. {
  5454. AddEffects( EF_NODRAW );
  5455. }
  5456. }
  5457. else
  5458. {
  5459. RemoveEffects( EF_NODRAW );
  5460. }
  5461. }
  5462. //-----------------------------------------------------------------------------
  5463. // Purpose:
  5464. //-----------------------------------------------------------------------------
  5465. void C_BaseAnimating::AddBoneAttachment( C_BaseAnimating* newBoneAttachment )
  5466. {
  5467. if ( !newBoneAttachment )
  5468. return;
  5469. m_BoneAttachments.AddToTail( newBoneAttachment );
  5470. }
  5471. //-----------------------------------------------------------------------------
  5472. // Purpose:
  5473. //-----------------------------------------------------------------------------
  5474. void C_BaseAnimating::RemoveBoneAttachment( C_BaseAnimating* boneAttachment )
  5475. {
  5476. if ( !boneAttachment )
  5477. return;
  5478. m_BoneAttachments.FindAndRemove( boneAttachment );
  5479. }
  5480. //-----------------------------------------------------------------------------
  5481. // Purpose:
  5482. //-----------------------------------------------------------------------------
  5483. int C_BaseAnimating::GetNumBoneAttachments()
  5484. {
  5485. return m_BoneAttachments.Count();
  5486. }
  5487. //-----------------------------------------------------------------------------
  5488. // Purpose:
  5489. //-----------------------------------------------------------------------------
  5490. C_BaseAnimating* C_BaseAnimating::GetBoneAttachment( int i )
  5491. {
  5492. if ( m_BoneAttachments.IsValidIndex(i) )
  5493. {
  5494. return m_BoneAttachments[i];
  5495. }
  5496. return NULL;
  5497. }
  5498. //-----------------------------------------------------------------------------
  5499. // Purpose:
  5500. //-----------------------------------------------------------------------------
  5501. void C_BaseAnimating::DestroyBoneAttachments()
  5502. {
  5503. while ( GetNumBoneAttachments() )
  5504. {
  5505. C_BaseAnimating *pAttachment = GetBoneAttachment(0);
  5506. if ( pAttachment )
  5507. {
  5508. pAttachment->Release();
  5509. }
  5510. else
  5511. {
  5512. m_BoneAttachments.Remove(0);
  5513. }
  5514. }
  5515. }
  5516. //-----------------------------------------------------------------------------
  5517. // Purpose:
  5518. //-----------------------------------------------------------------------------
  5519. void C_BaseAnimating::MoveBoneAttachments( C_BaseAnimating* attachTarget )
  5520. {
  5521. if ( !attachTarget )
  5522. return;
  5523. // Move all of our bone attachments to this new object.
  5524. // Preserves the specific bone and attachment location information.
  5525. while ( GetNumBoneAttachments() )
  5526. {
  5527. C_BaseAnimating *pAttachment = GetBoneAttachment(0);
  5528. if ( pAttachment )
  5529. {
  5530. pAttachment->AttachEntityToBone( attachTarget );
  5531. }
  5532. else
  5533. {
  5534. m_BoneAttachments.Remove(0);
  5535. }
  5536. }
  5537. }