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