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