make weapon death messages translatable
[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_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         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
252                 self.ammo_rockets = self.ammo_rockets - autocvar_g_balance_rocketlauncher_ammo;
253
254         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 5, "weapons/rocket_fire.wav", CHAN_WEAPON, autocvar_g_balance_rocketlauncher_damage);
255         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
256
257         missile = WarpZone_RefSys_SpawnSameRefSys(self);
258         missile.owner = self;
259         self.lastrocket = missile;
260         if(autocvar_g_balance_rocketlauncher_detonatedelay >= 0)
261                 missile.spawnshieldtime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
262         else
263                 missile.spawnshieldtime = -1;
264         missile.pushltime = time + autocvar_g_balance_rocketlauncher_guidedelay;
265         missile.classname = "rocket";
266         missile.bot_dodge = TRUE;
267         missile.bot_dodgerating = autocvar_g_balance_rocketlauncher_damage * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
268
269         missile.takedamage = DAMAGE_YES;
270         missile.damageforcescale = autocvar_g_balance_rocketlauncher_damageforcescale;
271         missile.health = autocvar_g_balance_rocketlauncher_health;
272         missile.event_damage = W_Rocket_Damage;
273
274         missile.movetype = MOVETYPE_FLY;
275         PROJECTILE_MAKETRIGGER(missile);
276         missile.projectiledeathtype = WEP_ROCKET_LAUNCHER;
277         setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
278
279         setorigin (missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
280         W_SetupProjectileVelocity(missile, autocvar_g_balance_rocketlauncher_speedstart, 0);
281         missile.angles = vectoangles (missile.velocity);
282
283         missile.touch = W_Rocket_Touch;
284         missile.think = W_Rocket_Think;
285         missile.nextthink = time;
286         missile.cnt = time + autocvar_g_balance_rocketlauncher_lifetime;
287         missile.flags = FL_PROJECTILE;
288
289         CSQCProjectile(missile, autocvar_g_balance_rocketlauncher_guiderate == 0 && autocvar_g_balance_rocketlauncher_speedaccel == 0, PROJECTILE_ROCKET, FALSE); // because of fly sound
290
291         // muzzle flash for 1st person view
292         flash = spawn ();
293         setmodel (flash, "models/flash.md3"); // precision set below
294         SUB_SetFade (flash, time, 0.1);
295         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
296         W_AttachToShotorg(flash, '5 0 0');
297
298         // common properties
299         other = missile; MUTATOR_CALLHOOK(EditProjectile);
300 }
301
302 void spawnfunc_weapon_rocketlauncher (void); // defined in t_items.qc
303
304 float w_rlauncher(float req)
305 {
306         entity rock;
307         float rockfound;
308         if (req == WR_AIM)
309         {
310                 // aim and decide to fire if appropriate
311                 self.BUTTON_ATCK = bot_aim(autocvar_g_balance_rocketlauncher_speed, 0, autocvar_g_balance_rocketlauncher_lifetime, FALSE);
312                 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
313                 {
314                         // decide whether to detonate rockets
315                         local entity missile, targetlist, targ;
316                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
317                         local float selfdamage, teamdamage, enemydamage;
318                         edgedamage = autocvar_g_balance_rocketlauncher_edgedamage;
319                         coredamage = autocvar_g_balance_rocketlauncher_damage;
320                         edgeradius = autocvar_g_balance_rocketlauncher_radius;
321                         recipricoledgeradius = 1 / edgeradius;
322                         selfdamage = 0;
323                         teamdamage = 0;
324                         enemydamage = 0;
325                         targetlist = findchainfloat(bot_attack, TRUE);
326                         missile = find(world, classname, "rocket");
327                         while (missile)
328                         {
329                                 if (missile.owner != self)
330                                 {
331                                         missile = find(missile, classname, "rocket");
332                                         continue;
333                                 }
334                                 targ = targetlist;
335                                 while (targ)
336                                 {
337                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - missile.origin);
338                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
339                                         // count potential damage according to type of target
340                                         if (targ == self)
341                                                 selfdamage = selfdamage + d;
342                                         else if (targ.team == self.team && teams_matter)
343                                                 teamdamage = teamdamage + d;
344                                         else if (bot_shouldattack(targ))
345                                                 enemydamage = enemydamage + d;
346                                         targ = targ.chain;
347                                 }
348                                 missile = find(missile, classname, "rocket");
349                         }
350                         local float desirabledamage;
351                         desirabledamage = enemydamage;
352                         if (time > self.invincible_finished && time > self.spawnshieldtime)
353                                 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
354                         if (teams_matter && self.team)
355                                 desirabledamage = desirabledamage - teamdamage;
356
357                         missile = find(world, classname, "rocket");
358                         while (missile)
359                         {
360                                 if (missile.owner != self)
361                                 {
362                                         missile = find(missile, classname, "rocket");
363                                         continue;
364                                 }
365                                 makevectors(missile.v_angle);
366                                 targ = targetlist;
367                                 if (skill > 9) // normal players only do this for the target they are tracking
368                                 {
369                                         targ = targetlist;
370                                         while (targ)
371                                         {
372                                                 if (
373                                                         (v_forward * normalize(missile.origin - targ.origin)< 0.1)
374                                                         && desirabledamage > 0.1*coredamage
375                                                 )self.BUTTON_ATCK2 = TRUE;
376                                                 targ = targ.chain;
377                                         }
378                                 }else{
379                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
380                                         //As the distance gets larger, a correct detonation gets near imposible
381                                         //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
382                                         if(v_forward * normalize(missile.origin - self.enemy.origin)< 0.1)
383                                                 if(self.enemy.classname == "player")
384                                                         if(desirabledamage >= 0.1*coredamage)
385                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
386                                                                         self.BUTTON_ATCK2 = TRUE;
387                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
388                                 }
389
390                                 missile = find(missile, classname, "rocket");
391                         }
392                         // if we would be doing at X percent of the core damage, detonate it
393                         // but don't fire a new shot at the same time!
394                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
395                                 self.BUTTON_ATCK2 = TRUE;
396                         if ((skill > 6.5) && (selfdamage > self.health))
397                                 self.BUTTON_ATCK2 = FALSE;
398                         //if(self.BUTTON_ATCK2 == TRUE)
399                         //      dprint(ftos(desirabledamage),"\n");
400                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
401                 }
402         }
403         else if (req == WR_THINK)
404         {
405                 if (self.BUTTON_ATCK)
406                 {
407                         if(self.rl_release || autocvar_g_balance_rocketlauncher_guidestop)
408                         if(weapon_prepareattack(0, autocvar_g_balance_rocketlauncher_refire))
409                         {
410                                 W_Rocket_Attack();
411                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_rocketlauncher_animtime, w_ready);
412                                 self.rl_release = 0;
413                         }
414                 }
415                 else
416                         self.rl_release = 1;
417
418                 if (self.BUTTON_ATCK2)
419                 {
420                         rockfound = 0;
421                         for(rock = world; (rock = find(rock, classname, "rocket")); ) if(rock.owner == self)
422                         {
423                                 if(!rock.rl_detonate_later)
424                                 {
425                                         rock.rl_detonate_later = TRUE;
426                                         rockfound = 1;
427                                 }
428                         }
429                         if(rockfound)
430                                 sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
431                 }
432         }
433         else if (req == WR_PRECACHE)
434         {
435                 precache_model ("models/flash.md3");
436                 precache_model ("models/weapons/g_rl.md3");
437                 precache_model ("models/weapons/v_rl.md3");
438                 precache_model ("models/weapons/h_rl.iqm");
439                 precache_sound ("weapons/rocket_det.wav");
440                 precache_sound ("weapons/rocket_fire.wav");
441                 precache_sound ("weapons/rocket_mode.wav");
442         }
443         else if (req == WR_SETUP)
444         {
445                 weapon_setup(WEP_ROCKET_LAUNCHER);
446                 self.rl_release = 1;
447         }
448         else if (req == WR_CHECKAMMO1)
449         {
450                 // don't switch while guiding a missile
451                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_ROCKET_LAUNCHER)
452                         && self.ammo_rockets < autocvar_g_balance_rocketlauncher_ammo)
453                         return FALSE;
454         }
455         else if (req == WR_CHECKAMMO2)
456                 return FALSE;
457         else if (req == WR_RESETPLAYER)
458         {
459                 self.rl_release = 0;
460         }
461         return TRUE;
462 };
463 #endif
464 #ifdef CSQC
465 float w_rlauncher(float req)
466 {
467         if(req == WR_IMPACTEFFECT)
468         {
469                 vector org2;
470                 org2 = w_org + w_backoff * 12;
471                 pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
472                 if(!w_issilent)
473                         sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
474         }
475         else if(req == WR_PRECACHE)
476         {
477                 precache_sound("weapons/rocket_impact.wav");
478         }
479         else if (req == WR_SUICIDEMESSAGE)
480                 w_deathtypestring = _("%s exploded");
481         else if (req == WR_KILLMESSAGE)
482         {
483                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
484                         w_deathtypestring = _("%s got too close to %s's rocket");
485                 else if(w_deathtype & HITTYPE_SPLASH)
486                         w_deathtypestring = _("%s almost dodged %s's rocket");
487                 else
488                         w_deathtypestring = _("%s ate %s's rocket");
489         }
490         return TRUE;
491 }
492 #endif
493 #endif