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