]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/speaker.qc
f8f1c0ace5d32f38da5e6f3086b1f82011618eab
[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(this.noise)
83                 precache_sound (this.noise);
84
85         if(!this.atten && !(this.spawnflags & 4))
86         {
87                 IFTARGETED
88                         this.atten = ATTEN_NORM;
89                 else
90                         this.atten = ATTEN_STATIC;
91         }
92         else if(this.atten < 0)
93                 this.atten = 0;
94
95         if(!this.volume)
96                 this.volume = 1;
97
98         IFTARGETED
99         {
100                 if(this.spawnflags & 8) // ACTIVATOR
101                         this.use = target_speaker_use_activator;
102                 else if(this.spawnflags & 1) // LOOPED_ON
103                 {
104                         target_speaker_use_on(this, NULL, NULL);
105                         this.reset = target_speaker_reset;
106                 }
107                 else if(this.spawnflags & 2) // LOOPED_OFF
108                 {
109                         this.use = target_speaker_use_on;
110                         this.reset = target_speaker_reset;
111                 }
112                 else
113                         this.use = target_speaker_use_on;
114         }
115         else if(this.spawnflags & 1) // LOOPED_ON
116         {
117                 ambientsound (this.origin, this.noise, VOL_BASE * this.volume, this.atten);
118                 delete(this);
119         }
120         else if(this.spawnflags & 2) // LOOPED_OFF
121         {
122                 objerror(this, "This sound entity can never be activated");
123         }
124         else
125         {
126                 // Quake/Nexuiz fallback
127                 ambientsound (this.origin, this.noise, VOL_BASE * this.volume, this.atten);
128                 delete(this);
129         }
130 }
131 #endif