Merge branch 'TimePath/soundregistry' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / target / speaker.qc
1 #ifdef SVQC
2 // TODO add a way to do looped sounds with sound(); then complete this entity
3 void target_speaker_use_off();
4 void target_speaker_use_activator()
5 {SELFPARAM();
6         if (!IS_REAL_CLIENT(activator))
7                 return;
8         string snd;
9         if(substring(self.noise, 0, 1) == "*")
10         {
11                 var .string sample = GetVoiceMessageSampleField(substring(self.noise, 1, -1));
12                 if(GetPlayerSoundSampleField_notFound)
13                         snd = SND(Null);
14                 else if(activator.(sample) == "")
15                         snd = SND(Null);
16                 else
17                 {
18                         tokenize_console(activator.(sample));
19                         float n;
20                         n = stof(argv(1));
21                         if(n > 0)
22                                 snd = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
23                         else
24                                 snd = strcat(argv(0), ".wav"); // randomization
25                 }
26         }
27         else
28                 snd = self.noise;
29         msg_entity = activator;
30         soundto(MSG_ONE, self, CH_TRIGGER, snd, VOL_BASE * self.volume, self.atten);
31 }
32 void target_speaker_use_on()
33 {SELFPARAM();
34         string snd;
35         if(substring(self.noise, 0, 1) == "*")
36         {
37                 var .string sample = GetVoiceMessageSampleField(substring(self.noise, 1, -1));
38                 if(GetPlayerSoundSampleField_notFound)
39                         snd = SND(Null);
40                 else if(activator.(sample) == "")
41                         snd = SND(Null);
42                 else
43                 {
44                         tokenize_console(activator.(sample));
45                         float n;
46                         n = stof(argv(1));
47                         if(n > 0)
48                                 snd = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
49                         else
50                                 snd = strcat(argv(0), ".wav"); // randomization
51                 }
52         }
53         else
54                 snd = self.noise;
55         _sound(self, CH_TRIGGER_SINGLE, snd, VOL_BASE * self.volume, self.atten);
56         if(self.spawnflags & 3)
57                 self.use = target_speaker_use_off;
58 }
59 void target_speaker_use_off()
60 {SELFPARAM();
61         sound(self, CH_TRIGGER_SINGLE, SND_Null, VOL_BASE * self.volume, self.atten);
62         self.use = target_speaker_use_on;
63 }
64 void target_speaker_reset()
65 {SELFPARAM();
66         if(self.spawnflags & 1) // LOOPED_ON
67         {
68                 if(self.use == target_speaker_use_on)
69                         target_speaker_use_on();
70         }
71         else if(self.spawnflags & 2)
72         {
73                 if(self.use == target_speaker_use_off)
74                         target_speaker_use_off();
75         }
76 }
77
78 void spawnfunc_target_speaker()
79 {SELFPARAM();
80         // TODO: "*" prefix to sound file name
81         // TODO: wait and random (just, HOW? random is not a field)
82         if(self.noise)
83                 precache_sound (self.noise);
84
85         if(!self.atten && !(self.spawnflags & 4))
86         {
87                 IFTARGETED
88                         self.atten = ATTEN_NORM;
89                 else
90                         self.atten = ATTEN_STATIC;
91         }
92         else if(self.atten < 0)
93                 self.atten = 0;
94
95         if(!self.volume)
96                 self.volume = 1;
97
98         IFTARGETED
99         {
100                 if(self.spawnflags & 8) // ACTIVATOR
101                         self.use = target_speaker_use_activator;
102                 else if(self.spawnflags & 1) // LOOPED_ON
103                 {
104                         target_speaker_use_on();
105                         self.reset = target_speaker_reset;
106                 }
107                 else if(self.spawnflags & 2) // LOOPED_OFF
108                 {
109                         self.use = target_speaker_use_on;
110                         self.reset = target_speaker_reset;
111                 }
112                 else
113                         self.use = target_speaker_use_on;
114         }
115         else if(self.spawnflags & 1) // LOOPED_ON
116         {
117                 ambientsound (self.origin, self.noise, VOL_BASE * self.volume, self.atten);
118                 remove(self);
119         }
120         else if(self.spawnflags & 2) // LOOPED_OFF
121         {
122                 objerror("This sound entity can never be activated");
123         }
124         else
125         {
126                 // Quake/Nexuiz fallback
127                 ambientsound (self.origin, self.noise, VOL_BASE * self.volume, self.atten);
128                 remove(self);
129         }
130 }
131 #endif