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