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