bd0ae4e795552b15ff1deadf7036711207dd0365
[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 += WarpZone_RefSys_TransformOrigin(p, e, p.origin);
63                 avg_velocity += WarpZone_RefSys_TransformVelocity(p, e, 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(WarpZone_RefSys_TransformOrigin(p, e, p.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 = WarpZone_RefSys_TransformVelocity(e, p, 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 = WarpZone_RefSys_TransformVelocity(e, p, (targ_origin - WarpZone_RefSys_TransformOrigin(p, e, 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) && cvar("g_balance_crylink_primary_linkexplode"))
177         {
178                 W_Crylink_LinkExplode(self.queuenext, self);
179                 remove (self);
180                 return;
181         }
182         else if(finalhit)
183         {
184                 // just unlink
185                 W_Crylink_Dequeue(self);
186                 remove(self);
187                 return;
188         }
189         self.cnt = self.cnt - 1;
190         self.angles = vectoangles(self.velocity);
191         self.owner = world;
192         self.projectiledeathtype |= HITTYPE_BOUNCE;
193         // commented out as it causes a little hitch...
194         //if(proj.cnt == 0)
195         //      CSQCProjectile(proj, TRUE, PROJECTILE_CRYLINK, TRUE);
196 }
197
198 void W_Crylink_Touch2 (void)
199 {
200         float finalhit;
201         float f;
202         //PROJECTILE_TOUCH;
203         local entity savenext, saveprev, saveown;
204         savenext = self.queuenext;
205         saveprev = self.queueprev;
206         saveown = self.realowner;
207         if(WarpZone_Projectile_Touch())
208         {
209                 if(wasfreed(self))
210                         W_Crylink_Dequeue_Raw(saveown, saveprev, self, savenext);
211                 return;
212         }
213
214         float a;
215         a = 1 - (time - self.fade_time) * self.fade_rate;
216
217         finalhit = ((self.cnt <= 0) || (other.takedamage != DAMAGE_NO));
218         if(finalhit)
219                 f = 1;
220         else
221                 f = cvar("g_balance_crylink_secondary_bouncedamagefactor");
222         if(a)
223                 f *= a;
224         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) && cvar("g_balance_crylink_secondary_linkexplode"))
225         {
226                 W_Crylink_LinkExplode(self.queuenext, self);
227                 remove (self);
228                 return;
229         }
230         else if(finalhit)
231         {
232                 // just unlink
233                 W_Crylink_Dequeue(self);
234                 remove(self);
235                 return;
236         }
237         self.cnt = self.cnt - 1;
238         self.angles = vectoangles(self.velocity);
239         self.owner = world;
240         self.projectiledeathtype |= HITTYPE_BOUNCE;
241         // commented out as it causes a little hitch...
242         //if(proj.cnt == 0)
243         //      CSQCProjectile(proj, TRUE, PROJECTILE_CRYLINK, TRUE);
244 }
245
246 void W_Crylink_Fadethink (void)
247 {
248         W_Crylink_Dequeue(self);
249         remove(self);
250 }
251
252 void W_Crylink_Attack (void)
253 {
254         local float counter, shots;
255         local entity proj, prevproj, firstproj;
256         local vector s;
257         vector forward, right, up;
258
259         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
260                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_primary_ammo");
261
262         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire.wav", (cvar("g_balance_crylink_primary_damage")*cvar("g_balance_crylink_primary_shots")));
263         forward = v_forward;
264         right = v_right;
265         up = v_up;
266
267         shots = cvar("g_balance_crylink_primary_shots");
268         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
269         proj = world;
270         while (counter < shots)
271         {
272                 proj = spawn ();
273                 proj.realowner = proj.owner = self;
274                 proj.classname = "spike";
275                 proj.bot_dodge = TRUE;
276                 proj.bot_dodgerating = cvar("g_balance_crylink_primary_damage");
277                 if(counter == 0) { // first projectile, store in firstproj for now
278                         firstproj = proj;
279                 }
280                 else if(counter == shots - 1) { // last projectile, link up with first projectile
281                         prevproj.queuenext = proj;
282                         firstproj.queueprev = proj;
283                         proj.queuenext = firstproj;
284                         proj.queueprev = prevproj;
285                 }
286                 else { // else link up with previous projectile
287                         prevproj.queuenext = proj;
288                         proj.queueprev = prevproj;
289                 }
290
291                 prevproj = proj;
292
293                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
294                 PROJECTILE_MAKETRIGGER(proj);
295                 proj.projectiledeathtype = WEP_CRYLINK;
296                 //proj.gravity = 0.001;
297
298                 setorigin (proj, w_shotorg);
299                 setsize(proj, '0 0 0', '0 0 0');
300
301
302                 s = '0 0 0';
303                 if (counter == 0)
304                         s = '0 0 0';
305                 else
306                 {
307                         makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
308                         s_y = v_forward_x;
309                         s_z = v_forward_y;
310                 }
311                 s = s * cvar("g_balance_crylink_primary_spread") * g_weaponspreadfactor;
312                 W_SetupProjectileVelocityEx(proj, w_shotdir + right * s_y + up * s_z, v_up, cvar("g_balance_crylink_primary_speed"), 0, 0, 0);
313                 proj.touch = W_Crylink_Touch;
314
315                 proj.think = W_Crylink_Fadethink;
316                 if(counter == 0)
317                 {
318                         proj.fade_time = time + cvar("g_balance_crylink_primary_middle_lifetime");
319                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_middle_fadetime");
320                         proj.nextthink = time + cvar("g_balance_crylink_primary_middle_lifetime") + cvar("g_balance_crylink_primary_middle_fadetime");
321                 }
322                 else
323                 {
324                         proj.fade_time = time + cvar("g_balance_crylink_primary_other_lifetime");
325                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_other_fadetime");
326                         proj.nextthink = time + cvar("g_balance_crylink_primary_other_lifetime") + cvar("g_balance_crylink_primary_other_fadetime");
327                 }
328                 proj.cnt = cvar("g_balance_crylink_primary_bounces");
329                 //proj.scale = 1 + 1 * proj.cnt;
330
331                 proj.angles = vectoangles (proj.velocity);
332
333                 //proj.glow_size = 20;
334
335                 proj.flags = FL_PROJECTILE;
336
337                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
338
339                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
340
341                 counter = counter + 1;
342         }
343         self.crylink_lastgroup = proj;
344 }
345
346 void W_Crylink_Attack2 (void)
347 {
348         local float counter, shots;
349         local entity proj, prevproj, firstproj;
350
351         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
352                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_secondary_ammo");
353
354         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire2.wav", (cvar("g_balance_crylink_secondary_damage")*cvar("g_balance_crylink_secondary_shots")));
355
356         shots = cvar("g_balance_crylink_secondary_shots");
357         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
358         proj = world;
359         while (counter < shots)
360         {
361                 proj = spawn ();
362                 proj.realowner = proj.owner = self;
363                 proj.classname = "spike";
364                 proj.bot_dodge = TRUE;
365                 proj.bot_dodgerating = cvar("g_balance_crylink_secondary_damage");
366                 if(counter == 0) { // first projectile, store in firstproj for now
367                         firstproj = proj;
368                 }
369                 else if(counter == shots - 1) { // last projectile, link up with first projectile
370                         prevproj.queuenext = proj;
371                         firstproj.queueprev = proj;
372                         proj.queuenext = firstproj;
373                         proj.queueprev = prevproj;
374                 }
375                 else { // else link up with previous projectile
376                         prevproj.queuenext = proj;
377                         proj.queueprev = prevproj;
378                 }
379
380                 prevproj = proj;
381
382                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
383                 PROJECTILE_MAKETRIGGER(proj);
384                 proj.projectiledeathtype = WEP_CRYLINK | HITTYPE_SECONDARY;
385                 //proj.gravity = 0.001;
386
387                 setorigin (proj, w_shotorg);
388                 setsize(proj, '0 0 0', '0 0 0');
389
390                 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);
391                 proj.touch = W_Crylink_Touch2;
392                 proj.think = W_Crylink_Fadethink;
393                 if(counter == (shots - 1) / 2)
394                 {
395                         proj.fade_time = time + cvar("g_balance_crylink_secondary_middle_lifetime");
396                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_middle_fadetime");
397                         proj.nextthink = time + cvar("g_balance_crylink_secondary_middle_lifetime") + cvar("g_balance_crylink_secondary_middle_fadetime");
398                 }
399                 else
400                 {
401                         proj.fade_time = time + cvar("g_balance_crylink_secondary_line_lifetime");
402                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_line_fadetime");
403                         proj.nextthink = time + cvar("g_balance_crylink_secondary_line_lifetime") + cvar("g_balance_crylink_secondary_line_fadetime");
404                 }
405                 proj.cnt = cvar("g_balance_crylink_secondary_bounces");
406                 //proj.scale = 1 + 1 * proj.cnt;
407
408                 proj.angles = vectoangles (proj.velocity);
409
410                 //proj.glow_size = 20;
411
412                 proj.flags = FL_PROJECTILE;
413
414                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
415
416                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
417
418                 counter = counter + 1;
419         }
420         self.crylink_lastgroup = proj;
421 }
422
423 void spawnfunc_weapon_crylink (void)
424 {
425         weapon_defaultspawnfunc(WEP_CRYLINK);
426 }
427
428 float w_crylink(float req)
429 {
430         if (req == WR_AIM)
431         {
432                 if (random() > 0.15)
433                         self.BUTTON_ATCK = bot_aim(cvar("g_balance_crylink_primary_speed"), 0, cvar("g_balance_crylink_primary_middle_lifetime"), FALSE);
434                 else
435                         self.BUTTON_ATCK2 = bot_aim(cvar("g_balance_crylink_secondary_speed"), 0, cvar("g_balance_crylink_secondary_middle_lifetime"), FALSE);
436         }
437         else if (req == WR_THINK)
438         {
439                 if (self.BUTTON_ATCK)
440                 {
441                         if (!self.crylink_waitrelease)
442                         if (weapon_prepareattack(0, cvar("g_balance_crylink_primary_refire")))
443                         {
444                                 W_Crylink_Attack();
445                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_crylink_primary_animtime"), w_ready);
446                                 if(cvar("g_balance_crylink_primary_joinspeed") != 0)
447                                         self.crylink_waitrelease = 1;
448                         }
449                 }
450                 else if(self.BUTTON_ATCK2 && cvar("g_balance_crylink_secondary"))
451                 {
452                         if (!self.crylink_waitrelease)
453                         if (weapon_prepareattack(1, cvar("g_balance_crylink_secondary_refire")))
454                         {
455                                 W_Crylink_Attack2();
456                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_crylink_secondary_animtime"), w_ready);
457                                 if(cvar("g_balance_crylink_secondary_joinspeed") != 0)
458                                         self.crylink_waitrelease = 2;
459                         }
460                 }
461                 else
462                 {
463                         if (self.crylink_waitrelease)
464                         {
465                                 // fired and released now!
466                                 if(self.crylink_lastgroup)
467                                 {
468                                         vector pos;
469                                         if(self.crylink_waitrelease == 1)
470                                         {
471                                                 pos = W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_primary_joinspeed"));
472                                         }
473                                         else
474                                         {
475                                                 pos = W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_secondary_joinspeed"));
476                                         }
477
478                                         entity linkjoineffect;
479                                         linkjoineffect = spawn();
480                                         linkjoineffect.classname = "linkjoineffect";
481                                         linkjoineffect.think = W_Crylink_LinkJoinEffect_Think;
482                                         linkjoineffect.nextthink = time + w_crylink_linkjoin_time;
483                                         linkjoineffect.owner = self;
484                                         setorigin(linkjoineffect, pos);
485                                 }
486                                 self.crylink_waitrelease = 0;
487                                 if(!w_crylink(WR_CHECKAMMO1) && !w_crylink(WR_CHECKAMMO2))
488                                 {
489                                         // ran out of ammo!
490                                         self.cnt = WEP_CRYLINK;
491                                         self.switchweapon = w_getbestweapon(self);
492                                 }
493                         }
494                 }
495         }
496         else if (req == WR_PRECACHE)
497         {
498                 precache_model ("models/weapons/g_crylink.md3");
499                 precache_model ("models/weapons/v_crylink.md3");
500                 precache_model ("models/weapons/h_crylink.iqm");
501                 precache_sound ("weapons/crylink_fire.wav");
502                 precache_sound ("weapons/crylink_fire2.wav");
503                 precache_sound ("weapons/crylink_linkjoin.wav");
504         }
505         else if (req == WR_SETUP)
506                 weapon_setup(WEP_CRYLINK);
507         else if (req == WR_CHECKAMMO1)
508         {
509                 // don't "run out of ammo" and switch weapons while waiting for release
510                 if(self.crylink_lastgroup && self.crylink_waitrelease)
511                         return TRUE;
512                 return self.ammo_cells >= cvar("g_balance_crylink_primary_ammo");
513         }
514         else if (req == WR_CHECKAMMO2)
515         {
516                 // don't "run out of ammo" and switch weapons while waiting for release
517                 if(self.crylink_lastgroup && self.crylink_waitrelease)
518                         return TRUE;
519                 return self.ammo_cells >= cvar("g_balance_crylink_secondary_ammo");
520         }
521         return TRUE;
522 };
523 #endif
524 #ifdef CSQC
525 float w_crylink(float req)
526 {
527         if(req == WR_IMPACTEFFECT)
528         {
529                 vector org2;
530                 org2 = w_org + w_backoff * 2;
531                 if(w_deathtype & HITTYPE_SECONDARY)
532                 {
533                         pointparticles(particleeffectnum("crylink_impact"), org2, '0 0 0', 1);
534                         if(!w_issilent)
535                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact2.wav", VOL_BASE, ATTN_NORM);
536                 }
537                 else
538                 {
539                         pointparticles(particleeffectnum("crylink_impactbig"), org2, '0 0 0', 1);
540                         if(!w_issilent)
541                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact.wav", VOL_BASE, ATTN_NORM);
542                 }
543         }
544         else if(req == WR_PRECACHE)
545         {
546                 precache_sound("weapons/crylink_impact2.wav");
547                 precache_sound("weapons/crylink_impact.wav");
548         }
549         else if (req == WR_SUICIDEMESSAGE)
550         {
551                 w_deathtypestring = "%s succeeded at self-destructing themself with the Crylink";
552         }
553         else if (req == WR_KILLMESSAGE)
554         {
555                 if(w_deathtype & HITTYPE_BOUNCE)
556                         w_deathtypestring = "%s could not hide from %s's Crylink"; // unchecked: SPLASH (SECONDARY can't be)
557                 else if(w_deathtype & HITTYPE_SPLASH)
558                         w_deathtypestring = "%s was too close to %s's Crylink"; // unchecked: SECONDARY
559                 else
560                         w_deathtypestring = "%s took a close look at %s's Crylink"; // unchecked: SECONDARY
561         }
562         return TRUE;
563 }
564 #endif
565 #endif