-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathsound.cpp
1970 lines (1549 loc) · 48.7 KB
/
sound.cpp
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//=========================================================
// sound.cpp
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "weapons.h"
#include "player.h"
#include "gamerules.h"
#if !defined ( _WIN32 )
#include <ctype.h>
#endif
static char *memfgets( byte *pMemFile, int fileSize, int &filePos, char *pBuffer, int bufferSize );
// ==================== GENERIC AMBIENT SOUND ======================================
// runtime pitch shift and volume fadein/out structure
// NOTE: IF YOU CHANGE THIS STRUCT YOU MUST CHANGE THE SAVE/RESTORE VERSION NUMBER
// SEE BELOW (in the typedescription for the class)
typedef struct dynpitchvol
{
// NOTE: do not change the order of these parameters
// NOTE: unless you also change order of rgdpvpreset array elements!
int preset;
int pitchrun; // pitch shift % when sound is running 0 - 255
int pitchstart; // pitch shift % when sound stops or starts 0 - 255
int spinup; // spinup time 0 - 100
int spindown; // spindown time 0 - 100
int volrun; // volume change % when sound is running 0 - 10
int volstart; // volume change % when sound stops or starts 0 - 10
int fadein; // volume fade in time 0 - 100
int fadeout; // volume fade out time 0 - 100
// Low Frequency Oscillator
int lfotype; // 0) off 1) square 2) triangle 3) random
int lforate; // 0 - 1000, how fast lfo osciallates
int lfomodpitch; // 0-100 mod of current pitch. 0 is off.
int lfomodvol; // 0-100 mod of current volume. 0 is off.
int cspinup; // each trigger hit increments counter and spinup pitch
int cspincount;
int pitch;
int spinupsav;
int spindownsav;
int pitchfrac;
int vol;
int fadeinsav;
int fadeoutsav;
int volfrac;
int lfofrac;
int lfomult;
} dynpitchvol_t;
#define CDPVPRESETMAX 27
// presets for runtime pitch and vol modulation of ambient sounds
dynpitchvol_t rgdpvpreset[CDPVPRESETMAX] =
{
// pitch pstart spinup spindwn volrun volstrt fadein fadeout lfotype lforate modptch modvol cspnup
{1, 255, 75, 95, 95, 10, 1, 50, 95, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{2, 255, 85, 70, 88, 10, 1, 20, 88, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{3, 255, 100, 50, 75, 10, 1, 10, 75, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{4, 100, 100, 0, 0, 10, 1, 90, 90, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{5, 100, 100, 0, 0, 10, 1, 80, 80, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{6, 100, 100, 0, 0, 10, 1, 50, 70, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{7, 100, 100, 0, 0, 5, 1, 40, 50, 1, 50, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{8, 100, 100, 0, 0, 5, 1, 40, 50, 1, 150, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{9, 100, 100, 0, 0, 5, 1, 40, 50, 1, 750, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{10,128, 100, 50, 75, 10, 1, 30, 40, 2, 8, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{11,128, 100, 50, 75, 10, 1, 30, 40, 2, 25, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{12,128, 100, 50, 75, 10, 1, 30, 40, 2, 70, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{13,50, 50, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{14,70, 70, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{15,90, 90, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{16,120, 120, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{17,180, 180, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{18,255, 255, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{19,200, 75, 90, 90, 10, 1, 50, 90, 2, 100, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{20,255, 75, 97, 90, 10, 1, 50, 90, 1, 40, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{21,100, 100, 0, 0, 10, 1, 30, 50, 3, 15, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{22,160, 160, 0, 0, 10, 1, 50, 50, 3, 500, 25, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{23,255, 75, 88, 0, 10, 1, 40, 0, 0, 0, 0, 0, 5, 0,0,0,0,0,0,0,0,0,0},
{24,200, 20, 95, 70, 10, 1, 70, 70, 3, 20, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{25,180, 100, 50, 60, 10, 1, 40, 60, 2, 90, 100, 100, 0, 0,0,0,0,0,0,0,0,0,0},
{26,60, 60, 0, 0, 10, 1, 40, 70, 3, 80, 20, 50, 0, 0,0,0,0,0,0,0,0,0,0},
{27,128, 90, 10, 10, 10, 1, 20, 40, 1, 5, 10, 20, 0, 0,0,0,0,0,0,0,0,0,0}
};
class CAmbientGeneric : public CBaseEntity
{
public:
void KeyValue( KeyValueData* pkvd);
void Spawn( void );
void Precache( void );
void EXPORT ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT RampThink( void );
void InitModulationParms(void);
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); }
float m_flAttenuation; // attenuation value
dynpitchvol_t m_dpv;
BOOL m_fActive; // only TRUE when the entity is playing a looping sound
BOOL m_fLooping; // TRUE when the sound played will loop
};
LINK_ENTITY_TO_CLASS( ambient_generic, CAmbientGeneric );
TYPEDESCRIPTION CAmbientGeneric::m_SaveData[] =
{
DEFINE_FIELD( CAmbientGeneric, m_flAttenuation, FIELD_FLOAT ),
DEFINE_FIELD( CAmbientGeneric, m_fActive, FIELD_BOOLEAN ),
DEFINE_FIELD( CAmbientGeneric, m_fLooping, FIELD_BOOLEAN ),
// HACKHACK - This is not really in the spirit of the save/restore design, but save this
// out as a binary data block. If the dynpitchvol_t is changed, old saved games will NOT
// load these correctly, so bump the save/restore version if you change the size of the struct
// The right way to do this is to split the input parms (read in keyvalue) into members and re-init this
// struct in Precache(), but it's unlikely that the struct will change, so it's not worth the time right now.
DEFINE_ARRAY( CAmbientGeneric, m_dpv, FIELD_CHARACTER, sizeof(dynpitchvol_t) ),
};
IMPLEMENT_SAVERESTORE( CAmbientGeneric, CBaseEntity );
//
// ambient_generic - general-purpose user-defined static sound
//
void CAmbientGeneric :: Spawn( void )
{
/*
-1 : "Default"
0 : "Everywhere"
200 : "Small Radius"
125 : "Medium Radius"
80 : "Large Radius"
*/
if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_EVERYWHERE) )
{
m_flAttenuation = ATTN_NONE;
}
else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_SMALLRADIUS) )
{
m_flAttenuation = ATTN_IDLE;
}
else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_MEDIUMRADIUS) )
{
m_flAttenuation = ATTN_STATIC;
}
else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_LARGERADIUS) )
{
m_flAttenuation = ATTN_NORM;
}
else
{// if the designer didn't set a sound attenuation, default to one.
m_flAttenuation = ATTN_STATIC;
}
char* szSoundFile = (char*) STRING(pev->message);
if ( FStringNull( pev->message ) || strlen( szSoundFile ) < 1 )
{
ALERT( at_error, "EMPTY AMBIENT AT: %f, %f, %f\n", pev->origin.x, pev->origin.y, pev->origin.z );
pev->nextthink = gpGlobals->time + 0.1;
SetThink( &CAmbientGeneric::SUB_Remove );
return;
}
pev->solid = SOLID_NOT;
pev->movetype = MOVETYPE_NONE;
// Set up think function for dynamic modification
// of ambient sound's pitch or volume. Don't
// start thinking yet.
SetThink(&CAmbientGeneric::RampThink);
pev->nextthink = 0;
// allow on/off switching via 'use' function.
SetUse ( &CAmbientGeneric::ToggleUse );
m_fActive = FALSE;
if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_NOT_LOOPING ) )
m_fLooping = FALSE;
else
m_fLooping = TRUE;
Precache( );
}
void CAmbientGeneric :: Precache( void )
{
char* szSoundFile = (char*) STRING(pev->message);
if ( !FStringNull( pev->message ) && strlen( szSoundFile ) > 1 )
{
if (*szSoundFile != '!')
PRECACHE_SOUND(szSoundFile);
}
// init all dynamic modulation parms
InitModulationParms();
if ( !FBitSet (pev->spawnflags, AMBIENT_SOUND_START_SILENT ) )
{
// start the sound ASAP
if (m_fLooping)
m_fActive = TRUE;
}
if ( m_fActive )
{
UTIL_EmitAmbientSound ( ENT(pev), pev->origin, szSoundFile,
(m_dpv.vol * 0.01), m_flAttenuation, SND_SPAWNING, m_dpv.pitch);
pev->nextthink = gpGlobals->time + 0.1;
}
}
// RampThink - Think at 5hz if we are dynamically modifying
// pitch or volume of the playing sound. This function will
// ramp pitch and/or volume up or down, modify pitch/volume
// with lfo if active.
void CAmbientGeneric :: RampThink( void )
{
char* szSoundFile = (char*) STRING(pev->message);
int pitch = m_dpv.pitch;
int vol = m_dpv.vol;
int flags = 0;
int fChanged = 0; // FALSE if pitch and vol remain unchanged this round
int prev;
if (!m_dpv.spinup && !m_dpv.spindown && !m_dpv.fadein && !m_dpv.fadeout && !m_dpv.lfotype)
return; // no ramps or lfo, stop thinking
// ==============
// pitch envelope
// ==============
if (m_dpv.spinup || m_dpv.spindown)
{
prev = m_dpv.pitchfrac >> 8;
if (m_dpv.spinup > 0)
m_dpv.pitchfrac += m_dpv.spinup;
else if (m_dpv.spindown > 0)
m_dpv.pitchfrac -= m_dpv.spindown;
pitch = m_dpv.pitchfrac >> 8;
if (pitch > m_dpv.pitchrun)
{
pitch = m_dpv.pitchrun;
m_dpv.spinup = 0; // done with ramp up
}
if (pitch < m_dpv.pitchstart)
{
pitch = m_dpv.pitchstart;
m_dpv.spindown = 0; // done with ramp down
// shut sound off
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
0, 0, SND_STOP, 0);
// return without setting nextthink
return;
}
if (pitch > 255) pitch = 255;
if (pitch < 1) pitch = 1;
m_dpv.pitch = pitch;
fChanged |= (prev != pitch);
flags |= SND_CHANGE_PITCH;
}
// ==================
// amplitude envelope
// ==================
if (m_dpv.fadein || m_dpv.fadeout)
{
prev = m_dpv.volfrac >> 8;
if (m_dpv.fadein > 0)
m_dpv.volfrac += m_dpv.fadein;
else if (m_dpv.fadeout > 0)
m_dpv.volfrac -= m_dpv.fadeout;
vol = m_dpv.volfrac >> 8;
if (vol > m_dpv.volrun)
{
vol = m_dpv.volrun;
m_dpv.fadein = 0; // done with ramp up
}
if (vol < m_dpv.volstart)
{
vol = m_dpv.volstart;
m_dpv.fadeout = 0; // done with ramp down
// shut sound off
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
0, 0, SND_STOP, 0);
// return without setting nextthink
return;
}
if (vol > 100) vol = 100;
if (vol < 1) vol = 1;
m_dpv.vol = vol;
fChanged |= (prev != vol);
flags |= SND_CHANGE_VOL;
}
// ===================
// pitch/amplitude LFO
// ===================
if (m_dpv.lfotype)
{
int pos;
if (m_dpv.lfofrac > 0x6fffffff)
m_dpv.lfofrac = 0;
// update lfo, lfofrac/255 makes a triangle wave 0-255
m_dpv.lfofrac += m_dpv.lforate;
pos = m_dpv.lfofrac >> 8;
if (m_dpv.lfofrac < 0)
{
m_dpv.lfofrac = 0;
m_dpv.lforate = abs(m_dpv.lforate);
pos = 0;
}
else if (pos > 255)
{
pos = 255;
m_dpv.lfofrac = (255 << 8);
m_dpv.lforate = -abs(m_dpv.lforate);
}
switch(m_dpv.lfotype)
{
case LFO_SQUARE:
if (pos < 128)
m_dpv.lfomult = 255;
else
m_dpv.lfomult = 0;
break;
case LFO_RANDOM:
if (pos == 255)
m_dpv.lfomult = RANDOM_LONG(0, 255);
break;
case LFO_TRIANGLE:
default:
m_dpv.lfomult = pos;
break;
}
if (m_dpv.lfomodpitch)
{
prev = pitch;
// pitch 0-255
pitch += ((m_dpv.lfomult - 128) * m_dpv.lfomodpitch) / 100;
if (pitch > 255) pitch = 255;
if (pitch < 1) pitch = 1;
fChanged |= (prev != pitch);
flags |= SND_CHANGE_PITCH;
}
if (m_dpv.lfomodvol)
{
// vol 0-100
prev = vol;
vol += ((m_dpv.lfomult - 128) * m_dpv.lfomodvol) / 100;
if (vol > 100) vol = 100;
if (vol < 0) vol = 0;
fChanged |= (prev != vol);
flags |= SND_CHANGE_VOL;
}
}
// Send update to playing sound only if we actually changed
// pitch or volume in this routine.
if (flags && fChanged)
{
if (pitch == PITCH_NORM)
pitch = PITCH_NORM + 1; // don't send 'no pitch' !
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
(vol * 0.01), m_flAttenuation, flags, pitch);
}
// update ramps at 5hz
pev->nextthink = gpGlobals->time + 0.2;
return;
}
// Init all ramp params in preparation to
// play a new sound
void CAmbientGeneric :: InitModulationParms(void)
{
int pitchinc;
m_dpv.volrun = pev->health * 10; // 0 - 100
if (m_dpv.volrun > 100) m_dpv.volrun = 100;
if (m_dpv.volrun < 0) m_dpv.volrun = 0;
// get presets
if (m_dpv.preset != 0 && m_dpv.preset <= CDPVPRESETMAX)
{
// load preset values
m_dpv = rgdpvpreset[m_dpv.preset - 1];
// fixup preset values, just like
// fixups in KeyValue routine.
if (m_dpv.spindown > 0)
m_dpv.spindown = (101 - m_dpv.spindown) * 64;
if (m_dpv.spinup > 0)
m_dpv.spinup = (101 - m_dpv.spinup) * 64;
m_dpv.volstart *= 10;
m_dpv.volrun *= 10;
if (m_dpv.fadein > 0)
m_dpv.fadein = (101 - m_dpv.fadein) * 64;
if (m_dpv.fadeout > 0)
m_dpv.fadeout = (101 - m_dpv.fadeout) * 64;
m_dpv.lforate *= 256;
m_dpv.fadeinsav = m_dpv.fadein;
m_dpv.fadeoutsav = m_dpv.fadeout;
m_dpv.spinupsav = m_dpv.spinup;
m_dpv.spindownsav = m_dpv.spindown;
}
m_dpv.fadein = m_dpv.fadeinsav;
m_dpv.fadeout = 0;
if (m_dpv.fadein)
m_dpv.vol = m_dpv.volstart;
else
m_dpv.vol = m_dpv.volrun;
m_dpv.spinup = m_dpv.spinupsav;
m_dpv.spindown = 0;
if (m_dpv.spinup)
m_dpv.pitch = m_dpv.pitchstart;
else
m_dpv.pitch = m_dpv.pitchrun;
if (m_dpv.pitch == 0)
m_dpv.pitch = PITCH_NORM;
m_dpv.pitchfrac = m_dpv.pitch << 8;
m_dpv.volfrac = m_dpv.vol << 8;
m_dpv.lfofrac = 0;
m_dpv.lforate = abs(m_dpv.lforate);
m_dpv.cspincount = 1;
if (m_dpv.cspinup)
{
pitchinc = (255 - m_dpv.pitchstart) / m_dpv.cspinup;
m_dpv.pitchrun = m_dpv.pitchstart + pitchinc;
if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255;
}
if ((m_dpv.spinupsav || m_dpv.spindownsav || (m_dpv.lfotype && m_dpv.lfomodpitch))
&& (m_dpv.pitch == PITCH_NORM))
m_dpv.pitch = PITCH_NORM + 1; // must never send 'no pitch' as first pitch
// if we intend to pitch shift later!
}
//
// ToggleUse - turns an ambient sound on or off. If the
// ambient is a looping sound, mark sound as active (m_fActive)
// if it's playing, innactive if not. If the sound is not
// a looping sound, never mark it as active.
//
void CAmbientGeneric :: ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
char* szSoundFile = (char*) STRING(pev->message);
float fraction;
if ( useType != USE_TOGGLE )
{
if ( (m_fActive && useType == USE_ON) || (!m_fActive && useType == USE_OFF) )
return;
}
// Directly change pitch if arg passed. Only works if sound is already playing.
if (useType == USE_SET && m_fActive) // Momentary buttons will pass down a float in here
{
fraction = value;
if ( fraction > 1.0 )
fraction = 1.0;
if (fraction < 0.0)
fraction = 0.01;
m_dpv.pitch = fraction * 255;
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
0, 0, SND_CHANGE_PITCH, m_dpv.pitch);
return;
}
// Toggle
// m_fActive is TRUE only if a looping sound is playing.
if ( m_fActive )
{// turn sound off
if (m_dpv.cspinup)
{
// Don't actually shut off. Each toggle causes
// incremental spinup to max pitch
if (m_dpv.cspincount <= m_dpv.cspinup)
{
int pitchinc;
// start a new spinup
m_dpv.cspincount++;
pitchinc = (255 - m_dpv.pitchstart) / m_dpv.cspinup;
m_dpv.spinup = m_dpv.spinupsav;
m_dpv.spindown = 0;
m_dpv.pitchrun = m_dpv.pitchstart + pitchinc * m_dpv.cspincount;
if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255;
pev->nextthink = gpGlobals->time + 0.1;
}
}
else
{
m_fActive = FALSE;
// HACKHACK - this makes the code in Precache() work properly after a save/restore
pev->spawnflags |= AMBIENT_SOUND_START_SILENT;
if (m_dpv.spindownsav || m_dpv.fadeoutsav)
{
// spin it down (or fade it) before shutoff if spindown is set
m_dpv.spindown = m_dpv.spindownsav;
m_dpv.spinup = 0;
m_dpv.fadeout = m_dpv.fadeoutsav;
m_dpv.fadein = 0;
pev->nextthink = gpGlobals->time + 0.1;
}
else
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
0, 0, SND_STOP, 0);
}
}
else
{// turn sound on
// only toggle if this is a looping sound. If not looping, each
// trigger will cause the sound to play. If the sound is still
// playing from a previous trigger press, it will be shut off
// and then restarted.
if (m_fLooping)
m_fActive = TRUE;
else
// shut sound off now - may be interrupting a long non-looping sound
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
0, 0, SND_STOP, 0);
// init all ramp params for startup
InitModulationParms();
UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile,
(m_dpv.vol * 0.01), m_flAttenuation, 0, m_dpv.pitch);
pev->nextthink = gpGlobals->time + 0.1;
}
}
// KeyValue - load keyvalue pairs into member data of the
// ambient generic. NOTE: called BEFORE spawn!
void CAmbientGeneric :: KeyValue( KeyValueData *pkvd )
{
// NOTE: changing any of the modifiers in this code
// NOTE: also requires changing InitModulationParms code.
// preset
if (FStrEq(pkvd->szKeyName, "preset"))
{
m_dpv.preset = atoi(pkvd->szValue);
pkvd->fHandled = TRUE;
}
// pitchrun
else if (FStrEq(pkvd->szKeyName, "pitch"))
{
m_dpv.pitchrun = atoi(pkvd->szValue);
pkvd->fHandled = TRUE;
if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255;
if (m_dpv.pitchrun < 0) m_dpv.pitchrun = 0;
}
// pitchstart
else if (FStrEq(pkvd->szKeyName, "pitchstart"))
{
m_dpv.pitchstart = atoi(pkvd->szValue);
pkvd->fHandled = TRUE;
if (m_dpv.pitchstart > 255) m_dpv.pitchstart = 255;
if (m_dpv.pitchstart < 0) m_dpv.pitchstart = 0;
}
// spinup
else if (FStrEq(pkvd->szKeyName, "spinup"))
{
m_dpv.spinup = atoi(pkvd->szValue);
if (m_dpv.spinup > 100) m_dpv.spinup = 100;
if (m_dpv.spinup < 0) m_dpv.spinup = 0;
if (m_dpv.spinup > 0)
m_dpv.spinup = (101 - m_dpv.spinup) * 64;
m_dpv.spinupsav = m_dpv.spinup;
pkvd->fHandled = TRUE;
}
// spindown
else if (FStrEq(pkvd->szKeyName, "spindown"))
{
m_dpv.spindown = atoi(pkvd->szValue);
if (m_dpv.spindown > 100) m_dpv.spindown = 100;
if (m_dpv.spindown < 0) m_dpv.spindown = 0;
if (m_dpv.spindown > 0)
m_dpv.spindown = (101 - m_dpv.spindown) * 64;
m_dpv.spindownsav = m_dpv.spindown;
pkvd->fHandled = TRUE;
}
// volstart
else if (FStrEq(pkvd->szKeyName, "volstart"))
{
m_dpv.volstart = atoi(pkvd->szValue);
if (m_dpv.volstart > 10) m_dpv.volstart = 10;
if (m_dpv.volstart < 0) m_dpv.volstart = 0;
m_dpv.volstart *= 10; // 0 - 100
pkvd->fHandled = TRUE;
}
// fadein
else if (FStrEq(pkvd->szKeyName, "fadein"))
{
m_dpv.fadein = atoi(pkvd->szValue);
if (m_dpv.fadein > 100) m_dpv.fadein = 100;
if (m_dpv.fadein < 0) m_dpv.fadein = 0;
if (m_dpv.fadein > 0)
m_dpv.fadein = (101 - m_dpv.fadein) * 64;
m_dpv.fadeinsav = m_dpv.fadein;
pkvd->fHandled = TRUE;
}
// fadeout
else if (FStrEq(pkvd->szKeyName, "fadeout"))
{
m_dpv.fadeout = atoi(pkvd->szValue);
if (m_dpv.fadeout > 100) m_dpv.fadeout = 100;
if (m_dpv.fadeout < 0) m_dpv.fadeout = 0;
if (m_dpv.fadeout > 0)
m_dpv.fadeout = (101 - m_dpv.fadeout) * 64;
m_dpv.fadeoutsav = m_dpv.fadeout;
pkvd->fHandled = TRUE;
}
// lfotype
else if (FStrEq(pkvd->szKeyName, "lfotype"))
{
m_dpv.lfotype = atoi(pkvd->szValue);
if (m_dpv.lfotype > 4) m_dpv.lfotype = LFO_TRIANGLE;
pkvd->fHandled = TRUE;
}
// lforate
else if (FStrEq(pkvd->szKeyName, "lforate"))
{
m_dpv.lforate = atoi(pkvd->szValue);
if (m_dpv.lforate > 1000) m_dpv.lforate = 1000;
if (m_dpv.lforate < 0) m_dpv.lforate = 0;
m_dpv.lforate *= 256;
pkvd->fHandled = TRUE;
}
// lfomodpitch
else if (FStrEq(pkvd->szKeyName, "lfomodpitch"))
{
m_dpv.lfomodpitch = atoi(pkvd->szValue);
if (m_dpv.lfomodpitch > 100) m_dpv.lfomodpitch = 100;
if (m_dpv.lfomodpitch < 0) m_dpv.lfomodpitch = 0;
pkvd->fHandled = TRUE;
}
// lfomodvol
else if (FStrEq(pkvd->szKeyName, "lfomodvol"))
{
m_dpv.lfomodvol = atoi(pkvd->szValue);
if (m_dpv.lfomodvol > 100) m_dpv.lfomodvol = 100;
if (m_dpv.lfomodvol < 0) m_dpv.lfomodvol = 0;
pkvd->fHandled = TRUE;
}
// cspinup
else if (FStrEq(pkvd->szKeyName, "cspinup"))
{
m_dpv.cspinup = atoi(pkvd->szValue);
if (m_dpv.cspinup > 100) m_dpv.cspinup = 100;
if (m_dpv.cspinup < 0) m_dpv.cspinup = 0;
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
// =================== ROOM SOUND FX ==========================================
class CEnvSound : public CPointEntity
{
public:
void KeyValue( KeyValueData* pkvd);
void Spawn( void );
void Think( void );
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
float m_flRadius;
float m_flRoomtype;
};
LINK_ENTITY_TO_CLASS( env_sound, CEnvSound );
TYPEDESCRIPTION CEnvSound::m_SaveData[] =
{
DEFINE_FIELD( CEnvSound, m_flRadius, FIELD_FLOAT ),
DEFINE_FIELD( CEnvSound, m_flRoomtype, FIELD_FLOAT ),
};
IMPLEMENT_SAVERESTORE( CEnvSound, CBaseEntity );
void CEnvSound :: KeyValue( KeyValueData *pkvd )
{
if (FStrEq(pkvd->szKeyName, "radius"))
{
m_flRadius = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
if (FStrEq(pkvd->szKeyName, "roomtype"))
{
m_flRoomtype = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
}
// returns TRUE if the given sound entity (pev) is in range
// and can see the given player entity (pevTarget)
BOOL FEnvSoundInRange(entvars_t *pev, entvars_t *pevTarget, float *pflRange)
{
CEnvSound *pSound = GetClassPtr( (CEnvSound *)pev );
Vector vecSpot1 = pev->origin + pev->view_ofs;
Vector vecSpot2 = pevTarget->origin + pevTarget->view_ofs;
Vector vecRange;
float flRange;
TraceResult tr;
UTIL_TraceLine(vecSpot1, vecSpot2, ignore_monsters, ENT(pev), &tr);
// check if line of sight crosses water boundary, or is blocked
if ((tr.fInOpen && tr.fInWater) || tr.flFraction != 1)
return FALSE;
// calc range from sound entity to player
vecRange = tr.vecEndPos - vecSpot1;
flRange = vecRange.Length();
if (pSound->m_flRadius < flRange)
return FALSE;
if (pflRange)
*pflRange = flRange;
return TRUE;
}
//
// A client that is visible and in range of a sound entity will
// have its room_type set by that sound entity. If two or more
// sound entities are contending for a client, then the nearest
// sound entity to the client will set the client's room_type.
// A client's room_type will remain set to its prior value until
// a new in-range, visible sound entity resets a new room_type.
//
// CONSIDER: if player in water state, autoset roomtype to 14,15 or 16.
void CEnvSound :: Think( void )
{
// get pointer to client if visible; FIND_CLIENT_IN_PVS will
// cycle through visible clients on consecutive calls.
edict_t *pentPlayer = FIND_CLIENT_IN_PVS(edict());
CBasePlayer *pPlayer = NULL;
if (FNullEnt(pentPlayer))
goto env_sound_Think_slow; // no player in pvs of sound entity, slow it down
pPlayer = GetClassPtr( (CBasePlayer *)VARS(pentPlayer));
float flRange;
// check to see if this is the sound entity that is
// currently affecting this player
if(!FNullEnt(pPlayer->m_pentSndLast) && (pPlayer->m_pentSndLast == ENT(pev))) {
// this is the entity currently affecting player, check
// for validity
if (pPlayer->m_flSndRoomtype != 0 && pPlayer->m_flSndRange != 0) {
// we're looking at a valid sound entity affecting
// player, make sure it's still valid, update range
if (FEnvSoundInRange(pev, VARS(pentPlayer), &flRange)) {
pPlayer->m_flSndRange = flRange;
goto env_sound_Think_fast;
} else {
// current sound entity affecting player is no longer valid,
// flag this state by clearing room_type and range.
// NOTE: we do not actually change the player's room_type
// NOTE: until we have a new valid room_type to change it to.
pPlayer->m_flSndRange = 0;
pPlayer->m_flSndRoomtype = 0;
goto env_sound_Think_slow;
}
} else {
// entity is affecting player but is out of range,
// wait passively for another entity to usurp it...
goto env_sound_Think_slow;
}
}
// if we got this far, we're looking at an entity that is contending
// for current player sound. the closest entity to player wins.
if (FEnvSoundInRange(pev, VARS(pentPlayer), &flRange))
{
if (flRange < pPlayer->m_flSndRange || pPlayer->m_flSndRange == 0)
{
// new entity is closer to player, so it wins.
pPlayer->m_pentSndLast = ENT(pev);
pPlayer->m_flSndRoomtype = m_flRoomtype;
pPlayer->m_flSndRange = flRange;
// send room_type command to player's server.
// this should be a rare event - once per change of room_type
// only!
//CLIENT_COMMAND(pentPlayer, "room_type %f", m_flRoomtype);
MESSAGE_BEGIN( MSG_ONE, SVC_ROOMTYPE, NULL, pentPlayer ); // use the magic #1 for "one client"
WRITE_SHORT( (short)m_flRoomtype ); // sequence number
MESSAGE_END();
// crank up nextthink rate for new active sound entity
// by falling through to think_fast...
}
// player is not closer to the contending sound entity,
// just fall through to think_fast. this effectively
// cranks up the think_rate of entities near the player.
}
// player is in pvs of sound entity, but either not visible or
// not in range. do nothing, fall through to think_fast...
env_sound_Think_fast:
pev->nextthink = gpGlobals->time + 0.25;
return;
env_sound_Think_slow:
pev->nextthink = gpGlobals->time + 0.75;
return;
}
//
// env_sound - spawn a sound entity that will set player roomtype
// when player moves in range and sight.
//
//
void CEnvSound :: Spawn( )
{
// spread think times
pev->nextthink = gpGlobals->time + RANDOM_FLOAT(0.0, 0.5);
}
// ==================== SENTENCE GROUPS, UTILITY FUNCTIONS ======================================
#define CSENTENCE_LRU_MAX 32 // max number of elements per sentence group
// group of related sentences
typedef struct sentenceg
{
char szgroupname[CBSENTENCENAME_MAX];
int count;
unsigned char rgblru[CSENTENCE_LRU_MAX];