aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/c_rumble.cpp
blob: a22ef88429d0a80d1b3fe349c48eb2aab3d5266e (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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Rumble effects mixer for XBox
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "c_rumble.h"
#include "rumble_shared.h"
#include "inputsystem/iinputsystem.h"

ConVar cl_rumblescale( "cl_rumblescale", "1.0", FCVAR_ARCHIVE | FCVAR_ARCHIVE_XBOX, "Scale sensitivity of rumble effects (0 to 1.0)" ); 
ConVar cl_debugrumble( "cl_debugrumble", "0", FCVAR_ARCHIVE, "Turn on rumble debugging spew" );

#define MAX_RUMBLE_CHANNELS 3	// Max concurrent rumble effects

#define NUM_WAVE_SAMPLES 30		// Effects play at 10hz

typedef struct
{
	float			amplitude_left[NUM_WAVE_SAMPLES];
	float			amplitude_right[NUM_WAVE_SAMPLES];
	int				numSamples;
} RumbleWaveform_t;

//=========================================================
// Structure for a rumble effect channel. This is akin to
// a sound channel that is playing a sound.
//=========================================================
typedef struct
{
	float			starttime;			// When did this effect start playing? (gpGlobals->curtime)
	int				waveformIndex;		// Type of effect waveform used (an enum from rumble_shared.h)
	int				priority;			// How important this effect is (for making replacement decisions)
	bool			in_use;				// Is this channel in use?? (true if effect is currently playing, false if done or otherwise available)
	unsigned char	rumbleFlags;		// Flags pertaining to the effect currently playing on this channel.
	float			scale;				// Some effects are updated while they are running.
} RumbleChannel_t;

//=========================================================
// This structure contains parameters necessary to generate
// a sine or sawtooth waveform. 
//=========================================================
typedef struct tagWaveGenParams
{
	float	cycles;				// AKA frequency
	float	amplitudescale;	
	bool	leftChannel;		// If false, generating for the right channel

	float	maxAmplitude;		// Clamping
	float	minAmplitude;

	void Set( float c_cycles, float c_amplitudescale, bool c_leftChannel, float c_minAmplitude, float c_maxAmplitude )
	{
		cycles = c_cycles;
		amplitudescale = c_amplitudescale;
		leftChannel = c_leftChannel;
		minAmplitude = c_minAmplitude;
		maxAmplitude = c_maxAmplitude;
	}

	// CTOR
	tagWaveGenParams( float c_cycles, float c_amplitudescale, bool c_leftChannel, float c_minAmplitude, float c_maxAmplitude ) 
	{
		Set( c_cycles, c_amplitudescale, c_leftChannel, c_minAmplitude, c_maxAmplitude );
	}

} WaveGenParams_t;

//---------------------------------------------------------
//---------------------------------------------------------
void TerminateWaveform( RumbleWaveform_t *pWaveform, int samples )
{
	if( samples <= NUM_WAVE_SAMPLES )
	{
		pWaveform->numSamples = samples;
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void EaseInWaveform( RumbleWaveform_t *pWaveform, int samples, bool left )
{
	float step = 1.0f / ((float)samples);
	float factor = 0.0f;

	for( int i = 0 ; i < samples ; i++ )
	{
		if( left )
		{
			pWaveform->amplitude_left[i] *= factor;
		}
		else
		{
			pWaveform->amplitude_right[i] *= factor;
		}

		factor += step;
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void EaseOutWaveform( RumbleWaveform_t *pWaveform, int samples, bool left )
{
	float step = 1.0f / ((float)samples);
	float factor = 0.0f;

	int i = NUM_WAVE_SAMPLES - 1;

	for( int j = 0 ; j < samples ; j++ )
	{
		if( left )
		{
			pWaveform->amplitude_left[i] *= factor;
		}
		else
		{
			pWaveform->amplitude_right[i] *= factor;
		}

		factor += step;
		i--;
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void GenerateSawtoothEffect( RumbleWaveform_t *pWaveform, const WaveGenParams_t &params )
{
	float delta = params.maxAmplitude - params.minAmplitude;
	int waveLength = NUM_WAVE_SAMPLES / params.cycles;
	float vstep = (delta / waveLength);

	float amplitude = params.minAmplitude;

	for( int i = 0 ; i < NUM_WAVE_SAMPLES ; i++ )
	{
		if( params.leftChannel )
		{
			pWaveform->amplitude_left[i] = amplitude;
		}
		else
		{
			pWaveform->amplitude_right[i] = amplitude;
		}

		amplitude += vstep;

		if( amplitude > params.maxAmplitude )
		{
			amplitude = params.minAmplitude;
		}
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void GenerateSquareWaveEffect( RumbleWaveform_t *pWaveform, const WaveGenParams_t &params )
{
	int i = 0;
	int j;

	int steps = ((float)NUM_WAVE_SAMPLES) / (params.cycles*2.0f);

	while( i < NUM_WAVE_SAMPLES )
	{
		for( j = 0 ; j < steps ; j++ )
		{
			if( params.leftChannel )
			{
				pWaveform->amplitude_left[i++] = params.minAmplitude;
			}
			else
			{
				pWaveform->amplitude_right[i++] = params.minAmplitude;
			}
		}
		for( j = 0 ; j < steps ; j++ )
		{
			if( params.leftChannel )
			{
				pWaveform->amplitude_left[i++] = params.maxAmplitude;
			}
			else
			{
				pWaveform->amplitude_right[i++] = params.maxAmplitude;
			}
		}
	}
}

//---------------------------------------------------------
// If you pass a numSamples, this wave will only be that many
// samples long.
//---------------------------------------------------------
void GenerateFlatEffect( RumbleWaveform_t *pWaveform, const WaveGenParams_t &params )
{
	for( int i = 0 ; i < NUM_WAVE_SAMPLES ; i++ )
	{
		if( params.leftChannel )
		{
			pWaveform->amplitude_left[i] = params.maxAmplitude;
		}
		else
		{
			pWaveform->amplitude_right[i] = params.maxAmplitude;
		}
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void GenerateSineWaveEffect( RumbleWaveform_t *pWaveform, const WaveGenParams_t &params )
{
	float step = (360.0f * (params.cycles * 0.5f) ) / ((float)NUM_WAVE_SAMPLES);
	float degrees = 180.0f + step; // 180 to start at 0

	for( int i = 0 ; i < NUM_WAVE_SAMPLES ; i++ )
	{
		float radians = DEG2RAD(degrees);
		float value = fabs( sin(radians) );

		value *= params.amplitudescale;

		if( value < params.minAmplitude )
			value = params.minAmplitude;

		if( value > params.maxAmplitude )
			value = params.maxAmplitude;

		if( params.leftChannel )
		{
			pWaveform->amplitude_left[i] = value;
		}
		else
		{
			pWaveform->amplitude_right[i] = value;
		}

		degrees += step;
	}
}

//=========================================================
//=========================================================
class CRumbleEffects
{
public:
	CRumbleEffects()
	{
		Init();
	}

	void Init();
	void SetOutputEnabled( bool bEnable );
	void StartEffect( unsigned char effectIndex, unsigned char rumbleData, unsigned char rumbleFlags );
	void StopEffect( int effectIndex );
	void StopAllEffects();
	void ComputeAmplitudes( RumbleChannel_t *pChannel, float curtime, float *pLeft, float *pRight );
	void UpdateEffects( float curtime );
	void UpdateScreenShakeRumble( float shake, float balance );

	RumbleChannel_t *FindExistingChannel( int index );
	RumbleChannel_t	*FindAvailableChannel( int priority );

public:
	RumbleChannel_t	m_Channels[ MAX_RUMBLE_CHANNELS ];

	RumbleWaveform_t m_Waveforms[ NUM_RUMBLE_EFFECTS ];

	float	m_flScreenShake;
	bool	m_bOutputEnabled;
};


CRumbleEffects g_RumbleEffects;

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::Init()
{
	SetOutputEnabled( true );
	
	int i;

	for( i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		m_Channels[i].in_use = false;
		m_Channels[i].priority = 0;
	}

	// Every effect defaults to this many samples. Call TerminateWaveform() to trim these.
	for ( i = 0 ; i < NUM_RUMBLE_EFFECTS ; i++ )
	{
		m_Waveforms[i].numSamples = NUM_WAVE_SAMPLES;
	}

	// Jeep Idle
	WaveGenParams_t params( 1, 1.0f, false, 0.0f, 0.15f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_JEEP_ENGINE_LOOP], params );

	// Pistol
	params.Set( 1, 1.0f, false, 0.0f, 0.5f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_PISTOL], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_PISTOL], 3 );

	// SMG1
	params.Set( 1, 1.0f, true, 0.0f, 0.2f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SMG1], params );
	params.Set( 1, 1.0f, false, 0.0f, 0.4f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SMG1], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_SMG1], 3 );

	// AR2
	params.Set( 1, 1.0f, true, 0.0f, 0.5f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_AR2], params );
	params.Set( 1, 1.0f, false, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_AR2], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_AR2], 3 );

	// AR2 Alt
	params.Set( 1, 1.0f, true, 0.0, 0.5f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_AR2_ALT_FIRE], params );
	EaseInWaveform( &m_Waveforms[RUMBLE_AR2_ALT_FIRE], 5, true );
	params.Set( 1, 1.0f, false, 0.0, 0.7f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_AR2_ALT_FIRE], params );
	EaseInWaveform( &m_Waveforms[RUMBLE_AR2_ALT_FIRE], 5, false );
	TerminateWaveform( &m_Waveforms[RUMBLE_AR2_ALT_FIRE], 7 );

	// 357
	params.Set( 1, 1.0f, true, 0.0f, 0.75f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_357], params );
	params.Set( 1, 1.0f, false, 0.0f, 0.75f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_357], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_357], 2 );

	// Shotgun
	params.Set( 1, 1.0f, true, 0.0f, 0.8f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SHOTGUN_SINGLE], params );
	params.Set( 1, 1.0f, false, 0.0f, 0.8f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SHOTGUN_SINGLE], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_SHOTGUN_SINGLE], 3 );

	params.Set( 1, 1.0f, true, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SHOTGUN_DOUBLE], params );
	params.Set( 1, 1.0f, false, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_SHOTGUN_DOUBLE], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_SHOTGUN_DOUBLE], 3 );

	// RPG Missile
	params.Set( 1, 1.0f, false, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_RPG_MISSILE], params );
	EaseOutWaveform( &m_Waveforms[RUMBLE_RPG_MISSILE], 30, false );
	TerminateWaveform( &m_Waveforms[RUMBLE_RPG_MISSILE], 6 );

	// Physcannon open forks
	params.Set( 1, 1.0f, false, 0.0f, 0.25f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_PHYSCANNON_OPEN], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_PHYSCANNON_OPEN], 4 );

	// Physcannon holding something
	params.Set( 1, 1.0f, true, 0.0f, 0.2f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_PHYSCANNON_LOW], params );
	params.Set( 6, 1.0f, false, 0.0f, 0.25f );
	GenerateSquareWaveEffect( &m_Waveforms[RUMBLE_PHYSCANNON_LOW], params );

	// Crowbar
	params.Set( 1, 1.0f, false, 0.0f, 0.35f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_CROWBAR_SWING], params );
	EaseOutWaveform( &m_Waveforms[RUMBLE_CROWBAR_SWING], 30, false );
	TerminateWaveform( &m_Waveforms[RUMBLE_CROWBAR_SWING], 4 );

	// Airboat gun
	params.Set( 1, 1.0f, false, 0.0f, 0.4f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_AIRBOAT_GUN], params );
	params.Set( 12, 1.0f, true, 0.0f, 0.5f );
	GenerateSawtoothEffect( &m_Waveforms[RUMBLE_AIRBOAT_GUN], params );

	// Generic flat effects.
	params.Set( 1, 1.0f, true, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FLAT_LEFT], params );

	params.Set( 1, 1.0f, false, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FLAT_RIGHT], params );
	
	params.Set( 1, 1.0f, true, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FLAT_BOTH], params );
	params.Set( 1, 1.0f, false, 0.0f, 1.0f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FLAT_BOTH], params );

	// Impact from a long fall
	params.Set( 1, 1.0f, false, 0.0f, 0.5f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FALL_LONG], params );
	params.Set( 1, 1.0f, true, 0.0f, 0.5f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FALL_LONG], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_FALL_LONG], 3 );

	// Impact from a short fall
	params.Set( 1, 1.0f, false, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FALL_SHORT], params );
	params.Set( 1, 1.0f, true, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_FALL_SHORT], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_FALL_SHORT], 2 );

	// Portalgun left (blue) shot
	params.Set( 1, 1.0f, true, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_PORTALGUN_LEFT], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_PORTALGUN_LEFT], 2 );

	// Portalgun right (red) shot
	params.Set( 1, 1.0f, false, 0.0f, 0.3f );
	GenerateFlatEffect( &m_Waveforms[RUMBLE_PORTALGUN_RIGHT], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_PORTALGUN_RIGHT], 2 );

	// Portal failed to place feedback
	params.Set( 12, 1.0f, true, 0.0f, 1.0f );
	GenerateSquareWaveEffect( &m_Waveforms[RUMBLE_PORTAL_PLACEMENT_FAILURE], params );
	params.Set( 12, 1.0f, false, 0.0f, 1.0f );
	GenerateSquareWaveEffect( &m_Waveforms[RUMBLE_PORTAL_PLACEMENT_FAILURE], params );
	TerminateWaveform( &m_Waveforms[RUMBLE_PORTAL_PLACEMENT_FAILURE], 6 );
}

