]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/speaker.qc
take3: format 903 files
[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         }
10         string snd;
11         if (substring(this.noise, 0, 1) == "*") {
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                         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         }
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                 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                         tokenize_console(actor.(sample));
44                         float n;
45                         n = stof(argv(1));
46                         if (n > 0) {
47                                 snd = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
48                         } else {
49                                 snd = strcat(argv(0), ".wav");                                // randomization
50                         }
51                 }
52         } else {
53                 snd = this.noise;
54         }
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 }
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                 if (this.use == target_speaker_use_on) {
69                         target_speaker_use_on(this, NULL, NULL);
70                 }
71         } else if (this.spawnflags & 2) {
72                 if (this.use == target_speaker_use_off) {
73                         target_speaker_use_off(this, NULL, NULL);
74                 }
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
86         if (!this.atten && !(this.spawnflags & 4)) {
87                 if (THIS_TARGETED) {
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
96         if (!this.volume) {
97                 this.volume = 1;
98         }
99
100         if (THIS_TARGETED) {
101                 if (this.spawnflags & 8) { // ACTIVATOR
102                         this.use = target_speaker_use_activator;
103                 } else if (this.spawnflags & 1) { // LOOPED_ON
104                         target_speaker_use_on(this, NULL, NULL);
105                         this.reset = target_speaker_reset;
106                 } else if (this.spawnflags & 2) { // LOOPED_OFF
107                         this.use = target_speaker_use_on;
108                         this.reset = target_speaker_reset;
109                 } else {
110                         this.use = target_speaker_use_on;
111                 }
112         } else if (this.spawnflags & 1) { // LOOPED_ON
113                 ambientsound(this.origin, this.noise, VOL_BASE * this.volume, this.atten);
114                 delete(this);
115         } else if (this.spawnflags & 2) { // LOOPED_OFF
116                 objerror(this, "This sound entity can never be activated");
117         } else {
118                 // Quake/Nexuiz fallback
119                 ambientsound(this.origin, this.noise, VOL_BASE * this.volume, this.atten);
120                 delete(this);
121         }
122 }
123 #endif