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