Counter Strike : Global Offensive Source Code
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.

178 lines
6.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Pulling CCollisionEvent's definition out of physics.cpp so it can be abstracted upon (for the portal mod)
  4. //
  5. //
  6. // $Workfile: $
  7. // $Date: $
  8. // $NoKeywords: $
  9. //=============================================================================//
  10. #ifndef PHYSICS_COLLISIONEVENT_H
  11. #define PHYSICS_COLLISIONEVENT_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "physics.h"
  16. #include "tier1/callqueue.h"
  17. extern CCallQueue g_PostSimulationQueue;
  18. struct damageevent_t
  19. {
  20. CBaseEntity *pEntity;
  21. IPhysicsObject *pInflictorPhysics;
  22. CTakeDamageInfo info;
  23. bool bRestoreVelocity;
  24. };
  25. struct inflictorstate_t
  26. {
  27. Vector savedVelocity;
  28. AngularImpulse savedAngularVelocity;
  29. IPhysicsObject *pInflictorPhysics;
  30. float otherMassMax;
  31. short nextIndex;
  32. short restored;
  33. };
  34. enum
  35. {
  36. COLLSTATE_ENABLED = 0,
  37. COLLSTATE_TRYDISABLE = 1,
  38. COLLSTATE_TRYNPCSOLVER = 2,
  39. COLLSTATE_TRYENTITYSOLVER = 3,
  40. COLLSTATE_DISABLED = 4
  41. };
  42. struct penetrateevent_t
  43. {
  44. EHANDLE hEntity0;
  45. EHANDLE hEntity1;
  46. float startTime;
  47. float timeStamp;
  48. int collisionState;
  49. };
  50. class CCollisionEvent : public IPhysicsCollisionEvent, public IPhysicsCollisionSolver, public IPhysicsObjectEvent
  51. {
  52. public:
  53. CCollisionEvent();
  54. friction_t *FindFriction( CBaseEntity *pObject );
  55. void ShutdownFriction( friction_t &friction );
  56. void FrameUpdate();
  57. void LevelShutdown( void );
  58. // IPhysicsCollisionEvent
  59. void PreCollision( vcollisionevent_t *pEvent );
  60. void PostCollision( vcollisionevent_t *pEvent );
  61. void Friction( IPhysicsObject *pObject, float energy, int surfaceProps, int surfacePropsHit, IPhysicsCollisionData *pData );
  62. void StartTouch( IPhysicsObject *pObject1, IPhysicsObject *pObject2, IPhysicsCollisionData *pTouchData );
  63. void EndTouch( IPhysicsObject *pObject1, IPhysicsObject *pObject2, IPhysicsCollisionData *pTouchData );
  64. void FluidStartTouch( IPhysicsObject *pObject, IPhysicsFluidController *pFluid );
  65. void FluidEndTouch( IPhysicsObject *pObject, IPhysicsFluidController *pFluid );
  66. void PostSimulationFrame();
  67. void ObjectEnterTrigger( IPhysicsObject *pTrigger, IPhysicsObject *pObject );
  68. void ObjectLeaveTrigger( IPhysicsObject *pTrigger, IPhysicsObject *pObject );
  69. bool GetTriggerEvent( triggerevent_t *pEvent, CBaseEntity *pTriggerEntity );
  70. void BufferTouchEvents( bool enable ) { m_bBufferTouchEvents = enable; }
  71. virtual void AddDamageEvent( CBaseEntity *pEntity, const CTakeDamageInfo &info, IPhysicsObject *pInflictorPhysics, bool bRestoreVelocity, const Vector &savedVel, const AngularImpulse &savedAngVel );
  72. void AddImpulseEvent( IPhysicsObject *pPhysicsObject, const Vector &vecCenterForce, const AngularImpulse &vecCenterTorque );
  73. void AddSetVelocityEvent( IPhysicsObject *pPhysicsObject, const Vector &vecVelocity );
  74. void AddRemoveObject(IServerNetworkable *pRemove);
  75. void FlushQueuedOperations();
  76. // IPhysicsCollisionSolver
  77. int ShouldCollide( IPhysicsObject *pObj0, IPhysicsObject *pObj1, void *pGameData0, void *pGameData1 );
  78. int ShouldSolvePenetration( IPhysicsObject *pObj0, IPhysicsObject *pObj1, void *pGameData0, void *pGameData1, float dt );
  79. bool ShouldFreezeObject( IPhysicsObject *pObject );
  80. static const char *ModuleName() { return CBaseEntity::IsServer() ? "SERVER" : "CLIENT"; }
  81. int AdditionalCollisionChecksThisTick( int currentChecksDone )
  82. {
  83. //CallbackContext check(this);
  84. if ( currentChecksDone < 1200 )
  85. {
  86. DevMsg(1,"%s: VPhysics Collision detection getting expensive, check for too many convex pieces!\n", ModuleName());
  87. return 1200 - currentChecksDone;
  88. }
  89. DevMsg(1,"%s: VPhysics exceeded collision check limit (%d)!!!\nInterpenetration may result!\n", ModuleName(), currentChecksDone );
  90. return 0;
  91. }
  92. bool ShouldFreezeContacts( IPhysicsObject **pObjectList, int objectCount );
  93. // IPhysicsObjectEvent
  94. // these can be used to optimize out queries on sleeping objects
  95. // Called when an object is woken after sleeping
  96. virtual void ObjectWake( IPhysicsObject *pObject );
  97. // called when an object goes to sleep (no longer simulating)
  98. virtual void ObjectSleep( IPhysicsObject *pObject );
  99. // locals
  100. bool GetInflictorVelocity( IPhysicsObject *pInflictor, Vector &velocity, AngularImpulse &angVelocity );
  101. void GetListOfPenetratingEntities( CBaseEntity *pSearch, CUtlVector<CBaseEntity *> &list );
  102. bool IsInCallback() { return m_inCallback > 0 ? true : false; }
  103. void RemovePenetrationEvents( CBaseEntity *pEntity );
  104. private:
  105. #if _DEBUG
  106. int ShouldCollide_2( IPhysicsObject *pObj0, IPhysicsObject *pObj1, void *pGameData0, void *pGameData1 );
  107. #endif
  108. void UpdateFrictionSounds();
  109. void UpdateTouchEvents();
  110. void UpdateDamageEvents();
  111. void UpdatePenetrateEvents( void );
  112. void UpdateFluidEvents();
  113. void UpdateRemoveObjects();
  114. void AddTouchEvent( CBaseEntity *pEntity0, CBaseEntity *pEntity1, int touchType, const Vector &point, const Vector &normal );
  115. penetrateevent_t &FindOrAddPenetrateEvent( CBaseEntity *pEntity0, CBaseEntity *pEntity1 );
  116. float DeltaTimeSinceLastFluid( CBaseEntity *pEntity );
  117. void RestoreDamageInflictorState( IPhysicsObject *pInflictor );
  118. void RestoreDamageInflictorState( int inflictorStateIndex, float velocityBlend );
  119. int AddDamageInflictor( IPhysicsObject *pInflictorPhysics, float otherMass, const Vector &savedVel, const AngularImpulse &savedAngVel, bool addList );
  120. int FindDamageInflictor( IPhysicsObject *pInflictorPhysics );
  121. // make the call into the entity system
  122. void DispatchStartTouch( CBaseEntity *pEntity0, CBaseEntity *pEntity1, const Vector &point, const Vector &normal );
  123. void DispatchEndTouch( CBaseEntity *pEntity0, CBaseEntity *pEntity1 );
  124. class CallbackContext
  125. {
  126. public:
  127. CallbackContext(CCollisionEvent *pOuter)
  128. {
  129. m_pOuter = pOuter;
  130. m_pOuter->m_inCallback++;
  131. }
  132. ~CallbackContext()
  133. {
  134. m_pOuter->m_inCallback--;
  135. }
  136. private:
  137. CCollisionEvent *m_pOuter;
  138. };
  139. friend class CallbackContext;
  140. friction_t m_current[4];
  141. gamevcollisionevent_t m_gameEvent;
  142. CUtlVector<triggerevent_t> m_triggerEvents;
  143. triggerevent_t m_currentTriggerEvent;
  144. CUtlVector<touchevent_t> m_touchEvents;
  145. CUtlVector<damageevent_t> m_damageEvents;
  146. CUtlVector<inflictorstate_t> m_damageInflictors;
  147. CUtlVector<penetrateevent_t> m_penetrateEvents;
  148. CUtlVector<fluidevent_t> m_fluidEvents;
  149. CUtlVector<IServerNetworkable *> m_removeObjects;
  150. int m_inCallback;
  151. int m_lastTickFrictionError; // counter to control printing of the dev warning for large contact systems
  152. bool m_bBufferTouchEvents;
  153. };
  154. #endif //#ifndef PHYSICS_COLLISIONEVENT_H