]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
crosshair_hittest feature: blur out the crosshair if the shot can't hit the crosshair...
[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_DONTCHANGE = -1;
20 float WFRAME_FIRE1 = 0;
21 float WFRAME_FIRE2 = 1;
22 float WFRAME_IDLE = 2;
23 float WFRAME_RELOAD = 3;
24 .float wframe;
25
26 void(float fr, float t, void() func) weapon_thinkf;
27
28 vector w_shotorg;
29 vector w_shotdir;
30
31 // this function calculates w_shotorg and w_shotdir based on the weapon model
32 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
33 // make sure you call makevectors first (FIXME?)
34 void W_SetupShot(entity ent, float antilag, float recoil, string snd)
35 {
36         float nudge = 1; // added to traceline target and subtracted from result
37         local vector trueaimpoint;
38         local float oldsolid;
39         vector vecs;
40         oldsolid = ent.dphitcontentsmask;
41         ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
42         traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, ent);
43         trueaimpoint = trace_endpos;
44
45         if(ent.weaponentity.movedir_x > 0)
46         {
47                 vecs = ent.weaponentity.movedir;
48                 vecs_y = -vecs_y;
49         }
50         else
51                 vecs = '0 0 0';
52
53         if(debug_shotorg != '0 0 0')
54                 vecs = debug_shotorg;
55         
56         w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
57
58         // now move the shotorg forward as much as requested if possible
59         traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
60         w_shotorg = trace_endpos - v_forward * nudge;
61         // calculate the shotdir from the chosen shotorg
62         w_shotdir = normalize(trueaimpoint - w_shotorg);
63
64 #if 0
65         // explanation of g_antilag:
66         // if client reports it was aiming at a player, and the serverside trace
67         // says it would miss, change the aim point to the player's new origin,
68         // but only if the shot at the player's new origin would hit of course
69         //
70         // FIXME: a much better method for bullet weapons would be to leave a
71         // trail of lagged 'ghosts' behind players, and see if the bullet hits the
72         // ghost corresponding to this player's ping time, and if so it would do
73         // damage to the real player
74         if (antilag)
75         if (ent.cursor_trace_ent)                 // client was aiming at someone
76         if (ent.cursor_trace_ent != ent)         // just to make sure
77         if (ent.cursor_trace_ent.takedamage)      // and that person is killable
78         if (ent.cursor_trace_ent.classname == "player") // and actually a player
79         if (cvar("g_antilag") == 1)
80         {
81                 // verify that the shot would miss without antilag
82                 // (avoids an issue where guns would always shoot at their origin)
83                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
84                 if (!trace_ent.takedamage)
85                 {
86                         // verify that the shot would hit if altered
87                         traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
88                         if (trace_ent == ent.cursor_trace_ent)
89                         {
90                                 // verify that the shot would hit in the past
91                                 if(ent.antilag_debug)
92                                         antilag_takeback(ent.cursor_trace_ent, time - ent.antilag_debug);
93                                 else
94                                         antilag_takeback(ent.cursor_trace_ent, time - ANTILAG_LATENCY(ent));
95
96                                 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
97                                 antilag_restore(ent.cursor_trace_ent);
98
99                                 if(trace_ent == ent.cursor_trace_ent)
100                                 {
101                                         // HIT!
102                                         w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
103                                         dprint("ANTILAG HIT for ", ent.netname, "\n");
104                                 }
105                                 else
106                                 {
107                                         // prydon cursor aimbot or odd network conditions
108                                         dprint("WARNING: antilag ghost trace for ", ent.netname, " failed!\n");
109                                 }
110
111                                 if(cvar("developer") >= 2)
112                                 {
113                                         vector v, vplus, vel;
114                                         float X;
115                                         v     = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent)       ));
116                                         vplus = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) + 0.01));
117                                         vel = (vplus - v) * (1 / 0.01);
118                                         // solve: v + X * vel = closest to ent.origin + ent.view_ofs, v_forward axis
119                                         v     -= (ent.origin + ent.view_ofs);
120                                         // solve: v + X * vel = closest to v_forward axis
121                                         // project into 2D by subtracting v_forward components:
122                                         v   -= (v   * v_forward) * v_forward;
123                                         vel -= (vel * v_forward) * v_forward;
124                                         // solve: v + X * vel = closest to origin
125                                         // (v + X * vel)^2 closest to 0
126                                         // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
127                                         X = -(v * vel) / (vel * vel);
128                                         dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(ent) + X), " instead of ", ftos(ANTILAG_LATENCY(ent)), "\n");
129                                 }
130                         }
131                 }
132         }
133 #else
134         if (antilag)
135         {
136                 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
137                 {
138                         traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
139                         if (!trace_ent.takedamage)
140                         {
141                                 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
142                                 if (trace_ent.takedamage && trace_ent.classname == "player")
143                                 {
144                                         entity e;
145                                         e = trace_ent;
146                                         traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
147                                         if(trace_ent == e)
148                                                 w_shotdir = normalize(trace_ent.origin - w_shotorg);
149                                 }
150                         }
151                 }
152                 else if(cvar("g_antilag") == 3) // client side hitscan
153                 {
154                         if (ent.cursor_trace_ent)                 // client was aiming at someone
155                         if (ent.cursor_trace_ent != ent)         // just to make sure
156                         if (ent.cursor_trace_ent.takedamage)      // and that person is killable
157                         if (ent.cursor_trace_ent.classname == "player") // and actually a player
158                         {
159                                 // verify that the shot would miss without antilag
160                                 // (avoids an issue where guns would always shoot at their origin)
161                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
162                                 if (!trace_ent.takedamage)
163                                 {
164                                         // verify that the shot would hit if altered
165                                         traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
166                                         if (trace_ent == ent.cursor_trace_ent)
167                                                 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
168                                         else
169                                                 print("antilag fail\n");
170                                 }
171                         }
172                 }
173         }
174 #endif
175
176         ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
177
178         if (!g_norecoil)
179                 ent.punchangle_x = recoil * -1;
180
181         if (snd != "")
182         {
183                 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
184         }
185
186         if (ent.items & IT_STRENGTH)
187         if (!g_minstagib)
188                 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
189 };
190
191 void LaserTarget_Think()
192 {
193         entity e;
194         vector offset;
195         float uselaser;
196         uselaser = 0;
197
198         // list of weapons that will use the laser, and the options that enable it
199         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
200                 uselaser = 1;
201         // example
202         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
203         //      uselaser = 1;
204
205
206
207         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
208         if(!uselaser)
209         {
210                 // rocket launcher isn't selected, so no laser target.
211                 if(self.lasertarget != world)
212                 {
213                         remove(self.lasertarget);
214                         self.lasertarget = world;
215                 }
216                 return;
217         }
218
219         if(!self.lasertarget)
220         {
221                 // we don't have a lasertarget entity, so spawn one
222                 //bprint("create laser target\n");
223                 e = self.lasertarget = spawn();
224                 e.owner = self.owner;                   // Its owner is my owner
225                 e.classname = "laser_target";
226                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
227                 setmodel(e, "models/laser_dot.mdl");    // what it looks like, precision set below
228                 e.scale = 1.25;                         // make it larger
229                 e.alpha = 0.25;                         // transparency
230                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
231                 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
232                 // make it dynamically glow
233                 // you should avoid over-using this, as it can slow down the player's computer.
234                 e.glow_color = 251; // red color
235                 e.glow_size = 12;
236         }
237         else
238                 e = self.lasertarget;
239
240         // move the laser dot to where the player is looking
241
242         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
243         offset = '0 0 26' + v_right*3;
244         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
245         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
246         if(trace_plane_normal != '0 0 0')
247                 e.angles = vectoangles(trace_plane_normal);
248         else
249                 e.angles = vectoangles(v_forward);
250 }
251
252 float CL_Weaponentity_CustomizeEntityForClient()
253 {
254         self.viewmodelforclient = self.owner;
255         if(other.classname == "spectator")
256                 if(other.enemy == self.owner)
257                         self.viewmodelforclient = other;
258         return TRUE;
259 }
260
261 float qcweaponanimation;
262 vector weapon_offset = '0 -10 0';
263 vector weapon_adjust = '10 0 -15';
264 .vector weapon_morph0origin;
265 .vector weapon_morph0angles;
266 .float  weapon_morph0time;
267 .vector weapon_morph1origin;
268 .vector weapon_morph1angles;
269 .float  weapon_morph1time;
270 .vector weapon_morph2origin;
271 .vector weapon_morph2angles;
272 .float  weapon_morph2time;
273 .vector weapon_morph3origin;
274 .vector weapon_morph3angles;
275 .float  weapon_morph3time;
276 .vector weapon_morph4origin;
277 .vector weapon_morph4angles;
278 .float  weapon_morph4time;
279 .string weaponname;
280 #define QCWEAPONANIMATION_ORIGIN(e) ((weapon_offset_x + e.view_ofs_x) * v_forward - (weapon_offset_y + e.view_ofs_y) * v_right + (weapon_offset_z + e.view_ofs_z) * v_up + weapon_adjust)
281
282 /*
283  * supported formats:
284  *
285  * 1. simple animated model, muzzlr flash handling on h_ model:
286  *    h_tuba.dpm, h_tuba.dpm.animinfo - invisible model controlling the animation
287  *      tags:
288  *        shot = muzzle end (shot origin, also used for muzzle flashes)
289  *        shell = casings ejection point (must be on the right hand side of the gun)
290  *        weapon = attachment for v_tuba.md3
291  *    v_tuba.md3 - first and third person model
292  *    g_tuba.md3 - pickup model
293  *
294  * 2. fully animated model, muzzle flash handling on h_ model:
295  *    h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
296  *      tags:
297  *        shot = muzzle end (shot origin, also used for muzzle flashes)
298  *        shell = casings ejection point (must be on the right hand side of the gun)
299  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
300  *    v_tuba.md3 - third person model
301  *    g_tuba.md3 - pickup model
302  *
303  * 3. fully animated model, muzzle flash handling on v_ model:
304  *    h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
305  *      tags:
306  *        shot = muzzle end (shot origin)
307  *        shell = casings ejection point (must be on the right hand side of the gun)
308  *    v_tuba.md3 - third person model
309  *      tags:
310  *        shot = muzzle end (for muzzle flashes)
311  *    g_tuba.md3 - pickup model
312  */
313
314 void CL_Weaponentity_Think()
315 {
316         float tb, v_shot_idx;
317         self.nextthink = time;
318         if (intermission_running)
319                 self.frame = self.anim_idle_x;
320         if (self.owner.weaponentity != self)
321         {
322                 if (self.weaponentity)
323                         remove(self.weaponentity);
324                 remove(self);
325                 return;
326         }
327         if (self.owner.deadflag != DEAD_NO)
328         {
329                 self.model = "";
330                 if (self.weaponentity)
331                         self.weaponentity.model = "";
332                 return;
333         }
334         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
335         {
336                 self.cnt = self.owner.weapon;
337                 self.dmg = self.owner.modelindex;
338                 self.deadflag = self.owner.deadflag;
339
340                 string animfilename;
341                 float animfile;
342                 if (self.owner.weaponname != "")
343                 {
344                         // if there is a child entity, hide it until we're sure we use it
345                         if (self.weaponentity)
346                                 self.weaponentity.model = "";
347                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
348                         v_shot_idx = gettagindex(self, "shot"); // used later
349
350                         if(qcweaponanimation)
351                         {
352                                 self.angles = '0 0 0';
353                                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
354                                 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
355                                 self.movedir_x += 32;
356                                 self.spawnorigin = self.movedir;
357                                 // oldorigin - not calculated here
358                         }
359                         else
360                         {
361                                 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
362                                 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
363                                 animfile = fopen(animfilename, FILE_READ);
364                                 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
365                                 self.anim_fire1  = '0 1 0.01';
366                                 self.anim_fire2  = '1 1 0.01';
367                                 self.anim_idle   = '2 1 0.01';
368                                 self.anim_reload = '3 1 0.01';
369                                 if (animfile >= 0)
370                                 {
371                                         animparseerror = FALSE;
372                                         self.anim_fire1  = animparseline(animfile);
373                                         self.anim_fire2  = animparseline(animfile);
374                                         self.anim_idle   = animparseline(animfile);
375                                         self.anim_reload = animparseline(animfile);
376                                         fclose(animfile);
377                                         if (animparseerror)
378                                                 print("Parse error in ", animfilename, ", some player animations are broken\n");
379                                 }
380
381                                 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
382                                 // if we don't, this is a "real" animated model
383                                 if(gettagindex(self, "weapon"))
384                                 {
385                                         if (!self.weaponentity)
386                                                 self.weaponentity = spawn();
387                                         setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
388                                         setattachment(self.weaponentity, self, "weapon");
389                                 }
390                                 else
391                                 {
392                                         if(self.weaponentity)
393                                                 remove(self.weaponentity);
394                                         self.weaponentity = world;
395                                 }
396
397                                 self.origin = '0 0 0';
398                                 self.angles = '0 0 0';
399                                 self.frame = 0;
400                                 self.viewmodelforclient = world;
401
402                                 float idx;
403                                 idx = gettagindex(self, "shot");
404                                 if(idx)
405                                 {
406                                         self.movedir = gettaginfo(self, idx);
407                                 }
408                                 else
409                                 {
410                                         print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
411                                         self.movedir = '0 0 0';
412                                 }
413
414                                 idx = gettagindex(self, "shell");
415                                 if(idx)
416                                 {
417                                         self.spawnorigin = gettaginfo(self, idx);
418                                 }
419                                 else
420                                 {
421                                         print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
422                                         self.spawnorigin = self.movedir;
423                                 }
424
425                                 if(v_shot_idx)
426                                 {
427                                         self.oldorigin = '0 0 0';
428                                 }
429                                 else
430                                 {
431                                         if(self.weaponentity)
432                                                 idx = gettagindex(self, "weapon");
433                                         else
434                                                 idx = gettagindex(self, "handle");
435                                         if(idx)
436                                         {
437                                                 self.oldorigin = self.movedir - gettaginfo(self, idx);
438                                         }
439                                         else
440                                         {
441                                                 print("WARNING: weapon model ", self.model, " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
442                                                 self.oldorigin = '0 0 0'; // there is no way to recover from this
443                                         }
444                                 }
445
446                                 self.viewmodelforclient = self.owner;
447                         }
448                 }
449                 else
450                 {
451                         self.model = "";
452                         if(self.weaponentity)
453                                 remove(self.weaponentity);
454                         self.weaponentity = world;
455                         self.movedir = '0 0 0';
456                         self.spawnorigin = '0 0 0';
457                         self.oldorigin = '0 0 0';
458                         self.anim_fire1  = '0 1 0.01';
459                         self.anim_fire2  = '0 1 0.01';
460                         self.anim_idle   = '0 1 0.01';
461                         self.anim_reload = '0 1 0.01';
462                 }
463
464                 self.view_ofs = '0 0 0';
465
466                 if(self.movedir_x >= 0)
467                 {
468                         vector v0;
469                         v0 = self.movedir;
470                         self.movedir = shotorg_adjust(v0, FALSE, FALSE);
471                         self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
472                 }
473                 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
474                 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
475
476                 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
477
478                 // check if an instant weapon switch occurred
479                 if (qcweaponanimation)
480                 {
481                         if (self.state == WS_READY)
482                         {
483                                 self.angles = '0 0 0';
484                                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
485                                 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
486                         }
487                 }
488                 else
489                         setorigin(self, self.view_ofs);
490                 // reset animstate now
491                 self.wframe = WFRAME_IDLE;
492                 self.weapon_morph0time = 0;
493                 self.weapon_morph1time = 0;
494                 self.weapon_morph2time = 0;
495                 self.weapon_morph3time = 0;
496                 self.weapon_morph4time = 0;
497                 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
498         }
499
500         tb = (self.effects & EF_TELEPORT_BIT);
501         self.effects = self.owner.effects & EFMASK_CHEAP;
502         self.effects &~= EF_LOWPRECISION;
503         self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
504         self.effects &~= EF_TELEPORT_BIT;
505         self.effects |= tb;
506
507         if(self.owner.alpha != 0)
508                 self.alpha = self.owner.alpha;
509         else
510                 self.alpha = 1;
511         
512         self.colormap = self.owner.colormap;
513         if (self.weaponentity)
514         {
515                 self.weaponentity.effects = self.effects;
516                 self.weaponentity.alpha = self.alpha;
517                 self.weaponentity.colormap = self.colormap;
518         }
519
520         self.angles = '0 0 0';
521         local float f;
522         f = 0;
523         if (self.state == WS_RAISE)
524         {
525                 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
526                 self.angles_x = -90 * f * f;
527                 if (qcweaponanimation)
528                 {
529                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
530                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
531                 }
532         }
533         else if (self.state == WS_DROP)
534         {
535                 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
536                 self.angles_x = -90 * f * f;
537                 if (qcweaponanimation)
538                 {
539                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
540                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
541                 }
542         }
543         else if (self.state == WS_CLEAR)
544         {
545                 f = 1;
546                 self.angles_x = -90 * f * f;
547                 if (qcweaponanimation)
548                 {
549                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
550                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
551                 }
552         }
553         else if (qcweaponanimation && time < self.owner.weapon_morph1time)
554         {
555                 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
556                 f = 1 - pow(1 - f, 3);
557                 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
558                 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
559         }
560         else if (qcweaponanimation && time < self.owner.weapon_morph2time)
561         {
562                 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
563                 f = 1 - pow(1 - f, 3);
564                 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
565                 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
566         }
567         else if (qcweaponanimation && time < self.owner.weapon_morph3time)
568         {
569                 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
570                 f = 1 - pow(1 - f, 3);
571                 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
572                 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
573         }
574         else if (qcweaponanimation && time < self.owner.weapon_morph4time)
575         {
576                 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
577                 f = 1 - pow(1 - f, 3);
578                 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
579                 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
580         }
581         else if (qcweaponanimation)
582         {
583                 // begin a new idle morph
584                 self.owner.weapon_morph0time   = time;
585                 self.owner.weapon_morph0angles = self.angles;
586                 self.owner.weapon_morph0origin = self.origin;
587
588                 float r;
589                 float t;
590
591                 r = random();
592                 if (r < 0.1)
593                 {
594                         // turn gun to the left to look at it
595                         t = 2;
596                         self.owner.weapon_morph1time   = time + t * 0.2;
597                         self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
598                         makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
599                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
600
601                         self.owner.weapon_morph2time   = time + t * 0.6;
602                         self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
603                         makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
604                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
605
606                         self.owner.weapon_morph3time   = time + t;
607                         self.owner.weapon_morph3angles = '0 0 0';
608                         makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
609                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
610                 }
611                 else if (r < 0.2)
612                 {
613                         // raise the gun a bit
614                         t = 2;
615                         self.owner.weapon_morph1time   = time + t * 0.2;
616                         self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
617                         makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
618                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
619
620                         self.owner.weapon_morph2time   = time + t * 0.5;
621                         self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
622                         makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
623                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
624
625                         self.owner.weapon_morph3time   = time + t;
626                         self.owner.weapon_morph3angles = '0 0 0';
627                         makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
628                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
629                 }
630                 else if (r < 0.3)
631                 {
632                         // tweak it a bit
633                         t = 5;
634                         self.owner.weapon_morph1time   = time + t * 0.3;
635                         self.owner.weapon_morph1angles = randomvec() * 6;
636                         makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
637                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
638
639                         self.owner.weapon_morph2time   = time + t * 0.7;
640                         self.owner.weapon_morph2angles = randomvec() * 6;
641                         makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
642                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
643
644                         self.owner.weapon_morph3time   = time + t;
645                         self.owner.weapon_morph3angles = '0 0 0';
646                         makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
647                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
648                 }
649                 else
650                 {
651                         // hold it mostly steady
652                         t = random() * 6 + 4;
653                         self.owner.weapon_morph1time   = time + t * 0.2;
654                         self.owner.weapon_morph1angles = randomvec() * 1;
655                         makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
656                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
657
658                         self.owner.weapon_morph2time   = time + t * 0.5;
659                         self.owner.weapon_morph2angles = randomvec() * 1;
660                         makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
661                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
662
663                         self.owner.weapon_morph3time   = time + t * 0.7;
664                         self.owner.weapon_morph3angles = randomvec() * 1;
665                         makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
666                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
667                 }
668
669                 self.owner.weapon_morph4time   = time + t;
670                 self.owner.weapon_morph4angles = '0 0 0';
671                 makevectors(self.owner.weapon_morph4angles_x * '-1 0 0' + self.owner.weapon_morph4angles_y * '0 1 0' + self.owner.weapon_morph4angles_z * '0 0 1');
672                 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
673
674         }
675
676         // create or update the lasertarget entity
677         LaserTarget_Think();
678 };
679
680 void CL_ExteriorWeaponentity_Think()
681 {
682         float tag_found;
683         self.nextthink = time;
684         if (self.owner.exteriorweaponentity != self)
685         {
686                 remove(self);
687                 return;
688         }
689         if (self.owner.deadflag != DEAD_NO)
690         {
691                 self.model = "";
692                 return;
693         }
694         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
695         {
696                 self.cnt = self.owner.weapon;
697                 self.dmg = self.owner.modelindex;
698                 self.deadflag = self.owner.deadflag;
699                 if (self.owner.weaponname != "")
700                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
701                 else
702                         self.model = "";
703
704                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
705                 {
706                         self.tag_index = tag_found;
707                         self.tag_entity = self.owner;
708                 }
709                 else
710                         setattachment(self, self.owner, "bip01 r hand");
711
712                 // if that didn't find a tag, hide the exterior weapon model
713                 if (!self.tag_index)
714                         self.model = "";
715         }
716         self.effects = self.owner.effects | EF_LOWPRECISION;
717         self.effects = self.effects & EFMASK_CHEAP; // eat performance
718         if(self.owner.alpha != 0)
719                 self.alpha = self.owner.alpha;
720         else
721                 self.alpha = 1;
722         
723         self.colormap = self.owner.colormap;
724 };
725
726 // spawning weaponentity for client
727 void CL_SpawnWeaponentity()
728 {
729         self.weaponentity = spawn();
730         self.weaponentity.classname = "weaponentity";
731         self.weaponentity.solid = SOLID_NOT;
732         self.weaponentity.owner = self;
733         setmodel(self.weaponentity, ""); // precision set when changed
734         self.weaponentity.origin = '0 0 0';
735         self.weaponentity.angles = '0 0 0';
736         self.weaponentity.viewmodelforclient = self;
737         self.weaponentity.flags = 0;
738         self.weaponentity.think = CL_Weaponentity_Think;
739         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
740         self.weaponentity.nextthink = time;
741
742         self.exteriorweaponentity = spawn();
743         self.exteriorweaponentity.classname = "exteriorweaponentity";
744         self.exteriorweaponentity.solid = SOLID_NOT;
745         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
746         self.exteriorweaponentity.owner = self;
747         self.exteriorweaponentity.origin = '0 0 0';
748         self.exteriorweaponentity.angles = '0 0 0';
749         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
750         self.exteriorweaponentity.nextthink = time;
751 };
752
753 .float hasweapon_complain_spam;
754
755 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
756 {
757         local float weaponbit, f;
758         local entity oldself;
759
760         if(time < self.hasweapon_complain_spam)
761                 complain = 0;
762         if(complain)
763                 self.hasweapon_complain_spam = time + 0.2;
764
765         if (wpn < WEP_FIRST || wpn > WEP_LAST)
766         {
767                 if (complain)
768                         sprint(self, "Invalid weapon\n");
769                 return FALSE;
770         }
771         weaponbit = W_WeaponBit(wpn);
772         if (cl.weapons & weaponbit)
773         {
774                 if (andammo)
775                 {
776                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
777                         {
778                                 f = 1;
779                         }
780                         else
781                         {
782                                 oldself = self;
783                                 self = cl;
784                                 f = weapon_action(wpn, WR_CHECKAMMO1);
785                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
786                                 self = oldself;
787                         }
788                         if (!f)
789                         {
790                                 if (complain)
791                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
792                                 return FALSE;
793                         }
794                 }
795                 return TRUE;
796         }
797         if (complain)
798         {
799                 // DRESK - 3/16/07
800                 // Report Proper Weapon Status / Modified Weapon Ownership Message
801                 if(weaponsInMap & weaponbit)
802                 {
803                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
804
805                         if(cvar("g_showweaponspawns"))
806                         {
807                                 entity e;
808                                 string s;
809
810                                 e = get_weaponinfo(wpn);
811                                 s = e.model2;
812
813                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
814                                 {
815                                         if(e.classname == "droppedweapon")
816                                                 continue;
817                                         if not(e.flags & FL_ITEM)
818                                                 continue;
819                                         WaypointSprite_Spawn(
820                                                 s,
821                                                 1, 0,
822                                                 world, e.origin,
823                                                 self, 0,
824                                                 world, enemy,
825                                                 0
826                                         );
827                                 }
828                         }
829                 }
830                 else
831                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
832         }
833         return FALSE;
834 };
835
836 // Weapon subs
837 void w_clear()
838 {
839         if (self.weapon != -1)
840                 self.weapon = 0;
841         if (self.weaponentity)
842         {
843                 self.weaponentity.state = WS_CLEAR;
844                 self.weaponentity.effects = 0;
845         }
846 };
847
848 void w_ready()
849 {
850         if (self.weaponentity)
851                 self.weaponentity.state = WS_READY;
852         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
853 };
854
855 // Setup weapon for client (after this raise frame will be launched)
856 void weapon_setup(float windex)
857 {
858         entity e;
859         qcweaponanimation = cvar("sv_qcweaponanimation");
860         e = get_weaponinfo(windex);
861         self.items &~= IT_AMMO;
862         self.items = self.items | e.items;
863
864         // the two weapon entities will notice this has changed and update their models
865         self.weapon = windex;
866         self.weaponname = e.mdl;
867         self.bulletcounter = 0;
868 };
869
870 // perform weapon to attack (weaponstate and attack_finished check is here)
871 .float race_penalty;
872 float weapon_prepareattack(float secondary, float attacktime)
873 {
874         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
875         //if all players readied up and the countdown is running
876         if (cvar("sv_ready_restart_after_countdown"))
877                 if(time < game_starttime || time < self.race_penalty) {
878                         return FALSE;
879                 }
880         
881         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
882         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
883         {
884                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
885                 return FALSE;
886         }
887
888         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
889                 return FALSE;
890
891         // do not even think about shooting if switching
892         if(self.switchweapon != self.weapon)
893                 return FALSE;
894
895         // don't fire if previous attack is not finished
896         if(attacktime >= 0)
897                 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
898                         return FALSE;
899         // don't fire while changing weapon
900         if (self.weaponentity.state != WS_READY)
901                 return FALSE;
902         self.weaponentity.state = WS_INUSE;
903
904         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
905
906         // if the weapon hasn't been firing continuously, reset the timer
907         if(attacktime >= 0)
908         {
909                 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
910                 {
911                         ATTACK_FINISHED(self) = time;
912                         //dprint("resetting attack finished to ", ftos(time), "\n");
913                 }
914                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
915         }
916         self.bulletcounter += 1;
917         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
918         return TRUE;
919 };
920
921 void weapon_thinkf(float fr, float t, void() func)
922 {
923         vector a;
924         vector of, or, ou;
925         float restartanim;
926
927         if(fr == WFRAME_DONTCHANGE)
928         {
929                 fr = self.weaponentity.wframe;
930                 restartanim = FALSE;
931         }
932         else if (fr == WFRAME_IDLE)
933                 restartanim = FALSE;
934         else
935                 restartanim = TRUE;
936
937         of = v_forward;
938         or = v_right;
939         ou = v_up;
940
941         if (self.weaponentity)
942         {
943                 self.weaponentity.wframe = fr;
944                 if (qcweaponanimation)
945                 {
946                         if (fr != WFRAME_IDLE)
947                         {
948                                 self.weapon_morph0time = time;
949                                 self.weapon_morph0angles = self.weaponentity.angles;
950                                 self.weapon_morph0origin = self.weaponentity.origin;
951
952                                 self.weapon_morph1angles = '0 0 0';
953                                 self.weapon_morph1time = time + t;
954                                 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
955                                 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
956
957                                 self.weapon_morph2angles = '0 0 0';
958                                 self.weapon_morph2time = time + t;
959                                 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
960                                 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
961
962                                 self.weapon_morph3angles = '0 0 0';
963                                 self.weapon_morph3time = time + t;
964                                 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
965                                 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
966
967                                 self.weapon_morph4angles = '0 0 0';
968                                 self.weapon_morph4time = time + t;
969                                 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
970                                 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
971
972                                 if (fr == WFRAME_FIRE1)
973                                 {
974                                         self.weapon_morph1angles = '5 0 0';
975                                         self.weapon_morph1time = time + t * 0.1;
976                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
977                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
978                                         self.weapon_morph4time = time + t + 1; // delay idle effect
979                                 }
980                                 else if (fr == WFRAME_FIRE2)
981                                 {
982                                         self.weapon_morph1angles = '10 0 0';
983                                         self.weapon_morph1time = time + t * 0.1;
984                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
985                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
986                                         self.weapon_morph4time = time + t + 1; // delay idle effect
987                                 }
988                                 else if (fr == WFRAME_RELOAD)
989                                 {
990                                         self.weapon_morph1time = time + t * 0.05;
991                                         self.weapon_morph1angles = '-10 40 0';
992                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
993                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
994
995                                         self.weapon_morph2time = time + t * 0.15;
996                                         self.weapon_morph2angles = '-10 40 5';
997                                         makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
998                                         self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
999                 
1000                                         self.weapon_morph3time = time + t * 0.25;
1001                                         self.weapon_morph3angles = '-10 40 0';
1002                                         makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1003                                         self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1004                                 }
1005                         }
1006                 }
1007                 else
1008                 {
1009                         if (fr == WFRAME_IDLE)
1010                                 a = self.weaponentity.anim_idle;
1011                         else if (fr == WFRAME_FIRE1)
1012                                 a = self.weaponentity.anim_fire1;
1013                         else if (fr == WFRAME_FIRE2)
1014                                 a = self.weaponentity.anim_fire2;
1015                         else if (fr == WFRAME_RELOAD)
1016                                 a = self.weaponentity.anim_reload;
1017                         setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1018                 }
1019
1020                 if(restartanim)
1021                 {
1022                         BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
1023                 }
1024         }
1025
1026         v_forward = of;
1027         v_right = or;
1028         v_up = ou;
1029
1030         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1031         {
1032                 backtrace("Tried to override initial weapon think function - should this really happen?");
1033         }
1034
1035         if(g_runematch)
1036         {
1037                 if(self.runes & RUNE_SPEED)
1038                 {
1039                         if(self.runes & CURSE_SLOW)
1040                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
1041                         else
1042                                 t = t * cvar("g_balance_rune_speed_atkrate");
1043                 }
1044                 else if(self.runes & CURSE_SLOW)
1045                 {
1046                         t = t * cvar("g_balance_curse_slow_atkrate");
1047                 }
1048         }
1049
1050         // VorteX: haste can be added here
1051         if (self.weapon_think == w_ready)
1052         {
1053                 self.weapon_nextthink = time;
1054                 //dprint("started firing at ", ftos(time), "\n");
1055         }
1056         if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
1057         {
1058                 self.weapon_nextthink = time;
1059                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1060         }
1061         self.weapon_nextthink = self.weapon_nextthink + t;
1062         self.weapon_think = func;
1063         //dprint("next ", ftos(self.weapon_nextthink), "\n");
1064
1065         if (restartanim)
1066         if (t)
1067         if (!self.crouch) // shoot anim stands up, this looks bad
1068         {
1069                 local vector anim;
1070                 anim = self.anim_shoot;
1071                 anim_z = anim_y / t;
1072                 setanim(self, anim, FALSE, TRUE, TRUE);
1073         }
1074 };
1075
1076 void weapon_boblayer1(float spd, vector org)
1077 {
1078         // VorteX: haste can be added here
1079 };
1080
1081 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1082 {
1083         vector mdirection;
1084         float mspeed;
1085         float outspeed;
1086         float nstyle;
1087         vector outvelocity;
1088
1089         mdirection = normalize(mvelocity);
1090         mspeed = vlen(mvelocity);
1091
1092         nstyle = cvar("g_projectiles_newton_style");
1093         if(nstyle == 0)
1094         {
1095                 // absolute velocity
1096                 outvelocity = mvelocity;
1097         }
1098         else if(nstyle == 1)
1099         {
1100                 // true Newtonian projectiles
1101                 outvelocity = pvelocity + mvelocity;
1102         }
1103         else if(nstyle == 2)
1104         {
1105                 // true Newtonian projectiles with automatic aim adjustment
1106                 //
1107                 // solve: |outspeed * mdirection - pvelocity| = mspeed
1108                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1109                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1110                 // PLUS SIGN!
1111                 // not defined?
1112                 // then...
1113                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1114                 // velocity without mdirection component > mspeed
1115                 // fire at smallest possible mspeed that works?
1116                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1117
1118                 vector solution;
1119                 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1120                 if(solution_z)
1121                         outspeed = solution_y; // the larger one
1122                 else
1123                 {
1124                         //outspeed = 0; // slowest possible shot
1125                         outspeed = solution_x; // the real part (that is, the average!)
1126                         //dprint("impossible shot, adjusting\n");
1127                 }
1128
1129                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1130                 outvelocity = mdirection * outspeed;
1131         }
1132         else if(nstyle == 3)
1133         {
1134                 // pseudo-Newtonian:
1135                 outspeed = mspeed + mdirection * pvelocity;
1136                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1137                 outvelocity = mdirection * outspeed;
1138         }
1139         else if(nstyle == 4)
1140         {
1141                 // tZorkian:
1142                 outspeed = mspeed + vlen(pvelocity);
1143                 outvelocity = mdirection * outspeed;
1144         }
1145         else
1146                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1147
1148         return outvelocity;
1149 }
1150
1151 void W_SetupProjectileVelocity(entity missile)
1152 {
1153         if(missile.owner == world)
1154                 error("Unowned missile");
1155
1156         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1157 }
1158
1159 void W_AttachToShotorg(entity flash, vector offset)
1160 {
1161         entity xflash;
1162         flash.owner = self;
1163         flash.angles_z = random() * 360;
1164         if(qcweaponanimation)
1165         {
1166                 setorigin(flash, w_shotorg + w_shotdir * 50);
1167                 flash.angles = vectoangles(w_shotdir);
1168                 flash.angles_z = random() * 360;
1169         }
1170         else
1171         {
1172                 setattachment(flash, self.weaponentity, "shot");
1173                 setorigin(flash, offset);
1174
1175                 xflash = spawn();
1176                 copyentity(flash, xflash);
1177
1178                 flash.viewmodelforclient = self;
1179
1180                 if(self.weaponentity.oldorigin_x > 0)
1181                 {
1182                         setattachment(xflash, self.exteriorweaponentity, "");
1183                         setorigin(xflash, self.weaponentity.oldorigin + offset);
1184                 }
1185                 else
1186                 {
1187                         setattachment(xflash, self.exteriorweaponentity, "shot");
1188                 }
1189         }
1190 }