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