summaryrefslogtreecommitdiff
path: root/game/server/portal/PhysicsCloneArea.cpp
blob: 620bd8e5be0d20c54349de9df02ca353e2e8b67f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Instead of cloning all physics objects in a level to get proper
//			near-portal reactions, only clone from a larger area near portals.
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "PhysicsCloneArea.h"
#include "prop_portal.h"
#include "portal_shareddefs.h"
#include "collisionutils.h"
#include "env_debughistory.h"

LINK_ENTITY_TO_CLASS( physicsclonearea, CPhysicsCloneArea );


#define PHYSICSCLONEAREASCALE 4.0f

const Vector CPhysicsCloneArea::vLocalMins( 3.0f, 
										   -PORTAL_HALF_WIDTH * PHYSICSCLONEAREASCALE, 
										   -PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE );
const Vector CPhysicsCloneArea::vLocalMaxs( PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE,  //x is the forward which is fairly thin for portals, replacing with halfheight
											PORTAL_HALF_WIDTH * PHYSICSCLONEAREASCALE,
											PORTAL_HALF_HEIGHT * PHYSICSCLONEAREASCALE );

extern ConVar sv_portal_debug_touch;

void CPhysicsCloneArea::StartTouch( CBaseEntity *pOther )
{
	if( !m_bActive )
		return;

	if( sv_portal_debug_touch.GetBool() )
	{
		DevMsg( "PortalCloneArea %i Start Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime );
	}
#if !defined( DISABLE_DEBUG_HISTORY )
	if ( !IsMarkedForDeletion() )
	{
		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  ) );
	}
#endif

	m_pAttachedSimulator->StartCloningEntity( pOther );
}

void CPhysicsCloneArea::Touch( CBaseEntity *pOther )
{
	if( !m_bActive )
		return;

	//TODO: Planar checks to see if it's a better idea to reclone/unclone
	
}

void CPhysicsCloneArea::EndTouch( CBaseEntity *pOther )
{
	if( !m_bActive )
		return;

	if( sv_portal_debug_touch.GetBool() )
	{
		DevMsg( "PortalCloneArea %i End Touch: %s : %f\n", ((m_pAttachedPortal->m_bIsPortal2)?(2):(1)), pOther->GetClassname(), gpGlobals->curtime );
	}
#if !defined( DISABLE_DEBUG_HISTORY )
	if ( !IsMarkedForDeletion() )
	{
		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 ) );
	}
#endif

	m_pAttachedSimulator->StopCloningEntity( pOther );
}

void CPhysicsCloneArea::Spawn( void )
{
	BaseClass::Spawn();

	Assert( m_pAttachedPortal );

	AddEffects( EF_NORECEIVESHADOW | EF_NOSHADOW | EF_NODRAW );

	SetSolid( SOLID_OBB );
	SetSolidFlags( FSOLID_TRIGGER | FSOLID_NOT_SOLID );
	SetMoveType( MOVETYPE_NONE );
	SetCollisionGroup( COLLISION_GROUP_PLAYER );

	SetSize( vLocalMins, vLocalMaxs );
}

void CPhysicsCloneArea::Activate( void )
{
	BaseClass::Activate();
}

int CPhysicsCloneArea::ObjectCaps( void )
{ 
	return BaseClass::ObjectCaps() | FCAP_DONT_SAVE; //don't save this entity in any way, we naively recreate them
}


void CPhysicsCloneArea::UpdatePosition( void )
{
	Assert( m_pAttachedPortal );

	//untouch everything we're touching
	touchlink_t *root = ( touchlink_t * )GetDataObject( TOUCHLINK );
	if( root )
	{
		//don't want to risk list corruption while untouching
		CUtlVector<CBaseEntity *> TouchingEnts;
		for( touchlink_t *link = root->nextLink; link != root; link = link->nextLink )
			TouchingEnts.AddToTail( link->entityTouched );


		for( int i = TouchingEnts.Count(); --i >= 0; )
		{
			CBaseEntity *pTouch = TouchingEnts[i];

			pTouch->PhysicsNotifyOtherOfUntouch( pTouch, this );
			PhysicsNotifyOtherOfUntouch( this, pTouch );
		}
	}

	SetAbsOrigin( m_pAttachedPortal->GetAbsOrigin() );
	SetAbsAngles( m_pAttachedPortal->GetAbsAngles() );
	m_bActive = m_pAttachedPortal->m_bActivated;

	//NDebugOverlay::EntityBounds( this, 0, 0, 255, 25, 5.0f );

	//RemoveFlag( FL_DONTTOUCH );
	CloneNearbyEntities(); //wake new objects so they can figure out that they touch
}

