summaryrefslogtreecommitdiff
path: root/game/client/tf2/c_shield_mobile.cpp
blob: 1de859203aad03f16a40e63b115bf2e710372d8c (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Client's sheild entity
//
// $Workfile:     $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "C_Shield.h"
#include "tf_shieldshared.h"

enum
{
	NUM_SUBDIVISIONS = 21,
};

#define EMP_WAVE_AMPLITUDE 8.0f

//-----------------------------------------------------------------------------
// Mobile version of the shield 
//-----------------------------------------------------------------------------

class C_ShieldMobile;
class C_ShieldMobileActiveVertList : public IActiveVertList
{
public:
	void			Init( C_ShieldMobile *pShield, unsigned char *pVertList );

// IActiveVertList overrides.
public:

	virtual int		GetActiveVertState( int iVert );
	virtual void	SetActiveVertState( int iVert, int bOn );

private:
	C_ShieldMobile	*m_pShield;
	unsigned char	*m_pVertsActive;
};


class C_ShieldMobile : public C_Shield
{
	DECLARE_CLASS( C_ShieldMobile, C_Shield );
public:
	DECLARE_CLIENTCLASS();

	C_ShieldMobile();
	~C_ShieldMobile();

	void OnDataChanged( DataUpdateType_t updateType );
	virtual void GetBounds( Vector& mins, Vector& maxs );

	virtual void AddEntity( );

	// Return true if the panel is active 
	virtual bool IsPanelActive( int x, int y );

	// Gets at the control point data; who knows how it was made?
	virtual void GetShieldData( Vector const** ppVerts, float* pOpacity, float* pBlend );
	virtual const Vector& GetPoint( int x, int y ) { return m_ShieldEffect.GetPoint( x, y ); }

	virtual void SetThetaPhi( float flTheta, float flPhi ) { m_ShieldEffect.SetThetaPhi(flTheta,flPhi); }

public:
	// networked data
	unsigned char	m_pVertsActive[SHIELD_VERTEX_BYTES];
	unsigned char	m_ShieldState;

private:
	C_ShieldMobile( const C_ShieldMobile& );

	// Is a particular panel an edge?
	bool IsVertexValid( float s, float t ) const;
	void PreRender( );

private:
	CShieldEffect					m_ShieldEffect;
	C_ShieldMobileActiveVertList	m_VertList;
	float m_flTheta;
	float m_flPhi;
};


//-----------------------------------------------------------------------------
// C_ShieldMobileActiveVertList functions
//-----------------------------------------------------------------------------

void C_ShieldMobileActiveVertList::Init( C_ShieldMobile *pShield, unsigned char *pVertList )
{
	m_pShield = pShield;
	m_pVertsActive = pVertList;
}


int C_ShieldMobileActiveVertList::GetActiveVertState( int iVert )
{
	return m_pVertsActive[iVert>>3] & (1 << (iVert & 7));
}


void C_ShieldMobileActiveVertList::SetActiveVertState( int iVert, int bOn )
{
	if ( bOn )
		m_pVertsActive[iVert>>3] |= (1 << (iVert & 7));
	else
		m_pVertsActive[iVert>>3] &= ~(1 << (iVert & 7));
}


//-----------------------------------------------------------------------------
// Data table
//-----------------------------------------------------------------------------

IMPLEMENT_CLIENTCLASS_DT(C_ShieldMobile, DT_Shield_Mobile, CShieldMobile)

	RecvPropInt( RECVINFO(m_ShieldState) ),
	RecvPropArray( 
		RecvPropInt( RECVINFO(m_pVertsActive[0])), 
		m_pVertsActive 
	),
	RecvPropFloat( RECVINFO(m_flTheta) ),
	RecvPropFloat( RECVINFO(m_flPhi) ),

END_RECV_TABLE()


//-----------------------------------------------------------------------------
// Various raycasting routines
//-----------------------------------------------------------------------------


void ShieldTraceLine(const Vector &vecStart, const Vector &vecEnd, 
					 unsigned int mask, int collisionGroup, trace_t *ptr)
{
	UTIL_TraceLine(vecStart, vecEnd, mask, NULL, collisionGroup, ptr );
}

void ShieldTraceHull(const Vector &vecStart, const Vector &vecEnd, 
					 const Vector &hullMin, const Vector &hullMax, 
					 unsigned int mask, int collisionGroup, trace_t *ptr)
{
	CTraceFilterWorldOnly traceFilter;
	enginetrace->TraceHull( vecStart, vecEnd, hullMin, hullMax, mask, &traceFilter, ptr );
}


//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------

C_ShieldMobile::C_ShieldMobile() : m_ShieldEffect(ShieldTraceLine, ShieldTraceHull)
{
	m_VertList.Init( this, m_pVertsActive );
	m_ShieldEffect.SetActiveVertexList( &m_VertList );
	m_ShieldEffect.Spawn(vec3_origin, vec3_angle);
	InitShield( SHIELD_NUM_HORIZONTAL_POINTS, SHIELD_NUM_VERTICAL_POINTS, NUM_SUBDIVISIONS );
}

C_ShieldMobile::~C_ShieldMobile()
{
}

//-----------------------------------------------------------------------------
// Get this after the data changes
//-----------------------------------------------------------------------------

void C_ShieldMobile::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	m_ShieldEffect.SetCurrentPosition( GetAbsOrigin() );
	m_ShieldEffect.SetCurrentAngles( GetAbsAngles() );
	m_ShieldEffect.SetThetaPhi( m_flTheta, m_flPhi );

	// No need to simulate, just compute active panels from network data
	m_ShieldEffect.ComputeControlPoints();
	m_ShieldEffect.ComputePanelActivity();
}

