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