void CPhysicsCloneArea::CloneNearbyEntities( void )
{
	CBaseEntity*	pList[ 1024 ];

	Vector vForward, vUp, vRight;
	GetVectors( &vForward, &vRight, &vUp );

	Vector ptOrigin = GetAbsOrigin();
	QAngle qAngles = GetAbsAngles();

	Vector ptOBBStart = ptOrigin;
	ptOBBStart += vForward * vLocalMins.x;
	ptOBBStart += vRight * vLocalMins.y;
	ptOBBStart += vUp * vLocalMins.z;
	

	vForward *= vLocalMaxs.x - vLocalMins.x;
	vRight *= vLocalMaxs.y - vLocalMins.y;
	vUp *= vLocalMaxs.z - vLocalMins.z;


	Vector vAABBMins, vAABBMaxs;
	vAABBMins = vAABBMaxs = ptOBBStart;

	for( int i = 1; i != 8; ++i )
	{
		Vector ptTest = ptOBBStart;
		if( i & (1 << 0) ) ptTest += vForward;
		if( i & (1 << 1) ) ptTest += vRight;
		if( i & (1 << 2) ) ptTest += vUp;

		if( ptTest.x < vAABBMins.x ) vAABBMins.x = ptTest.x;
		if( ptTest.y < vAABBMins.y ) vAABBMins.y = ptTest.y;
		if( ptTest.z < vAABBMins.z ) vAABBMins.z = ptTest.z;
		if( ptTest.x > vAABBMaxs.x ) vAABBMaxs.x = ptTest.x;
		if( ptTest.y > vAABBMaxs.y ) vAABBMaxs.y = ptTest.y;
		if( ptTest.z > vAABBMaxs.z ) vAABBMaxs.z = ptTest.z;
	}
	

	/*{
		Vector ptAABBCenter = (vAABBMins + vAABBMaxs) * 0.5f;
		Vector vAABBExtent = (vAABBMaxs - vAABBMins) * 0.5f;
		NDebugOverlay::Box( ptAABBCenter, -vAABBExtent, vAABBExtent, 0, 0, 255, 128, 10.0f );
	}*/
	

	int count = UTIL_EntitiesInBox( pList, 1024, vAABBMins, vAABBMaxs, 0 );
	trace_t tr;
	UTIL_ClearTrace( tr );
	

	//Iterate over all the possible targets
	for ( int i = 0; i < count; i++ )
	{
		CBaseEntity *pEntity = pList[i];

		if ( pEntity  && (pEntity != this) )
		{
			IPhysicsObject *pPhysicsObject = pEntity->VPhysicsGetObject();

			if( pPhysicsObject )
			{
				CCollisionProperty *pEntCollision = pEntity->CollisionProp();
				Vector ptEntityCenter = pEntCollision->GetCollisionOrigin();

				//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
				if( IsOBBIntersectingOBB( ptOrigin, qAngles, vLocalMins, vLocalMaxs, 
					ptEntityCenter, pEntCollision->GetCollisionAngles(), pEntCollision->OBBMins(), pEntCollision->OBBMaxs() ) )
				{
					tr.endpos = (ptOrigin + ptEntityCenter) * 0.5;
					PhysicsMarkEntitiesAsTouching( pEntity, tr );
					//StartTouch( pEntity );
					
					//pEntity->WakeRestingObjects();
					//pPhysicsObject->Wake();
				}
			}
		}
	}
}

void CPhysicsCloneArea::CloneTouchingEntities( void )
{
	if( m_pAttachedPortal && m_pAttachedPortal->m_bActivated )
	{
		touchlink_t *root = ( touchlink_t * )GetDataObject( TOUCHLINK );
		if( root )
		{
			for( touchlink_t *link = root->nextLink; link != root; link = link->nextLink )
				m_pAttachedSimulator->StartCloningEntity( link->entityTouched );
		}
	}
}





CPhysicsCloneArea *CPhysicsCloneArea::CreatePhysicsCloneArea( CProp_Portal *pFollowPortal )
{
	if( !pFollowPortal )
		return NULL;

	CPhysicsCloneArea *pCloneArea = (CPhysicsCloneArea *)CreateEntityByName( "physicsclonearea" );

	pCloneArea->m_pAttachedPortal = pFollowPortal;
	pCloneArea->m_pAttachedSimulator = &pFollowPortal->m_PortalSimulator;

	DispatchSpawn( pCloneArea );

	pCloneArea->UpdatePosition();

	return pCloneArea;
}