]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
make use of EF_TELEPORT_BIT for weapon animation
[divverent/nexuiz.git] / data / qcsrc / server / cl_weaponsystem.qc
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 void W_SwitchWeapon_Force(entity e, float w)
11 {
12         e.cnt = e.switchweapon;
13         e.switchweapon = w;
14 }
15
16 .float antilag_debug;
17
18 // VorteX: static frame globals
19 float WFRAME_FIRE1 = 0;
20 float WFRAME_FIRE2 = 1;
21 float WFRAME_IDLE = 2;
22 float WFRAME_RELOAD = 3;
23
24 void(float fr, float t, void() func) weapon_thinkf;
25
26 vector w_shotorg;
27 vector w_shotdir;
28
29 // this function calculates w_shotorg and w_shotdir based on the weapon model
30 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
31 // make sure you call makevectors first (FIXME?)
32 void W_SetupShot(entity ent, vector vecs, float antilag, float recoil, string snd)
33 {
34         float nudge = 1; // added to traceline target and subtracted from result
35         local vector trueaimpoint;
36         local float oldsolid;
37         oldsolid = self.dphitcontentsmask;
38         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
39         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
40         trueaimpoint = trace_endpos;
41
42         if(debug_shotorg != '0 0 0')
43                 vecs = debug_shotorg;
44
45         if (cvar("g_shootfromeye"))
46                 w_shotorg = ent.origin + ent.view_ofs;
47         else if (cvar("g_shootfromcenter"))
48                 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
49         else
50                 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
51         // now move the shotorg forward as much as requested if possible
52         traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
53         w_shotorg = trace_endpos - v_forward * nudge;
54         // calculate the shotdir from the chosen shotorg
55         w_shotdir = normalize(trueaimpoint - w_shotorg);
56
57 #if 0
58         // explanation of g_antilag:
59         // if client reports it was aiming at a player, and the serverside trace
60         // says it would miss, change the aim point to the player's new origin,
61         // but only if the shot at the player's new origin would hit of course
62         //
63         // FIXME: a much better method for bullet weapons would be to leave a
64         // trail of lagged 'ghosts' behind players, and see if the bullet hits the
65         // ghost corresponding to this player's ping time, and if so it would do
66         // damage to the real player
67         if (antilag)
68         if (self.cursor_trace_ent)                 // client was aiming at someone
69         if (self.cursor_trace_ent != self)         // just to make sure
70         if (self.cursor_trace_ent.takedamage)      // and that person is killable
71         if (self.cursor_trace_ent.classname == "player") // and actually a player
72         if (cvar("g_antilag") == 1)
73         {
74                 // verify that the shot would miss without antilag
75                 // (avoids an issue where guns would always shoot at their origin)
76                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
77                 if (!trace_ent.takedamage)
78                 {
79                         // verify that the shot would hit if altered
80                         traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
81                         if (trace_ent == self.cursor_trace_ent)
82                         {
83                                 // verify that the shot would hit in the past
84                                 if(self.antilag_debug)
85                                         antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
86                                 else
87                                         antilag_takeback(self.cursor_trace_ent, time - ANTILAG_LATENCY(self));
88
89                                 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
90                                 antilag_restore(self.cursor_trace_ent);
91
92                                 if(trace_ent == self.cursor_trace_ent)
93                                 {
94                                         // HIT!
95                                         w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
96                                         dprint("ANTILAG HIT for ", self.netname, "\n");
97                                 }
98                                 else
99                                 {
100                                         // prydon cursor aimbot or odd network conditions
101                                         dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
102                                 }
103
104                                 if(cvar("developer") >= 2)
105                                 {
106                                         vector v, vplus, vel;
107                                         float X;
108                                         v     = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self)       ));
109                                         vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self) + 0.01));
110                                         vel = (vplus - v) * (1 / 0.01);
111                                         // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
112                                         v     -= (self.origin + self.view_ofs);
113                                         // solve: v + X * vel = closest to v_forward axis
114                                         // project into 2D by subtracting v_forward components:
115                                         v   -= (v   * v_forward) * v_forward;
116                                         vel -= (vel * v_forward) * v_forward;
117                                         // solve: v + X * vel = closest to origin
118                                         // (v + X * vel)^2 closest to 0
119                                         // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
120                                         X = -(v * vel) / (vel * vel);
121                                         dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(self) + X), " instead of ", ftos(ANTILAG_LATENCY(self)), "\n");
122                                 }
123                         }
124                 }
125         }
126 #else
127         if (antilag)
128         {
129                 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
130                 {
131                         traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
132                         if (!trace_ent.takedamage)
133                         {
134                                 traceline_antilag_force (self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self, ANTILAG_LATENCY(self));
135                                 if (trace_ent.takedamage && trace_ent.classname == "player")
136                                 {
137                                         entity e;
138                                         e = trace_ent;
139                                         traceline(w_shotorg, e.origin, MOVE_NORMAL, self);
140                                         if(trace_ent == e)
141                                                 w_shotdir = normalize(trace_ent.origin - w_shotorg);
142                                 }
143                         }
144                 }
145                 else if(cvar("g_antilag") == 3) // client side hitscan
146                 {
147                         if (self.cursor_trace_ent)                 // client was aiming at someone
148                         if (self.cursor_trace_ent != self)         // just to make sure
149                         if (self.cursor_trace_ent.takedamage)      // and that person is killable
150                         if (self.cursor_trace_ent.classname == "player") // and actually a player
151                         {
152                                 // verify that the shot would miss without antilag
153                                 // (avoids an issue where guns would always shoot at their origin)
154                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
155                                 if (!trace_ent.takedamage)
156                                 {
157                                         // verify that the shot would hit if altered
158                                         traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
159                                         if (trace_ent == self.cursor_trace_ent)
160                                                 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
161                                         else
162                                                 print("antilag fail\n");
163                                 }
164                         }
165                 }
166         }
167 #endif
168
169         self.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
170
171         if (!g_norecoil)
172                 self.punchangle_x = recoil * -1;
173
174         if (snd != "")
175         {
176                 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
177         }
178
179         if (self.items & IT_STRENGTH)
180         if (!g_minstagib)
181                 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
182 };
183
184 void LaserTarget_Think()
185 {
186         entity e;
187         vector offset;
188         float uselaser;
189         uselaser = 0;
190
191         // list of weapons that will use the laser, and the options that enable it
192         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
193                 uselaser = 1;
194         // example
195         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
196         //      uselaser = 1;
197
198
199
200         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
201         if(!uselaser)
202         {
203                 // rocket launcher isn't selected, so no laser target.
204                 if(self.lasertarget != world)
205                 {
206                         remove(self.lasertarget);
207                         self.lasertarget = world;
208                 }
209                 return;
210         }
211
212         if(!self.lasertarget)
213         {
214                 // we don't have a lasertarget entity, so spawn one
215                 //bprint("create laser target\n");
216                 e = self.lasertarget = spawn();
217                 e.owner = self.owner;                   // Its owner is my owner
218                 e.classname = "laser_target";
219                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
220                 setmodel(e, "models/laser_dot.mdl");    // what it looks like, precision set below
221                 e.scale = 1.25;                         // make it larger
222                 e.alpha = 0.25;                         // transparency
223                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
224                 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
225                 // make it dynamically glow
226                 // you should avoid over-using this, as it can slow down the player's computer.
227                 e.glow_color = 251; // red color
228                 e.glow_size = 12;
229         }
230         else
231                 e = self.lasertarget;
232
233         // move the laser dot to where the player is looking
234
235         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
236         offset = '0 0 26' + v_right*3;
237         traceline(self.owner.origin + offset, self.owner.origin + offset + v_forward * MAX_SHOT_DISTANCE, FALSE, self); // trace forward until you hit something, like a player or wall
238         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
239         if(trace_plane_normal != '0 0 0')
240                 e.angles = vectoangles(trace_plane_normal);
241         else
242                 e.angles = vectoangles(v_forward);
243 }
244
245 float CL_Weaponentity_CustomizeEntityForClient()
246 {
247         self.viewmodelforclient = self.owner;
248         if(other.classname == "spectator")
249                 if(other.enemy == self.owner)
250                         self.viewmodelforclient = other;
251         return TRUE;
252 }
253
254 .string weaponname;
255 void CL_Weaponentity_Think()
256 {
257         float tb;
258         self.nextthink = time;
259         if (intermission_running)
260                 self.frame = WFRAME_IDLE;
261         if (self.owner.weaponentity != self)
262         {
263                 remove(self);
264                 return;
265         }
266         if (self.owner.deadflag != DEAD_NO)
267         {
268                 self.model = "";
269                 return;
270         }
271         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
272         {
273                 self.cnt = self.owner.weapon;
274                 self.dmg = self.owner.modelindex;
275                 self.deadflag = self.owner.deadflag;
276                 if (self.owner.weaponname != "")
277                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
278                 else
279                         self.model = "";
280         }
281
282         tb = (self.effects & EF_TELEPORT_BIT);
283         self.effects = self.owner.effects | EF_LOWPRECISION;
284         self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
285         self.effects &~= EF_TELEPORT_BIT;
286         self.effects |= tb;
287
288         if(self.owner.alpha != 0)
289                 self.alpha = self.owner.alpha;
290         else
291                 self.alpha = 1;
292         
293         self.colormap = self.owner.colormap;
294
295         self.angles = '0 0 0';
296         local float f;
297         f = 0;
298         if (self.state == WS_RAISE)
299                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
300         else if (self.state == WS_DROP)
301                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
302         else if (self.state == WS_CLEAR)
303                 f = 1;
304         self.angles_x = -90 * f * f;
305
306         // create or update the lasertarget entity
307         LaserTarget_Think();
308 };
309
310 void CL_ExteriorWeaponentity_Think()
311 {
312         float tag_found;
313         self.nextthink = time;
314         if (self.owner.exteriorweaponentity != self)
315         {
316                 remove(self);
317                 return;
318         }
319         if (self.owner.deadflag != DEAD_NO)
320         {
321                 self.model = "";
322                 return;
323         }
324         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
325         {
326                 self.cnt = self.owner.weapon;
327                 self.dmg = self.owner.modelindex;
328                 self.deadflag = self.owner.deadflag;
329                 if (self.owner.weaponname != "")
330                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
331                 else
332                         self.model = "";
333
334                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
335                 {
336                         self.tag_index = tag_found;
337                         self.tag_entity = self.owner;
338                 }
339                 else
340                         setattachment(self, self.owner, "bip01 r hand");
341
342                 // if that didn't find a tag, hide the exterior weapon model
343                 if (!self.tag_index)
344                         self.model = "";
345         }
346         self.effects = self.owner.effects | EF_LOWPRECISION;
347         self.effects = self.effects & EFMASK_CHEAP; // eat performance
348         if(self.owner.alpha != 0)
349                 self.alpha = self.owner.alpha;
350         else
351                 self.alpha = 1;
352         
353         self.colormap = self.owner.colormap;
354 };
355
356 // spawning weaponentity for client
357 void CL_SpawnWeaponentity()
358 {
359         self.weaponentity = spawn();
360         self.weaponentity.classname = "weaponentity";
361         self.weaponentity.solid = SOLID_NOT;
362         self.weaponentity.owner = self;
363         self.weaponentity.weaponentity = self.weaponentity;
364         setmodel(self.weaponentity, ""); // precision set when changed
365         self.weaponentity.origin = '0 0 0';
366         self.weaponentity.angles = '0 0 0';
367         self.weaponentity.viewmodelforclient = self;
368         self.weaponentity.flags = 0;
369         self.weaponentity.think = CL_Weaponentity_Think;
370         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
371         self.weaponentity.nextthink = time;
372         self.weaponentity.scale = 0.61;
373
374         self.exteriorweaponentity = spawn();
375         self.exteriorweaponentity.classname = "exteriorweaponentity";
376         self.exteriorweaponentity.solid = SOLID_NOT;
377         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
378         self.exteriorweaponentity.owner = self;
379         self.exteriorweaponentity.origin = '0 0 0';
380         self.exteriorweaponentity.angles = '0 0 0';
381         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
382         self.exteriorweaponentity.nextthink = time;
383 };
384
385 .float hasweapon_complain_spam;
386
387 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
388 {
389         local float weaponbit, f;
390         local entity oldself;
391
392         if(time < self.hasweapon_complain_spam)
393                 complain = 0;
394         if(complain)
395                 self.hasweapon_complain_spam = time + 0.2;
396
397         if (wpn < WEP_FIRST || wpn > WEP_LAST)
398         {
399                 if (complain)
400                         sprint(self, "Invalid weapon\n");
401                 return FALSE;
402         }
403         weaponbit = W_WeaponBit(wpn);
404         if (cl.weapons & weaponbit)
405         {
406                 if (andammo)
407                 {
408                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
409                         {
410                                 f = 1;
411                         }
412                         else
413                         {
414                                 oldself = self;
415                                 self = cl;
416                                 f = weapon_action(wpn, WR_CHECKAMMO1);
417                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
418                                 self = oldself;
419                         }
420                         if (!f)
421                         {
422                                 if (complain)
423                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
424                                 return FALSE;
425                         }
426                 }
427                 return TRUE;
428         }
429         if (complain)
430         {
431                 // DRESK - 3/16/07
432                 // Report Proper Weapon Status / Modified Weapon Ownership Message
433                 if(weaponsInMap & weaponbit)
434                 {
435                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
436
437                         if(cvar("g_showweaponspawns"))
438                         {
439                                 entity e;
440                                 string s;
441
442                                 e = get_weaponinfo(wpn);
443                                 s = e.model2;
444
445                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
446                                 {
447                                         if(e.classname == "droppedweapon")
448                                                 continue;
449                                         if not(e.flags & FL_ITEM)
450                                                 continue;
451                                         WaypointSprite_Spawn(
452                                                 s,
453                                                 1, 0,
454                                                 world, e.origin,
455                                                 self, 0,
456                                                 world, enemy,
457                                                 0
458                                         );
459                                 }
460                         }
461                 }
462                 else
463                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
464         }
465         return FALSE;
466 };
467
468 // Weapon subs
469 void w_clear()
470 {
471         if (self.weapon != -1)
472                 self.weapon = 0;
473         if (self.weaponentity)
474         {
475                 self.weaponentity.state = WS_CLEAR;
476                 self.weaponentity.effects = 0;
477         }
478 };
479
480 void w_ready()
481 {
482         if (self.weaponentity)
483                 self.weaponentity.state = WS_READY;
484         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
485 };
486
487 // Setup weapon for client (after this raise frame will be launched)
488 void weapon_setup(float windex)
489 {
490         entity e;
491         e = get_weaponinfo(windex);
492         self.items &~= IT_AMMO;
493         self.items = self.items | e.items;
494
495         // the two weapon entities will notice this has changed and update their models
496         self.weapon = windex;
497         self.weaponname = e.mdl;
498         self.bulletcounter = 0;
499 };
500
501 // perform weapon to attack (weaponstate and attack_finished check is here)
502 .float race_penalty;
503 float weapon_prepareattack(float secondary, float attacktime)
504 {
505         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
506         //if all players readied up and the countdown is running
507         if (cvar("sv_ready_restart_after_countdown"))
508                 if(time < game_starttime || time < self.race_penalty) {
509                         return FALSE;
510                 }
511         
512         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
513         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
514         {
515                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
516                 return FALSE;
517         }
518
519         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
520                 return FALSE;
521
522         // do not even think about shooting if switching
523         if(self.switchweapon != self.weapon)
524                 return FALSE;
525
526         // don't fire if previous attack is not finished
527         if(attacktime >= 0)
528                 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
529                         return FALSE;
530         // don't fire while changing weapon
531         if (self.weaponentity.state != WS_READY)
532                 return FALSE;
533         self.weaponentity.state = WS_INUSE;
534
535         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
536
537         // if the weapon hasn't been firing continuously, reset the timer
538         if(attacktime >= 0)
539         {
540                 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
541                 {
542                         ATTACK_FINISHED(self) = time;
543                         //dprint("resetting attack finished to ", ftos(time), "\n");
544                 }
545                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
546         }
547         self.bulletcounter += 1;
548         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
549         return TRUE;
550 };
551
552 void weapon_thinkf(float fr, float t, void() func)
553 {
554         if (fr >= 0)
555         {
556                 if (self.weaponentity != world)
557                 {
558                         if(fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
559                         {
560                                 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
561                         }
562                         self.weaponentity.frame = fr;
563                 }
564         }
565
566         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
567         {
568                 backtrace("Tried to override initial weapon think function - should this really happen?");
569         }
570
571         if(g_runematch)
572         {
573                 if(self.runes & RUNE_SPEED)
574                 {
575                         if(self.runes & CURSE_SLOW)
576                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
577                         else
578                                 t = t * cvar("g_balance_rune_speed_atkrate");
579                 }
580                 else if(self.runes & CURSE_SLOW)
581                 {
582                         t = t * cvar("g_balance_curse_slow_atkrate");
583                 }
584         }
585
586         // VorteX: haste can be added here
587         if (self.weapon_think == w_ready)
588         {
589                 self.weapon_nextthink = time;
590                 //dprint("started firing at ", ftos(time), "\n");
591         }
592         if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
593         {
594                 self.weapon_nextthink = time;
595                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
596         }
597         self.weapon_nextthink = self.weapon_nextthink + t;
598         self.weapon_think = func;
599         //dprint("next ", ftos(self.weapon_nextthink), "\n");
600
601         if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
602         if (t)
603         if (!self.crouch) // shoot anim stands up, this looks bad
604         {
605                 local vector anim;
606                 anim = self.anim_shoot;
607                 anim_z = anim_y / t;
608                 player_setanim(anim, FALSE, TRUE, TRUE);
609         }
610 };
611
612 void weapon_boblayer1(float spd, vector org)
613 {
614         // VorteX: haste can be added here
615 };
616
617 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
618 {
619         vector mdirection;
620         float mspeed;
621         float outspeed;
622         float nstyle;
623         vector outvelocity;
624
625         mdirection = normalize(mvelocity);
626         mspeed = vlen(mvelocity);
627
628         nstyle = cvar("g_projectiles_newton_style");
629         if(nstyle == 0)
630         {
631                 // absolute velocity
632                 outvelocity = mvelocity;
633         }
634         else if(nstyle == 1)
635         {
636                 // true Newtonian projectiles
637                 outvelocity = pvelocity + mvelocity;
638         }
639         else if(nstyle == 2)
640         {
641                 // true Newtonian projectiles with automatic aim adjustment
642                 //
643                 // solve: |outspeed * mdirection - pvelocity| = mspeed
644                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
645                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
646                 // PLUS SIGN!
647                 // not defined?
648                 // then...
649                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
650                 // velocity without mdirection component > mspeed
651                 // fire at smallest possible mspeed that works?
652                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
653
654                 float D;
655                 float p;
656                 float q;
657                 p = mdirection * pvelocity;
658                 q = pvelocity * pvelocity - mspeed * mspeed;
659                 D = p * p - q;
660                 if(D < 0)
661                 {
662                         //dprint("impossible shot, adjusting\n");
663                         D = 0;
664                 }
665                 outspeed = p + sqrt(D);
666                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
667                 outvelocity = mdirection * outspeed;
668         }
669         else if(nstyle == 3)
670         {
671                 // pseudo-Newtonian:
672                 outspeed = mspeed + mdirection * pvelocity;
673                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
674                 outvelocity = mdirection * outspeed;
675         }
676         else if(nstyle == 4)
677         {
678                 // tZorkian:
679                 outspeed = mspeed + vlen(pvelocity);
680                 outvelocity = mdirection * outspeed;
681         }
682         else
683                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
684
685         return outvelocity;
686 }
687
688 void W_SetupProjectileVelocity(entity missile)
689 {
690         if(missile.owner == world)
691                 error("Unowned missile");
692
693         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
694 }