]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_tuba.qc
Merge remote-tracking branch 'origin/mrbougo/clonefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_tuba.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ TUBA,
4 /* function  */ W_Tuba,
5 /* ammotype  */ ammo_none,
6 /* impulse   */ 1,
7 /* flags     */ WEP_FLAG_HIDDEN | WEP_TYPE_SPLASH,
8 /* rating    */ BOT_PICKUP_RATING_MID,
9 /* color     */ '0 1 0',
10 /* modelname */ "tuba",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairtuba",
13 /* wepimg    */ "weapontuba",
14 /* refname   */ "tuba",
15 /* xgettext:no-c-format */
16 /* wepname   */ _("@!#%'n Tuba")
17 );
18
19 #define TUBA_SETTINGS(w_cvar,w_prop) TUBA_SETTINGS_LIST(w_cvar, w_prop, TUBA, tuba)
20 #define TUBA_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
21         w_cvar(id, sn, NONE, animtime) \
22         w_cvar(id, sn, NONE, attenuation) \
23         w_cvar(id, sn, NONE, damage) \
24         w_cvar(id, sn, NONE, edgedamage) \
25         w_cvar(id, sn, NONE, fadetime) \
26         w_cvar(id, sn, NONE, force) \
27         w_cvar(id, sn, NONE, pitchstep) \
28         w_cvar(id, sn, NONE, radius) \
29         w_cvar(id, sn, NONE, refire) \
30         w_cvar(id, sn, NONE, volume) \
31         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
32         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
33         w_prop(id, sn, string, weaponreplace, weaponreplace) \
34         w_prop(id, sn, float,  weaponstart, weaponstart) \
35         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
36         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
37
38 #ifdef SVQC
39 TUBA_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
40 float W_Tuba_MarkClientOnlyFieldsAsUsed() {
41         // These variables are only used by client/tuba.qc. TODO: move client/tuba.qc code here.
42         return WEP_CVAR(tuba, fadetime) + WEP_CVAR(tuba, pitchstep) + WEP_CVAR(tuba, volume);
43 }
44
45 .entity tuba_note;
46 .float tuba_smoketime;
47 .float tuba_instrument;
48
49 #define MAX_TUBANOTES 32
50 .float tuba_lastnotes_last;
51 .float tuba_lastnotes_cnt; // over
52 .vector tuba_lastnotes[MAX_TUBANOTES];
53 #endif
54 #else
55 #ifdef SVQC
56 void spawnfunc_weapon_tuba(void) { weapon_defaultspawnfunc(WEP_TUBA); }
57
58 float W_Tuba_HasPlayed(entity pl, string melody, float instrument, float ignorepitch, float mintempo, float maxtempo)
59 {
60         float i, j, mmin, mmax, nolength;
61         float n = tokenize_console(melody);
62         if(n > pl.tuba_lastnotes_cnt)
63                 return FALSE;
64         float pitchshift = 0;
65
66         if(instrument >= 0)
67                 if(pl.tuba_instrument != instrument)
68                         return FALSE;
69
70         // verify notes...
71         nolength = FALSE;
72         for(i = 0; i < n; ++i)
73         {
74                 vector v = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
75                 float ai = stof(argv(n - i - 1));
76                 float np = floor(ai);
77                 if(ai == np)
78                         nolength = TRUE;
79                 // n counts the last played notes BACKWARDS
80                 // _x is start
81                 // _y is end
82                 // _z is note pitch
83                 if(ignorepitch && i == 0)
84                 {
85                         pitchshift = np - v_z;
86                 }
87                 else
88                 {
89                         if(v_z + pitchshift != np)
90                                 return FALSE;
91                 }
92         }
93
94         // now we know the right NOTES were played
95         if(!nolength)
96         {
97                 // verify rhythm...
98                 float ti = 0;
99                 if(maxtempo > 0)
100                         mmin = 240 / maxtempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
101                 else
102                         mmin = 0;
103                 if(mintempo > 0)
104                         mmax = 240 / mintempo; // 60 = "0.25 means 1 sec", at 120 0.5 means 1 sec, at 240 1 means 1 sec
105                 else
106                         mmax = 240; // you won't try THAT hard... (tempo 1)
107                 //printf("initial tempo rules: %f %f\n", mmin, mmax);
108
109                 for(i = 0; i < n; ++i)
110                 {
111                         vector vi = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - i + MAX_TUBANOTES) % MAX_TUBANOTES]);
112                         float ai = stof(argv(n - i - 1));
113                         ti -= 1 / (ai - floor(ai));
114                         float tj = ti;
115                         for(j = i+1; j < n; ++j)
116                         {
117                                 vector vj = pl.(tuba_lastnotes[(pl.tuba_lastnotes_last - j + MAX_TUBANOTES) % MAX_TUBANOTES]);
118                                 float aj = stof(argv(n - j - 1));
119                                 tj -= (aj - floor(aj));
120
121                                 // note i should be at m*ti+b
122                                 // note j should be at m*tj+b
123                                 // so:
124                                 // we have a LINE l, so that
125                                 // vi_x <= l(ti) <= vi_y
126                                 // vj_x <= l(tj) <= vj_y
127                                 // what is m?
128
129                                 // vi_x <= vi_y <= vj_x <= vj_y
130                                 // ti <= tj
131                                 //printf("first note: %f to %f, should be %f\n", vi_x, vi_y, ti);
132                                 //printf("second note: %f to %f, should be %f\n", vj_x, vj_y, tj);
133                                 //printf("m1 = %f\n", (vi_x - vj_y) / (ti - tj));
134                                 //printf("m2 = %f\n", (vi_y - vj_x) / (ti - tj));
135                                 mmin = max(mmin, (vi_x - vj_y) / (ti - tj)); // lower bound
136                                 mmax = min(mmax, (vi_y - vj_x) / (ti - tj)); // upper bound
137                         }
138                 }
139
140                 if(mmin > mmax) // rhythm fail
141                         return FALSE;
142         }
143
144         pl.tuba_lastnotes_cnt = 0;
145
146         return TRUE;
147 }
148
149 void W_Tuba_NoteOff(void)
150 {
151         // we have a note:
152         //   on: self.spawnshieldtime
153         //   off: time
154         //   note: self.cnt
155         if(self.owner.tuba_note == self)
156         {
157                 self.owner.tuba_lastnotes_last = (self.owner.tuba_lastnotes_last + 1) % MAX_TUBANOTES;
158                 self.owner.(tuba_lastnotes[self.owner.tuba_lastnotes_last]) = eX * self.spawnshieldtime + eY * time + eZ * self.cnt;
159                 self.owner.tuba_note = world;
160                 self.owner.tuba_lastnotes_cnt = bound(0, self.owner.tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
161
162                 string s;
163                 s = trigger_magicear_processmessage_forallears(self.owner, 0, world, string_null);
164                 if(s != "")
165                 {
166                         // simulate a server message
167                         switch(self.tuba_instrument)
168                         {
169                                 default:
170                                 case 0: // Tuba
171                                         bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Tuba: ^7", s, "\n"));
172                                         break;
173                                 case 1:
174                                         bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Accordeon: ^7", s, "\n"));
175                                         break;
176                                 case 2:
177                                         bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%'n Klein Bottle: ^7", s, "\n"));
178                                         break;
179                         }
180                 }
181         }
182         remove(self);
183 }
184
185 float W_Tuba_GetNote(entity pl, float hittype)
186 {
187         float note;
188         float movestate;
189         movestate = 5;
190         if(pl.movement_x < 0) movestate -= 3;
191         if(pl.movement_x > 0) movestate += 3;
192         if(pl.movement_y < 0) movestate -= 1;
193         if(pl.movement_y > 0) movestate += 1;
194 #ifdef GMQCC
195         note = 0;
196 #endif
197         switch(movestate)
198         {
199         // layout: originally I wanted
200         //   eb e  e#=f
201         //   B  c  d
202         //   Gb G  G#
203         // but then you only use forward and right key. So to make things more
204         // interesting, I swapped B with e#. Har har har...
205         //   eb e  B
206         // f=e# c  d
207         //   Gb G  G#
208                 case 1: note = -6; break; // Gb
209                 case 2: note = -5; break; // G
210                 case 3: note = -4; break; // G#
211                 case 4: note = +5; break; // e#
212                 default:
213                 case 5: note =  0; break; // c
214                 case 6: note = +2; break; // d
215                 case 7: note = +3; break; // eb
216                 case 8: note = +4; break; // e
217                 case 9: note = -1; break; // B
218         }
219         if(pl.BUTTON_CROUCH)
220                 note -= 12;
221         if(pl.BUTTON_JUMP)
222                 note += 12;
223         if(hittype & HITTYPE_SECONDARY)
224                 note += 7;
225
226         // we support two kinds of tubas, those tuned in Eb and those tuned in C
227         // kind of tuba currently is player slot number, or team number if in
228         // teamplay
229         // that way, holes in the range of notes are "plugged"
230         if(teamplay)
231         {
232                 if(pl.team == NUM_TEAM_2 || pl.team == NUM_TEAM_4)
233                         note += 3;
234         }
235         else
236         {
237                 if(pl.clientcolors & 1)
238                         note += 3;
239         }
240
241         // total range of notes:
242         //                       0
243         //                 ***  ** ****
244         //                        ***  ** ****
245         //     ***  ** ****
246         //            ***  ** ****
247         //     ***  ********************* ****
248         //     -18.........................+12
249         //        ***  ********************* ****
250         //     -18............................+15
251         //     with jump: ... +24
252         //     ... +27
253         return note;
254 }
255
256 float W_Tuba_NoteSendEntity(entity to, float sf)
257 {
258         float f;
259
260         msg_entity = to;
261         if(!sound_allowed(MSG_ONE, self.realowner))
262                 return FALSE;
263
264         WriteByte(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
265         WriteByte(MSG_ENTITY, sf);
266         if(sf & 1)
267         {
268                 WriteChar(MSG_ENTITY, self.cnt);
269                 f = 0;
270                 if(self.realowner != to)
271                         f |= 1;
272                 f |= 2 * self.tuba_instrument;
273                 WriteByte(MSG_ENTITY, f);
274         }
275         if(sf & 2)
276         {
277                 WriteCoord(MSG_ENTITY, self.origin_x);
278                 WriteCoord(MSG_ENTITY, self.origin_y);
279                 WriteCoord(MSG_ENTITY, self.origin_z);
280         }
281         return TRUE;
282 }
283
284 void W_Tuba_NoteThink(void)
285 {
286         float dist_mult;
287         float vol0, vol1;
288         vector dir0, dir1;
289         vector v;
290         entity e;
291         if(time > self.teleport_time)
292         {
293                 W_Tuba_NoteOff();
294                 return;
295         }
296         self.nextthink = time;
297         dist_mult = WEP_CVAR(tuba, attenuation) / autocvar_snd_soundradius;
298         FOR_EACH_REALCLIENT(e)
299         if(e != self.realowner)
300         {
301                 v = self.origin - (e.origin + e.view_ofs);
302                 vol0 = max(0, 1 - vlen(v) * dist_mult);
303                 dir0 = normalize(v);
304                 v = self.realowner.origin - (e.origin + e.view_ofs);
305                 vol1 = max(0, 1 - vlen(v) * dist_mult);
306                 dir1 = normalize(v);
307                 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
308                 {
309                         setorigin(self, self.realowner.origin);
310                         self.SendFlags |= 2;
311                         break;
312                 }
313                 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
314                 {
315                         setorigin(self, self.realowner.origin);
316                         self.SendFlags |= 2;
317                         break;
318                 }
319         }
320 }
321
322 void W_Tuba_NoteOn(float hittype)
323 {
324         vector o;
325         float n;
326
327         W_SetupShot(self, FALSE, 2, "", 0, WEP_CVAR(tuba, damage));
328
329         n = W_Tuba_GetNote(self, hittype);
330
331         hittype = 0;
332         if(self.tuba_instrument & 1)
333                 hittype |= HITTYPE_SECONDARY;
334         if(self.tuba_instrument & 2)
335                 hittype |= HITTYPE_BOUNCE;
336
337         if(self.tuba_note)
338         {
339                 if(self.tuba_note.cnt != n || self.tuba_note.tuba_instrument != self.tuba_instrument)
340                 {
341                         entity oldself = self;
342                         self = self.tuba_note;
343                         W_Tuba_NoteOff();
344                         self = oldself;
345                 }
346         }
347
348         if(!self.tuba_note)
349         {
350                 self.tuba_note = spawn();
351                 self.tuba_note.owner = self.tuba_note.realowner = self;
352                 self.tuba_note.cnt = n;
353                 self.tuba_note.tuba_instrument = self.tuba_instrument;
354                 self.tuba_note.think = W_Tuba_NoteThink;
355                 self.tuba_note.nextthink = time;
356                 self.tuba_note.spawnshieldtime = time;
357                 Net_LinkEntity(self.tuba_note, FALSE, 0, W_Tuba_NoteSendEntity);
358         }
359
360         self.tuba_note.teleport_time = time + WEP_CVAR(tuba, refire) * 2 * W_WeaponRateFactor(); // so it can get prolonged safely
361
362         //sound(self, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
363         RadiusDamage(self, self, WEP_CVAR(tuba, damage), WEP_CVAR(tuba, edgedamage), WEP_CVAR(tuba, radius), world, world, WEP_CVAR(tuba, force), hittype | WEP_TUBA, world);
364
365         o = gettaginfo(self.exteriorweaponentity, 0);
366         if(time > self.tuba_smoketime)
367         {
368                 pointparticles(particleeffectnum("smoke_ring"), o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
369                 self.tuba_smoketime = time + 0.25;
370         }
371 }
372
373 float W_Tuba(float req)
374 {
375         switch(req)
376         {
377                 case WR_AIM:
378                 {
379                         // bots cannot play the Tuba well yet
380                         // I think they should start with the recorder first
381                         if(vlen(self.origin - self.enemy.origin) < WEP_CVAR(tuba, radius))
382                         {
383                                 if(random() > 0.5)
384                                         self.BUTTON_ATCK = 1;
385                                 else
386                                         self.BUTTON_ATCK2 = 1;
387                         }
388                         
389                         return TRUE;
390                 }
391                 case WR_THINK:
392                 {
393                         if(self.BUTTON_ATCK)
394                         if(weapon_prepareattack(0, WEP_CVAR(tuba, refire)))
395                         {
396                                 W_Tuba_NoteOn(0);
397                                 //weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_tuba_animtime, w_ready);
398                                 weapon_thinkf(WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
399                         }
400                         if(self.BUTTON_ATCK2)
401                         if(weapon_prepareattack(1, WEP_CVAR(tuba, refire)))
402                         {
403                                 W_Tuba_NoteOn(HITTYPE_SECONDARY);
404                                 //weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_tuba_animtime, w_ready);
405                                 weapon_thinkf(WFRAME_IDLE, WEP_CVAR(tuba, animtime), w_ready);
406                         }
407                         if(self.tuba_note)
408                         {
409                                 if(!self.BUTTON_ATCK && !self.BUTTON_ATCK2)
410                                 {
411                                         entity oldself = self;
412                                         self = self.tuba_note;
413                                         W_Tuba_NoteOff();
414                                         self = oldself;
415                                 }
416                         }
417                         
418                         return TRUE;
419                 }
420                 case WR_INIT:
421                 {
422                         precache_model("models/weapons/g_tuba.md3");
423                         precache_model("models/weapons/v_tuba.md3");
424                         precache_model("models/weapons/h_tuba.iqm");
425                         precache_model("models/weapons/v_akordeon.md3");
426                         precache_model("models/weapons/h_akordeon.iqm");
427                         precache_model("models/weapons/v_kleinbottle.md3");
428                         precache_model("models/weapons/h_kleinbottle.iqm");
429                         TUBA_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
430                         return TRUE;
431                 }
432                 case WR_SETUP:
433                 {
434                         self.ammo_field = ammo_none;
435                         self.tuba_instrument = 0;
436                         return TRUE;
437                 }
438                 case WR_RELOAD:
439                 {
440                         // switch to alternate instruments :)
441                         if(self.weaponentity.state == WS_READY)
442                         {
443                                 switch(self.tuba_instrument)
444                                 {
445                                         case 0:
446                                                 self.tuba_instrument = 1;
447                                                 self.weaponname = "akordeon";
448                                                 break;
449                                         case 1:
450                                                 self.tuba_instrument = 2;
451                                                 self.weaponname = "kleinbottle";
452                                                 break;
453                                         case 2:
454                                                 self.tuba_instrument = 0;
455                                                 self.weaponname = "tuba";
456                                                 break;
457                                 }
458                                 W_SetupShot(self, FALSE, 0, "", 0, 0);
459                                 pointparticles(particleeffectnum("teleport"), w_shotorg, '0 0 0', 1);
460                                 self.weaponentity.state = WS_INUSE;
461                                 weapon_thinkf(WFRAME_RELOAD, 0.5, w_ready);
462                         }
463                         
464                         return TRUE;
465                 }
466                 case WR_CHECKAMMO1:
467                 case WR_CHECKAMMO2:
468                 {
469                         return TRUE; // tuba has infinite ammo
470                 }
471                 case WR_CONFIG:
472                 {
473                         TUBA_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
474                         return TRUE;
475                 }
476                 case WR_SUICIDEMESSAGE:
477                 {
478                         if(w_deathtype & HITTYPE_BOUNCE)
479                                 return WEAPON_KLEINBOTTLE_SUICIDE;
480                         else if(w_deathtype & HITTYPE_SECONDARY)
481                                 return WEAPON_ACCORDEON_SUICIDE;
482                         else
483                                 return WEAPON_TUBA_SUICIDE;
484                 }
485                 case WR_KILLMESSAGE:
486                 {
487                         if(w_deathtype & HITTYPE_BOUNCE)
488                                 return WEAPON_KLEINBOTTLE_MURDER;
489                         else if(w_deathtype & HITTYPE_SECONDARY)
490                                 return WEAPON_ACCORDEON_MURDER;
491                         else
492                                 return WEAPON_TUBA_MURDER;
493                 }
494         }
495         return FALSE;
496 }
497 #endif
498 #ifdef CSQC
499 float W_Tuba(float req)
500 {
501         // nothing to do here; particles of tuba are handled differently
502         // WEAPONTODO
503
504         switch(req)
505         {
506                 case WR_ZOOMRETICLE:
507                 {
508                         // no weapon specific image for this weapon
509                         return FALSE;
510                 }
511         }
512
513         return FALSE;
514 }
515 #endif
516 #endif