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