//---------------------------------------------------------
//---------------------------------------------------------
RumbleChannel_t *CRumbleEffects::FindExistingChannel( int index )
{
	RumbleChannel_t *pChannel;

	for( int i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		pChannel = &m_Channels[i];

		if( pChannel->in_use && pChannel->waveformIndex == index )
		{
			// This effect is already playing. Provide this channel for the 
			// effect to be re-started on.
			return pChannel;
		}
	}

	return NULL;
}

//---------------------------------------------------------
// priority - the priority of the effect we want to play.
//---------------------------------------------------------
RumbleChannel_t	*CRumbleEffects::FindAvailableChannel( int priority )
{
	RumbleChannel_t *pChannel;
	int i;

	for( i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		pChannel = &m_Channels[i];

		if( !pChannel->in_use )
		{
			return pChannel;
		}
	}

	int lowestPriority = priority;
	RumbleChannel_t	*pBestChannel = NULL;
	float oldestChannel = FLT_MAX;

	// All channels already in use. Find a channel to slam.
	for( i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		pChannel = &m_Channels[i];

		if( (pChannel->rumbleFlags & RUMBLE_FLAG_LOOP) )
			continue;

		if( pChannel->priority < lowestPriority )
		{
			// Always happily slam a lower priority sound.
			pBestChannel = pChannel;
			lowestPriority = pChannel->priority;
		}
		else if( pChannel->priority == lowestPriority )
		{
			// Priority is the same, so replace the oldest.
			if( pBestChannel )
			{
				// If we already have a channel of the same priority to discard, make sure we discard the oldest.
				float age = gpGlobals->curtime - pChannel->starttime;

				if( age > oldestChannel )
				{
					pBestChannel = pChannel;
					oldestChannel = age;
				}
			}
			else
			{
				// Take it.
				pBestChannel = pChannel;
				oldestChannel = gpGlobals->curtime - pChannel->starttime;
			}
		}
	}

	return pBestChannel; // Can still be NULL if we couldn't find a channel to slam.
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::SetOutputEnabled( bool bEnable )
{
	m_bOutputEnabled = bEnable;

	if( !bEnable )
	{
		// Tell the hardware to shut down motors right now, in case this gets called
		// and some other process blocks us before the next rumble system update.
		m_flScreenShake = 0.0f;

		inputsystem->StopRumble();
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::StartEffect( unsigned char effectIndex, unsigned char rumbleData, unsigned char rumbleFlags )
{
	if( effectIndex == RUMBLE_STOP_ALL )
	{
		StopAllEffects();
		return;
	}

	if( rumbleFlags & RUMBLE_FLAG_STOP )
	{
		StopEffect( effectIndex );
		return;
	}

	int priority = 1;
	RumbleChannel_t *pChannel = NULL;

	if( (rumbleFlags & RUMBLE_FLAG_RESTART) )
	{
		// Try to find any active instance of this effect and replace it.
		pChannel = FindExistingChannel( effectIndex );
	}

	if( (rumbleFlags & RUMBLE_FLAG_ONLYONE) )
	{
		pChannel = FindExistingChannel( effectIndex );

		if( pChannel )
		{
			// Bail out. An instance of this effect is already playing.
			return;
		}
	}

	if( (rumbleFlags & RUMBLE_FLAG_UPDATE_SCALE) )
	{
		pChannel = FindExistingChannel( effectIndex );
		if( pChannel )
		{
			pChannel->scale = ((float)rumbleData) / 100.0f;
		}

		// It's possible to return without finding a rumble to update.
		// This means you tried to update a rumble you never started.
		return;
	}

	if( !pChannel )
	{
		pChannel = FindAvailableChannel( priority );
	}

	if( pChannel )
	{
		pChannel->waveformIndex = effectIndex;
		pChannel->priority = 1;
		pChannel->starttime = gpGlobals->curtime;
		pChannel->in_use = true;
		pChannel->rumbleFlags = rumbleFlags;

		if( rumbleFlags & RUMBLE_FLAG_INITIAL_SCALE )
		{
			pChannel->scale = ((float)rumbleData) / 100.0f;
		}
		else
		{
			pChannel->scale = 1.0f;
		}
	}

	if( (rumbleFlags & RUMBLE_FLAG_RANDOM_AMPLITUDE) )
	{
		pChannel->scale = random->RandomFloat( 0.1f, 1.0f );
	}
}

//---------------------------------------------------------
// Find all playing effects of this type and stop them.
//---------------------------------------------------------
void CRumbleEffects::StopEffect( int effectIndex )
{
	for( int i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		if( m_Channels[i].in_use && m_Channels[i].waveformIndex == effectIndex )
		{
			m_Channels[i].in_use = false;
		}
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::StopAllEffects()
{
	for( int i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		m_Channels[i].in_use = false;
	}

	m_flScreenShake = 0.0f;
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::ComputeAmplitudes( RumbleChannel_t *pChannel, float curtime, float *pLeft, float *pRight )
{
	// How long has this waveform been playing?
	float elapsed = curtime - pChannel->starttime;
	
	if( elapsed >= (NUM_WAVE_SAMPLES/10) )
	{
		if( (pChannel->rumbleFlags & RUMBLE_FLAG_LOOP) )
		{
			// This effect loops. Just fixup the start time and recompute elapsed.
			pChannel->starttime = curtime;
			elapsed = curtime - pChannel->starttime;
		}
		else
		{
			// This effect is done! Should it loop?
			*pLeft = 0;
			*pRight = 0;
			pChannel->in_use = false;
			return;
		}
	}

	// Figure out which sample we're playing FROM. 
	int seconds = ((int) elapsed);
	int sample = (int)(elapsed*10.0f);

	// Get the fraction bit.
	float fraction = elapsed - seconds;

	float left, right;

	if( sample == m_Waveforms[pChannel->waveformIndex].numSamples )
	{
		// This effect is done. Send zeroes to the mixer for this
		// final frame and then turn the channel off. (Unless it loops!)

		if( (pChannel->rumbleFlags & RUMBLE_FLAG_LOOP) )
		{
			// Loop this effect
			pChannel->starttime = gpGlobals->curtime;

			// Send the first sample.
			left = m_Waveforms[pChannel->waveformIndex].amplitude_left[0];
			right = m_Waveforms[pChannel->waveformIndex].amplitude_right[0];
		}
		else
		{
			left = 0.0f;
			right = 0.0f;
			pChannel->in_use = false;
		}
	}
	else
	{
		// Use values for the last sample that we have passed
		left = m_Waveforms[pChannel->waveformIndex].amplitude_left[sample];
		right = m_Waveforms[pChannel->waveformIndex].amplitude_right[sample];
	}

	left *= pChannel->scale;
	right *= pChannel->scale;

	if( cl_debugrumble.GetBool() )
	{
		Msg("Seconds:%d Fraction:%f Sample:%d  L:%f R:%f\n", seconds, fraction, sample, left, right );
	}

	if( !m_bOutputEnabled )
	{
		// Send zeroes to stop any current rumbling, and to keep it silenced.
		left = 0;
		right = 0;
	}

	*pLeft = left;
	*pRight = right;
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::UpdateScreenShakeRumble( float shake, float balance )
{
	if( m_bOutputEnabled )
	{
		m_flScreenShake = shake;
	}
	else
	{
		// Silence
		m_flScreenShake = 0.0f;
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void CRumbleEffects::UpdateEffects( float curtime )
{
	float fLeftMotor = 0.0f;
	float fRightMotor = 0.0f;

	for( int i = 0 ; i < MAX_RUMBLE_CHANNELS ; i++ )
	{
		// Expire old channels
		RumbleChannel_t *pChannel = & m_Channels[i];

		if( pChannel->in_use )
		{
			float left, right;

			ComputeAmplitudes( pChannel, curtime, &left, &right );
			
			fLeftMotor += left;
			fRightMotor += right;
		}
	}

	// Add in any screenshake
	float shakeLeft = 0.0f;
	float shakeRight = 0.0f;
	if( m_flScreenShake != 0.0f )
	{
		if( m_flScreenShake < 0.0f )
		{
			shakeLeft = fabs( m_flScreenShake );
		}
		else
		{
			shakeRight = m_flScreenShake;
		}
	}

	fLeftMotor += shakeLeft;
	fRightMotor += shakeRight;

	fLeftMotor *= cl_rumblescale.GetFloat();
	fRightMotor *= cl_rumblescale.GetFloat();

	if( engine->IsPaused() )
	{
		// Send nothing when paused.
		fLeftMotor = 0.0f;
		fRightMotor = 0.0f;
	}

	inputsystem->SetRumble( fLeftMotor, fRightMotor );
}

//---------------------------------------------------------
//---------------------------------------------------------
void StopAllRumbleEffects( void )
{
	g_RumbleEffects.StopAllEffects();

	inputsystem->StopRumble();
}

//---------------------------------------------------------
//---------------------------------------------------------
void RumbleEffect( unsigned char effectIndex, unsigned char rumbleData, unsigned char rumbleFlags )
{
	g_RumbleEffects.StartEffect( effectIndex, rumbleData, rumbleFlags );	
}

//---------------------------------------------------------
//---------------------------------------------------------
void UpdateRumbleEffects()
{
	C_BasePlayer *localPlayer = C_BasePlayer::GetLocalPlayer();
	if( !localPlayer || !localPlayer->IsAlive() )
	{
		StopAllRumbleEffects();
		return;
	}

	g_RumbleEffects.UpdateEffects( gpGlobals->curtime );
}

//---------------------------------------------------------
//---------------------------------------------------------
void UpdateScreenShakeRumble( float shake, float balance )
{
	C_BasePlayer *localPlayer = C_BasePlayer::GetLocalPlayer();
	if( !localPlayer || !localPlayer->IsAlive() )
	{
		return;
	}

	g_RumbleEffects.UpdateScreenShakeRumble( shake, balance );
}

//---------------------------------------------------------
//---------------------------------------------------------
void EnableRumbleOutput( bool bEnable )
{
	g_RumbleEffects.SetOutputEnabled( bEnable );
}