]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weaponsystem.qc
cl_noantilag
[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         self.nextthink = time;
741         if (self.owner.exteriorweaponentity != self)
742         {
743                 remove(self);
744                 return;
745         }
746         if (self.owner.deadflag != DEAD_NO)
747         {
748                 self.model = "";
749                 return;
750         }
751         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
752         {
753                 self.cnt = self.owner.weapon;
754                 self.dmg = self.owner.modelindex;
755                 self.deadflag = self.owner.deadflag;
756                 if (self.owner.weaponname != "")
757                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
758                 else
759                         self.model = "";
760
761                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
762                 {
763                         self.tag_index = tag_found;
764                         self.tag_entity = self.owner;
765                 }
766                 else
767                         setattachment(self, self.owner, "bip01 r hand");
768
769                 // if that didn't find a tag, hide the exterior weapon model
770                 if (!self.tag_index)
771                         self.model = "";
772         }
773         self.effects = self.owner.effects | EF_LOWPRECISION;
774         self.effects = self.effects & EFMASK_CHEAP; // eat performance
775         if(self.owner.alpha != 0)
776                 self.alpha = self.owner.alpha;
777         else
778                 self.alpha = 1;
779
780         self.colormap = self.owner.colormap;
781 };
782
783 // spawning weaponentity for client
784 void CL_SpawnWeaponentity()
785 {
786         self.weaponentity = spawn();
787         self.weaponentity.classname = "weaponentity";
788         self.weaponentity.solid = SOLID_NOT;
789         self.weaponentity.owner = self;
790         setmodel(self.weaponentity, ""); // precision set when changed
791         setorigin(self.weaponentity, '0 0 0');
792         self.weaponentity.angles = '0 0 0';
793         self.weaponentity.viewmodelforclient = self;
794         self.weaponentity.flags = 0;
795         self.weaponentity.think = CL_Weaponentity_Think;
796         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
797         self.weaponentity.nextthink = time;
798
799         self.exteriorweaponentity = spawn();
800         self.exteriorweaponentity.classname = "exteriorweaponentity";
801         self.exteriorweaponentity.solid = SOLID_NOT;
802         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
803         self.exteriorweaponentity.owner = self;
804         setorigin(self.exteriorweaponentity, '0 0 0');
805         self.exteriorweaponentity.angles = '0 0 0';
806         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
807         self.exteriorweaponentity.nextthink = time;
808 };
809
810 .float hasweapon_complain_spam;
811
812 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
813 {
814         local float weaponbit, f;
815         local entity oldself;
816
817         if(time < self.hasweapon_complain_spam)
818                 complain = 0;
819         if(complain)
820                 self.hasweapon_complain_spam = time + 0.2;
821
822         if (wpn < WEP_FIRST || wpn > WEP_LAST)
823         {
824                 if (complain)
825                         sprint(self, "Invalid weapon\n");
826                 return FALSE;
827         }
828         weaponbit = W_WeaponBit(wpn);
829         if (cl.weapons & weaponbit)
830         {
831                 if (andammo)
832                 {
833                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
834                         {
835                                 f = 1;
836                         }
837                         else
838                         {
839                                 oldself = self;
840                                 self = cl;
841                                 f = weapon_action(wpn, WR_CHECKAMMO1);
842                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
843                                 self = oldself;
844                         }
845                         if (!f)
846                         {
847                                 if (complain)
848                                 if(clienttype(cl) == CLIENTTYPE_REAL)
849                                 {
850                                         play2(cl, "weapons/unavailable.wav");
851                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
852                                 }
853                                 return FALSE;
854                         }
855                 }
856                 return TRUE;
857         }
858         if (complain)
859         {
860                 // DRESK - 3/16/07
861                 // Report Proper Weapon Status / Modified Weapon Ownership Message
862                 if(weaponsInMap & weaponbit)
863                 {
864                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
865
866                         if(cvar("g_showweaponspawns"))
867                         {
868                                 entity e;
869                                 string s;
870
871                                 e = get_weaponinfo(wpn);
872                                 s = e.model2;
873
874                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
875                                 {
876                                         if(e.classname == "droppedweapon")
877                                                 continue;
878                                         if not(e.flags & FL_ITEM)
879                                                 continue;
880                                         WaypointSprite_Spawn(
881                                                 s,
882                                                 1, 0,
883                                                 world, e.origin,
884                                                 self, 0,
885                                                 world, enemy,
886                                                 0
887                                         );
888                                 }
889                         }
890                 }
891                 else
892                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
893
894                 play2(cl, "weapons/unavailable.wav");
895         }
896         return FALSE;
897 };
898
899 // Weapon subs
900 void w_clear()
901 {
902         if (self.weapon != -1)
903                 self.weapon = 0;
904         if (self.weaponentity)
905         {
906                 self.weaponentity.state = WS_CLEAR;
907                 self.weaponentity.effects = 0;
908         }
909 };
910
911 void w_ready()
912 {
913         if (self.weaponentity)
914                 self.weaponentity.state = WS_READY;
915         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
916 };
917
918 // Setup weapon for client (after this raise frame will be launched)
919 void weapon_setup(float windex)
920 {
921         entity e;
922         qcweaponanimation = cvar("sv_qcweaponanimation");
923         e = get_weaponinfo(windex);
924         self.items &~= IT_AMMO;
925         self.items = self.items | e.items;
926
927         // the two weapon entities will notice this has changed and update their models
928         self.weapon = windex;
929         self.weaponname = e.mdl;
930         self.bulletcounter = 0;
931 };
932
933 // perform weapon to attack (weaponstate and attack_finished check is here)
934 .float race_penalty;
935 float weapon_prepareattack(float secondary, float attacktime)
936 {
937         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
938         //if all players readied up and the countdown is running
939         if(time < game_starttime || time < self.race_penalty) {
940                 return FALSE;
941         }
942
943         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
944         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
945         {
946                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
947                 return FALSE;
948         }
949
950         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
951                 return FALSE;
952
953         // do not even think about shooting if switching
954         if(self.switchweapon != self.weapon)
955                 return FALSE;
956
957         if(attacktime >= 0)
958         {
959                 // don't fire if previous attack is not finished
960                 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
961                         return FALSE;
962                 // don't fire while changing weapon
963                 if (self.weaponentity.state != WS_READY)
964                         return FALSE;
965         }
966         self.weaponentity.state = WS_INUSE;
967
968         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
969
970         // if the weapon hasn't been firing continuously, reset the timer
971         if(attacktime >= 0)
972         {
973                 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
974                 {
975                         ATTACK_FINISHED(self) = time;
976                         //dprint("resetting attack finished to ", ftos(time), "\n");
977                 }
978                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
979         }
980         self.bulletcounter += 1;
981         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
982         return TRUE;
983 };
984
985 void weapon_thinkf(float fr, float t, void() func)
986 {
987         vector a;
988         vector of, or, ou;
989         float restartanim;
990
991         if(fr == WFRAME_DONTCHANGE)
992         {
993                 fr = self.weaponentity.wframe;
994                 restartanim = FALSE;
995         }
996         else if (fr == WFRAME_IDLE)
997                 restartanim = FALSE;
998         else
999                 restartanim = TRUE;
1000
1001         of = v_forward;
1002         or = v_right;
1003         ou = v_up;
1004
1005         if (self.weaponentity)
1006         {
1007                 self.weaponentity.wframe = fr;
1008                 if (qcweaponanimation)
1009                 {
1010                         if (fr != WFRAME_IDLE)
1011                         {
1012                                 self.weapon_morph0time = time;
1013                                 self.weapon_morph0angles = self.weaponentity.angles;
1014                                 self.weapon_morph0origin = self.weaponentity.origin;
1015
1016                                 self.weapon_morph1angles = '0 0 0';
1017                                 self.weapon_morph1time = time + t;
1018                                 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1019                                 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1020
1021                                 self.weapon_morph2angles = '0 0 0';
1022                                 self.weapon_morph2time = time + t;
1023                                 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1024                                 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1025
1026                                 self.weapon_morph3angles = '0 0 0';
1027                                 self.weapon_morph3time = time + t;
1028                                 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1029                                 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1030
1031                                 self.weapon_morph4angles = '0 0 0';
1032                                 self.weapon_morph4time = time + t;
1033                                 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1034                                 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1035
1036                                 if (fr == WFRAME_FIRE1)
1037                                 {
1038                                         self.weapon_morph1angles = '5 0 0';
1039                                         self.weapon_morph1time = time + t * 0.1;
1040                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1041                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1042                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1043                                 }
1044                                 else if (fr == WFRAME_FIRE2)
1045                                 {
1046                                         self.weapon_morph1angles = '10 0 0';
1047                                         self.weapon_morph1time = time + t * 0.1;
1048                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1049                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1050                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1051                                 }
1052                                 else if (fr == WFRAME_RELOAD)
1053                                 {
1054                                         self.weapon_morph1time = time + t * 0.05;
1055                                         self.weapon_morph1angles = '-10 40 0';
1056                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1057                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1058
1059                                         self.weapon_morph2time = time + t * 0.15;
1060                                         self.weapon_morph2angles = '-10 40 5';
1061                                         makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1062                                         self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1063
1064                                         self.weapon_morph3time = time + t * 0.25;
1065                                         self.weapon_morph3angles = '-10 40 0';
1066                                         makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1067                                         self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1068                                 }
1069                         }
1070                 }
1071                 else
1072                 {
1073                         if (fr == WFRAME_IDLE)
1074                                 a = self.weaponentity.anim_idle;
1075                         else if (fr == WFRAME_FIRE1)
1076                                 a = self.weaponentity.anim_fire1;
1077                         else if (fr == WFRAME_FIRE2)
1078                                 a = self.weaponentity.anim_fire2;
1079                         else if (fr == WFRAME_RELOAD)
1080                                 a = self.weaponentity.anim_reload;
1081                         a_z *= g_weaponratefactor;
1082                         setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1083                 }
1084         }
1085
1086         v_forward = of;
1087         v_right = or;
1088         v_up = ou;
1089
1090         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1091         {
1092                 backtrace("Tried to override initial weapon think function - should this really happen?");
1093         }
1094
1095         t *= W_WeaponRateFactor();
1096
1097         // VorteX: haste can be added here
1098         if (self.weapon_think == w_ready)
1099         {
1100                 self.weapon_nextthink = time;
1101                 //dprint("started firing at ", ftos(time), "\n");
1102         }
1103         if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
1104         {
1105                 self.weapon_nextthink = time;
1106                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1107         }
1108         self.weapon_nextthink = self.weapon_nextthink + t;
1109         self.weapon_think = func;
1110         //dprint("next ", ftos(self.weapon_nextthink), "\n");
1111
1112         if (restartanim)
1113         if (t)
1114         if (!self.crouch) // shoot anim stands up, this looks bad
1115         {
1116                 local vector anim;
1117                 anim = self.anim_shoot;
1118                 anim_z = anim_y / t;
1119                 setanim(self, anim, FALSE, TRUE, TRUE);
1120         }
1121 };
1122
1123 void weapon_boblayer1(float spd, vector org)
1124 {
1125         // VorteX: haste can be added here
1126 };
1127
1128 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1129 {
1130         vector mdirection;
1131         float mspeed;
1132         float outspeed;
1133         float nstyle;
1134         vector outvelocity;
1135
1136         mvelocity = mvelocity * g_weaponspeedfactor;
1137
1138         mdirection = normalize(mvelocity);
1139         mspeed = vlen(mvelocity);
1140
1141         nstyle = cvar("g_projectiles_newton_style");
1142         if(nstyle == 0)
1143         {
1144                 // absolute velocity
1145                 outvelocity = mvelocity;
1146         }
1147         else if(nstyle == 1)
1148         {
1149                 // true Newtonian projectiles
1150                 outvelocity = pvelocity + mvelocity;
1151         }
1152         else if(nstyle == 2)
1153         {
1154                 // true Newtonian projectiles with automatic aim adjustment
1155                 //
1156                 // solve: |outspeed * mdirection - pvelocity| = mspeed
1157                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1158                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1159                 // PLUS SIGN!
1160                 // not defined?
1161                 // then...
1162                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1163                 // velocity without mdirection component > mspeed
1164                 // fire at smallest possible mspeed that works?
1165                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1166
1167                 vector solution;
1168                 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1169                 if(solution_z)
1170                         outspeed = solution_y; // the larger one
1171                 else
1172                 {
1173                         //outspeed = 0; // slowest possible shot
1174                         outspeed = solution_x; // the real part (that is, the average!)
1175                         //dprint("impossible shot, adjusting\n");
1176                 }
1177
1178                 outspeed = bound(mspeed * cvar("g_projectiles_newton_style_2_minfactor"), outspeed, mspeed * cvar("g_projectiles_newton_style_2_maxfactor"));
1179                 outvelocity = mdirection * outspeed;
1180         }
1181         else if(nstyle == 3)
1182         {
1183                 // pseudo-Newtonian:
1184                 outspeed = mspeed + mdirection * pvelocity;
1185                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1186                 outvelocity = mdirection * outspeed;
1187         }
1188         else if(nstyle == 4)
1189         {
1190                 // tZorkian:
1191                 outspeed = mspeed + vlen(pvelocity);
1192                 outvelocity = mdirection * outspeed;
1193         }
1194         else
1195                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1196
1197         return outvelocity;
1198 }
1199
1200 void W_SetupProjectileVelocity(entity missile)
1201 {
1202         if(missile.owner == world)
1203                 error("Unowned missile");
1204
1205         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1206 }
1207
1208 void W_AttachToShotorg(entity flash, vector offset)
1209 {
1210         entity xflash;
1211         flash.owner = self;
1212         flash.angles_z = random() * 360;
1213         if(qcweaponanimation)
1214         {
1215                 setorigin(flash, w_shotorg + w_shotdir * 50);
1216                 flash.angles = vectoangles(w_shotdir);
1217                 flash.angles_z = random() * 360;
1218         }
1219         else
1220         {
1221                 setattachment(flash, self.weaponentity, "shot");
1222                 setorigin(flash, offset);
1223
1224                 xflash = spawn();
1225                 copyentity(flash, xflash);
1226
1227                 flash.viewmodelforclient = self;
1228
1229                 if(self.weaponentity.oldorigin_x > 0)
1230                 {
1231                         setattachment(xflash, self.exteriorweaponentity, "");
1232                         setorigin(xflash, self.weaponentity.oldorigin + offset);
1233                 }
1234                 else
1235                 {
1236                         setattachment(xflash, self.exteriorweaponentity, "shot");
1237                 }
1238         }
1239 }