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