summaryrefslogtreecommitdiff
path: root/game/shared/tf2/grenade_emp.cpp
blob: 800c80fa30250281f99373a3e732a1b5abad9a93 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "basetfplayer_shared.h"
#include "basegrenade_shared.h"
#include "engine/IEngineSound.h"
#include "tf_shareddefs.h"
#include "Sprite.h"
#include "grenade_emp.h"
#include "tf_gamerules.h"

#if defined( CLIENT_DLL )

#include "particles_simple.h"

#else

#endif

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


int g_iEMPPulseEffectIndex = 0;

// Damage CVars
ConVar	weapon_emp_grenade_duration( "weapon_emp_grenade_duration","5", FCVAR_REPLICATED, "Duration of the EMP grenade's effect." );
ConVar	weapon_emp_grenade_object_duration( "weapon_emp_grenade_object_duration","5", FCVAR_REPLICATED, "Duration of the EMP grenade's effect on objects." );
ConVar	weapon_emp_grenade_radius( "weapon_emp_grenade_radius","256", FCVAR_REPLICATED, "EMP grenade splash radius" );

IMPLEMENT_NETWORKCLASS_ALIASED( GrenadeEMP, DT_GrenadeEMP );

BEGIN_NETWORK_TABLE( CGrenadeEMP, DT_GrenadeEMP )
#if !defined( CLIENT_DLL )
	SendPropEHandle( SENDINFO( m_hLiveSprite ) ),
#else
	RecvPropEHandle( RECVINFO( m_hLiveSprite ) ),
