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