summaryrefslogtreecommitdiff
path: root/game/shared/tf2/tf_shieldshared.cpp
blob: a7370ce98b82cb5037a3ae5f92c9ca25148df8a6 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:			The Escort's Shield weapon effect
//
// $Workfile:     $
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"

#include "tf_shieldshared.h"
#include "edict.h"
#include "mathlib/vmatrix.h"
#include "engine/IEngineTrace.h"

#ifdef CLIENT_DLL
#include "cdll_client_int.h"
#else
#include "gameinterface.h"
#endif	


	  
BEGIN_PREDICTION_DATA_NO_BASE( CShieldEffect )

	DEFINE_FIELD( m_TestPoint, FIELD_INTEGER ),
	DEFINE_FIELD( m_Position, FIELD_VECTOR ),
	DEFINE_FIELD( m_Velocity, FIELD_VECTOR ),
	DEFINE_FIELD( m_CurrentAngles, FIELD_VECTOR ),
	DEFINE_FIELD( m_Theta, FIELD_FLOAT ),
	DEFINE_FIELD( m_Phi, FIELD_FLOAT ),
	DEFINE_FIELD( m_ThetaVelocity, FIELD_FLOAT ),
	DEFINE_FIELD( m_PhiVelocity, FIELD_FLOAT ),
	DEFINE_FIELD( m_vecDesiredOrigin, FIELD_VECTOR ),
	DEFINE_FIELD( m_angDesiredAngles, FIELD_VECTOR ),
	DEFINE_FIELD( m_ShieldTheta, FIELD_FLOAT ),
	DEFINE_FIELD( m_ShieldPhi, FIELD_FLOAT ),

END_PREDICTION_DATA()



//-----------------------------------------------------------------------------
// constructor, destructor
//-----------------------------------------------------------------------------
CShieldEffect::CShieldEffect( )
{
}


//-----------------------------------------------------------------------------
// compute rest positions of the springs
//-----------------------------------------------------------------------------
void CShieldEffect::ComputeRestPositions()
{
	int i;

	m_vecRenderMins.Init( FLT_MAX, FLT_MAX, FLT_MAX );
	m_vecRenderMaxs.Init( -FLT_MAX, -FLT_MAX, -FLT_MAX );

	// Set the initial directions and distances (in shield space)...
	for	( i = 0; i < SHIELD_NUM_VERTICAL_POINTS; ++i)
	{
		// Choose phi centered at pi/2
		float phi = (M_PI - m_ShieldPhi) * 0.5f + m_ShieldPhi * 
			(float)i / (float)(SHIELD_NUM_VERTICAL_POINTS - 1);

		for (int j = 0; j < SHIELD_NUM_HORIZONTAL_POINTS; ++j)
		{
			// Choose theta centered at pi/2 also (y, or forward axis)
			float theta = (M_PI - m_ShieldTheta) * 0.5f + m_ShieldTheta * 
				(float)j / (float)(SHIELD_NUM_HORIZONTAL_POINTS - 1);

			int idx = i * SHIELD_NUM_HORIZONTAL_POINTS + j;

			m_pFixedDirection[idx].x = cos(theta) * sin(phi);
			m_pFixedDirection[idx].y = sin(theta) * sin(phi);
			m_pFixedDirection[idx].z = cos(phi);

			m_pFixedDirection[idx] *= m_RestLength;

			VectorMin( m_vecRenderMins, m_pFixedDirection[idx], m_vecRenderMins );
			VectorMax( m_vecRenderMaxs, m_pFixedDirection[idx], m_vecRenderMaxs );
		}
	}

	// Compute box for fake volume testing
	Vector dist = m_pFixedDirection[0] - m_pFixedDirection[1];
	float l = dist.Length(); // * m_RestLength;
	SetShieldPanelSize( Vector( -l * 0.25f, -l * 0.25f, -l * 0.25f), 
		Vector( l * 0.25f, l * 0.25f, l * 0.25f) );
}


//-----------------------------------------------------------------------------
// Sets orientation + position
//-----------------------------------------------------------------------------
void CShieldEffect::SetDesiredOrigin( const Vector& origin )
{
	VectorCopy( origin, m_vecDesiredOrigin );
}

void CShieldEffect::SetDesiredAngles( const QAngle& angles )
{
	VectorCopy( angles, m_angDesiredAngles );
}

const QAngle& CShieldEffect::GetDesiredAngles() const
{
	return m_angDesiredAngles;
}


//-----------------------------------------------------------------------------
// Gets a point...
//-----------------------------------------------------------------------------
const Vector& CShieldEffect::GetPoint( int x, int y ) const
{
	return m_pControlPoint[ x + y * SHIELD_NUM_HORIZONTAL_POINTS ];
}

