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