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