summaryrefslogtreecommitdiff
path: root/game/server/tf2/tf_shieldgrenade.cpp
blob: 157395f508df06161a84d4c573c9af4da0516d82 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "BaseAnimating.h"
#include "tf_shieldgrenade.h"
#include "tf_shieldshared.h"
#include "tf_shield_flat.h"
#include "tf_player.h"
#include "engine/IEngineSound.h"
#include "Sprite.h"

#define SHIELD_GRENADE_FUSE_TIME 2.25f

//-----------------------------------------------------------------------------
//
// The shield grenade class
//
//-----------------------------------------------------------------------------

class CShieldGrenade : public CBaseAnimating
{
	DECLARE_CLASS( CShieldGrenade, CBaseAnimating );
public:
	DECLARE_DATADESC();

	CShieldGrenade();

	virtual void	Spawn( void );
	virtual void	Precache( void );
	virtual void	UpdateOnRemove( void );
	void			SetLifetime( float timer );

	int GetDamageType() const { return DMG_ENERGYBEAM; }

	void			StickyTouch( CBaseEntity *pOther );
	void			BeepThink( void );
	void			ShieldActiveThink( void );
	void			DeathThink( void );

	virtual bool CanTakeEMPDamage() { return true; }
	virtual bool TakeEMPDamage( float duration );

private:
	// Check when we're done with EMP
	void	CheckEMPDamageFinish( );
	void	CreateShield( );
	void	ComputeActiveThinkTime( );
	void	BounceSound( );

private:
	// Make sure all grenades explode
	Vector	m_LastCollision;

	// Time when EMP runs out
	float	m_flEMPDamageEndTime;
	float	m_ShieldLifetime;
	float	m_flDetonateTime;

	// Are we EMPed?
	bool	m_IsEMPed;
	bool	m_IsDeployed;

	// The deployed shield
	CHandle<CShieldFlat>	m_hDeployedShield;

	CSprite	*m_pLiveSprite;
};

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

// Global Savedata for friction modifier
BEGIN_DATADESC( CShieldGrenade )

	// Function Pointers
	DEFINE_FUNCTION( StickyTouch ),
	DEFINE_FUNCTION( BeepThink ),
	DEFINE_FUNCTION( ShieldActiveThink ),
	DEFINE_FUNCTION( DeathThink ),

END_DATADESC()

