]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_tuba.qc
magicear: support tuba notes
[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                 //print(ftos(mmin), " ... ", ftos(mmax), "\n");
98
99                 if(mmin > mmax) // rhythm fail
100                         return FALSE;
101         }
102
103         pl.tuba_lastnotes_cnt = 0;
104
105         return TRUE;
106 }
107
108 void W_Tuba_NoteOff()
109 {
110         // we have a note:
111         //   on: self.spawnshieldtime
112         //   off: time
113         //   note: self.cnt
114         if(self.owner.tuba_note == self)
115         {
116                 self.owner.tuba_lastnotes_last = mod(self.owner.tuba_lastnotes_last + 1, MAX_TUBANOTES);
117                 self.owner.(tuba_lastnotes[self.owner.tuba_lastnotes_last]) = eX * self.spawnshieldtime + eY * time + eZ * self.cnt;
118                 self.owner.tuba_note = world;
119                 self.owner.tuba_lastnotes_cnt = bound(0, self.owner.tuba_lastnotes_cnt + 1, MAX_TUBANOTES);
120
121                 string s;
122                 s = trigger_magicear_processmessage_forallears(self.owner, 0, world, string_null);
123                 if(s != "")
124                 {
125                         // simulate a server message
126                         switch(self.tuba_instrument)
127                         {
128                                 default:
129                                 case 0: // Tuba
130                                         bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%%'n Tuba: ^7", s, "\n"));
131                                         break;
132                                 case 1:
133                                         bprint(strcat("\{1}\{13}* ^3", self.owner.netname, "^3 played on the @!#%%'n Accordeon: ^7", s, "\n"));
134                                         break;
135                         }
136                 }
137         }
138         remove(self);
139 }
140
141 float Tuba_GetNote(entity pl, float hittype)
142 {
143         float note;
144         float movestate;
145         movestate = 5;
146         if(pl.movement_x < 0) movestate -= 3;
147         if(pl.movement_x > 0) movestate += 3;
148         if(pl.movement_y < 0) movestate -= 1;
149         if(pl.movement_y > 0) movestate += 1;
150         switch(movestate)
151         {
152         // layout: originally I wanted
153         //   eb e  e#=f
154         //   B  c  d
155         //   Gb G  G#
156         // but then you only use forward and right key. So to make things more
157         // interesting, I swapped B with e#. Har har har...
158         //   eb e  B
159         // f=e# c  d
160         //   Gb G  G#
161                 case 1: note = -6; break; // Gb
162                 case 2: note = -5; break; // G
163                 case 3: note = -4; break; // G#
164                 case 4: note = +5; break; // e#
165                 case 5: note =  0; break; // c
166                 case 6: note = +2; break; // d
167                 case 7: note = +3; break; // eb
168                 case 8: note = +4; break; // e
169                 case 9: note = -1; break; // B
170         }
171         if(pl.BUTTON_CROUCH)
172                 note -= 12;
173         if(pl.BUTTON_JUMP)
174                 note += 12;
175         if(hittype & HITTYPE_SECONDARY)
176                 note += 7;
177         
178         // we support two kinds of tubas, those tuned in Eb and those tuned in C
179         // kind of tuba currently is player slot number, or team number if in
180         // teamplay
181         // that way, holes in the range of notes are "plugged"
182         if(teamplay)
183         {
184                 if(pl.team == COLOR_TEAM2 || pl.team == COLOR_TEAM4)
185                         note += 3;
186         }
187         else
188         {
189                 if(pl.clientcolors & 1)
190                         note += 3;
191         }
192         
193         // total range of notes:
194         //                       0
195         //                 ***  ** ****
196         //                        ***  ** ****
197         //     ***  ** ****
198         //            ***  ** ****
199         //     ***  ********************* ****
200         //     -18.........................+12
201         //        ***  ********************* ****
202         //     -18............................+15
203         //     with jump: ... +24
204         //     ... +27
205         return note;
206 }
207
208 float W_Tuba_NoteSendEntity(entity to, float sf)
209 {
210         float f;
211
212         msg_entity = to;
213         if(!sound_allowed(MSG_ONE, self.realowner))
214                 return FALSE;
215
216         WriteByte(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
217         WriteByte(MSG_ENTITY, sf);
218         if(sf & 1)
219         {
220                 WriteChar(MSG_ENTITY, self.cnt);
221                 f = 0;
222                 if(self.realowner != to)
223                         f |= 1;
224                 f |= 2 * self.tuba_instrument;
225                 WriteByte(MSG_ENTITY, f);
226         }
227         if(sf & 2)
228         {
229                 WriteCoord(MSG_ENTITY, self.origin_x);
230                 WriteCoord(MSG_ENTITY, self.origin_y);
231                 WriteCoord(MSG_ENTITY, self.origin_z);
232         }
233         return TRUE;
234 }
235
236 void W_Tuba_NoteThink()
237 {
238         float dist_mult;
239         float vol0, vol1;
240         vector dir0, dir1;
241         vector v;
242         entity e;
243         if(time > self.teleport_time)
244         {
245                 W_Tuba_NoteOff();
246                 return;
247         }
248         self.nextthink = time;
249         dist_mult = autocvar_g_balance_tuba_attenuation / autocvar_snd_soundradius;
250         FOR_EACH_REALCLIENT(e)
251         if(e != self.realowner)
252         {
253                 v = self.origin - (e.origin + e.view_ofs);
254                 vol0 = max(0, 1 - vlen(v) * dist_mult);
255                 dir0 = normalize(v);
256                 v = self.realowner.origin - (e.origin + e.view_ofs);
257                 vol1 = max(0, 1 - vlen(v) * dist_mult);
258                 dir1 = normalize(v);
259                 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
260                 {
261                         setorigin(self, self.realowner.origin);
262                         self.SendFlags |= 2;
263                         break;
264                 }
265                 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
266                 {
267                         setorigin(self, self.realowner.origin);
268                         self.SendFlags |= 2;
269                         break;
270                 }
271         }
272 }
273
274 void W_Tuba_NoteOn(float hittype)
275 {
276         vector o;
277         float n;
278
279         W_SetupShot(self, FALSE, 2, "", 0, autocvar_g_balance_tuba_damage);
280
281         n = Tuba_GetNote(self, hittype);
282
283         hittype = 0;
284         if(self.tuba_instrument & 1)
285                 hittype |= HITTYPE_SECONDARY;
286         if(self.tuba_instrument & 2)
287                 hittype |= HITTYPE_BOUNCE;
288         if(self.tuba_instrument & 4)
289                 hittype |= HITTYPE_HEADSHOT;
290
291         if(self.tuba_note)
292         {
293                 if(self.tuba_note.cnt != n || self.tuba_note.tuba_instrument != self.tuba_instrument)
294                 {
295                         entity oldself = self;
296                         self = self.tuba_note;
297                         W_Tuba_NoteOff();
298                         self = oldself;
299                 }
300         }
301
302         if not(self.tuba_note)
303         {
304                 self.tuba_note = spawn();
305                 self.tuba_note.owner = self.tuba_note.realowner = self;
306                 self.tuba_note.cnt = n;
307                 self.tuba_note.tuba_instrument = self.tuba_instrument;
308                 self.tuba_note.think = W_Tuba_NoteThink;
309                 self.tuba_note.nextthink = time;
310                 self.tuba_note.spawnshieldtime = time;
311                 Net_LinkEntity(self.tuba_note, FALSE, 0, W_Tuba_NoteSendEntity);
312         }
313
314         self.tuba_note.teleport_time = time + autocvar_g_balance_tuba_refire * 2 * W_WeaponRateFactor(); // so it can get prolonged safely
315
316         //sound(self, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), autocvar_g_balance_tuba_attenuation);
317         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);
318
319         o = gettaginfo(self.exteriorweaponentity, 0);
320         if(time > self.tuba_smoketime)
321         {
322                 pointparticles(particleeffectnum("smoke_ring"), o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
323                 self.tuba_smoketime = time + 0.25;
324         }
325 }
326
327 void spawnfunc_weapon_tuba (void)
328 {
329         weapon_defaultspawnfunc(WEP_TUBA);
330 }
331
332 float w_tuba(float req)
333 {
334         if (req == WR_AIM)
335         {
336                 // bots cannot play the Tuba well yet
337                 // I think they should start with the recorder first
338                 if(vlen(self.origin - self.enemy.origin) < autocvar_g_balance_tuba_radius)
339                 {
340                         if(random() > 0.5)
341                                 self.BUTTON_ATCK = 1;
342                         else
343                                 self.BUTTON_ATCK2 = 1;
344                 }
345         }
346         else if (req == WR_THINK)
347         {
348                 if (self.BUTTON_ATCK)
349                 if (weapon_prepareattack(0, autocvar_g_balance_tuba_refire))
350                 {
351                         W_Tuba_NoteOn(0);
352                         //weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_tuba_animtime, w_ready);
353                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_tuba_animtime, w_ready);
354                 }
355                 if (self.BUTTON_ATCK2)
356                 if (weapon_prepareattack(1, autocvar_g_balance_tuba_refire))
357                 {
358                         W_Tuba_NoteOn(HITTYPE_SECONDARY);
359                         //weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_tuba_animtime, w_ready);
360                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_tuba_animtime, w_ready);
361                 }
362                 if(self.tuba_note)
363                 {
364                         if(!self.BUTTON_ATCK && !self.BUTTON_ATCK2)
365                         {
366                                 entity oldself = self;
367                                 self = self.tuba_note;
368                                 W_Tuba_NoteOff();
369                                 self = oldself;
370                         }
371                 }
372         }
373         else if (req == WR_PRECACHE)
374         {
375                 precache_model ("models/weapons/g_tuba.md3");
376                 precache_model ("models/weapons/v_tuba.md3");
377                 precache_model ("models/weapons/h_tuba.iqm");
378                 precache_model ("models/weapons/g_akordeon.md3");
379                 precache_model ("models/weapons/v_akordeon.md3");
380                 precache_model ("models/weapons/h_akordeon.iqm");
381
382                 //float i;
383                 //for(i = -18; i <= +27; ++i)
384                 //      precache_sound(TUBA_NOTE(i));
385         }
386         else if (req == WR_SETUP)
387         {
388                 weapon_setup(WEP_TUBA);
389                 self.current_ammo = ammo_none;
390                 self.tuba_instrument = 0;
391         }
392         else if (req == WR_RELOAD)
393         {
394                 // switch to alternate instruments :)
395                 if(self.weaponentity.state == WS_READY)
396                 {
397                         switch(self.tuba_instrument)
398                         {
399                                 case 0:
400                                         self.tuba_instrument = 1;
401                                         self.weaponname = "akordeon";
402                                         break;
403                                 case 1:
404                                         self.tuba_instrument = 0;
405                                         self.weaponname = "tuba";
406                                         break;
407                         }
408                         W_SetupShot(self, FALSE, 0, "", 0, 0);
409                         pointparticles(particleeffectnum("teleport"), w_shotorg, '0 0 0', 1);
410                         self.weaponentity.state = WS_INUSE;
411                         weapon_thinkf(WFRAME_RELOAD, 0.5, w_ready);
412                 }
413         }
414         else if (req == WR_CHECKAMMO1)
415                 return TRUE; // TODO use fuel?
416         else if (req == WR_CHECKAMMO2)
417                 return TRUE; // TODO use fuel?
418         return TRUE;
419 }
420 #endif
421 #ifdef CSQC
422 float w_tuba(float req)
423 {
424         if(req == WR_IMPACTEFFECT)
425         {
426                 // nothing to do here; particles of tuba are handled differently
427         }
428         else if(req == WR_PRECACHE)
429         {
430                 // nothing to do
431         }
432         else if (req == WR_SUICIDEMESSAGE)
433         {
434                 float instr;
435                 instr = 0;
436                 if(w_deathtype & HITTYPE_SECONDARY)
437                         instr |= 1;
438                 if(w_deathtype & HITTYPE_BOUNCE)
439                         instr |= 2;
440                 if(w_deathtype & HITTYPE_HEADSHOT)
441                         instr |= 4;
442                 switch(instr)
443                 {
444                         default:
445                         case 0: // Tuba
446                                 w_deathtypestring = _("%s hurt his own ears with the @!#%%'n Tuba");
447                                 break;
448                         case 1: // Accordeon
449                                 w_deathtypestring = _("%s hurt his own ears with the @!#%%'n Accordeon");
450                                 break;
451                 }
452         }
453         else if (req == WR_KILLMESSAGE)
454         {
455                 float instr;
456                 instr = 0;
457                 if(w_deathtype & HITTYPE_SECONDARY)
458                         instr |= 1;
459                 if(w_deathtype & HITTYPE_BOUNCE)
460                         instr |= 2;
461                 if(w_deathtype & HITTYPE_HEADSHOT)
462                         instr |= 4;
463                 switch(instr)
464                 {
465                         default:
466                         case 0: // Tuba
467                                 w_deathtypestring = _("%s died of %s's great playing on the @!#%%'n Tuba");
468                                 break;
469                         case 1: // Accordeon
470                                 w_deathtypestring = _("%s died of %s's great playing on the @!#%%'n Accordeon");
471                                 break;
472                 }
473         }
474         return TRUE;
475 }
476 #endif
477 #endif