fix a typo
[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 .float weapon_frametime;
11
12 float W_WeaponRateFactor()
13 {
14         float t;
15         t = 1.0 / g_weaponratefactor;
16
17         if(g_runematch)
18         {
19                 if(self.runes & RUNE_SPEED)
20                 {
21                         if(self.runes & CURSE_SLOW)
22                                 t = t * cvar("g_balance_rune_speed_combo_atkrate");
23                         else
24                                 t = t * cvar("g_balance_rune_speed_atkrate");
25                 }
26                 else if(self.runes & CURSE_SLOW)
27                 {
28                         t = t * cvar("g_balance_curse_slow_atkrate");
29                 }
30         }
31
32         return t;
33 }
34
35 void W_SwitchWeapon_Force(entity e, float w)
36 {
37         e.cnt = e.switchweapon;
38         e.switchweapon = w;
39 }
40
41 .float antilag_debug;
42
43 // VorteX: static frame globals
44 float WFRAME_DONTCHANGE = -1;
45 float WFRAME_FIRE1 = 0;
46 float WFRAME_FIRE2 = 1;
47 float WFRAME_IDLE = 2;
48 float WFRAME_RELOAD = 3;
49 .float wframe;
50
51 void(float fr, float t, void() func) weapon_thinkf;
52
53 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
54 {
55         vector ret;
56         ret_x = screenright * v;
57         ret_y = screenup * v;
58         ret_z = screenforward * v;
59         return ret;
60 }
61
62 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
63 {
64         float i, j, k;
65         vector mi, ma, thisv, myv, ret;
66
67         myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
68
69         // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
70
71         for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
72         {
73                 thisv = targ.origin;
74                 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
75                 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
76                 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
77                 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
78                 if(i || j || k)
79                 {
80                         if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
81                         if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
82                         //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
83                 }
84                 else
85                 {
86                         // first run
87                         mi = ma = thisv;
88                 }
89         }
90
91         thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
92         ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
93         ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
94         ret_z = thisv_z - myv_z;
95         return ret;
96 }
97
98 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
99 {
100         vector hitplot;
101         vector org;
102         float lag;
103
104         if(player.hitplotfh >= 0)
105         {
106                 lag = ANTILAG_LATENCY(player);
107                 if(lag < 0.001)
108                         lag = 0;
109                 if(clienttype(player) != CLIENTTYPE_REAL)
110                         lag = 0; // only antilag for clients
111
112                 org = player.origin + player.view_ofs;
113                 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
114                 if(trace_ent.flags & FL_CLIENT)
115                 {
116                         antilag_takeback(trace_ent, time - lag);
117                         hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
118                         antilag_restore(trace_ent);
119                         fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
120                         //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
121                 }
122         }
123 }
124
125 vector w_shotorg;
126 vector w_shotdir;
127
128 // this function calculates w_shotorg and w_shotdir based on the weapon model
129 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
130 // make sure you call makevectors first (FIXME?)
131 void W_SetupShot_Dir_ProjectileSize(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float maxdamage)
132 {
133         float nudge = 1; // added to traceline target and subtracted from result
134         local vector trueaimpoint;
135         local float oldsolid;
136         vector vecs, dv;
137         oldsolid = ent.dphitcontentsmask;
138         if(ent.weapon == WEP_CAMPINGRIFLE)
139                 ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
140         else
141                 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
142         if(antilag)
143                 WarpZone_traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
144                 // passing world, because we do NOT want it to touch dphitcontentsmask
145         else
146                 WarpZone_TraceLine(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, ent);
147         ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
148
149         vector vf, vr, vu;
150         vf = v_forward;
151         vr = v_right;
152         vu = v_up;
153         trueaimpoint = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
154         v_forward = vf;
155         v_right = vr;
156         v_up = vu;
157
158         // track max damage
159         if not(inWarmupStage) {
160                 entity w;
161                 w = get_weaponinfo(ent.weapon);
162                 if(w.spawnflags & WEP_TYPE_SPLASH) {  // splash damage
163                         ent.stats_fired[ent.weapon - 1] += maxdamage;
164                         ent.stat_fired = ent.weapon + 64 * floor(ent.stats_fired[ent.weapon - 1]);
165                 }
166         }
167
168         W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
169
170         if(ent.weaponentity.movedir_x > 0)
171         {
172                 vecs = ent.weaponentity.movedir;
173                 vecs_y = -vecs_y;
174         }
175         else
176                 vecs = '0 0 0';
177
178         if(debug_shotorg != '0 0 0')
179                 vecs = debug_shotorg;
180
181         dv = v_right * vecs_y + v_up * vecs_z;
182         w_shotorg = ent.origin + ent.view_ofs + dv;
183
184         // now move the shotorg forward as much as requested if possible
185         if(antilag)
186         {
187                 if(ent.antilag_debug)
188                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
189                 else
190                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
191         }
192         else
193                 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
194         w_shotorg = trace_endpos - v_forward * nudge;
195         // calculate the shotdir from the chosen shotorg
196         w_shotdir = normalize(trueaimpoint - w_shotorg);
197
198         if (antilag)
199         if (!ent.cvar_cl_noantilag)
200         {
201                 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
202                 {
203                         traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
204                         if (!trace_ent.takedamage)
205                         {
206                                 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
207                                 if (trace_ent.takedamage && trace_ent.classname == "player")
208                                 {
209                                         entity e;
210                                         e = trace_ent;
211                                         traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
212                                         if(trace_ent == e)
213                                                 w_shotdir = normalize(trace_ent.origin - w_shotorg);
214                                 }
215                         }
216                 }
217                 else if(cvar("g_antilag") == 3) // client side hitscan
218                 {
219                         // this part MUST use prydon cursor
220                         if (ent.cursor_trace_ent)                 // client was aiming at someone
221                         if (ent.cursor_trace_ent != ent)         // just to make sure
222                         if (ent.cursor_trace_ent.takedamage)      // and that person is killable
223                         if (ent.cursor_trace_ent.classname == "player") // and actually a player
224                         {
225                                 // verify that the shot would miss without antilag
226                                 // (avoids an issue where guns would always shoot at their origin)
227                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
228                                 if (!trace_ent.takedamage)
229                                 {
230                                         // verify that the shot would hit if altered
231                                         traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
232                                         if (trace_ent == ent.cursor_trace_ent)
233                                                 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
234                                         else
235                                                 print("antilag fail\n");
236                                 }
237                         }
238                 }
239         }
240
241         ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
242
243         if (!g_norecoil)
244                 ent.punchangle_x = recoil * -1;
245
246         if (snd != "")
247         {
248                 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
249         }
250
251         if (ent.items & IT_STRENGTH)
252         if (!g_minstagib)
253                 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
254 };
255
256 #define W_SetupShot_ProjectileSize(ent,mi,ma,antilag,recoil,snd,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, v_forward, mi, ma, antilag, recoil, snd, maxdamage)
257 #define W_SetupShot_Dir(ent,s_forward,antilag,recoil,snd,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, s_forward, '0 0 0', '0 0 0', antilag, recoil, snd, maxdamage)
258 #define W_SetupShot(ent,antilag,recoil,snd,maxdamage) W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, maxdamage)
259
260 void LaserTarget_Think()
261 {
262         entity e;
263         vector offset;
264         float uselaser;
265         uselaser = 0;
266
267         // list of weapons that will use the laser, and the options that enable it
268         if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
269                 uselaser = 1;
270         // example
271         //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
272         //      uselaser = 1;
273
274
275
276         // if a laser-enabled weapon isn't selected, delete any existing laser and quit
277         if(!uselaser)
278         {
279                 // rocket launcher isn't selected, so no laser target.
280                 if(self.lasertarget != world)
281                 {
282                         remove(self.lasertarget);
283                         self.lasertarget = world;
284                 }
285                 return;
286         }
287
288         if(!self.lasertarget)
289         {
290                 // we don't have a lasertarget entity, so spawn one
291                 //bprint("create laser target\n");
292                 e = self.lasertarget = spawn();
293                 e.owner = self.owner;                   // Its owner is my owner
294                 e.classname = "laser_target";
295                 e.movetype = MOVETYPE_NOCLIP;   // don't touch things
296                 setmodel(e, "models/laser_dot.mdl");    // what it looks like, precision set below
297                 e.scale = 1.25;                         // make it larger
298                 e.alpha = 0.25;                         // transparency
299                 e.colormod = '255 0 0' * (1/255) * 8;   // change colors
300                 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
301                 // make it dynamically glow
302                 // you should avoid over-using this, as it can slow down the player's computer.
303                 e.glow_color = 251; // red color
304                 e.glow_size = 12;
305         }
306         else
307                 e = self.lasertarget;
308
309         // move the laser dot to where the player is looking
310
311         makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
312         offset = '0 0 26' + v_right*3;
313         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
314         setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
315         if(trace_plane_normal != '0 0 0')
316                 e.angles = vectoangles(trace_plane_normal);
317         else
318                 e.angles = vectoangles(v_forward);
319 }
320
321 float CL_Weaponentity_CustomizeEntityForClient()
322 {
323         self.viewmodelforclient = self.owner;
324         if(other.classname == "spectator")
325                 if(other.enemy == self.owner)
326                         self.viewmodelforclient = other;
327         return TRUE;
328 }
329
330 float qcweaponanimation;
331 vector weapon_offset = '0 -10 0';
332 vector weapon_adjust = '10 0 -15';
333 .vector weapon_morph0origin;
334 .vector weapon_morph0angles;
335 .float  weapon_morph0time;
336 .vector weapon_morph1origin;
337 .vector weapon_morph1angles;
338 .float  weapon_morph1time;
339 .vector weapon_morph2origin;
340 .vector weapon_morph2angles;
341 .float  weapon_morph2time;
342 .vector weapon_morph3origin;
343 .vector weapon_morph3angles;
344 .float  weapon_morph3time;
345 .vector weapon_morph4origin;
346 .vector weapon_morph4angles;
347 .float  weapon_morph4time;
348 .string weaponname;
349 #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)
350
351 /*
352  * supported formats:
353  *
354  * 1. simple animated model, muzzle flash handling on h_ model:
355  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
356  *      tags:
357  *        shot = muzzle end (shot origin, also used for muzzle flashes)
358  *        shell = casings ejection point (must be on the right hand side of the gun)
359  *        weapon = attachment for v_tuba.md3
360  *    v_tuba.md3 - first and third person model
361  *    g_tuba.md3 - pickup model
362  *
363  * 2. simple animated model, muzzle flash handling on v_ model:
364  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
365  *      tags:
366  *        weapon = attachment for v_tuba.md3
367  *    v_tuba.md3 - first and third person model
368  *      tags:
369  *        shot = muzzle end (shot origin, also used for muzzle flashes)
370  *        shell = casings ejection point (must be on the right hand side of the gun)
371  *    g_tuba.md3 - pickup model
372  *
373  * 3. fully animated model, muzzle flash handling on h_ model:
374  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
375  *      tags:
376  *        shot = muzzle end (shot origin, also used for muzzle flashes)
377  *        shell = casings ejection point (must be on the right hand side of the gun)
378  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
379  *    v_tuba.md3 - third person model
380  *    g_tuba.md3 - pickup model
381  *
382  * 4. fully animated model, muzzle flash handling on v_ model:
383  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
384  *      tags:
385  *        shot = muzzle end (shot origin)
386  *        shell = casings ejection point (must be on the right hand side of the gun)
387  *    v_tuba.md3 - third person model
388  *      tags:
389  *        shot = muzzle end (for muzzle flashes)
390  *    g_tuba.md3 - pickup model
391  */
392
393 void CL_Weaponentity_Think()
394 {
395         float tb, v_shot_idx;
396         self.nextthink = time;
397         if (intermission_running)
398                 self.frame = self.anim_idle_x;
399         if (self.owner.weaponentity != self)
400         {
401                 if (self.weaponentity)
402                         remove(self.weaponentity);
403                 remove(self);
404                 return;
405         }
406         if (self.owner.deadflag != DEAD_NO)
407         {
408                 self.model = "";
409                 if (self.weaponentity)
410                         self.weaponentity.model = "";
411                 return;
412         }
413         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
414         {
415                 self.cnt = self.owner.weapon;
416                 self.dmg = self.owner.modelindex;
417                 self.deadflag = self.owner.deadflag;
418
419                 string animfilename;
420                 float animfile;
421                 if (self.owner.weaponname != "")
422                 {
423                         // if there is a child entity, hide it until we're sure we use it
424                         if (self.weaponentity)
425                                 self.weaponentity.model = "";
426                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
427                         v_shot_idx = gettagindex(self, "shot"); // used later
428                         if(!v_shot_idx)
429                                 v_shot_idx = gettagindex(self, "tag_shot");
430
431                         if(qcweaponanimation)
432                         {
433                                 self.angles = '0 0 0';
434                                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
435                                 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
436                                 self.movedir_x += 32;
437                                 self.spawnorigin = self.movedir;
438                                 // oldorigin - not calculated here
439                         }
440                         else
441                         {
442                                 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
443                                 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
444                                 animfile = fopen(animfilename, FILE_READ);
445                                 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
446                                 self.anim_fire1  = '0 1 0.01';
447                                 self.anim_fire2  = '1 1 0.01';
448                                 self.anim_idle   = '2 1 0.01';
449                                 self.anim_reload = '3 1 0.01';
450                                 if (animfile >= 0)
451                                 {
452                                         animparseerror = FALSE;
453                                         self.anim_fire1  = animparseline(animfile);
454                                         self.anim_fire2  = animparseline(animfile);
455                                         self.anim_idle   = animparseline(animfile);
456                                         self.anim_reload = animparseline(animfile);
457                                         fclose(animfile);
458                                         if (animparseerror)
459                                                 print("Parse error in ", animfilename, ", some player animations are broken\n");
460                                 }
461
462                                 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
463                                 // if we don't, this is a "real" animated model
464                                 if(gettagindex(self, "weapon"))
465                                 {
466                                         if (!self.weaponentity)
467                                                 self.weaponentity = spawn();
468                                         setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
469                                         setattachment(self.weaponentity, self, "weapon");
470                                 }
471                                 else if(gettagindex(self, "tag_weapon"))
472                                 {
473                                         if (!self.weaponentity)
474                                                 self.weaponentity = spawn();
475                                         setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
476                                         setattachment(self.weaponentity, self, "tag_weapon");
477                                 }
478                                 else
479                                 {
480                                         if(self.weaponentity)
481                                                 remove(self.weaponentity);
482                                         self.weaponentity = world;
483                                 }
484
485                                 setorigin(self,'0 0 0');
486                                 self.angles = '0 0 0';
487                                 self.frame = 0;
488                                 self.viewmodelforclient = world;
489
490                                 float idx;
491
492                                 if(v_shot_idx) // v_ model attached to invisible h_ model
493                                 {
494                                         self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
495                                 }
496                                 else
497                                 {
498                                         idx = gettagindex(self, "shot");
499                                         if(!idx)
500                                                 idx = gettagindex(self, "tag_shot");
501                                         if(idx)
502                                                 self.movedir = gettaginfo(self, idx);
503                                         else
504                                         {
505                                                 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
506                                                 self.movedir = '0 0 0';
507                                         }
508                                 }
509
510                                 if(self.weaponentity) // v_ model attached to invisible h_ model
511                                 {
512                                         idx = gettagindex(self.weaponentity, "shell");
513                                         if(!idx)
514                                                 idx = gettagindex(self.weaponentity, "tag_shell");
515                                         if(idx)
516                                                 self.spawnorigin = gettaginfo(self.weaponentity, idx);
517                                 }
518                                 else
519                                         idx = 0;
520                                 if(!idx)
521                                 {
522                                         idx = gettagindex(self, "shell");
523                                         if(!idx)
524                                                 idx = gettagindex(self, "tag_shell");
525                                         if(idx)
526                                                 self.spawnorigin = gettaginfo(self, idx);
527                                         else
528                                         {
529                                                 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
530                                                 self.spawnorigin = self.movedir;
531                                         }
532                                 }
533
534                                 if(v_shot_idx)
535                                 {
536                                         self.oldorigin = '0 0 0'; // use regular attachment
537                                 }
538                                 else
539                                 {
540                                         if(self.weaponentity)
541                                         {
542                                                 idx = gettagindex(self, "weapon");
543                                                 if(!idx)
544                                                         idx = gettagindex(self, "tag_weapon");
545                                         }
546                                         else
547                                         {
548                                                 idx = gettagindex(self, "handle");
549                                                 if(!idx)
550                                                         idx = gettagindex(self, "tag_handle");
551                                         }
552                                         if(idx)
553                                         {
554                                                 self.oldorigin = self.movedir - gettaginfo(self, idx);
555                                         }
556                                         else
557                                         {
558                                                 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");
559                                                 self.oldorigin = '0 0 0'; // there is no way to recover from this
560                                         }
561                                 }
562
563                                 self.viewmodelforclient = self.owner;
564                         }
565                 }
566                 else
567                 {
568                         self.model = "";
569                         if(self.weaponentity)
570                                 remove(self.weaponentity);
571                         self.weaponentity = world;
572                         self.movedir = '0 0 0';
573                         self.spawnorigin = '0 0 0';
574                         self.oldorigin = '0 0 0';
575                         self.anim_fire1  = '0 1 0.01';
576                         self.anim_fire2  = '0 1 0.01';
577                         self.anim_idle   = '0 1 0.01';
578                         self.anim_reload = '0 1 0.01';
579                 }
580
581                 self.view_ofs = '0 0 0';
582
583                 if(self.movedir_x >= 0)
584                 {
585                         vector v0;
586                         v0 = self.movedir;
587                         self.movedir = shotorg_adjust(v0, FALSE, FALSE);
588                         self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
589                 }
590                 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
591                 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
592
593                 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
594
595                 // check if an instant weapon switch occurred
596                 if (qcweaponanimation)
597                 {
598                         if (self.state == WS_READY)
599                         {
600                                 self.angles = '0 0 0';
601                                 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
602                                 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
603                         }
604                 }
605                 else
606                         setorigin(self, self.view_ofs);
607                 // reset animstate now
608                 self.wframe = WFRAME_IDLE;
609                 self.weapon_morph0time = 0;
610                 self.weapon_morph1time = 0;
611                 self.weapon_morph2time = 0;
612                 self.weapon_morph3time = 0;
613                 self.weapon_morph4time = 0;
614                 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
615         }
616
617         tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
618         self.effects = self.owner.effects & EFMASK_CHEAP;
619         self.effects &~= EF_LOWPRECISION;
620         self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
621         self.effects &~= EF_TELEPORT_BIT;
622         self.effects &~= EF_RESTARTANIM_BIT;
623         self.effects |= tb;
624
625         if(self.owner.alpha == default_player_alpha)
626                 self.alpha = default_weapon_alpha;
627         else if(self.owner.alpha != 0)
628                 self.alpha = self.owner.alpha;
629         else
630                 self.alpha = 1;
631
632         self.colormap = self.owner.colormap;
633         if (self.weaponentity)
634         {
635                 self.weaponentity.effects = self.effects;
636                 self.weaponentity.alpha = self.alpha;
637                 self.weaponentity.colormap = self.colormap;
638         }
639
640         self.angles = '0 0 0';
641         local float f;
642         f = 0;
643         if (self.state == WS_RAISE && !intermission_running)
644         {
645                 f = (self.owner.weapon_nextthink - time) * g_weaponratefactor / cvar("g_balance_weaponswitchdelay");
646                 self.angles_x = -90 * f * f;
647                 if (qcweaponanimation)
648                 {
649                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
650                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
651                 }
652         }
653         else if (self.state == WS_DROP && !intermission_running)
654         {
655                 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponratefactor / cvar("g_balance_weaponswitchdelay");
656                 self.angles_x = -90 * f * f;
657                 if (qcweaponanimation)
658                 {
659                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
660                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
661                 }
662         }
663         else if (self.state == WS_CLEAR)
664         {
665                 f = 1;
666                 self.angles_x = -90 * f * f;
667                 if (qcweaponanimation)
668                 {
669                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
670                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
671                 }
672         }
673         else if (qcweaponanimation && time < self.owner.weapon_morph1time)
674         {
675                 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
676                 f = 1 - pow(1 - f, 3);
677                 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
678                 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
679         }
680         else if (qcweaponanimation && time < self.owner.weapon_morph2time)
681         {
682                 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
683                 f = 1 - pow(1 - f, 3);
684                 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
685                 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
686         }
687         else if (qcweaponanimation && time < self.owner.weapon_morph3time)
688         {
689                 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
690                 f = 1 - pow(1 - f, 3);
691                 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
692                 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
693         }
694         else if (qcweaponanimation && time < self.owner.weapon_morph4time)
695         {
696                 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
697                 f = 1 - pow(1 - f, 3);
698                 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
699                 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
700         }
701         else if (qcweaponanimation)
702         {
703                 // begin a new idle morph
704                 self.owner.weapon_morph0time   = time;
705                 self.owner.weapon_morph0angles = self.angles;
706                 self.owner.weapon_morph0origin = self.origin;
707
708                 float r;
709                 float t;
710
711                 r = random();
712                 if (r < 0.1)
713                 {
714                         // turn gun to the left to look at it
715                         t = 2;
716                         self.owner.weapon_morph1time   = time + t * 0.2;
717                         self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
718                         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');
719                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
720
721                         self.owner.weapon_morph2time   = time + t * 0.6;
722                         self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
723                         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');
724                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
725
726                         self.owner.weapon_morph3time   = time + t;
727                         self.owner.weapon_morph3angles = '0 0 0';
728                         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');
729                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
730                 }
731                 else if (r < 0.2)
732                 {
733                         // raise the gun a bit
734                         t = 2;
735                         self.owner.weapon_morph1time   = time + t * 0.2;
736                         self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
737                         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');
738                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
739
740                         self.owner.weapon_morph2time   = time + t * 0.5;
741                         self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
742                         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');
743                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
744
745                         self.owner.weapon_morph3time   = time + t;
746                         self.owner.weapon_morph3angles = '0 0 0';
747                         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');
748                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
749                 }
750                 else if (r < 0.3)
751                 {
752                         // tweak it a bit
753                         t = 5;
754                         self.owner.weapon_morph1time   = time + t * 0.3;
755                         self.owner.weapon_morph1angles = randomvec() * 6;
756                         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');
757                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
758
759                         self.owner.weapon_morph2time   = time + t * 0.7;
760                         self.owner.weapon_morph2angles = randomvec() * 6;
761                         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');
762                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
763
764                         self.owner.weapon_morph3time   = time + t;
765                         self.owner.weapon_morph3angles = '0 0 0';
766                         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');
767                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
768                 }
769                 else
770                 {
771                         // hold it mostly steady
772                         t = random() * 6 + 4;
773                         self.owner.weapon_morph1time   = time + t * 0.2;
774                         self.owner.weapon_morph1angles = randomvec() * 1;
775                         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');
776                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
777
778                         self.owner.weapon_morph2time   = time + t * 0.5;
779                         self.owner.weapon_morph2angles = randomvec() * 1;
780                         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');
781                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
782
783                         self.owner.weapon_morph3time   = time + t * 0.7;
784                         self.owner.weapon_morph3angles = randomvec() * 1;
785                         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');
786                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
787                 }
788
789                 self.owner.weapon_morph4time   = time + t;
790                 self.owner.weapon_morph4angles = '0 0 0';
791                 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');
792                 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
793
794         }
795
796         // create or update the lasertarget entity
797         LaserTarget_Think();
798 };
799
800 void CL_ExteriorWeaponentity_Think()
801 {
802         float tag_found;
803         vector ang;
804         self.nextthink = time;
805         if (self.owner.exteriorweaponentity != self)
806         {
807                 remove(self);
808                 return;
809         }
810         if (self.owner.deadflag != DEAD_NO)
811         {
812                 self.model = "";
813                 return;
814         }
815         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
816         {
817                 self.cnt = self.owner.weapon;
818                 self.dmg = self.owner.modelindex;
819                 self.deadflag = self.owner.deadflag;
820                 if (self.owner.weaponname != "")
821                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
822                 else
823                         self.model = "";
824
825                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
826                 {
827                         self.tag_index = tag_found;
828                         self.tag_entity = self.owner;
829                 }
830                 else
831                         setattachment(self, self.owner, "bip01 r hand");
832
833                 // if that didn't find a tag, hide the exterior weapon model
834                 if (!self.tag_index)
835                         self.model = "";
836         }
837         self.effects = self.owner.effects;
838         if(sv_pitch_min == sv_pitch_max)
839                 self.effects |= EF_LOWPRECISION;
840         else
841                 self.effects &~= EF_LOWPRECISION;
842         self.effects = self.effects & EFMASK_CHEAP; // eat performance
843         if(self.owner.alpha == default_player_alpha)
844                 self.alpha = default_weapon_alpha;
845         else if(self.owner.alpha != 0)
846                 self.alpha = self.owner.alpha;
847         else
848                 self.alpha = 1;
849
850         ang_x = bound(sv_pitch_min, self.owner.v_angle_x, sv_pitch_max);
851         ang_y = 0;
852         ang_z = 0;
853
854         if(sv_pitch_fixyaw) // workaround for stupid player models that don't aim forward
855         {
856                 ang_y = self.owner.v_angle_y;
857                 makevectors(ang);
858                 var vector v = v_forward;
859                 var float t = self.tag_entity.frame1time;
860                 var float f = self.tag_entity.frame;
861                 self.tag_entity.frame1time = time;
862                 self.tag_entity.frame = self.tag_entity.anim_idle_x;
863                 gettaginfo(self.tag_entity, self.tag_index);
864                 self.tag_entity.frame1time = t;
865                 self.tag_entity.frame = f;
866                 // untransform v according to this coordinate space
867                 vector w;
868                 w_x = v_forward * v;
869                 w_y = -v_right * v;
870                 w_z = v_up * v;
871                 self.angles = vectoangles(w);
872         }
873         else
874         {
875                 ang_x = -/* don't ask */ang_x;
876                 self.angles = ang;
877         }
878
879         self.colormap = self.owner.colormap;
880 };
881
882 // spawning weaponentity for client
883 void CL_SpawnWeaponentity()
884 {
885         self.weaponentity = spawn();
886         self.weaponentity.classname = "weaponentity";
887         self.weaponentity.solid = SOLID_NOT;
888         self.weaponentity.owner = self;
889         setmodel(self.weaponentity, ""); // precision set when changed
890         setorigin(self.weaponentity, '0 0 0');
891         self.weaponentity.angles = '0 0 0';
892         self.weaponentity.viewmodelforclient = self;
893         self.weaponentity.flags = 0;
894         self.weaponentity.think = CL_Weaponentity_Think;
895         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
896         self.weaponentity.nextthink = time;
897
898         self.exteriorweaponentity = spawn();
899         self.exteriorweaponentity.classname = "exteriorweaponentity";
900         self.exteriorweaponentity.solid = SOLID_NOT;
901         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
902         self.exteriorweaponentity.owner = self;
903         setorigin(self.exteriorweaponentity, '0 0 0');
904         self.exteriorweaponentity.angles = '0 0 0';
905         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
906         self.exteriorweaponentity.nextthink = time;
907 };
908
909 .float hasweapon_complain_spam;
910
911 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
912 {
913         local float weaponbit, f;
914         local entity oldself;
915
916         if(time < self.hasweapon_complain_spam)
917                 complain = 0;
918         if(complain)
919                 self.hasweapon_complain_spam = time + 0.2;
920
921         if (wpn < WEP_FIRST || wpn > WEP_LAST)
922         {
923                 if (complain)
924                         sprint(self, "Invalid weapon\n");
925                 return FALSE;
926         }
927         weaponbit = W_WeaponBit(wpn);
928         if (cl.weapons & weaponbit)
929         {
930                 if (andammo)
931                 {
932                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
933                         {
934                                 f = 1;
935                         }
936                         else
937                         {
938                                 oldself = self;
939                                 self = cl;
940                                 f = weapon_action(wpn, WR_CHECKAMMO1);
941                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
942                                 self = oldself;
943                         }
944                         if (!f)
945                         {
946                                 if (complain)
947                                 if(clienttype(cl) == CLIENTTYPE_REAL)
948                                 {
949                                         play2(cl, "weapons/unavailable.wav");
950                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
951                                 }
952                                 return FALSE;
953                         }
954                 }
955                 return TRUE;
956         }
957         if (complain)
958         {
959                 // DRESK - 3/16/07
960                 // Report Proper Weapon Status / Modified Weapon Ownership Message
961                 if(weaponsInMap & weaponbit)
962                 {
963                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
964
965                         if(cvar("g_showweaponspawns"))
966                         {
967                                 entity e;
968                                 string s;
969
970                                 e = get_weaponinfo(wpn);
971                                 s = e.model2;
972
973                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
974                                 {
975                                         if(e.classname == "droppedweapon")
976                                                 continue;
977                                         if not(e.flags & FL_ITEM)
978                                                 continue;
979                                         WaypointSprite_Spawn(
980                                                 s,
981                                                 1, 0,
982                                                 world, e.origin,
983                                                 self, 0,
984                                                 world, enemy,
985                                                 0
986                                         );
987                                 }
988                         }
989                 }
990                 else
991                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
992
993                 play2(cl, "weapons/unavailable.wav");
994         }
995         return FALSE;
996 };
997
998 // Weapon subs
999 void w_clear()
1000 {
1001         if (self.weapon != -1)
1002                 self.weapon = 0;
1003         if (self.weaponentity)
1004         {
1005                 self.weaponentity.state = WS_CLEAR;
1006                 self.weaponentity.effects = 0;
1007         }
1008 };
1009
1010 void w_ready()
1011 {
1012         if (self.weaponentity)
1013                 self.weaponentity.state = WS_READY;
1014         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
1015 };
1016
1017 // Setup weapon for client (after this raise frame will be launched)
1018 void weapon_setup(float windex)
1019 {
1020         entity e;
1021         qcweaponanimation = cvar("sv_qcweaponanimation");
1022         e = get_weaponinfo(windex);
1023         self.items &~= IT_AMMO;
1024         self.items = self.items | e.items;
1025
1026         // the two weapon entities will notice this has changed and update their models
1027         self.weapon = windex;
1028         self.weaponname = e.mdl;
1029         self.bulletcounter = 0;
1030 };
1031
1032 // perform weapon to attack (weaponstate and attack_finished check is here)
1033 .float race_penalty;
1034 float weapon_prepareattack(float secondary, float attacktime)
1035 {
1036         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
1037         //if all players readied up and the countdown is running
1038         if(time < game_starttime || time < self.race_penalty) {
1039                 return FALSE;
1040         }
1041
1042         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1043         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
1044         {
1045                 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
1046                 float w, ww;
1047                 w = W_WeaponBit(self.weapon);
1048                 self.weapons &~= w;
1049                 ww = w_getbestweapon(self);
1050                 self.weapons |= w;
1051                 if(ww)
1052                         W_SwitchWeapon_Force(self, ww);
1053                 return FALSE;
1054         }
1055
1056         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
1057                 return FALSE;
1058
1059         // do not even think about shooting if switching
1060         if(self.switchweapon != self.weapon)
1061                 return FALSE;
1062
1063         if(attacktime >= 0)
1064         {
1065                 // don't fire if previous attack is not finished
1066                 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
1067                         return FALSE;
1068                 // don't fire while changing weapon
1069                 if (self.weaponentity.state != WS_READY)
1070                         return FALSE;
1071         }
1072         self.weaponentity.state = WS_INUSE;
1073
1074         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1075
1076         // if the weapon hasn't been firing continuously, reset the timer
1077         if(attacktime >= 0)
1078         {
1079                 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
1080                 {
1081                         ATTACK_FINISHED(self) = time;
1082                         //dprint("resetting attack finished to ", ftos(time), "\n");
1083                 }
1084                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
1085         }
1086         self.bulletcounter += 1;
1087         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1088         return TRUE;
1089 };
1090
1091 void weapon_thinkf(float fr, float t, void() func)
1092 {
1093         vector a;
1094         vector of, or, ou;
1095         float restartanim;
1096
1097         if(fr == WFRAME_DONTCHANGE)
1098         {
1099                 fr = self.weaponentity.wframe;
1100                 restartanim = FALSE;
1101         }
1102         else if (fr == WFRAME_IDLE)
1103                 restartanim = FALSE;
1104         else
1105                 restartanim = TRUE;
1106
1107         of = v_forward;
1108         or = v_right;
1109         ou = v_up;
1110
1111         if (self.weaponentity)
1112         {
1113                 self.weaponentity.wframe = fr;
1114                 if (qcweaponanimation)
1115                 {
1116                         if (fr != WFRAME_IDLE)
1117                         {
1118                                 self.weapon_morph0time = time;
1119                                 self.weapon_morph0angles = self.weaponentity.angles;
1120                                 self.weapon_morph0origin = self.weaponentity.origin;
1121
1122                                 self.weapon_morph1angles = '0 0 0';
1123                                 self.weapon_morph1time = time + t;
1124                                 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1125                                 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1126
1127                                 self.weapon_morph2angles = '0 0 0';
1128                                 self.weapon_morph2time = time + t;
1129                                 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1130                                 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1131
1132                                 self.weapon_morph3angles = '0 0 0';
1133                                 self.weapon_morph3time = time + t;
1134                                 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1135                                 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1136
1137                                 self.weapon_morph4angles = '0 0 0';
1138                                 self.weapon_morph4time = time + t;
1139                                 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1140                                 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1141
1142                                 if (fr == WFRAME_FIRE1)
1143                                 {
1144                                         self.weapon_morph1angles = '5 0 0';
1145                                         self.weapon_morph1time = time + t * 0.1;
1146                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1147                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1148                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1149                                 }
1150                                 else if (fr == WFRAME_FIRE2)
1151                                 {
1152                                         self.weapon_morph1angles = '10 0 0';
1153                                         self.weapon_morph1time = time + t * 0.1;
1154                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1155                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1156                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1157                                 }
1158                                 else if (fr == WFRAME_RELOAD)
1159                                 {
1160                                         self.weapon_morph1time = time + t * 0.05;
1161                                         self.weapon_morph1angles = '-10 40 0';
1162                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1163                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1164
1165                                         self.weapon_morph2time = time + t * 0.15;
1166                                         self.weapon_morph2angles = '-10 40 5';
1167                                         makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1168                                         self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1169
1170                                         self.weapon_morph3time = time + t * 0.25;
1171                                         self.weapon_morph3angles = '-10 40 0';
1172                                         makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1173                                         self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1174                                 }
1175                         }
1176                 }
1177                 else
1178                 {
1179                         if (fr == WFRAME_IDLE)
1180                                 a = self.weaponentity.anim_idle;
1181                         else if (fr == WFRAME_FIRE1)
1182                                 a = self.weaponentity.anim_fire1;
1183                         else if (fr == WFRAME_FIRE2)
1184                                 a = self.weaponentity.anim_fire2;
1185                         else if (fr == WFRAME_RELOAD)
1186                                 a = self.weaponentity.anim_reload;
1187                         a_z *= g_weaponratefactor;
1188                         setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1189                 }
1190         }
1191
1192         v_forward = of;
1193         v_right = or;
1194         v_up = ou;
1195
1196         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1197         {
1198                 backtrace("Tried to override initial weapon think function - should this really happen?");
1199         }
1200
1201         t *= W_WeaponRateFactor();
1202
1203         // VorteX: haste can be added here
1204         if (self.weapon_think == w_ready)
1205         {
1206                 self.weapon_nextthink = time;
1207                 //dprint("started firing at ", ftos(time), "\n");
1208         }
1209         if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
1210         {
1211                 self.weapon_nextthink = time;
1212                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1213         }
1214         self.weapon_nextthink = self.weapon_nextthink + t;
1215         self.weapon_think = func;
1216         //dprint("next ", ftos(self.weapon_nextthink), "\n");
1217
1218         if (restartanim)
1219         if (t)
1220         if (!self.crouch) // shoot anim stands up, this looks bad
1221         {
1222                 local vector anim;
1223                 anim = self.anim_shoot;
1224                 anim_z = anim_y / (t + sys_frametime);
1225                 setanim(self, anim, FALSE, TRUE, TRUE);
1226         }
1227 };
1228
1229 void weapon_boblayer1(float spd, vector org)
1230 {
1231         // VorteX: haste can be added here
1232 };
1233
1234 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1235 {
1236         vector mdirection;
1237         float mspeed;
1238         float outspeed;
1239         float nstyle;
1240         vector outvelocity;
1241
1242         mvelocity = mvelocity * g_weaponspeedfactor;
1243
1244         mdirection = normalize(mvelocity);
1245         mspeed = vlen(mvelocity);
1246
1247         nstyle = cvar("g_projectiles_newton_style");
1248         if(nstyle == 0)
1249         {
1250                 // absolute velocity
1251                 outvelocity = mvelocity;
1252         }
1253         else if(nstyle == 1)
1254         {
1255                 // true Newtonian projectiles
1256                 outvelocity = pvelocity + mvelocity;
1257         }
1258         else if(nstyle == 2)
1259         {
1260                 // true Newtonian projectiles with automatic aim adjustment
1261                 //
1262                 // solve: |outspeed * mdirection - pvelocity| = mspeed
1263                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1264                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1265                 // PLUS SIGN!
1266                 // not defined?
1267                 // then...
1268                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1269                 // velocity without mdirection component > mspeed
1270                 // fire at smallest possible mspeed that works?
1271                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1272
1273                 vector solution;
1274                 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1275                 if(solution_z)
1276                         outspeed = solution_y; // the larger one
1277                 else
1278                 {
1279                         //outspeed = 0; // slowest possible shot
1280                         outspeed = solution_x; // the real part (that is, the average!)
1281                         //dprint("impossible shot, adjusting\n");
1282                 }
1283
1284                 outspeed = bound(mspeed * cvar("g_projectiles_newton_style_2_minfactor"), outspeed, mspeed * cvar("g_projectiles_newton_style_2_maxfactor"));
1285                 outvelocity = mdirection * outspeed;
1286         }
1287         else if(nstyle == 3)
1288         {
1289                 // pseudo-Newtonian:
1290                 outspeed = mspeed + mdirection * pvelocity;
1291                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1292                 outvelocity = mdirection * outspeed;
1293         }
1294         else if(nstyle == 4)
1295         {
1296                 // tZorkian:
1297                 outspeed = mspeed + vlen(pvelocity);
1298                 outvelocity = mdirection * outspeed;
1299         }
1300         else
1301                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1302
1303         return outvelocity;
1304 }
1305
1306 void W_AttachToShotorg(entity flash, vector offset)
1307 {
1308         entity xflash;
1309         flash.owner = self;
1310         flash.angles_z = random() * 360;
1311         if(qcweaponanimation)
1312         {
1313                 setorigin(flash, w_shotorg + w_shotdir * 50);
1314                 flash.angles = vectoangles(w_shotdir);
1315                 flash.angles_z = random() * 360;
1316         }
1317         else
1318         {
1319                 setattachment(flash, self.weaponentity, "shot");
1320                 setorigin(flash, offset);
1321
1322                 xflash = spawn();
1323                 copyentity(flash, xflash);
1324
1325                 flash.viewmodelforclient = self;
1326
1327                 if(self.weaponentity.oldorigin_x > 0)
1328                 {
1329                         setattachment(xflash, self.exteriorweaponentity, "");
1330                         setorigin(xflash, self.weaponentity.oldorigin + offset);
1331                 }
1332                 else
1333                 {
1334                         setattachment(xflash, self.exteriorweaponentity, "shot");
1335                 }
1336         }
1337 }
1338
1339 vector cliptoplane(vector v, vector p)
1340 {
1341         return v - (v * p) * p;
1342 }
1343
1344 vector solve_cubic_pq(float p, float q)
1345 {
1346         float D, u, v, a;
1347         D = q*q/4.0 + p*p*p/27.0;
1348         if(D < 0)
1349         {
1350                 // irreducibilis
1351                 a = 1.0/3.0 * acos(-q/2.0 * sqrt(-27.0/(p*p*p)));
1352                 u = sqrt(-4.0/3.0 * p);
1353                 // a in range 0..pi/3
1354                 // cos(a)
1355                 // cos(a + 2pi/3)
1356                 // cos(a + 4pi/3)
1357                 return
1358                         u *
1359                         (
1360                                 '1 0 0' * cos(a + 2.0/3.0*M_PI)
1361                                 +
1362                                 '0 1 0' * cos(a + 4.0/3.0*M_PI)
1363                                 +
1364                                 '0 0 1' * cos(a)
1365                         );
1366         }
1367         else if(D == 0)
1368         {
1369                 // simple
1370                 if(p == 0)
1371                         return '0 0 0';
1372                 u = 3*q/p;
1373                 v = -u/2;
1374                 if(u >= v)
1375                         return '1 1 0' * v + '0 0 1' * u;
1376                 else
1377                         return '0 1 1' * v + '1 0 0' * u;
1378         }
1379         else
1380         {
1381                 // cardano
1382                 u = cbrt(-q/2.0 + sqrt(D));
1383                 v = cbrt(-q/2.0 - sqrt(D));
1384                 return '1 1 1' * (u + v);
1385         }
1386 }
1387 vector solve_cubic_abcd(float a, float b, float c, float d)
1388 {
1389         // y = 3*a*x + b
1390         // x = (y - b) / 3a
1391         float p, q;
1392         vector v;
1393         p = (9*a*c - 3*b*b);
1394         q = (27*a*a*d - 9*a*b*c + 2*b*b*b);
1395         v = solve_cubic_pq(p, q);
1396         v = (v -  b * '1 1 1') * (1.0 / (3.0 * a));
1397         if(a < 0)
1398                 v += '1 0 -1' * (v_z - v_x); // swap x, z
1399         return v;
1400 }
1401
1402 vector findperpendicular(vector v)
1403 {
1404         vector p;
1405         p_x = v_z;
1406         p_y = -v_x;
1407         p_z = v_y;
1408         return normalize(cliptoplane(p, v));
1409 }
1410
1411 vector W_CalculateProjectileSpread(vector forward, float spread)
1412 {
1413         float sigma;
1414         vector v1, v2;
1415         float dx, dy, r;
1416         float sstyle;
1417         spread *= g_weaponspreadfactor;
1418         if(spread <= 0)
1419                 return forward;
1420         sstyle = cvar("g_projectiles_spread_style");
1421         
1422         if(sstyle == 0)
1423         {
1424                 // this is the baseline for the spread value!
1425                 // standard deviation: sqrt(2/5)
1426                 // density function: sqrt(1-r^2)
1427                 return forward + randomvec() * spread;
1428         }
1429         else if(sstyle == 1)
1430         {
1431                 // same thing, basically
1432                 return normalize(forward + cliptoplane(randomvec() * spread, forward));
1433         }
1434         else if(sstyle == 2)
1435         {
1436                 // circle spread... has at sigma=1 a standard deviation of sqrt(1/2)
1437                 sigma = spread * 0.89442719099991587855; // match baseline stddev
1438                 v1 = findperpendicular(forward);
1439                 v2 = cross(forward, v1);
1440                 // random point on unit circle
1441                 dx = random() * 2 * M_PI;
1442                 dy = sin(dx);
1443                 dx = cos(dx);
1444                 // radius in our dist function
1445                 r = random();
1446                 r = sqrt(r);
1447                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1448         }
1449         else if(sstyle == 3) // gauss 3d
1450         {
1451                 sigma = spread * 0.44721359549996; // match baseline stddev
1452                 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1453                 v1 = forward;
1454                 v1_x += gsl_ran_gaussian(sigma);
1455                 v1_y += gsl_ran_gaussian(sigma);
1456                 v1_z += gsl_ran_gaussian(sigma);
1457                 return v1;
1458         }
1459         else if(sstyle == 4) // gauss 2d
1460         {
1461                 sigma = spread * 0.44721359549996; // match baseline stddev
1462                 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1463                 v1_x = gsl_ran_gaussian(sigma);
1464                 v1_y = gsl_ran_gaussian(sigma);
1465                 v1_z = gsl_ran_gaussian(sigma);
1466                 return normalize(forward + cliptoplane(v1, forward));
1467         }
1468         else if(sstyle == 5) // 1-r
1469         {
1470                 sigma = spread * 1.154700538379252; // match baseline stddev
1471                 v1 = findperpendicular(forward);
1472                 v2 = cross(forward, v1);
1473                 // random point on unit circle
1474                 dx = random() * 2 * M_PI;
1475                 dy = sin(dx);
1476                 dx = cos(dx);
1477                 // radius in our dist function
1478                 r = random();
1479                 r = solve_cubic_abcd(-2, 3, 0, -r) * '0 1 0';
1480                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1481         }
1482         else if(sstyle == 6) // 1-r^2
1483         {
1484                 sigma = spread * 1.095445115010332; // match baseline stddev
1485                 v1 = findperpendicular(forward);
1486                 v2 = cross(forward, v1);
1487                 // random point on unit circle
1488                 dx = random() * 2 * M_PI;
1489                 dy = sin(dx);
1490                 dx = cos(dx);
1491                 // radius in our dist function
1492                 r = random();
1493                 r = sqrt(1 - r);
1494                 r = sqrt(1 - r);
1495                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1496         }
1497         else if(sstyle == 7) // (1-r) (2-r)
1498         {
1499                 sigma = spread * 1.224744871391589; // match baseline stddev
1500                 v1 = findperpendicular(forward);
1501                 v2 = cross(forward, v1);
1502                 // random point on unit circle
1503                 dx = random() * 2 * M_PI;
1504                 dy = sin(dx);
1505                 dx = cos(dx);
1506                 // radius in our dist function
1507                 r = random();
1508                 r = 1 - sqrt(r);
1509                 r = 1 - sqrt(r);
1510                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1511         }
1512         else
1513                 error("g_projectiles_spread_style must be 0 (sphere), 1 (flattened sphere), 2 (circle), 3 (gauss 3D), 4 (gauss plane), 5 (linear falloff), 6 (quadratic falloff), 7 (stronger falloff)!");
1514         return '0 0 0';
1515         /*
1516          * how to derive falloff functions:
1517          * rho(r) := (2-r) * (1-r);
1518          * a : 0;
1519          * b : 1;
1520          * rhor(r) := r * rho(r);
1521          * cr(t) := integrate(rhor(r), r, a, t);
1522          * scr(t) := integrate(rhor(r) * r^2, r, a, t);
1523          * variance : scr(b) / cr(b);
1524          * solve(cr(r) = rand * cr(b), r), programmmode:false;
1525          * sqrt(0.4 / variance), numer;
1526          */
1527 }
1528
1529 #if 0
1530 float mspercallsum;
1531 float mspercallsstyle;
1532 float mspercallcount;
1533 #endif
1534 void W_SetupProjectileVelocityEx(entity missile, vector dir, vector upDir, float pSpeed, float pUpSpeed, float pZSpeed, float spread)
1535 {
1536         if(missile.owner == world)
1537                 error("Unowned missile");
1538
1539         dir = dir + upDir * (pUpSpeed / pSpeed);
1540         dir_z += pZSpeed / pSpeed;
1541         pSpeed *= vlen(dir);
1542         dir = normalize(dir);
1543
1544 #if 0
1545         if(cvar("g_projectiles_spread_style") != mspercallsstyle)
1546         {
1547                 mspercallsum = mspercallcount = 0;
1548                 mspercallsstyle = cvar("g_projectiles_spread_style");
1549         }
1550         mspercallsum -= gettime(GETTIME_HIRES);
1551 #endif
1552         dir = W_CalculateProjectileSpread(dir, spread);
1553 #if 0
1554         mspercallsum += gettime(GETTIME_HIRES);
1555         mspercallcount += 1;
1556         print("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
1557 #endif
1558
1559         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, pSpeed * dir);
1560 }
1561
1562 void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
1563 {
1564         W_SetupProjectileVelocityEx(missile, w_shotdir, v_up, pSpeed, 0, 0, spread);
1565 }
1566
1567 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"))
1568 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"))