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