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