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