]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_crylink.qc
No need for that extra prefix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_crylink.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id */ CRYLINK,
4 /* function */ w_crylink,
5 /* ammotype */ ammo_cells,
6 /* impulse  */ 6,
7 /* flags    */ WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_TYPE_SPLASH,
8 /* rating   */ BOT_PICKUP_RATING_MID,
9 /* model    */ "crylink",
10 /* netname  */ "crylink",
11 /* fullname */ _("Crylink")
12 );
13
14 #define CRYLINK_SETTINGS(w_cvar,w_prop) CRYLINK_SETTINGS_LIST(w_cvar, w_prop, CRYLINK, crylink)
15 #define CRYLINK_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
16         w_cvar(id, sn, BOTH, ammo) \
17         w_cvar(id, sn, BOTH, animtime) \
18         w_cvar(id, sn, BOTH, damage) \
19         w_cvar(id, sn, BOTH, edgedamage) \
20         w_cvar(id, sn, BOTH, radius) \
21         w_cvar(id, sn, BOTH, force) \
22         w_cvar(id, sn, BOTH, spread) \
23         w_cvar(id, sn, BOTH, refire) \
24         w_cvar(id, sn, BOTH, speed) \
25         w_cvar(id, sn, BOTH, shots) \
26         w_cvar(id, sn, BOTH, bounces) \
27         w_cvar(id, sn, BOTH, bouncedamagefactor) \
28         w_cvar(id, sn, BOTH, middle_lifetime) \
29         w_cvar(id, sn, BOTH, middle_fadetime) \
30         w_cvar(id, sn, BOTH, other_lifetime) \
31         w_cvar(id, sn, BOTH, other_fadetime) \
32         w_cvar(id, sn, BOTH, linkexplode) \
33         w_cvar(id, sn, BOTH, joindelay) \
34         w_cvar(id, sn, BOTH, joinspread) \
35         w_cvar(id, sn, BOTH, joinexplode) \
36         w_cvar(id, sn, BOTH, joinexplode_damage) \
37         w_cvar(id, sn, BOTH, joinexplode_edgedamage) \
38         w_cvar(id, sn, BOTH, joinexplode_radius) \
39         w_cvar(id, sn, BOTH, joinexplode_force) \
40         w_cvar(id, sn, SEC,  spreadtype) \
41         w_cvar(id, sn, NONE, secondary) \
42         w_prop(id, sn, float,  reloading_ammo, reload_ammo) \
43         w_prop(id, sn, float,  reloading_time, reload_time) \
44         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
45         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
46         w_prop(id, sn, string, weaponreplace, weaponreplace) \
47         w_prop(id, sn, float,  weaponstart, weaponstart) \
48         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride)
49
50 #ifdef SVQC
51 CRYLINK_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
52 .float gravity;
53 .float crylink_waitrelease;
54 .entity crylink_lastgroup;
55
56 .entity queuenext;
57 .entity queueprev;
58 #endif
59 #else
60 #ifdef SVQC
61 void spawnfunc_weapon_crylink() { weapon_defaultspawnfunc(WEP_CRYLINK); }
62
63 void W_Crylink_CheckLinks(entity e)
64 {
65         float i;
66         entity p;
67
68         if(e == world)
69                 error("W_Crylink_CheckLinks: entity is world");
70         if(e.classname != "spike" || wasfreed(e))
71                 error(sprintf("W_Crylink_CheckLinks: entity is not a spike but a %s (freed: %d)", e.classname, wasfreed(e)));
72
73         p = e;
74         for(i = 0; i < 1000; ++i)
75         {
76                 if(p.queuenext.queueprev != p || p.queueprev.queuenext != p)
77                         error("W_Crylink_CheckLinks: queue is inconsistent");
78                 p = p.queuenext;
79                 if(p == e)
80                         break;
81         }
82         if(i >= 1000)
83                 error("W_Crylink_CheckLinks: infinite chain");
84 }
85
86 void W_Crylink_Dequeue_Raw(entity own, entity prev, entity me, entity next)
87 {
88         W_Crylink_CheckLinks(next);
89         if(me == own.crylink_lastgroup)
90                 own.crylink_lastgroup = ((me == next) ? world : next);
91         prev.queuenext = next;
92         next.queueprev = prev;
93         me.classname = "spike_oktoremove";
94         if(me != next)
95                 W_Crylink_CheckLinks(next);
96 }
97
98 void W_Crylink_Dequeue(entity e)
99 {
100         W_Crylink_Dequeue_Raw(e.realowner, e.queueprev, e, e.queuenext);
101 }
102
103 void W_Crylink_Reset(void)
104 {
105         W_Crylink_Dequeue(self);
106         remove(self);
107 }
108
109 // force projectile to explode
110 void W_Crylink_LinkExplode (entity e, entity e2)
111 {
112         float a;
113
114         if(e == e2)
115                 return;
116
117         a = bound(0, 1 - (time - e.fade_time) * e.fade_rate, 1);
118
119         if(e == e.realowner.crylink_lastgroup)
120                 e.realowner.crylink_lastgroup = world;
121                 
122         float isprimary = !(e.projectiledeathtype & HITTYPE_SECONDARY);
123                 
124         RadiusDamage(e, e.realowner, WEP_CVAR_BOTH(crylink, isprimary, damage) * a, WEP_CVAR_BOTH(crylink, isprimary, edgedamage) * a, WEP_CVAR_BOTH(crylink, isprimary, radius), world, world, WEP_CVAR_BOTH(crylink, isprimary, force) * a, e.projectiledeathtype, other);
125
126         W_Crylink_LinkExplode(e.queuenext, e2);
127
128         e.classname = "spike_oktoremove";
129         remove (e);
130 }
131
132 // adjust towards center
133 // returns the origin where they will meet... and the time till the meeting is
134 // stored in w_crylink_linkjoin_time.
135 // could possibly network this origin and time, and display a special particle
136 // effect when projectiles meet there :P
137 // jspeed: joining speed (calculate this as join spread * initial speed)
138 float w_crylink_linkjoin_time;
139 vector W_Crylink_LinkJoin(entity e, float jspeed)
140 {
141         vector avg_origin, avg_velocity;
142         vector targ_origin;
143         float avg_dist, n;
144         entity p;
145
146         // FIXME remove this debug code
147         W_Crylink_CheckLinks(e);
148
149         w_crylink_linkjoin_time = 0;
150
151         avg_origin = e.origin;
152         avg_velocity = e.velocity;
153         n = 1;
154         for(p = e; (p = p.queuenext) != e; )
155         {
156                 avg_origin += WarpZone_RefSys_TransformOrigin(p, e, p.origin);
157                 avg_velocity += WarpZone_RefSys_TransformVelocity(p, e, p.velocity);
158                 ++n;
159         }
160         avg_origin *= (1.0 / n);
161         avg_velocity *= (1.0 / n);
162
163         if(n < 2)
164                 return avg_origin; // nothing to do
165
166         // yes, mathematically we can do this in ONE step, but beware of 32bit floats...
167         avg_dist = pow(vlen(e.origin - avg_origin), 2);
168         for(p = e; (p = p.queuenext) != e; )
169                 avg_dist += pow(vlen(WarpZone_RefSys_TransformOrigin(p, e, p.origin) - avg_origin), 2);
170         avg_dist *= (1.0 / n);
171         avg_dist = sqrt(avg_dist);
172
173         if(avg_dist == 0)
174                 return avg_origin; // no change needed
175
176         if(jspeed == 0)
177         {
178                 e.velocity = avg_velocity;
179                 UpdateCSQCProjectile(e);
180                 for(p = e; (p = p.queuenext) != e; )
181                 {
182                         p.velocity = WarpZone_RefSys_TransformVelocity(e, p, avg_velocity);
183                         UpdateCSQCProjectile(p);
184                 }
185                 targ_origin = avg_origin + 1000000000 * normalize(avg_velocity); // HUUUUUUGE
186         }
187         else
188         {
189                 w_crylink_linkjoin_time = avg_dist / jspeed;
190                 targ_origin = avg_origin + w_crylink_linkjoin_time * avg_velocity;
191
192                 e.velocity = (targ_origin - e.origin) * (1.0 / w_crylink_linkjoin_time);
193                 UpdateCSQCProjectile(e);
194                 for(p = e; (p = p.queuenext) != e; )
195                 {
196                         p.velocity = WarpZone_RefSys_TransformVelocity(e, p, (targ_origin - WarpZone_RefSys_TransformOrigin(p, e, p.origin)) * (1.0 / w_crylink_linkjoin_time));
197                         UpdateCSQCProjectile(p);
198                 }
199
200                 // analysis:
201                 //   jspeed -> +infinity:
202                 //      w_crylink_linkjoin_time -> +0
203                 //      targ_origin -> avg_origin
204                 //      p->velocity -> HUEG towards center
205                 //   jspeed -> 0:
206                 //      w_crylink_linkjoin_time -> +/- infinity
207                 //      targ_origin -> avg_velocity * +/- infinity
208                 //      p->velocity -> avg_velocity
209                 //   jspeed -> -infinity:
210                 //      w_crylink_linkjoin_time -> -0
211                 //      targ_origin -> avg_origin
212                 //      p->velocity -> HUEG away from center
213         }
214
215         W_Crylink_CheckLinks(e);
216
217         return targ_origin;
218 }
219
220 void W_Crylink_LinkJoinEffect_Think()
221 {
222         // is there at least 2 projectiles very close?
223         entity e, p;
224         float n;
225         e = self.owner.crylink_lastgroup;
226         n = 0;
227         if(e)
228         {
229                 if(vlen(e.origin - self.origin) < vlen(e.velocity) * frametime)
230                         ++n;
231                 for(p = e; (p = p.queuenext) != e; )
232                 {
233                         if(vlen(p.origin - self.origin) < vlen(p.velocity) * frametime)
234                                 ++n;
235                 }
236                 if(n >= 2)
237                 {
238                         float isprimary = !(e.projectiledeathtype & HITTYPE_SECONDARY);
239                         
240                         if(WEP_CVAR_BOTH(crylink, isprimary, joinexplode))
241                         {
242                                 n /= WEP_CVAR_BOTH(crylink, isprimary, shots);
243                                 RadiusDamage(
244                                         e,
245                                         e.realowner,
246                                         WEP_CVAR_BOTH(crylink, isprimary, joinexplode_damage) * n,
247                                         WEP_CVAR_BOTH(crylink, isprimary, joinexplode_edgedamage) * n,
248                                         WEP_CVAR_BOTH(crylink, isprimary, joinexplode_radius) * n,
249                                         e.realowner,
250                                         world,
251                                         WEP_CVAR_BOTH(crylink, isprimary, joinexplode_force) * n,
252                                         e.projectiledeathtype,
253                                         other
254                                 );
255                                 pointparticles(particleeffectnum("crylink_joinexplode"), self.origin, '0 0 0', n);
256                         }
257                 }
258         }
259         remove(self);
260 }
261
262 float W_Crylink_Touch_WouldHitFriendly(entity projectile, float rad)
263 {
264         entity head = WarpZone_FindRadius((projectile.origin + (projectile.mins + projectile.maxs) * 0.5), rad + MAX_DAMAGEEXTRARADIUS, FALSE);
265         float hit_friendly = 0;
266         float hit_enemy = 0;
267
268         while(head)
269         {
270                 if((head.takedamage != DAMAGE_NO) && (head.deadflag == DEAD_NO))
271                 {
272                         if(SAME_TEAM(head, projectile.realowner))
273                                 ++hit_friendly;
274                         else
275                                 ++hit_enemy;
276                 }
277                         
278                 head = head.chain;
279         }
280
281         return (hit_enemy ? FALSE : hit_friendly);
282 }
283
284 // NO bounce protection, as bounces are limited!
285 void W_Crylink_Touch (void)
286 {
287         float finalhit;
288         float f;
289         float isprimary = !(self.projectiledeathtype & HITTYPE_SECONDARY);
290         PROJECTILE_TOUCH;
291
292         float a;
293         a = bound(0, 1 - (time - self.fade_time) * self.fade_rate, 1);
294
295         finalhit = ((self.cnt <= 0) || (other.takedamage != DAMAGE_NO));
296         if(finalhit)
297                 f = 1;
298         else
299                 f = WEP_CVAR_BOTH(crylink, isprimary, bouncedamagefactor);
300         if(a)
301                 f *= a;
302
303         float totaldamage = RadiusDamage(self, self.realowner, WEP_CVAR_BOTH(crylink, isprimary, damage) * f, WEP_CVAR_BOTH(crylink, isprimary, edgedamage) * f, WEP_CVAR_BOTH(crylink, isprimary, radius), world, world, WEP_CVAR_BOTH(crylink, isprimary, force) * f, self.projectiledeathtype, other);
304                 
305         if(totaldamage && ((WEP_CVAR_BOTH(crylink, isprimary, linkexplode) == 2) || ((WEP_CVAR_BOTH(crylink, isprimary, linkexplode) == 1) && !W_Crylink_Touch_WouldHitFriendly(self, WEP_CVAR_BOTH(crylink, isprimary, radius)))))
306         {
307                 if(self == self.realowner.crylink_lastgroup)
308                         self.realowner.crylink_lastgroup = world;
309                 W_Crylink_LinkExplode(self.queuenext, self);
310                 self.classname = "spike_oktoremove";
311                 remove (self);
312                 return;
313         }
314         else if(finalhit)
315         {
316                 // just unlink
317                 W_Crylink_Dequeue(self);
318                 remove(self);
319                 return;
320         }
321         self.cnt = self.cnt - 1;
322         self.angles = vectoangles(self.velocity);
323         self.owner = world;
324         self.projectiledeathtype |= HITTYPE_BOUNCE;
325         // commented out as it causes a little hitch...
326         //if(proj.cnt == 0)
327         //      CSQCProjectile(proj, TRUE, PROJECTILE_CRYLINK, TRUE);
328 }
329
330 void W_Crylink_Fadethink (void)
331 {
332         W_Crylink_Dequeue(self);
333         remove(self);
334 }
335
336 void W_Crylink_Attack (void)
337 {
338         float counter, shots;
339         entity proj, prevproj, firstproj;
340         vector s;
341         vector forward, right, up;
342         float maxdmg;
343
344         W_DecreaseAmmo(WEP_CVAR_PRI(crylink, ammo));
345
346         maxdmg = WEP_CVAR_PRI(crylink, damage) * WEP_CVAR_PRI(crylink, shots);
347         maxdmg *= 1 + WEP_CVAR_PRI(crylink, bouncedamagefactor) * WEP_CVAR_PRI(crylink, bounces);
348         if(WEP_CVAR_PRI(crylink, joinexplode))
349                 maxdmg += WEP_CVAR_PRI(crylink, joinexplode_damage);
350
351         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire.wav", CH_WEAPON_A, maxdmg);
352         forward = v_forward;
353         right = v_right;
354         up = v_up;
355
356         shots = WEP_CVAR_PRI(crylink, shots);
357         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
358         proj = prevproj = firstproj = world;
359         for(counter = 0; counter < shots; ++counter)
360         {
361                 proj = spawn ();
362                 proj.reset = W_Crylink_Reset;
363                 proj.realowner = proj.owner = self;
364                 proj.classname = "spike";
365                 proj.bot_dodge = TRUE;
366                 proj.bot_dodgerating = WEP_CVAR_PRI(crylink, damage);
367                 if(shots == 1) {
368                         proj.queuenext = proj;
369                         proj.queueprev = proj;
370                 }
371                 else if(counter == 0) { // first projectile, store in firstproj for now
372                         firstproj = proj;
373                 }
374                 else if(counter == shots - 1) { // last projectile, link up with first projectile
375                         prevproj.queuenext = proj;
376                         firstproj.queueprev = proj;
377                         proj.queuenext = firstproj;
378                         proj.queueprev = prevproj;
379                 }
380                 else { // else link up with previous projectile
381                         prevproj.queuenext = proj;
382                         proj.queueprev = prevproj;
383                 }
384
385                 prevproj = proj;
386
387                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
388                 PROJECTILE_MAKETRIGGER(proj);
389                 proj.projectiledeathtype = WEP_CRYLINK;
390                 //proj.gravity = 0.001;
391
392                 setorigin (proj, w_shotorg);
393                 setsize(proj, '0 0 0', '0 0 0');
394
395
396                 s = '0 0 0';
397                 if (counter == 0)
398                         s = '0 0 0';
399                 else
400                 {
401                         makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
402                         s_y = v_forward_x;
403                         s_z = v_forward_y;
404                 }
405                 s = s * WEP_CVAR_PRI(crylink, spread) * g_weaponspreadfactor;
406                 W_SetupProjectileVelocityEx(proj, w_shotdir + right * s_y + up * s_z, v_up, WEP_CVAR_PRI(crylink, speed), 0, 0, 0, FALSE);
407                 proj.touch = W_Crylink_Touch;
408
409                 proj.think = W_Crylink_Fadethink;
410                 if(counter == 0)
411                 {
412                         proj.fade_time = time + WEP_CVAR_PRI(crylink, middle_lifetime);
413                         proj.fade_rate = 1 / WEP_CVAR_PRI(crylink, middle_fadetime);
414                         proj.nextthink = time + WEP_CVAR_PRI(crylink, middle_lifetime) + WEP_CVAR_PRI(crylink, middle_fadetime);
415                 }
416                 else
417                 {
418                         proj.fade_time = time + WEP_CVAR_PRI(crylink, other_lifetime);
419                         proj.fade_rate = 1 / WEP_CVAR_PRI(crylink, other_fadetime);
420                         proj.nextthink = time + WEP_CVAR_PRI(crylink, other_lifetime) + WEP_CVAR_PRI(crylink, other_fadetime);
421                 }
422                 proj.teleport_time = time + WEP_CVAR_PRI(crylink, joindelay);
423                 proj.cnt = WEP_CVAR_PRI(crylink, bounces);
424                 //proj.scale = 1 + 1 * proj.cnt;
425
426                 proj.angles = vectoangles (proj.velocity);
427
428                 //proj.glow_size = 20;
429
430                 proj.flags = FL_PROJECTILE;
431                 proj.missile_flags = MIF_SPLASH;
432     
433                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
434
435                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
436         }
437         if(WEP_CVAR_PRI(crylink, joinspread) != 0)
438         {
439                 self.crylink_lastgroup = proj;
440                 W_Crylink_CheckLinks(proj);
441                 self.crylink_waitrelease = 1;
442         }
443 }
444
445 void W_Crylink_Attack2 (void)
446 {
447         float counter, shots;
448         entity proj, prevproj, firstproj;
449         vector s;
450         vector forward, right, up;
451         float maxdmg;
452
453         W_DecreaseAmmo(WEP_CVAR_SEC(crylink, ammo));
454
455         maxdmg = WEP_CVAR_SEC(crylink, damage) * WEP_CVAR_SEC(crylink, shots);
456         maxdmg *= 1 + WEP_CVAR_SEC(crylink, bouncedamagefactor) * WEP_CVAR_SEC(crylink, bounces);
457         if(WEP_CVAR_SEC(crylink, joinexplode))
458                 maxdmg += WEP_CVAR_SEC(crylink, joinexplode_damage);
459
460         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire2.wav", CH_WEAPON_A, maxdmg);
461         forward = v_forward;
462         right = v_right;
463         up = v_up;
464
465         shots = WEP_CVAR_SEC(crylink, shots);
466         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
467         proj = prevproj = firstproj = world;
468         for(counter = 0; counter < shots; ++counter)
469         {
470                 proj = spawn ();
471                 proj.reset = W_Crylink_Reset;
472                 proj.realowner = proj.owner = self;
473                 proj.classname = "spike";
474                 proj.bot_dodge = TRUE;
475                 proj.bot_dodgerating = WEP_CVAR_SEC(crylink, damage);
476                 if(shots == 1) {
477                         proj.queuenext = proj;
478                         proj.queueprev = proj;
479                 }
480                 else if(counter == 0) { // first projectile, store in firstproj for now
481                         firstproj = proj;
482                 }
483                 else if(counter == shots - 1) { // last projectile, link up with first projectile
484                         prevproj.queuenext = proj;
485                         firstproj.queueprev = proj;
486                         proj.queuenext = firstproj;
487                         proj.queueprev = prevproj;
488                 }
489                 else { // else link up with previous projectile
490                         prevproj.queuenext = proj;
491                         proj.queueprev = prevproj;
492                 }
493
494                 prevproj = proj;
495
496                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
497                 PROJECTILE_MAKETRIGGER(proj);
498                 proj.projectiledeathtype = WEP_CRYLINK | HITTYPE_SECONDARY;
499                 //proj.gravity = 0.001;
500
501                 setorigin (proj, w_shotorg);
502                 setsize(proj, '0 0 0', '0 0 0');
503
504                 if(WEP_CVAR_SEC(crylink, spreadtype) == 1)
505                 {
506                         s = '0 0 0';
507                         if (counter == 0)
508                                 s = '0 0 0';
509                         else
510                         {
511                                 makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
512                                 s_y = v_forward_x;
513                                 s_z = v_forward_y;
514                         }
515                         s = s * WEP_CVAR_SEC(crylink, spread) * g_weaponspreadfactor;
516                         s = w_shotdir + right * s_y + up * s_z;
517                 }
518                 else
519                 {
520                         s = (w_shotdir + (((counter + 0.5) / shots) * 2 - 1) * v_right * WEP_CVAR_SEC(crylink, spread) * g_weaponspreadfactor);
521                 }
522
523                 W_SetupProjectileVelocityEx(proj, s, v_up, WEP_CVAR_SEC(crylink, speed), 0, 0, 0, FALSE);
524                 proj.touch = W_Crylink_Touch;
525                 proj.think = W_Crylink_Fadethink;
526                 if(counter == (shots - 1) / 2)
527                 {
528                         proj.fade_time = time + WEP_CVAR_SEC(crylink, middle_lifetime);
529                         proj.fade_rate = 1 / WEP_CVAR_SEC(crylink, middle_fadetime);
530                         proj.nextthink = time + WEP_CVAR_SEC(crylink, middle_lifetime) + WEP_CVAR_SEC(crylink, middle_fadetime);
531                 }
532                 else
533                 {
534                         proj.fade_time = time + WEP_CVAR_SEC(crylink, other_lifetime);
535                         proj.fade_rate = 1 / WEP_CVAR_SEC(crylink, other_fadetime);
536                         proj.nextthink = time + WEP_CVAR_SEC(crylink, other_lifetime) + WEP_CVAR_SEC(crylink, other_fadetime);
537                 }
538                 proj.teleport_time = time + WEP_CVAR_SEC(crylink, joindelay);
539                 proj.cnt = WEP_CVAR_SEC(crylink, bounces);
540                 //proj.scale = 1 + 1 * proj.cnt;
541
542                 proj.angles = vectoangles (proj.velocity);
543
544                 //proj.glow_size = 20;
545
546                 proj.flags = FL_PROJECTILE;
547         proj.missile_flags = MIF_SPLASH;
548         
549                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
550
551                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
552         }
553         if(WEP_CVAR_SEC(crylink, joinspread) != 0)
554         {
555                 self.crylink_lastgroup = proj;
556                 W_Crylink_CheckLinks(proj);
557                 self.crylink_waitrelease = 2;
558         }
559 }
560
561 float w_crylink(float req)
562 {
563         float ammo_amount;
564         switch(req)
565         {
566                 case WR_AIM:
567                 {
568                         if (random() < 0.10)
569                                 self.BUTTON_ATCK = bot_aim(WEP_CVAR_PRI(crylink, speed), 0, WEP_CVAR_PRI(crylink, middle_lifetime), FALSE);
570                         else
571                                 self.BUTTON_ATCK2 = bot_aim(WEP_CVAR_SEC(crylink, speed), 0, WEP_CVAR_SEC(crylink, middle_lifetime), FALSE);
572                                 
573                         return TRUE;
574                 }
575                 case WR_THINK:
576                 {
577                         if(autocvar_g_balance_crylink_reload_ammo && self.clip_load < min(WEP_CVAR_PRI(crylink, ammo), WEP_CVAR_SEC(crylink, ammo))) // forced reload
578                                 WEP_ACTION(self.weapon, WR_RELOAD);
579
580                         if (self.BUTTON_ATCK)
581                         {
582                                 if (self.crylink_waitrelease != 1)
583                                 if (weapon_prepareattack(0, WEP_CVAR_PRI(crylink, refire)))
584                                 {
585                                         W_Crylink_Attack();
586                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(crylink, animtime), w_ready);
587                                 }
588                         }
589
590                         if(self.BUTTON_ATCK2 && autocvar_g_balance_crylink_secondary)
591                         {
592                                 if (self.crylink_waitrelease != 2)
593                                 if (weapon_prepareattack(1, WEP_CVAR_SEC(crylink, refire)))
594                                 {
595                                         W_Crylink_Attack2();
596                                         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR_SEC(crylink, animtime), w_ready);
597                                 }
598                         }
599
600                         if ((self.crylink_waitrelease == 1 && !self.BUTTON_ATCK) || (self.crylink_waitrelease == 2 && !self.BUTTON_ATCK2))
601                         {
602                                 if (!self.crylink_lastgroup || time > self.crylink_lastgroup.teleport_time)
603                                 {
604                                         // fired and released now!
605                                         if(self.crylink_lastgroup)
606                                         {
607                                                 vector pos;
608                                                 entity linkjoineffect;
609                                                 float isprimary = (self.crylink_waitrelease == 1);
610                                                 
611                                                 pos = W_Crylink_LinkJoin(self.crylink_lastgroup, WEP_CVAR_BOTH(crylink, isprimary, joinspread) * WEP_CVAR_BOTH(crylink, isprimary, speed));
612
613                                                 linkjoineffect = spawn();
614                                                 linkjoineffect.think = W_Crylink_LinkJoinEffect_Think;
615                                                 linkjoineffect.classname = "linkjoineffect";
616                                                 linkjoineffect.nextthink = time + w_crylink_linkjoin_time;
617                                                 linkjoineffect.owner = self;
618                                                 setorigin(linkjoineffect, pos);
619                                         }
620                                         self.crylink_waitrelease = 0;
621                                         if(!w_crylink(WR_CHECKAMMO1) && !w_crylink(WR_CHECKAMMO2))
622                                         if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
623                                         {
624                                                 // ran out of ammo!
625                                                 self.cnt = WEP_CRYLINK;
626                                                 self.switchweapon = w_getbestweapon(self);
627                                         }
628                                 }
629                         }
630                         
631                         return TRUE;
632                 }
633                 case WR_INIT:
634                 {
635                         precache_model ("models/weapons/g_crylink.md3");
636                         precache_model ("models/weapons/v_crylink.md3");
637                         precache_model ("models/weapons/h_crylink.iqm");
638                         precache_sound ("weapons/crylink_fire.wav");
639                         precache_sound ("weapons/crylink_fire2.wav");
640                         precache_sound ("weapons/crylink_linkjoin.wav");
641                         CRYLINK_SETTINGS(WEP_SKIPCVAR, WEP_SET_PROP)
642                         return TRUE;
643                 }
644                 case WR_SETUP:
645                 {
646                         self.current_ammo = ammo_cells;
647                         return TRUE;
648                 }
649                 case WR_CHECKAMMO1:
650                 {
651                         // don't "run out of ammo" and switch weapons while waiting for release
652                         if(self.crylink_lastgroup && self.crylink_waitrelease)
653                                 return TRUE;
654
655                         ammo_amount = self.ammo_cells >= WEP_CVAR_PRI(crylink, ammo);
656                         ammo_amount += self.(weapon_load[WEP_CRYLINK]) >= WEP_CVAR_PRI(crylink, ammo);
657                         return ammo_amount;
658                 }
659                 case WR_CHECKAMMO2:
660                 {
661                         // don't "run out of ammo" and switch weapons while waiting for release
662                         if(self.crylink_lastgroup && self.crylink_waitrelease)
663                                 return TRUE;
664
665                         ammo_amount = self.ammo_cells >= WEP_CVAR_SEC(crylink, ammo);
666                         ammo_amount += self.(weapon_load[WEP_CRYLINK]) >= WEP_CVAR_SEC(crylink, ammo);
667                         return ammo_amount;
668                 }
669                 case WR_CONFIG:
670                 {
671                         CRYLINK_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
672                         return TRUE;
673                 }
674                 case WR_RELOAD:
675                 {
676                         W_Reload(min(WEP_CVAR_PRI(crylink, ammo), WEP_CVAR_SEC(crylink, ammo)), "weapons/reload.wav");
677                         return TRUE;
678                 }
679                 case WR_SUICIDEMESSAGE:
680                 {
681                         return WEAPON_CRYLINK_SUICIDE;
682                 }
683                 case WR_KILLMESSAGE:
684                 {
685                         return WEAPON_CRYLINK_MURDER;
686                 }
687         }
688         return TRUE;
689 }
690 #endif
691 #ifdef CSQC
692 float w_crylink(float req)
693 {
694         switch(req)
695         {
696                 case WR_IMPACTEFFECT:
697                 {
698                         vector org2;
699                         org2 = w_org + w_backoff * 2;
700                         if(w_deathtype & HITTYPE_SECONDARY)
701                         {
702                                 pointparticles(particleeffectnum("crylink_impact"), org2, '0 0 0', 1);
703                                 if(!w_issilent)
704                                         sound(self, CH_SHOTS, "weapons/crylink_impact2.wav", VOL_BASE, ATTN_NORM);
705                         }
706                         else
707                         {
708                                 pointparticles(particleeffectnum("crylink_impactbig"), org2, '0 0 0', 1);
709                                 if(!w_issilent)
710                                         sound(self, CH_SHOTS, "weapons/crylink_impact.wav", VOL_BASE, ATTN_NORM);
711                         }
712                         
713                         return TRUE;
714                 }
715                 case WR_INIT:
716                 {
717                         precache_sound("weapons/crylink_impact2.wav");
718                         precache_sound("weapons/crylink_impact.wav");
719                         return TRUE;
720                 }
721         }
722         return TRUE;
723 }
724 #endif
725 #endif