]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/tuba.qc
Merge branch 'master' into Mario/physics
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / tuba.qc
1 #define TUBA_MIN -18
2 #define TUBA_MAX  27
3 #define TUBA_INSTRUMENTS 3
4
5 #define TUBA_STARTNOTE(i,n) strcat("weapons/tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n), ".wav")
6 .float note; // note
7 .float attenuate; // if set, attenuate it
8 .float cnt; // current volume
9 .float count; // initial volume
10 .float tuba_instrument;
11
12 float Tuba_PitchStep;
13
14 void tubasound(entity e, float restart)
15 {
16         string snd1;
17
18         snd1 = string_null;
19
20         if(Tuba_PitchStep)
21         {
22                 string snd2;
23                 float f1, f2;
24                 float p1, p2;
25                 float m;
26
27                 f1 = 1;
28                 p1 = 1;
29                 snd2 = string_null;
30                 f2 = 0;
31                 p2 = 1;
32
33                 m = mod(e.note, Tuba_PitchStep);
34                 if(m)
35                 {
36                         if(e.note - m < TUBA_MIN)
37                         {
38                                 if(restart)
39                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
40                                 p1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
41                         }
42                         else if(e.note - m + Tuba_PitchStep > TUBA_MAX)
43                         {
44                                 if(restart)
45                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
46                                 p1 = pow(2.0, m / 12.0);
47                         }
48                         else
49                         {
50                                 if(restart)
51                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
52                                 f1 = cos(M_PI_2 * m / Tuba_PitchStep);
53                                 p1 = pow(2.0, m / 12.0);
54                                 if(restart)
55                                         snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
56                                 f2 = sin(M_PI_2 * m / Tuba_PitchStep);
57                                 p2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
58                         }
59                 }
60                 else
61                 {
62                         if(restart)
63                                 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
64                 }
65
66                 sound7(e, CH_TUBA_SINGLE, snd1, e.cnt * f1, e.attenuate * autocvar_g_balance_tuba_attenuation, 100 * p1, 0);
67                 if(f2)
68                         sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.cnt * f2, e.attenuate * autocvar_g_balance_tuba_attenuation, 100 * p2, 0);
69         }
70         else
71         {
72                 if(restart)
73                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
74                 sound(e, CH_TUBA_SINGLE, snd1, e.cnt, e.attenuate * autocvar_g_balance_tuba_attenuation);
75         }
76 }
77
78 void Ent_TubaNote_Think()
79 {
80         float f;
81         f = autocvar_g_balance_tuba_fadetime;
82         if(f > 0)
83                 self.cnt -= frametime * self.count / f;
84         else
85                 self.cnt = 0;
86         self.nextthink = time;
87         if(self.cnt <= 0)
88         {
89                 sound(self, CH_TUBA_SINGLE, "misc/null.wav", 0, 0);
90                 if(self.enemy)
91                 {
92                         sound(self.enemy, CH_TUBA_SINGLE, "misc/null.wav", 0, 0);
93                         remove(self.enemy);
94                 }
95                 remove(self);
96         }
97         else
98         {
99                 tubasound(self, 0);
100         }
101 }
102
103 void Ent_TubaNote_UpdateSound()
104 {
105         self.enemy.cnt = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
106         self.enemy.count = self.enemy.cnt;
107         self.enemy.note = self.note;
108         self.enemy.tuba_instrument = self.tuba_instrument;
109         tubasound(self.enemy, 1);
110 }
111
112 void Ent_TubaNote_StopSound()
113 {
114         self.enemy.nextthink = time;
115         self.enemy = world;
116 }
117
118 void Ent_TubaNote(float bIsNew)
119 {
120         float f, n, i, att, upd;
121         f = ReadByte();
122
123         upd = 0;
124
125         if(f & 1)
126         {
127                 n = ReadChar();
128                 i = ReadByte();
129                 att = (i & 1);
130                 i = floor(i / 2);
131
132                 if(n != self.note || i != self.tuba_instrument || bIsNew)
133                 {
134                         if(self.enemy)
135                                 Ent_TubaNote_StopSound();
136                 }
137
138                 if(!self.enemy)
139                 {
140                         self.enemy = spawn();
141                         self.enemy.classname = "tuba_note";
142                         if(Tuba_PitchStep)
143                         {
144                                 self.enemy.enemy = spawn();
145                                 self.enemy.enemy.classname = "tuba_note_2";
146                         }
147                         bIsNew = TRUE;
148                 }
149
150                 self.enemy.attenuate = att;
151
152                 if(bIsNew)
153                 {
154                         self.note = n;
155                         self.tuba_instrument = i;
156                         upd = 1;
157                 }
158         }
159
160         if(f & 2)
161         {
162                 self.enemy.origin_x = ReadCoord();
163                 self.enemy.origin_y = ReadCoord();
164                 self.enemy.origin_z = ReadCoord();
165                 setorigin(self.enemy, self.enemy.origin);
166                 if(self.enemy.enemy)
167                         setorigin(self.enemy.enemy, self.enemy.origin);
168         }
169
170         self.think = Ent_TubaNote_StopSound;
171         self.entremove = Ent_TubaNote_StopSound;
172         self.enemy.think = Ent_TubaNote_Think;
173         self.enemy.nextthink = time + 10;
174
175         if(upd)
176                 Ent_TubaNote_UpdateSound();
177 }
178
179 void Tuba_Precache()
180 {
181         float i, n;
182         Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
183         if(Tuba_PitchStep)
184         {
185                 if(!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7"))
186                 {
187                         print("^1NOTE:^7 requested pitch shifting, but not supported by this engine build\n");
188                         Tuba_PitchStep = 0;
189                 }
190         }
191         for(n = TUBA_MIN; n <= TUBA_MAX; ++n)
192         {
193                 if(!Tuba_PitchStep || (mod(n, Tuba_PitchStep) == 0))
194                 {
195                         for(i = 0; i < TUBA_INSTRUMENTS; ++i)
196                                 precache_sound(TUBA_STARTNOTE(i, n));
197                 }
198         }
199 }