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