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