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.

280 lines
8.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Instead of cloning all physics objects in a level to get proper
  4. // near-portal reactions, only clone from a larger area near portals.
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "PhysicsCloneArea.h"
  10. #include "portal_base2d.h"
  11. #include "collisionutils.h"
  12. #include "env_debughistory.h"
  13. LINK_ENTITY_TO_CLASS( physicsclonearea, CPhysicsCloneArea );
  14. const float CPhysicsCloneArea::s_fPhysicsCloneAreaScale = 4.0f;
  15. //#define PHYSICSCLONEAREASCALE 4.0f
  16. /*const Vector CPhysicsCloneArea::vLocalMins( 3.0f,
  17. -PORTAL_HALF_WIDTH * PHYSICSCLONEAREASCALE,
  18. -PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE );
  19. const Vector CPhysicsCloneArea::vLocalMaxs( PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE, //x is the forward which is fairly thin for portals, replacing with halfheight
  20. PORTAL_HALF_WIDTH * PHYSICSCLONEAREASCALE,
  21. PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE );*/
  22. extern ConVar sv_portal_debug_touch;
  23. void CPhysicsCloneArea::StartTouch( CBaseEntity *pOther )
  24. {
  25. if( !m_bActive )
  26. return;
  27. if( sv_portal_debug_touch.GetBool() )
  28. {
  29. DevMsg( "PortalCloneArea %i Start Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime );
  30. }
  31. #if !defined( DISABLE_DEBUG_HISTORY )
  32. if ( !IsMarkedForDeletion() )
  33. {
  34. ADD_DEBUG_HISTORY( HISTORY_PLAYER_DAMAGE, UTIL_VarArgs( "PortalCloneArea %i Start Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime ) );
  35. }
  36. #endif
  37. m_pAttachedSimulator->StartCloningEntityFromMain( pOther );
  38. }
  39. void CPhysicsCloneArea::Touch( CBaseEntity *pOther )
  40. {
  41. if( !m_bActive )
  42. return;
  43. //TODO: Planar checks to see if it's a better idea to reclone/unclone
  44. }
  45. void CPhysicsCloneArea::EndTouch( CBaseEntity *pOther )
  46. {
  47. if( !m_bActive )
  48. return;
  49. if( sv_portal_debug_touch.GetBool() )
  50. {
  51. DevMsg( "PortalCloneArea %i End Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime );
  52. }
  53. #if !defined( DISABLE_DEBUG_HISTORY )
  54. if ( !IsMarkedForDeletion() )
  55. {
  56. ADD_DEBUG_HISTORY( HISTORY_PLAYER_DAMAGE, UTIL_VarArgs( "PortalCloneArea %i End Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime ) );
  57. }
  58. #endif
  59. m_pAttachedSimulator->StopCloningEntityFromMain( pOther );
  60. }
  61. void CPhysicsCloneArea::Spawn( void )
  62. {
  63. BaseClass::Spawn();
  64. Assert( m_pAttachedPortal );
  65. AddEffects( EF_NORECEIVESHADOW | EF_NOSHADOW | EF_NODRAW );
  66. SetSolid( SOLID_OBB );
  67. SetSolidFlags( FSOLID_TRIGGER | FSOLID_NOT_SOLID );
  68. SetMoveType( MOVETYPE_NONE );
  69. SetCollisionGroup( COLLISION_GROUP_PLAYER );
  70. m_fHalfWidth = m_pAttachedPortal->GetHalfWidth() * s_fPhysicsCloneAreaScale;
  71. m_fHalfHeight = m_pAttachedPortal->GetHalfHeight() * s_fPhysicsCloneAreaScale;
  72. m_fHalfDepth = MAX( m_fHalfWidth, m_fHalfHeight );
  73. SetSize( GetLocalMins(), GetLocalMaxs() );
  74. }
  75. void CPhysicsCloneArea::Activate( void )
  76. {
  77. BaseClass::Activate();
  78. }
  79. int CPhysicsCloneArea::ObjectCaps( void )
  80. {
  81. return BaseClass::ObjectCaps() | FCAP_DONT_SAVE; //don't save this entity in any way, we naively recreate them
  82. }
  83. void CPhysicsCloneArea::UpdatePosition( void )
  84. {
  85. Assert( m_pAttachedPortal );
  86. //untouch everything we're touching
  87. touchlink_t *root = ( touchlink_t * )GetDataObject( TOUCHLINK );
  88. if( root )
  89. {
  90. //don't want to risk list corruption while untouching
  91. CUtlVector<CBaseEntity *> TouchingEnts;
  92. for( touchlink_t *link = root->nextLink; link != root; link = link->nextLink )
  93. TouchingEnts.AddToTail( link->entityTouched );
  94. for( int i = TouchingEnts.Count(); --i >= 0; )
  95. {
  96. CBaseEntity *pTouch = TouchingEnts[i];
  97. pTouch->PhysicsNotifyOtherOfUntouch( pTouch, this );
  98. PhysicsNotifyOtherOfUntouch( this, pTouch );
  99. }
  100. }
  101. //update size as well
  102. m_fHalfWidth = m_pAttachedPortal->GetHalfWidth() * s_fPhysicsCloneAreaScale;
  103. m_fHalfHeight = m_pAttachedPortal->GetHalfHeight() * s_fPhysicsCloneAreaScale;
  104. m_fHalfDepth = MAX( m_fHalfWidth, m_fHalfHeight );
  105. SetSize( GetLocalMins(), GetLocalMaxs() );
  106. SetAbsOrigin( m_pAttachedPortal->GetAbsOrigin() );
  107. SetAbsAngles( m_pAttachedPortal->GetAbsAngles() );
  108. m_bActive = m_pAttachedPortal->IsActive();
  109. //NDebugOverlay::EntityBounds( this, 0, 0, 255, 25, 5.0f );
  110. //RemoveFlag( FL_DONTTOUCH );
  111. CloneNearbyEntities(); //wake new objects so they can figure out that they touch
  112. }
  113. void CPhysicsCloneArea::CloneNearbyEntities( void )
  114. {
  115. CBaseEntity* pList[ 1024 ];
  116. Vector vForward, vUp, vRight;
  117. GetVectors( &vForward, &vRight, &vUp );
  118. Vector ptOrigin = GetAbsOrigin();
  119. QAngle qAngles = GetAbsAngles();
  120. Vector vLocalMins = GetLocalMins();
  121. Vector vLocalMaxs = GetLocalMaxs();
  122. Vector ptOBBStart = ptOrigin;
  123. ptOBBStart += vForward * vLocalMins.x;
  124. ptOBBStart += vRight * vLocalMins.y;
  125. ptOBBStart += vUp * vLocalMins.z;
  126. vForward *= vLocalMaxs.x - vLocalMins.x;
  127. vRight *= vLocalMaxs.y - vLocalMins.y;
  128. vUp *= vLocalMaxs.z - vLocalMins.z;
  129. Vector vAABBMins, vAABBMaxs;
  130. vAABBMins = vAABBMaxs = ptOBBStart;
  131. for( int i = 1; i != 8; ++i )
  132. {
  133. Vector ptTest = ptOBBStart;
  134. if( i & (1 << 0) ) ptTest += vForward;
  135. if( i & (1 << 1) ) ptTest += vRight;
  136. if( i & (1 << 2) ) ptTest += vUp;
  137. if( ptTest.x < vAABBMins.x ) vAABBMins.x = ptTest.x;
  138. if( ptTest.y < vAABBMins.y ) vAABBMins.y = ptTest.y;
  139. if( ptTest.z < vAABBMins.z ) vAABBMins.z = ptTest.z;
  140. if( ptTest.x > vAABBMaxs.x ) vAABBMaxs.x = ptTest.x;
  141. if( ptTest.y > vAABBMaxs.y ) vAABBMaxs.y = ptTest.y;
  142. if( ptTest.z > vAABBMaxs.z ) vAABBMaxs.z = ptTest.z;
  143. }
  144. /*{
  145. Vector ptAABBCenter = (vAABBMins + vAABBMaxs) * 0.5f;
  146. Vector vAABBExtent = (vAABBMaxs - vAABBMins) * 0.5f;
  147. NDebugOverlay::Box( ptAABBCenter, -vAABBExtent, vAABBExtent, 0, 0, 255, 128, 10.0f );
  148. }*/
  149. int count = UTIL_EntitiesInBox( pList, 1024, vAABBMins, vAABBMaxs, 0 );
  150. trace_t tr;
  151. UTIL_ClearTrace( tr );
  152. //Iterate over all the possible targets
  153. for ( int i = 0; i < count; i++ )
  154. {
  155. CBaseEntity *pEntity = pList[i];
  156. if ( pEntity && (pEntity != this) )
  157. {
  158. IPhysicsObject *pPhysicsObject = pEntity->VPhysicsGetObject();
  159. if( pPhysicsObject )
  160. {
  161. CCollisionProperty *pEntCollision = pEntity->CollisionProp();
  162. Vector ptEntityCenter = pEntCollision->GetCollisionOrigin();
  163. //double check intersection at the OBB vs OBB level, we don't want to affect large piles of physics objects if we don't have to, it gets slow
  164. if( IsOBBIntersectingOBB( ptOrigin, qAngles, vLocalMins, vLocalMaxs,
  165. ptEntityCenter, pEntCollision->GetCollisionAngles(), pEntCollision->OBBMins(), pEntCollision->OBBMaxs() ) )
  166. {
  167. tr.endpos = (ptOrigin + ptEntityCenter) * 0.5;
  168. PhysicsMarkEntitiesAsTouching( pEntity, tr );
  169. //StartTouch( pEntity );
  170. //pEntity->WakeRestingObjects();
  171. //pPhysicsObject->Wake();
  172. }
  173. }
  174. }
  175. }
  176. }
  177. void CPhysicsCloneArea::CloneTouchingEntities( void )
  178. {
  179. if( m_pAttachedPortal && m_pAttachedPortal->IsActive() )
  180. {
  181. touchlink_t *root = ( touchlink_t * )GetDataObject( TOUCHLINK );
  182. if( root )
  183. {
  184. for( touchlink_t *link = root->nextLink; link != root; link = link->nextLink )
  185. m_pAttachedSimulator->StartCloningEntityFromMain( link->entityTouched );
  186. }
  187. }
  188. }
  189. CPhysicsCloneArea *CPhysicsCloneArea::CreatePhysicsCloneArea( CPortal_Base2D *pFollowPortal )
  190. {
  191. if( !pFollowPortal )
  192. return NULL;
  193. CPhysicsCloneArea *pCloneArea = (CPhysicsCloneArea *)CreateEntityByName( "physicsclonearea" );
  194. pCloneArea->m_pAttachedPortal = pFollowPortal;
  195. pCloneArea->m_pAttachedSimulator = &pFollowPortal->m_PortalSimulator;
  196. DispatchSpawn( pCloneArea );
  197. pCloneArea->UpdatePosition();
  198. return pCloneArea;
  199. }
  200. void CPhysicsCloneArea::Resize( float fPortalHalfWidth, float fPortalHalfHeight )
  201. {
  202. fPortalHalfWidth *= s_fPhysicsCloneAreaScale;
  203. fPortalHalfHeight *= s_fPhysicsCloneAreaScale;
  204. if( (fPortalHalfWidth == m_fHalfWidth) && (fPortalHalfHeight == m_fHalfHeight) )
  205. return;
  206. m_fHalfWidth = fPortalHalfWidth;
  207. m_fHalfHeight = fPortalHalfHeight;
  208. m_fHalfDepth = MAX( m_fHalfWidth, m_fHalfHeight );
  209. SetSize( GetLocalMins(), GetLocalMaxs() );
  210. UpdatePosition();
  211. }