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");
10 /* model */ ATTRIB(Tuba, m_model, Model, MDL_TUBA_ITEM);
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"));
19 #define X(BEGIN, P, END, class, prefix) \
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) \
38 W_PROPS(X, Tuba, tuba)
41 REGISTER_WEAPON(TUBA, tuba, NEW(Tuba));
49 .float tuba_smoketime;
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, .entity weaponentity, 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.(weaponentity).tuba_lastnotes_cnt)
67 if(pl.(weaponentity).tuba_instrument != instrument)
72 for(i = 0; i < n; ++i)
74 vector v = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).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.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).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.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).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.(weaponentity).tuba_lastnotes_cnt = 0;
149 void W_Tuba_NoteOff(entity this)
151 entity actor = this.owner;
153 // on: this.spawnshieldtime
156 .entity weaponentity = this.weaponentity_fld;
157 if (actor.(weaponentity).tuba_note == this)
159 actor.(weaponentity).tuba_lastnotes_last = (actor.(weaponentity).tuba_lastnotes_last + 1) % MAX_TUBANOTES;
160 actor.(weaponentity).(tuba_lastnotes[actor.(weaponentity).tuba_lastnotes_last]) = eX * this.spawnshieldtime + eY * time + eZ * this.cnt;
161 actor.(weaponentity).tuba_note = NULL;
162 actor.(weaponentity).tuba_lastnotes_cnt = bound(0, actor.(weaponentity).tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
164 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
167 // simulate a server message
168 switch (this.tuba_instrument)
172 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
175 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
178 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
186 int W_Tuba_GetNote(entity pl, int hittype)
189 if (pl.movement.x < 0) movestate -= 3;
190 else if (pl.movement.x > 0) movestate += 3;
191 if (pl.movement.y < 0) movestate -= 1;
192 else if (pl.movement.y > 0) movestate += 1;
197 // layout: originally I wanted
201 // but then you only use forward and right key. So to make things more
202 // interesting, I swapped B with e#. Har har har...
206 case 1: note = -6; break; // Gb
207 case 2: note = -5; break; // G
208 case 3: note = -4; break; // G#
209 case 4: note = +5; break; // e#
211 case 5: note = 0; break; // c
212 case 6: note = +2; break; // d
213 case 7: note = +3; break; // eb
214 case 8: note = +4; break; // e
215 case 9: note = -1; break; // B
217 if(PHYS_INPUT_BUTTON_CROUCH(pl))
219 if(PHYS_INPUT_BUTTON_JUMP(pl))
221 if(hittype & HITTYPE_SECONDARY)
224 // we support two kinds of tubas, those tuned in Eb and those tuned in C
225 // kind of tuba currently is player slot number, or team number if in
227 // that way, holes in the range of notes are "plugged"
230 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
235 if(pl.clientcolors & 1)
239 // total range of notes:
245 // *** ********************* ****
246 // -18.........................+12
247 // *** ********************* ****
248 // -18............................+15
249 // with jump: ... +24
254 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
257 if (!sound_allowed(MSG_ONE, this.realowner)) return false;
259 WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
260 WriteByte(MSG_ENTITY, sf);
263 WriteChar(MSG_ENTITY, this.cnt);
265 f |= 1 * (this.realowner != to);
266 f |= 2 * this.tuba_instrument;
267 WriteByte(MSG_ENTITY, f);
271 WriteCoord(MSG_ENTITY, this.origin.x);
272 WriteCoord(MSG_ENTITY, this.origin.y);
273 WriteCoord(MSG_ENTITY, this.origin.z);
278 void W_Tuba_NoteThink(entity this)
284 if(time > this.teleport_time)
286 W_Tuba_NoteOff(this);
289 this.nextthink = time;
290 dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
291 FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != this.realowner, {
292 v = this.origin - (it.origin + it.view_ofs);
293 vol0 = max(0, 1 - vlen(v) * dist_mult);
295 v = this.realowner.origin - (it.origin + it.view_ofs);
296 vol1 = max(0, 1 - vlen(v) * dist_mult);
298 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
300 setorigin(this, this.realowner.origin);
304 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
306 setorigin(this, this.realowner.origin);
313 void W_Tuba_NoteOn(entity actor, .entity weaponentity, float hittype)
318 W_SetupShot(actor, weaponentity, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage));
320 n = W_Tuba_GetNote(actor, hittype);
323 if(actor.(weaponentity).tuba_instrument & 1)
324 hittype |= HITTYPE_SECONDARY;
325 if(actor.(weaponentity).tuba_instrument & 2)
326 hittype |= HITTYPE_BOUNCE;
328 if(actor.(weaponentity).tuba_note)
330 if(actor.(weaponentity).tuba_note.cnt != n || actor.(weaponentity).tuba_note.tuba_instrument != actor.(weaponentity).tuba_instrument)
332 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
336 if(!actor.(weaponentity).tuba_note)
338 entity note = new(tuba_note);
339 note.weaponentity_fld = weaponentity;
340 actor.(weaponentity).tuba_note = note;
341 note.owner = note.realowner = actor;
343 note.tuba_instrument = actor.(weaponentity).tuba_instrument;
344 setthink(note, W_Tuba_NoteThink);
345 note.nextthink = time;
346 note.spawnshieldtime = time;
347 Net_LinkEntity(note, false, 0, W_Tuba_NoteSendEntity);
350 actor.(weaponentity).tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(actor); // so it can get prolonged safely
352 //sound(actor, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
353 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 o = gettaginfo(actor.exteriorweaponentity, 0);
356 if(time > actor.(weaponentity).tuba_smoketime)
358 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
359 actor.(weaponentity).tuba_smoketime = time + 0.25;
365 METHOD(Tuba, wr_aim, void(Tuba this, entity actor, .entity weaponentity))
367 // bots cannot play the Tuba well yet
368 // I think they should start with the recorder first
369 if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
372 PHYS_INPUT_BUTTON_ATCK(actor) = true;
374 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
378 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
381 if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
383 W_Tuba_NoteOn(actor, weaponentity, 0);
384 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
387 if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
389 W_Tuba_NoteOn(actor, weaponentity, HITTYPE_SECONDARY);
390 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
392 if (actor.(weaponentity).tuba_note)
394 if (!(fire & 1) && !(fire & 2))
396 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
401 METHOD(Tuba, wr_setup, void(Tuba this, entity actor, .entity weaponentity))
403 actor.(weaponentity).tuba_instrument = 0;
406 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
408 // switch to alternate instruments :)
409 if (actor.(weaponentity).state == WS_READY)
411 switch (actor.(weaponentity).tuba_instrument)
414 actor.(weaponentity).tuba_instrument = 1;
415 actor.(weaponentity).weaponname = "akordeon";
418 actor.(weaponentity).tuba_instrument = 2;
419 actor.(weaponentity).weaponname = "kleinbottle";
422 actor.(weaponentity).tuba_instrument = 0;
423 actor.(weaponentity).weaponname = "tuba";
426 W_SetupShot(actor, weaponentity, false, 0, SND_Null, 0, 0);
427 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
428 actor.(weaponentity).state = WS_INUSE;
429 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
437 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
438 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
440 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
442 if (w_deathtype & HITTYPE_BOUNCE)
443 return WEAPON_KLEINBOTTLE_SUICIDE;
444 else if (w_deathtype & HITTYPE_SECONDARY)
445 return WEAPON_ACCORDEON_SUICIDE;
447 return WEAPON_TUBA_SUICIDE;
449 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
451 if (w_deathtype & HITTYPE_BOUNCE)
452 return WEAPON_KLEINBOTTLE_MURDER;
453 else if (w_deathtype & HITTYPE_SECONDARY)
454 return WEAPON_ACCORDEON_MURDER;
456 return WEAPON_TUBA_MURDER;
463 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
465 const int TUBA_MIN = -18;
466 const int TUBA_MAX = 27;
467 const int TUBA_INSTRUMENTS = 3;
471 void tubasound(entity e, bool restart)
473 string snd1 = string_null;
474 if (Tuba_PitchStep) {
477 string snd2 = string_null;
481 int m = pymod(e.note, Tuba_PitchStep);
483 if (e.note - m < TUBA_MIN) {
485 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
487 speed1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
488 } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
490 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
492 speed1 = pow(2.0, m / 12.0);
495 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
497 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
498 speed1 = pow(2.0, m / 12.0);
500 snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
502 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
503 speed2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
505 } else if (restart) {
506 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
509 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
511 sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
515 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
517 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
521 void Ent_TubaNote_Think(entity this)
523 float f = autocvar_g_balance_tuba_fadetime;
525 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
527 this.tuba_volume = 0;
529 this.nextthink = time;
530 if (this.tuba_volume <= 0) {
531 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
533 sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
542 void Ent_TubaNote_UpdateSound(entity this)
544 this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
545 this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
546 this.enemy.note = this.note;
547 this.enemy.tuba_instrument = this.tuba_instrument;
548 tubasound(this.enemy, 1);
551 void Ent_TubaNote_StopSound(entity this)
553 this.enemy.nextthink = time;
557 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
568 if (n != this.note || i != this.tuba_instrument || isNew) {
569 Ent_TubaNote_StopSound(this);
572 this.enemy = new(tuba_note);
573 if (Tuba_PitchStep) {
574 this.enemy.enemy = new(tuba_note_2);
579 this.enemy.tuba_attenuate = att;
583 this.tuba_instrument = i;
589 this.enemy.origin_x = ReadCoord();
590 this.enemy.origin_y = ReadCoord();
591 this.enemy.origin_z = ReadCoord();
592 setorigin(this.enemy, this.enemy.origin);
593 if (this.enemy.enemy) {
594 setorigin(this.enemy.enemy, this.enemy.origin);
598 setthink(this, Ent_TubaNote_StopSound);
599 this.entremove = Ent_TubaNote_StopSound;
600 setthink(this.enemy, Ent_TubaNote_Think);
601 this.enemy.nextthink = time + 10;
604 Ent_TubaNote_UpdateSound(this);
611 Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
612 if (Tuba_PitchStep) {
613 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
614 LOG_WARN("requested pitch shifting, but not supported by this engine build");
618 for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
619 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
620 for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
621 precache_sound(TUBA_STARTNOTE(i, n));