]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/sounds/sound.qh
Merge branch 'master' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / sounds / sound.qh
1 #pragma once
2
3 // negative = SVQC autochannels
4 // positive = one per entity
5
6 const int CH_INFO = 0;
7 const int CH_WEAPON_A = -1;
8 const int CH_WEAPON_B = -1;
9 const int CH_WEAPON_SINGLE = 1;
10 const int CH_VOICE = -2;
11 // const int CH_VOICE_SINGLE = 2;
12 const int CH_TRIGGER = -3;
13 const int CH_TRIGGER_SINGLE = 3;
14 const int CH_SHOTS = -4;
15 const int CH_SHOTS_SINGLE = 4;
16 // const int CH_TUBA = -5;
17 const int CH_TUBA_SINGLE = 5;
18 const int CH_PAIN = -6;
19 const int CH_PAIN_SINGLE = 6;
20 const int CH_PLAYER = -7;
21 const int CH_PLAYER_SINGLE = 7;
22 // const int CH_BGM_SINGLE = -8;
23 const int CH_BGM_SINGLE = 8;
24 const int CH_AMBIENT = -9;
25 const int CH_AMBIENT_SINGLE = 9;
26
27 const float ATTEN_NONE = 0;
28 const float ATTEN_MIN = 0.015625;
29 const float ATTEN_NORM = 0.5;
30 const float ATTEN_LARGE = 1;
31 const float ATTEN_IDLE = 2;
32 const float ATTEN_STATIC = 3;
33 const float ATTEN_MAX = 3.984375;
34
35 const float VOL_BASE = 0.7;
36 const float VOL_BASEVOICE = 1.0;
37 const float VOL_MUFFLED = 0.35;
38
39 // Play all sounds via sound7, for access to the extra channels.
40 // Otherwise, channels 8 to 15 would be blocked for a weird QW feature.
41 #ifdef SVQC
42         #define _sound(e, c, s, v, a) \
43                 MACRO_BEGIN \
44                         entity __e = e; \
45                         if (sound_allowed(MSG_BROADCAST, __e)) \
46                                 sound7(__e, c, s, v, a, 0, 0); \
47                 MACRO_END
48 #else
49         #define _sound(e, c, s, v, a) sound7(e, c, s, v, a, 0, 0)
50 #endif
51 #define sound(e, c, s, v, a) _sound(e, c, Sound_fixpath(s), v, a)
52
53 /**
54  * because sound7 didn't have origin
55  *
56  * @param e sound owner
57  * @param o sound origin
58  * @param chan sound channel
59  * @param samp sound filename
60  * @param vol sound volume
61  * @param atten sound attenuation
62  * @param speed
63  * @param sf
64  */
65 #define sound8(e, o, chan, samp, vol, atten, speed, sf) \
66         MACRO_BEGIN \
67                 entity __e; \
68                 int __chan = chan; \
69                 string __samp = samp; \
70                 bool auto = false; \
71                 if (__chan > 0) __e = e; \
72                 else \
73                 { \
74                         auto = true; \
75                         __chan = fabs(__chan); \
76                         entity tmp = __e = new(csqc_autochannel); \
77                         setthink(tmp, SUB_Remove); \
78                         tmp.nextthink = time + soundlength(__samp); \
79                 } \
80                 vector old_origin = __e.origin; \
81                 vector old_mins = __e.mins; \
82                 vector old_maxs = __e.maxs; \
83                 setorigin(__e, o); \
84                 setsize(__e, '0 0 0', '0 0 0'); \
85                 sound7(__e, __chan, __samp, vol, atten, speed, sf); \
86                 if (!auto) \
87                 { \
88                         setorigin(__e, old_origin); \
89                         setsize(__e, old_mins, old_maxs); \
90                 } \
91         MACRO_END
92
93 string _Sound_fixpath(string base)
94 {
95         if (base == "") return string_null;
96 #ifdef SVQC
97         return strcat(base, ".wav");  // let the client engine decide
98 #else
99 #define extensions(x) \
100         x(wav) \
101         x(ogg) \
102         x(flac) \
103         /**/
104 #define tryext(ext) { \
105                 string s = strcat(base, "." #ext); \
106                 if (fexists(strcat("sound/", s))) { \
107                         return s; \
108                 } \
109         }
110         extensions(tryext);
111         LOG_WARNF("Missing sound: \"%s\"", strcat("sound/", base));
112 #undef tryext
113 #undef extensions
114         return string_null;
115 #endif
116 }
117
118 CLASS(Sound, Object)
119         ATTRIB(Sound, m_id, int, 0);
120         ATTRIB(Sound, sound_str, string());
121         ATTRIB(Sound, sound_str_, string);
122         CONSTRUCTOR(Sound, string() path)
123         {
124                 CONSTRUCT(Sound);
125                 this.sound_str = path;
126         }
127         METHOD(Sound, sound_precache, void(Sound this))
128         {
129                 TC(Sound, this);
130                 string s = _Sound_fixpath(this.sound_str());
131                 if (!s) return;
132                 profile(sprintf("precache_sound(\"%s\")", s));
133                 precache_sound(s);
134                 strcpy(this.sound_str_, s);
135         }
136 ENDCLASS(Sound)
137
138 entity _Sound_fixpath_this;
139 string _Sound_fixpath_cached;
140 #define Sound_fixpath(this) ( \
141         _Sound_fixpath_this = (this), \
142         _Sound_fixpath_cached = _Sound_fixpath_this.sound_str_, \
143         _Sound_fixpath_cached ? _Sound_fixpath_cached : _Sound_fixpath(_Sound_fixpath_this.sound_str()) \
144 )