2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 .float weapon_frametime;
12 float W_WeaponRateFactor()
15 t = 1.0 / g_weaponratefactor;
19 if(self.runes & RUNE_SPEED)
21 if(self.runes & CURSE_SLOW)
22 t = t * autocvar_g_balance_rune_speed_combo_atkrate;
24 t = t * autocvar_g_balance_rune_speed_atkrate;
26 else if(self.runes & CURSE_SLOW)
28 t = t * autocvar_g_balance_curse_slow_atkrate;
35 void W_SwitchWeapon_Force(entity e, float w)
37 // don't allow switching to another weapon if we're loading rockets into the hagar
38 if(e.weapon == WEP_HAGAR && e.hagar_load)
39 if(e.weapons & WEPBIT_HAGAR) // only if we still have the hagar, prevents a bug with dropping
42 e.cnt = e.switchweapon;
49 // VorteX: static frame globals
50 float WFRAME_DONTCHANGE = -1;
51 float WFRAME_FIRE1 = 0;
52 float WFRAME_FIRE2 = 1;
53 float WFRAME_IDLE = 2;
54 float WFRAME_RELOAD = 3;
57 void(float fr, float t, void() func) weapon_thinkf;
59 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
62 ret_x = screenright * v;
64 ret_z = screenforward * v;
68 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
71 vector mi, ma, thisv, myv, ret;
73 myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
75 // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
77 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
80 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
81 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
82 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
83 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
86 if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
87 if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
88 //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
97 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
98 ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
99 ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
100 ret_z = thisv_z - myv_z;
104 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
110 if(player.hitplotfh >= 0)
112 lag = ANTILAG_LATENCY(player);
115 if(clienttype(player) != CLIENTTYPE_REAL)
116 lag = 0; // only antilag for clients
118 org = player.origin + player.view_ofs;
119 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
120 if(trace_ent.flags & FL_CLIENT)
122 antilag_takeback(trace_ent, time - lag);
123 hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
124 antilag_restore(trace_ent);
125 fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
126 //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
135 // this function calculates w_shotorg and w_shotdir based on the weapon model
136 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
137 // make sure you call makevectors first (FIXME?)
138 .float prevstrengthsound;
139 .float prevstrengthsoundattempt;
140 void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float chan, float maxdamage, float range)
142 float nudge = 1; // added to traceline target and subtracted from result
143 local float oldsolid;
145 oldsolid = ent.dphitcontentsmask;
146 if(ent.weapon == WEP_SNIPERRIFLE)
147 ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
149 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
151 WarpZone_traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
152 // passing world, because we do NOT want it to touch dphitcontentsmask
154 WarpZone_TraceLine(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NOMONSTERS, ent);
155 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
161 w_shotend = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
166 // un-adjust trueaim if shotend is too close
167 if(vlen(w_shotend - (ent.origin + ent.view_ofs)) < autocvar_g_trueaim_minrange)
168 w_shotend = ent.origin + ent.view_ofs + s_forward * autocvar_g_trueaim_minrange;
171 if(accuracy_canbegooddamage(ent))
172 accuracy_add(ent, ent.weapon, maxdamage, 0);
174 W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
176 if(ent.weaponentity.movedir_x > 0)
177 vecs = ent.weaponentity.movedir;
180 if(debug_shotorg != '0 0 0')
181 vecs = debug_shotorg;
183 dv = v_right * -vecs_y + v_up * vecs_z;
184 w_shotorg = ent.origin + ent.view_ofs + dv;
186 // now move the shotorg forward as much as requested if possible
189 if(ent.antilag_debug)
190 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
192 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
195 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
196 w_shotorg = trace_endpos - v_forward * nudge;
197 // calculate the shotdir from the chosen shotorg
198 w_shotdir = normalize(w_shotend - w_shotorg);
201 if (!ent.cvar_cl_noantilag)
203 if (autocvar_g_antilag == 1) // switch to "ghost" if not hitting original
205 traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
206 if (!trace_ent.takedamage)
208 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
209 if (trace_ent.takedamage && trace_ent.classname == "player")
213 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
215 w_shotdir = normalize(trace_ent.origin - w_shotorg);
219 else if(autocvar_g_antilag == 3) // client side hitscan
221 // this part MUST use prydon cursor
222 if (ent.cursor_trace_ent) // client was aiming at someone
223 if (ent.cursor_trace_ent != ent) // just to make sure
224 if (ent.cursor_trace_ent.takedamage) // and that person is killable
225 if (ent.cursor_trace_ent.classname == "player") // and actually a player
227 // verify that the shot would miss without antilag
228 // (avoids an issue where guns would always shoot at their origin)
229 traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
230 if (!trace_ent.takedamage)
232 // verify that the shot would hit if altered
233 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
234 if (trace_ent == ent.cursor_trace_ent)
235 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
237 print("antilag fail\n");
243 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
246 ent.punchangle_x = recoil * -1;
249 sound (ent, chan, snd, VOL_BASE, ATTN_NORM);
251 if(ent.items & IT_STRENGTH)
254 (time > ent.prevstrengthsound + autocvar_sv_strengthsound_antispam_time)
256 (time > ent.prevstrengthsoundattempt + autocvar_sv_strengthsound_antispam_refire_threshold)
257 ) // prevent insane sound spam
259 sound(ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
260 ent.prevstrengthsound = time;
262 ent.prevstrengthsoundattempt = time;
264 // nudge w_shotend so a trace to w_shotend hits
265 w_shotend = w_shotend + normalize(w_shotend - w_shotorg) * nudge;
268 #define W_SetupShot_Dir_ProjectileSize(ent,s_forward,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize_Range(ent, s_forward, mi, ma, antilag, recoil, snd, chan, maxdamage, MAX_SHOT_DISTANCE)
269 #define W_SetupShot_ProjectileSize(ent,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, v_forward, mi, ma, antilag, recoil, snd, chan, maxdamage)
270 #define W_SetupShot_Dir(ent,s_forward,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, s_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
271 #define W_SetupShot(ent,antilag,recoil,snd,chan,maxdamage) W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
272 #define W_SetupShot_Range(ent,antilag,recoil,snd,chan,maxdamage,range) W_SetupShot_Dir_ProjectileSize_Range(ent, v_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage, range)
274 float CL_Weaponentity_CustomizeEntityForClient()
276 self.viewmodelforclient = self.owner;
277 if(other.classname == "spectator")
278 if(other.enemy == self.owner)
279 self.viewmodelforclient = other;
283 float qcweaponanimation;
284 vector weapon_offset = '0 -10 0';
285 vector weapon_adjust = '10 0 -15';
286 .vector weapon_morph0origin;
287 .vector weapon_morph0angles;
288 .float weapon_morph0time;
289 .vector weapon_morph1origin;
290 .vector weapon_morph1angles;
291 .float weapon_morph1time;
292 .vector weapon_morph2origin;
293 .vector weapon_morph2angles;
294 .float weapon_morph2time;
295 .vector weapon_morph3origin;
296 .vector weapon_morph3angles;
297 .float weapon_morph3time;
298 .vector weapon_morph4origin;
299 .vector weapon_morph4angles;
300 .float weapon_morph4time;
302 #define QCWEAPONANIMATION_ORIGIN(e) ((weapon_offset_x + e.view_ofs_x) * v_forward - (weapon_offset_y + e.view_ofs_y) * v_right + (weapon_offset_z + e.view_ofs_z) * v_up + weapon_adjust)
307 * 1. simple animated model, muzzle flash handling on h_ model:
308 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
310 * shot = muzzle end (shot origin, also used for muzzle flashes)
311 * shell = casings ejection point (must be on the right hand side of the gun)
312 * weapon = attachment for v_tuba.md3
313 * v_tuba.md3 - first and third person model
314 * g_tuba.md3 - pickup model
316 * 2. simple animated model, muzzle flash handling on v_ model:
317 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
319 * weapon = attachment for v_tuba.md3
320 * v_tuba.md3 - first and third person model
322 * shot = muzzle end (shot origin, also used for muzzle flashes)
323 * shell = casings ejection point (must be on the right hand side of the gun)
324 * g_tuba.md3 - pickup model
326 * 3. fully animated model, muzzle flash handling on h_ model:
327 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
329 * shot = muzzle end (shot origin, also used for muzzle flashes)
330 * shell = casings ejection point (must be on the right hand side of the gun)
331 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
332 * v_tuba.md3 - third person model
333 * g_tuba.md3 - pickup model
335 * 4. fully animated model, muzzle flash handling on v_ model:
336 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
338 * shot = muzzle end (shot origin)
339 * shell = casings ejection point (must be on the right hand side of the gun)
340 * v_tuba.md3 - third person model
342 * shot = muzzle end (for muzzle flashes)
343 * g_tuba.md3 - pickup model
347 // self.origin, self.angles
349 // self.movedir, self.view_ofs
353 // call again with ""
355 void CL_WeaponEntity_SetModel(string name)
362 // if there is a child entity, hide it until we're sure we use it
363 if (self.weaponentity)
364 self.weaponentity.model = "";
365 setmodel(self, strcat("models/weapons/v_", name, ".md3")); // precision set below
366 v_shot_idx = gettagindex(self, "shot"); // used later
368 v_shot_idx = gettagindex(self, "tag_shot");
370 if(qcweaponanimation)
372 self.angles = '0 0 0';
373 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
374 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
375 self.movedir_x += 32;
376 self.spawnorigin = self.movedir;
377 // oldorigin - not calculated here
381 setmodel(self, strcat("models/weapons/h_", name, ".iqm")); // precision set below
382 animfilename = strcat("models/weapons/h_", name, ".iqm.animinfo");
383 animfile = fopen(animfilename, FILE_READ);
384 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
385 self.anim_fire1 = '0 1 0.01';
386 self.anim_fire2 = '1 1 0.01';
387 self.anim_idle = '2 1 0.01';
388 self.anim_reload = '3 1 0.01';
391 animparseerror = FALSE;
392 self.anim_fire1 = animparseline(animfile);
393 self.anim_fire2 = animparseline(animfile);
394 self.anim_idle = animparseline(animfile);
395 self.anim_reload = animparseline(animfile);
398 print("Parse error in ", animfilename, ", some player animations are broken\n");
401 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
402 // if we don't, this is a "real" animated model
403 if(gettagindex(self, "weapon"))
405 if (!self.weaponentity)
406 self.weaponentity = spawn();
407 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
408 setattachment(self.weaponentity, self, "weapon");
410 else if(gettagindex(self, "tag_weapon"))
412 if (!self.weaponentity)
413 self.weaponentity = spawn();
414 setmodel(self.weaponentity, strcat("models/weapons/v_", name, ".md3")); // precision does not matter
415 setattachment(self.weaponentity, self, "tag_weapon");
419 if(self.weaponentity)
420 remove(self.weaponentity);
421 self.weaponentity = world;
424 setorigin(self,'0 0 0');
425 self.angles = '0 0 0';
427 self.viewmodelforclient = world;
431 if(v_shot_idx) // v_ model attached to invisible h_ model
433 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
437 idx = gettagindex(self, "shot");
439 idx = gettagindex(self, "tag_shot");
441 self.movedir = gettaginfo(self, idx);
444 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
445 self.movedir = '0 0 0';
449 if(self.weaponentity) // v_ model attached to invisible h_ model
451 idx = gettagindex(self.weaponentity, "shell");
453 idx = gettagindex(self.weaponentity, "tag_shell");
455 self.spawnorigin = gettaginfo(self.weaponentity, idx);
461 idx = gettagindex(self, "shell");
463 idx = gettagindex(self, "tag_shell");
465 self.spawnorigin = gettaginfo(self, idx);
468 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
469 self.spawnorigin = self.movedir;
475 self.oldorigin = '0 0 0'; // use regular attachment
479 if(self.weaponentity)
481 idx = gettagindex(self, "weapon");
483 idx = gettagindex(self, "tag_weapon");
487 idx = gettagindex(self, "handle");
489 idx = gettagindex(self, "tag_handle");
493 self.oldorigin = self.movedir - gettaginfo(self, idx);
497 print("WARNING: weapon model ", self.model, " does not support the 'handle' tag and neither does the v_ model support the 'shot' tag, will display muzzle flashes TOTALLY wrong\n");
498 self.oldorigin = '0 0 0'; // there is no way to recover from this
502 self.viewmodelforclient = self.owner;
508 if(self.weaponentity)
509 remove(self.weaponentity);
510 self.weaponentity = world;
511 self.movedir = '0 0 0';
512 self.spawnorigin = '0 0 0';
513 self.oldorigin = '0 0 0';
514 self.anim_fire1 = '0 1 0.01';
515 self.anim_fire2 = '0 1 0.01';
516 self.anim_idle = '0 1 0.01';
517 self.anim_reload = '0 1 0.01';
520 self.view_ofs = '0 0 0';
522 if(self.movedir_x >= 0)
526 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
527 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
529 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
530 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
532 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
534 // check if an instant weapon switch occurred
535 if (qcweaponanimation)
537 if (self.state == WS_READY)
539 self.angles = '0 0 0';
540 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
541 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
545 setorigin(self, self.view_ofs);
546 // reset animstate now
547 self.wframe = WFRAME_IDLE;
548 self.weapon_morph0time = 0;
549 self.weapon_morph1time = 0;
550 self.weapon_morph2time = 0;
551 self.weapon_morph3time = 0;
552 self.weapon_morph4time = 0;
553 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
556 vector CL_Weapon_GetShotOrg(float wpn)
560 wi = get_weaponinfo(wpn);
563 CL_WeaponEntity_SetModel(wi.mdl);
565 CL_WeaponEntity_SetModel("");
571 void CL_Weaponentity_Think()
574 self.nextthink = time;
575 if (intermission_running)
576 self.frame = self.anim_idle_x;
577 if (self.owner.weaponentity != self)
579 if (self.weaponentity)
580 remove(self.weaponentity);
584 if (self.owner.deadflag != DEAD_NO)
587 if (self.weaponentity)
588 self.weaponentity.model = "";
591 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
593 self.cnt = self.owner.weapon;
594 self.dmg = self.owner.modelindex;
595 self.deadflag = self.owner.deadflag;
597 CL_WeaponEntity_SetModel(self.owner.weaponname);
600 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
601 self.effects = self.owner.effects & EFMASK_CHEAP;
602 self.effects &~= EF_LOWPRECISION;
603 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
604 self.effects &~= EF_TELEPORT_BIT;
605 self.effects &~= EF_RESTARTANIM_BIT;
608 if(self.owner.alpha == default_player_alpha)
609 self.alpha = default_weapon_alpha;
610 else if(self.owner.alpha != 0)
611 self.alpha = self.owner.alpha;
615 self.glowmod = self.owner.weaponentity_glowmod;
616 self.colormap = self.owner.colormap;
617 if (self.weaponentity)
619 self.weaponentity.effects = self.effects;
620 self.weaponentity.alpha = self.alpha;
621 self.weaponentity.colormap = self.colormap;
622 self.weaponentity.glowmod = self.glowmod;
625 self.angles = '0 0 0';
628 if (self.state == WS_RAISE && !intermission_running)
630 f = (self.owner.weapon_nextthink - time) * g_weaponratefactor / autocvar_g_balance_weaponswitchdelay;
631 self.angles_x = -90 * f * f;
632 if (qcweaponanimation)
634 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
635 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
638 else if (self.state == WS_DROP && !intermission_running)
640 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponratefactor / autocvar_g_balance_weaponswitchdelay;
641 self.angles_x = -90 * f * f;
642 if (qcweaponanimation)
644 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
645 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
648 else if (self.state == WS_CLEAR)
651 self.angles_x = -90 * f * f;
652 if (qcweaponanimation)
654 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
655 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
658 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
660 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
661 f = 1 - pow(1 - f, 3);
662 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
663 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
665 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
667 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
668 f = 1 - pow(1 - f, 3);
669 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
670 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
672 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
674 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
675 f = 1 - pow(1 - f, 3);
676 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
677 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
679 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
681 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
682 f = 1 - pow(1 - f, 3);
683 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
684 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
686 else if (qcweaponanimation)
688 // begin a new idle morph
689 self.owner.weapon_morph0time = time;
690 self.owner.weapon_morph0angles = self.angles;
691 self.owner.weapon_morph0origin = self.origin;
699 // turn gun to the left to look at it
701 self.owner.weapon_morph1time = time + t * 0.2;
702 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
703 makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
704 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
706 self.owner.weapon_morph2time = time + t * 0.6;
707 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
708 makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
709 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
711 self.owner.weapon_morph3time = time + t;
712 self.owner.weapon_morph3angles = '0 0 0';
713 makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
714 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
718 // raise the gun a bit
720 self.owner.weapon_morph1time = time + t * 0.2;
721 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
722 makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
723 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
725 self.owner.weapon_morph2time = time + t * 0.5;
726 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
727 makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
728 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
730 self.owner.weapon_morph3time = time + t;
731 self.owner.weapon_morph3angles = '0 0 0';
732 makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
733 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
739 self.owner.weapon_morph1time = time + t * 0.3;
740 self.owner.weapon_morph1angles = randomvec() * 6;
741 makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
742 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
744 self.owner.weapon_morph2time = time + t * 0.7;
745 self.owner.weapon_morph2angles = randomvec() * 6;
746 makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
747 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
749 self.owner.weapon_morph3time = time + t;
750 self.owner.weapon_morph3angles = '0 0 0';
751 makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
752 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
756 // hold it mostly steady
757 t = random() * 6 + 4;
758 self.owner.weapon_morph1time = time + t * 0.2;
759 self.owner.weapon_morph1angles = randomvec() * 1;
760 makevectors(self.owner.weapon_morph1angles_x * '-1 0 0' + self.owner.weapon_morph1angles_y * '0 1 0' + self.owner.weapon_morph1angles_z * '0 0 1');
761 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
763 self.owner.weapon_morph2time = time + t * 0.5;
764 self.owner.weapon_morph2angles = randomvec() * 1;
765 makevectors(self.owner.weapon_morph2angles_x * '-1 0 0' + self.owner.weapon_morph2angles_y * '0 1 0' + self.owner.weapon_morph2angles_z * '0 0 1');
766 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
768 self.owner.weapon_morph3time = time + t * 0.7;
769 self.owner.weapon_morph3angles = randomvec() * 1;
770 makevectors(self.owner.weapon_morph3angles_x * '-1 0 0' + self.owner.weapon_morph3angles_y * '0 1 0' + self.owner.weapon_morph3angles_z * '0 0 1');
771 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
774 self.owner.weapon_morph4time = time + t;
775 self.owner.weapon_morph4angles = '0 0 0';
776 makevectors(self.owner.weapon_morph4angles_x * '-1 0 0' + self.owner.weapon_morph4angles_y * '0 1 0' + self.owner.weapon_morph4angles_z * '0 0 1');
777 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
782 void CL_ExteriorWeaponentity_Think()
786 self.nextthink = time;
787 if (self.owner.exteriorweaponentity != self)
792 if (self.owner.deadflag != DEAD_NO)
797 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
799 self.cnt = self.owner.weapon;
800 self.dmg = self.owner.modelindex;
801 self.deadflag = self.owner.deadflag;
802 if (self.owner.weaponname != "")
803 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
807 if((tag_found = gettagindex(self.owner, "tag_weapon")))
809 self.tag_index = tag_found;
810 self.tag_entity = self.owner;
813 setattachment(self, self.owner, "bip01 r hand");
815 // if that didn't find a tag, hide the exterior weapon model
819 self.effects = self.owner.effects;
820 if(sv_pitch_min == sv_pitch_max)
821 self.effects |= EF_LOWPRECISION;
823 self.effects &~= EF_LOWPRECISION;
824 self.effects = self.effects & EFMASK_CHEAP; // eat performance
825 if(self.owner.alpha == default_player_alpha)
826 self.alpha = default_weapon_alpha;
827 else if(self.owner.alpha != 0)
828 self.alpha = self.owner.alpha;
832 ang_x = bound(sv_pitch_min, self.owner.v_angle_x, sv_pitch_max);
836 if(sv_pitch_fixyaw) // workaround for stupid player models that don't aim forward
838 ang_y = self.owner.v_angle_y;
840 var vector v = v_forward;
841 var float t = self.tag_entity.frame1time;
842 var float f = self.tag_entity.frame;
843 self.tag_entity.frame1time = time;
844 self.tag_entity.frame = self.tag_entity.anim_idle_x;
845 gettaginfo(self.tag_entity, self.tag_index);
846 self.tag_entity.frame1time = t;
847 self.tag_entity.frame = f;
848 // untransform v according to this coordinate space
853 self.angles = vectoangles(w);
857 ang_x = -/* don't ask */ang_x;
861 self.glowmod = self.owner.weaponentity_glowmod;
862 self.colormap = self.owner.colormap;
865 // spawning weaponentity for client
866 void CL_SpawnWeaponentity()
868 self.weaponentity = spawn();
869 self.weaponentity.classname = "weaponentity";
870 self.weaponentity.solid = SOLID_NOT;
871 self.weaponentity.owner = self;
872 setmodel(self.weaponentity, ""); // precision set when changed
873 setorigin(self.weaponentity, '0 0 0');
874 self.weaponentity.angles = '0 0 0';
875 self.weaponentity.viewmodelforclient = self;
876 self.weaponentity.flags = 0;
877 self.weaponentity.think = CL_Weaponentity_Think;
878 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
879 self.weaponentity.nextthink = time;
881 self.exteriorweaponentity = spawn();
882 self.exteriorweaponentity.classname = "exteriorweaponentity";
883 self.exteriorweaponentity.solid = SOLID_NOT;
884 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
885 self.exteriorweaponentity.owner = self;
886 setorigin(self.exteriorweaponentity, '0 0 0');
887 self.exteriorweaponentity.angles = '0 0 0';
888 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
889 self.exteriorweaponentity.nextthink = time;
892 void Send_WeaponComplain (entity e, float wpn, string wpnname, float type)
895 WriteByte(MSG_ONE, SVC_TEMPENTITY);
896 WriteByte(MSG_ONE, TE_CSQC_WEAPONCOMPLAIN);
897 WriteByte(MSG_ONE, wpn);
898 WriteString(MSG_ONE, wpnname);
899 WriteByte(MSG_ONE, type);
902 .float hasweapon_complain_spam;
904 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
906 local float weaponbit, f;
907 local entity oldself;
909 if(time < self.hasweapon_complain_spam)
912 self.hasweapon_complain_spam = time + 0.2;
914 if (wpn < WEP_FIRST || wpn > WEP_LAST)
917 sprint(self, "Invalid weapon\n");
920 weaponbit = W_WeaponBit(wpn);
921 if (cl.weapons & weaponbit)
925 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
933 f = weapon_action(wpn, WR_CHECKAMMO1);
934 f = f + weapon_action(wpn, WR_CHECKAMMO2);
936 // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
938 if(wpn == WEP_MINE_LAYER)
939 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
947 if(clienttype(cl) == CLIENTTYPE_REAL)
949 play2(cl, "weapons/unavailable.wav");
950 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
951 Send_WeaponComplain (cl, wpn, W_Name(wpn), 0);
961 // Report Proper Weapon Status / Modified Weapon Ownership Message
962 if(weaponsInMap & weaponbit)
964 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
965 Send_WeaponComplain (cl, wpn, W_Name(wpn), 1);
967 if(autocvar_g_showweaponspawns)
972 e = get_weaponinfo(wpn);
975 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
977 if(e.classname == "droppedweapon")
979 if not(e.flags & FL_ITEM)
981 WaypointSprite_Spawn(
994 Send_WeaponComplain (cl, wpn, W_Name(wpn), 2);
995 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
998 play2(cl, "weapons/unavailable.wav");
1006 if (self.weapon != -1)
1008 if (self.weaponentity)
1010 self.weaponentity.state = WS_CLEAR;
1011 self.weaponentity.effects = 0;
1017 if (self.weaponentity)
1018 self.weaponentity.state = WS_READY;
1019 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
1022 // Setup weapon for client (after this raise frame will be launched)
1023 void weapon_setup(float windex)
1026 qcweaponanimation = autocvar_sv_qcweaponanimation;
1027 e = get_weaponinfo(windex);
1028 self.items &~= IT_AMMO;
1029 self.items = self.items | e.items;
1031 // the two weapon entities will notice this has changed and update their models
1032 self.weapon = windex;
1033 self.weaponname = e.mdl;
1034 self.bulletcounter = 0;
1037 // perform weapon to attack (weaponstate and attack_finished check is here)
1038 void W_SwitchToOtherWeapon(entity pl)
1040 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
1042 w = W_WeaponBit(pl.weapon);
1044 ww = w_getbestweapon(pl);
1047 W_SwitchWeapon_Force(pl, ww);
1050 float weapon_prepareattack_checkammo(float secondary)
1052 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1053 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
1055 // always keep the Mine Layer if we placed mines, so that we can detonate them
1057 if(self.weapon == WEP_MINE_LAYER)
1058 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
1061 if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons
1063 sound (self, CHAN_AUTO, "weapons/dryfire.wav", VOL_BASE, ATTN_NORM);
1064 self.prevdryfire = time;
1067 W_SwitchToOtherWeapon(self);
1072 .float race_penalty;
1073 float weapon_prepareattack_check(float secondary, float attacktime)
1075 if(!weapon_prepareattack_checkammo(secondary))
1078 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
1079 //if all players readied up and the countdown is running
1080 if(time < game_starttime || time < self.race_penalty) {
1084 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
1087 // do not even think about shooting if switching
1088 if(self.switchweapon != self.weapon)
1093 // don't fire if previous attack is not finished
1094 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
1096 // don't fire while changing weapon
1097 if (self.weaponentity.state != WS_READY)
1103 float weapon_prepareattack_do(float secondary, float attacktime)
1105 self.weaponentity.state = WS_INUSE;
1107 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1109 // if the weapon hasn't been firing continuously, reset the timer
1112 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
1114 ATTACK_FINISHED(self) = time;
1115 //dprint("resetting attack finished to ", ftos(time), "\n");
1117 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
1119 self.bulletcounter += 1;
1120 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1123 float weapon_prepareattack(float secondary, float attacktime)
1125 if(weapon_prepareattack_check(secondary, attacktime))
1127 weapon_prepareattack_do(secondary, attacktime);
1134 void weapon_thinkf(float fr, float t, void() func)
1140 if(fr == WFRAME_DONTCHANGE)
1142 fr = self.weaponentity.wframe;
1143 restartanim = FALSE;
1145 else if (fr == WFRAME_IDLE)
1146 restartanim = FALSE;
1154 if (self.weaponentity)
1156 self.weaponentity.wframe = fr;
1157 if (qcweaponanimation)
1159 if (fr != WFRAME_IDLE)
1161 self.weapon_morph0time = time;
1162 self.weapon_morph0angles = self.weaponentity.angles;
1163 self.weapon_morph0origin = self.weaponentity.origin;
1165 self.weapon_morph1angles = '0 0 0';
1166 self.weapon_morph1time = time + t;
1167 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1168 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1170 self.weapon_morph2angles = '0 0 0';
1171 self.weapon_morph2time = time + t;
1172 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1173 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1175 self.weapon_morph3angles = '0 0 0';
1176 self.weapon_morph3time = time + t;
1177 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1178 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1180 self.weapon_morph4angles = '0 0 0';
1181 self.weapon_morph4time = time + t;
1182 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1183 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1185 if (fr == WFRAME_FIRE1)
1187 self.weapon_morph1angles = '5 0 0';
1188 self.weapon_morph1time = time + t * 0.1;
1189 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1190 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1191 self.weapon_morph4time = time + t + 1; // delay idle effect
1193 else if (fr == WFRAME_FIRE2)
1195 self.weapon_morph1angles = '10 0 0';
1196 self.weapon_morph1time = time + t * 0.1;
1197 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1198 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1199 self.weapon_morph4time = time + t + 1; // delay idle effect
1201 else if (fr == WFRAME_RELOAD)
1203 self.weapon_morph1time = time + t * 0.05;
1204 self.weapon_morph1angles = '-10 40 0';
1205 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1206 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1208 self.weapon_morph2time = time + t * 0.15;
1209 self.weapon_morph2angles = '-10 40 5';
1210 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1211 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1213 self.weapon_morph3time = time + t * 0.25;
1214 self.weapon_morph3angles = '-10 40 0';
1215 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1216 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1222 if (fr == WFRAME_IDLE)
1223 a = self.weaponentity.anim_idle;
1224 else if (fr == WFRAME_FIRE1)
1225 a = self.weaponentity.anim_fire1;
1226 else if (fr == WFRAME_FIRE2)
1227 a = self.weaponentity.anim_fire2;
1228 else if (fr == WFRAME_RELOAD)
1229 a = self.weaponentity.anim_reload;
1230 a_z *= g_weaponratefactor;
1231 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1239 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1241 backtrace("Tried to override initial weapon think function - should this really happen?");
1244 t *= W_WeaponRateFactor();
1246 // VorteX: haste can be added here
1247 if (self.weapon_think == w_ready)
1249 self.weapon_nextthink = time;
1250 //dprint("started firing at ", ftos(time), "\n");
1252 if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
1254 self.weapon_nextthink = time;
1255 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1257 self.weapon_nextthink = self.weapon_nextthink + t;
1258 self.weapon_think = func;
1259 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1263 if (!self.crouch) // shoot anim stands up, this looks bad
1266 anim = self.anim_shoot;
1267 anim_z = anim_y / (t + sys_frametime);
1268 setanim(self, anim, FALSE, TRUE, TRUE);
1272 void weapon_boblayer1(float spd, vector org)
1274 // VorteX: haste can be added here
1277 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity, float forceAbsolute)
1285 mvelocity = mvelocity * g_weaponspeedfactor;
1287 mdirection = normalize(mvelocity);
1288 mspeed = vlen(mvelocity);
1290 nstyle = autocvar_g_projectiles_newton_style;
1291 if(nstyle == 0 || forceAbsolute)
1293 // absolute velocity
1294 outvelocity = mvelocity;
1296 else if(nstyle == 1)
1298 // true Newtonian projectiles
1299 outvelocity = pvelocity + mvelocity;
1301 else if(nstyle == 2)
1303 // true Newtonian projectiles with automatic aim adjustment
1305 // solve: |outspeed * mdirection - pvelocity| = mspeed
1306 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1307 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1311 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1312 // velocity without mdirection component > mspeed
1313 // fire at smallest possible mspeed that works?
1314 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1317 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1319 outspeed = solution_y; // the larger one
1322 //outspeed = 0; // slowest possible shot
1323 outspeed = solution_x; // the real part (that is, the average!)
1324 //dprint("impossible shot, adjusting\n");
1327 outspeed = bound(mspeed * autocvar_g_projectiles_newton_style_2_minfactor, outspeed, mspeed * autocvar_g_projectiles_newton_style_2_maxfactor);
1328 outvelocity = mdirection * outspeed;
1330 else if(nstyle == 3)
1332 // pseudo-Newtonian:
1333 outspeed = mspeed + mdirection * pvelocity;
1334 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1335 outvelocity = mdirection * outspeed;
1337 else if(nstyle == 4)
1340 outspeed = mspeed + vlen(pvelocity);
1341 outvelocity = mdirection * outspeed;
1344 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1349 void W_AttachToShotorg(entity flash, vector offset)
1353 flash.angles_z = random() * 360;
1354 if(qcweaponanimation)
1356 setorigin(flash, w_shotorg + w_shotdir * 50);
1357 flash.angles = vectoangles(w_shotdir);
1358 flash.angles_z = random() * 360;
1362 if(gettagindex(self.weaponentity, "shot"))
1363 setattachment(flash, self.weaponentity, "shot");
1365 setattachment(flash, self.weaponentity, "tag_shot");
1366 setorigin(flash, offset);
1369 copyentity(flash, xflash);
1371 flash.viewmodelforclient = self;
1373 if(self.weaponentity.oldorigin_x > 0)
1375 setattachment(xflash, self.exteriorweaponentity, "");
1376 setorigin(xflash, self.weaponentity.oldorigin + offset);
1380 if(gettagindex(self.exteriorweaponentity, "shot"))
1381 setattachment(xflash, self.exteriorweaponentity, "shot");
1383 setattachment(xflash, self.exteriorweaponentity, "tag_shot");
1384 setorigin(xflash, offset);
1389 vector cliptoplane(vector v, vector p)
1391 return v - (v * p) * p;
1394 vector solve_cubic_pq(float p, float q)
1397 D = q*q/4.0 + p*p*p/27.0;
1401 a = 1.0/3.0 * acos(-q/2.0 * sqrt(-27.0/(p*p*p)));
1402 u = sqrt(-4.0/3.0 * p);
1403 // a in range 0..pi/3
1410 '1 0 0' * cos(a + 2.0/3.0*M_PI)
1412 '0 1 0' * cos(a + 4.0/3.0*M_PI)
1425 return '1 1 0' * v + '0 0 1' * u;
1427 return '0 1 1' * v + '1 0 0' * u;
1432 u = cbrt(-q/2.0 + sqrt(D));
1433 v = cbrt(-q/2.0 - sqrt(D));
1434 return '1 1 1' * (u + v);
1437 vector solve_cubic_abcd(float a, float b, float c, float d)
1443 p = (9*a*c - 3*b*b);
1444 q = (27*a*a*d - 9*a*b*c + 2*b*b*b);
1445 v = solve_cubic_pq(p, q);
1446 v = (v - b * '1 1 1') * (1.0 / (3.0 * a));
1448 v += '1 0 -1' * (v_z - v_x); // swap x, z
1452 vector findperpendicular(vector v)
1458 return normalize(cliptoplane(p, v));
1461 vector W_CalculateProjectileSpread(vector forward, float spread)
1467 spread *= g_weaponspreadfactor;
1470 sstyle = autocvar_g_projectiles_spread_style;
1474 // this is the baseline for the spread value!
1475 // standard deviation: sqrt(2/5)
1476 // density function: sqrt(1-r^2)
1477 return forward + randomvec() * spread;
1479 else if(sstyle == 1)
1481 // same thing, basically
1482 return normalize(forward + cliptoplane(randomvec() * spread, forward));
1484 else if(sstyle == 2)
1486 // circle spread... has at sigma=1 a standard deviation of sqrt(1/2)
1487 sigma = spread * 0.89442719099991587855; // match baseline stddev
1488 v1 = findperpendicular(forward);
1489 v2 = cross(forward, v1);
1490 // random point on unit circle
1491 dx = random() * 2 * M_PI;
1494 // radius in our dist function
1497 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1499 else if(sstyle == 3) // gauss 3d
1501 sigma = spread * 0.44721359549996; // match baseline stddev
1502 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1504 v1_x += gsl_ran_gaussian(sigma);
1505 v1_y += gsl_ran_gaussian(sigma);
1506 v1_z += gsl_ran_gaussian(sigma);
1509 else if(sstyle == 4) // gauss 2d
1511 sigma = spread * 0.44721359549996; // match baseline stddev
1512 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1513 v1_x = gsl_ran_gaussian(sigma);
1514 v1_y = gsl_ran_gaussian(sigma);
1515 v1_z = gsl_ran_gaussian(sigma);
1516 return normalize(forward + cliptoplane(v1, forward));
1518 else if(sstyle == 5) // 1-r
1520 sigma = spread * 1.154700538379252; // match baseline stddev
1521 v1 = findperpendicular(forward);
1522 v2 = cross(forward, v1);
1523 // random point on unit circle
1524 dx = random() * 2 * M_PI;
1527 // radius in our dist function
1529 r = solve_cubic_abcd(-2, 3, 0, -r) * '0 1 0';
1530 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1532 else if(sstyle == 6) // 1-r^2
1534 sigma = spread * 1.095445115010332; // match baseline stddev
1535 v1 = findperpendicular(forward);
1536 v2 = cross(forward, v1);
1537 // random point on unit circle
1538 dx = random() * 2 * M_PI;
1541 // radius in our dist function
1545 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1547 else if(sstyle == 7) // (1-r) (2-r)
1549 sigma = spread * 1.224744871391589; // match baseline stddev
1550 v1 = findperpendicular(forward);
1551 v2 = cross(forward, v1);
1552 // random point on unit circle
1553 dx = random() * 2 * M_PI;
1556 // radius in our dist function
1560 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1563 error("g_projectiles_spread_style must be 0 (sphere), 1 (flattened sphere), 2 (circle), 3 (gauss 3D), 4 (gauss plane), 5 (linear falloff), 6 (quadratic falloff), 7 (stronger falloff)!");
1566 * how to derive falloff functions:
1567 * rho(r) := (2-r) * (1-r);
1570 * rhor(r) := r * rho(r);
1571 * cr(t) := integrate(rhor(r), r, a, t);
1572 * scr(t) := integrate(rhor(r) * r^2, r, a, t);
1573 * variance : scr(b) / cr(b);
1574 * solve(cr(r) = rand * cr(b), r), programmmode:false;
1575 * sqrt(0.4 / variance), numer;
1581 float mspercallsstyle;
1582 float mspercallcount;
1584 void W_SetupProjectileVelocityEx(entity missile, vector dir, vector upDir, float pSpeed, float pUpSpeed, float pZSpeed, float spread, float forceAbsolute)
1586 if(missile.owner == world)
1587 error("Unowned missile");
1589 dir = dir + upDir * (pUpSpeed / pSpeed);
1590 dir_z += pZSpeed / pSpeed;
1591 pSpeed *= vlen(dir);
1592 dir = normalize(dir);
1595 if(autocvar_g_projectiles_spread_style != mspercallsstyle)
1597 mspercallsum = mspercallcount = 0;
1598 mspercallsstyle = autocvar_g_projectiles_spread_style;
1600 mspercallsum -= gettime(GETTIME_HIRES);
1602 dir = W_CalculateProjectileSpread(dir, spread);
1604 mspercallsum += gettime(GETTIME_HIRES);
1605 mspercallcount += 1;
1606 print("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
1609 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, pSpeed * dir, forceAbsolute);
1612 void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
1614 W_SetupProjectileVelocityEx(missile, w_shotdir, v_up, pSpeed, 0, 0, spread, FALSE);
1617 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"), FALSE)
1618 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"), FALSE)
1620 void W_DecreaseAmmo(.float ammo_type, float ammo_use, float ammo_reload)
1622 if((self.items & IT_UNLIMITED_WEAPON_AMMO) && !ammo_reload)
1625 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
1628 self.clip_load -= ammo_use;
1629 self.weapon_load[self.weapon] = self.clip_load;
1632 self.(self.current_ammo) -= ammo_use;
1635 // weapon reloading code
1637 .float reload_ammo_amount, reload_ammo_min, reload_time;
1638 .float reload_complain;
1639 .string reload_sound;
1641 void W_ReloadedAndReady()
1643 // finish the reloading process, and do the ammo transfer
1645 self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
1647 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
1648 if(!self.reload_ammo_min || self.items & IT_UNLIMITED_WEAPON_AMMO)
1649 self.clip_load = self.reload_ammo_amount;
1652 while(self.clip_load < self.reload_ammo_amount && self.(self.current_ammo)) // make sure we don't add more ammo than we have
1654 self.clip_load += 1;
1655 self.(self.current_ammo) -= 1;
1658 self.weapon_load[self.weapon] = self.clip_load;
1660 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1661 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1662 // so your weapon is disabled for a few seconds without reason
1664 //ATTACK_FINISHED(self) -= self.reload_time - 1;
1669 void W_Reload(float sent_ammo_min, float sent_ammo_amount, float sent_time, string sent_sound)
1671 // set global values to work with
1673 self.reload_ammo_min = sent_ammo_min;
1674 self.reload_ammo_amount = sent_ammo_amount;
1675 self.reload_time = sent_time;
1676 self.reload_sound = sent_sound;
1678 // check if we meet the necessary conditions to reload
1681 e = get_weaponinfo(self.weapon);
1683 // don't reload weapons that don't have the RELOADABLE flag
1684 if not(e.spawnflags & WEP_FLAG_RELOADABLE)
1686 dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
1690 // return if reloading is disabled for this weapon
1691 if(!self.reload_ammo_amount)
1694 // our weapon is fully loaded, no need to reload
1695 if (self.clip_load >= self.reload_ammo_amount)
1698 // no ammo, so nothing to load
1699 if(!self.(self.current_ammo) && self.reload_ammo_min)
1700 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1702 if(clienttype(self) == CLIENTTYPE_REAL && self.reload_complain < time)
1704 play2(self, "weapons/unavailable.wav");
1705 sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
1706 self.reload_complain = time + 1;
1708 // switch away if the amount of ammo is not enough to keep using this weapon
1709 if not(weapon_action(self.weapon, WR_CHECKAMMO1) + weapon_action(self.weapon, WR_CHECKAMMO2))
1711 self.clip_load = -1; // reload later
1712 W_SwitchToOtherWeapon(self);
1717 if (self.weaponentity)
1719 if (self.weaponentity.wframe == WFRAME_RELOAD)
1722 // allow switching away while reloading, but this will cause a new reload!
1723 self.weaponentity.state = WS_READY;
1726 // now begin the reloading process
1728 sound (self, CHAN_WEAPON2, self.reload_sound, VOL_BASE, ATTN_NORM);
1730 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
1731 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
1732 // so your weapon is disabled for a few seconds without reason
1734 //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
1736 weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);
1738 if(self.clip_load < 0)
1740 self.old_clip_load = self.clip_load;
1741 self.clip_load = self.weapon_load[self.weapon] = -1;