8 #define MAX_TUBANOTES 32
9 .float tuba_lastnotes_last;
10 .float tuba_lastnotes_cnt; // over
11 .vector tuba_lastnotes[MAX_TUBANOTES];
13 bool W_Tuba_HasPlayed(entity pl, .entity weaponentity, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
15 float i, j, mmin, mmax, nolength;
16 float n = tokenize_console(melody);
17 if(n > pl.(weaponentity).tuba_lastnotes_cnt)
22 if(pl.(weaponentity).tuba_instrument != instrument)
27 for(i = 0; i < n; ++i)
29 vector v = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
30 float ai = stof(argv(n - i - 1));
34 // n counts the last played notes BACKWARDS
38 if(ignorepitch && i == 0)
40 pitchshift = np - v.z;
44 if(v.z + pitchshift != np)
49 // now we know the right NOTES were played
55 mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
59 mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
61 mmax = 240; // you won't try THAT hard... (tempo 1)
62 //printf("initial tempo rules: %f %f\n", mmin, mmax);
64 for(i = 0; i < n; ++i)
66 vector vi = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
67 float ai = stof(argv(n - i - 1));
68 ti -= 1 / (ai - floor(ai));
70 for(j = i+1; j < n; ++j)
72 vector vj = pl.(weaponentity).(tuba_lastnotes[(pl.(weaponentity).tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
73 float aj = stof(argv(n - j - 1));
74 tj -= (aj - floor(aj));
76 // note i should be at m*ti+b
77 // note j should be at m*tj+b
79 // we have a LINE l, so that
80 // vi_x <= l(ti) <= vi_y
81 // vj_x <= l(tj) <= vj_y
84 // vi_x <= vi_y <= vj_x <= vj_y
86 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
87 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
88 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
89 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
90 mmin = max(mmin, (vi.x - vj.y) / (ti - tj)); // lower bound
91 mmax = min(mmax, (vi.y - vj.x) / (ti - tj)); // upper bound
95 if(mmin > mmax) // rhythm fail
99 pl.(weaponentity).tuba_lastnotes_cnt = 0;
104 void W_Tuba_NoteOff(entity this)
106 entity actor = this.owner;
108 // on: this.spawnshieldtime
111 .entity weaponentity = this.weaponentity_fld;
112 if (actor.(weaponentity).tuba_note == this)
114 actor.(weaponentity).tuba_lastnotes_last = (actor.(weaponentity).tuba_lastnotes_last + 1) % MAX_TUBANOTES;
115 actor.(weaponentity).(tuba_lastnotes[actor.(weaponentity).tuba_lastnotes_last]) = vec3(this.spawnshieldtime, time, this.cnt);
116 actor.(weaponentity).tuba_note = NULL;
117 actor.(weaponentity).tuba_lastnotes_cnt = bound(0, actor.(weaponentity).tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
119 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
122 // simulate a server message
123 switch (this.tuba_instrument)
127 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
130 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
133 bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
141 int W_Tuba_GetNote(entity pl, int hittype)
144 if (CS(pl).movement.x < 0) movestate -= 3;
145 else if (CS(pl).movement.x > 0) movestate += 3;
146 if (CS(pl).movement.y < 0) movestate -= 1;
147 else if (CS(pl).movement.y > 0) movestate += 1;
152 // layout: originally I wanted
156 // but then you only use forward and right key. So to make things more
157 // interesting, I swapped B with e#. Har har har...
161 case 1: note = -6; break; // Gb
162 case 2: note = -5; break; // G
163 case 3: note = -4; break; // G#
164 case 4: note = +5; break; // e#
166 case 5: note = 0; break; // c
167 case 6: note = +2; break; // d
168 case 7: note = +3; break; // eb
169 case 8: note = +4; break; // e
170 case 9: note = -1; break; // B
172 if(PHYS_INPUT_BUTTON_CROUCH(pl))
174 if(PHYS_INPUT_BUTTON_JUMP(pl))
176 if(hittype & HITTYPE_SECONDARY)
179 // we support two kinds of tubas, those tuned in Eb and those tuned in C
180 // kind of tuba currently is player slot number, or team number if in
182 // that way, holes in the range of notes are "plugged"
185 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
190 if(pl.clientcolors & 1)
194 // total range of notes:
200 // *** ********************* ****
201 // -18.........................+12
202 // *** ********************* ****
203 // -18............................+15
204 // with jump: ... +24
209 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
212 if (!sound_allowed(MSG_ONE, this.realowner)) return false;
214 WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
215 WriteByte(MSG_ENTITY, sf);
218 WriteChar(MSG_ENTITY, this.cnt);
220 f |= 1 * (this.realowner != to);
221 f |= 2 * this.tuba_instrument;
222 WriteByte(MSG_ENTITY, f);
226 WriteVector(MSG_ENTITY, this.origin);
231 void W_Tuba_NoteThink(entity this)
237 if(time > this.teleport_time)
239 W_Tuba_NoteOff(this);
242 this.nextthink = time;
243 dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
244 FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != this.realowner, {
245 v = this.origin - (it.origin + it.view_ofs);
246 vol0 = max(0, 1 - vlen(v) * dist_mult);
248 v = this.realowner.origin - (it.origin + it.view_ofs);
249 vol1 = max(0, 1 - vlen(v) * dist_mult);
251 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
253 setorigin(this, this.realowner.origin);
257 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
259 setorigin(this, this.realowner.origin);
266 void W_Tuba_NoteOn(entity actor, .entity weaponentity, float hittype)
269 float n = W_Tuba_GetNote(actor, hittype);
272 if(actor.(weaponentity).tuba_instrument & 1)
273 hittype |= HITTYPE_SECONDARY;
274 if(actor.(weaponentity).tuba_instrument & 2)
275 hittype |= HITTYPE_BOUNCE;
277 W_SetupShot(actor, weaponentity, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage), hittype | WEP_TUBA.m_id);
279 if(actor.(weaponentity).tuba_note)
281 if(actor.(weaponentity).tuba_note.cnt != n || actor.(weaponentity).tuba_note.tuba_instrument != actor.(weaponentity).tuba_instrument)
283 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
287 if(!actor.(weaponentity).tuba_note)
289 entity note = new(tuba_note);
290 note.weaponentity_fld = weaponentity;
291 actor.(weaponentity).tuba_note = note;
292 note.owner = note.realowner = actor;
294 note.tuba_instrument = actor.(weaponentity).tuba_instrument;
295 setthink(note, W_Tuba_NoteThink);
296 note.nextthink = time;
297 note.spawnshieldtime = time;
298 Net_LinkEntity(note, false, 0, W_Tuba_NoteSendEntity);
301 actor.(weaponentity).tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(actor); // so it can get prolonged safely
303 //sound(actor, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
304 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, weaponentity, NULL);
306 o = gettaginfo(actor.exteriorweaponentity, 0);
307 if(time > actor.(weaponentity).tuba_smoketime)
309 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
310 actor.(weaponentity).tuba_smoketime = time + 0.25;
316 METHOD(Tuba, wr_aim, void(Tuba this, entity actor, .entity weaponentity))
318 // bots cannot play the Tuba well yet
319 // I think they should start with the recorder first
320 if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
323 PHYS_INPUT_BUTTON_ATCK(actor) = true;
325 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
329 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
332 if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
334 W_Tuba_NoteOn(actor, weaponentity, 0);
335 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
338 if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
340 W_Tuba_NoteOn(actor, weaponentity, HITTYPE_SECONDARY);
341 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
343 if (actor.(weaponentity).tuba_note)
345 if (!(fire & 1) && !(fire & 2))
347 W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
352 METHOD(Tuba, wr_setup, void(Tuba this, entity actor, .entity weaponentity))
354 actor.(weaponentity).tuba_instrument = 0;
357 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
359 // switch to alternate instruments :)
360 if (actor.(weaponentity).state == WS_READY)
362 switch (actor.(weaponentity).tuba_instrument)
365 actor.(weaponentity).tuba_instrument = 1;
366 actor.(weaponentity).weaponname = "akordeon";
369 actor.(weaponentity).tuba_instrument = 2;
370 actor.(weaponentity).weaponname = "kleinbottle";
373 actor.(weaponentity).tuba_instrument = 0;
374 actor.(weaponentity).weaponname = "tuba";
378 if(actor.(weaponentity).tuba_instrument & 1)
379 hittype |= HITTYPE_SECONDARY;
380 if(actor.(weaponentity).tuba_instrument & 2)
381 hittype |= HITTYPE_BOUNCE;
382 W_SetupShot(actor, weaponentity, false, 0, SND_Null, 0, 0, hittype | WEP_TUBA.m_id);
383 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
384 actor.(weaponentity).state = WS_INUSE;
385 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
393 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
394 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
396 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
398 if (w_deathtype & HITTYPE_BOUNCE)
399 return WEAPON_KLEINBOTTLE_SUICIDE;
400 else if (w_deathtype & HITTYPE_SECONDARY)
401 return WEAPON_ACCORDEON_SUICIDE;
403 return WEAPON_TUBA_SUICIDE;
405 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
407 if (w_deathtype & HITTYPE_BOUNCE)
408 return WEAPON_KLEINBOTTLE_MURDER;
409 else if (w_deathtype & HITTYPE_SECONDARY)
410 return WEAPON_ACCORDEON_MURDER;
412 return WEAPON_TUBA_MURDER;
417 METHOD(Tuba, wr_viewmodel, string(Tuba this, entity wep))
419 return (wep.tuba_instrument == 0) ? "tuba" :
420 (wep.tuba_instrument == 1) ? "akordeon" :
428 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
430 const int TUBA_MIN = -18;
431 const int TUBA_MAX = 27;
432 const int TUBA_INSTRUMENTS = 3;
436 void tubasound(entity e, bool restart)
438 string snd1 = string_null;
439 if (Tuba_PitchStep) {
442 string snd2 = string_null;
446 int m = pymod(e.note, Tuba_PitchStep);
448 if (e.note - m < TUBA_MIN) {
450 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
452 speed1 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
453 } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
455 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
457 speed1 = (2.0 ** (m / 12.0));
460 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
462 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
463 speed1 = (2.0 ** (m / 12.0));
465 snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
467 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
468 speed2 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
470 } else if (restart) {
471 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
474 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
476 sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
480 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
482 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
486 void Ent_TubaNote_Think(entity this)
488 float f = autocvar_g_balance_tuba_fadetime;
490 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
492 this.tuba_volume = 0;
494 this.nextthink = time;
495 if (this.tuba_volume <= 0) {
496 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
498 sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
507 void Ent_TubaNote_UpdateSound(entity this)
509 this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
510 this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
511 this.enemy.note = this.note;
512 this.enemy.tuba_instrument = this.tuba_instrument;
513 tubasound(this.enemy, 1);
516 void Ent_TubaNote_StopSound(entity this)
518 this.enemy.nextthink = time;
522 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
533 if (n != this.note || i != this.tuba_instrument || isNew) {
534 Ent_TubaNote_StopSound(this);
537 this.enemy = new(tuba_note);
538 if (Tuba_PitchStep) {
539 this.enemy.enemy = new(tuba_note_2);
544 this.enemy.tuba_attenuate = att;
548 this.tuba_instrument = i;
554 this.enemy.origin = ReadVector();
555 setorigin(this.enemy, this.enemy.origin);
556 if (this.enemy.enemy) {
557 setorigin(this.enemy.enemy, this.enemy.origin);
561 setthink(this, Ent_TubaNote_StopSound);
562 this.entremove = Ent_TubaNote_StopSound;
563 setthink(this.enemy, Ent_TubaNote_Think);
564 this.enemy.nextthink = time + 10;
567 Ent_TubaNote_UpdateSound(this);
574 Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
575 if (Tuba_PitchStep) {
576 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
577 LOG_WARN("requested pitch shifting, but not supported by this engine build");
581 for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
582 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
583 for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
584 precache_sound(TUBA_STARTNOTE(i, n));