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