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