Xonotic
speaker.qc
Go to the documentation of this file.
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));
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, 0);
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));
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);
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 & SPEAKER_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 & SPEAKER_LOOPED_OFF)
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 & SPEAKER_GLOBAL) && !(this.spawnflags & 3)) // special check for quake 3: looped sounds are never global
87  {
88  LOG_WARN("target_speaker uses legacy spawnflag GLOBAL (BIT(2)), please set atten to -1 instead");
89  this.atten = -1;
90  }
91 
92  if(!this.atten)
93  {
94  if(this.targetname && this.targetname != "")
95  this.atten = ATTEN_NORM;
96  else
97  this.atten = ATTEN_STATIC;
98  }
99  else if(this.atten < 0)
100  this.atten = 0;
101 
102  if(!this.volume)
103  this.volume = 1;
104 
105  if(this.targetname && this.targetname != "")
106  {
107  if(this.spawnflags & SPEAKER_ACTIVATOR)
108  this.use = target_speaker_use_activator;
109  else if(this.spawnflags & SPEAKER_LOOPED_ON)
110  {
111  target_speaker_use_on(this, NULL, NULL);
112  this.reset = target_speaker_reset;
113  }
114  else if(this.spawnflags & SPEAKER_LOOPED_OFF)
115  {
116  this.use = target_speaker_use_on;
117  this.reset = target_speaker_reset;
118  }
119  else
120  this.use = target_speaker_use_on;
121  }
122  else if(this.spawnflags & SPEAKER_LOOPED_ON)
123  {
124  ambientsound (this.origin, this.noise, VOL_BASE * this.volume, this.atten);
125  delete(this);
126  }
127  else if(this.spawnflags & SPEAKER_LOOPED_OFF)
128  {
129  objerror(this, "This sound entity can never be activated");
130  }
131  else
132  {
133  // Quake/Nexuiz fallback
134  ambientsound (this.origin, this.noise, VOL_BASE * this.volume, this.atten);
135  delete(this);
136  }
137 }
138 #endif
#define LOG_WARN(...)
Definition: log.qh:66
#define SND(id)
Definition: all.qh:35
string noise
Definition: progsdefs.qc:209
const int SPEAKER_GLOBAL
Definition: speaker.qh:6
entity() spawn
float atten
Definition: triggers.qh:47
spawnfunc(info_player_attacker)
Definition: sv_assault.qc:283
origin
Definition: ent_cs.qc:114
const int SPEAKER_LOOPED_ON
Definition: speaker.qh:4
float spawnflags
Definition: progsdefs.qc:191
#define IS_REAL_CLIENT(v)
Definition: utils.qh:17
const float ATTEN_STATIC
Definition: sound.qh:33
entity msg_entity
Definition: progsdefs.qc:63
string GetVoiceMessageSampleField(string type)
Definition: globalsound.qc:217
const int SPEAKER_ACTIVATOR
Definition: speaker.qh:7
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
bool GetPlayerSoundSampleField_notFound
Definition: globalsound.qh:123
const int CH_TRIGGER
Definition: sound.qh:12
#define NULL
Definition: post.qh:17
const float VOL_BASE
Definition: sound.qh:36
const float ATTEN_NORM
Definition: sound.qh:30
const int CH_TRIGGER_SINGLE
Definition: sound.qh:13
#define tokenize_console
Definition: dpextensions.qh:24
#define _sound(e, c, s, v, a)
Definition: sound.qh:50
string targetname
Definition: progsdefs.qc:194
#define use
Definition: csprogsdefs.qh:50
#define sound(e, c, s, v, a)
Definition: sound.qh:52
const int SPEAKER_LOOPED_OFF
Definition: speaker.qh:5
float volume
Definition: triggers.qh:47