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