8 #define MAX_TUBANOTES 32
9 .float tuba_lastnotes_last;
10 .float tuba_lastnotes_cnt; // over
11 .vector tuba_lastnotes[MAX_TUBANOTES];
13 spawnfunc(weapon_tuba) { weapon_defaultspawnfunc(this, WEP_TUBA); }
15 bool W_Tuba_HasPlayed(entity pl, .entity weaponentity, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
17 float i, j, mmin, mmax, nolength;
18 float n = tokenize_console(melody);
19 if(n > pl.(weaponentity).tuba_lastnotes_cnt)
24 if(pl.(weaponentity).tuba_instrument != instrument)
29 for(i = 0; i < n; ++i)
31 vector v = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
32 float ai = stof(argv(n - i - 1));
36 // n counts the last played notes BACKWARDS
40 if(ignorepitch && i == 0)
42 pitchshift = np - v.z;
46 if(v.z + pitchshift != np)
51 // now we know the right NOTES were played
57 mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
61 mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
63 mmax = 240; // you won't try THAT hard... (tempo 1)
64 //printf("initial tempo rules: %f %f\n", mmin, mmax);
66 for(i = 0; i < n; ++i)
68 vector vi = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
69 float ai = stof(argv(n - i - 1));
70 ti -= 1 / (ai - floor(ai));
72 for(j = i+1; j < n; ++j)
74 vector vj = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
75 float aj = stof(argv(n - j - 1));
76 tj -= (aj - floor(aj));
78 // note i should be at m*ti+b
79 // note j should be at m*tj+b
81 // we have a LINE l, so that
82 // vi_x <= l(ti) <= vi_y
83 // vj_x <= l(tj) <= vj_y
86 // vi_x <= vi_y <= vj_x <= vj_y
88 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
89 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
90 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
91 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
92 mmin = max(mmin, (vi.x - vj.y) / (ti - tj)); // lower bound
93 mmax = min(mmax, (vi.y - vj.x) / (ti - tj)); // upper bound
97 if(mmin > mmax) // rhythm fail
101 pl.(weaponentity).tuba_lastnotes_cnt = 0;
106 void W_Tuba_NoteOff(entity this)
108 entity actor = this.owner;
110 // on: this.spawnshieldtime
113 .entity weaponentity = this.weaponentity_fld;
114 if (actor.(weaponentity).tuba_note == this)
116 actor.(weaponentity).tuba_lastnotes_last = (actor.(weaponentity).tuba_lastnotes_last + 1) % MAX_TUBANOTES;
117 actor.(weaponentity).(tuba_lastnotes[actor.(weaponentity).tuba_lastnotes_last]) = vec3(this.spawnshieldtime, time, this.cnt);
118 actor.(weaponentity).tuba_note = NULL;
119 actor.(weaponentity).tuba_lastnotes_cnt = bound(0, actor.(weaponentity).tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
121 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
124 // simulate a server message
125 switch (this.tuba_instrument)
129 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
132 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
135 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
143 int W_Tuba_GetNote(entity pl, int hittype)
146 if (CS(pl).movement.x < 0) movestate -= 3;
147 else if (CS(pl).movement.x > 0) movestate += 3;
148 if (CS(pl).movement.y < 0) movestate -= 1;
149 else if (CS(pl).movement.y > 0) movestate += 1;
154 // layout: originally I wanted
158 // but then you only use forward and right key. So to make things more
159 // interesting, I swapped B with e#. Har har har...
163 case 1: note = -6; break; // Gb
164 case 2: note = -5; break; // G
165 case 3: note = -4; break; // G#
166 case 4: note = +5; break; // e#
168 case 5: note = 0; break; // c
169 case 6: note = +2; break; // d
170 case 7: note = +3; break; // eb
171 case 8: note = +4; break; // e
172 case 9: note = -1; break; // B
174 if(PHYS_INPUT_BUTTON_CROUCH(pl))
176 if(PHYS_INPUT_BUTTON_JUMP(pl))
178 if(hittype & HITTYPE_SECONDARY)
181 // we support two kinds of tubas, those tuned in Eb and those tuned in C
182 // kind of tuba currently is player slot number, or team number if in
184 // that way, holes in the range of notes are "plugged"
187 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
192 if(pl.clientcolors & 1)
196 // total range of notes:
202 // *** ********************* ****
203 // -18.........................+12
204 // *** ********************* ****
205 // -18............................+15
206 // with jump: ... +24
211 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
214 if (!sound_allowed(MSG_ONE, this.realowner)) return false;
216 WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
217 WriteByte(MSG_ENTITY, sf);
220 WriteChar(MSG_ENTITY, this.cnt);
222 f |= 1 * (this.realowner != to);
223 f |= 2 * this.tuba_instrument;
224 WriteByte(MSG_ENTITY, f);
228 WriteCoord(MSG_ENTITY, this.origin.x);
229 WriteCoord(MSG_ENTITY, this.origin.y);
230 WriteCoord(MSG_ENTITY, this.origin.z);
235 void W_Tuba_NoteThink(entity this)
241 if(time > this.teleport_time)
243 W_Tuba_NoteOff(this);
246 this.nextthink = time;
247 dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
248 FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != this.realowner, {
249 v = this.origin - (it.origin + it.view_ofs);
250 vol0 = max(0, 1 - vlen(v) * dist_mult);
252 v = this.realowner.origin - (it.origin + it.view_ofs);
253 vol1 = max(0, 1 - vlen(v) * dist_mult);
255 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
257 setorigin(this, this.realowner.origin);
261 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
263 setorigin(this, this.realowner.origin);
270 void W_Tuba_NoteOn(entity actor, .entity weaponentity, float hittype)
275 W_SetupShot(actor, weaponentity, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage));
277 n = W_Tuba_GetNote(actor, hittype);
280 if(actor.(weaponentity).tuba_instrument & 1)
281 hittype |= HITTYPE_SECONDARY;
282 if(actor.(weaponentity).tuba_instrument & 2)
283 hittype |= HITTYPE_BOUNCE;
285 if(actor.(weaponentity).tuba_note)
287 if(actor.(weaponentity).tuba_note.cnt != n || actor.(weaponentity).tuba_note.tuba_instrument != actor.(weaponentity).tuba_instrument)
289 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
293 if(!actor.(weaponentity).tuba_note)
295 entity note = new(tuba_note);
296 note.weaponentity_fld = weaponentity;
297 actor.(weaponentity).tuba_note = note;
298 note.owner = note.realowner = actor;
300 note.tuba_instrument = actor.(weaponentity).tuba_instrument;
301 setthink(note, W_Tuba_NoteThink);
302 note.nextthink = time;
303 note.spawnshieldtime = time;
304 Net_LinkEntity(note, false, 0, W_Tuba_NoteSendEntity);
307 actor.(weaponentity).tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(actor); // so it can get prolonged safely
309 //sound(actor, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
310 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);
312 o = gettaginfo(actor.exteriorweaponentity, 0);
313 if(time > actor.(weaponentity).tuba_smoketime)
315 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
316 actor.(weaponentity).tuba_smoketime = time + 0.25;
322 METHOD(Tuba, wr_aim, void(Tuba this, entity actor, .entity weaponentity))
324 // bots cannot play the Tuba well yet
325 // I think they should start with the recorder first
326 if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
329 PHYS_INPUT_BUTTON_ATCK(actor) = true;
331 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
335 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
338 if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
340 W_Tuba_NoteOn(actor, weaponentity, 0);
341 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
344 if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
346 W_Tuba_NoteOn(actor, weaponentity, HITTYPE_SECONDARY);
347 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
349 if (actor.(weaponentity).tuba_note)
351 if (!(fire & 1) && !(fire & 2))
353 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
358 METHOD(Tuba, wr_setup, void(Tuba this, entity actor, .entity weaponentity))
360 actor.(weaponentity).tuba_instrument = 0;
363 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
365 // switch to alternate instruments :)
366 if (actor.(weaponentity).state == WS_READY)
368 switch (actor.(weaponentity).tuba_instrument)
371 actor.(weaponentity).tuba_instrument = 1;
372 actor.(weaponentity).weaponname = "akordeon";
375 actor.(weaponentity).tuba_instrument = 2;
376 actor.(weaponentity).weaponname = "kleinbottle";
379 actor.(weaponentity).tuba_instrument = 0;
380 actor.(weaponentity).weaponname = "tuba";
383 W_SetupShot(actor, weaponentity, false, 0, SND_Null, 0, 0);
384 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
385 actor.(weaponentity).state = WS_INUSE;
386 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
394 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
395 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
397 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
399 if (w_deathtype & HITTYPE_BOUNCE)
400 return WEAPON_KLEINBOTTLE_SUICIDE;
401 else if (w_deathtype & HITTYPE_SECONDARY)
402 return WEAPON_ACCORDEON_SUICIDE;
404 return WEAPON_TUBA_SUICIDE;
406 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
408 if (w_deathtype & HITTYPE_BOUNCE)
409 return WEAPON_KLEINBOTTLE_MURDER;
410 else if (w_deathtype & HITTYPE_SECONDARY)
411 return WEAPON_ACCORDEON_MURDER;
413 return WEAPON_TUBA_MURDER;
418 METHOD(Tuba, wr_viewmodel, string(Tuba this, entity wep))
420 return (wep.tuba_instrument == 0) ? "tuba" :
421 (wep.tuba_instrument == 1) ? "akordeon" :
429 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
431 const int TUBA_MIN = -18;
432 const int TUBA_MAX = 27;
433 const int TUBA_INSTRUMENTS = 3;
437 void tubasound(entity e, bool restart)
439 string snd1 = string_null;
440 if (Tuba_PitchStep) {
443 string snd2 = string_null;
447 int m = pymod(e.note, Tuba_PitchStep);
449 if (e.note - m < TUBA_MIN) {
451 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
453 speed1 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
454 } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
456 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
458 speed1 = (2.0 ** (m / 12.0));
461 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
463 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
464 speed1 = (2.0 ** (m / 12.0));
466 snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
468 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
469 speed2 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
471 } else if (restart) {
472 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
475 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
477 sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
481 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
483 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
487 void Ent_TubaNote_Think(entity this)
489 float f = autocvar_g_balance_tuba_fadetime;
491 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
493 this.tuba_volume = 0;
495 this.nextthink = time;
496 if (this.tuba_volume <= 0) {
497 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
499 sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
508 void Ent_TubaNote_UpdateSound(entity this)
510 this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
511 this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
512 this.enemy.note = this.note;
513 this.enemy.tuba_instrument = this.tuba_instrument;
514 tubasound(this.enemy, 1);
517 void Ent_TubaNote_StopSound(entity this)
519 this.enemy.nextthink = time;
523 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
534 if (n != this.note || i != this.tuba_instrument || isNew) {
535 Ent_TubaNote_StopSound(this);
538 this.enemy = new(tuba_note);
539 if (Tuba_PitchStep) {
540 this.enemy.enemy = new(tuba_note_2);
545 this.enemy.tuba_attenuate = att;
549 this.tuba_instrument = i;
555 this.enemy.origin_x = ReadCoord();
556 this.enemy.origin_y = ReadCoord();
557 this.enemy.origin_z = ReadCoord();
558 setorigin(this.enemy, this.enemy.origin);
559 if (this.enemy.enemy) {
560 setorigin(this.enemy.enemy, this.enemy.origin);
564 setthink(this, Ent_TubaNote_StopSound);
565 this.entremove = Ent_TubaNote_StopSound;
566 setthink(this.enemy, Ent_TubaNote_Think);
567 this.enemy.nextthink = time + 10;
570 Ent_TubaNote_UpdateSound(this);
577 Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
578 if (Tuba_PitchStep) {
579 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
580 LOG_WARN("requested pitch shifting, but not supported by this engine build");
584 for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
585 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
586 for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
587 precache_sound(TUBA_STARTNOTE(i, n));