]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_weaponsystem.qc
Fix a bug with dropping the hagar while loading it. You still lose the ammo though...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_weaponsystem.qc
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 .float weapon_frametime;
11
12 float W_WeaponRateFactor()
13 {
14         float t;
15         t = 1.0 / g_weaponratefactor;
16
17         if(g_runematch)
18         {
19                 if(self.runes & RUNE_SPEED)
20                 {
21                         if(self.runes & CURSE_SLOW)
22                                 t = t * autocvar_g_balance_rune_speed_combo_atkrate;
23                         else
24                                 t = t * autocvar_g_balance_rune_speed_atkrate;
25                 }
26                 else if(self.runes & CURSE_SLOW)
27                 {
28                         t = t * autocvar_g_balance_curse_slow_atkrate;
29                 }
30         }
31
32         return t;
33 }
34
35 void W_SwitchWeapon_Force(entity e, float w)
36 {
37         // don't allow switching to another weapon if we're loading rockets into the hagar
38         if(e.weapon == WEP_HAGAR && e.hagar_load)
39         if(e.weapons & WEPBIT_HAGAR) // only if we still have the hagar, prevents a bug with dropping
40                 return;
41
42         e.cnt = e.switchweapon;
43         e.switchweapon = w;
44         e.selectweapon = w;
45 }
46
47 .float antilag_debug;
48
49 // VorteX: static frame globals
50 float WFRAME_DONTCHANGE = -1;
51 float WFRAME_FIRE1 = 0;
52 float WFRAME_FIRE2 = 1;
53 float WFRAME_IDLE = 2;
54 float WFRAME_RELOAD = 3;
55 .float wframe;
56
57 void(float fr, float t, void() func) weapon_thinkf;
58
59 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
60 {
61         vector ret;
62         ret_x = screenright * v;
63         ret_y = screenup * v;
64         ret_z = screenforward * v;
65         return ret;
66 }
67
68 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
69 {
70         float i, j, k;
71         vector mi, ma, thisv, myv, ret;
72
73         myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
74
75         // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
76
77         for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
78         {
79                 thisv = targ.origin;
80                 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
81                 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
82                 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
83                 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
84                 if(i || j || k)
85                 {
86                         if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
87                         if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
88                         //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
89                 }
90                 else
91                 {
92                         // first run
93                         mi = ma = thisv;
94                 }
95         }
96
97         thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
98         ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
99         ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
100         ret_z = thisv_z - myv_z;
101         return ret;
102 }
103
104 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
105 {
106         vector hitplot;
107         vector org;
108         float lag;
109
110         if(player.hitplotfh >= 0)
111         {
112                 lag = ANTILAG_LATENCY(player);
113                 if(lag < 0.001)
114                         lag = 0;
115                 if(clienttype(player) != CLIENTTYPE_REAL)
116                         lag = 0; // only antilag for clients
117
118                 org = player.origin + player.view_ofs;
119                 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
120                 if(trace_ent.flags & FL_CLIENT)
121                 {
122                         antilag_takeback(trace_ent, time - lag);
123                         hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
124                         antilag_restore(trace_ent);
125                         fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
126                         //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
127                 }
128         }
129 }
130
131 vector w_shotorg;
132 vector w_shotdir;
133 vector w_shotend;
134
135 // this function calculates w_shotorg and w_shotdir based on the weapon model
136 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
137 // make sure you call makevectors first (FIXME?)
138 .float prevstrengthsound;
139 .float prevstrengthsoundattempt;
140 void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float chan, float maxdamage, float range)
141 {
142         float nudge = 1; // added to traceline target and subtracted from result
143         local float oldsolid;
144         vector vecs, dv;
145         oldsolid = ent.dphitcontentsmask;
146         if(ent.weapon == WEP_SNIPERRIFLE)
147                 ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
148         else
149                 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
150         if(antilag)
151                 WarpZone_traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
152                 // passing world, because we do NOT want it to touch dphitcontentsmask
153         else
154                 WarpZone_TraceLine(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NOMONSTERS, ent);
155         ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
156
157         vector vf, vr, vu;
158         vf = v_forward;
159         vr = v_right;
160         vu = v_up;
161         w_shotend = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
162         v_forward = vf;
163         v_right = vr;
164         v_up = vu;
165
166         // un-adjust trueaim if shotend is too close
167         if(vlen(w_shotend - (ent.origin + ent.view_ofs)) < autocvar_g_trueaim_minrange)
168                 w_shotend = ent.origin + ent.view_ofs + s_forward * autocvar_g_trueaim_minrange;
169
170         // track max damage
171         if(accuracy_canbegooddamage(ent))
172                 accuracy_add(ent, ent.weapon, maxdamage, 0);
173
174         W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
175
176         if(ent.weaponentity.movedir_x > 0)
177                 vecs = ent.weaponentity.movedir;
178         else
179                 vecs = '0 0 0';
180         if(debug_shotorg != '0 0 0')
181                 vecs = debug_shotorg;
182
183         dv = v_right * -vecs_y + v_up * vecs_z;
184         w_shotorg = ent.origin + ent.view_ofs + dv;
185
186         // now move the shotorg forward as much as requested if possible
187         if(antilag)
188         {
189                 if(ent.antilag_debug)
190                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
191                 else
192                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
193         }
194         else
195                 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
196         w_shotorg = trace_endpos - v_forward * nudge;
197         // calculate the shotdir from the chosen shotorg
198         w_shotdir = normalize(w_shotend - w_shotorg);
199
200         if (antilag)
201         if (!ent.cvar_cl_noantilag)
202         {
203                 if (autocvar_g_antilag == 1) // switch to "ghost" if not hitting original
204                 {
205                         traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
206                         if (!trace_ent.takedamage)
207                         {
208                                 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
209                                 if (trace_ent.takedamage && trace_ent.classname == "player")
210                                 {
211                                         entity e;
212                                         e = trace_ent;
213                                         traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
214                                         if(trace_ent == e)
215                                                 w_shotdir = normalize(trace_ent.origin - w_shotorg);
216                                 }
217                         }
218                 }
219                 else if(autocvar_g_antilag == 3) // client side hitscan
220                 {
221                         // this part MUST use prydon cursor
222                         if (ent.cursor_trace_ent)                 // client was aiming at someone
223                         if (ent.cursor_trace_ent != ent)         // just to make sure
224                         if (ent.cursor_trace_ent.takedamage)      // and that person is killable
225                         if (ent.cursor_trace_ent.classname == "player") // and actually a player
226                         {
227                                 // verify that the shot would miss without antilag
228                                 // (avoids an issue where guns would always shoot at their origin)
229                                 traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
230                                 if (!trace_ent.takedamage)
231                                 {
232                                         // verify that the shot would hit if altered
233                                         traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
234                                         if (trace_ent == ent.cursor_trace_ent)
235                                                 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
236                                         else
237                                                 print("antilag fail\n");
238                                 }
239                         }
240                 }
241         }
242
243         ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
244
245         if (!g_norecoil)
246                 ent.punchangle_x = recoil * -1;
247
248         if (snd != "")
249                 sound (ent, chan, snd, VOL_BASE, ATTN_NORM);
250
251         if(ent.items & IT_STRENGTH)
252         if(!g_minstagib)
253         if(
254                 (time > ent.prevstrengthsound + autocvar_sv_strengthsound_antispam_time)
255                 ||
256                 (time > ent.prevstrengthsoundattempt + autocvar_sv_strengthsound_antispam_refire_threshold)
257         ) // prevent insane sound spam
258         {
259                 sound(ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
260                 ent.prevstrengthsound = time;
261         }
262         ent.prevstrengthsoundattempt = time;
263
264         // nudge w_shotend so a trace to w_shotend hits
265         w_shotend = w_shotend + normalize(w_shotend - w_shotorg) * nudge;
266 };
267
268 #define W_SetupShot_Dir_ProjectileSize(ent,s_forward,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize_Range(ent, s_forward, mi, ma, antilag, recoil, snd, chan, maxdamage, MAX_SHOT_DISTANCE)
269 #define W_SetupShot_ProjectileSize(ent,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, v_forward, mi, ma, antilag, recoil, snd, chan, maxdamage)
270 #define W_SetupShot_Dir(ent,s_forward,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, s_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
271 #define W_SetupShot(ent,antilag,recoil,snd,chan,maxdamage) W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
272 #define W_SetupShot_Range(ent,antilag,recoil,snd,chan,maxdamage,range) W_SetupShot_Dir_ProjectileSize_Range(ent, v_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage, range)
273
274 float CL_Weaponentity_CustomizeEntityForClient()
275 {
276         self.viewmodelforclient = self.owner;
277         if(other.classname == "spectator")
278                 if(other.enemy == self.owner)
279                         self.viewmodelforclient = other;
280         return TRUE;
281 }
282
283 float qcweaponanimation;
284 vector weapon_offset = '0 -10 0';
285 vector weapon_adjust = '10 0 -15';
286 .vector weapon_morph0origin;
287 .vector weapon_morph0angles;
288 .float  weapon_morph0time;
289 .vector weapon_morph1origin;
290 .vector weapon_morph1angles;
291 .float  weapon_morph1time;
292 .vector weapon_morph2origin;
293 .vector weapon_morph2angles;
294 .float  weapon_morph2time;
295 .vector weapon_morph3origin;
296 .vector weapon_morph3angles;
297 .float  weapon_morph3time;
298 .vector weapon_morph4origin;
299 .vector weapon_morph4angles;
300 .float  weapon_morph4time;
301 .string weaponname;
302 #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)
303
304 /*
305  * supported formats:
306  *
307  * 1. simple animated model, muzzle flash handling on h_ model:
308  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
309  *      tags:
310  *        shot = muzzle end (shot origin, also used for muzzle flashes)
311  *        shell = casings ejection point (must be on the right hand side of the gun)
312  *        weapon = attachment for v_tuba.md3
313  *    v_tuba.md3 - first and third person model
314  *    g_tuba.md3 - pickup model
315  *
316  * 2. simple animated model, muzzle flash handling on v_ model:
317  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
318  *      tags:
319  *        weapon = attachment for v_tuba.md3
320  *    v_tuba.md3 - first and third person model
321  *      tags:
322  *        shot = muzzle end (shot origin, also used for muzzle flashes)
323  *        shell = casings ejection point (must be on the right hand side of the gun)
324  *    g_tuba.md3 - pickup model
325  *
326  * 3. fully animated model, muzzle flash handling on h_ model:
327  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
328  *      tags:
329  *        shot = muzzle end (shot origin, also used for muzzle flashes)
330  *        shell = casings ejection point (must be on the right hand side of the gun)
331  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
332  *    v_tuba.md3 - third person model
333  *    g_tuba.md3 - pickup model
334  *
335  * 4. fully animated model, muzzle flash handling on v_ model:
336  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
337  *      tags:
338  *        shot = muzzle end (shot origin)
339  *        shell = casings ejection point (must be on the right hand side of the gun)
340  *    v_tuba.md3 - third person model
341  *      tags:
342  *        shot = muzzle end (for muzzle flashes)
343  *    g_tuba.md3 - pickup model
344  */
345
346 // writes:
347 //   self.origin, self.angles
348 //   self.weaponentity
349 //   self.movedir, self.view_ofs
350 //   attachment stuff
351 //   anim stuff
352 // to free:
353 //   call again with ""
354 //   remove the ent
355 void CL_WeaponEntity_SetModel(string name)
356 {
357         string animfilename;
358         float animfile;
359         float v_shot_idx;
360         if (name != "")
361         {
362                 // if there is a child entity, hide it until we're sure we use it
363                 if (self.weaponentity)
364                         self.weaponentity.model = "";
365                 setmodel(self, strcat("models/weapons/v_", name, ".md3")); // precision set below
366                 v_shot_idx = gettagindex(self, "shot"); // used later
367                 if(!v_shot_idx)
368                         v_shot_idx = gettagindex(self, "tag_shot");
369
370                 if(qcweaponanimation)
371                 {
372                         self.angles = '0 0 0';
373                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
374                         self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
375                         self.movedir_x += 32;
376                         self.spawnorigin = self.movedir;
377                         // oldorigin - not calculated here
378                 }
379                 else
380                 {
381                         setmodel(self, strcat("models/weapons/h_", name, ".iqm")); // precision set below
382                         animfilename = strcat("models/weapons/h_", name, ".iqm.animinfo");
383                         animfile = fopen(animfilename, FILE_READ);
384                         // preset some defaults that work great for renamed zym files (which don't need an animinfo)
385                         self.anim_fire1  = '0 1 0.01';
386                         self.anim_fire2  = '1 1 0.01';
387                         self.anim_idle   = '2 1 0.01';
388                         self.anim_reload = '3 1 0.01';
389                         if (animfile >= 0)
390                         {
391                                 animparseerror = FALSE;
392                                 self.anim_fire1  = animparseline(animfile);
393                                 self.anim_fire2  = animparseline(animfile);
394                                 self.anim_idle   = animparseline(animfile);
395                                 self.anim_reload = animparseline(animfile);
396                                 fclose(animfile);
397                                 if (animparseerror)
398                                         print("Parse error in ", animfilename, ", some player animations are broken\n");
399                         }
400
401                         // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
402                         // if we don't, this is a "real" animated model
403                         if(gettagindex(self, "weapon"))
404                         {
405                                 if (!self.weaponentity)
406                                         self.weaponentity = spawn();
407                                 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
408                                 setattachment(self.weaponentity, self, "weapon");
409                         }
410                         else if(gettagindex(self, "tag_weapon"))
411                         {
412                                 if (!self.weaponentity)
413                                         self.weaponentity = spawn();
414                                 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
415                                 setattachment(self.weaponentity, self, "tag_weapon");
416                         }
417                         else
418                         {
419                                 if(self.weaponentity)
420                                         remove(self.weaponentity);
421                                 self.weaponentity = world;
422                         }
423
424                         setorigin(self,'0 0 0');
425                         self.angles = '0 0 0';
426                         self.frame = 0;
427                         self.viewmodelforclient = world;
428
429                         float idx;
430
431                         if(v_shot_idx) // v_ model attached to invisible h_ model
432                         {
433                                 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
434                         }
435                         else
436                         {
437                                 idx = gettagindex(self, "shot");
438                                 if(!idx)
439                                         idx = gettagindex(self, "tag_shot");
440                                 if(idx)
441                                         self.movedir = gettaginfo(self, idx);
442                                 else
443                                 {
444                                         print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
445                                         self.movedir = '0 0 0';
446                                 }
447                         }
448
449                         if(self.weaponentity) // v_ model attached to invisible h_ model
450                         {
451                                 idx = gettagindex(self.weaponentity, "shell");
452                                 if(!idx)
453                                         idx = gettagindex(self.weaponentity, "tag_shell");
454                                 if(idx)
455                                         self.spawnorigin = gettaginfo(self.weaponentity, idx);
456                         }
457                         else
458                                 idx = 0;
459                         if(!idx)
460                         {
461                                 idx = gettagindex(self, "shell");
462                                 if(!idx)
463                                         idx = gettagindex(self, "tag_shell");
464                                 if(idx)
465                                         self.spawnorigin = gettaginfo(self, idx);
466                                 else
467                                 {
468                                         print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
469                                         self.spawnorigin = self.movedir;
470                                 }
471                         }
472
473                         if(v_shot_idx)
474                         {
475                                 self.oldorigin = '0 0 0'; // use regular attachment
476                         }
477                         else
478                         {
479                                 if(self.weaponentity)
480                                 {
481                                         idx = gettagindex(self, "weapon");
482                                         if(!idx)
483                                                 idx = gettagindex(self, "tag_weapon");
484                                 }
485                                 else
486                                 {
487                                         idx = gettagindex(self, "handle");
488                                         if(!idx)
489                                                 idx = gettagindex(self, "tag_handle");
490                                 }
491                                 if(idx)
492                                 {
493                                         self.oldorigin = self.movedir - gettaginfo(self, idx);
494                                 }
495                                 else
496                                 {
497                                         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");
498                                         self.oldorigin = '0 0 0'; // there is no way to recover from this
499                                 }
500                         }
501
502                         self.viewmodelforclient = self.owner;
503                 }
504         }
505         else
506         {
507                 self.model = "";
508                 if(self.weaponentity)
509                         remove(self.weaponentity);
510                 self.weaponentity = world;
511                 self.movedir = '0 0 0';
512                 self.spawnorigin = '0 0 0';
513                 self.oldorigin = '0 0 0';
514                 self.anim_fire1  = '0 1 0.01';
515                 self.anim_fire2  = '0 1 0.01';
516                 self.anim_idle   = '0 1 0.01';
517                 self.anim_reload = '0 1 0.01';
518         }
519
520         self.view_ofs = '0 0 0';
521
522         if(self.movedir_x >= 0)
523         {
524                 vector v0;
525                 v0 = self.movedir;
526                 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
527                 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
528         }
529         self.owner.stat_shotorg = compressShotOrigin(self.movedir);
530         self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
531
532         self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
533
534         // check if an instant weapon switch occurred
535         if (qcweaponanimation)
536         {
537                 if (self.state == WS_READY)
538                 {
539                         self.angles = '0 0 0';
540                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
541                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
542                 }
543         }
544         else
545                 setorigin(self, self.view_ofs);
546         // reset animstate now
547         self.wframe = WFRAME_IDLE;
548         self.weapon_morph0time = 0;
549         self.weapon_morph1time = 0;
550         self.weapon_morph2time = 0;
551         self.weapon_morph3time = 0;
552         self.weapon_morph4time = 0;
553         setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
554 }
555
556 vector CL_Weapon_GetShotOrg(float wpn)
557 {
558         entity wi, oldself;
559         vector ret;
560         wi = get_weaponinfo(wpn);
561         oldself = self;
562         self = spawn();
563         CL_WeaponEntity_SetModel(wi.mdl);
564         ret = self.movedir;
565         CL_WeaponEntity_SetModel("");
566         remove(self);
567         self = oldself;
568         return ret;
569 }
570
571 void CL_Weaponentity_Think()
572 {
573         float tb;
574         self.nextthink = time;
575         if (intermission_running)
576                 self.frame = self.anim_idle_x;
577         if (self.owner.weaponentity != self)
578         {
579                 if (self.weaponentity)
580                         remove(self.weaponentity);
581                 remove(self);
582                 return;
583         }
584         if (self.owner.deadflag != DEAD_NO)
585         {
586                 self.model = "";
587                 if (self.weaponentity)
588                         self.weaponentity.model = "";
589                 return;
590         }
591         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
592         {
593                 self.cnt = self.owner.weapon;
594                 self.dmg = self.owner.modelindex;
595                 self.deadflag = self.owner.deadflag;
596
597                 CL_WeaponEntity_SetModel(self.owner.weaponname);
598         }
599
600         tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
601         self.effects = self.owner.effects & EFMASK_CHEAP;
602         self.effects &~= EF_LOWPRECISION;
603         self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
604         self.effects &~= EF_TELEPORT_BIT;
605         self.effects &~= EF_RESTARTANIM_BIT;
606         self.effects |= tb;
607
608         if(self.owner.alpha == default_player_alpha)
609                 self.alpha = default_weapon_alpha;
610         else if(self.owner.alpha != 0)
611                 self.alpha = self.owner.alpha;
612         else
613                 self.alpha = 1;
614
615         self.glowmod = self.owner.weaponentity_glowmod;
616         self.colormap = self.owner.colormap;
617         if (self.weaponentity)
618         {
619                 self.weaponentity.effects = self.effects;
620                 self.weaponentity.alpha = self.alpha;
621                 self.weaponentity.colormap = self.colormap;
622                 self.weaponentity.glowmod = self.glowmod;
623         }
624
625         self.angles = '0 0 0';
626         local float f;
627         f = 0;
628         if (self.state == WS_RAISE && !intermission_running)
629         {
630                 f = (self.owner.weapon_nextthink - time) * g_weaponratefactor / autocvar_g_balance_weaponswitchdelay;
631                 self.angles_x = -90 * f * f;
632                 if (qcweaponanimation)
633                 {
634                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
635                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
636                 }
637         }
638         else if (self.state == WS_DROP && !intermission_running)
639         {
640                 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponratefactor / autocvar_g_balance_weaponswitchdelay;
641                 self.angles_x = -90 * f * f;
642                 if (qcweaponanimation)
643                 {
644                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
645                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
646                 }
647         }
648         else if (self.state == WS_CLEAR)
649         {
650                 f = 1;
651                 self.angles_x = -90 * f * f;
652                 if (qcweaponanimation)
653                 {
654                         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
655                         setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
656                 }
657         }
658         else if (qcweaponanimation && time < self.owner.weapon_morph1time)
659         {
660                 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
661                 f = 1 - pow(1 - f, 3);
662                 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
663                 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
664         }
665         else if (qcweaponanimation && time < self.owner.weapon_morph2time)
666         {
667                 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
668                 f = 1 - pow(1 - f, 3);
669                 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
670                 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
671         }
672         else if (qcweaponanimation && time < self.owner.weapon_morph3time)
673         {
674                 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
675                 f = 1 - pow(1 - f, 3);
676                 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
677                 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
678         }
679         else if (qcweaponanimation && time < self.owner.weapon_morph4time)
680         {
681                 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
682                 f = 1 - pow(1 - f, 3);
683                 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
684                 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
685         }
686         else if (qcweaponanimation)
687         {
688                 // begin a new idle morph
689                 self.owner.weapon_morph0time   = time;
690                 self.owner.weapon_morph0angles = self.angles;
691                 self.owner.weapon_morph0origin = self.origin;
692
693                 float r;
694                 float t;
695
696                 r = random();
697                 if (r < 0.1)
698                 {
699                         // turn gun to the left to look at it
700                         t = 2;
701                         self.owner.weapon_morph1time   = time + t * 0.2;
702                         self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
703                         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');
704                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
705
706                         self.owner.weapon_morph2time   = time + t * 0.6;
707                         self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
708                         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');
709                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
710
711                         self.owner.weapon_morph3time   = time + t;
712                         self.owner.weapon_morph3angles = '0 0 0';
713                         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');
714                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
715                 }
716                 else if (r < 0.2)
717                 {
718                         // raise the gun a bit
719                         t = 2;
720                         self.owner.weapon_morph1time   = time + t * 0.2;
721                         self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
722                         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');
723                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
724
725                         self.owner.weapon_morph2time   = time + t * 0.5;
726                         self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
727                         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');
728                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
729
730                         self.owner.weapon_morph3time   = time + t;
731                         self.owner.weapon_morph3angles = '0 0 0';
732                         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');
733                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
734                 }
735                 else if (r < 0.3)
736                 {
737                         // tweak it a bit
738                         t = 5;
739                         self.owner.weapon_morph1time   = time + t * 0.3;
740                         self.owner.weapon_morph1angles = randomvec() * 6;
741                         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');
742                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
743
744                         self.owner.weapon_morph2time   = time + t * 0.7;
745                         self.owner.weapon_morph2angles = randomvec() * 6;
746                         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');
747                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
748
749                         self.owner.weapon_morph3time   = time + t;
750                         self.owner.weapon_morph3angles = '0 0 0';
751                         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');
752                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
753                 }
754                 else
755                 {
756                         // hold it mostly steady
757                         t = random() * 6 + 4;
758                         self.owner.weapon_morph1time   = time + t * 0.2;
759                         self.owner.weapon_morph1angles = randomvec() * 1;
760                         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');
761                         self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
762
763                         self.owner.weapon_morph2time   = time + t * 0.5;
764                         self.owner.weapon_morph2angles = randomvec() * 1;
765                         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');
766                         self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
767
768                         self.owner.weapon_morph3time   = time + t * 0.7;
769                         self.owner.weapon_morph3angles = randomvec() * 1;
770                         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');
771                         self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
772                 }
773
774                 self.owner.weapon_morph4time   = time + t;
775                 self.owner.weapon_morph4angles = '0 0 0';
776                 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');
777                 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
778
779         }
780 };
781
782 void CL_ExteriorWeaponentity_Think()
783 {
784         float tag_found;
785         vector ang;
786         self.nextthink = time;
787         if (self.owner.exteriorweaponentity != self)
788         {
789                 remove(self);
790                 return;
791         }
792         if (self.owner.deadflag != DEAD_NO)
793         {
794                 self.model = "";
795                 return;
796         }
797         if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
798         {
799                 self.cnt = self.owner.weapon;
800                 self.dmg = self.owner.modelindex;
801                 self.deadflag = self.owner.deadflag;
802                 if (self.owner.weaponname != "")
803                         setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
804                 else
805                         self.model = "";
806
807                 if((tag_found = gettagindex(self.owner, "tag_weapon")))
808                 {
809                         self.tag_index = tag_found;
810                         self.tag_entity = self.owner;
811                 }
812                 else
813                         setattachment(self, self.owner, "bip01 r hand");
814
815                 // if that didn't find a tag, hide the exterior weapon model
816                 if (!self.tag_index)
817                         self.model = "";
818         }
819         self.effects = self.owner.effects;
820         if(sv_pitch_min == sv_pitch_max)
821                 self.effects |= EF_LOWPRECISION;
822         else
823                 self.effects &~= EF_LOWPRECISION;
824         self.effects = self.effects & EFMASK_CHEAP; // eat performance
825         if(self.owner.alpha == default_player_alpha)
826                 self.alpha = default_weapon_alpha;
827         else if(self.owner.alpha != 0)
828                 self.alpha = self.owner.alpha;
829         else
830                 self.alpha = 1;
831
832         ang_x = bound(sv_pitch_min, self.owner.v_angle_x, sv_pitch_max);
833         ang_y = 0;
834         ang_z = 0;
835
836         if(sv_pitch_fixyaw) // workaround for stupid player models that don't aim forward
837         {
838                 ang_y = self.owner.v_angle_y;
839                 makevectors(ang);
840                 var vector v = v_forward;
841                 var float t = self.tag_entity.frame1time;
842                 var float f = self.tag_entity.frame;
843                 self.tag_entity.frame1time = time;
844                 self.tag_entity.frame = self.tag_entity.anim_idle_x;
845                 gettaginfo(self.tag_entity, self.tag_index);
846                 self.tag_entity.frame1time = t;
847                 self.tag_entity.frame = f;
848                 // untransform v according to this coordinate space
849                 vector w;
850                 w_x = v_forward * v;
851                 w_y = -v_right * v;
852                 w_z = v_up * v;
853                 self.angles = vectoangles(w);
854         }
855         else
856         {
857                 ang_x = -/* don't ask */ang_x;
858                 self.angles = ang;
859         }
860
861         self.glowmod = self.owner.weaponentity_glowmod;
862         self.colormap = self.owner.colormap;
863 };
864
865 // spawning weaponentity for client
866 void CL_SpawnWeaponentity()
867 {
868         self.weaponentity = spawn();
869         self.weaponentity.classname = "weaponentity";
870         self.weaponentity.solid = SOLID_NOT;
871         self.weaponentity.owner = self;
872         setmodel(self.weaponentity, ""); // precision set when changed
873         setorigin(self.weaponentity, '0 0 0');
874         self.weaponentity.angles = '0 0 0';
875         self.weaponentity.viewmodelforclient = self;
876         self.weaponentity.flags = 0;
877         self.weaponentity.think = CL_Weaponentity_Think;
878         self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
879         self.weaponentity.nextthink = time;
880
881         self.exteriorweaponentity = spawn();
882         self.exteriorweaponentity.classname = "exteriorweaponentity";
883         self.exteriorweaponentity.solid = SOLID_NOT;
884         self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
885         self.exteriorweaponentity.owner = self;
886         setorigin(self.exteriorweaponentity, '0 0 0');
887         self.exteriorweaponentity.angles = '0 0 0';
888         self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
889         self.exteriorweaponentity.nextthink = time;
890 };
891
892 void Send_WeaponComplain (entity e, float wpn, string wpnname, float type)
893 {
894         msg_entity = e;
895         WriteByte(MSG_ONE, SVC_TEMPENTITY);
896         WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
897         WriteByte(MSG_ONE, wpn);
898         WriteString(MSG_ONE, wpnname);
899         WriteByte(MSG_ONE, type);
900 }
901
902 .float hasweapon_complain_spam;
903
904 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
905 {
906         local float weaponbit, f;
907         local entity oldself;
908
909         if(time < self.hasweapon_complain_spam)
910                 complain = 0;
911         if(complain)
912                 self.hasweapon_complain_spam = time + 0.2;
913
914         if (wpn < WEP_FIRST || wpn > WEP_LAST)
915         {
916                 if (complain)
917                         sprint(self, "Invalid weapon\n");
918                 return FALSE;
919         }
920         weaponbit = W_WeaponBit(wpn);
921         if (cl.weapons & weaponbit)
922         {
923                 if (andammo)
924                 {
925                         if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
926                         {
927                                 f = 1;
928                         }
929                         else
930                         {
931                                 oldself = self;
932                                 self = cl;
933                                 f = weapon_action(wpn, WR_CHECKAMMO1);
934                                 f = f + weapon_action(wpn, WR_CHECKAMMO2);
935
936                                 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
937                                 local entity mine;
938                                 if(wpn == WEP_MINE_LAYER)
939                                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
940                                         f = 1;
941
942                                 self = oldself;
943                         }
944                         if (!f)
945                         {
946                                 if (complain)
947                                 if(clienttype(cl) == CLIENTTYPE_REAL)
948                                 {
949                                         play2(cl, "weapons/unavailable.wav");
950                                         sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
951                                         Send_WeaponComplain (cl, wpn, W_Name(wpn), 0);
952                                 }
953                                 return FALSE;
954                         }
955                 }
956                 return TRUE;
957         }
958         if (complain)
959         {
960                 // DRESK - 3/16/07
961                 // Report Proper Weapon Status / Modified Weapon Ownership Message
962                 if(weaponsInMap & weaponbit)
963                 {
964                         sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
965                         Send_WeaponComplain (cl, wpn, W_Name(wpn), 1);
966
967                         if(autocvar_g_showweaponspawns)
968                         {
969                                 entity e;
970                                 string s;
971
972                                 e = get_weaponinfo(wpn);
973                                 s = e.model2;
974
975                                 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
976                                 {
977                                         if(e.classname == "droppedweapon")
978                                                 continue;
979                                         if not(e.flags & FL_ITEM)
980                                                 continue;
981                                         WaypointSprite_Spawn(
982                                                 s,
983                                                 1, 0,
984                                                 world, e.origin,
985                                                 self, 0,
986                                                 world, enemy,
987                                                 0
988                                         );
989                                 }
990                         }
991                 }
992                 else
993                 {
994                         Send_WeaponComplain (cl, wpn, W_Name(wpn), 2);
995                         sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
996                 }
997
998                 play2(cl, "weapons/unavailable.wav");
999         }
1000         return FALSE;
1001 };
1002
1003 // Weapon subs
1004 void w_clear()
1005 {
1006         if (self.weapon != -1)
1007                 self.weapon = 0;
1008         if (self.weaponentity)
1009         {
1010                 self.weaponentity.state = WS_CLEAR;
1011                 self.weaponentity.effects = 0;
1012         }
1013 };
1014
1015 void w_ready()
1016 {
1017         if (self.weaponentity)
1018                 self.weaponentity.state = WS_READY;
1019         weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
1020 };
1021
1022 // Setup weapon for client (after this raise frame will be launched)
1023 void weapon_setup(float windex)
1024 {
1025         entity e;
1026         qcweaponanimation = autocvar_sv_qcweaponanimation;
1027         e = get_weaponinfo(windex);
1028         self.items &~= IT_AMMO;
1029         self.items = self.items | e.items;
1030
1031         // the two weapon entities will notice this has changed and update their models
1032         self.weapon = windex;
1033         self.weaponname = e.mdl;
1034         self.bulletcounter = 0;
1035 };
1036
1037 // perform weapon to attack (weaponstate and attack_finished check is here)
1038 void W_SwitchToOtherWeapon(entity pl)
1039 {
1040         // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
1041         float w, ww;
1042         w = W_WeaponBit(pl.weapon);
1043         pl.weapons &~= w;
1044         ww = w_getbestweapon(pl);
1045         pl.weapons |= w;
1046         if(ww)
1047                 W_SwitchWeapon_Force(pl, ww);
1048 }
1049 .float prevdryfire;
1050 float weapon_prepareattack_checkammo(float secondary)
1051 {
1052         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1053         if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
1054         {
1055                 // always keep the Mine Layer if we placed mines, so that we can detonate them
1056                 local entity mine;
1057                 if(self.weapon == WEP_MINE_LAYER)
1058                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
1059                         return FALSE;
1060
1061                 if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons
1062                 {
1063                         sound (self, CHAN_AUTO, "weapons/dryfire.wav", VOL_BASE, ATTN_NORM);
1064                         self.prevdryfire = time;
1065                 }
1066
1067                 W_SwitchToOtherWeapon(self);
1068                 return FALSE;
1069         }
1070         return TRUE;
1071 }
1072 .float race_penalty;
1073 float weapon_prepareattack_check(float secondary, float attacktime)
1074 {
1075         if(!weapon_prepareattack_checkammo(secondary))
1076                 return FALSE;
1077
1078         //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
1079         //if all players readied up and the countdown is running
1080         if(time < game_starttime || time < self.race_penalty) {
1081                 return FALSE;
1082         }
1083
1084         if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
1085                 return FALSE;
1086
1087         // do not even think about shooting if switching
1088         if(self.switchweapon != self.weapon)
1089                 return FALSE;
1090
1091         if(attacktime >= 0)
1092         {
1093                 // don't fire if previous attack is not finished
1094                 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
1095                         return FALSE;
1096                 // don't fire while changing weapon
1097                 if (self.weaponentity.state != WS_READY)
1098                         return FALSE;
1099         }
1100
1101         return TRUE;
1102 }
1103 float weapon_prepareattack_do(float secondary, float attacktime)
1104 {
1105         self.weaponentity.state = WS_INUSE;
1106
1107         self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1108
1109         // if the weapon hasn't been firing continuously, reset the timer
1110         if(attacktime >= 0)
1111         {
1112                 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
1113                 {
1114                         ATTACK_FINISHED(self) = time;
1115                         //dprint("resetting attack finished to ", ftos(time), "\n");
1116                 }
1117                 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
1118         }
1119         self.bulletcounter += 1;
1120         //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1121         return TRUE;
1122 }
1123 float weapon_prepareattack(float secondary, float attacktime)
1124 {
1125         if(weapon_prepareattack_check(secondary, attacktime))
1126         {
1127                 weapon_prepareattack_do(secondary, attacktime);
1128                 return TRUE;
1129         }
1130         else
1131                 return FALSE;
1132 }
1133
1134 void weapon_thinkf(float fr, float t, void() func)
1135 {
1136         vector a;
1137         vector of, or, ou;
1138         float restartanim;
1139
1140         if(fr == WFRAME_DONTCHANGE)
1141         {
1142                 fr = self.weaponentity.wframe;
1143                 restartanim = FALSE;
1144         }
1145         else if (fr == WFRAME_IDLE)
1146                 restartanim = FALSE;
1147         else
1148                 restartanim = TRUE;
1149
1150         of = v_forward;
1151         or = v_right;
1152         ou = v_up;
1153
1154         if (self.weaponentity)
1155         {
1156                 self.weaponentity.wframe = fr;
1157                 if (qcweaponanimation)
1158                 {
1159                         if (fr != WFRAME_IDLE)
1160                         {
1161                                 self.weapon_morph0time = time;
1162                                 self.weapon_morph0angles = self.weaponentity.angles;
1163                                 self.weapon_morph0origin = self.weaponentity.origin;
1164
1165                                 self.weapon_morph1angles = '0 0 0';
1166                                 self.weapon_morph1time = time + t;
1167                                 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1168                                 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1169
1170                                 self.weapon_morph2angles = '0 0 0';
1171                                 self.weapon_morph2time = time + t;
1172                                 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1173                                 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1174
1175                                 self.weapon_morph3angles = '0 0 0';
1176                                 self.weapon_morph3time = time + t;
1177                                 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1178                                 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1179
1180                                 self.weapon_morph4angles = '0 0 0';
1181                                 self.weapon_morph4time = time + t;
1182                                 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1183                                 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1184
1185                                 if (fr == WFRAME_FIRE1)
1186                                 {
1187                                         self.weapon_morph1angles = '5 0 0';
1188                                         self.weapon_morph1time = time + t * 0.1;
1189                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1190                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1191                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1192                                 }
1193                                 else if (fr == WFRAME_FIRE2)
1194                                 {
1195                                         self.weapon_morph1angles = '10 0 0';
1196                                         self.weapon_morph1time = time + t * 0.1;
1197                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1198                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1199                                         self.weapon_morph4time = time + t + 1; // delay idle effect
1200                                 }
1201                                 else if (fr == WFRAME_RELOAD)
1202                                 {
1203                                         self.weapon_morph1time = time + t * 0.05;
1204                                         self.weapon_morph1angles = '-10 40 0';
1205                                         makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1206                                         self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1207
1208                                         self.weapon_morph2time = time + t * 0.15;
1209                                         self.weapon_morph2angles = '-10 40 5';
1210                                         makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1211                                         self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1212
1213                                         self.weapon_morph3time = time + t * 0.25;
1214                                         self.weapon_morph3angles = '-10 40 0';
1215                                         makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1216                                         self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1217                                 }
1218                         }
1219                 }
1220                 else
1221                 {
1222                         if (fr == WFRAME_IDLE)
1223                                 a = self.weaponentity.anim_idle;
1224                         else if (fr == WFRAME_FIRE1)
1225                                 a = self.weaponentity.anim_fire1;
1226                         else if (fr == WFRAME_FIRE2)
1227                                 a = self.weaponentity.anim_fire2;
1228                         else if (fr == WFRAME_RELOAD)
1229                                 a = self.weaponentity.anim_reload;
1230                         a_z *= g_weaponratefactor;
1231                         setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1232                 }
1233         }
1234
1235         v_forward = of;
1236         v_right = or;
1237         v_up = ou;
1238
1239         if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1240         {
1241                 backtrace("Tried to override initial weapon think function - should this really happen?");
1242         }
1243
1244         t *= W_WeaponRateFactor();
1245
1246         // VorteX: haste can be added here
1247         if (self.weapon_think == w_ready)
1248         {
1249                 self.weapon_nextthink = time;
1250                 //dprint("started firing at ", ftos(time), "\n");
1251         }
1252         if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
1253         {
1254                 self.weapon_nextthink = time;
1255                 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1256         }
1257         self.weapon_nextthink = self.weapon_nextthink + t;
1258         self.weapon_think = func;
1259         //dprint("next ", ftos(self.weapon_nextthink), "\n");
1260
1261         if (restartanim)
1262         if (t)
1263         if (!self.crouch) // shoot anim stands up, this looks bad
1264         {
1265                 local vector anim;
1266                 anim = self.anim_shoot;
1267                 anim_z = anim_y / (t + sys_frametime);
1268                 setanim(self, anim, FALSE, TRUE, TRUE);
1269         }
1270 };
1271
1272 void weapon_boblayer1(float spd, vector org)
1273 {
1274         // VorteX: haste can be added here
1275 };
1276
1277 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity, float forceAbsolute)
1278 {
1279         vector mdirection;
1280         float mspeed;
1281         float outspeed;
1282         float nstyle;
1283         vector outvelocity;
1284
1285         mvelocity = mvelocity * g_weaponspeedfactor;
1286
1287         mdirection = normalize(mvelocity);
1288         mspeed = vlen(mvelocity);
1289
1290         nstyle = autocvar_g_projectiles_newton_style;
1291         if(nstyle == 0 || forceAbsolute)
1292         {
1293                 // absolute velocity
1294                 outvelocity = mvelocity;
1295         }
1296         else if(nstyle == 1)
1297         {
1298                 // true Newtonian projectiles
1299                 outvelocity = pvelocity + mvelocity;
1300         }
1301         else if(nstyle == 2)
1302         {
1303                 // true Newtonian projectiles with automatic aim adjustment
1304                 //
1305                 // solve: |outspeed * mdirection - pvelocity| = mspeed
1306                 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1307                 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1308                 // PLUS SIGN!
1309                 // not defined?
1310                 // then...
1311                 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1312                 // velocity without mdirection component > mspeed
1313                 // fire at smallest possible mspeed that works?
1314                 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1315
1316                 vector solution;
1317                 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1318                 if(solution_z)
1319                         outspeed = solution_y; // the larger one
1320                 else
1321                 {
1322                         //outspeed = 0; // slowest possible shot
1323                         outspeed = solution_x; // the real part (that is, the average!)
1324                         //dprint("impossible shot, adjusting\n");
1325                 }
1326
1327                 outspeed = bound(mspeed * autocvar_g_projectiles_newton_style_2_minfactor, outspeed, mspeed * autocvar_g_projectiles_newton_style_2_maxfactor);
1328                 outvelocity = mdirection * outspeed;
1329         }
1330         else if(nstyle == 3)
1331         {
1332                 // pseudo-Newtonian:
1333                 outspeed = mspeed + mdirection * pvelocity;
1334                 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1335                 outvelocity = mdirection * outspeed;
1336         }
1337         else if(nstyle == 4)
1338         {
1339                 // tZorkian:
1340                 outspeed = mspeed + vlen(pvelocity);
1341                 outvelocity = mdirection * outspeed;
1342         }
1343         else
1344                 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1345
1346         return outvelocity;
1347 }
1348
1349 void W_AttachToShotorg(entity flash, vector offset)
1350 {
1351         entity xflash;
1352         flash.owner = self;
1353         flash.angles_z = random() * 360;
1354         if(qcweaponanimation)
1355         {
1356                 setorigin(flash, w_shotorg + w_shotdir * 50);
1357                 flash.angles = vectoangles(w_shotdir);
1358                 flash.angles_z = random() * 360;
1359         }
1360         else
1361         {
1362                 if(gettagindex(self.weaponentity, "shot"))
1363                         setattachment(flash, self.weaponentity, "shot");
1364                 else
1365                         setattachment(flash, self.weaponentity, "tag_shot");
1366                 setorigin(flash, offset);
1367
1368                 xflash = spawn();
1369                 copyentity(flash, xflash);
1370
1371                 flash.viewmodelforclient = self;
1372
1373                 if(self.weaponentity.oldorigin_x > 0)
1374                 {
1375                         setattachment(xflash, self.exteriorweaponentity, "");
1376                         setorigin(xflash, self.weaponentity.oldorigin + offset);
1377                 }
1378                 else
1379                 {
1380                         if(gettagindex(self.exteriorweaponentity, "shot"))
1381                                 setattachment(xflash, self.exteriorweaponentity, "shot");
1382                         else
1383                                 setattachment(xflash, self.exteriorweaponentity, "tag_shot");
1384                         setorigin(xflash, offset);
1385                 }
1386         }
1387 }
1388
1389 vector cliptoplane(vector v, vector p)
1390 {
1391         return v - (v * p) * p;
1392 }
1393
1394 vector solve_cubic_pq(float p, float q)
1395 {
1396         float D, u, v, a;
1397         D = q*q/4.0 + p*p*p/27.0;
1398         if(D < 0)
1399         {
1400                 // irreducibilis
1401                 a = 1.0/3.0 * acos(-q/2.0 * sqrt(-27.0/(p*p*p)));
1402                 u = sqrt(-4.0/3.0 * p);
1403                 // a in range 0..pi/3
1404                 // cos(a)
1405                 // cos(a + 2pi/3)
1406                 // cos(a + 4pi/3)
1407                 return
1408                         u *
1409                         (
1410                                 '1 0 0' * cos(a + 2.0/3.0*M_PI)
1411                                 +
1412                                 '0 1 0' * cos(a + 4.0/3.0*M_PI)
1413                                 +
1414                                 '0 0 1' * cos(a)
1415                         );
1416         }
1417         else if(D == 0)
1418         {
1419                 // simple
1420                 if(p == 0)
1421                         return '0 0 0';
1422                 u = 3*q/p;
1423                 v = -u/2;
1424                 if(u >= v)
1425                         return '1 1 0' * v + '0 0 1' * u;
1426                 else
1427                         return '0 1 1' * v + '1 0 0' * u;
1428         }
1429         else
1430         {
1431                 // cardano
1432                 u = cbrt(-q/2.0 + sqrt(D));
1433                 v = cbrt(-q/2.0 - sqrt(D));
1434                 return '1 1 1' * (u + v);
1435         }
1436 }
1437 vector solve_cubic_abcd(float a, float b, float c, float d)
1438 {
1439         // y = 3*a*x + b
1440         // x = (y - b) / 3a
1441         float p, q;
1442         vector v;
1443         p = (9*a*c - 3*b*b);
1444         q = (27*a*a*d - 9*a*b*c + 2*b*b*b);
1445         v = solve_cubic_pq(p, q);
1446         v = (v -  b * '1 1 1') * (1.0 / (3.0 * a));
1447         if(a < 0)
1448                 v += '1 0 -1' * (v_z - v_x); // swap x, z
1449         return v;
1450 }
1451
1452 vector findperpendicular(vector v)
1453 {
1454         vector p;
1455         p_x = v_z;
1456         p_y = -v_x;
1457         p_z = v_y;
1458         return normalize(cliptoplane(p, v));
1459 }
1460
1461 vector W_CalculateProjectileSpread(vector forward, float spread)
1462 {
1463         float sigma;
1464         vector v1, v2;
1465         float dx, dy, r;
1466         float sstyle;
1467         spread *= g_weaponspreadfactor;
1468         if(spread <= 0)
1469                 return forward;
1470         sstyle = autocvar_g_projectiles_spread_style;
1471         
1472         if(sstyle == 0)
1473         {
1474                 // this is the baseline for the spread value!
1475                 // standard deviation: sqrt(2/5)
1476                 // density function: sqrt(1-r^2)
1477                 return forward + randomvec() * spread;
1478         }
1479         else if(sstyle == 1)
1480         {
1481                 // same thing, basically
1482                 return normalize(forward + cliptoplane(randomvec() * spread, forward));
1483         }
1484         else if(sstyle == 2)
1485         {
1486                 // circle spread... has at sigma=1 a standard deviation of sqrt(1/2)
1487                 sigma = spread * 0.89442719099991587855; // match baseline stddev
1488                 v1 = findperpendicular(forward);
1489                 v2 = cross(forward, v1);
1490                 // random point on unit circle
1491                 dx = random() * 2 * M_PI;
1492                 dy = sin(dx);
1493                 dx = cos(dx);
1494                 // radius in our dist function
1495                 r = random();
1496                 r = sqrt(r);
1497                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1498         }
1499         else if(sstyle == 3) // gauss 3d
1500         {
1501                 sigma = spread * 0.44721359549996; // match baseline stddev
1502                 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1503                 v1 = forward;
1504                 v1_x += gsl_ran_gaussian(sigma);
1505                 v1_y += gsl_ran_gaussian(sigma);
1506                 v1_z += gsl_ran_gaussian(sigma);
1507                 return v1;
1508         }
1509         else if(sstyle == 4) // gauss 2d
1510         {
1511                 sigma = spread * 0.44721359549996; // match baseline stddev
1512                 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1513                 v1_x = gsl_ran_gaussian(sigma);
1514                 v1_y = gsl_ran_gaussian(sigma);
1515                 v1_z = gsl_ran_gaussian(sigma);
1516                 return normalize(forward + cliptoplane(v1, forward));
1517         }
1518         else if(sstyle == 5) // 1-r
1519         {
1520                 sigma = spread * 1.154700538379252; // match baseline stddev
1521                 v1 = findperpendicular(forward);
1522                 v2 = cross(forward, v1);
1523                 // random point on unit circle
1524                 dx = random() * 2 * M_PI;
1525                 dy = sin(dx);
1526                 dx = cos(dx);
1527                 // radius in our dist function
1528                 r = random();
1529                 r = solve_cubic_abcd(-2, 3, 0, -r) * '0 1 0';
1530                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1531         }
1532         else if(sstyle == 6) // 1-r^2
1533         {
1534                 sigma = spread * 1.095445115010332; // match baseline stddev
1535                 v1 = findperpendicular(forward);
1536                 v2 = cross(forward, v1);
1537                 // random point on unit circle
1538                 dx = random() * 2 * M_PI;
1539                 dy = sin(dx);
1540                 dx = cos(dx);
1541                 // radius in our dist function
1542                 r = random();
1543                 r = sqrt(1 - r);
1544                 r = sqrt(1 - r);
1545                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1546         }
1547         else if(sstyle == 7) // (1-r) (2-r)
1548         {
1549                 sigma = spread * 1.224744871391589; // match baseline stddev
1550                 v1 = findperpendicular(forward);
1551                 v2 = cross(forward, v1);
1552                 // random point on unit circle
1553                 dx = random() * 2 * M_PI;
1554                 dy = sin(dx);
1555                 dx = cos(dx);
1556                 // radius in our dist function
1557                 r = random();
1558                 r = 1 - sqrt(r);
1559                 r = 1 - sqrt(r);
1560                 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1561         }
1562         else
1563                 error("g_projectiles_spread_style must be 0 (sphere), 1 (flattened sphere), 2 (circle), 3 (gauss 3D), 4 (gauss plane), 5 (linear falloff), 6 (quadratic falloff), 7 (stronger falloff)!");
1564         return '0 0 0';
1565         /*
1566          * how to derive falloff functions:
1567          * rho(r) := (2-r) * (1-r);
1568          * a : 0;
1569          * b : 1;
1570          * rhor(r) := r * rho(r);
1571          * cr(t) := integrate(rhor(r), r, a, t);
1572          * scr(t) := integrate(rhor(r) * r^2, r, a, t);
1573          * variance : scr(b) / cr(b);
1574          * solve(cr(r) = rand * cr(b), r), programmmode:false;
1575          * sqrt(0.4 / variance), numer;
1576          */
1577 }
1578
1579 #if 0
1580 float mspercallsum;
1581 float mspercallsstyle;
1582 float mspercallcount;
1583 #endif
1584 void W_SetupProjectileVelocityEx(entity missile, vector dir, vector upDir, float pSpeed, float pUpSpeed, float pZSpeed, float spread, float forceAbsolute)
1585 {
1586         if(missile.owner == world)
1587                 error("Unowned missile");
1588
1589         dir = dir + upDir * (pUpSpeed / pSpeed);
1590         dir_z += pZSpeed / pSpeed;
1591         pSpeed *= vlen(dir);
1592         dir = normalize(dir);
1593
1594 #if 0
1595         if(autocvar_g_projectiles_spread_style != mspercallsstyle)
1596         {
1597                 mspercallsum = mspercallcount = 0;
1598                 mspercallsstyle = autocvar_g_projectiles_spread_style;
1599         }
1600         mspercallsum -= gettime(GETTIME_HIRES);
1601 #endif
1602         dir = W_CalculateProjectileSpread(dir, spread);
1603 #if 0
1604         mspercallsum += gettime(GETTIME_HIRES);
1605         mspercallcount += 1;
1606         print("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
1607 #endif
1608
1609         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, pSpeed * dir, forceAbsolute);
1610 }
1611
1612 void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
1613 {
1614         W_SetupProjectileVelocityEx(missile, w_shotdir, v_up, pSpeed, 0, 0, spread, FALSE);
1615 }
1616
1617 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"), FALSE)
1618 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"), FALSE)
1619
1620 void W_DecreaseAmmo(.float ammo_type, float ammo_use, float ammo_reload)
1621 {
1622         if((self.items & IT_UNLIMITED_WEAPON_AMMO) && !ammo_reload)
1623                 return;
1624
1625         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
1626         if(ammo_reload)
1627         {
1628                 self.clip_load -= ammo_use;
1629                 self.weapon_load[self.weapon] = self.clip_load;
1630         }
1631         else
1632                 self.(self.current_ammo) -= ammo_use;
1633 }
1634
1635 // weapon reloading code
1636
1637 .float reload_ammo_amount, reload_ammo_min, reload_time;
1638 .float reload_complain;
1639 .string reload_sound;
1640
1641 void W_ReloadedAndReady()
1642 {
1643         // finish the reloading process, and do the ammo transfer
1644
1645         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
1646
1647         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
1648         if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO)
1649                 self.clip_load = self.reload_ammo_amount;
1650         else
1651         {
1652                 while(self.clip_load < self.reload_ammo_amount && self.(self.current_ammo)) // make sure we don't add more ammo than we have
1653                 {
1654                         self.clip_load += 1;
1655                         self.(self.current_ammo) -= 1;
1656                 }
1657         }
1658         self.weapon_load[self.weapon] = self.clip_load;
1659
1660         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1661         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1662         // so your weapon is disabled for a few seconds without reason
1663
1664         //ATTACK_FINISHED(self) -= self.reload_time - 1;
1665
1666         w_ready();
1667 }
1668
1669 void W_Reload(float sent_ammo_min, float sent_ammo_amount, float sent_time, string sent_sound)
1670 {
1671         // set global values to work with
1672
1673         self.reload_ammo_min = sent_ammo_min;
1674         self.reload_ammo_amount = sent_ammo_amount;
1675         self.reload_time = sent_time;
1676         self.reload_sound = sent_sound;
1677
1678         // check if we meet the necessary conditions to reload
1679
1680         entity e;
1681         e = get_weaponinfo(self.weapon);
1682
1683         // don't reload weapons that don't have the RELOADABLE flag
1684         if not(e.spawnflags & WEP_FLAG_RELOADABLE)
1685         {
1686                 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
1687                 return;
1688         }
1689
1690         // return if reloading is disabled for this weapon
1691         if(!self.reload_ammo_amount)
1692                 return;
1693
1694         // our weapon is fully loaded, no need to reload
1695         if (self.clip_load >= self.reload_ammo_amount)
1696                 return;
1697
1698         // no ammo, so nothing to load
1699         if(!self.(self.current_ammo) && self.reload_ammo_min)
1700         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1701         {
1702                 if(clienttype(self) == CLIENTTYPE_REAL && self.reload_complain < time)
1703                 {
1704                         play2(self, "weapons/unavailable.wav");
1705                         sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
1706                         self.reload_complain = time + 1;
1707                 }
1708                 // switch away if the amount of ammo is not enough to keep using this weapon
1709                 if not(weapon_action(self.weapon, WR_CHECKAMMO1) + weapon_action(self.weapon, WR_CHECKAMMO2))
1710                 {
1711                         self.clip_load = -1; // reload later
1712                         W_SwitchToOtherWeapon(self);
1713                 }
1714                 return;
1715         }
1716
1717         if (self.weaponentity)
1718         {
1719                 if (self.weaponentity.wframe == WFRAME_RELOAD)
1720                         return;
1721
1722                 // allow switching away while reloading, but this will cause a new reload!
1723                 self.weaponentity.state = WS_READY;
1724         }
1725
1726         // now begin the reloading process
1727
1728         sound (self, CHAN_WEAPON2, self.reload_sound, VOL_BASE, ATTN_NORM);
1729
1730         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1731         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1732         // so your weapon is disabled for a few seconds without reason
1733
1734         //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
1735
1736         weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
1737
1738         if(self.clip_load < 0)
1739                 self.clip_load = 0;
1740         self.old_clip_load = self.clip_load;
1741         self.clip_load = self.weapon_load[self.weapon] = -1;
1742 }