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