change all function declarations (except builtins and data types) to ANSI C-like...
[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_BODY, "weapons/rocket_impact.wav", 1, 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"), IT_ROCKET_LAUNCHER);
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                         W_Rocket_Explode ();
93                 }
94                 else
95                 {
96                 }
97         }
98 }
99
100 void W_Rocket_Think (void)
101 {
102         entity e;
103         vector desireddir, olddir, newdir;
104         float turnrate, velspeed;
105         self.nextthink = time;
106         if (time > self.cnt)
107         {
108                 W_Rocket_Explode ();
109                 return;
110         }
111
112         if(g_laserguided_missile)
113         {
114                 // accelerate
115                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
116                 velspeed = cvar("g_balance_rocketlauncher_laserguided_speed") - (self.velocity * v_forward);
117                 if (velspeed > 0)
118                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_laserguided_speedaccel") * frametime, velspeed);
119         }
120         else
121         {
122                 // accelerate
123                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
124                 velspeed = cvar("g_balance_rocketlauncher_speed") - (self.velocity * v_forward);
125                 if (velspeed > 0)
126                         self.velocity = self.velocity + v_forward * min(cvar("g_balance_rocketlauncher_speedaccel") * frametime, velspeed);
127         }
128
129         // laser guided, or remote detonation
130         if (self.owner.weapon == WEP_ROCKET_LAUNCHER)
131         {
132                 if(g_laserguided_missile)
133                 {
134                         if(!self.owner.BUTTON_ATCK)
135                                 self.ltime = -1; // indicate that the player has let go of the button
136
137
138                         if(self.owner.deadflag == DEAD_NO)
139                         if (self.owner.BUTTON_ATCK && self.ltime < 0) // if the player let go of the button and then pushed it again
140                                 self.rl_detonate_later = TRUE;
141                         if(self.rl_detonate_later)
142                                 W_Rocket_RemoteExplode();
143
144                         if(cvar("g_balance_rocketlauncher_laserguided_allow_steal"))
145                         {
146                                 if(self.owner.laser_on)
147                                 {
148                                         if(self.attack_finished_single < time)
149                                         {
150                                                 self.attack_finished_single = time + 0.2 + random()*0.3;
151                                                 self.enemy = FindLaserTarget(self, 0.7, 0.7);
152                                         }
153
154                                         if(!self.enemy)
155                                                 self.enemy = self.owner.weaponentity.lasertarget;
156                                 }
157                                 else self.enemy = world;
158                         }
159                         else // don't allow stealing: always target my owner's laser (if it exists)
160                                 self.enemy = self.owner.weaponentity.lasertarget;
161
162                         if(self.enemy != world)
163                         {
164                                 //bprint("Targeting ", self.enemy.owner.netname, "'s laser\n");
165                                 velspeed = vlen(self.velocity);
166                                 e = self.enemy;//self.owner.weaponentity.lasertarget;
167                                 turnrate = cvar("g_balance_rocketlauncher_laserguided_turnrate");//0.65;                                                // how fast to turn
168                                 desireddir = normalize(e.origin - self.origin);         // get direction from my position to the laser target
169                                 olddir = normalize(self.velocity);                                      // get my current direction
170                                 newdir = normalize((olddir + desireddir * turnrate) * 0.5);     // take the average of the 2 directions; not the best method but simple & easy
171                                 self.velocity = newdir * velspeed;                                      // make me fly in the new direction at my flight speed
172                                 self.angles = vectoangles(self.velocity);                       // turn model in the new flight direction
173
174                                 ATTACK_FINISHED(self.owner) = time + 0.2;
175                         }
176                 }
177                 else
178                 {
179                         if (self.owner.BUTTON_ATCK2)
180                                 self.rl_detonate_later = TRUE;
181                         if(self.rl_detonate_later)
182                                 W_Rocket_RemoteExplode();
183                 }
184         }
185 }
186
187 void W_Rocket_Touch (void)
188 {
189         if(self.owner && self.owner.lastrocket == self)
190                 self.owner.lastrocket = world;
191         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
192         {
193                 sound (self, CHAN_BODY, "misc/null.wav", 1, ATTN_NORM);
194                 remove(self);
195                 return;
196         }
197         W_Rocket_Explode ();
198 }
199
200 void W_Rocket_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
201 {
202         self.health = self.health - damage;
203         self.angles = vectoangles(self.velocity);
204         if (self.health <= 0)
205         {
206                 self.owner = attacker;
207                 W_Rocket_Explode();
208         }
209 }
210
211 void W_Rocket_Attack (void)
212 {
213         local entity missile;
214         local entity flash, flash2;
215
216         if (cvar("g_use_ammunition") && !g_rocketarena)
217                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_rocketlauncher_ammo");
218
219         W_SetupShot (self, '25 3 -4', FALSE, 5, "weapons/rocket_fire.wav");
220         //W_SetupShot (self, '25 8 -8', FALSE, 5, "weapons/rocket_fire.wav"); // TODO: move model to the right a little
221         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
222
223         missile = spawn ();
224         missile.owner = self;
225         self.lastrocket = missile;
226         if(cvar("g_balance_rocketlauncher_detonatedelay") >= 0)
227                 missile.spawnshieldtime = time + cvar("g_balance_rocketlauncher_detonatedelay");
228         else
229                 missile.spawnshieldtime = -1;
230         missile.classname = "missile";
231         missile.bot_dodge = TRUE;
232         missile.bot_dodgerating = cvar("g_balance_rocketlauncher_primary_damage") * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
233
234         missile.takedamage = DAMAGE_YES;
235         missile.damageforcescale = 4;
236         missile.health = 30;
237         missile.event_damage = W_Rocket_Damage;
238
239         missile.movetype = MOVETYPE_FLY;
240         missile.solid = SOLID_BBOX;
241         setmodel (missile, "models/rocket.md3"); // precision set below
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         if(g_laserguided_missile && self.laser_on)
246                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_laserguided_speedstart");
247         else
248                 missile.velocity = w_shotdir * cvar("g_balance_rocketlauncher_speedstart");
249         W_SetupProjectileVelocity(missile);
250         missile.angles = vectoangles (missile.velocity);
251
252         missile.touch = W_Rocket_Touch;
253         missile.think = W_Rocket_Think;
254         missile.nextthink = time;
255         missile.cnt = time + cvar("g_balance_rocketlauncher_lifetime");
256         missile.effects = EF_LOWPRECISION;
257         sound (missile, CHAN_BODY, "weapons/rocket_fly.wav", 0.4, ATTN_NORM);
258         missile.flags = FL_PROJECTILE;
259
260         // muzzle flash for 1st person view
261         flash = spawn ();
262         flash.scale = 1.2;
263         flash.angles_z = 180;
264         flash.owner = self;
265         flash.viewmodelforclient = self;
266         flash.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
267         setorigin (flash, '35 8 0');
268         setmodel (flash, "models/flash.md3"); // precision set below
269         setattachment(flash, self.weaponentity, "bone01");
270         SUB_SetFade (flash, time, 0.4);
271
272         // muzzle flash for 3rd person view
273         flash2 = spawn ();
274         flash2.scale = 0.8;
275         flash2.angles_y = 180;
276         flash2.angles_z = 90;
277         setorigin (flash2, '42 0 5');
278         setmodel (flash2, "models/flash.md3"); // precision set below
279         setattachment(flash2, self.exteriorweaponentity, "");
280         SUB_SetFade (flash2, time, 0.4);
281
282         // common properties
283         flash.effects = flash2.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
284 }
285
286 float w_rlauncher(float req)
287 {
288         if (req == WR_AIM)
289         {
290                 // aim and decide to fire if appropriate
291                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_rocketlauncher_speed"), 0, cvar("g_balance_rocketlauncher_lifetime"), FALSE);
292                 if(skill >= 2) // skill 0 and 1 bots won't detonate rockets!
293                 {
294                         // decide whether to detonate rockets
295                         local entity missile, targetlist, targ;
296                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
297                         local float selfdamage, teamdamage, enemydamage;
298                         edgedamage = cvar("g_balance_rocketlauncher_edgedamage");
299                         coredamage = cvar("g_balance_rocketlauncher_damage");
300                         edgeradius = cvar("g_balance_rocketlauncher_radius");
301                         recipricoledgeradius = 1 / edgeradius;
302                         selfdamage = 0;
303                         teamdamage = 0;
304                         enemydamage = 0;
305                         targetlist = findchainfloat(bot_attack, TRUE);
306                         missile = find(world, classname, "missile");
307                         while (missile)
308                         {
309                                 if (missile.owner != self)
310                                 {
311                                         missile = find(missile, classname, "missile");
312                                         continue;
313                                 }
314                                 targ = targetlist;
315                                 while (targ)
316                                 {
317                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - missile.origin);
318                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
319                                         // count potential damage according to type of target
320                                         if (targ == self)
321                                                 selfdamage = selfdamage + d;
322                                         else if (targ.team == self.team && teamplay)
323                                                 teamdamage = teamdamage + d;
324                                         else if (bot_shouldattack(targ))
325                                                 enemydamage = enemydamage + d;
326                                         targ = targ.chain;
327                                 }
328                                 missile = find(missile, classname, "missile");
329                         }
330                         local float desirabledamage;
331                         desirabledamage = enemydamage;
332                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
333                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
334                         if (self.team && teamplay == 2)
335                                 desirabledamage = desirabledamage - teamdamage;
336
337                         missile = find(world, classname, "missile");
338                         while (missile)
339                         {
340                                 if (missile.owner != self)
341                                 {
342                                         missile = find(missile, classname, "missile");
343                                         continue;
344                                 }
345                                 makevectors(missile.v_angle);
346                                 targ = targetlist;
347                                 if (skill > 9) // normal players only do this for the target they are tracking
348                                 {
349                                         targ = targetlist;
350                                         while (targ)
351                                         {
352                                                 if (
353                                                         (v_forward * normalize(missile.origin - targ.origin)< 0.1)
354                                                         && desirabledamage > 0.1*coredamage
355                                                 )self.BUTTON_ATCK2 = TRUE;
356                                                 targ = targ.chain;
357                                         }
358                                 }else{
359                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
360                                         //As the distance gets larger, a correct detonation gets near imposible
361                                         //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
362                                         if(v_forward * normalize(missile.origin - self.enemy.origin)< 0.1)
363                                                 if(self.enemy.classname == "player")
364                                                         if(desirabledamage >= 0.1*coredamage)
365                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
366                                                                         self.BUTTON_ATCK2 = TRUE;
367                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
368                                 }
369
370                                 missile = find(missile, classname, "missile");
371                         }
372                         // if we would be doing at X percent of the core damage, detonate it
373                         // but don't fire a new shot at the same time!
374                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
375                                 self.BUTTON_ATCK2 = TRUE;
376                         if ((skill > 6.5) && (selfdamage > self.health))
377                                 self.BUTTON_ATCK2 = FALSE;
378                         //if(self.BUTTON_ATCK2 == TRUE)
379                         //      dprint(ftos(desirabledamage),"\n");
380                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
381                 }
382         }
383         else if (req == WR_THINK)
384         {
385                 if (self.BUTTON_ATCK)
386                 if (weapon_prepareattack(0, cvar("g_balance_rocketlauncher_refire")))
387                 {
388                         W_Rocket_Attack();
389                         weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_rocketlauncher_animtime"), w_ready);
390                 }
391                 if (self.BUTTON_ATCK2)
392                 if(time > self.rl_sound)
393                 {
394                         self.rl_sound = time + 1;
395                         sound (self, CHAN_BODY, "weapons/rocket_det.wav", 0.5, ATTN_NORM);
396                 }
397                 if (self.BUTTON_ATCK2)
398                 if(g_laserguided_missile)
399                 if(self.exteriorweaponentity.attack_finished_single < time)
400                 {
401                         self.exteriorweaponentity.attack_finished_single = time + 0.4;
402                         self.laser_on = !self.laser_on;
403                         sound (self, CHAN_AUTO, "weapons/tink1.wav", 1, ATTN_NORM);
404                 }
405         }
406         else if (req == WR_PRECACHE)
407         {
408                 precache_model ("models/flash.md3");
409                 precache_model ("models/rocket.md3");
410                 precache_model ("models/weapons/g_rl.md3");
411                 precache_model ("models/weapons/v_rl.md3");
412                 precache_model ("models/weapons/w_rl.zym");
413                 precache_sound ("weapons/rocket_det.wav");
414                 precache_sound ("weapons/rocket_fire.wav");
415                 precache_sound ("weapons/rocket_fly.wav");
416                 precache_sound ("weapons/rocket_impact.wav");
417                 if (g_laserguided_missile)
418                         precache_model ("models/laser_dot.mdl"); // rocket launcher
419         }
420         else if (req == WR_SETUP)
421                 weapon_setup(WEP_ROCKET_LAUNCHER, "rl", IT_ROCKETS);
422         else if (req == WR_CHECKAMMO1)
423         {
424                 // don't switch while guiding a missile
425                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_ROCKET_LAUNCHER)
426                         && self.ammo_rockets < cvar("g_balance_rocketlauncher_ammo"))
427                         return FALSE;
428         }
429         else if (req == WR_CHECKAMMO2)
430                 return FALSE;
431         else if (req == WR_REGISTER)
432                 weapon_register(WEP_ROCKET_LAUNCHER, cvar("g_balance_rocketlauncher_ammo"));
433         return TRUE;
434 };