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