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