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