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