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