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