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