//-----------------------------------------------------------------------------
// A little pre-render processing
//-----------------------------------------------------------------------------

void C_ShieldMobile::PreRender( )
{
	if (m_ShieldState & SHIELD_MOBILE_EMP)
	{
		// Decay fade if we've been EMPed or if we're inactive
		if (m_FadeValue > 0.0f)
		{
			m_FadeValue -= gpGlobals->frametime / SHIELD_EMP_FADE_TIME;
			if (m_FadeValue < 0.0f)
			{
				m_FadeValue = 0.0f;

				// Reset the shield to un-wobbled state
				m_ShieldEffect.ComputeControlPoints();
			}
			else
			{
				Vector dir;
				AngleVectors( m_ShieldEffect.GetCurrentAngles(), & dir );

				// Futz with the control points if we've been EMPed
				for (int i = 0; i < SHIELD_NUM_CONTROL_POINTS; ++i)
				{
					// Get the direction for the point
					float factor = -EMP_WAVE_AMPLITUDE * sin( i * M_PI * 0.5f + gpGlobals->curtime * M_PI / SHIELD_EMP_WOBBLE_TIME );
					m_ShieldEffect.GetPoint(i) += dir * factor;
				}
			}
		}
	}
	else
	{
		// Fade back in, no longer EMPed
		if (m_FadeValue < 1.0f)
		{
			m_FadeValue += gpGlobals->frametime / SHIELD_EMP_FADE_TIME;
			if (m_FadeValue >= 1.0f)
			{
				m_FadeValue = 1.0f;
			}
		}
	}
}

void C_ShieldMobile::AddEntity( )
{
	BaseClass::AddEntity( );
	PreRender();
}

//-----------------------------------------------------------------------------
// Bounds computation
//-----------------------------------------------------------------------------

void C_ShieldMobile::GetBounds( Vector& mins, Vector& maxs )
{
	m_ShieldEffect.ComputeBounds( mins, maxs );
}

//-----------------------------------------------------------------------------
// Return true if the panel is active 
//-----------------------------------------------------------------------------

bool C_ShieldMobile::IsPanelActive( int x, int y )
{
	return m_ShieldEffect.IsPanelActive(x, y);
}

//-----------------------------------------------------------------------------
// Gets at the control point data; who knows how it was made?
//-----------------------------------------------------------------------------

void C_ShieldMobile::GetShieldData( Vector const** ppVerts, float* pOpacity, float* pBlend )
{
	for ( int i = 0; i < SHIELD_NUM_CONTROL_POINTS; ++i )
	{
		ppVerts[i] = &m_ShieldEffect.GetControlPoint(i);

		if ( m_pVertsActive[i >> 3] & (1 << (i & 0x7)) )
		{
			pOpacity[i] = m_ShieldEffect.ComputeOpacity( *ppVerts[i], GetAbsOrigin() );
			pBlend[i] = 1.0f;
		}
		else
		{
			pOpacity[i] = 192.0f;
			pBlend[i] = 0.0f;
		}
	}
}