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