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