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)
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);
138 trueaimpoint = trace_endpos;
140 W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
142 if(ent.weaponentity.movedir_x > 0)
144 vecs = ent.weaponentity.movedir;
150 if(debug_shotorg != '0 0 0')
151 vecs = debug_shotorg;
153 dv = v_right * vecs_y + v_up * vecs_z;
154 w_shotorg = ent.origin + ent.view_ofs + dv;
156 // now move the shotorg forward as much as requested if possible
157 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
158 w_shotorg = trace_endpos - v_forward * nudge;
159 // calculate the shotdir from the chosen shotorg
160 w_shotdir = normalize(trueaimpoint - w_shotorg);
163 // explanation of g_antilag:
164 // if client reports it was aiming at a player, and the serverside trace
165 // says it would miss, change the aim point to the player's new origin,
166 // but only if the shot at the player's new origin would hit of course
168 // FIXME: a much better method for bullet weapons would be to leave a
169 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
170 // ghost corresponding to this player's ping time, and if so it would do
171 // damage to the real player
173 if (ent.cursor_trace_ent) // client was aiming at someone
174 if (ent.cursor_trace_ent != ent) // just to make sure
175 if (ent.cursor_trace_ent.takedamage) // and that person is killable
176 if (ent.cursor_trace_ent.classname == "player") // and actually a player
177 if (cvar("g_antilag") == 1)
179 // verify that the shot would miss without antilag
180 // (avoids an issue where guns would always shoot at their origin)
181 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
182 if (!trace_ent.takedamage)
184 // verify that the shot would hit if altered
185 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
186 if (trace_ent == ent.cursor_trace_ent)
188 // verify that the shot would hit in the past
189 if(ent.antilag_debug)
190 antilag_takeback(ent.cursor_trace_ent, time - ent.antilag_debug);
192 antilag_takeback(ent.cursor_trace_ent, time - ANTILAG_LATENCY(ent));
194 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
195 antilag_restore(ent.cursor_trace_ent);
197 if(trace_ent == ent.cursor_trace_ent)
200 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
201 dprint("ANTILAG HIT for ", ent.netname, "\n");
205 // prydon cursor aimbot or odd network conditions
206 dprint("WARNING: antilag ghost trace for ", ent.netname, " failed!\n");
209 if(cvar("developer") >= 2)
211 vector v, vplus, vel;
213 v = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) ));
214 vplus = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) + 0.01));
215 vel = (vplus - v) * (1 / 0.01);
216 // solve: v + X * vel = closest to ent.origin + ent.view_ofs, v_forward axis
217 v -= (ent.origin + ent.view_ofs);
218 // solve: v + X * vel = closest to v_forward axis
219 // project into 2D by subtracting v_forward components:
220 v -= (v * v_forward) * v_forward;
221 vel -= (vel * v_forward) * v_forward;
222 // solve: v + X * vel = closest to origin
223 // (v + X * vel)^2 closest to 0
224 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
225 X = -(v * vel) / (vel * vel);
226 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(ent) + X), " instead of ", ftos(ANTILAG_LATENCY(ent)), "\n");
234 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
236 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
237 if (!trace_ent.takedamage)
239 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
240 if (trace_ent.takedamage && trace_ent.classname == "player")
244 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
246 w_shotdir = normalize(trace_ent.origin - w_shotorg);
250 else if(cvar("g_antilag") == 3) // client side hitscan
252 if (ent.cursor_trace_ent) // client was aiming at someone
253 if (ent.cursor_trace_ent != ent) // just to make sure
254 if (ent.cursor_trace_ent.takedamage) // and that person is killable
255 if (ent.cursor_trace_ent.classname == "player") // and actually a player
257 // verify that the shot would miss without antilag
258 // (avoids an issue where guns would always shoot at their origin)
259 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
260 if (!trace_ent.takedamage)
262 // verify that the shot would hit if altered
263 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
264 if (trace_ent == ent.cursor_trace_ent)
265 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
267 print("antilag fail\n");
274 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
277 ent.punchangle_x = recoil * -1;
281 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
284 if (ent.items & IT_STRENGTH)
286 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
289 void W_SetupShot(entity ent, float antilag, float recoil, string snd)
291 W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd);
294 void LaserTarget_Think()
301 // list of weapons that will use the laser, and the options that enable it
302 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
305 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
310 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
313 // rocket launcher isn't selected, so no laser target.
314 if(self.lasertarget != world)
316 remove(self.lasertarget);
317 self.lasertarget = world;
322 if(!self.lasertarget)
324 // we don't have a lasertarget entity, so spawn one
325 //bprint("create laser target\n");
326 e = self.lasertarget = spawn();
327 e.owner = self.owner; // Its owner is my owner
328 e.classname = "laser_target";
329 e.movetype = MOVETYPE_NOCLIP; // don't touch things
330 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
331 e.scale = 1.25; // make it larger
332 e.alpha = 0.25; // transparency
333 e.colormod = '255 0 0' * (1/255) * 8; // change colors
334 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
335 // make it dynamically glow
336 // you should avoid over-using this, as it can slow down the player's computer.
337 e.glow_color = 251; // red color
341 e = self.lasertarget;
343 // move the laser dot to where the player is looking
345 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
346 offset = '0 0 26' + v_right*3;
347 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
348 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
349 if(trace_plane_normal != '0 0 0')
350 e.angles = vectoangles(trace_plane_normal);
352 e.angles = vectoangles(v_forward);
355 float CL_Weaponentity_CustomizeEntityForClient()
357 self.viewmodelforclient = self.owner;
358 if(other.classname == "spectator")
359 if(other.enemy == self.owner)
360 self.viewmodelforclient = other;
364 float qcweaponanimation;
365 vector weapon_offset = '0 -10 0';
366 vector weapon_adjust = '10 0 -15';
367 .vector weapon_morph0origin;
368 .vector weapon_morph0angles;
369 .float weapon_morph0time;
370 .vector weapon_morph1origin;
371 .vector weapon_morph1angles;
372 .float weapon_morph1time;
373 .vector weapon_morph2origin;
374 .vector weapon_morph2angles;
375 .float weapon_morph2time;
376 .vector weapon_morph3origin;
377 .vector weapon_morph3angles;
378 .float weapon_morph3time;
379 .vector weapon_morph4origin;
380 .vector weapon_morph4angles;
381 .float weapon_morph4time;
383 #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)
388 * 1. simple animated model, muzzlr flash handling on h_ model:
389 * h_tuba.dpm, h_tuba.dpm.animinfo - invisible model controlling the animation
391 * shot = muzzle end (shot origin, also used for muzzle flashes)
392 * shell = casings ejection point (must be on the right hand side of the gun)
393 * weapon = attachment for v_tuba.md3
394 * v_tuba.md3 - first and third person model
395 * g_tuba.md3 - pickup model
397 * 2. fully animated model, muzzle flash handling on h_ model:
398 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
400 * shot = muzzle end (shot origin, also used for muzzle flashes)
401 * shell = casings ejection point (must be on the right hand side of the gun)
402 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
403 * v_tuba.md3 - third person model
404 * g_tuba.md3 - pickup model
406 * 3. fully animated model, muzzle flash handling on v_ model:
407 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
409 * shot = muzzle end (shot origin)
410 * shell = casings ejection point (must be on the right hand side of the gun)
411 * v_tuba.md3 - third person model
413 * shot = muzzle end (for muzzle flashes)
414 * g_tuba.md3 - pickup model
417 void CL_Weaponentity_Think()
419 float tb, v_shot_idx;
420 self.nextthink = time;
421 if (intermission_running)
422 self.frame = self.anim_idle_x;
423 if (self.owner.weaponentity != self)
425 if (self.weaponentity)
426 remove(self.weaponentity);
430 if (self.owner.deadflag != DEAD_NO)
433 if (self.weaponentity)
434 self.weaponentity.model = "";
437 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
439 self.cnt = self.owner.weapon;
440 self.dmg = self.owner.modelindex;
441 self.deadflag = self.owner.deadflag;
445 if (self.owner.weaponname != "")
447 // if there is a child entity, hide it until we're sure we use it
448 if (self.weaponentity)
449 self.weaponentity.model = "";
450 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
451 v_shot_idx = gettagindex(self, "shot"); // used later
453 if(qcweaponanimation)
455 self.angles = '0 0 0';
456 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
457 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
458 self.movedir_x += 32;
459 self.spawnorigin = self.movedir;
460 // oldorigin - not calculated here
464 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
465 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
466 animfile = fopen(animfilename, FILE_READ);
467 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
468 self.anim_fire1 = '0 1 0.01';
469 self.anim_fire2 = '1 1 0.01';
470 self.anim_idle = '2 1 0.01';
471 self.anim_reload = '3 1 0.01';
474 animparseerror = FALSE;
475 self.anim_fire1 = animparseline(animfile);
476 self.anim_fire2 = animparseline(animfile);
477 self.anim_idle = animparseline(animfile);
478 self.anim_reload = animparseline(animfile);
481 print("Parse error in ", animfilename, ", some player animations are broken\n");
484 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
485 // if we don't, this is a "real" animated model
486 if(gettagindex(self, "weapon"))
488 if (!self.weaponentity)
489 self.weaponentity = spawn();
490 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
491 setattachment(self.weaponentity, self, "weapon");
495 if(self.weaponentity)
496 remove(self.weaponentity);
497 self.weaponentity = world;
500 self.origin = '0 0 0';
501 self.angles = '0 0 0';
503 self.viewmodelforclient = world;
506 idx = gettagindex(self, "shot");
509 self.movedir = gettaginfo(self, idx);
513 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
514 self.movedir = '0 0 0';
517 idx = gettagindex(self, "shell");
520 self.spawnorigin = gettaginfo(self, idx);
524 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
525 self.spawnorigin = self.movedir;
530 self.oldorigin = '0 0 0';
534 if(self.weaponentity)
535 idx = gettagindex(self, "weapon");
537 idx = gettagindex(self, "handle");
540 self.oldorigin = self.movedir - gettaginfo(self, idx);
544 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");
545 self.oldorigin = '0 0 0'; // there is no way to recover from this
549 self.viewmodelforclient = self.owner;
555 if(self.weaponentity)
556 remove(self.weaponentity);
557 self.weaponentity = world;
558 self.movedir = '0 0 0';
559 self.spawnorigin = '0 0 0';
560 self.oldorigin = '0 0 0';
561 self.anim_fire1 = '0 1 0.01';
562 self.anim_fire2 = '0 1 0.01';
563 self.anim_idle = '0 1 0.01';
564 self.anim_reload = '0 1 0.01';
567 self.view_ofs = '0 0 0';
569 if(self.movedir_x >= 0)
573 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
574 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
576 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
577 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
579 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
581 // check if an instant weapon switch occurred
582 if (qcweaponanimation)
584 if (self.state == WS_READY)
586 self.angles = '0 0 0';
587 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
588 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
592 setorigin(self, self.view_ofs);
593 // reset animstate now
594 self.wframe = WFRAME_IDLE;
595 self.weapon_morph0time = 0;
596 self.weapon_morph1time = 0;
597 self.weapon_morph2time = 0;
598 self.weapon_morph3time = 0;
599 self.weapon_morph4time = 0;
600 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
603 tb = (self.effects & EF_TELEPORT_BIT);
604 self.effects = self.owner.effects & EFMASK_CHEAP;
605 self.effects &~= EF_LOWPRECISION;
606 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
607 self.effects &~= EF_TELEPORT_BIT;
610 if(self.owner.alpha != 0)
611 self.alpha = self.owner.alpha;
615 self.colormap = self.owner.colormap;
616 if (self.weaponentity)
618 self.weaponentity.effects = self.effects;
619 self.weaponentity.alpha = self.alpha;
620 self.weaponentity.colormap = self.colormap;
623 self.angles = '0 0 0';
626 if (self.state == WS_RAISE)
628 f = (self.owner.weapon_nextthink - time) * g_weaponspeedfactor / cvar("g_balance_weaponswitchdelay");
629 self.angles_x = -90 * f * f;
630 if (qcweaponanimation)
632 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
633 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
636 else if (self.state == WS_DROP)
638 f = 1 - (self.owner.weapon_nextthink - time) * g_weaponspeedfactor / cvar("g_balance_weaponswitchdelay");
639 self.angles_x = -90 * f * f;
640 if (qcweaponanimation)
642 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
643 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
646 else if (self.state == WS_CLEAR)
649 self.angles_x = -90 * f * f;
650 if (qcweaponanimation)
652 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
653 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
656 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
658 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
659 f = 1 - pow(1 - f, 3);
660 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
661 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
663 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
665 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
666 f = 1 - pow(1 - f, 3);
667 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
668 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
670 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
672 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
673 f = 1 - pow(1 - f, 3);
674 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
675 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
677 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
679 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
680 f = 1 - pow(1 - f, 3);
681 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
682 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
684 else if (qcweaponanimation)
686 // begin a new idle morph
687 self.owner.weapon_morph0time = time;
688 self.owner.weapon_morph0angles = self.angles;
689 self.owner.weapon_morph0origin = self.origin;
697 // turn gun to the left to look at it
699 self.owner.weapon_morph1time = time + t * 0.2;
700 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
701 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');
702 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
704 self.owner.weapon_morph2time = time + t * 0.6;
705 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
706 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');
707 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
709 self.owner.weapon_morph3time = time + t;
710 self.owner.weapon_morph3angles = '0 0 0';
711 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');
712 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
716 // raise the gun a bit
718 self.owner.weapon_morph1time = time + t * 0.2;
719 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
720 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');
721 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
723 self.owner.weapon_morph2time = time + t * 0.5;
724 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
725 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');
726 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
728 self.owner.weapon_morph3time = time + t;
729 self.owner.weapon_morph3angles = '0 0 0';
730 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');
731 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
737 self.owner.weapon_morph1time = time + t * 0.3;
738 self.owner.weapon_morph1angles = randomvec() * 6;
739 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');
740 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
742 self.owner.weapon_morph2time = time + t * 0.7;
743 self.owner.weapon_morph2angles = randomvec() * 6;
744 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');
745 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
747 self.owner.weapon_morph3time = time + t;
748 self.owner.weapon_morph3angles = '0 0 0';
749 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');
750 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
754 // hold it mostly steady
755 t = random() * 6 + 4;
756 self.owner.weapon_morph1time = time + t * 0.2;
757 self.owner.weapon_morph1angles = randomvec() * 1;
758 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');
759 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
761 self.owner.weapon_morph2time = time + t * 0.5;
762 self.owner.weapon_morph2angles = randomvec() * 1;
763 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');
764 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
766 self.owner.weapon_morph3time = time + t * 0.7;
767 self.owner.weapon_morph3angles = randomvec() * 1;
768 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');
769 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
772 self.owner.weapon_morph4time = time + t;
773 self.owner.weapon_morph4angles = '0 0 0';
774 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');
775 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
779 // create or update the lasertarget entity
783 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 | EF_LOWPRECISION;
820 self.effects = self.effects & EFMASK_CHEAP; // eat performance
821 if(self.owner.alpha != 0)
822 self.alpha = self.owner.alpha;
826 self.colormap = self.owner.colormap;
829 // spawning weaponentity for client
830 void CL_SpawnWeaponentity()
832 self.weaponentity = spawn();
833 self.weaponentity.classname = "weaponentity";
834 self.weaponentity.solid = SOLID_NOT;
835 self.weaponentity.owner = self;
836 setmodel(self.weaponentity, ""); // precision set when changed
837 self.weaponentity.origin = '0 0 0';
838 self.weaponentity.angles = '0 0 0';
839 self.weaponentity.viewmodelforclient = self;
840 self.weaponentity.flags = 0;
841 self.weaponentity.think = CL_Weaponentity_Think;
842 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
843 self.weaponentity.nextthink = time;
845 self.exteriorweaponentity = spawn();
846 self.exteriorweaponentity.classname = "exteriorweaponentity";
847 self.exteriorweaponentity.solid = SOLID_NOT;
848 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
849 self.exteriorweaponentity.owner = self;
850 self.exteriorweaponentity.origin = '0 0 0';
851 self.exteriorweaponentity.angles = '0 0 0';
852 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
853 self.exteriorweaponentity.nextthink = time;
856 .float hasweapon_complain_spam;
858 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
860 local float weaponbit, f;
861 local entity oldself;
863 if(time < self.hasweapon_complain_spam)
866 self.hasweapon_complain_spam = time + 0.2;
868 if (wpn < WEP_FIRST || wpn > WEP_LAST)
871 sprint(self, "Invalid weapon\n");
874 weaponbit = W_WeaponBit(wpn);
875 if (cl.weapons & weaponbit)
879 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
887 f = weapon_action(wpn, WR_CHECKAMMO1);
888 f = f + weapon_action(wpn, WR_CHECKAMMO2);
895 play2(cl, "weapons/unavailable.wav");
896 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
906 // Report Proper Weapon Status / Modified Weapon Ownership Message
907 if(weaponsInMap & weaponbit)
909 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
911 if(cvar("g_showweaponspawns"))
916 e = get_weaponinfo(wpn);
919 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
921 if(e.classname == "droppedweapon")
923 if not(e.flags & FL_ITEM)
925 WaypointSprite_Spawn(
937 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
939 play2(cl, "weapons/unavailable.wav");
947 if (self.weapon != -1)
949 if (self.weaponentity)
951 self.weaponentity.state = WS_CLEAR;
952 self.weaponentity.effects = 0;
958 if (self.weaponentity)
959 self.weaponentity.state = WS_READY;
960 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
963 // Setup weapon for client (after this raise frame will be launched)
964 void weapon_setup(float windex)
967 qcweaponanimation = cvar("sv_qcweaponanimation");
968 e = get_weaponinfo(windex);
969 self.items &~= IT_AMMO;
970 self.items = self.items | e.items;
972 // the two weapon entities will notice this has changed and update their models
973 self.weapon = windex;
974 self.weaponname = e.mdl;
975 self.bulletcounter = 0;
978 // perform weapon to attack (weaponstate and attack_finished check is here)
980 float weapon_prepareattack(float secondary, float attacktime)
982 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
983 //if all players readied up and the countdown is running
984 if (cvar("sv_ready_restart_after_countdown"))
985 if(time < game_starttime || time < self.race_penalty) {
989 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
990 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
992 W_SwitchWeapon_Force(self, w_getbestweapon(self));
996 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
999 // do not even think about shooting if switching
1000 if(self.switchweapon != self.weapon)
1003 // don't fire if previous attack is not finished
1005 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
1007 // don't fire while changing weapon
1008 if (self.weaponentity.state != WS_READY)
1010 self.weaponentity.state = WS_INUSE;
1012 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
1014 // if the weapon hasn't been firing continuously, reset the timer
1017 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
1019 ATTACK_FINISHED(self) = time;
1020 //dprint("resetting attack finished to ", ftos(time), "\n");
1022 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime * W_WeaponSpeedFactor();
1024 self.bulletcounter += 1;
1025 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1029 void weapon_thinkf(float fr, float t, void() func)
1035 if(fr == WFRAME_DONTCHANGE)
1037 fr = self.weaponentity.wframe;
1038 restartanim = FALSE;
1040 else if (fr == WFRAME_IDLE)
1041 restartanim = FALSE;
1049 if (self.weaponentity)
1051 self.weaponentity.wframe = fr;
1052 if (qcweaponanimation)
1054 if (fr != WFRAME_IDLE)
1056 self.weapon_morph0time = time;
1057 self.weapon_morph0angles = self.weaponentity.angles;
1058 self.weapon_morph0origin = self.weaponentity.origin;
1060 self.weapon_morph1angles = '0 0 0';
1061 self.weapon_morph1time = time + t;
1062 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1063 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1065 self.weapon_morph2angles = '0 0 0';
1066 self.weapon_morph2time = time + t;
1067 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1068 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1070 self.weapon_morph3angles = '0 0 0';
1071 self.weapon_morph3time = time + t;
1072 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1073 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1075 self.weapon_morph4angles = '0 0 0';
1076 self.weapon_morph4time = time + t;
1077 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1078 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1080 if (fr == WFRAME_FIRE1)
1082 self.weapon_morph1angles = '5 0 0';
1083 self.weapon_morph1time = time + t * 0.1;
1084 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1085 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1086 self.weapon_morph4time = time + t + 1; // delay idle effect
1088 else if (fr == WFRAME_FIRE2)
1090 self.weapon_morph1angles = '10 0 0';
1091 self.weapon_morph1time = time + t * 0.1;
1092 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1093 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1094 self.weapon_morph4time = time + t + 1; // delay idle effect
1096 else if (fr == WFRAME_RELOAD)
1098 self.weapon_morph1time = time + t * 0.05;
1099 self.weapon_morph1angles = '-10 40 0';
1100 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1101 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1103 self.weapon_morph2time = time + t * 0.15;
1104 self.weapon_morph2angles = '-10 40 5';
1105 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1106 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1108 self.weapon_morph3time = time + t * 0.25;
1109 self.weapon_morph3angles = '-10 40 0';
1110 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1111 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1117 if (fr == WFRAME_IDLE)
1118 a = self.weaponentity.anim_idle;
1119 else if (fr == WFRAME_FIRE1)
1120 a = self.weaponentity.anim_fire1;
1121 else if (fr == WFRAME_FIRE2)
1122 a = self.weaponentity.anim_fire2;
1123 else if (fr == WFRAME_RELOAD)
1124 a = self.weaponentity.anim_reload;
1125 a_z *= g_weaponspeedfactor;
1126 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1131 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
1139 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1141 backtrace("Tried to override initial weapon think function - should this really happen?");
1144 t *= W_WeaponSpeedFactor();
1146 // VorteX: haste can be added here
1147 if (self.weapon_think == w_ready)
1149 self.weapon_nextthink = time;
1150 //dprint("started firing at ", ftos(time), "\n");
1152 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
1154 self.weapon_nextthink = time;
1155 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1157 self.weapon_nextthink = self.weapon_nextthink + t;
1158 self.weapon_think = func;
1159 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1163 if (!self.crouch) // shoot anim stands up, this looks bad
1166 anim = self.anim_shoot;
1167 anim_z = anim_y / t;
1168 setanim(self, anim, FALSE, TRUE, TRUE);
1172 void weapon_boblayer1(float spd, vector org)
1174 // VorteX: haste can be added here
1177 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1185 mdirection = normalize(mvelocity);
1186 mspeed = vlen(mvelocity);
1188 nstyle = cvar("g_projectiles_newton_style");
1191 // absolute velocity
1192 outvelocity = mvelocity;
1194 else if(nstyle == 1)
1196 // true Newtonian projectiles
1197 outvelocity = pvelocity + mvelocity;
1199 else if(nstyle == 2)
1201 // true Newtonian projectiles with automatic aim adjustment
1203 // solve: |outspeed * mdirection - pvelocity| = mspeed
1204 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1205 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1209 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1210 // velocity without mdirection component > mspeed
1211 // fire at smallest possible mspeed that works?
1212 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1215 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1217 outspeed = solution_y; // the larger one
1220 //outspeed = 0; // slowest possible shot
1221 outspeed = solution_x; // the real part (that is, the average!)
1222 //dprint("impossible shot, adjusting\n");
1225 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1226 outvelocity = mdirection * outspeed;
1228 else if(nstyle == 3)
1230 // pseudo-Newtonian:
1231 outspeed = mspeed + mdirection * pvelocity;
1232 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1233 outvelocity = mdirection * outspeed;
1235 else if(nstyle == 4)
1238 outspeed = mspeed + vlen(pvelocity);
1239 outvelocity = mdirection * outspeed;
1242 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1247 void W_SetupProjectileVelocity(entity missile)
1249 if(missile.owner == world)
1250 error("Unowned missile");
1252 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1255 void W_AttachToShotorg(entity flash, vector offset)
1259 flash.angles_z = random() * 360;
1260 if(qcweaponanimation)
1262 setorigin(flash, w_shotorg + w_shotdir * 50);
1263 flash.angles = vectoangles(w_shotdir);
1264 flash.angles_z = random() * 360;
1268 setattachment(flash, self.weaponentity, "shot");
1269 setorigin(flash, offset);
1272 copyentity(flash, xflash);
1274 flash.viewmodelforclient = self;
1276 if(self.weaponentity.oldorigin_x > 0)
1278 setattachment(xflash, self.exteriorweaponentity, "");
1279 setorigin(xflash, self.weaponentity.oldorigin + offset);
1283 setattachment(xflash, self.exteriorweaponentity, "shot");