#endif
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CGrenadeEMP )
	DEFINE_PRED_FIELD( m_hLiveSprite, FIELD_EHANDLE, FTYPEDESC_INSENDTABLE ),
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( grenade_emp, CGrenadeEMP );
PRECACHE_REGISTER(grenade_emp);

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CGrenadeEMP::CGrenadeEMP()
{
	SetPredictionEligible( true );

#if defined( CLIENT_DLL )
	m_ParticleEvent.Init( 100 );
#else
	UseClientSideAnimation();
#endif
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CGrenadeEMP::Precache( void )
{
	BaseClass::Precache( );

	PrecacheModel( "models/weapons/w_grenade.mdl" );
	PrecacheModel( "sprites/redglow1.vmt" );
	g_iEMPPulseEffectIndex = PrecacheModel( "sprites/lgtning.spr" );

	PrecacheScriptSound( "GrenadeEMP.Bounce" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CGrenadeEMP::Spawn( void )
{
	Precache();

	SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_BOUNCE );
	SetSolid( SOLID_BBOX );
	//m_flGravity = 1.0;
	SetFriction( 0.75 );
	SetModel( "models/weapons/w_grenade.mdl" );
	SetSize( Vector( -4, -4, -4), Vector(4, 4, 4) );
	SetTouch( BounceTouch );
	SetCollisionGroup( TFCOLLISION_GROUP_GRENADE );

	m_flDetonateTime = gpGlobals->curtime + 4.0;
	SetThink( TumbleThink );
	SetNextThink( gpGlobals->curtime + 0.1f );

	// Set my damages to the cvar values
	SetDamage( weapon_emp_grenade_duration.GetFloat() );
	SetDamageRadius( weapon_emp_grenade_radius.GetFloat() );

	// Create a white light
	CBasePlayer *player = ToBasePlayer( GetOwnerEntity() );
	if ( player )
	{
		m_hLiveSprite = SPRITE_CREATE_PREDICTABLE( "sprites/chargeball2.vmt", GetLocalOrigin() + Vector(0,0,1), false );
		if ( m_hLiveSprite )
		{
			m_hLiveSprite->SetOwnerEntity( player );
			m_hLiveSprite->SetPlayerSimulated( player );
			m_hLiveSprite->SetTransparency( kRenderGlow, 255, 255, 255, 128, kRenderFxNoDissipation );
			m_hLiveSprite->SetBrightness( 255 );
			m_hLiveSprite->SetScale( 0.15, 5.0f );
			m_hLiveSprite->SetAttachment( this, 0 );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CGrenadeEMP::UpdateOnRemove( void )
{
	// Remove our live sprite
	if ( m_hLiveSprite )
	{
		m_hLiveSprite->Remove( );
		m_hLiveSprite = NULL;
	}

	// Chain at end to mimic destructor unwind order
	BaseClass::UpdateOnRemove();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CGrenadeEMP::Explode( trace_t *pTrace, int bitsDamageType )
{
#if !defined( CLIENT_DLL )
	// While in scope, this will allow messages to pass through without being filtered.
	CDisablePredictionFiltering dpf;

	// Create EMP pulse effect
	int iEmpRings = 4;
	float fEmpDelay = 0.05f;
	float delay = 0.0f;
	float frac;
	for ( int r = 0 ; r < iEmpRings; r++, delay += fEmpDelay )
	{
		frac = (float)( r )/(float)(iEmpRings - 1);

		CBroadcastRecipientFilter filter;

		// Since this doesn't fire on the client right now, ignore the culling of the local player
		filter.SetIgnorePredictionCull( true );

		te->BeamRingPoint( filter, delay, 
			GetAbsOrigin() + Vector(0,0,32) ,  // origin
			64.0f,					// start radius
			weapon_emp_grenade_radius.GetFloat() * 2,					// end radius
			g_iEMPPulseEffectIndex, 
			0, // halo index
			0, // start frame
			2, // framerate
			0.3f, //  life
			25.0, // width
			50, // spread
			2, // amplitude
			50 + ( 1-frac ) * 200, 
			63, 
			63 + 127 * frac, 
			255 - frac * 127, 
			20 );
	}

	ApplyRadiusEMPEffect( GetThrower(), GetAbsOrigin() + Vector(0,0,16) );
	Remove( );
#endif
}

//-----------------------------------------------------------------------------
// Purpose: EMP enemies around the grenade
//-----------------------------------------------------------------------------
void CGrenadeEMP::ApplyRadiusEMPEffect( CBaseEntity *pOwner, const Vector& vecCenter )
{
	// Oh oh, owner is gone...
	if ( !pOwner )
		return;

#if !defined( CLIENT_DLL )
	CBaseEntity *pEntity = NULL;

	for ( CEntitySphereQuery sphere( vecCenter, weapon_emp_grenade_radius.GetFloat() ); ( pEntity = sphere.GetCurrentEntity() ) != NULL; sphere.NextEntity() )
	{
		// Ignore team members, and unaligned targets
		if ( pOwner->InSameTeam( pEntity ) || pEntity->GetTeamNumber() == 0 )
			continue;

		if ( pEntity->IsSolidFlagSet( FSOLID_NOT_SOLID ) )
			continue;

		// Make sure it's not blocked by a shield or wall
		trace_t tr;
		if ( TFGameRules()->IsTraceBlockedByWorldOrShield( vecCenter, pEntity->WorldSpaceCenter(), this, DMG_PROBE, &tr ) )
			continue;

		if ( pEntity->CanBePoweredUp() )
		{
			// Is it an object?
			if ( pEntity->Classify() == CLASS_MILITARY )
			{
				pEntity->AttemptToPowerup( POWERUP_EMP, weapon_emp_grenade_object_duration.GetFloat() );
			}
			else
			{
				pEntity->AttemptToPowerup( POWERUP_EMP, weapon_emp_grenade_duration.GetFloat() );
			}
		}
	}
#endif
}

//-----------------------------------------------------------------------------
// Purpose: Allow shield parry's
//-----------------------------------------------------------------------------
void CGrenadeEMP::BounceTouch( CBaseEntity *pOther )
{
	Assert( pOther );
	if ( !pOther->IsSolid() )
		return;

	BaseClass::BounceTouch( pOther );
}

//-----------------------------------------------------------------------------
// Purpose: Play a distinctive grenade bounce sound to warn nearby players
//-----------------------------------------------------------------------------
void CGrenadeEMP::BounceSound( void )
{
	CPASAttenuationFilter filter( this, "GrenadeEMP.Bounce" );
	filter.UsePredictionRules();
	EmitSound( filter, entindex(), "GrenadeEMP.Bounce" );
}

//-----------------------------------------------------------------------------
// Purpose: Return the amplitude for the screenshake
//-----------------------------------------------------------------------------
float CGrenadeEMP::GetShakeAmplitude( void )
{
	return 0;
}

//-----------------------------------------------------------------------------
// Purpose: Create a missile
//-----------------------------------------------------------------------------
CGrenadeEMP *CGrenadeEMP::Create( const Vector &vecOrigin, const Vector &vecForward, CBasePlayer *pOwner )
{
	CGrenadeEMP *pGrenade = (CGrenadeEMP*)CREATE_PREDICTED_ENTITY( "grenade_emp" );
	if ( pGrenade )
	{
		UTIL_SetOrigin( pGrenade, vecOrigin );
		pGrenade->SetOwnerEntity( pOwner );
		pGrenade->Spawn();
		pGrenade->SetPlayerSimulated( pOwner );
		pGrenade->ChangeTeam( pOwner->GetTeamNumber() );

		pGrenade->SetThrower( pOwner );

		pGrenade->SetAbsVelocity( vecForward );

		QAngle angles;
		VectorAngles( vecForward, angles );
		pGrenade->SetLocalAngles( angles );

		pGrenade->SetLocalAngularVelocity( SHARED_RANDOMANGLE( -500, 500 ) );
	}

	return pGrenade;
}

#if defined( CLIENT_DLL )
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CGrenadeEMP::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	// Only think when sapping
	SetNextClientThink( CLIENT_THINK_ALWAYS );
}

//-----------------------------------------------------------------------------
// Purpose: Trail smoke
//-----------------------------------------------------------------------------
void CGrenadeEMP::ClientThink( void )
{
return;

	CSmartPtr<CSimpleEmitter> pEmitter = CSimpleEmitter::Create( "EMPGrenade::Effect" );
	PMaterialHandle	hSphereMaterial = pEmitter->GetPMaterial( "sprites/chargeball" );

	// Add particles at the target.
	float flCur = gpGlobals->frametime;
	while ( m_ParticleEvent.NextEvent( flCur ) )
	{
		Vector vecOrigin = GetAbsOrigin() + RandomVector( -2,2 );
		pEmitter->SetSortOrigin( vecOrigin );

		SimpleParticle *pParticle = (SimpleParticle *) pEmitter->AddParticle( sizeof(SimpleParticle), hSphereMaterial, vecOrigin );
		if ( pParticle == NULL )
			return;

		pParticle->m_flLifetime	= 0.0f;
		pParticle->m_flDieTime	= random->RandomFloat( 0.1f, 0.3f );

		pParticle->m_uchStartSize	= random->RandomFloat(2,4);
		pParticle->m_uchEndSize		= pParticle->m_uchStartSize + 2;

		pParticle->m_vecVelocity = vec3_origin;
		pParticle->m_uchStartAlpha = 128;
		pParticle->m_uchEndAlpha = 0;
		pParticle->m_flRoll	= random->RandomFloat( 180, 360 );
		pParticle->m_flRollDelta = random->RandomFloat( -1, 1 );

		pParticle->m_uchColor[0] = 128;
		pParticle->m_uchColor[1] = 128;
		pParticle->m_uchColor[2] = 128;
	}
}

int CGrenadeEMP::DrawModel( int flags )
{
	bool bret = BaseClass::DrawModel( flags );

	return bret;
}

#endif