const Vector& CShieldEffect::GetPoint( int i ) const
{
	return m_pControlPoint[ i ];
}

Vector& CShieldEffect::GetPoint( int i )
{
	return m_pControlPoint[ i ];
}


//-----------------------------------------------------------------------------
// Sets the collision group
//-----------------------------------------------------------------------------
void CShieldEffect::SetCollisionGroup( int group )
{
	m_CollisionGroup = group;
}


//-----------------------------------------------------------------------------
// Hooks in active bits...
//-----------------------------------------------------------------------------
void CShieldEffect::SetActiveVertexList( IActiveVertList *pActiveVerts )
{
	m_pActiveVerts = pActiveVerts;

	// No points are visible initially
	for ( int i=0; i < SHIELD_VERTEX_BYTES*8; i++ )
		m_pActiveVerts->SetActiveVertState( i, 0 );
}


//-----------------------------------------------------------------------------
// Is a particular vertex active?
//-----------------------------------------------------------------------------
bool CShieldEffect::IsVertexActive( int x, int y ) const
{
	if ((x < 0) || (y < 0) || (x >= SHIELD_NUM_HORIZONTAL_POINTS) ||
		(y >= SHIELD_NUM_VERTICAL_POINTS))
	{
		return false;
	}

	int idx = x + (SHIELD_NUM_HORIZONTAL_POINTS) * y;
	return m_pActiveVerts->GetActiveVertState( idx ) != 0;
}


//-----------------------------------------------------------------------------
// Is a particular panel active?
//-----------------------------------------------------------------------------
bool CShieldEffect::IsPanelActive( int x, int y ) const
{
	if ((x < 0) || (y < 0) || (x >= SHIELD_HORIZONTAL_PANEL_COUNT) ||
		(y >= SHIELD_VERTICAL_PANEL_COUNT))
	{
		return false;
	}

	int idx = x + (SHIELD_HORIZONTAL_PANEL_COUNT) * y;
	return m_pActivePanels[idx];
}


//-----------------------------------------------------------------------------
// Recompute whether the panels are active or not
//-----------------------------------------------------------------------------
void CShieldEffect::ComputePanelActivity()
{
	// Check neighbors to see how many squares we've got
	for	( int i = 0; i < SHIELD_NUM_HORIZONTAL_POINTS - 1; ++i)
	{
		for ( int j = 0; j < SHIELD_NUM_VERTICAL_POINTS - 1; ++j)
		{
			int idx = i + j * (SHIELD_NUM_HORIZONTAL_POINTS - 1);

			// Test the neighbors
			m_pActivePanels[idx] = 
				IsVertexActive( i, j ) ||
				IsVertexActive( i+1, j ) ||
				IsVertexActive( i, j+1 ) ||
				IsVertexActive( i+1, j+1 );
		}
	}
}


//-----------------------------------------------------------------------------
// Compute vertex activity
//-----------------------------------------------------------------------------

enum
{
	SHIELD_TESTS_PER_FRAME = 4
};


void CShieldEffect::ComputeVertexActivity()
{
	int i;
	for ( i = 0; i < SHIELD_TESTS_PER_FRAME; ++i )
	{
		// Visit points in random order...
		int pt = m_PointList[m_TestPoint];

		// Collision test...
		// Check a line that goes farther out than our current point...
		// This will let us check for resting contact
		trace_t tr;
		CTraceFilterWorldOnly traceFilter;
		UTIL_TraceHull( m_Position, m_pControlPoint[pt], 
			m_PanelBoxMin, m_PanelBoxMax, MASK_SOLID_BRUSHONLY, &traceFilter, &tr );
		bool isActive = (!tr.allsolid) && ( (tr.fraction - 1.0f) >= 0.0f );

		m_pActiveVerts->SetActiveVertState( pt, isActive );

		if (++m_TestPoint >= SHIELD_NUM_CONTROL_POINTS)
			m_TestPoint = 0;

	}

	ComputePanelActivity();
}


//-----------------------------------------------------------------------------
// bounding box for collision
//-----------------------------------------------------------------------------
void CShieldEffect::SetShieldPanelSize( Vector& mins, Vector& maxs )
{
	m_PanelBoxMin = mins;
	m_PanelBoxMax = maxs;
}


