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