]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/tuba.qc
Give wr_setup a weaponentity parameter so it isn't resetting the other weapon entitie...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / tuba.qc
1 #include "tuba.qh"
2 #ifndef IMPLEMENTATION
3 CLASS(Tuba, Weapon)
4 /* impulse   */ ATTRIB(Tuba, impulse, int, 1);
5 /* flags     */ ATTRIB(Tuba, spawnflags, int, WEP_FLAG_HIDDEN | WEP_TYPE_SPLASH);
6 /* rating    */ ATTRIB(Tuba, bot_pickupbasevalue, float, BOT_PICKUP_RATING_MID);
7 /* color     */ ATTRIB(Tuba, wpcolor, vector, '0 1 0');
8 /* modelname */ ATTRIB(Tuba, mdl, string, "tuba");
9 #ifdef GAMEQC
10 /* model     */ ATTRIB(Tuba, m_model, Model, MDL_TUBA_ITEM);
11 #endif
12 /* crosshair */ ATTRIB(Tuba, w_crosshair, string, "gfx/crosshairtuba");
13 /* crosshair */ //ATTRIB(Tuba, w_crosshair_size, float, 0.65);
14 /* wepimg    */ ATTRIB(Tuba, model2, string, "weapontuba");
15 /* refname   */ ATTRIB(Tuba, netname, string, "tuba");
16 /* xgettext:no-c-format */
17 /* wepname   */ ATTRIB(Tuba, m_name, string, _("@!#%'n Tuba"));
18
19 #define X(BEGIN, P, END, class, prefix) \
20         BEGIN(class) \
21                 P(class, prefix, animtime, float, NONE) \
22                 P(class, prefix, attenuation, float, NONE) \
23                 P(class, prefix, damage, float, NONE) \
24                 P(class, prefix, edgedamage, float, NONE) \
25                 P(class, prefix, fadetime, float, NONE) \
26                 P(class, prefix, force, float, NONE) \
27                 P(class, prefix, pitchstep, float, NONE) \
28                 P(class, prefix, radius, float, NONE) \
29                 P(class, prefix, refire, float, NONE) \
30                 P(class, prefix, switchdelay_drop, float, NONE) \
31                 P(class, prefix, switchdelay_raise, float, NONE) \
32                 P(class, prefix, volume, float, NONE) \
33                 P(class, prefix, weaponreplace, string, NONE) \
34                 P(class, prefix, weaponstartoverride, float, NONE) \
35                 P(class, prefix, weaponstart, float, NONE) \
36                 P(class, prefix, weaponthrowable, float, NONE) \
37         END()
38     W_PROPS(X, Tuba, tuba)
39 #undef X
40 ENDCLASS(Tuba)
41 REGISTER_WEAPON(TUBA, tuba, NEW(Tuba));
42 #endif
43
44 #ifdef IMPLEMENTATION
45
46 #ifdef SVQC
47
48 .entity tuba_note;
49 .float tuba_smoketime;
50 .float tuba_instrument;
51
52 #define MAX_TUBANOTES 32
53 .float tuba_lastnotes_last;
54 .float tuba_lastnotes_cnt; // over
55 .vector tuba_lastnotes[MAX_TUBANOTES];
56
57 spawnfunc(weapon_tuba) { weapon_defaultspawnfunc(this, WEP_TUBA); }
58
59 bool W_Tuba_HasPlayed(entity pl, .entity weaponentity, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
60 {
61         float i, j, mmin, mmax, nolength;
62         float n = tokenize_console(melody);
63         if(n > pl.(weaponentity).tuba_lastnotes_cnt)
64                 return false;
65         float pitchshift = 0;
66
67         if(instrument >= 0)
68                 if(pl.(weaponentity).tuba_instrument != instrument)
69                         return false;
70
71         // verify notes...
72         nolength = false;
73         for(i = 0; i < n; ++i)
74         {
75                 vector v = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
76                 float ai = stof(argv(n - i - 1));
77                 float np = floor(ai);
78                 if(ai == np)
79                         nolength = true;
80                 // n counts the last played notes BACKWARDS
81                 // _x is start
82                 // _y is end
83                 // _z is note pitch
84                 if(ignorepitch && i == 0)
85                 {
86                         pitchshift = np - v.z;
87                 }
88                 else
89                 {
90                         if(v.z + pitchshift != np)
91                                 return false;
92                 }
93         }
94
95         // now we know the right NOTES were played
96         if(!nolength)
97         {
98                 // verify rhythm...
99                 float ti = 0;
100                 if(maxtempo > 0)
101                         mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
102                 else
103                         mmin = 0;
104                 if(mintempo > 0)
105                         mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
106                 else
107                         mmax = 240; // you won't try THAT hard... (tempo 1)
108                 //printf("initial tempo rules: %f %f\n", mmin, mmax);
109
110                 for(i = 0; i < n; ++i)
111                 {
112                         vector vi = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
113                         float ai = stof(argv(n - i - 1));
114                         ti -= 1 / (ai - floor(ai));
115                         float tj = ti;
116                         for(j = i+1; j < n; ++j)
117                         {
118                                 vector vj = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
119                                 float aj = stof(argv(n - j - 1));
120                                 tj -= (aj - floor(aj));
121
122                                 // note i should be at m*ti+b
123                                 // note j should be at m*tj+b
124                                 // so:
125                                 // we have a LINE l, so that
126                                 // vi_x <= l(ti) <= vi_y
127                                 // vj_x <= l(tj) <= vj_y
128                                 // what is m?
129
130                                 // vi_x <= vi_y <= vj_x <= vj_y
131                                 // ti <= tj
132                                 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
133                                 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
134                                 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
135                                 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
136                                 mmin = max(mmin, (vi.x - vj.y) / (ti - tj)); // lower bound
137                                 mmax = min(mmax, (vi.y - vj.x) / (ti - tj)); // upper bound
138                         }
139                 }
140
141                 if(mmin > mmax) // rhythm fail
142                         return false;
143         }
144
145         pl.(weaponentity).tuba_lastnotes_cnt = 0;
146
147         return true;
148 }
149
150 void W_Tuba_NoteOff(entity this)
151 {
152         entity actor = this.owner;
153         // we have a note:
154         //   on: this.spawnshieldtime
155         //   off: time
156         //   note: this.cnt
157         .entity weaponentity = this.weaponentity_fld;
158         if (actor.(weaponentity).tuba_note == this)
159         {
160                 actor.(weaponentity).tuba_lastnotes_last = (actor.(weaponentity).tuba_lastnotes_last + 1) % MAX_TUBANOTES;
161                 actor.(weaponentity).(tuba_lastnotes[actor.(weaponentity).tuba_lastnotes_last]) = eX * this.spawnshieldtime + eY * time + eZ * this.cnt;
162                 actor.(weaponentity).tuba_note = NULL;
163                 actor.(weaponentity).tuba_lastnotes_cnt = bound(0, actor.(weaponentity).tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
164
165                 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
166                 if (s != "")
167                 {
168                         // simulate a server message
169                         switch (this.tuba_instrument)
170                         {
171                                 default:
172                                 case 0: // Tuba
173                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
174                                         break;
175                                 case 1:
176                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
177                                         break;
178                                 case 2:
179                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
180                                         break;
181                         }
182                 }
183         }
184         delete(this);
185 }
186
187 int W_Tuba_GetNote(entity pl, int hittype)
188 {
189         float movestate = 5;
190         if (pl.movement.x < 0)          movestate -= 3;
191         else if (pl.movement.x > 0)     movestate += 3;
192         if (pl.movement.y < 0)          movestate -= 1;
193         else if (pl.movement.y > 0)     movestate += 1;
194
195         int note = 0;
196         switch (movestate)
197         {
198         // layout: originally I wanted
199         //   eb e  e#=f
200         //   B  c  d
201         //   Gb G  G#
202         // but then you only use forward and right key. So to make things more
203         // interesting, I swapped B with e#. Har har har...
204         //   eb e  B
205         // f=e# c  d
206         //   Gb G  G#
207                 case 1: note = -6; break; // Gb
208                 case 2: note = -5; break; // G
209                 case 3: note = -4; break; // G#
210                 case 4: note = +5; break; // e#
211                 default:
212                 case 5: note =  0; break; // c
213                 case 6: note = +2; break; // d
214                 case 7: note = +3; break; // eb
215                 case 8: note = +4; break; // e
216                 case 9: note = -1; break; // B
217         }
218         if(PHYS_INPUT_BUTTON_CROUCH(pl))
219                 note -= 12;
220         if(PHYS_INPUT_BUTTON_JUMP(pl))
221                 note += 12;
222         if(hittype & HITTYPE_SECONDARY)
223                 note += 7;
224
225         // we support two kinds of tubas, those tuned in Eb and those tuned in C
226         // kind of tuba currently is player slot number, or team number if in
227         // teamplay
228         // that way, holes in the range of notes are "plugged"
229         if(teamplay)
230         {
231                 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
232                         note += 3;
233         }
234         else
235         {
236                 if(pl.clientcolors & 1)
237                         note += 3;
238         }
239
240         // total range of notes:
241         //                       0
242         //                 ***  ** ****
243         //                        ***  ** ****
244         //     ***  ** ****
245         //            ***  ** ****
246         //     ***  ********************* ****
247         //     -18.........................+12
248         //        ***  ********************* ****
249         //     -18............................+15
250         //     with jump: ... +24
251         //     ... +27
252         return note;
253 }
254
255 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
256 {
257         msg_entity = to;
258         if (!sound_allowed(MSG_ONE, this.realowner)) return false;
259
260         WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
261         WriteByte(MSG_ENTITY, sf);
262         if (sf & 1)
263         {
264                 WriteChar(MSG_ENTITY, this.cnt);
265                 int f = 0;
266                 f |= 1 * (this.realowner != to);
267                 f |= 2 * this.tuba_instrument;
268                 WriteByte(MSG_ENTITY, f);
269         }
270         if (sf & 2)
271         {
272                 WriteCoord(MSG_ENTITY, this.origin.x);
273                 WriteCoord(MSG_ENTITY, this.origin.y);
274                 WriteCoord(MSG_ENTITY, this.origin.z);
275         }
276         return true;
277 }
278
279 void W_Tuba_NoteThink(entity this)
280 {
281         float dist_mult;
282         float vol0, vol1;
283         vector dir0, dir1;
284         vector v;
285         if(time > this.teleport_time)
286         {
287                 W_Tuba_NoteOff(this);
288                 return;
289         }
290         this.nextthink = time;
291         dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
292         FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != this.realowner, {
293                 v = this.origin - (it.origin + it.view_ofs);
294                 vol0 = max(0, 1 - vlen(v) * dist_mult);
295                 dir0 = normalize(v);
296                 v = this.realowner.origin - (it.origin + it.view_ofs);
297                 vol1 = max(0, 1 - vlen(v) * dist_mult);
298                 dir1 = normalize(v);
299                 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
300                 {
301                         setorigin(this, this.realowner.origin);
302                         this.SendFlags |= 2;
303                         break;
304                 }
305                 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
306                 {
307                         setorigin(this, this.realowner.origin);
308                         this.SendFlags |= 2;
309                         break;
310                 }
311         });
312 }
313
314 void W_Tuba_NoteOn(entity actor, .entity weaponentity, float hittype)
315 {
316         vector o;
317         float n;
318
319         W_SetupShot(actor, weaponentity, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage));
320
321         n = W_Tuba_GetNote(actor, hittype);
322
323         hittype = 0;
324         if(actor.(weaponentity).tuba_instrument & 1)
325                 hittype |= HITTYPE_SECONDARY;
326         if(actor.(weaponentity).tuba_instrument & 2)
327                 hittype |= HITTYPE_BOUNCE;
328
329         if(actor.(weaponentity).tuba_note)
330         {
331                 if(actor.(weaponentity).tuba_note.cnt != n || actor.(weaponentity).tuba_note.tuba_instrument != actor.(weaponentity).tuba_instrument)
332                 {
333                         W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
334                 }
335         }
336
337         if(!actor.(weaponentity).tuba_note)
338         {
339                 entity note = new(tuba_note);
340                 note.weaponentity_fld = weaponentity;
341                 actor.(weaponentity).tuba_note = note;
342                 note.owner = note.realowner = actor;
343                 note.cnt = n;
344                 note.tuba_instrument = actor.(weaponentity).tuba_instrument;
345                 setthink(note, W_Tuba_NoteThink);
346                 note.nextthink = time;
347                 note.spawnshieldtime = time;
348                 Net_LinkEntity(note, false, 0, W_Tuba_NoteSendEntity);
349         }
350
351         actor.(weaponentity).tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(actor); // so it can get prolonged safely
352
353         //sound(actor, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
354         RadiusDamage(actor, actor, WEP_CVAR(tuba, damage), WEP_CVAR(tuba, edgedamage), WEP_CVAR(tuba, radius), NULL, NULL, WEP_CVAR(tuba, force), hittype | WEP_TUBA.m_id, NULL);
355
356         o = gettaginfo(actor.exteriorweaponentity, 0);
357         if(time > actor.(weaponentity).tuba_smoketime)
358         {
359                 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
360                 actor.(weaponentity).tuba_smoketime = time + 0.25;
361         }
362 }
363 #endif
364
365 #ifdef SVQC
366 METHOD(Tuba, wr_aim, void(Tuba this, entity actor))
367 {
368         // bots cannot play the Tuba well yet
369         // I think they should start with the recorder first
370         if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
371         {
372                 if (random() > 0.5)
373                         PHYS_INPUT_BUTTON_ATCK(actor) = true;
374                 else
375                         PHYS_INPUT_BUTTON_ATCK2(actor) = true;
376         }
377 }
378
379 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
380 {
381         if (fire & 1)
382         if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
383         {
384                 W_Tuba_NoteOn(actor, weaponentity, 0);
385                 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
386         }
387         if (fire & 2)
388         if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
389         {
390                 W_Tuba_NoteOn(actor, weaponentity, HITTYPE_SECONDARY);
391                 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
392         }
393         if (actor.(weaponentity).tuba_note)
394         {
395                 if (!(fire & 1) && !(fire & 2))
396                 {
397                         W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
398                 }
399         }
400 }
401
402 METHOD(Tuba, wr_setup, void(Tuba this, entity actor, .entity weaponentity))
403 {
404         actor.(weaponentity).tuba_instrument = 0;
405 }
406 #endif
407
408 REGISTER_NET_S2C(tuba_instrument)
409 #ifdef CSQC
410 NET_HANDLE(tuba_instrument, bool)
411 {
412         int i = ReadByte();
413         int slot = ReadByte();
414         return = true;
415         string s = (i == 0) ? "tuba" :
416                    (i == 1) ? "akordeon" :
417                               "kleinbottle" ;
418
419     entity wep = viewmodels[slot];
420     CL_WeaponEntity_SetModel(wep, s, true);
421 }
422 #endif
423 #ifdef SVQC
424 void tuba_instrument_send(entity this, .entity weaponentity, int instr)
425 {
426         msg_entity = this;
427         int chan = MSG_ONE;
428         WriteHeader(chan, tuba_instrument);
429         WriteByte(chan, instr);
430         WriteByte(chan, weaponslot(weaponentity));
431 }
432 SPECTATE_COPY()
433 {
434         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
435         {
436                 .entity weaponentity = weaponentities[slot];
437                 if(this.(weaponentity).tuba_instrument != spectatee.(weaponentity).tuba_instrument)
438                         tuba_instrument_send(this, weaponentity, this.(weaponentity).tuba_instrument = spectatee.(weaponentity).tuba_instrument);
439         }
440 }
441 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
442 {
443         // switch to alternate instruments :)
444         if (actor.(weaponentity).state == WS_READY)
445         {
446                 switch (actor.(weaponentity).tuba_instrument)
447                 {
448                         case 0:
449                                 actor.(weaponentity).tuba_instrument = 1;
450                                 actor.(weaponentity).weaponname = "akordeon";
451                                 break;
452                         case 1:
453                                 actor.(weaponentity).tuba_instrument = 2;
454                                 actor.(weaponentity).weaponname = "kleinbottle";
455                                 break;
456                         case 2:
457                                 actor.(weaponentity).tuba_instrument = 0;
458                                 actor.(weaponentity).weaponname = "tuba";
459                                 break;
460                 }
461                 tuba_instrument_send(actor, weaponentity, actor.(weaponentity).tuba_instrument);
462                 W_SetupShot(actor, weaponentity, false, 0, SND_Null, 0, 0);
463                 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
464                 actor.(weaponentity).state = WS_INUSE;
465                 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
466         }
467 }
468 #endif
469
470 #ifdef SVQC
471
472 // infinite ammo
473 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
474 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
475
476 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
477 {
478         if (w_deathtype & HITTYPE_BOUNCE)
479                 return WEAPON_KLEINBOTTLE_SUICIDE;
480         else if (w_deathtype & HITTYPE_SECONDARY)
481                 return WEAPON_ACCORDEON_SUICIDE;
482         else
483                 return WEAPON_TUBA_SUICIDE;
484 }
485 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
486 {
487         if (w_deathtype & HITTYPE_BOUNCE)
488                 return WEAPON_KLEINBOTTLE_MURDER;
489         else if (w_deathtype & HITTYPE_SECONDARY)
490                 return WEAPON_ACCORDEON_MURDER;
491         else
492                 return WEAPON_TUBA_MURDER;
493 }
494
495 #endif
496
497 #ifdef CSQC
498
499 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
500
501 const int TUBA_MIN = -18;
502 const int TUBA_MAX = 27;
503 const int TUBA_INSTRUMENTS = 3;
504
505 entityclass(Tuba);
506 class(Tuba) .int note;
507 class(Tuba) .bool tuba_attenuate;
508 class(Tuba) .float tuba_volume;
509 class(Tuba) .float tuba_volume_initial;
510 class(Tuba) .int tuba_instrument;
511
512 int Tuba_PitchStep;
513
514 void tubasound(entity e, bool restart)
515 {
516         string snd1 = string_null;
517         if (Tuba_PitchStep) {
518                 float vol1 = 1;
519                 float speed1 = 1;
520                 string snd2 = string_null;
521                 float vol2 = 0;
522                 float speed2 = 1;
523
524                 int m = pymod(e.note, Tuba_PitchStep);
525                 if (m) {
526                         if (e.note - m < TUBA_MIN) {
527                                 if (restart) {
528                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
529                                 }
530                                 speed1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
531                         } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
532                                 if (restart) {
533                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
534                                 }
535                                 speed1 = pow(2.0, m / 12.0);
536                         } else {
537                                 if (restart) {
538                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
539                                 }
540                                 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
541                                 speed1 = pow(2.0, m / 12.0);
542                                 if (restart) {
543                                         snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
544                                 }
545                                 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
546                                 speed2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
547                         }
548                 } else if (restart) {
549                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
550                 }
551
552                 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
553                 if (vol2) {
554                         sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
555                 }
556         } else {
557                 if (restart) {
558                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
559                 }
560                 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
561         }
562 }
563
564 void Ent_TubaNote_Think(entity this)
565 {
566         float f = autocvar_g_balance_tuba_fadetime;
567         if (f > 0) {
568                 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
569         } else {
570                 this.tuba_volume = 0;
571         }
572         this.nextthink = time;
573         if (this.tuba_volume <= 0) {
574                 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
575                 if (this.enemy) {
576                         sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
577                         delete(this.enemy);
578                 }
579                 delete(this);
580         } else {
581                 tubasound(this, 0);
582         }
583 }
584
585 void Ent_TubaNote_UpdateSound(entity this)
586 {
587         this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
588         this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
589         this.enemy.note = this.note;
590         this.enemy.tuba_instrument = this.tuba_instrument;
591         tubasound(this.enemy, 1);
592 }
593
594 void Ent_TubaNote_StopSound(entity this)
595 {
596         this.enemy.nextthink = time;
597         this.enemy = NULL;
598 }
599
600 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
601 {
602         bool upd = false;
603         int f = ReadByte();
604         if (f & 1) {
605                 int n = ReadChar();
606                 int i = ReadByte();
607                 bool att = (i & 1);
608                 i >>= 1;
609
610                 if (this.enemy) {
611                         if (n != this.note || i != this.tuba_instrument || isNew) {
612                                 Ent_TubaNote_StopSound(this);
613                         }
614                 } else {
615                         this.enemy = new(tuba_note);
616                         if (Tuba_PitchStep) {
617                                 this.enemy.enemy = new(tuba_note_2);
618                         }
619                         isNew = true;
620                 }
621
622                 this.enemy.tuba_attenuate = att;
623
624                 if (isNew) {
625                         this.note = n;
626                         this.tuba_instrument = i;
627                         upd = true;
628                 }
629         }
630
631         if (f & 2) {
632                 this.enemy.origin_x = ReadCoord();
633                 this.enemy.origin_y = ReadCoord();
634                 this.enemy.origin_z = ReadCoord();
635                 setorigin(this.enemy, this.enemy.origin);
636                 if (this.enemy.enemy) {
637                         setorigin(this.enemy.enemy, this.enemy.origin);
638                 }
639         }
640
641         setthink(this, Ent_TubaNote_StopSound);
642         this.entremove = Ent_TubaNote_StopSound;
643         setthink(this.enemy, Ent_TubaNote_Think);
644         this.enemy.nextthink = time + 10;
645
646         if (upd) {
647                 Ent_TubaNote_UpdateSound(this);
648         }
649         return true;
650 }
651
652 PRECACHE(Tuba)
653 {
654         Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
655         if (Tuba_PitchStep) {
656                 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
657                         LOG_WARN("requested pitch shifting, but not supported by this engine build");
658                         Tuba_PitchStep = 0;
659                 }
660         }
661         for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
662                 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
663                         for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
664                                 precache_sound(TUBA_STARTNOTE(i, n));
665                         }
666                 }
667         }
668 }
669
670 #endif
671 #endif