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