2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 float W_WeaponSpeedFactor()
13 t = 1.0 / g_weaponspeedfactor;
17 if(self.runes & RUNE_SPEED)
19 if(self.runes & CURSE_SLOW)
20 t = t * cvar("g_balance_rune_speed_combo_atkrate");
22 t = t * cvar("g_balance_rune_speed_atkrate");
24 else if(self.runes & CURSE_SLOW)
26 t = t * cvar("g_balance_curse_slow_atkrate");
33 void W_SwitchWeapon_Force(entity e, float w)
35 e.cnt = e.switchweapon;
41 // VorteX: static frame globals
42 float WFRAME_DONTCHANGE = -1;
43 float WFRAME_FIRE1 = 0;
44 float WFRAME_FIRE2 = 1;
45 float WFRAME_IDLE = 2;
46 float WFRAME_RELOAD = 3;
49 void(float fr, float t, void() func) weapon_thinkf;
51 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
54 ret_x = screenright * v;
56 ret_z = screenforward * v;
60 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
63 vector mi, ma, thisv, myv, ret;
65 myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
67 // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
69 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
72 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
73 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
74 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
75 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
78 if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
79 if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
80 //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
89 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
90 ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
91 ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
92 ret_z = thisv_z - myv_z;
96 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
102 if(player.hitplotfh >= 0)
104 lag = ANTILAG_LATENCY(player);
107 if(clienttype(player) != CLIENTTYPE_REAL)
108 lag = 0; // only antilag for clients
110 org = player.origin + player.view_ofs;
111 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
112 if(trace_ent.flags & FL_CLIENT)
114 antilag_takeback(trace_ent, time - lag);
115 hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
116 antilag_restore(trace_ent);
117 fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
118 //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
126 // this function calculates w_shotorg and w_shotdir based on the weapon model
127 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
128 // make sure you call makevectors first (FIXME?)
129 void W_SetupShot_ProjectileSize(entity ent, vector mi, vector ma, float antilag, float recoil, string snd, float maxdamage)
131 float nudge = 1; // added to traceline target and subtracted from result
132 local vector trueaimpoint;
133 local float oldsolid;
135 oldsolid = ent.dphitcontentsmask;
136 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
137 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, ent); // no antilag needed
138 trueaimpoint = trace_endpos;
140 self.max_damage[self.weapon] += maxdamage;
141 self.maxdamage_fired = self.weapon + 64 * rint(self.max_damage[self.weapon]);
143 W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
145 if(ent.weaponentity.movedir_x > 0)
147 vecs = ent.weaponentity.movedir;
153 if(debug_shotorg != '0 0 0')
154 vecs = debug_shotorg;
156 dv = v_right * vecs_y + v_up * vecs_z;
157 w_shotorg = ent.origin + ent.view_ofs + dv;
159 // now move the shotorg forward as much as requested if possible
162 if(ent.antilag_debug)
163 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
165 tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
168 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
169 w_shotorg = trace_endpos - v_forward * nudge;
170 // calculate the shotdir from the chosen shotorg
171 w_shotdir = normalize(trueaimpoint - w_shotorg);
174 // explanation of g_antilag:
175 // if client reports it was aiming at a player, and the serverside trace
176 // says it would miss, change the aim point to the player's new origin,
177 // but only if the shot at the player's new origin would hit of course
179 // FIXME: a much better method for bullet weapons would be to leave a
180 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
181 // ghost corresponding to this player's ping time, and if so it would do
182 // damage to the real player
184 if (ent.cursor_trace_ent) // client was aiming at someone
185 if (ent.cursor_trace_ent != ent) // just to make sure
186 if (ent.cursor_trace_ent.takedamage) // and that person is killable
187 if (ent.cursor_trace_ent.classname == "player") // and actually a player
188 if (cvar("g_antilag") == 1)
190 // verify that the shot would miss without antilag
191 // (avoids an issue where guns would always shoot at their origin)
192 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
193 if (!trace_ent.takedamage)
195 // verify that the shot would hit if altered
196 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
197 if (trace_ent == ent.cursor_trace_ent)
199 // verify that the shot would hit in the past
200 if(ent.antilag_debug)
201 antilag_takeback(ent.cursor_trace_ent, time - ent.antilag_debug);
203 antilag_takeback(ent.cursor_trace_ent, time - ANTILAG_LATENCY(ent));
205 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
206 antilag_restore(ent.cursor_trace_ent);
208 if(trace_ent == ent.cursor_trace_ent)
211 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
212 dprint("ANTILAG HIT for ", ent.netname, "\n");
216 // prydon cursor aimbot or odd network conditions
217 dprint("WARNING: antilag ghost trace for ", ent.netname, " failed!\n");
220 if(cvar("developer") >= 2)
222 vector v, vplus, vel;
224 v = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) ));
225 vplus = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) + 0.01));
226 vel = (vplus - v) * (1 / 0.01);
227 // solve: v + X * vel = closest to ent.origin + ent.view_ofs, v_forward axis
228 v -= (ent.origin + ent.view_ofs);
229 // solve: v + X * vel = closest to v_forward axis
230 // project into 2D by subtracting v_forward components:
231 v -= (v * v_forward) * v_forward;
232 vel -= (vel * v_forward) * v_forward;
233 // solve: v + X * vel = closest to origin
234 // (v + X * vel)^2 closest to 0
235 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
236 X = -(v * vel) / (vel * vel);
237 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(ent) + X), " instead of ", ftos(ANTILAG_LATENCY(ent)), "\n");
245 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
247 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
248 if (!trace_ent.takedamage)
250 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
251 if (trace_ent.takedamage && trace_ent.classname == "player")
255 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
257 w_shotdir = normalize(trace_ent.origin - w_shotorg);
261 else if(cvar("g_antilag") == 3) // client side hitscan
263 if (ent.cursor_trace_ent) // client was aiming at someone
264 if (ent.cursor_trace_ent != ent) // just to make sure
265 if (ent.cursor_trace_ent.takedamage) // and that person is killable
266 if (ent.cursor_trace_ent.classname == "player") // and actually a player
268 // verify that the shot would miss without antilag
269 // (avoids an issue where guns would always shoot at their origin)
270 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
271 if (!trace_ent.takedamage)
273 // verify that the shot would hit if altered
274 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
275 if (trace_ent == ent.cursor_trace_ent)
276 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
278 print("antilag fail\n");
285 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
288 ent.punchangle_x = recoil * -1;
292 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
295 if (ent.items & IT_STRENGTH)
297 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
300 void W_SetupShot(entity ent, float antilag, float recoil, string snd, float maxdamage)
302 W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, maxdamage);
305 void LaserTarget_Think()
312 // list of weapons that will use the laser, and the options that enable it
313 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
316 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
321 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
324 // rocket launcher isn't selected, so no laser target.
325 if(self.lasertarget != world)
327 remove(self.lasertarget);
328 self.lasertarget = world;
333 if(!self.lasertarget)
335 // we don't have a lasertarget entity, so spawn one
336 //bprint("create laser target\n");
337 e = self.lasertarget = spawn();
338 e.owner = self.owner; // Its owner is my owner
339 e.classname = "laser_target";
340 e.movetype = MOVETYPE_NOCLIP; // don't touch things
341 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
342 e.scale = 1.25; // make it larger
343 e.alpha = 0.25; // transparency
344 e.colormod = '255 0 0' * (1/255) * 8; // change colors
345 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
346 // make it dynamically glow
347 // you should avoid over-using this, as it can slow down the player's computer.
348 e.glow_color = 251; // red color
352 e = self.lasertarget;
354 // move the laser dot to where the player is looking
356 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
357 offset = '0 0 26' + v_right*3;
358 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
359 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
360 if(trace_plane_normal != '0 0 0')
361 e.angles = vectoangles(trace_plane_normal);
363 e.angles = vectoangles(v_forward);
366 float CL_Weaponentity_CustomizeEntityForClient()
368 self.viewmodelforclient = self.owner;
369 if(other.classname == "spectator")
370 if(other.enemy == self.owner)
371 self.viewmodelforclient = other;
375 float qcweaponanimation;
376 vector weapon_offset = '0 -10 0';
377 vector weapon_adjust = '10 0 -15';
378 .vector weapon_morph0origin;
379 .vector weapon_morph0angles;
380 .float weapon_morph0time;
381 .vector weapon_morph1origin;
382 .vector weapon_morph1angles;
383 .float weapon_morph1time;
384 .vector weapon_morph2origin;
385 .vector weapon_morph2angles;
386 .float weapon_morph2time;
387 .vector weapon_morph3origin;
388 .vector weapon_morph3angles;
389 .float weapon_morph3time;
390 .vector weapon_morph4origin;
391 .vector weapon_morph4angles;
392 .float weapon_morph4time;
394 #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)
399 * 1. simple animated model, muzzlr flash handling on h_ model:
400 * h_tuba.dpm, h_tuba.dpm.animinfo - invisible model controlling the animation
402 * shot = muzzle end (shot origin, also used for muzzle flashes)
403 * shell = casings ejection point (must be on the right hand side of the gun)
404 * weapon = attachment for v_tuba.md3
405 * v_tuba.md3 - first and third person model
406 * g_tuba.md3 - pickup model
408 * 2. fully animated model, muzzle flash handling on h_ model:
409 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
411 * shot = muzzle end (shot origin, also used for muzzle flashes)
412 * shell = casings ejection point (must be on the right hand side of the gun)
413 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
414 * v_tuba.md3 - third person model
415 * g_tuba.md3 - pickup model
417 * 3. fully animated model, muzzle flash handling on v_ model:
418 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
420 * shot = muzzle end (shot origin)
421 * shell = casings ejection point (must be on the right hand side of the gun)
422 * v_tuba.md3 - third person model
424 * shot = muzzle end (for muzzle flashes)
425 * g_tuba.md3 - pickup model
428 void CL_Weaponentity_Think()
430 float tb, v_shot_idx;
431 self.nextthink = time;
432 if (intermission_running)
433 self.frame = self.anim_idle_x;
434 if (self.owner.weaponentity != self)
436 if (self.weaponentity)
437 remove(self.weaponentity);
441 if (self.owner.deadflag != DEAD_NO)
444 if (self.weaponentity)
445 self.weaponentity.model = "";
448 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
450 self.cnt = self.owner.weapon;
451 self.dmg = self.owner.modelindex;
452 self.deadflag = self.owner.deadflag;
456 if (self.owner.weaponname != "")
458 // if there is a child entity, hide it until we're sure we use it
459 if (self.weaponentity)
460 self.weaponentity.model = "";
461 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
462 v_shot_idx = gettagindex(self, "shot"); // used later
464 if(qcweaponanimation)
466 self.angles = '0 0 0';
467 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
468 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
469 self.movedir_x += 32;
470 self.spawnorigin = self.movedir;
471 // oldorigin - not calculated here
475 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
476 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
477 animfile = fopen(animfilename, FILE_READ);
478 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
479 self.anim_fire1 = '0 1 0.01';
480 self.anim_fire2 = '1 1 0.01';
481 self.anim_idle = '2 1 0.01';
482 self.anim_reload = '3 1 0.01';
485 animparseerror = FALSE;
486 self.anim_fire1 = animparseline(animfile);
487 self.anim_fire2 = animparseline(animfile);
488 self.anim_idle = animparseline(animfile);
489 self.anim_reload = animparseline(animfile);
492 print("Parse error in ", animfilename, ", some player animations are broken\n");
495 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
496 // if we don't, this is a "real" animated model
497 if(gettagindex(self, "weapon"))
499 if (!self.weaponentity)
500 self.weaponentity = spawn();
501 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
502 setattachment(self.weaponentity, self, "weapon");
506 if(self.weaponentity)
507 remove(self.weaponentity);
508 self.weaponentity = world;
511 self.origin = '0 0 0';
512 self.angles = '0 0 0';
514 self.viewmodelforclient = world;
517 idx = gettagindex(self, "shot");
520 self.movedir = gettaginfo(self, idx);
524 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
525 self.movedir = '0 0 0';
528 idx = gettagindex(self, "shell");
531 self.spawnorigin = gettaginfo(self, idx);
535 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
536 self.spawnorigin = self.movedir;
541 self.oldorigin = '0 0 0';
545 if(self.weaponentity)
546 idx = gettagindex(self, "weapon");
548 idx = gettagindex(self, "handle");
551 self.oldorigin = self.movedir - gettaginfo(self, idx);
555 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");
556 self.oldorigin = '0 0 0'; // there is no way to recover from this
560 self.viewmodelforclient = self.owner;
566 if(self.weaponentity)
567 remove(self.weaponentity);
568 self.weaponentity = world;
569 self.movedir = '0 0 0';
570 self.spawnorigin = '0 0 0';
571 self.oldorigin = '0 0 0';
572 self.anim_fire1 = '0 1 0.01';
573 self.anim_fire2 = '0 1 0.01';
574 self.anim_idle = '0 1 0.01';
575 self.anim_reload = '0 1 0.01';
578 self.view_ofs = '0 0 0';
580 if(self.movedir_x >= 0)
584 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
585 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
587 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
588 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
590 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
592 // check if an instant weapon switch occurred
593 if (qcweaponanimation)
595 if (self.state == WS_READY)
597 self.angles = '0 0 0';
598 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
599 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
603 setorigin(self, self.view_ofs);
604 // reset animstate now
605 self.wframe = WFRAME_IDLE;
606 self.weapon_morph0time = 0;
607 self.weapon_morph1time = 0;
608 self.weapon_morph2time = 0;
609 self.weapon_morph3time = 0;
610 self.weapon_morph4time = 0;
611 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
614 tb = (self.effects & EF_TELEPORT_BIT);
615 self.effects = self.owner.effects & EFMASK_CHEAP;
616 self.effects &~= EF_LOWPRECISION;
617 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
618 self.effects &~= EF_TELEPORT_BIT;
621 if(self.owner.alpha != 0)
622 self.alpha = self.owner.alpha;
626 self.colormap = self.owner.colormap;
627 if (self.weaponentity)
629 self.weaponentity.effects = self.effects;
630 self.weaponentity.alpha = self.alpha;
631 self.weaponentity.colormap = self.colormap;
634 self.angles = '0 0 0';
637 if (self.state == WS_RAISE)
639 f = (self.owner.weapon_nextthink - time) * g_weaponspeedfactor / cvar("g_balance_weaponswitchdelay");
640 self.angles_x = -90 * f * f;
641 if (qcweaponanimation)
643 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
644 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
647 else if (self.state == WS_DROP)
649 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponspeedfactor / cvar("g_balance_weaponswitchdelay");
650 self.angles_x = -90 * f * f;
651 if (qcweaponanimation)
653 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
654 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
657 else if (self.state == WS_CLEAR)
660 self.angles_x = -90 * f * f;
661 if (qcweaponanimation)
663 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
664 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
667 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
669 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
670 f = 1 - pow(1 - f, 3);
671 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
672 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
674 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
676 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
677 f = 1 - pow(1 - f, 3);
678 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
679 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
681 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
683 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
684 f = 1 - pow(1 - f, 3);
685 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
686 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
688 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
690 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
691 f = 1 - pow(1 - f, 3);
692 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
693 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
695 else if (qcweaponanimation)
697 // begin a new idle morph
698 self.owner.weapon_morph0time = time;
699 self.owner.weapon_morph0angles = self.angles;
700 self.owner.weapon_morph0origin = self.origin;
708 // turn gun to the left to look at it
710 self.owner.weapon_morph1time = time + t * 0.2;
711 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
712 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');
713 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
715 self.owner.weapon_morph2time = time + t * 0.6;
716 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
717 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');
718 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
720 self.owner.weapon_morph3time = time + t;
721 self.owner.weapon_morph3angles = '0 0 0';
722 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');
723 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
727 // raise the gun a bit
729 self.owner.weapon_morph1time = time + t * 0.2;
730 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
731 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');
732 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
734 self.owner.weapon_morph2time = time + t * 0.5;
735 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
736 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');
737 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
739 self.owner.weapon_morph3time = time + t;
740 self.owner.weapon_morph3angles = '0 0 0';
741 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');
742 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
748 self.owner.weapon_morph1time = time + t * 0.3;
749 self.owner.weapon_morph1angles = randomvec() * 6;
750 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');
751 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
753 self.owner.weapon_morph2time = time + t * 0.7;
754 self.owner.weapon_morph2angles = randomvec() * 6;
755 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');
756 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
758 self.owner.weapon_morph3time = time + t;
759 self.owner.weapon_morph3angles = '0 0 0';
760 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');
761 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
765 // hold it mostly steady
766 t = random() * 6 + 4;
767 self.owner.weapon_morph1time = time + t * 0.2;
768 self.owner.weapon_morph1angles = randomvec() * 1;
769 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');
770 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
772 self.owner.weapon_morph2time = time + t * 0.5;
773 self.owner.weapon_morph2angles = randomvec() * 1;
774 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');
775 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
777 self.owner.weapon_morph3time = time + t * 0.7;
778 self.owner.weapon_morph3angles = randomvec() * 1;
779 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');
780 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
783 self.owner.weapon_morph4time = time + t;
784 self.owner.weapon_morph4angles = '0 0 0';
785 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');
786 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
790 // create or update the lasertarget entity
794 void CL_ExteriorWeaponentity_Think()
797 self.nextthink = time;
798 if (self.owner.exteriorweaponentity != self)
803 if (self.owner.deadflag != DEAD_NO)
808 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
810 self.cnt = self.owner.weapon;
811 self.dmg = self.owner.modelindex;
812 self.deadflag = self.owner.deadflag;
813 if (self.owner.weaponname != "")
814 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
818 if((tag_found = gettagindex(self.owner, "tag_weapon")))
820 self.tag_index = tag_found;
821 self.tag_entity = self.owner;
824 setattachment(self, self.owner, "bip01 r hand");
826 // if that didn't find a tag, hide the exterior weapon model
830 self.effects = self.owner.effects | EF_LOWPRECISION;
831 self.effects = self.effects & EFMASK_CHEAP; // eat performance
832 if(self.owner.alpha != 0)
833 self.alpha = self.owner.alpha;
837 self.colormap = self.owner.colormap;
840 // spawning weaponentity for client
841 void CL_SpawnWeaponentity()
843 self.weaponentity = spawn();
844 self.weaponentity.classname = "weaponentity";
845 self.weaponentity.solid = SOLID_NOT;
846 self.weaponentity.owner = self;
847 setmodel(self.weaponentity, ""); // precision set when changed
848 self.weaponentity.origin = '0 0 0';
849 self.weaponentity.angles = '0 0 0';
850 self.weaponentity.viewmodelforclient = self;
851 self.weaponentity.flags = 0;
852 self.weaponentity.think = CL_Weaponentity_Think;
853 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
854 self.weaponentity.nextthink = time;
856 self.exteriorweaponentity = spawn();
857 self.exteriorweaponentity.classname = "exteriorweaponentity";
858 self.exteriorweaponentity.solid = SOLID_NOT;
859 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
860 self.exteriorweaponentity.owner = self;
861 self.exteriorweaponentity.origin = '0 0 0';
862 self.exteriorweaponentity.angles = '0 0 0';
863 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
864 self.exteriorweaponentity.nextthink = time;
867 .float hasweapon_complain_spam;
869 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
871 local float weaponbit, f;
872 local entity oldself;
874 if(time < self.hasweapon_complain_spam)
877 self.hasweapon_complain_spam = time + 0.2;
879 if (wpn < WEP_FIRST || wpn > WEP_LAST)
882 sprint(self, "Invalid weapon\n");
885 weaponbit = W_WeaponBit(wpn);
886 if (cl.weapons & weaponbit)
890 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
898 f = weapon_action(wpn, WR_CHECKAMMO1);
899 f = f + weapon_action(wpn, WR_CHECKAMMO2);
905 if(clienttype(cl) == CLIENTTYPE_REAL)
907 play2(cl, "weapons/unavailable.wav");
908 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
918 // Report Proper Weapon Status / Modified Weapon Ownership Message
919 if(weaponsInMap & weaponbit)
921 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
923 if(cvar("g_showweaponspawns"))
928 e = get_weaponinfo(wpn);
931 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
933 if(e.classname == "droppedweapon")
935 if not(e.flags & FL_ITEM)
937 WaypointSprite_Spawn(
949 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
951 play2(cl, "weapons/unavailable.wav");
959 if (self.weapon != -1)
961 if (self.weaponentity)
963 self.weaponentity.state = WS_CLEAR;
964 self.weaponentity.effects = 0;
970 if (self.weaponentity)
971 self.weaponentity.state = WS_READY;
972 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
975 // Setup weapon for client (after this raise frame will be launched)
976 void weapon_setup(float windex)
979 qcweaponanimation = cvar("sv_qcweaponanimation");
980 e = get_weaponinfo(windex);
981 self.items &~= IT_AMMO;
982 self.items = self.items | e.items;
984 // the two weapon entities will notice this has changed and update their models
985 self.weapon = windex;
986 self.weaponname = e.mdl;
987 self.bulletcounter = 0;
990 // perform weapon to attack (weaponstate and attack_finished check is here)
992 float weapon_prepareattack(float secondary, float attacktime)
994 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
995 //if all players readied up and the countdown is running
996 if (cvar("sv_ready_restart_after_countdown"))
997 if(time < game_starttime || time < self.race_penalty) {
1001 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1002 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
1004 W_SwitchWeapon_Force(self, w_getbestweapon(self));
1008 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
1011 // do not even think about shooting if switching
1012 if(self.switchweapon != self.weapon)
1015 // don't fire if previous attack is not finished
1017 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
1019 // don't fire while changing weapon
1020 if (self.weaponentity.state != WS_READY)
1022 self.weaponentity.state = WS_INUSE;
1024 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1026 // if the weapon hasn't been firing continuously, reset the timer
1029 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
1031 ATTACK_FINISHED(self) = time;
1032 //dprint("resetting attack finished to ", ftos(time), "\n");
1034 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponSpeedFactor();
1036 self.bulletcounter += 1;
1037 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1041 void weapon_thinkf(float fr, float t, void() func)
1047 if(fr == WFRAME_DONTCHANGE)
1049 fr = self.weaponentity.wframe;
1050 restartanim = FALSE;
1052 else if (fr == WFRAME_IDLE)
1053 restartanim = FALSE;
1061 if (self.weaponentity)
1063 self.weaponentity.wframe = fr;
1064 if (qcweaponanimation)
1066 if (fr != WFRAME_IDLE)
1068 self.weapon_morph0time = time;
1069 self.weapon_morph0angles = self.weaponentity.angles;
1070 self.weapon_morph0origin = self.weaponentity.origin;
1072 self.weapon_morph1angles = '0 0 0';
1073 self.weapon_morph1time = time + t;
1074 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1075 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1077 self.weapon_morph2angles = '0 0 0';
1078 self.weapon_morph2time = time + t;
1079 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1080 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1082 self.weapon_morph3angles = '0 0 0';
1083 self.weapon_morph3time = time + t;
1084 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1085 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1087 self.weapon_morph4angles = '0 0 0';
1088 self.weapon_morph4time = time + t;
1089 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1090 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1092 if (fr == WFRAME_FIRE1)
1094 self.weapon_morph1angles = '5 0 0';
1095 self.weapon_morph1time = time + t * 0.1;
1096 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1097 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1098 self.weapon_morph4time = time + t + 1; // delay idle effect
1100 else if (fr == WFRAME_FIRE2)
1102 self.weapon_morph1angles = '10 0 0';
1103 self.weapon_morph1time = time + t * 0.1;
1104 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1105 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1106 self.weapon_morph4time = time + t + 1; // delay idle effect
1108 else if (fr == WFRAME_RELOAD)
1110 self.weapon_morph1time = time + t * 0.05;
1111 self.weapon_morph1angles = '-10 40 0';
1112 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1113 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1115 self.weapon_morph2time = time + t * 0.15;
1116 self.weapon_morph2angles = '-10 40 5';
1117 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1118 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1120 self.weapon_morph3time = time + t * 0.25;
1121 self.weapon_morph3angles = '-10 40 0';
1122 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1123 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1129 if (fr == WFRAME_IDLE)
1130 a = self.weaponentity.anim_idle;
1131 else if (fr == WFRAME_FIRE1)
1132 a = self.weaponentity.anim_fire1;
1133 else if (fr == WFRAME_FIRE2)
1134 a = self.weaponentity.anim_fire2;
1135 else if (fr == WFRAME_RELOAD)
1136 a = self.weaponentity.anim_reload;
1137 a_z *= g_weaponspeedfactor;
1138 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1143 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
1151 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1153 backtrace("Tried to override initial weapon think function - should this really happen?");
1156 t *= W_WeaponSpeedFactor();
1158 // VorteX: haste can be added here
1159 if (self.weapon_think == w_ready)
1161 self.weapon_nextthink = time;
1162 //dprint("started firing at ", ftos(time), "\n");
1164 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
1166 self.weapon_nextthink = time;
1167 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1169 self.weapon_nextthink = self.weapon_nextthink + t;
1170 self.weapon_think = func;
1171 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1175 if (!self.crouch) // shoot anim stands up, this looks bad
1178 anim = self.anim_shoot;
1179 anim_z = anim_y / t;
1180 setanim(self, anim, FALSE, TRUE, TRUE);
1184 void weapon_boblayer1(float spd, vector org)
1186 // VorteX: haste can be added here
1189 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1197 mdirection = normalize(mvelocity);
1198 mspeed = vlen(mvelocity);
1200 nstyle = cvar("g_projectiles_newton_style");
1203 // absolute velocity
1204 outvelocity = mvelocity;
1206 else if(nstyle == 1)
1208 // true Newtonian projectiles
1209 outvelocity = pvelocity + mvelocity;
1211 else if(nstyle == 2)
1213 // true Newtonian projectiles with automatic aim adjustment
1215 // solve: |outspeed * mdirection - pvelocity| = mspeed
1216 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1217 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1221 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1222 // velocity without mdirection component > mspeed
1223 // fire at smallest possible mspeed that works?
1224 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1227 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1229 outspeed = solution_y; // the larger one
1232 //outspeed = 0; // slowest possible shot
1233 outspeed = solution_x; // the real part (that is, the average!)
1234 //dprint("impossible shot, adjusting\n");
1237 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1238 outvelocity = mdirection * outspeed;
1240 else if(nstyle == 3)
1242 // pseudo-Newtonian:
1243 outspeed = mspeed + mdirection * pvelocity;
1244 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1245 outvelocity = mdirection * outspeed;
1247 else if(nstyle == 4)
1250 outspeed = mspeed + vlen(pvelocity);
1251 outvelocity = mdirection * outspeed;
1254 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1259 void W_SetupProjectileVelocity(entity missile)
1261 if(missile.owner == world)
1262 error("Unowned missile");
1264 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1267 void W_AttachToShotorg(entity flash, vector offset)
1271 flash.angles_z = random() * 360;
1272 if(qcweaponanimation)
1274 setorigin(flash, w_shotorg + w_shotdir * 50);
1275 flash.angles = vectoangles(w_shotdir);
1276 flash.angles_z = random() * 360;
1280 setattachment(flash, self.weaponentity, "shot");
1281 setorigin(flash, offset);
1284 copyentity(flash, xflash);
1286 flash.viewmodelforclient = self;
1288 if(self.weaponentity.oldorigin_x > 0)
1290 setattachment(xflash, self.exteriorweaponentity, "");
1291 setorigin(xflash, self.weaponentity.oldorigin + offset);
1295 setattachment(xflash, self.exteriorweaponentity, "shot");