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