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