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