3 /* ammotype */ //ATTRIB(Tuba, ammo_field, .int, ammo_none)
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)
42 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];
60 spawnfunc(weapon_tuba) { weapon_defaultspawnfunc(this, WEP_TUBA); }
62 bool W_Tuba_HasPlayed(entity pl, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
64 float i, j, mmin, mmax, nolength;
65 float n = tokenize_console(melody);
66 if(n > pl.tuba_lastnotes_cnt)
71 if(pl.tuba_instrument != instrument)
76 for(i = 0; i < n; ++i)
78 vector v = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
79 float ai = stof(argv(n - i - 1));
83 // n counts the last played notes BACKWARDS
87 if(ignorepitch && i == 0)
89 pitchshift = np - v.z;
93 if(v.z + pitchshift != np)
98 // now we know the right NOTES were played
104 mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
108 mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
110 mmax = 240; // you won't try THAT hard... (tempo 1)
111 //printf("initial tempo rules: %f %f\n", mmin, mmax);
113 for(i = 0; i < n; ++i)
115 vector vi = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
116 float ai = stof(argv(n - i - 1));
117 ti -= 1 / (ai - floor(ai));
119 for(j = i+1; j < n; ++j)
121 vector vj = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
122 float aj = stof(argv(n - j - 1));
123 tj -= (aj - floor(aj));
125 // note i should be at m*ti+b
126 // note j should be at m*tj+b
128 // we have a LINE l, so that
129 // vi_x <= l(ti) <= vi_y
130 // vj_x <= l(tj) <= vj_y
133 // vi_x <= vi_y <= vj_x <= vj_y
135 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
136 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
137 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
138 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
139 mmin = max(mmin, (vi.x - vj.y) / (ti - tj)); // lower bound
140 mmax = min(mmax, (vi.y - vj.x) / (ti - tj)); // upper bound
144 if(mmin > mmax) // rhythm fail
148 pl.tuba_lastnotes_cnt = 0;
153 void W_Tuba_NoteOff()
156 // on: self.spawnshieldtime
159 if(self.owner.tuba_note == self)
161 self.owner.tuba_lastnotes_last = (self.owner.tuba_lastnotes_last + 1) % MAX_TUBANOTES;
162 self.owner.(tuba_lastnotes[self.owner.tuba_lastnotes_last]) = eX * self.spawnshieldtime + eY * time + eZ * self.cnt;
163 self.owner.tuba_note = world;
164 self.owner.tuba_lastnotes_cnt = bound(0, self.owner.tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
167 s = trigger_magicear_processmessage_forallears(self.owner, 0, world, string_null);
170 // simulate a server message
171 switch(self.tuba_instrument)
175 bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
178 bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
181 bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
189 int W_Tuba_GetNote(entity pl, int hittype)
192 if (pl.movement.x < 0) movestate -= 3;
193 else if (pl.movement.x > 0) movestate += 3;
194 if (pl.movement.y < 0) movestate -= 1;
195 else if (pl.movement.y > 0) movestate += 1;
200 // layout: originally I wanted
204 // but then you only use forward and right key. So to make things more
205 // interesting, I swapped B with e#. Har har har...
209 case 1: note = -6; break; // Gb
210 case 2: note = -5; break; // G
211 case 3: note = -4; break; // G#
212 case 4: note = +5; break; // e#
214 case 5: note = 0; break; // c
215 case 6: note = +2; break; // d
216 case 7: note = +3; break; // eb
217 case 8: note = +4; break; // e
218 case 9: note = -1; break; // B
224 if(hittype & HITTYPE_SECONDARY)
227 // we support two kinds of tubas, those tuned in Eb and those tuned in C
228 // kind of tuba currently is player slot number, or team number if in
230 // that way, holes in the range of notes are "plugged"
233 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
238 if(pl.clientcolors & 1)
242 // total range of notes:
248 // *** ********************* ****
249 // -18.........................+12
250 // *** ********************* ****
251 // -18............................+15
252 // with jump: ... +24
257 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
262 if(!sound_allowed(MSG_ONE, self.realowner))
265 WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
266 WriteByte(MSG_ENTITY, sf);
269 WriteChar(MSG_ENTITY, self.cnt);
271 if(self.realowner != to)
273 f |= 2 * self.tuba_instrument;
274 WriteByte(MSG_ENTITY, f);
278 WriteCoord(MSG_ENTITY, self.origin.x);
279 WriteCoord(MSG_ENTITY, self.origin.y);
280 WriteCoord(MSG_ENTITY, self.origin.z);
285 void W_Tuba_NoteThink()
291 if(time > self.teleport_time)
296 self.nextthink = time;
297 dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
298 FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != self.realowner, LAMBDA(
299 v = self.origin - (it.origin + it.view_ofs);
300 vol0 = max(0, 1 - vlen(v) * dist_mult);
302 v = self.realowner.origin - (it.origin + it.view_ofs);
303 vol1 = max(0, 1 - vlen(v) * dist_mult);
305 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
307 setorigin(self, self.realowner.origin);
311 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
313 setorigin(self, self.realowner.origin);
320 void W_Tuba_NoteOn(float hittype)
325 W_SetupShot(self, false, 2, "", 0, WEP_CVAR(tuba, damage));
327 n = W_Tuba_GetNote(self, hittype);
330 if(self.tuba_instrument & 1)
331 hittype |= HITTYPE_SECONDARY;
332 if(self.tuba_instrument & 2)
333 hittype |= HITTYPE_BOUNCE;
337 if(self.tuba_note.cnt != n || self.tuba_note.tuba_instrument != self.tuba_instrument)
339 WITH(entity, self, self.tuba_note, W_Tuba_NoteOff());
345 self.tuba_note = spawn();
346 self.tuba_note.owner = self.tuba_note.realowner = self;
347 self.tuba_note.cnt = n;
348 self.tuba_note.tuba_instrument = self.tuba_instrument;
349 self.tuba_note.think = W_Tuba_NoteThink;
350 self.tuba_note.nextthink = time;
351 self.tuba_note.spawnshieldtime = time;
352 Net_LinkEntity(self.tuba_note, false, 0, W_Tuba_NoteSendEntity);
355 self.tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(); // so it can get prolonged safely
357 //sound(self, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
358 RadiusDamage(self, self, WEP_CVAR(tuba, damage), WEP_CVAR(tuba, edgedamage), WEP_CVAR(tuba, radius), world, world, WEP_CVAR(tuba, force), hittype | WEP_TUBA.m_id, world);
360 o = gettaginfo(self.exteriorweaponentity, 0);
361 if(time > self.tuba_smoketime)
363 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
364 self.tuba_smoketime = time + 0.25;
368 METHOD(Tuba, wr_aim, void(entity thiswep))
370 // bots cannot play the Tuba well yet
371 // I think they should start with the recorder first
372 if(vlen(self.origin - self.enemy.origin) < WEP_CVAR(tuba, radius))
375 self.BUTTON_ATCK = 1;
377 self.BUTTON_ATCK2 = 1;
380 METHOD(Tuba, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
383 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
386 //weapon_thinkf(actor, WFRAME_FIRE1, autocvar_g_balance_tuba_animtime, w_ready);
387 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
390 if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
392 W_Tuba_NoteOn(HITTYPE_SECONDARY);
393 //weapon_thinkf(actor, WFRAME_FIRE2, autocvar_g_balance_tuba_animtime, w_ready);
394 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
398 if(!(fire & 1) && !(fire & 2))
400 WITH(entity, self, actor.tuba_note, W_Tuba_NoteOff());
404 METHOD(Tuba, wr_setup, void(entity thiswep))
406 self.ammo_field = ammo_none;
407 self.tuba_instrument = 0;
409 METHOD(Tuba, wr_reload, void(entity thiswep))
411 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
412 // switch to alternate instruments :)
413 if(self.(weaponentity).state == WS_READY)
415 switch(self.tuba_instrument)
418 self.tuba_instrument = 1;
419 self.weaponname = "akordeon";
422 self.tuba_instrument = 2;
423 self.weaponname = "kleinbottle";
426 self.tuba_instrument = 0;
427 self.weaponname = "tuba";
430 W_SetupShot(self, false, 0, "", 0, 0);
431 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
432 self.(weaponentity).state = WS_INUSE;
433 weapon_thinkf(self, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
436 METHOD(Tuba, wr_checkammo1, bool(entity thiswep))
438 return true; // infinite ammo
440 METHOD(Tuba, wr_checkammo2, bool(entity thiswep))
442 return true; // tuba has infinite ammo
444 METHOD(Tuba, wr_suicidemessage, int(entity thiswep))
446 if(w_deathtype & HITTYPE_BOUNCE)
447 return WEAPON_KLEINBOTTLE_SUICIDE;
448 else if(w_deathtype & HITTYPE_SECONDARY)
449 return WEAPON_ACCORDEON_SUICIDE;
451 return WEAPON_TUBA_SUICIDE;
453 METHOD(Tuba, wr_killmessage, int(entity thiswep))
455 if(w_deathtype & HITTYPE_BOUNCE)
456 return WEAPON_KLEINBOTTLE_MURDER;
457 else if(w_deathtype & HITTYPE_SECONDARY)
458 return WEAPON_ACCORDEON_MURDER;
460 return WEAPON_TUBA_MURDER;
467 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
469 const int TUBA_MIN = -18;
470 const int TUBA_MAX = 27;
471 const int TUBA_INSTRUMENTS = 3;
474 class(Tuba) .int note;
475 class(Tuba) .bool tuba_attenuate;
476 class(Tuba) .float tuba_volume;
477 class(Tuba) .float tuba_volume_initial;
478 class(Tuba) .int tuba_instrument;
482 void tubasound(entity e, bool restart)
484 string snd1 = string_null;
485 if (Tuba_PitchStep) {
488 string snd2 = string_null;
492 int m = pymod(e.note, Tuba_PitchStep);
494 if (e.note - m < TUBA_MIN) {
496 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
498 speed1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
499 } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
501 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
503 speed1 = pow(2.0, m / 12.0);
506 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
508 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
509 speed1 = pow(2.0, m / 12.0);
511 snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
513 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
514 speed2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
516 } else if (restart) {
517 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
520 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
522 sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
526 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
528 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
532 void Ent_TubaNote_Think()
534 float f = autocvar_g_balance_tuba_fadetime;
536 self.tuba_volume -= frametime * self.tuba_volume_initial / f;
538 self.tuba_volume = 0;
540 self.nextthink = time;
541 if (self.tuba_volume <= 0) {
542 sound(self, CH_TUBA_SINGLE, SND_Null, 0, 0);
544 sound(self.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
553 void Ent_TubaNote_UpdateSound()
555 self.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
556 self.enemy.tuba_volume_initial = self.enemy.tuba_volume;
557 self.enemy.note = self.note;
558 self.enemy.tuba_instrument = self.tuba_instrument;
559 tubasound(self.enemy, 1);
562 void Ent_TubaNote_StopSound()
564 self.enemy.nextthink = time;
568 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
579 if (n != self.note || i != self.tuba_instrument || isNew) {
580 Ent_TubaNote_StopSound();
583 self.enemy = new(tuba_note);
584 if (Tuba_PitchStep) {
585 self.enemy.enemy = new(tuba_note_2);
590 self.enemy.tuba_attenuate = att;
594 self.tuba_instrument = i;
600 self.enemy.origin_x = ReadCoord();
601 self.enemy.origin_y = ReadCoord();
602 self.enemy.origin_z = ReadCoord();
603 setorigin(self.enemy, self.enemy.origin);
604 if (self.enemy.enemy) {
605 setorigin(self.enemy.enemy, self.enemy.origin);
609 self.think = Ent_TubaNote_StopSound;
610 self.entremove = Ent_TubaNote_StopSound;
611 self.enemy.think = Ent_TubaNote_Think;
612 self.enemy.nextthink = time + 10;
615 Ent_TubaNote_UpdateSound();
622 Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
623 if (Tuba_PitchStep) {
624 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
625 LOG_INFO("^1NOTE:^7 requested pitch shifting, but not supported by this engine build\n");
629 for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
630 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
631 for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
632 precache_sound(TUBA_STARTNOTE(i, n));