g_blance_electro_secondary_bouncefactor & _bouncestop for electro seoncdary. merge...
[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); // csqc
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); // csqc
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         float counter, shots;
217         entity proj, prevproj, firstproj;
218         vector forward, right, up, s
219
220         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
221                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_primary_ammo");
222
223         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire.wav", (cvar("g_balance_crylink_primary_damage")*cvar("g_balance_crylink_primary_shots")));
224         forward = v_forward;
225         right = v_right;
226         up = v_up;
227
228         shots = cvar("g_balance_crylink_primary_shots");
229         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
230         proj = world;
231         while (counter < shots)
232         {
233                 proj = spawn ();
234                 proj.realowner = proj.owner = self;
235                 proj.classname = "spike";
236                 proj.bot_dodge = TRUE;
237                 proj.bot_dodgerating = cvar("g_balance_crylink_primary_damage");
238                 if(counter == 0) { // first projectile, store in firstproj for now
239                         firstproj = proj;
240                 }
241                 else if(counter == shots - 1) { // last projectile, link up with first projectile
242                         prevproj.queuenext = proj;
243                         firstproj.queueprev = proj;
244                         proj.queuenext = firstproj;
245                         proj.queueprev = prevproj;
246                 }
247                 else { // else link up with previous projectile
248                         prevproj.queuenext = proj;
249                         proj.queueprev = prevproj;
250                 }
251
252                 prevproj = proj;
253
254                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
255                 PROJECTILE_MAKETRIGGER(proj);
256                 proj.projectiledeathtype = WEP_CRYLINK;
257                 //proj.gravity = 0.001;
258
259                 setorigin (proj, w_shotorg);
260                 setsize(proj, '0 0 0', '0 0 0');
261
262
263                 s = '0 0 0';
264                 if (counter == 0)
265                         s = '0 0 0';
266                 else
267                 {
268                         makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
269                         s_y = v_forward_x;
270                         s_z = v_forward_y;
271                 }
272                 s = s * cvar("g_balance_crylink_primary_spread") * g_weaponspreadfactor;
273                 W_SetupProjectileVelocityEx(proj, w_shotdir + right * s_y + up * s_z, v_up, cvar("g_balance_crylink_primary_speed"), 0, 0, 0);
274                 proj.touch = W_Crylink_Touch;
275
276                 proj.think = W_Crylink_Fadethink;
277                 if(counter == 0)
278                 {
279                         proj.fade_time = time + cvar("g_balance_crylink_primary_middle_lifetime");
280                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_middle_fadetime");
281                         proj.nextthink = time + cvar("g_balance_crylink_primary_middle_lifetime") + cvar("g_balance_crylink_primary_middle_fadetime");
282                 }
283                 else if(counter <= 3)
284                 {
285                         proj.fade_time = time + cvar("g_balance_crylink_primary_star_lifetime");
286                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_star_fadetime");
287                         proj.nextthink = time + cvar("g_balance_crylink_primary_star_lifetime") + cvar("g_balance_crylink_primary_star_fadetime");
288                 }
289                 else
290                 {
291                         proj.fade_time = time + cvar("g_balance_crylink_primary_other_lifetime");
292                         self.fade_rate = 1 / cvar("g_balance_crylink_primary_other_fadetime");
293                         proj.nextthink = time + cvar("g_balance_crylink_primary_other_lifetime") + cvar("g_balance_crylink_primary_other_fadetime");
294                 }
295                 proj.cnt = cvar("g_balance_crylink_primary_bounces");
296                 
297                 //proj.scale = 1 + 1 * proj.cnt;
298                 //proj.angles = vectoangles (proj.velocity); // csqc
299                 //proj.glow_size = 20;
300
301                 proj.flags = FL_PROJECTILE;
302
303                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
304
305                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
306
307                 counter = counter + 1;
308         }
309         self.crylink_lastgroup = proj;
310 }
311
312 void W_Crylink_Attack2 (void)
313 {
314         local float counter, shots;
315         local entity proj, prevproj, firstproj;
316
317         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
318                 self.ammo_cells = self.ammo_cells - cvar("g_balance_crylink_secondary_ammo");
319
320         W_SetupShot (self, FALSE, 2, "weapons/crylink_fire2.wav", (cvar("g_balance_crylink_secondary_damage")*cvar("g_balance_crylink_secondary_shots")));
321
322         shots = cvar("g_balance_crylink_secondary_shots");
323         pointparticles(particleeffectnum("crylink_muzzleflash"), w_shotorg, w_shotdir * 1000, shots);
324         proj = world;
325         while (counter < shots)
326         {
327                 proj = spawn ();
328                 proj.realowner = proj.owner = self;
329                 proj.classname = "spike";
330                 proj.bot_dodge = TRUE;
331                 proj.bot_dodgerating = cvar("g_balance_crylink_secondary_damage");
332                 if(counter == 0) { // first projectile, store in firstproj for now
333                         firstproj = proj;
334                 }
335                 else if(counter == shots - 1) { // last projectile, link up with first projectile
336                         prevproj.queuenext = proj;
337                         firstproj.queueprev = proj;
338                         proj.queuenext = firstproj;
339                         proj.queueprev = prevproj;
340                 }
341                 else { // else link up with previous projectile
342                         prevproj.queuenext = proj;
343                         proj.queueprev = prevproj;
344                 }
345
346                 prevproj = proj;
347
348                 proj.movetype = MOVETYPE_BOUNCEMISSILE;
349                 PROJECTILE_MAKETRIGGER(proj);
350                 proj.projectiledeathtype = WEP_CRYLINK | HITTYPE_SECONDARY;
351                 //proj.gravity = 0.001;
352
353                 setorigin (proj, w_shotorg);
354                 setsize(proj, '0 0 0', '0 0 0');
355
356                 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);
357                 proj.touch = W_Crylink_Touch2;
358                 proj.think = W_Crylink_Fadethink;
359                 if(counter == (shots - 1) / 2)
360                 {
361                         proj.fade_time = time + cvar("g_balance_crylink_secondary_middle_lifetime");
362                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_middle_fadetime");
363                         proj.nextthink = time + cvar("g_balance_crylink_secondary_middle_lifetime") + cvar("g_balance_crylink_secondary_middle_fadetime");
364                 }
365                 else
366                 {
367                         proj.fade_time = time + cvar("g_balance_crylink_secondary_line_lifetime");
368                         self.fade_rate = 1 / cvar("g_balance_crylink_secondary_line_fadetime");
369                         proj.nextthink = time + cvar("g_balance_crylink_secondary_line_lifetime") + cvar("g_balance_crylink_secondary_line_fadetime");
370                 }
371                 proj.cnt = cvar("g_balance_crylink_secondary_bounces");
372                 //proj.scale = 1 + 1 * proj.cnt;
373
374                 // proj.angles = vectoangles (proj.velocity); // csqc
375
376                 //proj.glow_size = 20;
377
378                 proj.flags = FL_PROJECTILE;
379
380                 CSQCProjectile(proj, TRUE, (proj.cnt ? PROJECTILE_CRYLINK_BOUNCING : PROJECTILE_CRYLINK), TRUE);
381
382                 other = proj; MUTATOR_CALLHOOK(EditProjectile);
383
384                 counter = counter + 1;
385         }
386         self.crylink_lastgroup = proj;
387 }
388
389 void spawnfunc_weapon_crylink (void)
390 {
391         weapon_defaultspawnfunc(WEP_CRYLINK);
392 }
393
394 float w_crylink(float req)
395 {
396         if (req == WR_AIM)
397         {
398                 if (random() > 0.15)
399                         self.BUTTON_ATCK = bot_aim(cvar("g_balance_crylink_primary_speed"), 0, cvar("g_balance_crylink_primary_middle_lifetime"), FALSE);
400                 else
401                         self.BUTTON_ATCK2 = bot_aim(cvar("g_balance_crylink_secondary_speed"), 0, cvar("g_balance_crylink_secondary_middle_lifetime"), FALSE);
402         }
403         else if (req == WR_THINK)
404         {
405                 if (self.BUTTON_ATCK)
406                 {
407                         if (!self.crylink_waitrelease)
408                         if (weapon_prepareattack(0, cvar("g_balance_crylink_primary_refire")))
409                         {
410                                 W_Crylink_Attack();
411                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_crylink_primary_animtime"), w_ready);
412                                 if(cvar("g_balance_crylink_primary_joinspeed") != 0)
413                                         self.crylink_waitrelease = 1;
414                         }               
415                 }
416                 else if(self.BUTTON_ATCK2 && cvar("g_balance_crylink_secondary"))
417                 {
418                         if (weapon_prepareattack(1, cvar("g_balance_crylink_secondary_refire")))
419                         {
420                                 W_Crylink_Attack2();
421                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_crylink_secondary_animtime"), w_ready);
422                                 if(cvar("g_balance_crylink_secondary_joinspeed") != 0)
423                                         self.crylink_waitrelease = 2;
424                         }
425                 }
426                 else
427                 {
428                         if (self.crylink_waitrelease)
429                         {
430                                 // fired and released now!
431                                 if(self.crylink_lastgroup)
432                                 {
433                                         if(self.crylink_waitrelease == 1)
434                                         {
435                                                 W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_primary_joinspeed"));                                                
436                                                 
437                                                 // 'splode @ join, if timing is just right, you get the target with the join-explode splash first, then the individual projectile's damage.
438                                                 self.crylink_lastgroup.think = W_Crylink_Touch;
439                                                 self.crylink_lastgroup.nextthink = w_crylink_linkjoin_time + time;                                                      
440                                                 
441                                         }
442                                         else
443                                         {
444                                                 W_Crylink_LinkJoin(self.crylink_lastgroup, cvar("g_balance_crylink_secondary_joinspeed"));
445                                         }
446                                 }
447                         }
448
449                         self.crylink_waitrelease = 0;
450                 }
451         }
452         else if (req == WR_PRECACHE)
453         {
454                 precache_model ("models/weapons/g_crylink.md3");
455                 precache_model ("models/weapons/v_crylink.md3");
456                 precache_model ("models/weapons/h_crylink.iqm");
457                 precache_sound ("weapons/crylink_fire.wav");
458                 precache_sound ("weapons/crylink_fire2.wav");
459         }
460         else if (req == WR_SETUP)
461                 weapon_setup(WEP_CRYLINK);
462         else if (req == WR_CHECKAMMO1)
463                 return self.ammo_cells >= cvar("g_balance_crylink_primary_ammo");
464         else if (req == WR_CHECKAMMO2)
465                 return self.ammo_cells >= cvar("g_balance_crylink_secondary_ammo");
466         return TRUE;
467 };
468 #endif
469 #ifdef CSQC
470 float w_crylink(float req)
471 {
472         if(req == WR_IMPACTEFFECT)
473         {
474                 vector org2;
475                 org2 = w_org + w_backoff * 2;
476                 if(w_deathtype & HITTYPE_SECONDARY)
477                 {
478                         pointparticles(particleeffectnum("crylink_impact"), org2, '0 0 0', 1);
479                         if(!w_issilent)
480                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact2.wav", VOL_BASE, ATTN_NORM);
481                 }
482                 else
483                 {
484                         pointparticles(particleeffectnum("crylink_impactbig"), org2, '0 0 0', 1);
485                         if(!w_issilent)
486                                 sound(self, CHAN_PROJECTILE, "weapons/crylink_impact.wav", VOL_BASE, ATTN_NORM);
487                 }
488         }
489         else if(req == WR_PRECACHE)
490         {
491                 precache_sound("weapons/crylink_impact2.wav");
492                 precache_sound("weapons/crylink_impact.wav");
493         }
494         else if (req == WR_SUICIDEMESSAGE)
495         {
496                 w_deathtypestring = "%s succeeded at self-destructing themself with the Crylink";
497         }
498         else if (req == WR_KILLMESSAGE)
499         {
500                 if(w_deathtype & HITTYPE_BOUNCE)
501                         w_deathtypestring = "%s could not hide from %s's Crylink"; // unchecked: SPLASH (SECONDARY can't be)
502                 else if(w_deathtype & HITTYPE_SPLASH)
503                         w_deathtypestring = "%s was too close to %s's Crylink"; // unchecked: SECONDARY
504                 else
505                         w_deathtypestring = "%s took a close look at %s's Crylink"; // unchecked: SECONDARY
506         }
507         return TRUE;
508 }
509 #endif
510 #endif