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