]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/speaker.qc
Merge branch 'sev/luma_update' 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(entity this, entity actor, entity trigger);
4 void target_speaker_use_activator(entity this, entity actor, entity trigger)
5 {
6         if (!IS_REAL_CLIENT(actor))
7                 return;
8         string snd;
9         if(substring(this.noise, 0, 1) == "*")
10         {
11                 var .string sample = GetVoiceMessageSampleField(substring(this.noise, 1, -1));
12                 if(GetPlayerSoundSampleField_notFound)
13                         snd = SND(Null);
14                 else if(actor.(sample) == "")
15                         snd = SND(Null);
16                 else
17                 {
18                         tokenize_console(actor.(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 = this.noise;
29         msg_entity = actor;
30         soundto(MSG_ONE, this, CH_TRIGGER, snd, VOL_BASE * this.volume, this.atten);
31 }
32 void target_speaker_use_on(entity this, entity actor, entity trigger)
33 {
34         string snd;
35         if(substring(this.noise, 0, 1) == "*")
36         {
37                 var .string sample = GetVoiceMessageSampleField(substring(this.noise, 1, -1));
38                 if(GetPlayerSoundSampleField_notFound)
39                         snd = SND(Null);
40                 else if(actor.(sample) == "")
41                         snd = SND(Null);
42                 else
43                 {
44                         tokenize_console(actor.(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 = this.noise;
55         _sound(this, CH_TRIGGER_SINGLE, snd, VOL_BASE * this.volume, this.atten);
56         if(this.spawnflags & 3)
57                 this.use = target_speaker_use_off;
58 }
59 void target_speaker_use_off(entity this, entity actor, entity trigger)
60 {
61         sound(this, CH_TRIGGER_SINGLE, SND_Null, VOL_BASE * this.volume, this.atten);
62         this.use = target_speaker_use_on;
63 }
64 void target_speaker_reset(entity this)
65 {
66         if(this.spawnflags & 1) // LOOPED_ON
67         {
68                 if(this.use == target_speaker_use_on)
69                         target_speaker_use_on(this, NULL, NULL);
70         }
71         else if(this.spawnflags & 2)
72         {
73                 if(this.use == target_speaker_use_off)
74                         target_speaker_use_off(this, NULL, NULL);
75         }
76 }
77
78 spawnfunc(target_speaker)
79 {
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(self, NULL, NULL);
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