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