fix a missing sqrt
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_crylink.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(CRYLINK, w_crylink, IT_CELLS, 6, WEP_FLAG_NORMAL | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "crylink", "crylink", "Crylink");
3 #else
4 #ifdef SVQC
5 .float gravity;
6 .float crylink_waitrelease;
7 .entity crylink_lastgroup;
8
9 .entity queuenext;
10 .entity queueprev;
11
12 void W_Crylink_Dequeue_Raw(entity own, entity prev, entity me, entity next)
13 {
14         if(me == own.crylink_lastgroup)
15                 own.crylink_lastgroup = ((me == next) ? world : next);
16         prev.queuenext = next;
17         next.queueprev = prev;
18 }
19
20 void W_Crylink_Dequeue(entity e)
21 {
22         W_Crylink_Dequeue_Raw(e.realowner, e.queueprev, e, e.queuenext);
23 }
24
25 // force projectile to explode
26 void W_Crylink_LinkExplode (entity e, entity e2)
27 {
28         float a;
29         a = bound(0, 1 - (time - e.fade_time) * e.fade_rate, 1);
30
31         if(e == e.realowner.crylink_lastgroup)
32                 e.realowner.crylink_lastgroup = world;
33
34         RadiusDamage (e, e.realowner, cvar("g_balance_crylink_primary_damage") * a, cvar("g_balance_crylink_primary_edgedamage") * a, cvar("g_balance_crylink_primary_radius"), world, cvar("g_balance_crylink_primary_force") * a, e.projectiledeathtype, other);
35
36         if(e.queuenext != e2)
37                 W_Crylink_LinkExplode(e.queuenext, e2);
38
39         remove (e);
40 }
41
42 // adjust towards center
43 // returns the origin where they will meet... and the time till the meeting is
44 // stored in w_crylink_linkjoin_time.
45 // could possibly network this origin and time, and display a special particle
46 // effect when projectiles meet there :P
47 float w_crylink_linkjoin_time;
48 vector W_Crylink_LinkJoin(entity e, float joinspeed)
49 {
50         vector avg_origin, avg_velocity;
51         vector targ_origin;
52         float avg_dist, n;
53         entity p;
54
55         avg_origin = e.origin;
56         avg_velocity = e.velocity;
57         n = 1;
58         for(p = e; (p = p.queuenext) != e; )
59         {
60                 avg_origin += p.origin;
61                 avg_velocity += p.velocity;
62                 ++n;
63         }
64         avg_origin *= (1.0 / n);
65         avg_velocity *= (1.0 / n);
66
67         // yes, mathematically we can do this in ONE step, but beware of 32bit floats...
68         avg_dist = pow(vlen(e.origin - avg_origin), 2);
69         for(p = e; (p = p.queuenext) != e; )
70                 avg_dist += pow(vlen(e.origin - avg_origin), 2);
71         avg_dist *= (1.0 / n);
72         avg_dist = sqrt(avg_dist);
73
74         w_crylink_linkjoin_time = 0;
75         if(avg_dist == 0)
76                 return avg_origin; // no change needed
77
78         if(joinspeed == 0)
79         {
80                 e.velocity = avg_velocity;
81                 UpdateCSQCProjectile(e);
82                 for(p = e; (p = p.queuenext) != e; )
83                 {
84                         p.velocity = avg_velocity;
85                         UpdateCSQCProjectile(p);
86                 }
87         }
88         else
89         {
90                 w_crylink_linkjoin_time = avg_dist / joinspeed;
91                 targ_origin = avg_origin + w_crylink_linkjoin_time * avg_velocity;
92
93                 e.velocity = (targ_origin - e.origin) * (joinspeed / avg_dist);
94                 UpdateCSQCProjectile(e);
95                 for(p = e; (p = p.queuenext) != e; )
96                 {
97                         p.velocity = (targ_origin - p.origin) * (joinspeed / avg_dist);
98                         UpdateCSQCProjectile(p);
99                 }
100
101                 // analysis:
102                 //   joinspeed -> +infinity:
103                 //      w_crylink_linkjoin_time -> +0
104                 //      targ_origin -> avg_origin
105                 //      p->velocity -> HUEG towards center
106                 //   joinspeed -> 0:
107                 //      w_crylink_linkjoin_time -> +/- infinity
108                 //      targ_origin -> avg_velocity * +/- infinity
109                 //      p->velocity -> avg_velocity
110                 //   joinspeed -> -infinity:
111                 //      w_crylink_linkjoin_time -> -0
112                 //      targ_origin -> avg_origin
113                 //      p->velocity -> HUEG away from center
114         }
115
116         return targ_origin;
117 }
118
119 // NO bounce protection, as bounces are limited!
120 void W_Crylink_Touch (void)
121 {
122         float finalhit;
123         float f;
124         //PROJECTILE_TOUCH;
125         local entity savenext, saveprev, saveown;
126         saveown = self.realowner;
127         savenext = self.queuenext;
128         saveprev = self.queueprev;
129         if(WarpZone_Projectile_Touch())
130         {
131                 if(wasfreed(self))
132                         W_Crylink_Dequeue_Raw(saveown, saveprev, self, savenext);
133                 return;
134         }
135
136         float a;
137         a = bound(0, 1 - (time - self.fade_time) * self.fade_rate, 1);
138
139         finalhit = ((self.cnt <= 0) || (other.takedamage != DAMAGE_NO));
140         if(finalhit)
141                 f = 1;
142         else
143                 f = cvar("g_balance_crylink_primary_bouncedamagefactor");
144         if(a)
145                 f *= a;
146         if (RadiusDamage (self, self.realowner, cvar("g_balance_crylink_primary_damage") * f, cvar("g_balance_crylink_primary_edgedamage") * f, cvar("g_balance_crylink_primary_radius"), world, cvar("g_balance_crylink_primary_force") * f, self.projectiledeathtype, other) || finalhit)
147         {
148                 W_Crylink_LinkExplode(self.queuenext, self);
149                 remove (self);
150                 return;
151         }
152         self.cnt = self.cnt - 1;
153         self.angles = vectoangles(self.velocity);
154         self.owner = world;
155         self.projectiledeathtype |= HITTYPE_BOUNCE;
156         // commented out as it causes a little hitch...
157         //if(proj.cnt == 0)
158         //      CSQCProjectile(proj, TRUE, PROJECTILE_CRYLINK, TRUE);
159 }
160
161 void W_Crylink_Touch2 (void)
162 {
163         float finalhit;
164         float f;
165         //PROJECTILE_TOUCH;
166         local entity savenext, saveprev, saveown;
167         savenext = self.queuenext;
168         saveprev = self.queueprev;
169         saveown = self.realowner;
170         if(WarpZone_Projectile_Touch())
171         {
172                 if(wasfreed(self))
173                         W_Crylink_Dequeue_Raw(saveown, saveprev, self, savenext);
174                 return;
175         }
176
177         float a;
178         a = 1 - (time - self.fade_time) * self.fade_rate;
179
180         finalhit = ((self.cnt <= 0) || (other.takedamage != DAMAGE_NO));
181         if(finalhit)
182                 f = 1;
183         else
184                 f = cvar("g_balance_crylink_secondary_bouncedamagefactor");
185         if(a)
186                 f *= a;
187         if (RadiusDamage (self, self.realowner, cvar("g_balance_crylink_secondary_damage") * f, cvar("g_balance_crylink_secondary_edgedamage") * f, cvar("g_balance_crylink_secondary_radius"), world, cvar("g_balance_crylink_secondary_force") * f, self.projectiledeathtype, other))
188         {
189                 W_Crylink_LinkExplode(self.queuenext, self);
190                 remove (self);
191                 return;
192         }
193         else if(finalhit)
194         {
195                 // just unlink
196                 W_Crylink_Dequeue(self);
197                 remove(self);
198                 return;
199         }
200         self.cnt = self.cnt - 1;
201         self.angles = vectoangles(self.velocity);
202         self.owner = world;
203         self.projectiledeathtype |= HITTYPE_BOUNCE;
204         // commented out as it causes a little hitch...
205         //if(proj.cnt == 0)
206         //      CSQCProjectile(proj, TRUE, PROJECTILE_CRYLINK, TRUE);
207 }
208
209 void W_Crylink_Fadethink (void)
210 {
211         W_Crylink_Dequeue(self);
212         remove(self);
213 }
214
215 void W_Crylink_Attack (void)
216 {
217         local float counter, shots;
218         local entity proj, prevproj, firstproj;
219         local vector s;
220         vector forward, right, up;
221
222         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
223                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_primary_ammo");
224
225         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire.wav", (cvar("g_balance_crylink_primary_damage")*cvar("g_balance_crylink_primary_shots")));
226         forward = v_forward;
227         right = v_right;
228         up = v_up;
229
230         shots = cvar("g_balance_crylink_primary_shots");
231         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
232         proj = world;
233         while (counter < shots)
234         {
235                 proj = spawn ();
236                 proj.realowner = proj.owner = self;
237                 proj.classname = "spike";
238                 proj.bot_dodge = TRUE;
239                 proj.bot_dodgerating = cvar("g_balance_crylink_primary_damage");
240                 if(counter == 0) { // first projectile, store in firstproj for now
241                         firstproj = proj;
242                 }
243                 else if(counter == shots - 1) { // last projectile, link up with first projectile
244                         prevproj.queuenext = proj;
245                         firstproj.queueprev = proj;
246                         proj.queuenext = firstproj;
247                         proj.queueprev = prevproj;
248                 }
249                 else { // else link up with previous projectile
250                         prevproj.queuenext = proj;
251                         proj.queueprev = prevproj;
252                 }
253
254                 prevproj = proj;
255
256                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
257                 PROJECTILE_MAKETRIGGER(proj);
258                 proj.projectiledeathtype = WEP_CRYLINK;
259                 //proj.gravity = 0.001;
260
261                 setorigin (proj, w_shotorg);
262                 setsize(proj, '0 0 0', '0 0 0');
263
264
265                 s = '0 0 0';
266                 if (counter == 0)
267                         s = '0 0 0';
268                 else
269                 {
270                         makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
271                         s_y = v_forward_x;
272                         s_z = v_forward_y;
273                 }
274                 s = s * cvar("g_balance_crylink_primary_spread") * g_weaponspreadfactor;
275                 W_SetupProjectileVelocityEx(proj, w_shotdir + right * s_y + up * s_z, v_up, cvar("g_balance_crylink_primary_speed"), 0, 0, 0);
276                 proj.touch = W_Crylink_Touch;
277
278                 proj.think = W_Crylink_Fadethink;
279                 if(counter == 0)
280                 {
281                         proj.fade_time = time + cvar("g_balance_crylink_primary_middle_lifetime");
282                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_middle_fadetime");
283                         proj.nextthink = time + cvar("g_balance_crylink_primary_middle_lifetime") + cvar("g_balance_crylink_primary_middle_fadetime");
284                 }
285                 else if(counter <= 3)
286                 {
287                         proj.fade_time = time + cvar("g_balance_crylink_primary_star_lifetime");
288                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_star_fadetime");
289                         proj.nextthink = time + cvar("g_balance_crylink_primary_star_lifetime") + cvar("g_balance_crylink_primary_star_fadetime");
290                 }
291                 else
292                 {
293                         proj.fade_time = time + cvar("g_balance_crylink_primary_other_lifetime");
294                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_other_fadetime");
295                         proj.nextthink = time + cvar("g_balance_crylink_primary_other_lifetime") + cvar("g_balance_crylink_primary_other_fadetime");
296                 }
297                 proj.cnt = cvar("g_balance_crylink_primary_bounces");
298                 //proj.scale = 1 + 1 * proj.cnt;
299
300                 proj.angles = vectoangles (proj.velocity);
301
302                 //proj.glow_size = 20;
303
304                 proj.flags = FL_PROJECTILE;
305
306                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
307
308                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
309
310                 counter = counter + 1;
311         }
312         self.crylink_lastgroup = proj;
313 }
314
315 void W_Crylink_Attack2 (void)
316 {
317         local float counter, shots;
318         local entity proj, prevproj, firstproj;
319
320         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
321                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_secondary_ammo");
322
323         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire2.wav", (cvar("g_balance_crylink_secondary_damage")*cvar("g_balance_crylink_secondary_shots")));
324
325         shots = cvar("g_balance_crylink_secondary_shots");
326         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
327         proj = world;
328         while (counter < shots)
329         {
330                 proj = spawn ();
331                 proj.realowner = proj.owner = self;
332                 proj.classname = "spike";
333                 proj.bot_dodge = TRUE;
334                 proj.bot_dodgerating = cvar("g_balance_crylink_secondary_damage");
335                 if(counter == 0) { // first projectile, store in firstproj for now
336                         firstproj = proj;
337                 }
338                 else if(counter == shots - 1) { // last projectile, link up with first projectile
339                         prevproj.queuenext = proj;
340                         firstproj.queueprev = proj;
341                         proj.queuenext = firstproj;
342                         proj.queueprev = prevproj;
343                 }
344                 else { // else link up with previous projectile
345                         prevproj.queuenext = proj;
346                         proj.queueprev = prevproj;
347                 }
348
349                 prevproj = proj;
350
351                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
352                 PROJECTILE_MAKETRIGGER(proj);
353                 proj.projectiledeathtype = WEP_CRYLINK | HITTYPE_SECONDARY;
354                 //proj.gravity = 0.001;
355
356                 setorigin (proj, w_shotorg);
357                 setsize(proj, '0 0 0', '0 0 0');
358
359                 W_SetupProjectileVelocityEx(proj, (w_shotdir + (((counter + 0.5) / shots) * 2 - 1) * v_right * cvar("g_balance_crylink_secondary_spread") * g_weaponspreadfactor), v_up, cvar("g_balance_crylink_secondary_speed"), 0, 0, 0);
360                 proj.touch = W_Crylink_Touch2;
361                 proj.think = W_Crylink_Fadethink;
362                 if(counter == (shots - 1) / 2)
363                 {
364                         proj.fade_time = time + cvar("g_balance_crylink_secondary_middle_lifetime");
365                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_middle_fadetime");
366                         proj.nextthink = time + cvar("g_balance_crylink_secondary_middle_lifetime") + cvar("g_balance_crylink_secondary_middle_fadetime");
367                 }
368                 else
369                 {
370                         proj.fade_time = time + cvar("g_balance_crylink_secondary_line_lifetime");
371                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_line_fadetime");
372                         proj.nextthink = time + cvar("g_balance_crylink_secondary_line_lifetime") + cvar("g_balance_crylink_secondary_line_fadetime");
373                 }
374                 proj.cnt = cvar("g_balance_crylink_secondary_bounces");
375                 //proj.scale = 1 + 1 * proj.cnt;
376
377                 proj.angles = vectoangles (proj.velocity);
378
379                 //proj.glow_size = 20;
380
381                 proj.flags = FL_PROJECTILE;
382
383                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
384
385                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
386
387                 counter = counter + 1;
388         }
389         self.crylink_lastgroup = proj;
390 }
391
392 void spawnfunc_weapon_crylink (void)
393 {
394         weapon_defaultspawnfunc(WEP_CRYLINK);
395 }
396
397 float w_crylink(float req)
398 {
399         if (req == WR_AIM)
400         {
401                 if (random() > 0.15)
402                         self.BUTTON_ATCK = bot_aim(cvar("g_balance_crylink_primary_speed"), 0, cvar("g_balance_crylink_primary_middle_lifetime"), FALSE);
403                 else
404                         self.BUTTON_ATCK2 = bot_aim(cvar("g_balance_crylink_secondary_speed"), 0, cvar("g_balance_crylink_secondary_middle_lifetime"), FALSE);
405         }
406         else if (req == WR_THINK)
407         {
408                 if (self.BUTTON_ATCK)
409                 {
410                         if (!self.crylink_waitrelease)
411                         if (weapon_prepareattack(0, cvar("g_balance_crylink_primary_refire")))
412                         {
413                                 W_Crylink_Attack();
414                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_crylink_primary_animtime"), w_ready);
415                                 if(cvar("g_balance_crylink_primary_joinspeed") != 0)
416                                         self.crylink_waitrelease = 1;
417                         }
418                 }
419                 else if(self.BUTTON_ATCK2 && cvar("g_balance_crylink_secondary"))
420                 {
421                         if (!self.crylink_waitrelease)
422                         if (weapon_prepareattack(1, cvar("g_balance_crylink_secondary_refire")))
423                         {
424                                 W_Crylink_Attack2();
425                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_crylink_secondary_animtime"), w_ready);
426                                 if(cvar("g_balance_crylink_secondary_joinspeed") != 0)
427                                         self.crylink_waitrelease = 2;
428                         }
429                 }
430                 else
431                 {
432                         if (self.crylink_waitrelease)
433                         {
434                                 // fired and released now!
435                                 if(self.crylink_lastgroup)
436                                 {
437                                         if(self.crylink_waitrelease == 1)
438                                         {
439                                                 W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_primary_joinspeed"));
440                                         }
441                                         else
442                                         {
443                                                 W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_secondary_joinspeed"));
444                                         }
445                                 }
446                         }
447                         self.crylink_waitrelease = 0;
448                 }
449         }
450         else if (req == WR_PRECACHE)
451         {
452                 precache_model ("models/weapons/g_crylink.md3");
453                 precache_model ("models/weapons/v_crylink.md3");
454                 precache_model ("models/weapons/h_crylink.iqm");
455                 precache_sound ("weapons/crylink_fire.wav");
456                 precache_sound ("weapons/crylink_fire2.wav");
457         }
458         else if (req == WR_SETUP)
459                 weapon_setup(WEP_CRYLINK);
460         else if (req == WR_CHECKAMMO1)
461                 return self.ammo_cells >= cvar("g_balance_crylink_primary_ammo");
462         else if (req == WR_CHECKAMMO2)
463                 return self.ammo_cells >= cvar("g_balance_crylink_secondary_ammo");
464         return TRUE;
465 };
466 #endif
467 #ifdef CSQC
468 float w_crylink(float req)
469 {
470         if(req == WR_IMPACTEFFECT)
471         {
472                 vector org2;
473                 org2 = w_org + w_backoff * 2;
474                 if(w_deathtype & HITTYPE_SECONDARY)
475                 {
476                         pointparticles(particleeffectnum("crylink_impact"), org2, '0 0 0', 1);
477                         if(!w_issilent)
478                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact2.wav", VOL_BASE, ATTN_NORM);
479                 }
480                 else
481                 {
482                         pointparticles(particleeffectnum("crylink_impactbig"), org2, '0 0 0', 1);
483                         if(!w_issilent)
484                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact.wav", VOL_BASE, ATTN_NORM);
485                 }
486         }
487         else if(req == WR_PRECACHE)
488         {
489                 precache_sound("weapons/crylink_impact2.wav");
490                 precache_sound("weapons/crylink_impact.wav");
491         }
492         else if (req == WR_SUICIDEMESSAGE)
493         {
494                 w_deathtypestring = "%s succeeded at self-destructing themself with the Crylink";
495         }
496         else if (req == WR_KILLMESSAGE)
497         {
498                 if(w_deathtype & HITTYPE_BOUNCE)
499                         w_deathtypestring = "%s could not hide from %s's Crylink"; // unchecked: SPLASH (SECONDARY can't be)
500                 else if(w_deathtype & HITTYPE_SPLASH)
501                         w_deathtypestring = "%s was too close to %s's Crylink"; // unchecked: SECONDARY
502                 else
503                         w_deathtypestring = "%s took a close look at %s's Crylink"; // unchecked: SECONDARY
504         }
505         return TRUE;
506 }
507 #endif
508 #endif