2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 void W_SwitchWeapon_Force(entity e, float w)
12 e.cnt = e.switchweapon;
18 // VorteX: static frame globals
19 float WFRAME_DONTCHANGE = -1;
20 float WFRAME_FIRE1 = 0;
21 float WFRAME_FIRE2 = 1;
22 float WFRAME_IDLE = 2;
23 float WFRAME_RELOAD = 3;
26 void(float fr, float t, void() func) weapon_thinkf;
28 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
31 ret_x = screenright * v;
33 ret_z = screenforward * v;
37 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
40 vector mi, ma, thisv, myv, ret;
42 myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
44 // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
46 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
49 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
50 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
51 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
52 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
55 if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
56 if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
57 //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
66 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
67 ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
68 ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
69 ret_z = thisv_z - myv_z;
73 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
79 if(player.hitplotfh >= 0)
81 lag = ANTILAG_LATENCY(player);
84 if(clienttype(player) != CLIENTTYPE_REAL)
85 lag = 0; // only antilag for clients
87 org = player.origin + player.view_ofs;
88 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
89 if(trace_ent.flags & FL_CLIENT)
91 antilag_takeback(trace_ent, time - lag);
92 hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
93 antilag_restore(trace_ent);
94 fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
95 //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
103 // this function calculates w_shotorg and w_shotdir based on the weapon model
104 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
105 // make sure you call makevectors first (FIXME?)
106 void W_SetupShot(entity ent, float antilag, float recoil, string snd)
108 float nudge = 1; // added to traceline target and subtracted from result
109 local vector trueaimpoint;
110 local float oldsolid;
112 oldsolid = ent.dphitcontentsmask;
113 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
114 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, ent);
115 trueaimpoint = trace_endpos;
117 W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
119 if(ent.weaponentity.movedir_x > 0)
121 vecs = ent.weaponentity.movedir;
127 if(debug_shotorg != '0 0 0')
128 vecs = debug_shotorg;
130 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
132 // now move the shotorg forward as much as requested if possible
133 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
134 w_shotorg = trace_endpos - v_forward * nudge;
135 // calculate the shotdir from the chosen shotorg
136 w_shotdir = normalize(trueaimpoint - w_shotorg);
139 // explanation of g_antilag:
140 // if client reports it was aiming at a player, and the serverside trace
141 // says it would miss, change the aim point to the player's new origin,
142 // but only if the shot at the player's new origin would hit of course
144 // FIXME: a much better method for bullet weapons would be to leave a
145 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
146 // ghost corresponding to this player's ping time, and if so it would do
147 // damage to the real player
149 if (ent.cursor_trace_ent) // client was aiming at someone
150 if (ent.cursor_trace_ent != ent) // just to make sure
151 if (ent.cursor_trace_ent.takedamage) // and that person is killable
152 if (ent.cursor_trace_ent.classname == "player") // and actually a player
153 if (cvar("g_antilag") == 1)
155 // verify that the shot would miss without antilag
156 // (avoids an issue where guns would always shoot at their origin)
157 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
158 if (!trace_ent.takedamage)
160 // verify that the shot would hit if altered
161 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
162 if (trace_ent == ent.cursor_trace_ent)
164 // verify that the shot would hit in the past
165 if(ent.antilag_debug)
166 antilag_takeback(ent.cursor_trace_ent, time - ent.antilag_debug);
168 antilag_takeback(ent.cursor_trace_ent, time - ANTILAG_LATENCY(ent));
170 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
171 antilag_restore(ent.cursor_trace_ent);
173 if(trace_ent == ent.cursor_trace_ent)
176 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
177 dprint("ANTILAG HIT for ", ent.netname, "\n");
181 // prydon cursor aimbot or odd network conditions
182 dprint("WARNING: antilag ghost trace for ", ent.netname, " failed!\n");
185 if(cvar("developer") >= 2)
187 vector v, vplus, vel;
189 v = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) ));
190 vplus = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) + 0.01));
191 vel = (vplus - v) * (1 / 0.01);
192 // solve: v + X * vel = closest to ent.origin + ent.view_ofs, v_forward axis
193 v -= (ent.origin + ent.view_ofs);
194 // solve: v + X * vel = closest to v_forward axis
195 // project into 2D by subtracting v_forward components:
196 v -= (v * v_forward) * v_forward;
197 vel -= (vel * v_forward) * v_forward;
198 // solve: v + X * vel = closest to origin
199 // (v + X * vel)^2 closest to 0
200 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
201 X = -(v * vel) / (vel * vel);
202 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(ent) + X), " instead of ", ftos(ANTILAG_LATENCY(ent)), "\n");
210 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
212 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
213 if (!trace_ent.takedamage)
215 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
216 if (trace_ent.takedamage && trace_ent.classname == "player")
220 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
222 w_shotdir = normalize(trace_ent.origin - w_shotorg);
226 else if(cvar("g_antilag") == 3) // client side hitscan
228 if (ent.cursor_trace_ent) // client was aiming at someone
229 if (ent.cursor_trace_ent != ent) // just to make sure
230 if (ent.cursor_trace_ent.takedamage) // and that person is killable
231 if (ent.cursor_trace_ent.classname == "player") // and actually a player
233 // verify that the shot would miss without antilag
234 // (avoids an issue where guns would always shoot at their origin)
235 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
236 if (!trace_ent.takedamage)
238 // verify that the shot would hit if altered
239 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
240 if (trace_ent == ent.cursor_trace_ent)
241 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
243 print("antilag fail\n");
250 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
253 ent.punchangle_x = recoil * -1;
257 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
260 if (ent.items & IT_STRENGTH)
262 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
265 void LaserTarget_Think()
272 // list of weapons that will use the laser, and the options that enable it
273 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
276 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
281 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
284 // rocket launcher isn't selected, so no laser target.
285 if(self.lasertarget != world)
287 remove(self.lasertarget);
288 self.lasertarget = world;
293 if(!self.lasertarget)
295 // we don't have a lasertarget entity, so spawn one
296 //bprint("create laser target\n");
297 e = self.lasertarget = spawn();
298 e.owner = self.owner; // Its owner is my owner
299 e.classname = "laser_target";
300 e.movetype = MOVETYPE_NOCLIP; // don't touch things
301 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
302 e.scale = 1.25; // make it larger
303 e.alpha = 0.25; // transparency
304 e.colormod = '255 0 0' * (1/255) * 8; // change colors
305 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
306 // make it dynamically glow
307 // you should avoid over-using this, as it can slow down the player's computer.
308 e.glow_color = 251; // red color
312 e = self.lasertarget;
314 // move the laser dot to where the player is looking
316 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
317 offset = '0 0 26' + v_right*3;
318 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
319 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
320 if(trace_plane_normal != '0 0 0')
321 e.angles = vectoangles(trace_plane_normal);
323 e.angles = vectoangles(v_forward);
326 float CL_Weaponentity_CustomizeEntityForClient()
328 self.viewmodelforclient = self.owner;
329 if(other.classname == "spectator")
330 if(other.enemy == self.owner)
331 self.viewmodelforclient = other;
335 float qcweaponanimation;
336 vector weapon_offset = '0 -10 0';
337 vector weapon_adjust = '10 0 -15';
338 .vector weapon_morph0origin;
339 .vector weapon_morph0angles;
340 .float weapon_morph0time;
341 .vector weapon_morph1origin;
342 .vector weapon_morph1angles;
343 .float weapon_morph1time;
344 .vector weapon_morph2origin;
345 .vector weapon_morph2angles;
346 .float weapon_morph2time;
347 .vector weapon_morph3origin;
348 .vector weapon_morph3angles;
349 .float weapon_morph3time;
350 .vector weapon_morph4origin;
351 .vector weapon_morph4angles;
352 .float weapon_morph4time;
354 #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)
359 * 1. simple animated model, muzzlr flash handling on h_ model:
360 * h_tuba.dpm, h_tuba.dpm.animinfo - invisible model controlling the animation
362 * shot = muzzle end (shot origin, also used for muzzle flashes)
363 * shell = casings ejection point (must be on the right hand side of the gun)
364 * weapon = attachment for v_tuba.md3
365 * v_tuba.md3 - first and third person model
366 * g_tuba.md3 - pickup model
368 * 2. fully animated model, muzzle flash handling on h_ model:
369 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
371 * shot = muzzle end (shot origin, also used for muzzle flashes)
372 * shell = casings ejection point (must be on the right hand side of the gun)
373 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
374 * v_tuba.md3 - third person model
375 * g_tuba.md3 - pickup model
377 * 3. fully animated model, muzzle flash handling on v_ model:
378 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
380 * shot = muzzle end (shot origin)
381 * shell = casings ejection point (must be on the right hand side of the gun)
382 * v_tuba.md3 - third person model
384 * shot = muzzle end (for muzzle flashes)
385 * g_tuba.md3 - pickup model
388 void CL_Weaponentity_Think()
390 float tb, v_shot_idx;
391 self.nextthink = time;
392 if (intermission_running)
393 self.frame = self.anim_idle_x;
394 if (self.owner.weaponentity != self)
396 if (self.weaponentity)
397 remove(self.weaponentity);
401 if (self.owner.deadflag != DEAD_NO)
404 if (self.weaponentity)
405 self.weaponentity.model = "";
408 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
410 self.cnt = self.owner.weapon;
411 self.dmg = self.owner.modelindex;
412 self.deadflag = self.owner.deadflag;
416 if (self.owner.weaponname != "")
418 // if there is a child entity, hide it until we're sure we use it
419 if (self.weaponentity)
420 self.weaponentity.model = "";
421 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
422 v_shot_idx = gettagindex(self, "shot"); // used later
424 if(qcweaponanimation)
426 self.angles = '0 0 0';
427 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
428 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
429 self.movedir_x += 32;
430 self.spawnorigin = self.movedir;
431 // oldorigin - not calculated here
435 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
436 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
437 animfile = fopen(animfilename, FILE_READ);
438 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
439 self.anim_fire1 = '0 1 0.01';
440 self.anim_fire2 = '1 1 0.01';
441 self.anim_idle = '2 1 0.01';
442 self.anim_reload = '3 1 0.01';
445 animparseerror = FALSE;
446 self.anim_fire1 = animparseline(animfile);
447 self.anim_fire2 = animparseline(animfile);
448 self.anim_idle = animparseline(animfile);
449 self.anim_reload = animparseline(animfile);
452 print("Parse error in ", animfilename, ", some player animations are broken\n");
455 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
456 // if we don't, this is a "real" animated model
457 if(gettagindex(self, "weapon"))
459 if (!self.weaponentity)
460 self.weaponentity = spawn();
461 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
462 setattachment(self.weaponentity, self, "weapon");
466 if(self.weaponentity)
467 remove(self.weaponentity);
468 self.weaponentity = world;
471 self.origin = '0 0 0';
472 self.angles = '0 0 0';
474 self.viewmodelforclient = world;
477 idx = gettagindex(self, "shot");
480 self.movedir = gettaginfo(self, idx);
484 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
485 self.movedir = '0 0 0';
488 idx = gettagindex(self, "shell");
491 self.spawnorigin = gettaginfo(self, idx);
495 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
496 self.spawnorigin = self.movedir;
501 self.oldorigin = '0 0 0';
505 if(self.weaponentity)
506 idx = gettagindex(self, "weapon");
508 idx = gettagindex(self, "handle");
511 self.oldorigin = self.movedir - gettaginfo(self, idx);
515 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");
516 self.oldorigin = '0 0 0'; // there is no way to recover from this
520 self.viewmodelforclient = self.owner;
526 if(self.weaponentity)
527 remove(self.weaponentity);
528 self.weaponentity = world;
529 self.movedir = '0 0 0';
530 self.spawnorigin = '0 0 0';
531 self.oldorigin = '0 0 0';
532 self.anim_fire1 = '0 1 0.01';
533 self.anim_fire2 = '0 1 0.01';
534 self.anim_idle = '0 1 0.01';
535 self.anim_reload = '0 1 0.01';
538 self.view_ofs = '0 0 0';
540 if(self.movedir_x >= 0)
544 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
545 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
547 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
548 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
550 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
552 // check if an instant weapon switch occurred
553 if (qcweaponanimation)
555 if (self.state == WS_READY)
557 self.angles = '0 0 0';
558 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
559 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
563 setorigin(self, self.view_ofs);
564 // reset animstate now
565 self.wframe = WFRAME_IDLE;
566 self.weapon_morph0time = 0;
567 self.weapon_morph1time = 0;
568 self.weapon_morph2time = 0;
569 self.weapon_morph3time = 0;
570 self.weapon_morph4time = 0;
571 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
574 tb = (self.effects & EF_TELEPORT_BIT);
575 self.effects = self.owner.effects & EFMASK_CHEAP;
576 self.effects &~= EF_LOWPRECISION;
577 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
578 self.effects &~= EF_TELEPORT_BIT;
581 if(self.owner.alpha != 0)
582 self.alpha = self.owner.alpha;
586 self.colormap = self.owner.colormap;
587 if (self.weaponentity)
589 self.weaponentity.effects = self.effects;
590 self.weaponentity.alpha = self.alpha;
591 self.weaponentity.colormap = self.colormap;
594 self.angles = '0 0 0';
597 if (self.state == WS_RAISE)
599 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
600 self.angles_x = -90 * f * f;
601 if (qcweaponanimation)
603 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
604 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
607 else if (self.state == WS_DROP)
609 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
610 self.angles_x = -90 * f * f;
611 if (qcweaponanimation)
613 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
614 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
617 else if (self.state == WS_CLEAR)
620 self.angles_x = -90 * f * f;
621 if (qcweaponanimation)
623 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
624 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
627 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
629 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
630 f = 1 - pow(1 - f, 3);
631 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
632 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
634 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
636 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
637 f = 1 - pow(1 - f, 3);
638 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
639 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
641 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
643 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
644 f = 1 - pow(1 - f, 3);
645 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
646 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
648 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
650 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
651 f = 1 - pow(1 - f, 3);
652 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
653 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
655 else if (qcweaponanimation)
657 // begin a new idle morph
658 self.owner.weapon_morph0time = time;
659 self.owner.weapon_morph0angles = self.angles;
660 self.owner.weapon_morph0origin = self.origin;
668 // turn gun to the left to look at it
670 self.owner.weapon_morph1time = time + t * 0.2;
671 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
672 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');
673 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
675 self.owner.weapon_morph2time = time + t * 0.6;
676 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
677 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');
678 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
680 self.owner.weapon_morph3time = time + t;
681 self.owner.weapon_morph3angles = '0 0 0';
682 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');
683 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
687 // raise the gun a bit
689 self.owner.weapon_morph1time = time + t * 0.2;
690 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
691 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');
692 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
694 self.owner.weapon_morph2time = time + t * 0.5;
695 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
696 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');
697 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
699 self.owner.weapon_morph3time = time + t;
700 self.owner.weapon_morph3angles = '0 0 0';
701 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');
702 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
708 self.owner.weapon_morph1time = time + t * 0.3;
709 self.owner.weapon_morph1angles = randomvec() * 6;
710 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');
711 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
713 self.owner.weapon_morph2time = time + t * 0.7;
714 self.owner.weapon_morph2angles = randomvec() * 6;
715 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');
716 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
718 self.owner.weapon_morph3time = time + t;
719 self.owner.weapon_morph3angles = '0 0 0';
720 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');
721 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
725 // hold it mostly steady
726 t = random() * 6 + 4;
727 self.owner.weapon_morph1time = time + t * 0.2;
728 self.owner.weapon_morph1angles = randomvec() * 1;
729 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');
730 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
732 self.owner.weapon_morph2time = time + t * 0.5;
733 self.owner.weapon_morph2angles = randomvec() * 1;
734 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');
735 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
737 self.owner.weapon_morph3time = time + t * 0.7;
738 self.owner.weapon_morph3angles = randomvec() * 1;
739 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');
740 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
743 self.owner.weapon_morph4time = time + t;
744 self.owner.weapon_morph4angles = '0 0 0';
745 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');
746 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
750 // create or update the lasertarget entity
754 void CL_ExteriorWeaponentity_Think()
757 self.nextthink = time;
758 if (self.owner.exteriorweaponentity != self)
763 if (self.owner.deadflag != DEAD_NO)
768 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
770 self.cnt = self.owner.weapon;
771 self.dmg = self.owner.modelindex;
772 self.deadflag = self.owner.deadflag;
773 if (self.owner.weaponname != "")
774 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
778 if((tag_found = gettagindex(self.owner, "tag_weapon")))
780 self.tag_index = tag_found;
781 self.tag_entity = self.owner;
784 setattachment(self, self.owner, "bip01 r hand");
786 // if that didn't find a tag, hide the exterior weapon model
790 self.effects = self.owner.effects | EF_LOWPRECISION;
791 self.effects = self.effects & EFMASK_CHEAP; // eat performance
792 if(self.owner.alpha != 0)
793 self.alpha = self.owner.alpha;
797 self.colormap = self.owner.colormap;
800 // spawning weaponentity for client
801 void CL_SpawnWeaponentity()
803 self.weaponentity = spawn();
804 self.weaponentity.classname = "weaponentity";
805 self.weaponentity.solid = SOLID_NOT;
806 self.weaponentity.owner = self;
807 setmodel(self.weaponentity, ""); // precision set when changed
808 self.weaponentity.origin = '0 0 0';
809 self.weaponentity.angles = '0 0 0';
810 self.weaponentity.viewmodelforclient = self;
811 self.weaponentity.flags = 0;
812 self.weaponentity.think = CL_Weaponentity_Think;
813 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
814 self.weaponentity.nextthink = time;
816 self.exteriorweaponentity = spawn();
817 self.exteriorweaponentity.classname = "exteriorweaponentity";
818 self.exteriorweaponentity.solid = SOLID_NOT;
819 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
820 self.exteriorweaponentity.owner = self;
821 self.exteriorweaponentity.origin = '0 0 0';
822 self.exteriorweaponentity.angles = '0 0 0';
823 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
824 self.exteriorweaponentity.nextthink = time;
827 .float hasweapon_complain_spam;
829 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
831 local float weaponbit, f;
832 local entity oldself;
834 if(time < self.hasweapon_complain_spam)
837 self.hasweapon_complain_spam = time + 0.2;
839 if (wpn < WEP_FIRST || wpn > WEP_LAST)
842 sprint(self, "Invalid weapon\n");
845 weaponbit = W_WeaponBit(wpn);
846 if (cl.weapons & weaponbit)
850 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
858 f = weapon_action(wpn, WR_CHECKAMMO1);
859 f = f + weapon_action(wpn, WR_CHECKAMMO2);
865 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
874 // Report Proper Weapon Status / Modified Weapon Ownership Message
875 if(weaponsInMap & weaponbit)
877 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
879 if(cvar("g_showweaponspawns"))
884 e = get_weaponinfo(wpn);
887 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
889 if(e.classname == "droppedweapon")
891 if not(e.flags & FL_ITEM)
893 WaypointSprite_Spawn(
905 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
913 if (self.weapon != -1)
915 if (self.weaponentity)
917 self.weaponentity.state = WS_CLEAR;
918 self.weaponentity.effects = 0;
924 if (self.weaponentity)
925 self.weaponentity.state = WS_READY;
926 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
929 // Setup weapon for client (after this raise frame will be launched)
930 void weapon_setup(float windex)
933 qcweaponanimation = cvar("sv_qcweaponanimation");
934 e = get_weaponinfo(windex);
935 self.items &~= IT_AMMO;
936 self.items = self.items | e.items;
938 // the two weapon entities will notice this has changed and update their models
939 self.weapon = windex;
940 self.weaponname = e.mdl;
941 self.bulletcounter = 0;
944 // perform weapon to attack (weaponstate and attack_finished check is here)
946 float weapon_prepareattack(float secondary, float attacktime)
948 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
949 //if all players readied up and the countdown is running
950 if (cvar("sv_ready_restart_after_countdown"))
951 if(time < game_starttime || time < self.race_penalty) {
955 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
956 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
958 W_SwitchWeapon_Force(self, w_getbestweapon(self));
962 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
965 // do not even think about shooting if switching
966 if(self.switchweapon != self.weapon)
969 // don't fire if previous attack is not finished
971 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
973 // don't fire while changing weapon
974 if (self.weaponentity.state != WS_READY)
976 self.weaponentity.state = WS_INUSE;
978 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
980 // if the weapon hasn't been firing continuously, reset the timer
983 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
985 ATTACK_FINISHED(self) = time;
986 //dprint("resetting attack finished to ", ftos(time), "\n");
988 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
990 self.bulletcounter += 1;
991 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
995 void weapon_thinkf(float fr, float t, void() func)
1001 if(fr == WFRAME_DONTCHANGE)
1003 fr = self.weaponentity.wframe;
1004 restartanim = FALSE;
1006 else if (fr == WFRAME_IDLE)
1007 restartanim = FALSE;
1015 if (self.weaponentity)
1017 self.weaponentity.wframe = fr;
1018 if (qcweaponanimation)
1020 if (fr != WFRAME_IDLE)
1022 self.weapon_morph0time = time;
1023 self.weapon_morph0angles = self.weaponentity.angles;
1024 self.weapon_morph0origin = self.weaponentity.origin;
1026 self.weapon_morph1angles = '0 0 0';
1027 self.weapon_morph1time = time + t;
1028 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1029 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1031 self.weapon_morph2angles = '0 0 0';
1032 self.weapon_morph2time = time + t;
1033 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1034 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1036 self.weapon_morph3angles = '0 0 0';
1037 self.weapon_morph3time = time + t;
1038 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1039 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1041 self.weapon_morph4angles = '0 0 0';
1042 self.weapon_morph4time = time + t;
1043 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1044 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1046 if (fr == WFRAME_FIRE1)
1048 self.weapon_morph1angles = '5 0 0';
1049 self.weapon_morph1time = time + t * 0.1;
1050 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1051 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1052 self.weapon_morph4time = time + t + 1; // delay idle effect
1054 else if (fr == WFRAME_FIRE2)
1056 self.weapon_morph1angles = '10 0 0';
1057 self.weapon_morph1time = time + t * 0.1;
1058 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1059 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1060 self.weapon_morph4time = time + t + 1; // delay idle effect
1062 else if (fr == WFRAME_RELOAD)
1064 self.weapon_morph1time = time + t * 0.05;
1065 self.weapon_morph1angles = '-10 40 0';
1066 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1067 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1069 self.weapon_morph2time = time + t * 0.15;
1070 self.weapon_morph2angles = '-10 40 5';
1071 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1072 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1074 self.weapon_morph3time = time + t * 0.25;
1075 self.weapon_morph3angles = '-10 40 0';
1076 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1077 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1083 if (fr == WFRAME_IDLE)
1084 a = self.weaponentity.anim_idle;
1085 else if (fr == WFRAME_FIRE1)
1086 a = self.weaponentity.anim_fire1;
1087 else if (fr == WFRAME_FIRE2)
1088 a = self.weaponentity.anim_fire2;
1089 else if (fr == WFRAME_RELOAD)
1090 a = self.weaponentity.anim_reload;
1091 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1096 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
1104 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1106 backtrace("Tried to override initial weapon think function - should this really happen?");
1111 if(self.runes & RUNE_SPEED)
1113 if(self.runes & CURSE_SLOW)
1114 t = t * cvar("g_balance_rune_speed_combo_atkrate");
1116 t = t * cvar("g_balance_rune_speed_atkrate");
1118 else if(self.runes & CURSE_SLOW)
1120 t = t * cvar("g_balance_curse_slow_atkrate");
1124 // VorteX: haste can be added here
1125 if (self.weapon_think == w_ready)
1127 self.weapon_nextthink = time;
1128 //dprint("started firing at ", ftos(time), "\n");
1130 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
1132 self.weapon_nextthink = time;
1133 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1135 self.weapon_nextthink = self.weapon_nextthink + t;
1136 self.weapon_think = func;
1137 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1141 if (!self.crouch) // shoot anim stands up, this looks bad
1144 anim = self.anim_shoot;
1145 anim_z = anim_y / t;
1146 setanim(self, anim, FALSE, TRUE, TRUE);
1150 void weapon_boblayer1(float spd, vector org)
1152 // VorteX: haste can be added here
1155 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1163 mdirection = normalize(mvelocity);
1164 mspeed = vlen(mvelocity);
1166 nstyle = cvar("g_projectiles_newton_style");
1169 // absolute velocity
1170 outvelocity = mvelocity;
1172 else if(nstyle == 1)
1174 // true Newtonian projectiles
1175 outvelocity = pvelocity + mvelocity;
1177 else if(nstyle == 2)
1179 // true Newtonian projectiles with automatic aim adjustment
1181 // solve: |outspeed * mdirection - pvelocity| = mspeed
1182 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1183 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1187 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1188 // velocity without mdirection component > mspeed
1189 // fire at smallest possible mspeed that works?
1190 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1193 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1195 outspeed = solution_y; // the larger one
1198 //outspeed = 0; // slowest possible shot
1199 outspeed = solution_x; // the real part (that is, the average!)
1200 //dprint("impossible shot, adjusting\n");
1203 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1204 outvelocity = mdirection * outspeed;
1206 else if(nstyle == 3)
1208 // pseudo-Newtonian:
1209 outspeed = mspeed + mdirection * pvelocity;
1210 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1211 outvelocity = mdirection * outspeed;
1213 else if(nstyle == 4)
1216 outspeed = mspeed + vlen(pvelocity);
1217 outvelocity = mdirection * outspeed;
1220 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1225 void W_SetupProjectileVelocity(entity missile)
1227 if(missile.owner == world)
1228 error("Unowned missile");
1230 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1233 void W_AttachToShotorg(entity flash, vector offset)
1237 flash.angles_z = random() * 360;
1238 if(qcweaponanimation)
1240 setorigin(flash, w_shotorg + w_shotdir * 50);
1241 flash.angles = vectoangles(w_shotdir);
1242 flash.angles_z = random() * 360;
1246 setattachment(flash, self.weaponentity, "shot");
1247 setorigin(flash, offset);
1250 copyentity(flash, xflash);
1252 flash.viewmodelforclient = self;
1254 if(self.weaponentity.oldorigin_x > 0)
1256 setattachment(xflash, self.exteriorweaponentity, "");
1257 setorigin(xflash, self.weaponentity.oldorigin + offset);
1261 setattachment(xflash, self.exteriorweaponentity, "shot");