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