]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/tuba.qc
Optimize W_Tuba_NoteOn
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / tuba.qc
1 #include "tuba.qh"
2
3 #ifdef SVQC
4
5 .entity tuba_note;
6 .float tuba_smoketime;
7
8 #define MAX_TUBANOTES 32
9 .float tuba_lastnotes_last;
10 .float tuba_lastnotes_cnt; // over
11 .vector tuba_lastnotes[MAX_TUBANOTES];
12
13 bool W_Tuba_HasPlayed(entity pl, .entity weaponentity, string melody, int instrument, bool ignorepitch, float mintempo, float maxtempo)
14 {
15         float i, j, mmin, mmax, nolength;
16         float n = tokenize_console(melody);
17         if(n > pl.(weaponentity).tuba_lastnotes_cnt)
18                 return false;
19         float pitchshift = 0;
20
21         if(instrument >= 0)
22                 if(pl.(weaponentity).tuba_instrument != instrument)
23                         return false;
24
25         // verify notes...
26         nolength = false;
27         for(i = 0; i < n; ++i)
28         {
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));
31                 float np = floor(ai);
32                 if(ai == np)
33                         nolength = true;
34                 // n counts the last played notes BACKWARDS
35                 // _x is start
36                 // _y is end
37                 // _z is note pitch
38                 if(ignorepitch && i == 0)
39                 {
40                         pitchshift = np - v.z;
41                 }
42                 else
43                 {
44                         if(v.z + pitchshift != np)
45                                 return false;
46                 }
47         }
48
49         // now we know the right NOTES were played
50         if(!nolength)
51         {
52                 // verify rhythm...
53                 float ti = 0;
54                 if(maxtempo > 0)
55                         mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
56                 else
57                         mmin = 0;
58                 if(mintempo > 0)
59                         mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
60                 else
61                         mmax = 240; // you won't try THAT hard... (tempo 1)
62                 //printf("initial tempo rules: %f %f\n", mmin, mmax);
63
64                 for(i = 0; i < n; ++i)
65                 {
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));
69                         float tj = ti;
70                         for(j = i+1; j < n; ++j)
71                         {
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));
75
76                                 // note i should be at m*ti+b
77                                 // note j should be at m*tj+b
78                                 // so:
79                                 // we have a LINE l, so that
80                                 // vi_x <= l(ti) <= vi_y
81                                 // vj_x <= l(tj) <= vj_y
82                                 // what is m?
83
84                                 // vi_x <= vi_y <= vj_x <= vj_y
85                                 // ti <= tj
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
92                         }
93                 }
94
95                 if(mmin > mmax) // rhythm fail
96                         return false;
97         }
98
99         pl.(weaponentity).tuba_lastnotes_cnt = 0;
100
101         return true;
102 }
103
104 void W_Tuba_NoteOff(entity this)
105 {
106         entity actor = this.owner;
107         // we have a note:
108         //   on: this.spawnshieldtime
109         //   off: time
110         //   note: this.cnt
111         .entity weaponentity = this.weaponentity_fld;
112         if (actor.(weaponentity).tuba_note == this)
113         {
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);
118
119                 string s = trigger_magicear_processmessage_forallears(actor, 0, NULL, string_null);
120                 if (s != "")
121                 {
122                         // simulate a server message
123                         switch (this.tuba_instrument)
124                         {
125                                 default:
126                                 case 0: // Tuba
127                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
128                                         break;
129                                 case 1:
130                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
131                                         break;
132                                 case 2:
133                                         bprint(strcat("\{1}\{13}* ^3", actor.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
134                                         break;
135                         }
136                 }
137         }
138         delete(this);
139 }
140
141 int W_Tuba_GetNote(entity pl, int hittype)
142 {
143         float movestate = 5;
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;
148
149         int note = 0;
150         switch (movestate)
151         {
152         // layout: originally I wanted
153         //   eb e  e#=f
154         //   B  c  d
155         //   Gb G  G#
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...
158         //   eb e  B
159         // f=e# c  d
160         //   Gb G  G#
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#
165                 default:
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
171         }
172         if(PHYS_INPUT_BUTTON_CROUCH(pl))
173                 note -= 12;
174         if(PHYS_INPUT_BUTTON_JUMP(pl))
175                 note += 12;
176         if(hittype & HITTYPE_SECONDARY)
177                 note += 7;
178
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
181         // teamplay
182         // that way, holes in the range of notes are "plugged"
183         if(teamplay)
184         {
185                 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
186                         note += 3;
187         }
188         else
189         {
190                 if(pl.clientcolors & 1)
191                         note += 3;
192         }
193
194         // total range of notes:
195         //                       0
196         //                 ***  ** ****
197         //                        ***  ** ****
198         //     ***  ** ****
199         //            ***  ** ****
200         //     ***  ********************* ****
201         //     -18.........................+12
202         //        ***  ********************* ****
203         //     -18............................+15
204         //     with jump: ... +24
205         //     ... +27
206         return note;
207 }
208
209 bool W_Tuba_NoteSendEntity(entity this, entity to, int sf)
210 {
211         msg_entity = to;
212         if (!sound_allowed(MSG_ONE, this.realowner)) return false;
213
214         WriteHeader(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
215         WriteByte(MSG_ENTITY, sf);
216         if (sf & 1)
217         {
218                 WriteChar(MSG_ENTITY, this.cnt);
219                 int f = 0;
220                 f |= 1 * (this.realowner != to);
221                 f |= 2 * this.tuba_instrument;
222                 WriteByte(MSG_ENTITY, f);
223         }
224         if (sf & 2)
225         {
226                 WriteVector(MSG_ENTITY, this.origin);
227         }
228         return true;
229 }
230
231 void W_Tuba_NoteThink(entity this)
232 {
233         float dist_mult;
234         float vol0, vol1;
235         vector dir0, dir1;
236         vector v;
237         if(time > this.teleport_time)
238         {
239                 W_Tuba_NoteOff(this);
240                 return;
241         }
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);
247                 dir0 = normalize(v);
248                 v = this.realowner.origin - (it.origin + it.view_ofs);
249                 vol1 = max(0, 1 - vlen(v) * dist_mult);
250                 dir1 = normalize(v);
251                 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
252                 {
253                         setorigin(this, this.realowner.origin);
254                         this.SendFlags |= 2;
255                         break;
256                 }
257                 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
258                 {
259                         setorigin(this, this.realowner.origin);
260                         this.SendFlags |= 2;
261                         break;
262                 }
263         });
264 }
265
266 void W_Tuba_NoteOn(entity actor, .entity weaponentity, float hittype)
267 {
268         float n = W_Tuba_GetNote(actor, hittype);
269
270         hittype = HITTYPE_SOUND;
271         if(actor.(weaponentity).tuba_instrument & 1)
272                 hittype |= HITTYPE_SECONDARY;
273         if(actor.(weaponentity).tuba_instrument & 2)
274                 hittype |= HITTYPE_BOUNCE;
275
276         W_SetupShot(actor, weaponentity, false, 2, SND_Null, 0, WEP_CVAR(tuba, damage), hittype | WEP_TUBA.m_id);
277
278         if(actor.(weaponentity).tuba_note)
279         {
280                 if(actor.(weaponentity).tuba_note.cnt != n || actor.(weaponentity).tuba_note.tuba_instrument != actor.(weaponentity).tuba_instrument)
281                 {
282                         W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
283                 }
284         }
285
286         if(!actor.(weaponentity).tuba_note)
287         {
288                 entity note = new(tuba_note);
289                 note.weaponentity_fld = weaponentity;
290                 actor.(weaponentity).tuba_note = note;
291                 note.owner = note.realowner = actor;
292                 note.cnt = n;
293                 note.tuba_instrument = actor.(weaponentity).tuba_instrument;
294                 setthink(note, W_Tuba_NoteThink);
295                 note.nextthink = time;
296                 note.spawnshieldtime = time;
297                 Net_LinkEntity(note, false, 0, W_Tuba_NoteSendEntity);
298         }
299
300         actor.(weaponentity).tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(actor); // so it can get prolonged safely
301
302         //sound(actor, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
303         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);
304
305         if(time > actor.(weaponentity).tuba_smoketime)
306         {
307                 vector o = gettaginfo(actor.exteriorweaponentity, 0);
308                 Send_Effect(EFFECT_SMOKE_RING, o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
309                 actor.(weaponentity).tuba_smoketime = time + 0.25;
310         }
311 }
312 #endif
313
314 #ifdef SVQC
315 METHOD(Tuba, wr_aim, void(Tuba this, entity actor, .entity weaponentity))
316 {
317         // bots cannot play the Tuba well yet
318         // I think they should start with the recorder first
319         if (vdist((actor.origin - actor.enemy.origin), <, WEP_CVAR(tuba, radius)))
320         {
321                 if (random() > 0.5)
322                         PHYS_INPUT_BUTTON_ATCK(actor) = true;
323                 else
324                         PHYS_INPUT_BUTTON_ATCK2(actor) = true;
325         }
326 }
327
328 METHOD(Tuba, wr_think, void(Tuba this, entity actor, .entity weaponentity, int fire))
329 {
330         if (fire & 1)
331         if (weapon_prepareattack(this, actor, weaponentity, false, WEP_CVAR(tuba, refire)))
332         {
333                 W_Tuba_NoteOn(actor, weaponentity, 0);
334                 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
335         }
336         if (fire & 2)
337         if (weapon_prepareattack(this, actor, weaponentity, true, WEP_CVAR(tuba, refire)))
338         {
339                 W_Tuba_NoteOn(actor, weaponentity, HITTYPE_SECONDARY);
340                 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
341         }
342         if (actor.(weaponentity).tuba_note)
343         {
344                 if (!(fire & 1) && !(fire & 2))
345                 {
346                         W_Tuba_NoteOff(actor.(weaponentity).tuba_note);
347                 }
348         }
349 }
350
351 METHOD(Tuba, wr_setup, void(Tuba this, entity actor, .entity weaponentity))
352 {
353         actor.(weaponentity).tuba_instrument = 0;
354 }
355
356 METHOD(Tuba, wr_reload, void(Tuba this, entity actor, .entity weaponentity))
357 {
358         // switch to alternate instruments :)
359         if (actor.(weaponentity).state == WS_READY)
360         {
361                 switch (actor.(weaponentity).tuba_instrument)
362                 {
363                         case 0:
364                                 actor.(weaponentity).tuba_instrument = 1;
365                                 actor.(weaponentity).weaponname = "akordeon";
366                                 break;
367                         case 1:
368                                 actor.(weaponentity).tuba_instrument = 2;
369                                 actor.(weaponentity).weaponname = "kleinbottle";
370                                 break;
371                         case 2:
372                                 actor.(weaponentity).tuba_instrument = 0;
373                                 actor.(weaponentity).weaponname = "tuba";
374                                 break;
375                 }
376                 int hittype = 0;
377                 if(actor.(weaponentity).tuba_instrument & 1)
378                         hittype |= HITTYPE_SECONDARY;
379                 if(actor.(weaponentity).tuba_instrument & 2)
380                         hittype |= HITTYPE_BOUNCE;
381                 W_SetupShot(actor, weaponentity, false, 0, SND_Null, 0, 0, hittype | WEP_TUBA.m_id);
382                 Send_Effect(EFFECT_TELEPORT, w_shotorg, '0 0 0', 1);
383                 actor.(weaponentity).state = WS_INUSE;
384                 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, 0.5, w_ready);
385         }
386 }
387 #endif
388
389 #ifdef SVQC
390
391 // infinite ammo
392 METHOD(Tuba, wr_checkammo1, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
393 METHOD(Tuba, wr_checkammo2, bool(Tuba this, entity actor, .entity weaponentity)) { return true; }
394
395 METHOD(Tuba, wr_suicidemessage, Notification(Tuba this))
396 {
397         if (w_deathtype & HITTYPE_BOUNCE)
398                 return WEAPON_KLEINBOTTLE_SUICIDE;
399         else if (w_deathtype & HITTYPE_SECONDARY)
400                 return WEAPON_ACCORDEON_SUICIDE;
401         else
402                 return WEAPON_TUBA_SUICIDE;
403 }
404 METHOD(Tuba, wr_killmessage, Notification(Tuba this))
405 {
406         if (w_deathtype & HITTYPE_BOUNCE)
407                 return WEAPON_KLEINBOTTLE_MURDER;
408         else if (w_deathtype & HITTYPE_SECONDARY)
409                 return WEAPON_ACCORDEON_MURDER;
410         else
411                 return WEAPON_TUBA_MURDER;
412 }
413
414 #elif defined(CSQC)
415
416 METHOD(Tuba, wr_viewmodel, string(Tuba this, entity wep))
417 {
418         return (wep.tuba_instrument == 0) ? "tuba" :
419                (wep.tuba_instrument == 1) ? "akordeon" :
420                                             "kleinbottle";
421 }
422
423 #endif
424
425 #ifdef CSQC
426
427 #define TUBA_STARTNOTE(i, n) _Sound_fixpath(W_Sound(strcat("tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n))))
428
429 const int TUBA_MIN = -18;
430 const int TUBA_MAX = 27;
431 const int TUBA_INSTRUMENTS = 3;
432
433 int Tuba_PitchStep;
434
435 void tubasound(entity e, bool restart)
436 {
437         string snd1 = string_null;
438         if (Tuba_PitchStep) {
439                 float vol1 = 1;
440                 float speed1 = 1;
441                 string snd2 = string_null;
442                 float vol2 = 0;
443                 float speed2 = 1;
444
445                 int m = pymod(e.note, Tuba_PitchStep);
446                 if (m) {
447                         if (e.note - m < TUBA_MIN) {
448                                 if (restart) {
449                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
450                                 }
451                                 speed1 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
452                         } else if (e.note - m + Tuba_PitchStep > TUBA_MAX) {
453                                 if (restart) {
454                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
455                                 }
456                                 speed1 = (2.0 ** (m / 12.0));
457                         } else {
458                                 if (restart) {
459                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
460                                 }
461                                 vol1 = cos(M_PI_2 * m / Tuba_PitchStep);
462                                 speed1 = (2.0 ** (m / 12.0));
463                                 if (restart) {
464                                         snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
465                                 }
466                                 vol2 = sin(M_PI_2 * m / Tuba_PitchStep);
467                                 speed2 = (2.0 ** ((m - Tuba_PitchStep) / 12.0));
468                         }
469                 } else if (restart) {
470                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
471                 }
472
473                 sound7(e, CH_TUBA_SINGLE, snd1, e.tuba_volume * vol1, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed1, 0);
474                 if (vol2) {
475                         sound7(e.enemy, CH_TUBA_SINGLE, snd2, e.tuba_volume * vol2, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation, 100 * speed2, 0);
476                 }
477         } else {
478                 if (restart) {
479                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
480                 }
481                 _sound(e, CH_TUBA_SINGLE, snd1, e.tuba_volume, e.tuba_attenuate * autocvar_g_balance_tuba_attenuation);
482         }
483 }
484
485 void Ent_TubaNote_Think(entity this)
486 {
487         float f = autocvar_g_balance_tuba_fadetime;
488         if (f > 0) {
489                 this.tuba_volume -= frametime * this.tuba_volume_initial / f;
490         } else {
491                 this.tuba_volume = 0;
492         }
493         this.nextthink = time;
494         if (this.tuba_volume <= 0) {
495                 sound(this, CH_TUBA_SINGLE, SND_Null, 0, 0);
496                 if (this.enemy) {
497                         sound(this.enemy, CH_TUBA_SINGLE, SND_Null, 0, 0);
498                         delete(this.enemy);
499                 }
500                 delete(this);
501         } else {
502                 tubasound(this, 0);
503         }
504 }
505
506 void Ent_TubaNote_UpdateSound(entity this)
507 {
508         this.enemy.tuba_volume = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
509         this.enemy.tuba_volume_initial = this.enemy.tuba_volume;
510         this.enemy.note = this.note;
511         this.enemy.tuba_instrument = this.tuba_instrument;
512         tubasound(this.enemy, 1);
513 }
514
515 void Ent_TubaNote_StopSound(entity this)
516 {
517         this.enemy.nextthink = time;
518         this.enemy = NULL;
519 }
520
521 NET_HANDLE(ENT_CLIENT_TUBANOTE, bool isNew)
522 {
523         bool upd = false;
524         int f = ReadByte();
525         if (f & 1) {
526                 int n = ReadChar();
527                 int i = ReadByte();
528                 bool att = (i & 1);
529                 i >>= 1;
530
531                 if (this.enemy) {
532                         if (n != this.note || i != this.tuba_instrument || isNew) {
533                                 Ent_TubaNote_StopSound(this);
534                         }
535                 } else {
536                         this.enemy = new(tuba_note);
537                         if (Tuba_PitchStep) {
538                                 this.enemy.enemy = new(tuba_note_2);
539                         }
540                         isNew = true;
541                 }
542
543                 this.enemy.tuba_attenuate = att;
544
545                 if (isNew) {
546                         this.note = n;
547                         this.tuba_instrument = i;
548                         upd = true;
549                 }
550         }
551
552         if (f & 2) {
553                 this.enemy.origin = ReadVector();
554                 setorigin(this.enemy, this.enemy.origin);
555                 if (this.enemy.enemy) {
556                         setorigin(this.enemy.enemy, this.enemy.origin);
557                 }
558         }
559
560         setthink(this, Ent_TubaNote_StopSound);
561         this.entremove = Ent_TubaNote_StopSound;
562         setthink(this.enemy, Ent_TubaNote_Think);
563         this.enemy.nextthink = time + 10;
564
565         if (upd) {
566                 Ent_TubaNote_UpdateSound(this);
567         }
568         return true;
569 }
570
571 PRECACHE(Tuba)
572 {
573         Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
574         if (Tuba_PitchStep) {
575                 if (!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7")) {
576                         LOG_WARN("requested pitch shifting, but not supported by this engine build");
577                         Tuba_PitchStep = 0;
578                 }
579         }
580         for (int n = TUBA_MIN; n <= TUBA_MAX; ++n) {
581                 if (!Tuba_PitchStep || pymod(n, Tuba_PitchStep) == 0) {
582                         for (int i = 0; i < TUBA_INSTRUMENTS; ++i) {
583                                 precache_sound(TUBA_STARTNOTE(i, n));
584                         }
585                 }
586         }
587 }
588
589 #endif