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