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