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