//-----------------------------------------------------------------------------
// bounding box for collision
//-----------------------------------------------------------------------------
void CShieldEffect::ComputeBounds( Vector& mins, Vector& maxs )
{
	VectorCopy( m_pControlPoint[0], mins );
	VectorCopy( m_pControlPoint[0], maxs );

	for (int i = 1; i < SHIELD_NUM_CONTROL_POINTS; ++i)
	{
		VectorMin( mins, m_pControlPoint[i], mins );
		VectorMax( maxs, m_pControlPoint[i], maxs );
	}

	// Bounds are in local coords
	mins -= m_Position;
	maxs -= m_Position;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CShieldEffect::Precache( void )
{
	m_RestLength = 200.0;

	m_SpringConstant = 30.0f;
	m_DampConstant = 4.0f; 
	m_ViscousDrag = 4.0f;
	m_Mass = 1.0f;

	m_AngularSpringConstant = 2.0f;
	m_AngularViscousDrag = 4.0f;

	SetThetaPhi( SHIELD_INITIAL_THETA, SHIELD_INITIAL_PHI );
}


//-----------------------------------------------------------------------------
// Compute orientation matrix: 
//-----------------------------------------------------------------------------
void CShieldEffect::ComputeOrientationMatrix()
{
	// Generate the orientation matrix from theta and phi...
	// X = forward direction, Y - left direction
	Vector forward, left, up;
	forward.x = cos(m_Theta) * sin(m_Phi);
	forward.y = sin(m_Theta) * sin(m_Phi);
	forward.z = cos(m_Phi);

	left.x = -forward.y;
	left.y = forward.x;
	left.z = 0;

	if ( VectorNormalize(left) == 0.0f )
		left.Init( 0.0f, 1.0f, 0.0f );

	CrossProduct( forward, left, up );

	m_Orientation.SetBasisVectors( forward, left, up );

	// Turn the current matrix into angles...
	MatrixToAngles( m_Orientation, m_CurrentAngles ); 
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldEffect::Spawn( const Vector& currentPosition, const QAngle& currentAngles )
{
	Precache();

	VectorCopy( currentPosition, m_Position );
	m_Velocity.Init();
	
	Vector forward;
	AngleVectors( currentAngles, &forward, 0, 0 );
	m_Phi = acos( forward.z );
	m_Theta = atan2( forward.y, forward.x );
	m_PhiVelocity = 0.0f;
	m_ThetaVelocity = 0.0f;
	ComputeOrientationMatrix();
	VectorCopy( currentAngles, m_CurrentAngles );
	VectorCopy( currentAngles, m_angDesiredAngles );

	// No points are visible initially
	memset( m_pActivePanels, 0, SHIELD_PANELS_COUNT );

	m_TestPoint = 0;

	// Choose random order to visit shield verts
	int i;
	for ( i = 0; i < SHIELD_NUM_CONTROL_POINTS; ++i )
	{
		m_PointList[i] = i;
	}

	for ( i = 0; i < SHIELD_NUM_CONTROL_POINTS; ++i )
	{
		int j = rand() % SHIELD_NUM_CONTROL_POINTS;
		swap( m_PointList[i], m_PointList[j] ); 
	}
}


//-----------------------------------------------------------------------------
// Computes the opacity....
//-----------------------------------------------------------------------------
float CShieldEffect::ComputeOpacity( const Vector& pt, const Vector& center ) const
{
	float dist = pt.DistTo( center ) / m_RestLength;
	if (dist > 1.0)
		dist = 1.0f;
	return 32 + (1.0 - dist) * 192;
}


//-----------------------------------------------------------------------------
// Computes control points
//-----------------------------------------------------------------------------
void CShieldEffect::ComputeControlPoints()
{
	Vector forward, right, up;
	AngleVectors(m_CurrentAngles, &forward, &right, &up);

	for ( int i = 0; i < SHIELD_NUM_CONTROL_POINTS; ++i )
	{
		// Compute the world space position...
		VectorCopy( m_Position, m_pControlPoint[i] );
		m_pControlPoint[i] += right * m_pFixedDirection[i].x;
		m_pControlPoint[i] += up * m_pFixedDirection[i].z;
		m_pControlPoint[i] += forward * m_pFixedDirection[i].y;
	}
}


//-----------------------------------------------------------------------------
// Gets the frustum size
//-----------------------------------------------------------------------------
void CShieldEffect::GetPanelSize( Vector& mins, Vector& maxs ) const
{
	VectorCopy( m_PanelBoxMin, mins );
	VectorCopy( m_PanelBoxMax, maxs );
}


void CShieldEffect::SetAngularSpringConstant( float flConstant )
{
	m_AngularSpringConstant = flConstant;
}


//-----------------------------------------------------------------------------
// Set the shield theta & phi
//-----------------------------------------------------------------------------
void CShieldEffect::SetThetaPhi( float flTheta, float flPhi )
{
	m_ShieldTheta = M_PI * flTheta / 180.0f;
	m_ShieldPhi = M_PI * flPhi / 180.0f;

	// Computes the rest positions
	ComputeRestPositions();
}


//-----------------------------------------------------------------------------
// The current position (computed by Simulate on the server)
//-----------------------------------------------------------------------------
const Vector& CShieldEffect::GetCurrentPosition()
{
	return m_Position;
}

void CShieldEffect::SetCurrentPosition( const Vector& pos )
{
	m_Position = pos;
}

void CShieldEffect::SetCurrentAngles( const QAngle& angles )
{
	VectorCopy( angles, m_CurrentAngles );
}


//-----------------------------------------------------------------------------
// Simulate the center of mass 
//-----------------------------------------------------------------------------
void CShieldEffect::SimulateTranslation( float dt )
{
	// Hook's law for a damped spring:
	// got two particles, a and b with positions xa and xb and velocities va and vb
	// and l = xa - xb
	// fa = -( ks * (|l| - r) + kd * (va - vb) dot (l) / |l|) * l/|l|
	Vector dx, force;

	// Case where we're connected to a control point
	dx = m_Position - m_vecDesiredOrigin;

	// rest condition
	float length = dx.Length();
	float speedSq = m_Velocity.LengthSqr(); 
	if ((length < 1e-3) && (speedSq < 1e-6))
		return;

	// Compute force
	if (length > 1e-3)
		dx /= length;
	else
		dx.Init( 0, 0, 0 );

	float springfactor = m_SpringConstant * length;
	float dampfactor = m_DampConstant * DotProduct( m_Velocity, dx );
	force = dx * -( springfactor + dampfactor );

	assert( force.IsValid( ) );
	Vector drag = m_Velocity * m_ViscousDrag;
	force -= drag;

	// Update position and velocity
	m_Position += m_Velocity * dt; 
	m_Velocity += force * dt / m_Mass;

	assert( m_Velocity.IsValid( ) );

	// clamp for stability
	if (speedSq > 1e6)
	{
		m_Velocity *= 1e3 / sqrt(speedSq);
	}
}


void CShieldEffect::SimulateRotation( float dt, const Vector& forward )
{
	// Here's a torsional spring for the angular component...
	// A little tricky: We need to actually think about 2 torsional springs,
	// one in thetha (x-y plane), and one in phi (z-plane)

	float phi2 = acos( forward.z );
	float dPhi = m_Phi - phi2;

	float theta2 = atan2( forward.y, forward.x );
	float dTheta = (m_Theta - theta2);
	if (dTheta > M_PI)
		dTheta -= 2 * M_PI;
	else if (dTheta < -M_PI)
		dTheta += 2 * M_PI;

	// rest condition...
	if ((fabs(dTheta) < 1e-3) && (fabs(m_ThetaVelocity) < 1e-6) && 
		(fabs(dPhi) < 1e-3) && (fabs(m_PhiVelocity) < 1e-6))
	{
		return;
	}

	float springfactor = m_AngularSpringConstant * dTheta;
	float torqueTheta = -springfactor; // + dampfactor);
	torqueTheta -= m_ThetaVelocity * m_AngularViscousDrag;

	springfactor = m_AngularSpringConstant * dPhi;
	float torqueTPhi = -springfactor; // + dampfactor);
	torqueTPhi -= m_PhiVelocity * m_AngularViscousDrag;

	// Update position and velocity
	m_Theta += m_ThetaVelocity * dt; 
	m_ThetaVelocity += torqueTheta * dt;
	m_Phi += m_PhiVelocity * dt; 
	m_PhiVelocity += torqueTPhi * dt;

	// clamp for stability
	if (fabs(m_ThetaVelocity) > 1e2)
	{
		m_ThetaVelocity *= 1e2 / m_ThetaVelocity;
	}
	if (fabs(m_PhiVelocity) > 1e2)
	{
		m_PhiVelocity *= 1e2 / m_PhiVelocity;
	}

	ComputeOrientationMatrix();
}


//-----------------------------------------------------------------------------
// Update the shield position: 
//-----------------------------------------------------------------------------
void CShieldEffect::Simulate( float dt )
{
	// We're gonna basically assume a spring connected to the center control point
	Vector forward;
	AngleVectors(m_angDesiredAngles, &forward, 0, 0);

	// We've got two springs: a spring connected to the origin
	// and a torsional spring connected to the view direction.

	// Stiff spring, subdivide time....
	dt /= SHIELD_TIME_SUBVISIBIONS;
	for (int i = 0; i < SHIELD_TIME_SUBVISIBIONS; ++i)
	{
		SimulateTranslation( dt );
		SimulateRotation( dt, forward );
	}

	ComputeControlPoints();
}


//-----------------------------------------------------------------------------
// Determines shield obstructions 
//-----------------------------------------------------------------------------
static inline bool IsPointValid( bool* pActivePoints, int i, int j )
{
	// Here's the control point we're checking
	int idx = j * SHIELD_NUM_HORIZONTAL_POINTS + i;

	return pActivePoints[idx];
}