LINK_ENTITY_TO_CLASS( grenade_shield, CShieldGrenade );
PRECACHE_REGISTER( grenade_shield );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CShieldGrenade::CShieldGrenade()
{
	UseClientSideAnimation();
	m_hDeployedShield.Set(0);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldGrenade::Precache( void )
{
	PrecacheModel( "models/weapons/w_grenade.mdl" );

	PrecacheScriptSound( "ShieldGrenade.Bounce" );
	PrecacheScriptSound( "ShieldGrenade.StickBeep" );

	PrecacheModel( "sprites/redglow1.vmt" );

	BaseClass::Precache();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldGrenade::Spawn( void )
{
	BaseClass::Spawn();
	m_LastCollision.Init( 0, 0, 0 );
	SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_BOUNCE );
	SetSolid( SOLID_BBOX );
	SetGravity( 1.0 );
	SetFriction( 0.9 );
	SetModel( "models/weapons/w_grenade.mdl");
	UTIL_SetSize(this, Vector( -4, -4, -4), Vector(4, 4, 4));
	m_IsEMPed = false;
	m_IsDeployed = false;

	m_flEMPDamageEndTime = 0.0f;

	SetTouch( StickyTouch );
	SetCollisionGroup( TFCOLLISION_GROUP_GRENADE );

	// Create a green light
	m_pLiveSprite = CSprite::SpriteCreate( "sprites/redglow1.vmt", GetLocalOrigin() + Vector(0,0,1), false );
	m_pLiveSprite->SetTransparency( kRenderGlow, 0, 0, 255, 128, kRenderFxNoDissipation );
	m_pLiveSprite->SetScale( 1 );
	m_pLiveSprite->SetAttachment( this, 0 );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldGrenade::UpdateOnRemove( void )
{
	if ( m_pLiveSprite )
	{
		UTIL_Remove( m_pLiveSprite );
		m_pLiveSprite = NULL;
	}

	BaseClass::UpdateOnRemove();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldGrenade::SetLifetime( float timer )
{
	m_ShieldLifetime = timer;
}


//-----------------------------------------------------------------------------
// EMP Related methods
//-----------------------------------------------------------------------------
bool CShieldGrenade::TakeEMPDamage( float duration )
{
	m_flEMPDamageEndTime = gpGlobals->curtime + duration;
	m_IsEMPed = true;
	if (m_hDeployedShield)
	{
		m_hDeployedShield->SetEMPed(true);

		// Recompute the next think time
		ComputeActiveThinkTime();
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: See if EMP impairment time has elapsed
//-----------------------------------------------------------------------------
void CShieldGrenade::CheckEMPDamageFinish( void )
{
	if ( !m_flEMPDamageEndTime || gpGlobals->curtime < m_flEMPDamageEndTime )
		return;

	m_flEMPDamageEndTime = 0.0f;
	m_IsEMPed = false;
	if (m_hDeployedShield)
	{
		m_hDeployedShield->SetEMPed(false);

		// Recompute the next think time
		ComputeActiveThinkTime();
	}
}


//-----------------------------------------------------------------------------
// Plays a random bounce sound
//-----------------------------------------------------------------------------
void CShieldGrenade ::BounceSound( void )
{
	EmitSound( "ShieldGrenade.Bounce" );
}

//-----------------------------------------------------------------------------
// Purpose: Make the grenade stick to whatever it touches
//-----------------------------------------------------------------------------
void CShieldGrenade::StickyTouch( CBaseEntity *pOther )
{
	if (m_IsDeployed)
		return;

	// The touch can get called multiple times - create an ignore case if we
	// have already stuck.
	if ( m_LastCollision == GetLocalOrigin() )
		return;

	// Only stick to floors...
	Vector up( 0, 0, 1 );
	if ( DotProduct( GetTouchTrace().plane.normal, up ) < 0.5f )
		return;
	
	// Only stick to BSP models
	if ( pOther->IsBSPModel() == false )
		return;

	BounceSound();
	SetAbsVelocity( vec3_origin );
	SetMoveType( MOVETYPE_NONE );

	// Beep
	EmitSound( "ShieldGrenade.StickBeep" );

	// Start ticking...
	SetThink( BeepThink );
	m_IsDeployed = true;
	SetNextThink( gpGlobals->curtime + 0.01f );
	m_flDetonateTime = gpGlobals->curtime + SHIELD_GRENADE_FUSE_TIME;

	m_LastCollision = GetLocalOrigin();
}

//-----------------------------------------------------------------------------
// Purpose: Play beeping sounds until the charge explode
//-----------------------------------------------------------------------------
void CShieldGrenade::CreateShield( void )
{
	/*
	// Set the orientation of the shield based on 
	// the closest teammate's relative position...
	float mindist = FLT_MAX;
	Vector dir;

	for ( int i = 1; i <= gpGlobals->maxClients; i++ )
	{
		CBaseTFPlayer *pPlayer = ToBaseTFPlayer( UTIL_PlayerByIndex(i) );
		if ( pPlayer )
		{
			if (pPlayer->GetTeamNumber() == GetTeamNumber())
			{
				Vector tempdir;
				VectorSubtract( pPlayer->Center(), Center(), tempdir );
				float dist = VectorNormalize( tempdir );
				if (dist < mindist)
				{
					 mindist = dist;
					 VectorCopy( tempdir, dir );
				}
			}
		}
	}

	if( mindist == FLT_MAX )
	{
		AngleVectors( GetAngles(), &dir );
	}

	// Never pitch the shield
	dir.z = 0.0f;
	VectorNormalize(dir);

	QAngle relAngles;
	VMatrix parentMatrix;
	VMatrix worldShieldMatrix;
	VMatrix relativeMatrix;
	VMatrix parentInvMatrix;

	// Construct a transform from shield to grenade (parent)
	MatrixFromAngles( GetAngles(), parentMatrix );

#ifdef _DEBUG
	bool ok = 
#endif
		MatrixInverseGeneral( parentMatrix, parentInvMatrix );
	Assert( ok );

	Vector up( 0, 0, 1 );
	Vector left;
	CrossProduct( up, dir, left );
	MatrixSetIdentity( worldShieldMatrix );
	worldShieldMatrix.SetUp( up );
	worldShieldMatrix.SetLeft( left );
	worldShieldMatrix.SetForward( dir );

	MatrixMultiply( parentInvMatrix, worldShieldMatrix, relativeMatrix );
	MatrixToAngles( relativeMatrix, relAngles );
	*/
	
	Vector offset( 0, 0, SHIELD_GRENADE_HEIGHT * 0.5f );
	m_hDeployedShield = CreateFlatShield( this, SHIELD_GRENADE_WIDTH, 
		SHIELD_GRENADE_HEIGHT, offset, vec3_angle );

	// Notify it about EMP state
	if (m_IsEMPed)
		m_hDeployedShield->SetEMPed(true);

	// Play a sound
//	WeaponSound( SPECIAL1 );
}


//-----------------------------------------------------------------------------
// Purpose: Play beeping sounds until the charge explode
//-----------------------------------------------------------------------------
void CShieldGrenade::BeepThink( void )
{
	if (!IsInWorld())
	{
		UTIL_Remove( this );
		return;
	}

	if (m_flDetonateTime <= gpGlobals->curtime)
	{
		// Here we must project the shield
		CreateShield();
		SetThink( ShieldActiveThink );
		m_flDetonateTime = gpGlobals->curtime + m_ShieldLifetime;

		// Get the EMP state correct
		CheckEMPDamageFinish();
		ComputeActiveThinkTime();
	}
	else
	{
		SetNextThink( gpGlobals->curtime + 1.0f );
	}
}


//-----------------------------------------------------------------------------
// Compute next think time while active
//-----------------------------------------------------------------------------
void CShieldGrenade::ComputeActiveThinkTime( void )
{
	// Next think should be when we detonate, unless we un-EMP before then
	SetNextThink( gpGlobals->curtime + m_flDetonateTime );
	if (m_IsEMPed)
	{
		Assert( m_flEMPDamageEndTime != 0.0f );
		if ( m_flEMPDamageEndTime < GetNextThink() )
			SetNextThink( gpGlobals->curtime + m_flEMPDamageEndTime );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Here's where the grenade "detonates"
//-----------------------------------------------------------------------------
void CShieldGrenade::ShieldActiveThink( void )
{
	if (m_flDetonateTime > gpGlobals->curtime)
	{
		// If it's not time to die, check EMP state
		CheckEMPDamageFinish();
	}
	else
	{
		if (m_hDeployedShield)
		{
			m_hDeployedShield->Activate( false );
		}
		SetNextThink( gpGlobals->curtime + SHIELD_FLAT_SHUTDOWN_TIME + 0.2f );
		SetThink( DeathThink );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CShieldGrenade::DeathThink( void )
{
	// kill the grenade
	if (m_hDeployedShield)
	{
		UTIL_Remove( m_hDeployedShield );
		m_hDeployedShield.Set(0);
	}
	UTIL_Remove( this );
}

//-----------------------------------------------------------------------------
// Creates a shield grenade
//-----------------------------------------------------------------------------
CBaseEntity *CreateShieldGrenade( const Vector &position, const QAngle &angles,	const Vector &velocity, const QAngle &angVelocity, CBaseEntity *pOwner, float timer )
{
	CShieldGrenade *pGrenade = (CShieldGrenade *)CBaseEntity::Create( "grenade_shield", position, angles, pOwner );
	pGrenade->SetLifetime( timer );
	pGrenade->SetAbsVelocity( velocity );

	if (pOwner)
	{
		pGrenade->ChangeTeam( pOwner->GetTeamNumber() );
	}

	return pGrenade;
}