add a missing cvar
[divverent/nexuiz.git] / data / qcsrc / server / w_rocketlauncher.qc
1 .float rl_release;
2 .float rl_detonate_later;
3
4 void W_Rocket_Unregister()
5 {
6         if(self.owner && self.owner.lastrocket == self)
7         {
8                 self.owner.lastrocket = world;
9                 // self.owner.rl_release = 1;
10         }
11 }
12
13 void W_Rocket_Explode (void)
14 {
15         W_Rocket_Unregister();
16
17         if(other.takedamage == DAMAGE_AIM)
18                 if(other.classname == "player")
19                         if(IsDifferentTeam(self.owner, other))
20                                 if(IsFlying(other))
21                                         announce(self.owner, "announcer/male/airshot.wav");
22
23         self.event_damage = SUB_Null;
24         self.takedamage = DAMAGE_NO;
25         RadiusDamage (self, self.owner, cvar("g_balance_rocketlauncher_damage"), cvar("g_balance_rocketlauncher_edgedamage"), cvar("g_balance_rocketlauncher_radius"), world, cvar("g_balance_rocketlauncher_force"), self.projectiledeathtype, other);
26
27         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
28         {
29                 if(self.owner.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
30                 {
31                         self.owner.cnt = WEP_ROCKET_LAUNCHER;
32                         ATTACK_FINISHED(self.owner) = time;
33                         self.owner.switchweapon = w_getbestweapon(self.owner);
34                 }
35                 if(g_laserguided_missile)
36                         ATTACK_FINISHED(self.owner) = time + cvar("g_balance_rocketlauncher_refire") * W_WeaponRateFactor();
37         }
38         remove (self);
39 }
40
41 entity FindLaserTarget(entity e, float dist_variance, float dot_variance)
42 {
43         entity head, selected;
44         vector dir;
45         float dist, maxdist,// bestdist,
46                 dot,// bestdot,
47                 points, bestpoints;
48         //bestdist = 9999;
49         //bestdot = -2;
50         bestpoints = 0;
51         maxdist = 800;
52         selected = world;
53
54         makevectors(e.angles);
55
56         head = find(world, classname, "laser_target");
57         while(head)
58         {
59                 points = 0;
60                 dir = normalize(head.origin - self.origin);
61                 dot = dir * v_forward;
62                 dist = vlen(head.origin - self.origin);
63                 if(dist > maxdist)
64                         dist = maxdist;
65
66                 // gain points for being in front
67                 points = points + ((dot+1)*0.5) * 500
68                         * (1 + crandom()*dot_variance);
69                 // gain points for being close away
70                 points = points + (1 - dist/maxdist) * 1000
71                         * (1 + crandom()*dot_variance);
72
73                 traceline(e.origin, head.origin, TRUE, self);
74                 if(trace_fraction < 1)
75                 {
76                         points = 0;
77                 }
78
79                 if(points > bestpoints)//random() > 0.5)//
80                 {
81                         bestpoints = points;
82                         selected = head;
83                 }
84
85                 head = find(head, classname, "laser_target");
86         }
87
88         //bprint(selected.owner.netname);
89         //bprint("\n");
90         return selected;
91 }
92
93 void W_Rocket_RemoteExplode()
94 {
95         if(self.owner.deadflag == DEAD_NO)
96         if(self.owner.lastrocket)
97         {
98                 if((self.spawnshieldtime >= 0)
99                         ? (time >= self.spawnshieldtime) // timer
100                         : (vlen(NearestPointOnBox(self.owner, self.origin) - self.origin) > cvar("g_balance_rocketlauncher_radius")) // safety device
101                 )
102                 {
103                         other = world;
104                         self.projectiledeathtype |= HITTYPE_BOUNCE;
105                         W_Rocket_Explode ();
106                 }
107                 else
108                 {
109                 }
110         }
111 }
112
113 vector rocket_steerto(vector thisdir, vector goaldir, float maxturn_cos)
114 {
115         if(thisdir * goaldir > maxturn_cos)
116                 return goaldir;
117         float f, m2;
118         vector v;
119         // solve:
120         //   g = normalize(thisdir + goaldir * X)
121         //   thisdir * g = maxturn
122         //
123         //   gg = thisdir + goaldir * X
124         //   (thisdir * gg)^2 = maxturn^2 * (gg * gg)
125         //
126         //   (1 + (thisdir * goaldir) * X)^2 = maxturn^2 * (1 + X*X + 2 * X * thisdir * goaldir)
127         f = thisdir * goaldir;
128         //   (1 + f * X)^2 = maxturn^2 * (1 + X*X + 2 * X * f)
129         //   0 = (m^2 - f^2) * x^2 + (2 * f * (m^2 - 1)) * x + (m^2 - 1)
130         m2 = maxturn_cos * maxturn_cos;
131         v = solve_quadratic(m2 - f * f, 2 * f * (m2 - 1), m2 - 1);
132         return normalize(thisdir + goaldir * v_y); // the larger solution!
133 }
134
135 void W_Rocket_Think (void)
136 {
137         entity e;
138         vector desireddir, olddir, newdir, desiredorigin, goal;
139 #if 0
140         float cosminang, cosmaxang, cosang;
141 #endif
142         float turnrate, velspeed, f;
143         self.nextthink = time;
144         if (time > self.cnt)
145         {
146                 other = world;
147                 self.projectiledeathtype |= HITTYPE_BOUNCE;
148                 W_Rocket_Explode ();
149                 return;
150         }
151
152         if(g_laserguided_missile)
153         {
154                 // accelerate
155                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
156                 velspeed = cvar("g_balance_rocketlauncher_laserguided_speed") * g_weaponspeedfactor - (self.velocity * v_forward);
157                 if (velspeed > 0)
158                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_laserguided_speedaccel") * frametime, velspeed);
159         }
160         else
161         {
162                 // accelerate
163                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
164                 velspeed = cvar("g_balance_rocketlauncher_speed") * g_weaponspeedfactor - (self.velocity * v_forward);
165                 if (velspeed > 0)
166                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_speedaccel") * g_weaponspeedfactor * frametime, velspeed);
167         }
168
169         // laser guided, or remote detonation
170         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
171         {
172                 if(g_laserguided_missile)
173                 {
174                         if(self.rl_detonate_later)
175                                 W_Rocket_RemoteExplode();
176
177                         if(cvar("g_balance_rocketlauncher_laserguided_allow_steal"))
178                         {
179                                 if(self.owner.laser_on)
180                                 {
181                                         if(self.attack_finished_single < time)
182                                         {
183                                                 self.attack_finished_single = time + 0.2 + random()*0.3;
184                                                 self.enemy = FindLaserTarget(self, 0.7, 0.7);
185                                         }
186
187                                         if(!self.enemy)
188                                                 self.enemy = self.owner.weaponentity.lasertarget;
189                                 }
190                                 else self.enemy = world;
191                         }
192                         else // don't allow stealing: always target my owner's laser (if it exists)
193                                 self.enemy = self.owner.weaponentity.lasertarget;
194
195                         if(self.enemy != world)
196                         {
197                                 //bprint("Targeting ", self.enemy.owner.netname, "'s laser\n");
198                                 velspeed = vlen(self.velocity);
199                                 e = self.enemy;//self.owner.weaponentity.lasertarget;
200                                 turnrate = cvar("g_balance_rocketlauncher_laserguided_turnrate");//0.65;                                                // how fast to turn
201                                 desireddir = normalize(e.origin - self.origin);         // get direction from my position to the laser target
202                                 olddir = normalize(self.velocity);                                      // get my current direction
203                                 newdir = normalize(olddir + desireddir * turnrate);     // take the average of the 2 directions; not the best method but simple & easy
204                                 self.velocity = newdir * velspeed;                                      // make me fly in the new direction at my flight speed
205                                 self.angles = vectoangles(self.velocity);                       // turn model in the new flight direction
206
207                                 ATTACK_FINISHED(self.owner) = time + 0.2 * W_WeaponRateFactor();
208                         }
209                 }
210                 else
211                 {
212                         if(self == self.owner.lastrocket)
213                         if not(self.owner.rl_release)
214                         if not(self.BUTTON_ATCK2)
215                         if(cvar("g_balance_rocketlauncher_guiderate"))
216                         if(time > self.pushltime)
217                         if(self.owner.deadflag == DEAD_NO)
218                         {
219                                 f = cvar("g_balance_rocketlauncher_guideratedelay");
220                                 if(f)
221                                         f = bound(0, (time - self.pushltime) / f, 1);
222                                 else
223                                         f = 1;
224
225                                 velspeed = vlen(self.velocity);
226
227                                 makevectors(self.owner.v_angle);
228                                 desireddir = v_forward;
229                                 desiredorigin = self.owner.origin + self.owner.view_ofs;
230                                 olddir = normalize(self.velocity);
231
232 #if 0
233                                 // disabled this code because it doesn't do what I want it to do :P
234                                 cosminang = cos(cvar("g_balance_rocketlauncher_guidefadeangle") * DEG2RAD);
235                                 cosmaxang = cos(cvar("g_balance_rocketlauncher_guidemaxangle") * DEG2RAD);
236                                 cosang = desireddir * normalize(self.origin - desiredorigin);
237                                 if(cosminang == cosmaxang)
238                                         f *= (cosang >= cosminang);
239                                 else
240                                         f *= bound(0, (cosang - cosmaxang) / (cosminang - cosmaxang), 1);
241 #endif
242
243                                 // now it gets tricky... we want to move like some curve to approximate the target direction
244                                 // but we are limiting the rate at which we can turn!
245                                 goal = desiredorigin + ((self.origin - desiredorigin) * desireddir + cvar("g_balance_rocketlauncher_guidegoal")) * desireddir;
246                                 newdir = rocket_steerto(olddir, normalize(goal - self.origin), cos(cvar("g_balance_rocketlauncher_guiderate") * f * frametime * DEG2RAD));
247
248                                 self.velocity = newdir * velspeed;
249                                 self.angles = vectoangles(self.velocity);
250
251                                 if(!self.count)
252                                 {
253                                         pointparticles(particleeffectnum("rocket_guide"), self.origin, self.velocity, 1);
254                                         // TODO add a better sound here
255                                         sound (self.owner, CHAN_WEAPON2, "weapons/rocket_mode.wav", VOL_BASE, ATTN_NORM);
256                                         self.count = 1;
257                                 }
258                         }
259
260                         if(self.rl_detonate_later)
261                                 W_Rocket_RemoteExplode();
262                 }
263         }
264
265         if(self.csqcprojectile_clientanimate == 0)
266                 UpdateCSQCProjectile(self);
267 }
268
269 void W_Rocket_Touch (void)
270 {
271         W_Rocket_Unregister();
272
273         PROJECTILE_TOUCH;
274         W_Rocket_Explode ();
275 }
276
277 void W_Rocket_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
278 {
279         if (self.health <= 0)
280                 return;
281         self.health = self.health - damage;
282         self.angles = vectoangles(self.velocity);
283         if (self.health <= 0)
284                 W_PrepareExplosionByDamage(attacker, W_Rocket_Explode);
285 }
286
287 void W_Rocket_Attack (void)
288 {
289         local entity missile;
290         local entity flash;
291
292         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
293                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_rocketlauncher_ammo");
294
295         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 5, "weapons/rocket_fire.wav", cvar("g_balance_rocketlauncher_damage"));
296         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
297
298         missile = spawn ();
299         missile.owner = self;
300         self.lastrocket = missile;
301         if(cvar("g_balance_rocketlauncher_detonatedelay") >= 0)
302                 missile.spawnshieldtime = time + cvar("g_balance_rocketlauncher_detonatedelay");
303         else
304                 missile.spawnshieldtime = -1;
305         missile.pushltime = time + cvar("g_balance_rocketlauncher_guidedelay");
306         missile.classname = "rocket";
307         missile.bot_dodge = TRUE;
308         missile.bot_dodgerating = cvar("g_balance_rocketlauncher_damage") * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
309
310         missile.takedamage = DAMAGE_YES;
311         missile.damageforcescale = cvar("g_balance_rocketlauncher_damageforcescale");
312         missile.health = cvar("g_balance_rocketlauncher_health");
313         missile.event_damage = W_Rocket_Damage;
314
315         missile.movetype = MOVETYPE_FLY;
316         PROJECTILE_MAKETRIGGER(missile);
317         missile.projectiledeathtype = WEP_ROCKET_LAUNCHER;
318         setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
319
320         setorigin (missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
321         if(g_laserguided_missile && self.laser_on)
322                 W_SetupProjectileVelocity(missile, cvar("g_balance_rocketlauncher_laserguided_speedstart"), 0);
323         else
324                 W_SetupProjectileVelocity(missile, cvar("g_balance_rocketlauncher_speedstart"), 0);
325         missile.angles = vectoangles (missile.velocity);
326
327         missile.touch = W_Rocket_Touch;
328         missile.think = W_Rocket_Think;
329         missile.nextthink = time;
330         missile.cnt = time + cvar("g_balance_rocketlauncher_lifetime");
331         missile.flags = FL_PROJECTILE;
332
333         CSQCProjectile(missile, cvar("g_balance_rocketlauncher_guiderate") == 0 && cvar("g_balance_rocketlauncher_speedaccel") == 0 && !g_laserguided_missile, PROJECTILE_ROCKET, FALSE); // because of fly sound
334
335         // muzzle flash for 1st person view
336         flash = spawn ();
337         setmodel (flash, "models/flash.md3"); // precision set below
338         SUB_SetFade (flash, time, 0.1);
339         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
340         W_AttachToShotorg(flash, '5 0 0');
341
342         // common properties
343 }
344
345 void spawnfunc_weapon_rocketlauncher (void); // defined in t_items.qc
346
347 float w_rlauncher(float req)
348 {
349         entity rock;
350         float rockfound;
351         if (req == WR_AIM)
352         {
353                 // aim and decide to fire if appropriate
354                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_rocketlauncher_speed"), 0, cvar("g_balance_rocketlauncher_lifetime"), FALSE);
355                 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
356                 {
357                         // decide whether to detonate rockets
358                         local entity missile, targetlist, targ;
359                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
360                         local float selfdamage, teamdamage, enemydamage;
361                         edgedamage = cvar("g_balance_rocketlauncher_edgedamage");
362                         coredamage = cvar("g_balance_rocketlauncher_damage");
363                         edgeradius = cvar("g_balance_rocketlauncher_radius");
364                         recipricoledgeradius = 1 / edgeradius;
365                         selfdamage = 0;
366                         teamdamage = 0;
367                         enemydamage = 0;
368                         targetlist = findchainfloat(bot_attack, TRUE);
369                         missile = find(world, classname, "rocket");
370                         while (missile)
371                         {
372                                 if (missile.owner != self)
373                                 {
374                                         missile = find(missile, classname, "rocket");
375                                         continue;
376                                 }
377                                 targ = targetlist;
378                                 while (targ)
379                                 {
380                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - missile.origin);
381                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
382                                         // count potential damage according to type of target
383                                         if (targ == self)
384                                                 selfdamage = selfdamage + d;
385                                         else if (targ.team == self.team && teams_matter)
386                                                 teamdamage = teamdamage + d;
387                                         else if (bot_shouldattack(targ))
388                                                 enemydamage = enemydamage + d;
389                                         targ = targ.chain;
390                                 }
391                                 missile = find(missile, classname, "rocket");
392                         }
393                         local float desirabledamage;
394                         desirabledamage = enemydamage;
395                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
396                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
397                         if (self.team && teamplay != 1)
398                                 desirabledamage = desirabledamage - teamdamage;
399
400                         missile = find(world, classname, "rocket");
401                         while (missile)
402                         {
403                                 if (missile.owner != self)
404                                 {
405                                         missile = find(missile, classname, "rocket");
406                                         continue;
407                                 }
408                                 makevectors(missile.v_angle);
409                                 targ = targetlist;
410                                 if (skill > 9) // normal players only do this for the target they are tracking
411                                 {
412                                         targ = targetlist;
413                                         while (targ)
414                                         {
415                                                 if (
416                                                         (v_forward * normalize(missile.origin - targ.origin)< 0.1)
417                                                         && desirabledamage > 0.1*coredamage
418                                                 )self.BUTTON_ATCK2 = TRUE;
419                                                 targ = targ.chain;
420                                         }
421                                 }else{
422                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
423                                         //As the distance gets larger, a correct detonation gets near imposible
424                                         //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
425                                         if(v_forward * normalize(missile.origin - self.enemy.origin)< 0.1)
426                                                 if(self.enemy.classname == "player")
427                                                         if(desirabledamage >= 0.1*coredamage)
428                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
429                                                                         self.BUTTON_ATCK2 = TRUE;
430                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
431                                 }
432
433                                 missile = find(missile, classname, "rocket");
434                         }
435                         // if we would be doing at X percent of the core damage, detonate it
436                         // but don't fire a new shot at the same time!
437                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
438                                 self.BUTTON_ATCK2 = TRUE;
439                         if ((skill > 6.5) && (selfdamage > self.health))
440                                 self.BUTTON_ATCK2 = FALSE;
441                         //if(self.BUTTON_ATCK2 == TRUE)
442                         //      dprint(ftos(desirabledamage),"\n");
443                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
444                 }
445         }
446         else if (req == WR_THINK)
447         {
448                 if(g_laserguided_missile)
449                 {
450                         if (self.BUTTON_ATCK && self.rl_release)
451                         {
452                                 rockfound = 0;
453                                 for(rock = world; (rock = find(rock, classname, "rocket")); ) if(rock.owner == self)
454                                 {
455                                         if(!rock.rl_detonate_later)
456                                         {
457                                                 rock.rl_detonate_later = TRUE;
458                                                 rockfound = 1;
459                                         }
460                                 }
461                                 if(rockfound)
462                                         sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
463                                 else
464                                 {
465                                         if (weapon_prepareattack(0, cvar("g_balance_rocketlauncher_refire")))
466                                         {
467                                                 W_Rocket_Attack();
468                                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_rocketlauncher_animtime"), w_ready);
469                                         }
470                                 }
471                                 self.rl_release = 0;
472                         }
473                         if (!self.BUTTON_ATCK)
474                                 self.rl_release = 1;
475                         if (self.BUTTON_ATCK2)
476                         if(self.exteriorweaponentity.attack_finished_single < time)
477                         {
478                                 self.exteriorweaponentity.attack_finished_single = time + 0.4;
479                                 self.laser_on = !self.laser_on;
480                                 // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
481                                 sound (self, CHAN_WEAPON2, "weapons/rocket_mode.wav", VOL_BASE, ATTN_NORM);
482                         }
483                 }
484                 else
485                 {
486                         if (self.BUTTON_ATCK)
487                         {
488                                 if(self.rl_release || cvar("g_balance_rocketlauncher_guidestop"))
489                                 if(weapon_prepareattack(0, cvar("g_balance_rocketlauncher_refire")))
490                                 {
491                                         W_Rocket_Attack();
492                                         weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_rocketlauncher_animtime"), w_ready);
493                                         self.rl_release = 0;
494                                 }
495                         }
496                         else
497                                 self.rl_release = 1;
498
499                         if (self.BUTTON_ATCK2)
500                         {
501                                 rockfound = 0;
502                                 for(rock = world; (rock = find(rock, classname, "rocket")); ) if(rock.owner == self)
503                                 {
504                                         if(!rock.rl_detonate_later)
505                                         {
506                                                 rock.rl_detonate_later = TRUE;
507                                                 rockfound = 1;
508                                         }
509                                 }
510                                 if(rockfound)
511                                         sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
512                         }
513                 }
514         }
515         else if (req == WR_PRECACHE)
516         {
517                 precache_model ("models/flash.md3");
518                 precache_model ("models/weapons/g_rl.md3");
519                 precache_model ("models/weapons/v_rl.md3");
520                 precache_model ("models/weapons/h_rl.dpm");
521                 precache_sound ("weapons/rocket_det.wav");
522                 precache_sound ("weapons/rocket_fire.wav");
523                 precache_sound ("weapons/rocket_mode.wav");
524                 if (g_laserguided_missile)
525                 {
526                         precache_model ("models/laser_dot.mdl"); // rocket launcher
527                 }
528         }
529         else if (req == WR_SETUP)
530         {
531                 weapon_setup(WEP_ROCKET_LAUNCHER);
532                 self.rl_release = 1;
533         }
534         else if (req == WR_CHECKAMMO1)
535         {
536                 // don't switch while guiding a missile
537                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_ROCKET_LAUNCHER)
538                         && self.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
539                         return FALSE;
540         }
541         else if (req == WR_CHECKAMMO2)
542                 return FALSE;
543         else if (req == WR_SUICIDEMESSAGE)
544                 w_deathtypestring = "exploded";
545         else if (req == WR_KILLMESSAGE)
546         {
547                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
548                         w_deathtypestring = "got too close to #'s rocket";
549                 else if(w_deathtype & HITTYPE_SPLASH)
550                         w_deathtypestring = "almost dodged #'s rocket";
551                 else
552                         w_deathtypestring = "ate #'s rocket";
553         }
554         else if (req == WR_RESETPLAYER)
555         {
556                 self.rl_release = 0;
557         }
558         return TRUE;
559 };