support for announcer/male/impressive and announcer/male/headshot ;)
[divverent/nexuiz.git] / data / qcsrc / server / w_rocketlauncher.qc
1
2 .float rl_sound;
3 .float rl_detonate_later;
4
5 void W_Rocket_Explode (void)
6 {
7         vector  org2;
8         sound (self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
9         org2 = findbetterlocation (self.origin, 16);
10
11         pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
12
13         //effect (org2, "models/sprites/rockexpl.spr", 0, 12, 35);
14         self.event_damage = SUB_Null;
15         RadiusDamage (self, self.owner, cvar("g_balance_rocketlauncher_damage"), cvar("g_balance_rocketlauncher_edgedamage"), cvar("g_balance_rocketlauncher_radius"), world, cvar("g_balance_rocketlauncher_force"), self.projectiledeathtype, other);
16
17         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
18         {
19                 if(self.owner.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
20                 {
21                         self.owner.cnt = WEP_ROCKET_LAUNCHER;
22                         ATTACK_FINISHED(self.owner) = time;
23                         self.owner.switchweapon = w_getbestweapon(self.owner);
24                 }
25                 if(g_laserguided_missile)
26                         ATTACK_FINISHED(self.owner) = time + cvar("g_balance_rocketlauncher_refire");
27         }
28         remove (self);
29 }
30
31 entity FindLaserTarget(entity e, float dist_variance, float dot_variance)
32 {
33         entity head, selected;
34         vector dir;
35         float dist, maxdist,// bestdist,
36                 dot,// bestdot,
37                 points, bestpoints;
38         //bestdist = 9999;
39         //bestdot = -2;
40         bestpoints = 0;
41         maxdist = 800;
42         selected = world;
43
44         makevectors(e.angles);
45
46         head = find(world, classname, "laser_target");
47         while(head)
48         {
49                 points = 0;
50                 dir = normalize(head.origin - self.origin);
51                 dot = dir * v_forward;
52                 dist = vlen(head.origin - self.origin);
53                 if(dist > maxdist)
54                         dist = maxdist;
55
56                 // gain points for being in front
57                 points = points + ((dot+1)*0.5) * 500
58                         * (1 + crandom()*dot_variance);
59                 // gain points for being close away
60                 points = points + (1 - dist/maxdist) * 1000
61                         * (1 + crandom()*dot_variance);
62
63                 traceline(e.origin, head.origin, TRUE, self);
64                 if(trace_fraction < 1)
65                 {
66                         points = 0;
67                 }
68
69                 if(points > bestpoints)//random() > 0.5)//
70                 {
71                         bestpoints = points;
72                         selected = head;
73                 }
74
75                 head = find(head, classname, "laser_target");
76         }
77
78         //bprint(selected.owner.netname);
79         //bprint("\n");
80         return selected;
81 }
82
83 void W_Rocket_RemoteExplode()
84 {
85         if(self.owner.deadflag == DEAD_NO)
86         {
87                 if((self.spawnshieldtime >= 0)
88                         ? (time >= self.spawnshieldtime) // timer
89                         : (vlen(NearestPointOnBox(self.owner, self.origin) - self.origin) > cvar("g_balance_rocketlauncher_radius")) // safety device
90                 )
91                 {
92                         other = world;
93                         self.projectiledeathtype |= HITTYPE_BOUNCE;
94                         W_Rocket_Explode ();
95                 }
96                 else
97                 {
98                 }
99         }
100 }
101
102 void W_Rocket_Think (void)
103 {
104         entity e;
105         vector desireddir, olddir, newdir;
106         float turnrate, velspeed;
107         self.nextthink = time;
108         if (time > self.cnt)
109         {
110                 other = world;
111                 self.projectiledeathtype |= HITTYPE_BOUNCE;
112                 W_Rocket_Explode ();
113                 return;
114         }
115
116         if(g_laserguided_missile)
117         {
118                 // accelerate
119                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
120                 velspeed = cvar("g_balance_rocketlauncher_laserguided_speed") - (self.velocity * v_forward);
121                 if (velspeed > 0)
122                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_laserguided_speedaccel") * frametime, velspeed);
123         }
124         else
125         {
126                 // accelerate
127                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
128                 velspeed = cvar("g_balance_rocketlauncher_speed") - (self.velocity * v_forward);
129                 if (velspeed > 0)
130                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_speedaccel") * frametime, velspeed);
131         }
132
133         // laser guided, or remote detonation
134         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
135         {
136                 if(g_laserguided_missile)
137                 {
138                         if(!self.owner.BUTTON_ATCK)
139                                 self.ltime = -1; // indicate that the player has let go of the button
140
141
142                         if(self.owner.deadflag == DEAD_NO)
143                         if (self.owner.BUTTON_ATCK && self.ltime < 0) // if the player let go of the button and then pushed it again
144                                 self.rl_detonate_later = TRUE;
145                         if(self.rl_detonate_later)
146                                 W_Rocket_RemoteExplode();
147
148                         if(cvar("g_balance_rocketlauncher_laserguided_allow_steal"))
149                         {
150                                 if(self.owner.laser_on)
151                                 {
152                                         if(self.attack_finished_single < time)
153                                         {
154                                                 self.attack_finished_single = time + 0.2 + random()*0.3;
155                                                 self.enemy = FindLaserTarget(self, 0.7, 0.7);
156                                         }
157
158                                         if(!self.enemy)
159                                                 self.enemy = self.owner.weaponentity.lasertarget;
160                                 }
161                                 else self.enemy = world;
162                         }
163                         else // don't allow stealing: always target my owner's laser (if it exists)
164                                 self.enemy = self.owner.weaponentity.lasertarget;
165
166                         if(self.enemy != world)
167                         {
168                                 //bprint("Targeting ", self.enemy.owner.netname, "'s laser\n");
169                                 velspeed = vlen(self.velocity);
170                                 e = self.enemy;//self.owner.weaponentity.lasertarget;
171                                 turnrate = cvar("g_balance_rocketlauncher_laserguided_turnrate");//0.65;                                                // how fast to turn
172                                 desireddir = normalize(e.origin - self.origin);         // get direction from my position to the laser target
173                                 olddir = normalize(self.velocity);                                      // get my current direction
174                                 newdir = normalize((olddir + desireddir * turnrate) * 0.5);     // take the average of the 2 directions; not the best method but simple & easy
175                                 self.velocity = newdir * velspeed;                                      // make me fly in the new direction at my flight speed
176                                 self.angles = vectoangles(self.velocity);                       // turn model in the new flight direction
177
178                                 ATTACK_FINISHED(self.owner) = time + 0.2;
179                         }
180                 }
181                 else
182                 {
183                         if (self.owner.BUTTON_ATCK2)
184                                 self.rl_detonate_later = TRUE;
185                         if(self.rl_detonate_later)
186                                 W_Rocket_RemoteExplode();
187                 }
188         }
189 }
190
191 void W_Rocket_Touch (void)
192 {
193         if(self.owner && self.owner.lastrocket == self)
194                 self.owner.lastrocket = world;
195         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
196         {
197                 sound (self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
198                 remove(self);
199                 return;
200         }
201         W_Rocket_Explode ();
202 }
203
204 void W_Rocket_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
205 {
206         self.health = self.health - damage;
207         self.angles = vectoangles(self.velocity);
208         if (self.health <= 0)
209         {
210                 self.owner = attacker;
211                 W_Rocket_Explode();
212         }
213 }
214
215 void W_Rocket_Attack (void)
216 {
217         local entity missile;
218         local entity flash, flash2;
219
220         if not(self.items & IT_UNLIMITED_AMMO)
221                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_rocketlauncher_ammo");
222
223         W_SetupShot (self, '25 3 -4', FALSE, 5, "weapons/rocket_fire.wav");
224         //W_SetupShot (self, '25 8 -8', FALSE, 5, "weapons/rocket_fire.wav"); // TODO: move model to the right a little
225         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
226
227         missile = spawn ();
228         missile.owner = self;
229         self.lastrocket = missile;
230         if(cvar("g_balance_rocketlauncher_detonatedelay") >= 0)
231                 missile.spawnshieldtime = time + cvar("g_balance_rocketlauncher_detonatedelay");
232         else
233                 missile.spawnshieldtime = -1;
234         missile.classname = "missile";
235         missile.bot_dodge = TRUE;
236         missile.bot_dodgerating = cvar("g_balance_rocketlauncher_primary_damage") * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
237
238         missile.takedamage = DAMAGE_YES;
239         missile.damageforcescale = 4;
240         missile.health = cvar("g_balance_rocketlauncher_health");
241         missile.event_damage = W_Rocket_Damage;
242
243         missile.movetype = MOVETYPE_FLY;
244         missile.solid = SOLID_BBOX;
245         missile.projectiledeathtype = WEP_ROCKET_LAUNCHER;
246         setmodel (missile, "models/rocket.md3"); // precision set below
247         setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
248
249         setorigin (missile, w_shotorg - v_forward * 3); // move it back so it hits the wall at the right point
250         if(g_laserguided_missile && self.laser_on)
251                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_laserguided_speedstart");
252         else
253                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_speedstart");
254         W_SetupProjectileVelocity(missile);
255         missile.angles = vectoangles (missile.velocity);
256
257         missile.touch = W_Rocket_Touch;
258         missile.think = W_Rocket_Think;
259         missile.nextthink = time;
260         missile.cnt = time + cvar("g_balance_rocketlauncher_lifetime");
261         missile.effects = EF_LOWPRECISION;
262         sound (missile, CHAN_PROJECTILE, "weapons/rocket_fly.wav", 0.4 * VOL_BASE, ATTN_NORM);
263         missile.flags = FL_PROJECTILE;
264
265         // muzzle flash for 1st person view
266         flash = spawn ();
267         flash.scale = 1.2;
268         flash.angles_z = 180;
269         flash.owner = self;
270         flash.viewmodelforclient = self;
271         flash.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
272         setorigin (flash, '35 8 0');
273         setmodel (flash, "models/flash.md3"); // precision set below
274         setattachment(flash, self.weaponentity, "bone01");
275         SUB_SetFade (flash, time, 0.4);
276
277         // muzzle flash for 3rd person view
278         flash2 = spawn ();
279         flash2.scale = 0.8;
280         flash2.angles_y = 180;
281         flash2.angles_z = 90;
282         setorigin (flash2, '42 0 5');
283         setmodel (flash2, "models/flash.md3"); // precision set below
284         setattachment(flash2, self.exteriorweaponentity, "");
285         SUB_SetFade (flash2, time, 0.4);
286
287         // common properties
288         flash.effects = flash2.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
289 }
290
291 void spawnfunc_weapon_rocketlauncher (void); // defined in t_items.qc
292
293 float w_rlauncher(float req)
294 {
295         if (req == WR_AIM)
296         {
297                 // aim and decide to fire if appropriate
298                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_rocketlauncher_speed"), 0, cvar("g_balance_rocketlauncher_lifetime"), FALSE);
299                 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
300                 {
301                         // decide whether to detonate rockets
302                         local entity missile, targetlist, targ;
303                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
304                         local float selfdamage, teamdamage, enemydamage;
305                         edgedamage = cvar("g_balance_rocketlauncher_edgedamage");
306                         coredamage = cvar("g_balance_rocketlauncher_damage");
307                         edgeradius = cvar("g_balance_rocketlauncher_radius");
308                         recipricoledgeradius = 1 / edgeradius;
309                         selfdamage = 0;
310                         teamdamage = 0;
311                         enemydamage = 0;
312                         targetlist = findchainfloat(bot_attack, TRUE);
313                         missile = find(world, classname, "missile");
314                         while (missile)
315                         {
316                                 if (missile.owner != self)
317                                 {
318                                         missile = find(missile, classname, "missile");
319                                         continue;
320                                 }
321                                 targ = targetlist;
322                                 while (targ)
323                                 {
324                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - missile.origin);
325                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
326                                         // count potential damage according to type of target
327                                         if (targ == self)
328                                                 selfdamage = selfdamage + d;
329                                         else if (targ.team == self.team && teamplay)
330                                                 teamdamage = teamdamage + d;
331                                         else if (bot_shouldattack(targ))
332                                                 enemydamage = enemydamage + d;
333                                         targ = targ.chain;
334                                 }
335                                 missile = find(missile, classname, "missile");
336                         }
337                         local float desirabledamage;
338                         desirabledamage = enemydamage;
339                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
340                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
341                         if (self.team && teamplay == 2)
342                                 desirabledamage = desirabledamage - teamdamage;
343
344                         missile = find(world, classname, "missile");
345                         while (missile)
346                         {
347                                 if (missile.owner != self)
348                                 {
349                                         missile = find(missile, classname, "missile");
350                                         continue;
351                                 }
352                                 makevectors(missile.v_angle);
353                                 targ = targetlist;
354                                 if (skill > 9) // normal players only do this for the target they are tracking
355                                 {
356                                         targ = targetlist;
357                                         while (targ)
358                                         {
359                                                 if (
360                                                         (v_forward * normalize(missile.origin - targ.origin)< 0.1)
361                                                         && desirabledamage > 0.1*coredamage
362                                                 )self.BUTTON_ATCK2 = TRUE;
363                                                 targ = targ.chain;
364                                         }
365                                 }else{
366                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
367                                         //As the distance gets larger, a correct detonation gets near imposible
368                                         //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
369                                         if(v_forward * normalize(missile.origin - self.enemy.origin)< 0.1)
370                                                 if(self.enemy.classname == "player")
371                                                         if(desirabledamage >= 0.1*coredamage)
372                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
373                                                                         self.BUTTON_ATCK2 = TRUE;
374                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
375                                 }
376
377                                 missile = find(missile, classname, "missile");
378                         }
379                         // if we would be doing at X percent of the core damage, detonate it
380                         // but don't fire a new shot at the same time!
381                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
382                                 self.BUTTON_ATCK2 = TRUE;
383                         if ((skill > 6.5) && (selfdamage > self.health))
384                                 self.BUTTON_ATCK2 = FALSE;
385                         //if(self.BUTTON_ATCK2 == TRUE)
386                         //      dprint(ftos(desirabledamage),"\n");
387                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
388                 }
389         }
390         else if (req == WR_THINK)
391         {
392                 if (self.BUTTON_ATCK)
393                 if (weapon_prepareattack(0, cvar("g_balance_rocketlauncher_refire")))
394                 {
395                         W_Rocket_Attack();
396                         weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_rocketlauncher_animtime"), w_ready);
397                 }
398                 if (self.BUTTON_ATCK2)
399                 if(time > self.rl_sound)
400                 {
401                         self.rl_sound = time + 1;
402                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
403                         sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", 0.5 * VOL_BASE, ATTN_NORM);
404                 }
405                 if (self.BUTTON_ATCK2)
406                 if(g_laserguided_missile)
407                 if(self.exteriorweaponentity.attack_finished_single < time)
408                 {
409                         self.exteriorweaponentity.attack_finished_single = time + 0.4;
410                         self.laser_on = !self.laser_on;
411                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
412                         sound (self, CHAN_WEAPON2, "weapons/tink1.wav", VOL_BASE, ATTN_NORM);
413                 }
414         }
415         else if (req == WR_PRECACHE)
416         {
417                 precache_model ("models/flash.md3");
418                 precache_model ("models/rocket.md3");
419                 precache_model ("models/weapons/g_rl.md3");
420                 precache_model ("models/weapons/v_rl.md3");
421                 precache_model ("models/weapons/w_rl.zym");
422                 precache_sound ("weapons/rocket_det.wav");
423                 precache_sound ("weapons/rocket_fire.wav");
424                 precache_sound ("weapons/rocket_fly.wav");
425                 precache_sound ("weapons/rocket_impact.wav");
426                 if (g_laserguided_missile)
427                         precache_model ("models/laser_dot.mdl"); // rocket launcher
428         }
429         else if (req == WR_SETUP)
430                 weapon_setup(WEP_ROCKET_LAUNCHER);
431         else if (req == WR_CHECKAMMO1)
432         {
433                 // don't switch while guiding a missile
434                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_ROCKET_LAUNCHER)
435                         && self.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
436                         return FALSE;
437         }
438         else if (req == WR_CHECKAMMO2)
439                 return FALSE;
440         else if (req == WR_SUICIDEMESSAGE)
441                 w_deathtypestring = "exploded";
442         else if (req == WR_KILLMESSAGE)
443         {
444                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
445                         w_deathtypestring = "got too close to #'s rocket";
446                 else if(w_deathtype & HITTYPE_SPLASH)
447                         w_deathtypestring = "almost dodged #'s rocket";
448                 else
449                         w_deathtypestring = "ate #'s rocket";
450         }
451         return TRUE;
452 };