3 /* impulse */ ATTRIB(Tuba, impulse, int, 1)
4 /* flags */ ATTRIB(Tuba, spawnflags, int, WEP_FLAG_HIDDEN | WEP_TYPE_SPLASH);
5 /* rating */ ATTRIB(Tuba, bot_pickupbasevalue, float, BOT_PICKUP_RATING_MID);
6 /* color */ ATTRIB(Tuba, wpcolor, vector, '0 1 0');
7 /* modelname */ ATTRIB(Tuba, mdl, string, "tuba");
9 /* model */ ATTRIB(Tuba, m_model, Model, MDL_TUBA_ITEM);
11 /* crosshair */ ATTRIB(Tuba, w_crosshair, string, "gfx/crosshairtuba");
12 /* crosshair */ //ATTRIB(Tuba, w_crosshair_size, float, 0.65);
13 /* wepimg */ ATTRIB(Tuba, model2, string, "weapontuba");
14 /* refname */ ATTRIB(Tuba, netname, string, "tuba");
15 /* xgettext:no-c-format */
16 /* wepname */ ATTRIB(Tuba, m_name, string, _("@!#%'n Tuba"));
18 #define X(BEGIN, P, END, class, prefix) \
20 P(class, prefix, animtime, float, NONE) \
21 P(class, prefix, attenuation, float, NONE) \
22 P(class, prefix, damage, float, NONE) \
23 P(class, prefix, edgedamage, float, NONE) \
24 P(class, prefix, fadetime, float, NONE) \
25 P(class, prefix, force, float, NONE) \
26 P(class, prefix, pitchstep, float, NONE) \
27 P(class, prefix, radius, float, NONE) \
28 P(class, prefix, refire, float, NONE) \
29 P(class, prefix, switchdelay_drop, float, NONE) \
30 P(class, prefix, switchdelay_raise, float, NONE) \
31 P(class, prefix, volume, float, NONE) \
32 P(class, prefix, weaponreplace, string, NONE) \
33 P(class, prefix, weaponstartoverride, float, NONE) \
34 P(class, prefix, weaponstart, float, NONE) \
35 P(class, prefix, weaponthrowable, float, NONE) \
37 W_PROPS(X, Tuba, tuba)
40 REGISTER_WEAPON(TUBA, tuba, NEW(Tuba));
48 .float tuba_smoketime;
49 .float tuba_instrument;
51 #define MAX_TUBANOTES 32
52 .float tuba_lastnotes_last;
53 .float tuba_lastnotes_cnt; // over
54 .vector tuba_lastnotes[MAX_TUBANOTES];
56 spawnfunc(weapon_tuba) { weapon_defaultspawnfunc(this, WEP_TUBA); }
58 bool W_Tuba_HasPlayed(entity pl, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
60 float i, j, mmin, mmax, nolength;
61 float n = tokenize_console(melody);
62 if(n > pl.tuba_lastnotes_cnt)
67 if(pl.tuba_instrument != instrument)
72 for(i = 0; i < n; ++i)
74 vector v = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
75 float ai = stof(argv(n - i - 1));
79 // n counts the last played notes BACKWARDS
83 if(ignorepitch && i == 0)
85 pitchshift = np - v.z;
89 if(v.z + pitchshift != np)
94 // now we know the right NOTES were played
100 mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
104 mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
106 mmax = 240; // you won't try THAT hard... (tempo 1)
107 //printf("initial tempo rules: %f %f\n", mmin, mmax);
109 for(i = 0; i < n; ++i)
111 vector vi = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
112 float ai = stof(argv(n - i - 1));
113 ti -= 1 / (ai - floor(ai));
115 for(j = i+1; j < n; ++j)
117 vector vj = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
118 float aj = stof(argv(n - j - 1));
119 tj -= (aj - floor(aj));
121 // note i should be at m*ti+b
122 // note j should be at m*tj+b
124 // we have a LINE l, so that
125 // vi_x <= l(ti) <= vi_y
126 // vj_x <= l(tj) <= vj_y
129 // vi_x <= vi_y <= vj_x <= vj_y
131 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
132 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
133 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
134 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
135 mmin = max(mmin, (vi.x - vj.y) / (ti - tj)); // lower bound
136 mmax = min(mmax, (vi.y - vj.x) / (ti - tj)); // upper bound
140 if(mmin > mmax) // rhythm fail
144 pl.tuba_lastnotes_cnt = 0;
149 void W_Tuba_NoteOff(entity this)
151 entity actor = this.owner;
153 // on: this.spawnshieldtime
156 if (actor.tuba_note == this)
158 actor.tuba_lastnotes_last = (actor.tuba_lastnotes_last + 1) % MAX_TUBANOTES;
159 actor.(tuba_lastnotes[actor.tuba_lastnotes_last]) = eX * this.spawnshieldtime + eY * time + eZ * this.cnt;
160 actor.tuba_note = NULL;
161 actor.tuba_lastnotes_cnt = bound(0, actor.tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
163 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
166 // simulate a server message
167 switch (this.tuba_instrument)
171 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
174 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
177 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
185 int W_Tuba_GetNote(entity pl, int hittype)
188 if (pl.movement.x < 0) movestate -= 3;
189 else if (pl.movement.x > 0) movestate += 3;
190 if (pl.movement.y < 0) movestate -= 1;
191 else if (pl.movement.y > 0) movestate += 1;
196 // layout: originally I wanted
200 // but then you only use forward and right key. So to make things more
201 // interesting, I swapped B with e#. Har har har...
205 case 1: note = -6; break; // Gb
206 case 2: note = -5; break; // G
207 case 3: note = -4; break; // G#
208 case 4: note = +5; break; // e#
210 case 5: note = 0; break; // c
211 case 6: note = +2; break; // d
212 case 7: note = +3; break; // eb
213 case 8: note = +4; break; // e
214 case 9: note = -1; break; // B
216 if(PHYS_INPUT_BUTTON_CROUCH(pl))
218 if(PHYS_INPUT_BUTTON_JUMP(pl))
220 if(hittype & HITTYPE_SECONDARY)
223 // we support two kinds of tubas, those tuned in Eb and those tuned in C
224 // kind of tuba currently is player slot number, or team number if in
226 // that way, holes in the range of notes are "plugged"
229 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
234 if(pl.clientcolors & 1)
238 // total range of notes:
244 // *** ********************* ****
245 // -18.........................+12
246 // *** ********************* ****
247 // -18............................+15
248 // with jump: ... +24
253 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
256 if (!sound_allowed(MSG_ONE, this.realowner)) return false;
258 WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
259 WriteByte(MSG_ENTITY, sf);
262 WriteChar(MSG_ENTITY, this.cnt);
264 f |= 1 * (this.realowner != to);
265 f |= 2 * this.tuba_instrument;
266 WriteByte(MSG_ENTITY, f);
270 WriteCoord(MSG_ENTITY, this.origin.x);
271 WriteCoord(MSG_ENTITY, this.origin.y);
272 WriteCoord(MSG_ENTITY, this.origin.z);
277 void W_Tuba_NoteThink()
283 if(time > self.teleport_time)
285 W_Tuba_NoteOff(this);
288 self.nextthink = time;
289 dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
290 FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != self.realowner, {
291 v = self.origin - (it.origin + it.view_ofs);
292 vol0 = max(0, 1 - vlen(v) * dist_mult);
294 v = self.realowner.origin - (it.origin + it.view_ofs);
295 vol1 = max(0, 1 - vlen(v) * dist_mult);
297 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
299 setorigin(self, self.realowner.origin);
303 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
305 setorigin(self, self.realowner.origin);
312 void W_Tuba_NoteOn(float hittype)
317 W_SetupShot(self, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage));
319 n = W_Tuba_GetNote(self, hittype);
322 if(self.tuba_instrument & 1)
323 hittype |= HITTYPE_SECONDARY;
324 if(self.tuba_instrument & 2)
325 hittype |= HITTYPE_BOUNCE;
329 if(self.tuba_note.cnt != n || self.tuba_note.tuba_instrument != self.tuba_instrument)
331 W_Tuba_NoteOff(self.tuba_note);
337 self.tuba_note = new(tuba_note);
338 self.tuba_note.owner = self.tuba_note.realowner = self;
339 self.tuba_note.cnt = n;
340 self.tuba_note.tuba_instrument = self.tuba_instrument;
341 self.tuba_note.think = W_Tuba_NoteThink;
342 self.tuba_note.nextthink = time;
343 self.tuba_note.spawnshieldtime = time;
344 Net_LinkEntity(self.tuba_note, false, 0, W_Tuba_NoteSendEntity);
347 self.tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(); // so it can get prolonged safely
349 //sound(self, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
350 RadiusDamage(self, self, WEP_CVAR(tuba, damage), WEP_CVAR(tuba, edgedamage), WEP_CVAR(tuba, radius), NULL, NULL, WEP_CVAR(tuba, force), hittype | WEP_TUBA.m_id, NULL);
352 o = gettaginfo(self.exteriorweaponentity, 0);
353 if(time > self.tuba_smoketime)
355 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
356 self.tuba_smoketime = time + 0.25;
362 METHOD(Tuba, wr_aim, void(Tuba this))
365 // bots cannot play the Tuba well yet
366 // I think they should start with the recorder first
367 if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
370 PHYS_INPUT_BUTTON_ATCK(actor) = true;
372 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
376 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
379 if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
382 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
385 if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
387 W_Tuba_NoteOn(HITTYPE_SECONDARY);
388 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
392 if (!(fire & 1) && !(fire & 2))
394 W_Tuba_NoteOff(actor.tuba_note);
399 METHOD(Tuba, wr_setup, void(Tuba this))
402 actor.ammo_field = ammo_none;
403 actor.tuba_instrument = 0;
407 REGISTER_NET_S2C(tuba_instrument)
409 NET_HANDLE(tuba_instrument, bool)
413 string s = (i == 0) ? "tuba" :
414 (i == 1) ? "akordeon" :
416 CL_WeaponEntity_SetModel(viewmodel, s, true);
420 void tuba_instrument_send(entity this, int instr)
424 WriteHeader(chan, tuba_instrument);
425 WriteByte(chan, instr);
429 if (this.tuba_instrument != spectatee.tuba_instrument)
430 tuba_instrument_send(this, this.tuba_instrument = spectatee.tuba_instrument);
432 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
434 // switch to alternate instruments :)
435 if (actor.(weaponentity).state == WS_READY)
437 switch (actor.tuba_instrument)
440 actor.tuba_instrument = 1;
441 actor.weaponname = "akordeon";
444 actor.tuba_instrument = 2;
445 actor.weaponname = "kleinbottle";
448 actor.tuba_instrument = 0;
449 actor.weaponname = "tuba";
452 tuba_instrument_send(actor, actor.tuba_instrument);
453 W_SetupShot(actor, false, 0, SND_Null, 0, 0);
454 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
455 actor.(weaponentity).state = WS_INUSE;
456 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
464 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor)) { return true; }
465 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor)) { return true; }
467 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
469 if (w_deathtype & HITTYPE_BOUNCE)
470 return WEAPON_KLEINBOTTLE_SUICIDE;
471 else if (w_deathtype & HITTYPE_SECONDARY)
472 return WEAPON_ACCORDEON_SUICIDE;
474 return WEAPON_TUBA_SUICIDE;
476 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
478 if (w_deathtype & HITTYPE_BOUNCE)
479 return WEAPON_KLEINBOTTLE_MURDER;
480 else if (w_deathtype & HITTYPE_SECONDARY)
481 return WEAPON_ACCORDEON_MURDER;
483 return WEAPON_TUBA_MURDER;
490 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
492 const int TUBA_MIN = -18;
493 const int TUBA_MAX = 27;
494 const int TUBA_INSTRUMENTS = 3;
497 class(Tuba) .int note;
498 class(Tuba) .bool tuba_attenuate;
499 class(Tuba) .float tuba_volume;
500 class(Tuba) .float tuba_volume_initial;
501 class(Tuba) .int tuba_instrument;
505 void tubasound(entity e, bool restart)
507 string snd1 = string_null;
508 if (Tuba_PitchStep) {
511 string snd2 = string_null;
515 int m = pymod(e.note, Tuba_PitchStep);
517 if (e.note - m < TUBA_MIN) {
519 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
521 speed1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
522 } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
524 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
526 speed1 = pow(2.0, m / 12.0);
529 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
531 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
532 speed1 = pow(2.0, m / 12.0);
534 snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
536 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
537 speed2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
539 } else if (restart) {
540 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
543 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
545 sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
549 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
551 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
555 void Ent_TubaNote_Think()
557 float f = autocvar_g_balance_tuba_fadetime;
559 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
561 this.tuba_volume = 0;
563 this.nextthink = time;
564 if (this.tuba_volume <= 0) {
565 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
567 sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
576 void Ent_TubaNote_UpdateSound(entity this)
578 this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
579 this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
580 this.enemy.note = this.note;
581 this.enemy.tuba_instrument = this.tuba_instrument;
582 tubasound(this.enemy, 1);
585 void Ent_TubaNote_StopSound(entity this)
587 this.enemy.nextthink = time;
591 void Ent_TubaNote_StopSound_self() { SELFPARAM(); Ent_TubaNote_StopSound(self); }
593 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
604 if (n != this.note || i != this.tuba_instrument || isNew) {
605 Ent_TubaNote_StopSound(this);
608 this.enemy = new(tuba_note);
609 if (Tuba_PitchStep) {
610 this.enemy.enemy = new(tuba_note_2);
615 this.enemy.tuba_attenuate = att;
619 this.tuba_instrument = i;
625 this.enemy.origin_x = ReadCoord();
626 this.enemy.origin_y = ReadCoord();
627 this.enemy.origin_z = ReadCoord();
628 setorigin(this.enemy, this.enemy.origin);
629 if (this.enemy.enemy) {
630 setorigin(this.enemy.enemy, this.enemy.origin);
634 this.think = Ent_TubaNote_StopSound_self;
635 this.entremove = Ent_TubaNote_StopSound;
636 this.enemy.think = Ent_TubaNote_Think;
637 this.enemy.nextthink = time + 10;
640 Ent_TubaNote_UpdateSound(this);
647 Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
648 if (Tuba_PitchStep) {
649 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
650 LOG_WARNING("requested pitch shifting, but not supported by this engine build");
654 for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
655 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
656 for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
657 precache_sound(TUBA_STARTNOTE(i, n));