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