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