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