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