]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
also remove ammo counter after throwing the last weapon
[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         self.nextthink = time;
258         if (intermission_running)
259                 self.frame = WFRAME_IDLE;
260         if (self.owner.weaponentity != self)
261         {
262                 remove(self);
263                 return;
264         }
265         if (self.owner.deadflag != DEAD_NO)
266         {
267                 self.model = "";
268                 return;
269         }
270         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
271         {
272                 self.cnt = self.owner.weapon;
273                 self.dmg = self.owner.modelindex;
274                 self.deadflag = self.owner.deadflag;
275                 if (self.owner.weaponname != "")
276                         setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
277                 else
278                         self.model = "";
279         }
280         self.effects = self.owner.effects | EF_LOWPRECISION;
281         self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
282
283         if (self.owner.teleport_time)
284                 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
285                 self.effects &~= EF_NODRAW;
286
287         if(self.owner.alpha != 0)
288                 self.alpha = self.owner.alpha;
289         else
290                 self.alpha = 1;
291         
292         self.colormap = self.owner.colormap;
293
294         self.angles = '0 0 0';
295         local float f;
296         f = 0;
297         if (self.state == WS_RAISE)
298                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
299         else if (self.state == WS_DROP)
300                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
301         else if (self.state == WS_CLEAR)
302                 f = 1;
303         self.angles_x = -90 * f * f;
304
305         // create or update the lasertarget entity
306         LaserTarget_Think();
307 };
308
309 void CL_ExteriorWeaponentity_Think()
310 {
311         float tag_found;
312         self.nextthink = time;
313         if (self.owner.exteriorweaponentity != self)
314         {
315                 remove(self);
316                 return;
317         }
318         if (self.owner.deadflag != DEAD_NO)
319         {
320                 self.model = "";
321                 return;
322         }
323         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
324         {
325                 self.cnt = self.owner.weapon;
326                 self.dmg = self.owner.modelindex;
327                 self.deadflag = self.owner.deadflag;
328                 if (self.owner.weaponname != "")
329                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
330                 else
331                         self.model = "";
332
333                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
334                 {
335                         self.tag_index = tag_found;
336                         self.tag_entity = self.owner;
337                 }
338                 else
339                         setattachment(self, self.owner, "bip01 r hand");
340
341                 // if that didn't find a tag, hide the exterior weapon model
342                 if (!self.tag_index)
343                         self.model = "";
344         }
345         self.effects = self.owner.effects | EF_LOWPRECISION;
346         self.effects = self.effects & EFMASK_CHEAP; // eat performance
347         if(self.owner.alpha != 0)
348                 self.alpha = self.owner.alpha;
349         else
350                 self.alpha = 1;
351         
352         self.colormap = self.owner.colormap;
353 };
354
355 // spawning weaponentity for client
356 void CL_SpawnWeaponentity()
357 {
358         self.weaponentity = spawn();
359         self.weaponentity.classname = "weaponentity";
360         self.weaponentity.solid = SOLID_NOT;
361         self.weaponentity.owner = self;
362         self.weaponentity.weaponentity = self.weaponentity;
363         setmodel(self.weaponentity, ""); // precision set when changed
364         self.weaponentity.origin = '0 0 0';
365         self.weaponentity.angles = '0 0 0';
366         self.weaponentity.viewmodelforclient = self;
367         self.weaponentity.flags = 0;
368         self.weaponentity.think = CL_Weaponentity_Think;
369         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
370         self.weaponentity.nextthink = time;
371         self.weaponentity.scale = 0.61;
372
373         self.exteriorweaponentity = spawn();
374         self.exteriorweaponentity.classname = "exteriorweaponentity";
375         self.exteriorweaponentity.solid = SOLID_NOT;
376         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
377         self.exteriorweaponentity.owner = self;
378         self.exteriorweaponentity.origin = '0 0 0';
379         self.exteriorweaponentity.angles = '0 0 0';
380         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
381         self.exteriorweaponentity.nextthink = time;
382 };
383
384 .float hasweapon_complain_spam;
385
386 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
387 {
388         local float weaponbit, f;
389         local entity oldself;
390
391         if(time < self.hasweapon_complain_spam)
392                 complain = 0;
393         if(complain)
394                 self.hasweapon_complain_spam = time + 0.2;
395
396         if (wpn < WEP_FIRST || wpn > WEP_LAST)
397         {
398                 if (complain)
399                         sprint(self, "Invalid weapon\n");
400                 return FALSE;
401         }
402         weaponbit = W_WeaponBit(wpn);
403         if (cl.weapons & weaponbit)
404         {
405                 if (andammo)
406                 {
407                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
408                         {
409                                 f = 1;
410                         }
411                         else
412                         {
413                                 oldself = self;
414                                 self = cl;
415                                 f = weapon_action(wpn, WR_CHECKAMMO1);
416                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
417                                 self = oldself;
418                         }
419                         if (!f)
420                         {
421                                 if (complain)
422                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
423                                 return FALSE;
424                         }
425                 }
426                 return TRUE;
427         }
428         if (complain)
429         {
430                 // DRESK - 3/16/07
431                 // Report Proper Weapon Status / Modified Weapon Ownership Message
432                 if(weaponsInMap & weaponbit)
433                 {
434                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
435
436                         if(cvar("g_showweaponspawns"))
437                         {
438                                 entity e;
439                                 string s;
440
441                                 e = get_weaponinfo(wpn);
442                                 s = e.model2;
443
444                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
445                                 {
446                                         if(e.classname == "droppedweapon")
447                                                 continue;
448                                         if not(e.flags & FL_ITEM)
449                                                 continue;
450                                         WaypointSprite_Spawn(
451                                                 s,
452                                                 1, 0,
453                                                 world, e.origin,
454                                                 self, 0,
455                                                 world, enemy,
456                                                 0
457                                         );
458                                 }
459                         }
460                 }
461                 else
462                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
463         }
464         return FALSE;
465 };
466
467 // Weapon subs
468 void w_clear()
469 {
470         if (self.weapon != -1)
471                 self.weapon = 0;
472         if (self.weaponentity)
473         {
474                 self.weaponentity.state = WS_CLEAR;
475                 self.weaponentity.effects = 0;
476         }
477 };
478
479 void w_ready()
480 {
481         if (self.weaponentity)
482                 self.weaponentity.state = WS_READY;
483         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
484 };
485
486 // Setup weapon for client (after this raise frame will be launched)
487 void weapon_setup(float windex)
488 {
489         entity e;
490         e = get_weaponinfo(windex);
491         self.items &~= IT_AMMO;
492         self.items = self.items | e.items;
493
494         // the two weapon entities will notice this has changed and update their models
495         self.weapon = windex;
496         self.weaponname = e.mdl;
497         self.bulletcounter = 0;
498 };
499
500 // perform weapon to attack (weaponstate and attack_finished check is here)
501 .float race_penalty;
502 float weapon_prepareattack(float secondary, float attacktime)
503 {
504         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
505         //if all players readied up and the countdown is running
506         if (cvar("sv_ready_restart_after_countdown"))
507                 if(time < game_starttime || time < self.race_penalty) {
508                         return FALSE;
509                 }
510         
511         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
512         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
513         {
514                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
515                 return FALSE;
516         }
517
518         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
519                 return FALSE;
520
521         // do not even think about shooting if switching
522         if(self.switchweapon != self.weapon)
523                 return FALSE;
524
525         // don't fire if previous attack is not finished
526         if(attacktime >= 0)
527                 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
528                         return FALSE;
529         // don't fire while changing weapon
530         if (self.weaponentity.state != WS_READY)
531                 return FALSE;
532         self.weaponentity.state = WS_INUSE;
533
534         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
535
536         // if the weapon hasn't been firing continuously, reset the timer
537         if(attacktime >= 0)
538         {
539                 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
540                 {
541                         ATTACK_FINISHED(self) = time;
542                         //dprint("resetting attack finished to ", ftos(time), "\n");
543                 }
544                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
545         }
546         self.bulletcounter += 1;
547         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
548         return TRUE;
549 };
550
551 void weapon_thinkf(float fr, float t, void() func)
552 {
553         if (fr >= 0)
554         {
555                 if (self.weaponentity != world)
556                         self.weaponentity.frame = fr;
557         }
558
559         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
560         {
561                 backtrace("Tried to override initial weapon think function - should this really happen?");
562         }
563
564         if(g_runematch)
565         {
566                 if(self.runes & RUNE_SPEED)
567                 {
568                         if(self.runes & CURSE_SLOW)
569                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
570                         else
571                                 t = t * cvar("g_balance_rune_speed_atkrate");
572                 }
573                 else if(self.runes & CURSE_SLOW)
574                 {
575                         t = t * cvar("g_balance_curse_slow_atkrate");
576                 }
577         }
578
579         // VorteX: haste can be added here
580         if (self.weapon_think == w_ready)
581         {
582                 self.weapon_nextthink = time;
583                 //dprint("started firing at ", ftos(time), "\n");
584         }
585         if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
586         {
587                 self.weapon_nextthink = time;
588                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
589         }
590         self.weapon_nextthink = self.weapon_nextthink + t;
591         self.weapon_think = func;
592         //dprint("next ", ftos(self.weapon_nextthink), "\n");
593
594         if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
595         if (t)
596         if (!self.crouch) // shoot anim stands up, this looks bad
597         {
598                 local vector anim;
599                 anim = self.anim_shoot;
600                 anim_z = anim_y / t;
601                 player_setanim(anim, FALSE, TRUE, TRUE);
602         }
603 };
604
605 void weapon_boblayer1(float spd, vector org)
606 {
607         // VorteX: haste can be added here
608 };
609
610 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
611 {
612         vector mdirection;
613         float mspeed;
614         float outspeed;
615         float nstyle;
616         vector outvelocity;
617
618         mdirection = normalize(mvelocity);
619         mspeed = vlen(mvelocity);
620
621         nstyle = cvar("g_projectiles_newton_style");
622         if(nstyle == 0)
623         {
624                 // absolute velocity
625                 outvelocity = mvelocity;
626         }
627         else if(nstyle == 1)
628         {
629                 // true Newtonian projectiles
630                 outvelocity = pvelocity + mvelocity;
631         }
632         else if(nstyle == 2)
633         {
634                 // true Newtonian projectiles with automatic aim adjustment
635                 //
636                 // solve: |outspeed * mdirection - pvelocity| = mspeed
637                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
638                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
639                 // PLUS SIGN!
640                 // not defined?
641                 // then...
642                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
643                 // velocity without mdirection component > mspeed
644                 // fire at smallest possible mspeed that works?
645                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
646
647                 float D;
648                 float p;
649                 float q;
650                 p = mdirection * pvelocity;
651                 q = pvelocity * pvelocity - mspeed * mspeed;
652                 D = p * p - q;
653                 if(D < 0)
654                 {
655                         //dprint("impossible shot, adjusting\n");
656                         D = 0;
657                 }
658                 outspeed = p + sqrt(D);
659                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
660                 outvelocity = mdirection * outspeed;
661         }
662         else if(nstyle == 3)
663         {
664                 // pseudo-Newtonian:
665                 outspeed = mspeed + mdirection * pvelocity;
666                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
667                 outvelocity = mdirection * outspeed;
668         }
669         else if(nstyle == 4)
670         {
671                 // tZorkian:
672                 outspeed = mspeed + vlen(pvelocity);
673                 outvelocity = mdirection * outspeed;
674         }
675         else
676                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
677
678         return outvelocity;
679 }
680
681 void W_SetupProjectileVelocity(entity missile)
682 {
683         if(missile.owner == world)
684                 error("Unowned missile");
685
686         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
687 }