summaryrefslogtreecommitdiff
path: root/game/shared/tf2/weapon_combat_plasmarifle.cpp
blob: 2ede901906d2437adb4754a0917b9fbed1d9fdcd (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Chargeable Plasma & Shield combo
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "basetfplayer_shared.h"
#include "weapon_combatshield.h"
#include "in_buttons.h"
#include "weapon_combat_usedwithshieldbase.h"
#include "plasmaprojectile.h"
#include "in_buttons.h"
#include "tf_shareddefs.h"

#if defined( CLIENT_DLL )

#include "iefx.h"
#include "dlight.h"
#include "clienteffectprecachesystem.h"
#include "beamdraw.h"

#define CWeaponCombatPlasmaRifle C_WeaponCombatPlasmaRifle
#define CWeaponCombatPlasmaRifleHuman C_WeaponCombatPlasmaRifleHuman
#define CWeaponCombatPlasmaRifleAlien C_WeaponCombatPlasmaRifleAlien

#endif

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

#if defined( CLIENT_DLL )

class CChargeBall;
// Precache the effects
CLIENTEFFECT_REGISTER_BEGIN( PrecacheWeaponCombatPlasmaRifle )
CLIENTEFFECT_MATERIAL( "sprites/chargeball_team1" )
CLIENTEFFECT_MATERIAL( "sprites/chargeball_team2" )
CLIENTEFFECT_REGISTER_END()

#endif

// Damage CVars
ConVar	weapon_combat_plasmarifle_damage( "weapon_combat_plasmarifle_damage","10", FCVAR_REPLICATED, "Plasma Rifle maximum damage" );
ConVar	weapon_combat_plasmarifle_range( "weapon_combat_plasmarifle_range","500", FCVAR_REPLICATED, "Plasma Rifle maximum range" );
ConVar	weapon_combat_plasmarifle_radius( "weapon_combat_plasmarifle_radius","90", FCVAR_REPLICATED, "Plasma Rifle explosion radius when charged" );
ConVar	weapon_combat_plasmarifle_ducking_mod( "weapon_combat_plasmarifle_ducking_mod", "0.6f", FCVAR_REPLICATED, "Plasma Rifle ducking speed modifier" );

//-----------------------------------------------------------------------------
// Purpose: Shared viersion of CWeaponCombatPlasmaRifle
//-----------------------------------------------------------------------------
class CWeaponCombatPlasmaRifle : public CWeaponCombatUsedWithShieldBase
{
	DECLARE_CLASS( CWeaponCombatPlasmaRifle, CWeaponCombatUsedWithShieldBase );
public:
	DECLARE_NETWORKCLASS();
	DECLARE_PREDICTABLE();
#if !defined( CLIENT_DLL )
	DECLARE_DATADESC();
#endif

	CWeaponCombatPlasmaRifle( void ) {}

	virtual void	ItemPostFrame( void );
	virtual void	PrimaryAttack( void );
	virtual float 	GetFireRate( void );
	virtual void	Spawn();
	virtual bool	Deploy( void );
	virtual bool	Holster( CBaseCombatWeapon *pSwitchingTo = NULL );
	void			ChargeThink( void );
	virtual float	GetDefaultAnimSpeed( void );

	// All predicted weapons need to implement and return true
	virtual bool			IsPredicted( void ) const
	{ 
		return true;
	}

private:
	CWeaponCombatPlasmaRifle( const CWeaponCombatPlasmaRifle & );
public:

#if defined( CLIENT_DLL )
	virtual bool	ShouldPredict( void )
	{
		if ( GetOwner() && 
			GetOwner() == C_BasePlayer::GetLocalPlayer() )
			return true;

		return BaseClass::ShouldPredict();
	}

	virtual void	OnDataChanged( DataUpdateType_t updateType );
	virtual int		DrawModel( int flags );
	virtual void	ViewModelDrawn( CBaseViewModel *pBaseViewModel );
	virtual void	ClientThink( );
	virtual bool	IsTransparent( );
private:
	// Purpose: Draws the charging effect
	void DrawChargingEffect( float flSize, CBaseAnimating *pAttachedEnt );
	CMaterialReference m_hMaterial;

#endif
private:
	CNetworkVar( float, m_flPower );
	CNetworkVar( bool, m_bCharging );
};

//-----------------------------------------------------------------------------
// Spawn weapon
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::Spawn()
{
	BaseClass::Spawn();
	m_flPower = 1;
	m_bCharging = false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::ItemPostFrame( void )
{
	ChargeThink();

	CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
	if (!pOwner)
		return;

	if ( UsesClipsForAmmo1() )
	{
		CheckReload();
	}

	// Handle charge firing
	if ( GetShieldState() == SS_DOWN && !m_bInReload )
	{
		if ( (pOwner->m_nButtons & IN_ATTACK ) && (m_flNextPrimaryAttack <= gpGlobals->curtime) )
		{
			if ( m_iClip1 > 0 )
			{
				// Fire the plasma shot
				PrimaryAttack();
				m_flPower = 1.0;
			}
			else
			{
				Reload();
			}
		}

		// Reload button (or fire button when we're out of ammo)
		if ( m_flNextPrimaryAttack <= gpGlobals->curtime ) 
		{
			if ( pOwner->m_nButtons & IN_RELOAD ) 
			{
				Reload();
			}
			else if ( !((pOwner->m_nButtons & IN_ATTACK) || (pOwner->m_nButtons & IN_ATTACK2) || (pOwner->m_nButtons & IN_RELOAD)) )
			{
				if ( !m_iClip1 && HasPrimaryAmmo() )
				{
					Reload();
				}
			}
		}
	}

	// Prevent shield post frame if we're not ready to attack, or we're charging
	AllowShieldPostFrame( m_flNextPrimaryAttack <= gpGlobals->curtime || m_bInReload );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::PrimaryAttack( void )
{
	CBaseTFPlayer *pPlayer = (CBaseTFPlayer*)GetOwner();
	if (!pPlayer)
		return;
	
	WeaponSound(SINGLE);

	// Fire the bullets
	Vector vecSrc = pPlayer->Weapon_ShootPosition( );
	Vector vecAiming;
	pPlayer->EyeVectors( &vecAiming );

	PlayAttackAnimation( GetPrimaryAttackActivity() );

	// Shift it down a bit so the firer can see it
	Vector right, forward;
	AngleVectors( pPlayer->EyeAngles() + pPlayer->m_Local.m_vecPunchAngle, &forward, &right, NULL );
	Vector vecStartSpot = vecSrc;

	Vector gunOffset = Vector(0,0,-8) + right * 12 + forward * 16;

	CPowerPlasmaProjectile *pPlasma = CPowerPlasmaProjectile::CreatePredicted( vecStartSpot, vecAiming, gunOffset, DMG_ENERGYBEAM, pPlayer );
	if ( pPlasma )
	{
		pPlasma->SetDamage( m_flPower * weapon_combat_plasmarifle_damage.GetFloat() );
		pPlasma->m_hOwner = pPlayer;
		pPlasma->SetPower( m_flPower );
		// Calculate range based upon charge power
		float flRange = weapon_combat_plasmarifle_range.GetFloat() + RemapVal( m_flPower, 1.0, MAX_RIFLE_POWER, 0, weapon_combat_plasmarifle_range.GetFloat() * 0.75 );
		pPlasma->SetMaxRange( flRange );
		pPlasma->Activate();
	}

	// Go explosive if fully charged
//	if ( m_flPower >= MAX_RIFLE_POWER )
//	{
//		pPlasma->SetExplosive( weapon_combat_plasmarifle_radius.GetFloat() );
//		pPlasma->SetPlasmaType( PLASMATYPE_PLASMABALL_EXPLOSIVE );
//	}

	m_flNextPrimaryAttack = gpGlobals->curtime + GetFireRate();
	m_iClip1 = m_iClip1 - 1;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CWeaponCombatPlasmaRifle::GetFireRate( void )
{
	float flFireRate = ( SequenceDuration() * 0.4 ) + SHARED_RANDOMFLOAT( 0.0, 0.035f );

	// Get the player.
	CBaseTFPlayer *pPlayer = ( CBaseTFPlayer* )GetOwner();
	if ( pPlayer )
	{
		// Fire more rapidly when we are ducking.
		if ( pPlayer->GetFlags() & FL_DUCKING )
		{
			flFireRate *= weapon_combat_plasmarifle_ducking_mod.GetFloat();
		}
	}

	return flFireRate;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CWeaponCombatPlasmaRifle::Deploy( void )
{
	if ( BaseClass::Deploy() )
	{
		m_bCharging = true;
		GainedNewTechnology(NULL);
		return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
// Purpose: Our player just died
//-----------------------------------------------------------------------------
bool CWeaponCombatPlasmaRifle::Holster( CBaseCombatWeapon *pSwitchingTo )
{
	if ( BaseClass::Holster(pSwitchingTo) )
	{
		m_bCharging = false;
		return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
// Purpose: Match the anim speed to the weapon speed while crouching
//-----------------------------------------------------------------------------
float CWeaponCombatPlasmaRifle::GetDefaultAnimSpeed( void )
{
	if ( GetOwner() && GetOwner()->IsPlayer() )
	{
		if ( GetOwner()->GetFlags() & FL_DUCKING )
			return (1.0 + (1.0 - weapon_combat_plasmarifle_ducking_mod.GetFloat()) );
	}

	return 1.0;
}

//-----------------------------------------------------------------------------
// Purpose: Charge up over time
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::ChargeThink( void )
{
	if ( !m_bCharging )
		return;

	if ( IsOwnerEMPed() )
	{
		m_flPower = 1;
	}
	else if ( m_iClip1 > 0 && m_flPower < MAX_RIFLE_POWER )
	{
		m_flPower = MIN( MAX_RIFLE_POWER, m_flPower + (((MAX_RIFLE_POWER-1.0) / RIFLE_CHARGE_TIME) * gpGlobals->frametime ) );
	}
}

#if defined( CLIENT_DLL )
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::OnDataChanged( DataUpdateType_t updateType )
{
	SetPredictionEligible( true );

	BaseClass::OnDataChanged( updateType );

	if (updateType == DATA_UPDATE_CREATED)
	{
		if ( GetTeamNumber() == 1 )
			m_hMaterial.Init( "sprites/chargeball_team1", TEXTURE_GROUP_CLIENT_EFFECTS );
		else 
			m_hMaterial.Init( "sprites/chargeball_team2", TEXTURE_GROUP_CLIENT_EFFECTS );
	}

	if (WeaponState() == WEAPON_IS_ACTIVE)
	{
		// Start thinking so we can manipulate the light
		SetNextClientThink( CLIENT_THINK_ALWAYS );
	}
	else
	{
		SetNextClientThink( CLIENT_THINK_NEVER );
	}
}


//-----------------------------------------------------------------------------
// Deal with dynamic lighting
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::ClientThink( )
{
	BaseClass::ClientThink();

	if (!inv_demo.GetInt())
	{
		C_BaseTFPlayer *pPlayer = (C_BaseTFPlayer *)GetOwner();
		if ( !pPlayer || (pPlayer->GetHealth() <= 0) || !IsDormant() )
		{
			SetNextClientThink( CLIENT_THINK_NEVER );
			return;
		}

		// FIXME: dl->origin should be based on the attachment point
		dlight_t *dl = effects->CL_AllocDlight( entindex() );
		dl->origin = GetRenderOrigin();
		if (GetTeamNumber() == 1)
		{
			dl->color.r = 40;
			dl->color.g = 60;
			dl->color.b = 250;
		}
		else
		{
			dl->color.r = 250;
			dl->color.g = 60;
			dl->color.b = 40;
		}
		dl->color.exponent = 7;
		dl->radius = 20 * m_flPower + 10;
		dl->die = gpGlobals->curtime + 0.01;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Draws the charging effect
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::DrawChargingEffect( float flSize, CBaseAnimating *pAttachedEnt )
{
	if (!pAttachedEnt)
		return;

	Vector vecMuzzleOrigin, vecBarrelOrigin;
	QAngle angMuzzleAngles, angBarrelAngles;
	int iMuzzle = pAttachedEnt->LookupAttachment( "muzzle" );
	//int iBarrel = pAttachedEnt->LookupAttachment( "barrel" );

	if ( pAttachedEnt->GetAttachment( iMuzzle, vecMuzzleOrigin, angMuzzleAngles ) )
	{
		// View model attachments are modified so you can place entities at the attachment
		// point and when they are rendered (with a different FOV than the view model itself uses)
		// they will render in the right spot when the view model is drawn.
		// 
		// In this case though, we are rendering at the same time as the view model, so we want
		// the attachment point before the correction has been applied.
		pAttachedEnt->UncorrectViewModelAttachment( vecMuzzleOrigin );

		// If I'm fully charged, put funky effects on the ball
		materials->Bind( m_hMaterial, this );
		
		if ( m_flPower >= MAX_RIFLE_POWER )
		{
			float frac = fmod( gpGlobals->curtime, 1.0 );
			frac *= 2 * M_PI;
			frac = sin( frac );
			flSize += (frac * 2) - 1.5;
			int colorFade = 190 + (int)( frac * 32.0f );

			color32 color = { 0, 0, 0, 255 };
			if ( GetTeamNumber() == 1 )
			{
				color.r = colorFade;
				color.g = colorFade;
			}
			else 
			{
				color.g = colorFade;
			}
			DrawSprite( vecMuzzleOrigin, flSize, flSize, color );
		}
		else
		{
			color32 color = { 255, 255, 255, 255 };
			DrawSprite( vecMuzzleOrigin, flSize, flSize, color );
		}
	}
}


//-----------------------------------------------------------------------------
// We're transparent because we draw a transparent charging effect
//-----------------------------------------------------------------------------
bool CWeaponCombatPlasmaRifle::IsTransparent( )
{
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Draws the model
//-----------------------------------------------------------------------------
int CWeaponCombatPlasmaRifle::DrawModel( int flags )
{
	int retval = BaseClass::DrawModel( flags );
	if (retval == 0)
		return 0;

	if (IsCarrierAlive())
	{
		// FIXME: Maybe do some client-side simulation on the size?
		// It may get jerky otherwise

		// Draw the charging effect
		float flSize = 10 * m_flPower + 5;

		DrawChargingEffect( flSize, this );
	}
	return retval;
}


//-----------------------------------------------------------------------------
// Purpose: Draws the model
//-----------------------------------------------------------------------------
void CWeaponCombatPlasmaRifle::ViewModelDrawn( CBaseViewModel *pBaseViewModel )
{
	// Draw the charging effect
	float flSize = 4 * m_flPower + 1;

	if ( m_iClip1 > 0 )
	{
		DrawChargingEffect( flSize, pBaseViewModel );
	}
}
#endif


IMPLEMENT_NETWORKCLASS_ALIASED( WeaponCombatPlasmaRifle, DT_WeaponCombatPlasmaRifle )

BEGIN_NETWORK_TABLE( CWeaponCombatPlasmaRifle, DT_WeaponCombatPlasmaRifle )
#if !defined( CLIENT_DLL )
	SendPropFloat( SENDINFO( m_flPower ), 14, SPROP_ROUNDUP, 1.0f, MAX_RIFLE_POWER ),
	SendPropInt( SENDINFO( m_bCharging ), 1, SPROP_UNSIGNED ),
#else
	RecvPropFloat( RECVINFO( m_flPower ) ),
	RecvPropInt( RECVINFO( m_bCharging ) ),
#endif
END_NETWORK_TABLE()

LINK_ENTITY_TO_CLASS( weapon_combat_plasmarifle_base, CWeaponCombatPlasmaRifle );

BEGIN_PREDICTION_DATA( CWeaponCombatPlasmaRifle  )

	DEFINE_PRED_FIELD_TOL( m_flPower, FIELD_FLOAT, FTYPEDESC_INSENDTABLE, 0.05f ),
	DEFINE_PRED_FIELD( m_bCharging, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),

END_PREDICTION_DATA()

#if !defined( CLIENT_DLL )

BEGIN_DATADESC( CWeaponCombatPlasmaRifle )

	// Function Pointers
	DEFINE_FUNCTION( ChargeThink ),

END_DATADESC()

// PRECACHE_WEAPON_REGISTER(weapon_combat_plasmarifle_base);
#endif

//-----------------------------------------------------------------------------
// Purpose: Need to do different art on client vs server
//-----------------------------------------------------------------------------
class CWeaponCombatPlasmaRifleHuman : public CWeaponCombatPlasmaRifle
{
	DECLARE_CLASS( CWeaponCombatPlasmaRifleHuman, CWeaponCombatPlasmaRifle );
public:
	DECLARE_NETWORKCLASS();
	DECLARE_PREDICTABLE();

	CWeaponCombatPlasmaRifleHuman( void ) {}

private:
	CWeaponCombatPlasmaRifleHuman( const CWeaponCombatPlasmaRifleHuman & );
};

//-----------------------------------------------------------------------------
// Purpose: Need to do different art on client vs server
//-----------------------------------------------------------------------------
class CWeaponCombatPlasmaRifleAlien : public CWeaponCombatPlasmaRifle
{
	DECLARE_CLASS( CWeaponCombatPlasmaRifleAlien, CWeaponCombatPlasmaRifle );
public:
	DECLARE_NETWORKCLASS();
	DECLARE_PREDICTABLE();

	CWeaponCombatPlasmaRifleAlien( void ) {}

private:
	CWeaponCombatPlasmaRifleAlien( const CWeaponCombatPlasmaRifleAlien & );
};

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponCombatPlasmaRifleHuman, DT_WeaponCombatPlasmaRifleHuman )

BEGIN_NETWORK_TABLE( CWeaponCombatPlasmaRifleHuman, DT_WeaponCombatPlasmaRifleHuman )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CWeaponCombatPlasmaRifleHuman )
END_PREDICTION_DATA()

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponCombatPlasmaRifleAlien, DT_WeaponCombatPlasmaRifleAlien )

BEGIN_NETWORK_TABLE( CWeaponCombatPlasmaRifleAlien, DT_WeaponCombatPlasmaRifleAlien )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CWeaponCombatPlasmaRifleAlien )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( weapon_combat_plasmarifle, CWeaponCombatPlasmaRifleHuman );
LINK_ENTITY_TO_CLASS( weapon_combat_plasmarifle_alien, CWeaponCombatPlasmaRifleAlien );

PRECACHE_WEAPON_REGISTER(weapon_combat_plasmarifle);
PRECACHE_WEAPON_REGISTER(weapon_combat_plasmarifle_alien);