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 * cvar("g_balance_rune_speed_combo_atkrate");
24 t = t * cvar("g_balance_rune_speed_atkrate");
26 else if(self.runes & CURSE_SLOW)
28 t = t * cvar("g_balance_curse_slow_atkrate");
35 void W_SwitchWeapon_Force(entity e, float w)
37 e.cnt = e.switchweapon;
43 // VorteX: static frame globals
44 float WFRAME_DONTCHANGE = -1;
45 float WFRAME_FIRE1 = 0;
46 float WFRAME_FIRE2 = 1;
47 float WFRAME_IDLE = 2;
48 float WFRAME_RELOAD = 3;
51 void(float fr, float t, void() func) weapon_thinkf;
53 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
56 ret_x = screenright * v;
58 ret_z = screenforward * v;
62 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
65 vector mi, ma, thisv, myv, ret;
67 myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
69 // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
71 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
74 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
75 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
76 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
77 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
80 if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
81 if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
82 //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
91 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
92 ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
93 ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
94 ret_z = thisv_z - myv_z;
98 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
104 if(player.hitplotfh >= 0)
106 lag = ANTILAG_LATENCY(player);
109 if(clienttype(player) != CLIENTTYPE_REAL)
110 lag = 0; // only antilag for clients
112 org = player.origin + player.view_ofs;
113 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
114 if(trace_ent.flags & FL_CLIENT)
116 antilag_takeback(trace_ent, time - lag);
117 hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
118 antilag_restore(trace_ent);
119 fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
120 //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
128 // this function calculates w_shotorg and w_shotdir based on the weapon model
129 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
130 // make sure you call makevectors first (FIXME?)
131 void W_SetupShot_Dir_ProjectileSize(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float maxdamage)
133 float nudge = 1; // added to traceline target and subtracted from result
134 local vector trueaimpoint;
135 local float oldsolid;
137 oldsolid = ent.dphitcontentsmask;
138 if(ent.weapon == WEP_CAMPINGRIFLE)
139 ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
141 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
143 traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
144 // passing world, because we do NOT want it to touch dphitcontentsmask
146 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, ent);
147 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
148 trueaimpoint = trace_endpos;
151 if not(inWarmupStage) {
153 w = get_weaponinfo(ent.weapon);
154 if(w.spawnflags & WEP_TYPE_SPLASH) { // splash damage
155 ent.stats_fired[ent.weapon - 1] += maxdamage;
156 ent.stat_fired = ent.weapon + 64 * floor(ent.stats_fired[ent.weapon - 1]);
160 W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
162 if(ent.weaponentity.movedir_x > 0)
164 vecs = ent.weaponentity.movedir;
170 if(debug_shotorg != '0 0 0')
171 vecs = debug_shotorg;
173 dv = v_right * vecs_y + v_up * vecs_z;
174 w_shotorg = ent.origin + ent.view_ofs + dv;
176 // now move the shotorg forward as much as requested if possible
179 if(ent.antilag_debug)
180 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
182 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
185 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
186 w_shotorg = trace_endpos - v_forward * nudge;
187 // calculate the shotdir from the chosen shotorg
188 w_shotdir = normalize(trueaimpoint - w_shotorg);
191 if (!ent.cvar_cl_noantilag)
193 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
195 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
196 if (!trace_ent.takedamage)
198 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
199 if (trace_ent.takedamage && trace_ent.classname == "player")
203 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
205 w_shotdir = normalize(trace_ent.origin - w_shotorg);
209 else if(cvar("g_antilag") == 3) // client side hitscan
211 if (ent.cursor_trace_ent) // client was aiming at someone
212 if (ent.cursor_trace_ent != ent) // just to make sure
213 if (ent.cursor_trace_ent.takedamage) // and that person is killable
214 if (ent.cursor_trace_ent.classname == "player") // and actually a player
216 // verify that the shot would miss without antilag
217 // (avoids an issue where guns would always shoot at their origin)
218 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
219 if (!trace_ent.takedamage)
221 // verify that the shot would hit if altered
222 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
223 if (trace_ent == ent.cursor_trace_ent)
224 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
226 print("antilag fail\n");
232 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
235 ent.punchangle_x = recoil * -1;
239 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
242 if (ent.items & IT_STRENGTH)
244 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
247 #define W_SetupShot_ProjectileSize(ent,mi,ma,antilag,recoil,snd,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, v_forward, mi, ma, antilag, recoil, snd, maxdamage)
248 #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)
249 #define W_SetupShot(ent,antilag,recoil,snd,maxdamage) W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, maxdamage)
251 void LaserTarget_Think()
258 // list of weapons that will use the laser, and the options that enable it
259 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
262 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
267 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
270 // rocket launcher isn't selected, so no laser target.
271 if(self.lasertarget != world)
273 remove(self.lasertarget);
274 self.lasertarget = world;
279 if(!self.lasertarget)
281 // we don't have a lasertarget entity, so spawn one
282 //bprint("create laser target\n");
283 e = self.lasertarget = spawn();
284 e.owner = self.owner; // Its owner is my owner
285 e.classname = "laser_target";
286 e.movetype = MOVETYPE_NOCLIP; // don't touch things
287 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
288 e.scale = 1.25; // make it larger
289 e.alpha = 0.25; // transparency
290 e.colormod = '255 0 0' * (1/255) * 8; // change colors
291 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
292 // make it dynamically glow
293 // you should avoid over-using this, as it can slow down the player's computer.
294 e.glow_color = 251; // red color
298 e = self.lasertarget;
300 // move the laser dot to where the player is looking
302 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
303 offset = '0 0 26' + v_right*3;
304 traceline(self.owner.origin + offset, self.owner.origin + offset + v_forward * MAX_SHOT_DISTANCE, FALSE, self); // trace forward until you hit something, like a player or wall
305 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
306 if(trace_plane_normal != '0 0 0')
307 e.angles = vectoangles(trace_plane_normal);
309 e.angles = vectoangles(v_forward);
312 float CL_Weaponentity_CustomizeEntityForClient()
314 self.viewmodelforclient = self.owner;
315 if(other.classname == "spectator")
316 if(other.enemy == self.owner)
317 self.viewmodelforclient = other;
321 float qcweaponanimation;
322 vector weapon_offset = '0 -10 0';
323 vector weapon_adjust = '10 0 -15';
324 .vector weapon_morph0origin;
325 .vector weapon_morph0angles;
326 .float weapon_morph0time;
327 .vector weapon_morph1origin;
328 .vector weapon_morph1angles;
329 .float weapon_morph1time;
330 .vector weapon_morph2origin;
331 .vector weapon_morph2angles;
332 .float weapon_morph2time;
333 .vector weapon_morph3origin;
334 .vector weapon_morph3angles;
335 .float weapon_morph3time;
336 .vector weapon_morph4origin;
337 .vector weapon_morph4angles;
338 .float weapon_morph4time;
340 #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)
345 * 1. simple animated model, muzzle flash handling on h_ model:
346 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
348 * shot = muzzle end (shot origin, also used for muzzle flashes)
349 * shell = casings ejection point (must be on the right hand side of the gun)
350 * weapon = attachment for v_tuba.md3
351 * v_tuba.md3 - first and third person model
352 * g_tuba.md3 - pickup model
354 * 2. simple animated model, muzzle flash handling on v_ model:
355 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
357 * weapon = attachment for v_tuba.md3
358 * v_tuba.md3 - first and third person model
360 * shot = muzzle end (shot origin, also used for muzzle flashes)
361 * shell = casings ejection point (must be on the right hand side of the gun)
362 * g_tuba.md3 - pickup model
364 * 3. fully animated model, muzzle flash handling on h_ model:
365 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
367 * shot = muzzle end (shot origin, also used for muzzle flashes)
368 * shell = casings ejection point (must be on the right hand side of the gun)
369 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
370 * v_tuba.md3 - third person model
371 * g_tuba.md3 - pickup model
373 * 4. fully animated model, muzzle flash handling on v_ model:
374 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
376 * shot = muzzle end (shot origin)
377 * shell = casings ejection point (must be on the right hand side of the gun)
378 * v_tuba.md3 - third person model
380 * shot = muzzle end (for muzzle flashes)
381 * g_tuba.md3 - pickup model
384 void CL_Weaponentity_Think()
386 float tb, v_shot_idx;
387 self.nextthink = time;
388 if (intermission_running)
389 self.frame = self.anim_idle_x;
390 if (self.owner.weaponentity != self)
392 if (self.weaponentity)
393 remove(self.weaponentity);
397 if (self.owner.deadflag != DEAD_NO)
400 if (self.weaponentity)
401 self.weaponentity.model = "";
404 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
406 self.cnt = self.owner.weapon;
407 self.dmg = self.owner.modelindex;
408 self.deadflag = self.owner.deadflag;
412 if (self.owner.weaponname != "")
414 // if there is a child entity, hide it until we're sure we use it
415 if (self.weaponentity)
416 self.weaponentity.model = "";
417 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
418 v_shot_idx = gettagindex(self, "shot"); // used later
420 v_shot_idx = gettagindex(self, "tag_shot");
422 if(qcweaponanimation)
424 self.angles = '0 0 0';
425 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
426 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
427 self.movedir_x += 32;
428 self.spawnorigin = self.movedir;
429 // oldorigin - not calculated here
433 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
434 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
435 animfile = fopen(animfilename, FILE_READ);
436 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
437 self.anim_fire1 = '0 1 0.01';
438 self.anim_fire2 = '1 1 0.01';
439 self.anim_idle = '2 1 0.01';
440 self.anim_reload = '3 1 0.01';
443 animparseerror = FALSE;
444 self.anim_fire1 = animparseline(animfile);
445 self.anim_fire2 = animparseline(animfile);
446 self.anim_idle = animparseline(animfile);
447 self.anim_reload = animparseline(animfile);
450 print("Parse error in ", animfilename, ", some player animations are broken\n");
453 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
454 // if we don't, this is a "real" animated model
455 if(gettagindex(self, "weapon"))
457 if (!self.weaponentity)
458 self.weaponentity = spawn();
459 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
460 setattachment(self.weaponentity, self, "weapon");
462 else if(gettagindex(self, "tag_weapon"))
464 if (!self.weaponentity)
465 self.weaponentity = spawn();
466 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
467 setattachment(self.weaponentity, self, "tag_weapon");
471 if(self.weaponentity)
472 remove(self.weaponentity);
473 self.weaponentity = world;
476 setorigin(self,'0 0 0');
477 self.angles = '0 0 0';
479 self.viewmodelforclient = world;
483 if(v_shot_idx) // v_ model attached to invisible h_ model
485 self.movedir = gettaginfo(self.weaponentity, v_shot_idx);
489 idx = gettagindex(self, "shot");
491 idx = gettagindex(self, "tag_shot");
493 self.movedir = gettaginfo(self, idx);
496 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
497 self.movedir = '0 0 0';
501 if(self.weaponentity) // v_ model attached to invisible h_ model
503 idx = gettagindex(self.weaponentity, "shell");
505 idx = gettagindex(self.weaponentity, "tag_shell");
507 self.spawnorigin = gettaginfo(self.weaponentity, idx);
513 idx = gettagindex(self, "shell");
515 idx = gettagindex(self, "tag_shell");
517 self.spawnorigin = gettaginfo(self, idx);
520 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
521 self.spawnorigin = self.movedir;
527 self.oldorigin = '0 0 0'; // use regular attachment
531 if(self.weaponentity)
533 idx = gettagindex(self, "weapon");
535 idx = gettagindex(self, "tag_weapon");
539 idx = gettagindex(self, "handle");
541 idx = gettagindex(self, "tag_handle");
545 self.oldorigin = self.movedir - gettaginfo(self, idx);
549 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");
550 self.oldorigin = '0 0 0'; // there is no way to recover from this
554 self.viewmodelforclient = self.owner;
560 if(self.weaponentity)
561 remove(self.weaponentity);
562 self.weaponentity = world;
563 self.movedir = '0 0 0';
564 self.spawnorigin = '0 0 0';
565 self.oldorigin = '0 0 0';
566 self.anim_fire1 = '0 1 0.01';
567 self.anim_fire2 = '0 1 0.01';
568 self.anim_idle = '0 1 0.01';
569 self.anim_reload = '0 1 0.01';
572 self.view_ofs = '0 0 0';
574 if(self.movedir_x >= 0)
578 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
579 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
581 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
582 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
584 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
586 // check if an instant weapon switch occurred
587 if (qcweaponanimation)
589 if (self.state == WS_READY)
591 self.angles = '0 0 0';
592 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
593 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
597 setorigin(self, self.view_ofs);
598 // reset animstate now
599 self.wframe = WFRAME_IDLE;
600 self.weapon_morph0time = 0;
601 self.weapon_morph1time = 0;
602 self.weapon_morph2time = 0;
603 self.weapon_morph3time = 0;
604 self.weapon_morph4time = 0;
605 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
608 tb = (self.effects & (EF_TELEPORT_BIT | EF_RESTARTANIM_BIT));
609 self.effects = self.owner.effects & EFMASK_CHEAP;
610 self.effects &~= EF_LOWPRECISION;
611 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
612 self.effects &~= EF_TELEPORT_BIT;
613 self.effects &~= EF_RESTARTANIM_BIT;
616 if(self.owner.alpha == default_player_alpha)
617 self.alpha = default_weapon_alpha;
618 else if(self.owner.alpha != 0)
619 self.alpha = self.owner.alpha;
623 self.colormap = self.owner.colormap;
624 if (self.weaponentity)
626 self.weaponentity.effects = self.effects;
627 self.weaponentity.alpha = self.alpha;
628 self.weaponentity.colormap = self.colormap;
631 self.angles = '0 0 0';
634 if (self.state == WS_RAISE && !intermission_running)
636 f = (self.owner.weapon_nextthink - time) * g_weaponratefactor / cvar("g_balance_weaponswitchdelay");
637 self.angles_x = -90 * f * f;
638 if (qcweaponanimation)
640 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
641 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
644 else if (self.state == WS_DROP && !intermission_running)
646 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponratefactor / cvar("g_balance_weaponswitchdelay");
647 self.angles_x = -90 * f * f;
648 if (qcweaponanimation)
650 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
651 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
654 else if (self.state == WS_CLEAR)
657 self.angles_x = -90 * f * f;
658 if (qcweaponanimation)
660 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
661 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
664 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
666 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
667 f = 1 - pow(1 - f, 3);
668 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
669 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
671 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
673 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
674 f = 1 - pow(1 - f, 3);
675 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
676 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
678 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
680 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
681 f = 1 - pow(1 - f, 3);
682 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
683 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
685 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
687 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
688 f = 1 - pow(1 - f, 3);
689 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
690 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
692 else if (qcweaponanimation)
694 // begin a new idle morph
695 self.owner.weapon_morph0time = time;
696 self.owner.weapon_morph0angles = self.angles;
697 self.owner.weapon_morph0origin = self.origin;
705 // turn gun to the left to look at it
707 self.owner.weapon_morph1time = time + t * 0.2;
708 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
709 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');
710 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
712 self.owner.weapon_morph2time = time + t * 0.6;
713 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
714 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');
715 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
717 self.owner.weapon_morph3time = time + t;
718 self.owner.weapon_morph3angles = '0 0 0';
719 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');
720 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
724 // raise the gun a bit
726 self.owner.weapon_morph1time = time + t * 0.2;
727 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
728 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');
729 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
731 self.owner.weapon_morph2time = time + t * 0.5;
732 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
733 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');
734 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
736 self.owner.weapon_morph3time = time + t;
737 self.owner.weapon_morph3angles = '0 0 0';
738 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');
739 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
745 self.owner.weapon_morph1time = time + t * 0.3;
746 self.owner.weapon_morph1angles = randomvec() * 6;
747 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');
748 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
750 self.owner.weapon_morph2time = time + t * 0.7;
751 self.owner.weapon_morph2angles = randomvec() * 6;
752 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');
753 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
755 self.owner.weapon_morph3time = time + t;
756 self.owner.weapon_morph3angles = '0 0 0';
757 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');
758 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
762 // hold it mostly steady
763 t = random() * 6 + 4;
764 self.owner.weapon_morph1time = time + t * 0.2;
765 self.owner.weapon_morph1angles = randomvec() * 1;
766 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');
767 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
769 self.owner.weapon_morph2time = time + t * 0.5;
770 self.owner.weapon_morph2angles = randomvec() * 1;
771 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');
772 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
774 self.owner.weapon_morph3time = time + t * 0.7;
775 self.owner.weapon_morph3angles = randomvec() * 1;
776 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');
777 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
780 self.owner.weapon_morph4time = time + t;
781 self.owner.weapon_morph4angles = '0 0 0';
782 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');
783 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
787 // create or update the lasertarget entity
791 void CL_ExteriorWeaponentity_Think()
795 self.nextthink = time;
796 if (self.owner.exteriorweaponentity != self)
801 if (self.owner.deadflag != DEAD_NO)
806 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
808 self.cnt = self.owner.weapon;
809 self.dmg = self.owner.modelindex;
810 self.deadflag = self.owner.deadflag;
811 if (self.owner.weaponname != "")
812 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
816 if((tag_found = gettagindex(self.owner, "tag_weapon")))
818 self.tag_index = tag_found;
819 self.tag_entity = self.owner;
822 setattachment(self, self.owner, "bip01 r hand");
824 // if that didn't find a tag, hide the exterior weapon model
828 self.effects = self.owner.effects;
829 if(sv_pitch_min == sv_pitch_max)
830 self.effects |= EF_LOWPRECISION;
832 self.effects &~= EF_LOWPRECISION;
833 self.effects = self.effects & EFMASK_CHEAP; // eat performance
834 if(self.owner.alpha == default_player_alpha)
835 self.alpha = default_weapon_alpha;
836 else if(self.owner.alpha != 0)
837 self.alpha = self.owner.alpha;
841 ang_x = bound(sv_pitch_min, self.owner.v_angle_x, sv_pitch_max);
845 if(sv_pitch_fixyaw) // workaround for stupid player models that don't aim forward
847 ang_y = self.owner.v_angle_y;
849 var vector v = v_forward;
850 var float t = self.tag_entity.frame1time;
851 var float f = self.tag_entity.frame;
852 self.tag_entity.frame1time = time;
853 self.tag_entity.frame = self.tag_entity.anim_idle_x;
854 gettaginfo(self.tag_entity, self.tag_index);
855 self.tag_entity.frame1time = t;
856 self.tag_entity.frame = f;
857 // untransform v according to this coordinate space
862 self.angles = vectoangles(w);
866 ang_x = -/* don't ask */ang_x;
870 self.colormap = self.owner.colormap;
873 // spawning weaponentity for client
874 void CL_SpawnWeaponentity()
876 self.weaponentity = spawn();
877 self.weaponentity.classname = "weaponentity";
878 self.weaponentity.solid = SOLID_NOT;
879 self.weaponentity.owner = self;
880 setmodel(self.weaponentity, ""); // precision set when changed
881 setorigin(self.weaponentity, '0 0 0');
882 self.weaponentity.angles = '0 0 0';
883 self.weaponentity.viewmodelforclient = self;
884 self.weaponentity.flags = 0;
885 self.weaponentity.think = CL_Weaponentity_Think;
886 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
887 self.weaponentity.nextthink = time;
889 self.exteriorweaponentity = spawn();
890 self.exteriorweaponentity.classname = "exteriorweaponentity";
891 self.exteriorweaponentity.solid = SOLID_NOT;
892 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
893 self.exteriorweaponentity.owner = self;
894 setorigin(self.exteriorweaponentity, '0 0 0');
895 self.exteriorweaponentity.angles = '0 0 0';
896 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
897 self.exteriorweaponentity.nextthink = time;
900 .float hasweapon_complain_spam;
902 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
904 local float weaponbit, f;
905 local entity oldself;
907 if(time < self.hasweapon_complain_spam)
910 self.hasweapon_complain_spam = time + 0.2;
912 if (wpn < WEP_FIRST || wpn > WEP_LAST)
915 sprint(self, "Invalid weapon\n");
918 weaponbit = W_WeaponBit(wpn);
919 if (cl.weapons & weaponbit)
923 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
931 f = weapon_action(wpn, WR_CHECKAMMO1);
932 f = f + weapon_action(wpn, WR_CHECKAMMO2);
938 if(clienttype(cl) == CLIENTTYPE_REAL)
940 play2(cl, "weapons/unavailable.wav");
941 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
951 // Report Proper Weapon Status / Modified Weapon Ownership Message
952 if(weaponsInMap & weaponbit)
954 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
956 if(cvar("g_showweaponspawns"))
961 e = get_weaponinfo(wpn);
964 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
966 if(e.classname == "droppedweapon")
968 if not(e.flags & FL_ITEM)
970 WaypointSprite_Spawn(
982 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
984 play2(cl, "weapons/unavailable.wav");
992 if (self.weapon != -1)
994 if (self.weaponentity)
996 self.weaponentity.state = WS_CLEAR;
997 self.weaponentity.effects = 0;
1003 if (self.weaponentity)
1004 self.weaponentity.state = WS_READY;
1005 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
1008 // Setup weapon for client (after this raise frame will be launched)
1009 void weapon_setup(float windex)
1012 qcweaponanimation = cvar("sv_qcweaponanimation");
1013 e = get_weaponinfo(windex);
1014 self.items &~= IT_AMMO;
1015 self.items = self.items | e.items;
1017 // the two weapon entities will notice this has changed and update their models
1018 self.weapon = windex;
1019 self.weaponname = e.mdl;
1020 self.bulletcounter = 0;
1023 // perform weapon to attack (weaponstate and attack_finished check is here)
1024 .float race_penalty;
1025 float weapon_prepareattack(float secondary, float attacktime)
1027 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
1028 //if all players readied up and the countdown is running
1029 if(time < game_starttime || time < self.race_penalty) {
1033 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1034 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
1036 // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)
1038 w = W_WeaponBit(self.weapon);
1040 ww = w_getbestweapon(self);
1043 W_SwitchWeapon_Force(self, ww);
1047 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
1050 // do not even think about shooting if switching
1051 if(self.switchweapon != self.weapon)
1056 // don't fire if previous attack is not finished
1057 if (ATTACK_FINISHED(self) > time + self.weapon_frametime * 0.5)
1059 // don't fire while changing weapon
1060 if (self.weaponentity.state != WS_READY)
1063 self.weaponentity.state = WS_INUSE;
1065 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1067 // if the weapon hasn't been firing continuously, reset the timer
1070 if (ATTACK_FINISHED(self) < time - self.weapon_frametime * 1.5)
1072 ATTACK_FINISHED(self) = time;
1073 //dprint("resetting attack finished to ", ftos(time), "\n");
1075 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponRateFactor();
1077 self.bulletcounter += 1;
1078 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1082 void weapon_thinkf(float fr, float t, void() func)
1088 if(fr == WFRAME_DONTCHANGE)
1090 fr = self.weaponentity.wframe;
1091 restartanim = FALSE;
1093 else if (fr == WFRAME_IDLE)
1094 restartanim = FALSE;
1102 if (self.weaponentity)
1104 self.weaponentity.wframe = fr;
1105 if (qcweaponanimation)
1107 if (fr != WFRAME_IDLE)
1109 self.weapon_morph0time = time;
1110 self.weapon_morph0angles = self.weaponentity.angles;
1111 self.weapon_morph0origin = self.weaponentity.origin;
1113 self.weapon_morph1angles = '0 0 0';
1114 self.weapon_morph1time = time + t;
1115 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1116 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1118 self.weapon_morph2angles = '0 0 0';
1119 self.weapon_morph2time = time + t;
1120 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1121 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1123 self.weapon_morph3angles = '0 0 0';
1124 self.weapon_morph3time = time + t;
1125 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1126 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1128 self.weapon_morph4angles = '0 0 0';
1129 self.weapon_morph4time = time + t;
1130 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1131 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1133 if (fr == WFRAME_FIRE1)
1135 self.weapon_morph1angles = '5 0 0';
1136 self.weapon_morph1time = time + t * 0.1;
1137 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1138 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1139 self.weapon_morph4time = time + t + 1; // delay idle effect
1141 else if (fr == WFRAME_FIRE2)
1143 self.weapon_morph1angles = '10 0 0';
1144 self.weapon_morph1time = time + t * 0.1;
1145 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1146 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1147 self.weapon_morph4time = time + t + 1; // delay idle effect
1149 else if (fr == WFRAME_RELOAD)
1151 self.weapon_morph1time = time + t * 0.05;
1152 self.weapon_morph1angles = '-10 40 0';
1153 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1154 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1156 self.weapon_morph2time = time + t * 0.15;
1157 self.weapon_morph2angles = '-10 40 5';
1158 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1159 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1161 self.weapon_morph3time = time + t * 0.25;
1162 self.weapon_morph3angles = '-10 40 0';
1163 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1164 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1170 if (fr == WFRAME_IDLE)
1171 a = self.weaponentity.anim_idle;
1172 else if (fr == WFRAME_FIRE1)
1173 a = self.weaponentity.anim_fire1;
1174 else if (fr == WFRAME_FIRE2)
1175 a = self.weaponentity.anim_fire2;
1176 else if (fr == WFRAME_RELOAD)
1177 a = self.weaponentity.anim_reload;
1178 a_z *= g_weaponratefactor;
1179 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1187 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1189 backtrace("Tried to override initial weapon think function - should this really happen?");
1192 t *= W_WeaponRateFactor();
1194 // VorteX: haste can be added here
1195 if (self.weapon_think == w_ready)
1197 self.weapon_nextthink = time;
1198 //dprint("started firing at ", ftos(time), "\n");
1200 if (self.weapon_nextthink < time - self.weapon_frametime * 1.5 || self.weapon_nextthink > time + self.weapon_frametime * 1.5)
1202 self.weapon_nextthink = time;
1203 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1205 self.weapon_nextthink = self.weapon_nextthink + t;
1206 self.weapon_think = func;
1207 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1211 if (!self.crouch) // shoot anim stands up, this looks bad
1214 anim = self.anim_shoot;
1215 anim_z = anim_y / (t + sys_frametime);
1216 setanim(self, anim, FALSE, TRUE, TRUE);
1220 void weapon_boblayer1(float spd, vector org)
1222 // VorteX: haste can be added here
1225 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1233 mvelocity = mvelocity * g_weaponspeedfactor;
1235 mdirection = normalize(mvelocity);
1236 mspeed = vlen(mvelocity);
1238 nstyle = cvar("g_projectiles_newton_style");
1241 // absolute velocity
1242 outvelocity = mvelocity;
1244 else if(nstyle == 1)
1246 // true Newtonian projectiles
1247 outvelocity = pvelocity + mvelocity;
1249 else if(nstyle == 2)
1251 // true Newtonian projectiles with automatic aim adjustment
1253 // solve: |outspeed * mdirection - pvelocity| = mspeed
1254 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1255 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1259 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1260 // velocity without mdirection component > mspeed
1261 // fire at smallest possible mspeed that works?
1262 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1265 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1267 outspeed = solution_y; // the larger one
1270 //outspeed = 0; // slowest possible shot
1271 outspeed = solution_x; // the real part (that is, the average!)
1272 //dprint("impossible shot, adjusting\n");
1275 outspeed = bound(mspeed * cvar("g_projectiles_newton_style_2_minfactor"), outspeed, mspeed * cvar("g_projectiles_newton_style_2_maxfactor"));
1276 outvelocity = mdirection * outspeed;
1278 else if(nstyle == 3)
1280 // pseudo-Newtonian:
1281 outspeed = mspeed + mdirection * pvelocity;
1282 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1283 outvelocity = mdirection * outspeed;
1285 else if(nstyle == 4)
1288 outspeed = mspeed + vlen(pvelocity);
1289 outvelocity = mdirection * outspeed;
1292 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1297 void W_AttachToShotorg(entity flash, vector offset)
1301 flash.angles_z = random() * 360;
1302 if(qcweaponanimation)
1304 setorigin(flash, w_shotorg + w_shotdir * 50);
1305 flash.angles = vectoangles(w_shotdir);
1306 flash.angles_z = random() * 360;
1310 setattachment(flash, self.weaponentity, "shot");
1311 setorigin(flash, offset);
1314 copyentity(flash, xflash);
1316 flash.viewmodelforclient = self;
1318 if(self.weaponentity.oldorigin_x > 0)
1320 setattachment(xflash, self.exteriorweaponentity, "");
1321 setorigin(xflash, self.weaponentity.oldorigin + offset);
1325 setattachment(xflash, self.exteriorweaponentity, "shot");
1330 vector cliptoplane(vector v, vector p)
1332 return v - (v * p) * p;
1335 vector solve_cubic_pq(float p, float q)
1338 D = q*q/4.0 + p*p*p/27.0;
1342 a = 1.0/3.0 * acos(-q/2.0 * sqrt(-27.0/(p*p*p)));
1343 u = sqrt(-4.0/3.0 * p);
1344 // a in range 0..pi/3
1351 '1 0 0' * cos(a + 2.0/3.0*M_PI)
1353 '0 1 0' * cos(a + 4.0/3.0*M_PI)
1366 return '1 1 0' * v + '0 0 1' * u;
1368 return '0 1 1' * v + '1 0 0' * u;
1373 u = cbrt(-q/2.0 + sqrt(D));
1374 v = cbrt(-q/2.0 - sqrt(D));
1375 return '1 1 1' * (u + v);
1378 vector solve_cubic_abcd(float a, float b, float c, float d)
1384 p = (9*a*c - 3*b*b);
1385 q = (27*a*a*d - 9*a*b*c + 2*b*b*b);
1386 v = solve_cubic_pq(p, q);
1387 v = (v - b * '1 1 1') * (1.0 / (3.0 * a));
1389 v += '1 0 -1' * (v_z - v_x); // swap x, z
1393 vector findperpendicular(vector v)
1399 return normalize(cliptoplane(p, v));
1402 vector W_CalculateProjectileSpread(vector forward, float spread)
1408 spread *= g_weaponspreadfactor;
1411 sstyle = cvar("g_projectiles_spread_style");
1415 // this is the baseline for the spread value!
1416 // standard deviation: sqrt(2/5)
1417 // density function: sqrt(1-r^2)
1418 return forward + randomvec() * spread;
1420 else if(sstyle == 1)
1422 // same thing, basically
1423 return normalize(forward + cliptoplane(randomvec() * spread, forward));
1425 else if(sstyle == 2)
1427 // circle spread... has at sigma=1 a standard deviation of sqrt(1/2)
1428 sigma = spread * 0.89442719099991587855; // match baseline stddev
1429 v1 = findperpendicular(forward);
1430 v2 = cross(forward, v1);
1431 // random point on unit circle
1432 dx = random() * 2 * M_PI;
1435 // radius in our dist function
1438 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1440 else if(sstyle == 3) // gauss 3d
1442 sigma = spread * 0.44721359549996; // match baseline stddev
1443 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1445 v1_x += gsl_ran_gaussian(sigma);
1446 v1_y += gsl_ran_gaussian(sigma);
1447 v1_z += gsl_ran_gaussian(sigma);
1450 else if(sstyle == 4) // gauss 2d
1452 sigma = spread * 0.44721359549996; // match baseline stddev
1453 // note: 2D gaussian has sqrt(2) times the stddev of 1D, so this factor is right
1454 v1_x = gsl_ran_gaussian(sigma);
1455 v1_y = gsl_ran_gaussian(sigma);
1456 v1_z = gsl_ran_gaussian(sigma);
1457 return normalize(forward + cliptoplane(v1, forward));
1459 else if(sstyle == 5) // 1-r
1461 sigma = spread * 1.154700538379252; // match baseline stddev
1462 v1 = findperpendicular(forward);
1463 v2 = cross(forward, v1);
1464 // random point on unit circle
1465 dx = random() * 2 * M_PI;
1468 // radius in our dist function
1470 r = solve_cubic_abcd(-2, 3, 0, -r) * '0 1 0';
1471 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1473 else if(sstyle == 6) // 1-r^2
1475 sigma = spread * 1.095445115010332; // match baseline stddev
1476 v1 = findperpendicular(forward);
1477 v2 = cross(forward, v1);
1478 // random point on unit circle
1479 dx = random() * 2 * M_PI;
1482 // radius in our dist function
1486 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1488 else if(sstyle == 7) // (1-r) (2-r)
1490 sigma = spread * 1.224744871391589; // match baseline stddev
1491 v1 = findperpendicular(forward);
1492 v2 = cross(forward, v1);
1493 // random point on unit circle
1494 dx = random() * 2 * M_PI;
1497 // radius in our dist function
1501 return normalize(forward + (v1 * dx + v2 * dy) * r * sigma);
1504 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)!");
1507 * how to derive falloff functions:
1508 * rho(r) := (2-r) * (1-r);
1511 * rhor(r) := r * rho(r);
1512 * cr(t) := integrate(rhor(r), r, a, t);
1513 * scr(t) := integrate(rhor(r) * r^2, r, a, t);
1514 * variance : scr(b) / cr(b);
1515 * solve(cr(r) = rand * cr(b), r), programmmode:false;
1516 * sqrt(0.4 / variance), numer;
1522 float mspercallsstyle;
1523 float mspercallcount;
1525 void W_SetupProjectileVelocityEx(entity missile, vector dir, vector upDir, float pSpeed, float pUpSpeed, float spread)
1527 if(missile.owner == world)
1528 error("Unowned missile");
1530 dir = dir + upDir * (pUpSpeed / pSpeed);
1531 pSpeed *= vlen(dir);
1532 dir = normalize(dir);
1535 if(cvar("g_projectiles_spread_style") != mspercallsstyle)
1537 mspercallsum = mspercallcount = 0;
1538 mspercallsstyle = cvar("g_projectiles_spread_style");
1540 mspercallsum -= gettime(GETTIME_HIRES);
1542 dir = W_CalculateProjectileSpread(dir, spread);
1544 mspercallsum += gettime(GETTIME_HIRES);
1545 mspercallcount += 1;
1546 print("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
1549 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, pSpeed * dir);
1552 void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
1554 W_SetupProjectileVelocityEx(missile, w_shotdir, v_up, pSpeed, 0, spread);
1557 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_spread"))
1558 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, cvar(#s "_spread"))