aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/episodic/c_vort_charge_token.cpp
blob: fa769fa05503d64b94d679068fcbdcef74a37ec2 (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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=====================================================================================//

#include "cbase.h"
#include "particles_simple.h"
#include "citadel_effects_shared.h"
#include "particles_attractor.h"
#include "iefx.h"
#include "dlight.h"
#include "clienteffectprecachesystem.h"
#include "c_te_effect_dispatch.h"
#include "fx_quad.h"

#include "c_ai_basenpc.h"

// For material proxy
#include "proxyentity.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imaterialvar.h"

#define NUM_INTERIOR_PARTICLES	8

#define DLIGHT_RADIUS (150.0f)
#define DLIGHT_MINLIGHT (40.0f/255.0f)

class C_NPC_Vortigaunt : public C_AI_BaseNPC
{
	DECLARE_CLASS( C_NPC_Vortigaunt, C_AI_BaseNPC );
	DECLARE_CLIENTCLASS();

public:
	virtual void	OnDataChanged( DataUpdateType_t updateType );
	virtual void	ClientThink( void );
	virtual void	ReceiveMessage( int classID, bf_read &msg );

public:
	bool  m_bIsBlue;           ///< wants to fade to blue
	float m_flBlueEndFadeTime; ///< when to end fading from one skin to another

	bool  m_bIsBlack;    ///< wants to fade to black (networked)
	float m_flBlackFade; ///< [0.00 .. 1.00] where 1.00 is all black. Locally interpolated.
};

IMPLEMENT_CLIENTCLASS_DT( C_NPC_Vortigaunt, DT_NPC_Vortigaunt, CNPC_Vortigaunt )
	RecvPropTime( RECVINFO(m_flBlueEndFadeTime ) ),
	RecvPropBool( RECVINFO(m_bIsBlue) ),
	RecvPropBool( RECVINFO(m_bIsBlack) ),
END_RECV_TABLE()


#define	VORTIGAUNT_BLUE_FADE_TIME			2.25f		// takes this long to fade from green to blue or back
#define VORT_BLACK_FADE_TIME 2.2f	// time to interpolate up or down in fading to black


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : updateType - 
//-----------------------------------------------------------------------------
void C_NPC_Vortigaunt::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	// start thinking if we need to fade.
	if ( m_flBlackFade != (m_bIsBlack ? 1.0f : 0.0f) )
	{
		SetNextClientThink( CLIENT_THINK_ALWAYS );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_NPC_Vortigaunt::ClientThink( void )
{
	// Don't update if our frame hasn't moved forward (paused)
	if ( gpGlobals->frametime <= 0.0f )
		return;

	if ( m_bIsBlack )
	{
		// are we done?
		if ( m_flBlackFade >= 1.0f )
		{
			m_flBlackFade = 1.0f;
			SetNextClientThink( CLIENT_THINK_NEVER );
		}
		else // interpolate there
		{
			float lerpQuant = gpGlobals->frametime / VORT_BLACK_FADE_TIME;
			m_flBlackFade += lerpQuant;
			if ( m_flBlackFade > 1.0f )
			{
				m_flBlackFade = 1.0f;
			}
		}
	}
	else 
	{
		// are we done?
		if ( m_flBlackFade <= 0.0f )
		{
			m_flBlackFade = 0.0f;
			SetNextClientThink( CLIENT_THINK_NEVER );
		}
		else // interpolate there
		{
			float lerpQuant = gpGlobals->frametime / VORT_BLACK_FADE_TIME;
			m_flBlackFade -= lerpQuant;
			if ( m_flBlackFade < 0.0f )
			{
				m_flBlackFade = 0.0f;
			}
		}
	}
}

// FIXME: Move to shared code!
#define VORTFX_ZAPBEAM	0
#define VORTFX_ARMBEAM	1

//-----------------------------------------------------------------------------
// Purpose: Receive messages from the server
//-----------------------------------------------------------------------------
void C_NPC_Vortigaunt::ReceiveMessage( int classID, bf_read &msg )
{
	// Is the message for a sub-class?
	if ( classID != GetClientClass()->m_ClassID )
	{
		BaseClass::ReceiveMessage( classID, msg );
		return;
	}
	
	int messageType = msg.ReadByte();
	switch( messageType )
	{
	case VORTFX_ZAPBEAM:
		{
			// Find our attachment point
			unsigned char nAttachment = msg.ReadByte();
			
			// Get our attachment position
			Vector vecStart;
			QAngle vecAngles;
			GetAttachment( nAttachment, vecStart, vecAngles );

			// Get the final position we'll strike
			Vector vecEndPos;
			msg.ReadBitVec3Coord( vecEndPos );

			// Place a beam between the two points
			CNewParticleEffect *pEffect = ParticleProp()->Create( "vortigaunt_beam", PATTACH_POINT_FOLLOW, nAttachment );
			if ( pEffect )
			{
				pEffect->SetControlPoint( 0, vecStart );
				pEffect->SetControlPoint( 1, vecEndPos );
			}
		}
		break;

	case VORTFX_ARMBEAM:
		{
			int nIndex = msg.ReadLong();
			C_BaseEntity *pEnt = ClientEntityList().GetBaseEntityFromHandle( ClientEntityList().EntIndexToHandle( nIndex ) );

			if ( pEnt )
			{
				unsigned char nAttachment = msg.ReadByte();
				Vector vecEndPos;
				msg.ReadBitVec3Coord( vecEndPos );
				
				Vector vecNormal;
				msg.ReadBitVec3Normal( vecNormal );
				
				CNewParticleEffect *pEffect = pEnt->ParticleProp()->Create( "vortigaunt_beam_charge", PATTACH_POINT_FOLLOW, nAttachment );
				if ( pEffect )
				{
					// Set the control point's angles to be the surface normal we struct
					Vector vecRight, vecUp;
					VectorVectors( vecNormal, vecRight, vecUp );
					pEffect->SetControlPointOrientation( 1, vecNormal, vecRight, vecUp );
					pEffect->SetControlPoint( 1, vecEndPos );
				}
			}
		}
		break;
	default:
		AssertMsg1( false, "Received unknown message %d", messageType);
	}
}

class C_VortigauntChargeToken : public C_BaseEntity
{
	DECLARE_CLASS( C_VortigauntChargeToken, C_BaseEntity );
	DECLARE_CLIENTCLASS();

public:
	virtual void	UpdateOnRemove( void );
	virtual void	ClientThink( void );
	virtual void	NotifyShouldTransmit( ShouldTransmitState_t state );
	virtual void	OnDataChanged( DataUpdateType_t type );

	// For RecvProxy handlers
	float							m_flFadeOutTime;
	float							m_flFadeOutStart;

private:
	bool		SetupEmitters( void );

	bool							m_bFadeOut;
	CNewParticleEffect				*m_hEffect;
	dlight_t						*m_pDLight;
};

void RecvProxy_FadeOutDuration( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
	C_VortigauntChargeToken *pVortToken = (C_VortigauntChargeToken *) pStruct;
	Assert( pOut == &pVortToken->m_flFadeOutTime );

	pVortToken->m_flFadeOutStart = gpGlobals->curtime;
	pVortToken->m_flFadeOutTime = ( pData->m_Value.m_Float - gpGlobals->curtime );
}

IMPLEMENT_CLIENTCLASS_DT( C_VortigauntChargeToken, DT_VortigauntChargeToken, CVortigauntChargeToken )
	RecvPropBool( RECVINFO( m_bFadeOut ) ),
END_RECV_TABLE()

void C_VortigauntChargeToken::UpdateOnRemove( void )
{
	if ( m_hEffect )
	{
		m_hEffect->StopEmission();
		m_hEffect = NULL;
	}

	if ( m_pDLight != NULL )
	{
		m_pDLight->die = gpGlobals->curtime;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Change our transmission state 
//-----------------------------------------------------------------------------
void C_VortigauntChargeToken::NotifyShouldTransmit( ShouldTransmitState_t state )
{
	BaseClass::NotifyShouldTransmit( state );

	// Turn off
	if ( state == SHOULDTRANSMIT_END )
	{
		if ( m_hEffect )
		{
			m_hEffect->StopEmission();
			m_hEffect = NULL;
		}
	}

	// Turn on
	if ( state == SHOULDTRANSMIT_START )
	{
		m_hEffect = ParticleProp()->Create( "vortigaunt_charge_token", PATTACH_ABSORIGIN_FOLLOW );
		m_hEffect->SetControlPointEntity( 0, this );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_VortigauntChargeToken::OnDataChanged( DataUpdateType_t type )
{
	if ( m_bFadeOut )
	{
		if ( m_hEffect )
		{
			m_hEffect->StopEmission();
			m_hEffect = NULL;
		}
	}

	BaseClass::OnDataChanged( type );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_VortigauntChargeToken::ClientThink( void )
{
	//
	// -- DLight 
	//

	if ( m_pDLight != NULL )
	{
		m_pDLight->origin = GetAbsOrigin();
		m_pDLight->radius = DLIGHT_RADIUS;
	}
}

//=============================================================================
// 
//  Dispel Effect
//	
//=============================================================================

class C_VortigauntEffectDispel : public C_BaseEntity
{
	DECLARE_CLASS( C_VortigauntEffectDispel, C_BaseEntity );
	DECLARE_CLIENTCLASS();

public:
	virtual void	UpdateOnRemove( void );
	virtual void	ClientThink( void );
	virtual void	NotifyShouldTransmit( ShouldTransmitState_t state );
	virtual void	OnDataChanged( DataUpdateType_t type );

	// For RecvProxy handlers
	float							m_flFadeOutTime;
	float							m_flFadeOutStart;

private:
	bool	SetupEmitters( void );

	CNewParticleEffect				*m_hEffect;
	bool							m_bFadeOut;
	dlight_t						*m_pDLight;
};

void RecvProxy_DispelFadeOutDuration( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
	C_VortigauntEffectDispel *pVortToken = (C_VortigauntEffectDispel *) pStruct;
	Assert( pOut == &pVortToken->m_flFadeOutTime );

	pVortToken->m_flFadeOutStart = gpGlobals->curtime;
	pVortToken->m_flFadeOutTime = ( pData->m_Value.m_Float - gpGlobals->curtime );
}

IMPLEMENT_CLIENTCLASS_DT( C_VortigauntEffectDispel, DT_VortigauntEffectDispel, CVortigauntEffectDispel )
	RecvPropBool( RECVINFO( m_bFadeOut ) ),
END_RECV_TABLE()

void C_VortigauntEffectDispel::UpdateOnRemove( void )
{
	if ( m_hEffect )
	{
		m_hEffect->StopEmission();
		m_hEffect = NULL;
	}

	if ( m_pDLight != NULL )
	{
		m_pDLight->die = gpGlobals->curtime;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_VortigauntEffectDispel::OnDataChanged( DataUpdateType_t type )
{
	if ( m_bFadeOut )
	{
		if ( m_hEffect )
		{
			m_hEffect->StopEmission();
			m_hEffect = NULL;
		}
	}

	BaseClass::OnDataChanged( type );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_VortigauntEffectDispel::NotifyShouldTransmit( ShouldTransmitState_t state )
{
	BaseClass::NotifyShouldTransmit( state );

	// Turn off
	if ( state == SHOULDTRANSMIT_END )
	{
		if ( m_hEffect )
		{
			m_hEffect->StopEmission();
			m_hEffect = NULL;
		}
	}

	// Turn on
	if ( state == SHOULDTRANSMIT_START )
	{
		m_hEffect = ParticleProp()->Create( "vortigaunt_hand_glow", PATTACH_ABSORIGIN_FOLLOW );
		m_hEffect->SetControlPointEntity( 0, this );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Create our emitter
//-----------------------------------------------------------------------------
bool C_VortigauntEffectDispel::SetupEmitters( void )
{
	m_pDLight = NULL;

#ifndef _X360
	m_pDLight = effects->CL_AllocDlight ( index );
	m_pDLight->origin = GetAbsOrigin();
	m_pDLight->color.r = 64;
	m_pDLight->color.g = 255;
	m_pDLight->color.b = 64;
	m_pDLight->radius = 0;
	m_pDLight->minlight = DLIGHT_MINLIGHT;
	m_pDLight->die = FLT_MAX;
#endif // _X360

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_VortigauntEffectDispel::ClientThink( void )
{
	if ( m_pDLight != NULL )
	{
		m_pDLight->origin = GetAbsOrigin();
		m_pDLight->radius = DLIGHT_RADIUS;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void DispelCallback( const CEffectData &data )
{
	// Kaboom!
	Vector startPos = data.m_vOrigin + Vector(0,0,16);
	Vector endPos = data.m_vOrigin + Vector(0,0,-64);

	trace_t tr;
	UTIL_TraceLine( startPos, endPos, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, &tr );

	if ( tr.fraction < 1.0f )
	{
		//Add a ripple quad to the surface
		FX_AddQuad( tr.endpos + ( tr.plane.normal * 8.0f ), 
			Vector( 0, 0, 1 ), 
			64.0f, 
			600.0f, 
			0.8f,
			1.0f,	// start alpha
			0.0f,	// end alpha
			0.3f,
			random->RandomFloat( 0, 360 ),
			0.0f,
			Vector( 0.5f, 1.0f, 0.5f ), 
			0.75f, 
			"effects/ar2_altfire1b", 
			(FXQUAD_BIAS_SCALE|FXQUAD_BIAS_ALPHA|FXQUAD_COLOR_FADE) );
		
		//Add a ripple quad to the surface
		FX_AddQuad( tr.endpos + ( tr.plane.normal * 8.0f ), 
			Vector( 0, 0, 1 ), 
			16.0f, 
			300.0f,
			0.9f,
			1.0f,	// start alpha
			0.0f,	// end alpha
			0.9f,
			random->RandomFloat( 0, 360 ),
			0.0f,
			Vector( 0.5f, 1.0f, 0.5f ), 
			1.25f, 
			"effects/rollerglow", 
			(FXQUAD_BIAS_SCALE|FXQUAD_BIAS_ALPHA) );
	}
}

DECLARE_CLIENT_EFFECT( "VortDispel", DispelCallback );

//-----------------------------------------------------------------------------
// Purpose: Used for emissive lightning layer on vort
//-----------------------------------------------------------------------------
class CVortEmissiveProxy : public CEntityMaterialProxy
{
public:
	CVortEmissiveProxy( void );
	virtual				~CVortEmissiveProxy( void );
	virtual bool		Init( IMaterial *pMaterial, KeyValues* pKeyValues );
	virtual void		OnBind( C_BaseEntity *pC_BaseEntity );
	virtual IMaterial *	GetMaterial();

private:

	IMaterialVar *m_pMatEmissiveStrength;
	IMaterialVar *m_pMatDetailBlendStrength;
};

//-----------------------------------------------------------------------------
CVortEmissiveProxy::CVortEmissiveProxy( void )
{
	m_pMatEmissiveStrength = NULL;
	m_pMatDetailBlendStrength = NULL;
}

CVortEmissiveProxy::~CVortEmissiveProxy( void )
{
	// Do nothing
}

//-----------------------------------------------------------------------------
bool CVortEmissiveProxy::Init( IMaterial *pMaterial, KeyValues* pKeyValues )
{
	Assert( pMaterial );

	// Need to get the material var
	bool bFound;
	m_pMatEmissiveStrength = pMaterial->FindVar( "$emissiveblendstrength", &bFound );

	if ( bFound )
	{
		// Optional
		bool bFound2;
		m_pMatDetailBlendStrength = pMaterial->FindVar( "$detailblendfactor", &bFound2 );
	}

	return bFound;
}

//-----------------------------------------------------------------------------
void CVortEmissiveProxy::OnBind( C_BaseEntity *pEnt )
{
	C_NPC_Vortigaunt *pVort = dynamic_cast<C_NPC_Vortigaunt *>(pEnt);

	float flBlendValue;

	if (pVort)
	{
		// do we need to crossfade?
		if (gpGlobals->curtime < pVort->m_flBlueEndFadeTime)
		{
			// will be 0 when fully faded and 1 when not faded at all:
			float fadeRatio = (pVort->m_flBlueEndFadeTime - gpGlobals->curtime) / VORTIGAUNT_BLUE_FADE_TIME;
			if (pVort->m_bIsBlue)
			{
				fadeRatio = 1.0f - fadeRatio;
			}
			flBlendValue = clamp( fadeRatio, 0.0f, 1.0f );
		}
		else // no crossfade
		{
			flBlendValue = pVort->m_bIsBlue ? 1.0f : 0.0f;
		}

		// ALEX VLACHOS: 
		// The following variable varies on [0 .. 1]. 0.0 means the vort wants to be his normal
		// color. 1.0 means he wants to be all black. It is interpolated in the
		// C_NPC_Vortigaunt::ClientThink() function. 
		// 
		// pVort->m_flBlackFade
	}
	else
	{	// if you bind this proxy to anything non-vort (eg a ragdoll) it's always green
		flBlendValue = 0.0f;
	}


	/*
	// !!! Change me !!! I'm using a clamped sin wave for debugging
	float flBlendValue = sinf( gpGlobals->curtime * 4.0f ) * 0.75f + 0.25f;

	// Clamp 0-1
	flBlendValue = ( flBlendValue < 0.0f ) ? 0.0f : ( flBlendValue > 1.0f ) ? 1.0f : flBlendValue;
	*/

	if( m_pMatEmissiveStrength != NULL )
	{
		m_pMatEmissiveStrength->SetFloatValue( flBlendValue );
	}

	if( m_pMatDetailBlendStrength != NULL )
	{
		m_pMatDetailBlendStrength->SetFloatValue( flBlendValue );
	}
}

//-----------------------------------------------------------------------------
IMaterial *CVortEmissiveProxy::GetMaterial()
{
	if ( m_pMatEmissiveStrength != NULL )
		return m_pMatEmissiveStrength->GetOwningMaterial();
	else if ( m_pMatDetailBlendStrength != NULL )
		return m_pMatDetailBlendStrength->GetOwningMaterial();
	else
		return NULL;
}

EXPOSE_INTERFACE( CVortEmissiveProxy, IMaterialProxy, "VortEmissive" IMATERIAL_PROXY_INTERFACE_VERSION );