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