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