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