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_ProjectileSize(entity ent, vector mi, vector ma, 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 dv = v_right * vecs_y + v_up * vecs_z;
131 w_shotorg = ent.origin + ent.view_ofs + dv;
133 // now move the shotorg forward as much as requested if possible
134 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
135 w_shotorg = trace_endpos - v_forward * nudge;
136 // calculate the shotdir from the chosen shotorg
137 w_shotdir = normalize(trueaimpoint - w_shotorg);
140 // explanation of g_antilag:
141 // if client reports it was aiming at a player, and the serverside trace
142 // says it would miss, change the aim point to the player's new origin,
143 // but only if the shot at the player's new origin would hit of course
145 // FIXME: a much better method for bullet weapons would be to leave a
146 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
147 // ghost corresponding to this player's ping time, and if so it would do
148 // damage to the real player
150 if (ent.cursor_trace_ent) // client was aiming at someone
151 if (ent.cursor_trace_ent != ent) // just to make sure
152 if (ent.cursor_trace_ent.takedamage) // and that person is killable
153 if (ent.cursor_trace_ent.classname == "player") // and actually a player
154 if (cvar("g_antilag") == 1)
156 // verify that the shot would miss without antilag
157 // (avoids an issue where guns would always shoot at their origin)
158 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
159 if (!trace_ent.takedamage)
161 // verify that the shot would hit if altered
162 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
163 if (trace_ent == ent.cursor_trace_ent)
165 // verify that the shot would hit in the past
166 if(ent.antilag_debug)
167 antilag_takeback(ent.cursor_trace_ent, time - ent.antilag_debug);
169 antilag_takeback(ent.cursor_trace_ent, time - ANTILAG_LATENCY(ent));
171 traceline(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
172 antilag_restore(ent.cursor_trace_ent);
174 if(trace_ent == ent.cursor_trace_ent)
177 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
178 dprint("ANTILAG HIT for ", ent.netname, "\n");
182 // prydon cursor aimbot or odd network conditions
183 dprint("WARNING: antilag ghost trace for ", ent.netname, " failed!\n");
186 if(cvar("developer") >= 2)
188 vector v, vplus, vel;
190 v = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) ));
191 vplus = antilag_takebackorigin(ent.cursor_trace_ent, time - (ANTILAG_LATENCY(ent) + 0.01));
192 vel = (vplus - v) * (1 / 0.01);
193 // solve: v + X * vel = closest to ent.origin + ent.view_ofs, v_forward axis
194 v -= (ent.origin + ent.view_ofs);
195 // solve: v + X * vel = closest to v_forward axis
196 // project into 2D by subtracting v_forward components:
197 v -= (v * v_forward) * v_forward;
198 vel -= (vel * v_forward) * v_forward;
199 // solve: v + X * vel = closest to origin
200 // (v + X * vel)^2 closest to 0
201 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
202 X = -(v * vel) / (vel * vel);
203 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(ent) + X), " instead of ", ftos(ANTILAG_LATENCY(ent)), "\n");
211 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
213 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
214 if (!trace_ent.takedamage)
216 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
217 if (trace_ent.takedamage && trace_ent.classname == "player")
221 traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
223 w_shotdir = normalize(trace_ent.origin - w_shotorg);
227 else if(cvar("g_antilag") == 3) // client side hitscan
229 if (ent.cursor_trace_ent) // client was aiming at someone
230 if (ent.cursor_trace_ent != ent) // just to make sure
231 if (ent.cursor_trace_ent.takedamage) // and that person is killable
232 if (ent.cursor_trace_ent.classname == "player") // and actually a player
234 // verify that the shot would miss without antilag
235 // (avoids an issue where guns would always shoot at their origin)
236 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, ent);
237 if (!trace_ent.takedamage)
239 // verify that the shot would hit if altered
240 traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
241 if (trace_ent == ent.cursor_trace_ent)
242 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
244 print("antilag fail\n");
251 ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
254 ent.punchangle_x = recoil * -1;
258 sound (ent, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
261 if (ent.items & IT_STRENGTH)
263 sound (ent, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
266 void W_SetupShot(entity ent, float antilag, float recoil, string snd)
268 W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd);
271 void LaserTarget_Think()
278 // list of weapons that will use the laser, and the options that enable it
279 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
282 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
287 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
290 // rocket launcher isn't selected, so no laser target.
291 if(self.lasertarget != world)
293 remove(self.lasertarget);
294 self.lasertarget = world;
299 if(!self.lasertarget)
301 // we don't have a lasertarget entity, so spawn one
302 //bprint("create laser target\n");
303 e = self.lasertarget = spawn();
304 e.owner = self.owner; // Its owner is my owner
305 e.classname = "laser_target";
306 e.movetype = MOVETYPE_NOCLIP; // don't touch things
307 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
308 e.scale = 1.25; // make it larger
309 e.alpha = 0.25; // transparency
310 e.colormod = '255 0 0' * (1/255) * 8; // change colors
311 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
312 // make it dynamically glow
313 // you should avoid over-using this, as it can slow down the player's computer.
314 e.glow_color = 251; // red color
318 e = self.lasertarget;
320 // move the laser dot to where the player is looking
322 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
323 offset = '0 0 26' + v_right*3;
324 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
325 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
326 if(trace_plane_normal != '0 0 0')
327 e.angles = vectoangles(trace_plane_normal);
329 e.angles = vectoangles(v_forward);
332 float CL_Weaponentity_CustomizeEntityForClient()
334 self.viewmodelforclient = self.owner;
335 if(other.classname == "spectator")
336 if(other.enemy == self.owner)
337 self.viewmodelforclient = other;
341 float qcweaponanimation;
342 vector weapon_offset = '0 -10 0';
343 vector weapon_adjust = '10 0 -15';
344 .vector weapon_morph0origin;
345 .vector weapon_morph0angles;
346 .float weapon_morph0time;
347 .vector weapon_morph1origin;
348 .vector weapon_morph1angles;
349 .float weapon_morph1time;
350 .vector weapon_morph2origin;
351 .vector weapon_morph2angles;
352 .float weapon_morph2time;
353 .vector weapon_morph3origin;
354 .vector weapon_morph3angles;
355 .float weapon_morph3time;
356 .vector weapon_morph4origin;
357 .vector weapon_morph4angles;
358 .float weapon_morph4time;
360 #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)
365 * 1. simple animated model, muzzlr flash handling on h_ model:
366 * h_tuba.dpm, h_tuba.dpm.animinfo - invisible model controlling the animation
368 * shot = muzzle end (shot origin, also used for muzzle flashes)
369 * shell = casings ejection point (must be on the right hand side of the gun)
370 * weapon = attachment for v_tuba.md3
371 * v_tuba.md3 - first and third person model
372 * g_tuba.md3 - pickup model
374 * 2. fully animated model, muzzle flash handling on h_ model:
375 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
377 * shot = muzzle end (shot origin, also used for muzzle flashes)
378 * shell = casings ejection point (must be on the right hand side of the gun)
379 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
380 * v_tuba.md3 - third person model
381 * g_tuba.md3 - pickup model
383 * 3. fully animated model, muzzle flash handling on v_ model:
384 * h_tuba.dpm, h_tuba.dpm.animinfo - animated first person model
386 * shot = muzzle end (shot origin)
387 * shell = casings ejection point (must be on the right hand side of the gun)
388 * v_tuba.md3 - third person model
390 * shot = muzzle end (for muzzle flashes)
391 * g_tuba.md3 - pickup model
394 void CL_Weaponentity_Think()
396 float tb, v_shot_idx;
397 self.nextthink = time;
398 if (intermission_running)
399 self.frame = self.anim_idle_x;
400 if (self.owner.weaponentity != self)
402 if (self.weaponentity)
403 remove(self.weaponentity);
407 if (self.owner.deadflag != DEAD_NO)
410 if (self.weaponentity)
411 self.weaponentity.model = "";
414 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
416 self.cnt = self.owner.weapon;
417 self.dmg = self.owner.modelindex;
418 self.deadflag = self.owner.deadflag;
422 if (self.owner.weaponname != "")
424 // if there is a child entity, hide it until we're sure we use it
425 if (self.weaponentity)
426 self.weaponentity.model = "";
427 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
428 v_shot_idx = gettagindex(self, "shot"); // used later
430 if(qcweaponanimation)
432 self.angles = '0 0 0';
433 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
434 self.movedir = weapon_offset_x * v_forward - weapon_offset_y * v_right + weapon_offset_z * v_up + weapon_adjust;
435 self.movedir_x += 32;
436 self.spawnorigin = self.movedir;
437 // oldorigin - not calculated here
441 setmodel(self, strcat("models/weapons/h_", self.owner.weaponname, ".dpm")); // precision set below
442 animfilename = strcat("models/weapons/h_", self.owner.weaponname, ".dpm.animinfo");
443 animfile = fopen(animfilename, FILE_READ);
444 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
445 self.anim_fire1 = '0 1 0.01';
446 self.anim_fire2 = '1 1 0.01';
447 self.anim_idle = '2 1 0.01';
448 self.anim_reload = '3 1 0.01';
451 animparseerror = FALSE;
452 self.anim_fire1 = animparseline(animfile);
453 self.anim_fire2 = animparseline(animfile);
454 self.anim_idle = animparseline(animfile);
455 self.anim_reload = animparseline(animfile);
458 print("Parse error in ", animfilename, ", some player animations are broken\n");
461 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
462 // if we don't, this is a "real" animated model
463 if(gettagindex(self, "weapon"))
465 if (!self.weaponentity)
466 self.weaponentity = spawn();
467 setmodel(self.weaponentity, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision does not matter
468 setattachment(self.weaponentity, self, "weapon");
472 if(self.weaponentity)
473 remove(self.weaponentity);
474 self.weaponentity = world;
477 self.origin = '0 0 0';
478 self.angles = '0 0 0';
480 self.viewmodelforclient = world;
483 idx = gettagindex(self, "shot");
486 self.movedir = gettaginfo(self, idx);
490 print("WARNING: weapon model ", self.model, " does not support the 'shot' tag, will display shots TOTALLY wrong\n");
491 self.movedir = '0 0 0';
494 idx = gettagindex(self, "shell");
497 self.spawnorigin = gettaginfo(self, idx);
501 print("WARNING: weapon model ", self.model, " does not support the 'shell' tag, will display casings wrong\n");
502 self.spawnorigin = self.movedir;
507 self.oldorigin = '0 0 0';
511 if(self.weaponentity)
512 idx = gettagindex(self, "weapon");
514 idx = gettagindex(self, "handle");
517 self.oldorigin = self.movedir - gettaginfo(self, idx);
521 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");
522 self.oldorigin = '0 0 0'; // there is no way to recover from this
526 self.viewmodelforclient = self.owner;
532 if(self.weaponentity)
533 remove(self.weaponentity);
534 self.weaponentity = world;
535 self.movedir = '0 0 0';
536 self.spawnorigin = '0 0 0';
537 self.oldorigin = '0 0 0';
538 self.anim_fire1 = '0 1 0.01';
539 self.anim_fire2 = '0 1 0.01';
540 self.anim_idle = '0 1 0.01';
541 self.anim_reload = '0 1 0.01';
544 self.view_ofs = '0 0 0';
546 if(self.movedir_x >= 0)
550 self.movedir = shotorg_adjust(v0, FALSE, FALSE);
551 self.view_ofs = shotorg_adjust(v0, FALSE, TRUE) - v0;
553 self.owner.stat_shotorg = compressShotOrigin(self.movedir);
554 self.movedir = decompressShotOrigin(self.owner.stat_shotorg); // make them match perfectly
556 self.spawnorigin += self.view_ofs; // offset the casings origin by the same amount
558 // check if an instant weapon switch occurred
559 if (qcweaponanimation)
561 if (self.state == WS_READY)
563 self.angles = '0 0 0';
564 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
565 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
569 setorigin(self, self.view_ofs);
570 // reset animstate now
571 self.wframe = WFRAME_IDLE;
572 self.weapon_morph0time = 0;
573 self.weapon_morph1time = 0;
574 self.weapon_morph2time = 0;
575 self.weapon_morph3time = 0;
576 self.weapon_morph4time = 0;
577 setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
580 tb = (self.effects & EF_TELEPORT_BIT);
581 self.effects = self.owner.effects & EFMASK_CHEAP;
582 self.effects &~= EF_LOWPRECISION;
583 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
584 self.effects &~= EF_TELEPORT_BIT;
587 if(self.owner.alpha != 0)
588 self.alpha = self.owner.alpha;
592 self.colormap = self.owner.colormap;
593 if (self.weaponentity)
595 self.weaponentity.effects = self.effects;
596 self.weaponentity.alpha = self.alpha;
597 self.weaponentity.colormap = self.colormap;
600 self.angles = '0 0 0';
603 if (self.state == WS_RAISE)
605 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
606 self.angles_x = -90 * f * f;
607 if (qcweaponanimation)
609 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
610 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
613 else if (self.state == WS_DROP)
615 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
616 self.angles_x = -90 * f * f;
617 if (qcweaponanimation)
619 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
620 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
623 else if (self.state == WS_CLEAR)
626 self.angles_x = -90 * f * f;
627 if (qcweaponanimation)
629 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0' + self.angles_z * '0 0 1');
630 setorigin(self, QCWEAPONANIMATION_ORIGIN(self));
633 else if (qcweaponanimation && time < self.owner.weapon_morph1time)
635 f = (time - self.owner.weapon_morph0time) / (self.owner.weapon_morph1time - self.owner.weapon_morph0time);
636 f = 1 - pow(1 - f, 3);
637 self.angles = self.owner.weapon_morph0angles * (1 - f) + self.owner.weapon_morph1angles * f;
638 setorigin(self, self.owner.weapon_morph0origin * (1 - f) + self.owner.weapon_morph1origin * f);
640 else if (qcweaponanimation && time < self.owner.weapon_morph2time)
642 f = (time - self.owner.weapon_morph1time) / (self.owner.weapon_morph2time - self.owner.weapon_morph1time);
643 f = 1 - pow(1 - f, 3);
644 self.angles = self.owner.weapon_morph1angles * (1 - f) + self.owner.weapon_morph2angles * f;
645 setorigin(self, self.owner.weapon_morph1origin * (1 - f) + self.owner.weapon_morph2origin * f);
647 else if (qcweaponanimation && time < self.owner.weapon_morph3time)
649 f = (time - self.owner.weapon_morph2time) / (self.owner.weapon_morph3time - self.owner.weapon_morph2time);
650 f = 1 - pow(1 - f, 3);
651 self.angles = self.owner.weapon_morph2angles * (1 - f) + self.owner.weapon_morph3angles * f;
652 setorigin(self, self.owner.weapon_morph2origin * (1 - f) + self.owner.weapon_morph3origin * f);
654 else if (qcweaponanimation && time < self.owner.weapon_morph4time)
656 f = (time - self.owner.weapon_morph3time) / (self.owner.weapon_morph4time - self.owner.weapon_morph3time);
657 f = 1 - pow(1 - f, 3);
658 self.angles = self.owner.weapon_morph3angles * (1 - f) + self.owner.weapon_morph4angles * f;
659 setorigin(self, self.owner.weapon_morph3origin * (1 - f) + self.owner.weapon_morph4origin * f);
661 else if (qcweaponanimation)
663 // begin a new idle morph
664 self.owner.weapon_morph0time = time;
665 self.owner.weapon_morph0angles = self.angles;
666 self.owner.weapon_morph0origin = self.origin;
674 // turn gun to the left to look at it
676 self.owner.weapon_morph1time = time + t * 0.2;
677 self.owner.weapon_morph1angles = randomvec() * 3 + '-5 30 0';
678 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');
679 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
681 self.owner.weapon_morph2time = time + t * 0.6;
682 self.owner.weapon_morph2angles = randomvec() * 3 + '-5 30 0';
683 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');
684 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
686 self.owner.weapon_morph3time = time + t;
687 self.owner.weapon_morph3angles = '0 0 0';
688 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');
689 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
693 // raise the gun a bit
695 self.owner.weapon_morph1time = time + t * 0.2;
696 self.owner.weapon_morph1angles = randomvec() * 3 + '30 -10 0';
697 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');
698 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
700 self.owner.weapon_morph2time = time + t * 0.5;
701 self.owner.weapon_morph2angles = randomvec() * 3 + '30 -10 5';
702 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');
703 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
705 self.owner.weapon_morph3time = time + t;
706 self.owner.weapon_morph3angles = '0 0 0';
707 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');
708 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
714 self.owner.weapon_morph1time = time + t * 0.3;
715 self.owner.weapon_morph1angles = randomvec() * 6;
716 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');
717 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
719 self.owner.weapon_morph2time = time + t * 0.7;
720 self.owner.weapon_morph2angles = randomvec() * 6;
721 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');
722 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
724 self.owner.weapon_morph3time = time + t;
725 self.owner.weapon_morph3angles = '0 0 0';
726 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');
727 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
731 // hold it mostly steady
732 t = random() * 6 + 4;
733 self.owner.weapon_morph1time = time + t * 0.2;
734 self.owner.weapon_morph1angles = randomvec() * 1;
735 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');
736 self.owner.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self);
738 self.owner.weapon_morph2time = time + t * 0.5;
739 self.owner.weapon_morph2angles = randomvec() * 1;
740 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');
741 self.owner.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self);
743 self.owner.weapon_morph3time = time + t * 0.7;
744 self.owner.weapon_morph3angles = randomvec() * 1;
745 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');
746 self.owner.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self);
749 self.owner.weapon_morph4time = time + t;
750 self.owner.weapon_morph4angles = '0 0 0';
751 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');
752 self.owner.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self);
756 // create or update the lasertarget entity
760 void CL_ExteriorWeaponentity_Think()
763 self.nextthink = time;
764 if (self.owner.exteriorweaponentity != self)
769 if (self.owner.deadflag != DEAD_NO)
774 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
776 self.cnt = self.owner.weapon;
777 self.dmg = self.owner.modelindex;
778 self.deadflag = self.owner.deadflag;
779 if (self.owner.weaponname != "")
780 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
784 if((tag_found = gettagindex(self.owner, "tag_weapon")))
786 self.tag_index = tag_found;
787 self.tag_entity = self.owner;
790 setattachment(self, self.owner, "bip01 r hand");
792 // if that didn't find a tag, hide the exterior weapon model
796 self.effects = self.owner.effects | EF_LOWPRECISION;
797 self.effects = self.effects & EFMASK_CHEAP; // eat performance
798 if(self.owner.alpha != 0)
799 self.alpha = self.owner.alpha;
803 self.colormap = self.owner.colormap;
806 // spawning weaponentity for client
807 void CL_SpawnWeaponentity()
809 self.weaponentity = spawn();
810 self.weaponentity.classname = "weaponentity";
811 self.weaponentity.solid = SOLID_NOT;
812 self.weaponentity.owner = self;
813 setmodel(self.weaponentity, ""); // precision set when changed
814 self.weaponentity.origin = '0 0 0';
815 self.weaponentity.angles = '0 0 0';
816 self.weaponentity.viewmodelforclient = self;
817 self.weaponentity.flags = 0;
818 self.weaponentity.think = CL_Weaponentity_Think;
819 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
820 self.weaponentity.nextthink = time;
822 self.exteriorweaponentity = spawn();
823 self.exteriorweaponentity.classname = "exteriorweaponentity";
824 self.exteriorweaponentity.solid = SOLID_NOT;
825 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
826 self.exteriorweaponentity.owner = self;
827 self.exteriorweaponentity.origin = '0 0 0';
828 self.exteriorweaponentity.angles = '0 0 0';
829 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
830 self.exteriorweaponentity.nextthink = time;
833 .float hasweapon_complain_spam;
835 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
837 local float weaponbit, f;
838 local entity oldself;
840 if(time < self.hasweapon_complain_spam)
843 self.hasweapon_complain_spam = time + 0.2;
845 if (wpn < WEP_FIRST || wpn > WEP_LAST)
848 sprint(self, "Invalid weapon\n");
851 weaponbit = W_WeaponBit(wpn);
852 if (cl.weapons & weaponbit)
856 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
864 f = weapon_action(wpn, WR_CHECKAMMO1);
865 f = f + weapon_action(wpn, WR_CHECKAMMO2);
871 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
880 // Report Proper Weapon Status / Modified Weapon Ownership Message
881 if(weaponsInMap & weaponbit)
883 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
885 if(cvar("g_showweaponspawns"))
890 e = get_weaponinfo(wpn);
893 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
895 if(e.classname == "droppedweapon")
897 if not(e.flags & FL_ITEM)
899 WaypointSprite_Spawn(
911 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
919 if (self.weapon != -1)
921 if (self.weaponentity)
923 self.weaponentity.state = WS_CLEAR;
924 self.weaponentity.effects = 0;
930 if (self.weaponentity)
931 self.weaponentity.state = WS_READY;
932 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
935 // Setup weapon for client (after this raise frame will be launched)
936 void weapon_setup(float windex)
939 qcweaponanimation = cvar("sv_qcweaponanimation");
940 e = get_weaponinfo(windex);
941 self.items &~= IT_AMMO;
942 self.items = self.items | e.items;
944 // the two weapon entities will notice this has changed and update their models
945 self.weapon = windex;
946 self.weaponname = e.mdl;
947 self.bulletcounter = 0;
950 // perform weapon to attack (weaponstate and attack_finished check is here)
952 float weapon_prepareattack(float secondary, float attacktime)
954 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
955 //if all players readied up and the countdown is running
956 if (cvar("sv_ready_restart_after_countdown"))
957 if(time < game_starttime || time < self.race_penalty) {
961 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
962 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
964 W_SwitchWeapon_Force(self, w_getbestweapon(self));
968 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
971 // do not even think about shooting if switching
972 if(self.switchweapon != self.weapon)
975 // don't fire if previous attack is not finished
977 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
979 // don't fire while changing weapon
980 if (self.weaponentity.state != WS_READY)
982 self.weaponentity.state = WS_INUSE;
984 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
986 // if the weapon hasn't been firing continuously, reset the timer
989 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
991 ATTACK_FINISHED(self) = time;
992 //dprint("resetting attack finished to ", ftos(time), "\n");
994 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
996 self.bulletcounter += 1;
997 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
1001 void weapon_thinkf(float fr, float t, void() func)
1007 if(fr == WFRAME_DONTCHANGE)
1009 fr = self.weaponentity.wframe;
1010 restartanim = FALSE;
1012 else if (fr == WFRAME_IDLE)
1013 restartanim = FALSE;
1021 if (self.weaponentity)
1023 self.weaponentity.wframe = fr;
1024 if (qcweaponanimation)
1026 if (fr != WFRAME_IDLE)
1028 self.weapon_morph0time = time;
1029 self.weapon_morph0angles = self.weaponentity.angles;
1030 self.weapon_morph0origin = self.weaponentity.origin;
1032 self.weapon_morph1angles = '0 0 0';
1033 self.weapon_morph1time = time + t;
1034 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1035 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1037 self.weapon_morph2angles = '0 0 0';
1038 self.weapon_morph2time = time + t;
1039 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1040 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1042 self.weapon_morph3angles = '0 0 0';
1043 self.weapon_morph3time = time + t;
1044 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1045 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1047 self.weapon_morph4angles = '0 0 0';
1048 self.weapon_morph4time = time + t;
1049 makevectors(self.weapon_morph4angles_x * '-1 0 0' + self.weapon_morph4angles_y * '0 1 0' + self.weapon_morph4angles_z * '0 0 1');
1050 self.weapon_morph4origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1052 if (fr == WFRAME_FIRE1)
1054 self.weapon_morph1angles = '5 0 0';
1055 self.weapon_morph1time = time + t * 0.1;
1056 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1057 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1058 self.weapon_morph4time = time + t + 1; // delay idle effect
1060 else if (fr == WFRAME_FIRE2)
1062 self.weapon_morph1angles = '10 0 0';
1063 self.weapon_morph1time = time + t * 0.1;
1064 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1065 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1066 self.weapon_morph4time = time + t + 1; // delay idle effect
1068 else if (fr == WFRAME_RELOAD)
1070 self.weapon_morph1time = time + t * 0.05;
1071 self.weapon_morph1angles = '-10 40 0';
1072 makevectors(self.weapon_morph1angles_x * '-1 0 0' + self.weapon_morph1angles_y * '0 1 0' + self.weapon_morph1angles_z * '0 0 1');
1073 self.weapon_morph1origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1075 self.weapon_morph2time = time + t * 0.15;
1076 self.weapon_morph2angles = '-10 40 5';
1077 makevectors(self.weapon_morph2angles_x * '-1 0 0' + self.weapon_morph2angles_y * '0 1 0' + self.weapon_morph2angles_z * '0 0 1');
1078 self.weapon_morph2origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1080 self.weapon_morph3time = time + t * 0.25;
1081 self.weapon_morph3angles = '-10 40 0';
1082 makevectors(self.weapon_morph3angles_x * '-1 0 0' + self.weapon_morph3angles_y * '0 1 0' + self.weapon_morph3angles_z * '0 0 1');
1083 self.weapon_morph3origin = QCWEAPONANIMATION_ORIGIN(self.weaponentity);
1089 if (fr == WFRAME_IDLE)
1090 a = self.weaponentity.anim_idle;
1091 else if (fr == WFRAME_FIRE1)
1092 a = self.weaponentity.anim_fire1;
1093 else if (fr == WFRAME_FIRE2)
1094 a = self.weaponentity.anim_fire2;
1095 else if (fr == WFRAME_RELOAD)
1096 a = self.weaponentity.anim_reload;
1097 setanim(self.weaponentity, a, restartanim == FALSE, restartanim, restartanim);
1102 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
1110 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
1112 backtrace("Tried to override initial weapon think function - should this really happen?");
1117 if(self.runes & RUNE_SPEED)
1119 if(self.runes & CURSE_SLOW)
1120 t = t * cvar("g_balance_rune_speed_combo_atkrate");
1122 t = t * cvar("g_balance_rune_speed_atkrate");
1124 else if(self.runes & CURSE_SLOW)
1126 t = t * cvar("g_balance_curse_slow_atkrate");
1130 // VorteX: haste can be added here
1131 if (self.weapon_think == w_ready)
1133 self.weapon_nextthink = time;
1134 //dprint("started firing at ", ftos(time), "\n");
1136 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
1138 self.weapon_nextthink = time;
1139 //dprint("reset weapon animation timer at ", ftos(time), "\n");
1141 self.weapon_nextthink = self.weapon_nextthink + t;
1142 self.weapon_think = func;
1143 //dprint("next ", ftos(self.weapon_nextthink), "\n");
1147 if (!self.crouch) // shoot anim stands up, this looks bad
1150 anim = self.anim_shoot;
1151 anim_z = anim_y / t;
1152 setanim(self, anim, FALSE, TRUE, TRUE);
1156 void weapon_boblayer1(float spd, vector org)
1158 // VorteX: haste can be added here
1161 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
1169 mdirection = normalize(mvelocity);
1170 mspeed = vlen(mvelocity);
1172 nstyle = cvar("g_projectiles_newton_style");
1175 // absolute velocity
1176 outvelocity = mvelocity;
1178 else if(nstyle == 1)
1180 // true Newtonian projectiles
1181 outvelocity = pvelocity + mvelocity;
1183 else if(nstyle == 2)
1185 // true Newtonian projectiles with automatic aim adjustment
1187 // solve: |outspeed * mdirection - pvelocity| = mspeed
1188 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
1189 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
1193 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
1194 // velocity without mdirection component > mspeed
1195 // fire at smallest possible mspeed that works?
1196 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
1199 solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
1201 outspeed = solution_y; // the larger one
1204 //outspeed = 0; // slowest possible shot
1205 outspeed = solution_x; // the real part (that is, the average!)
1206 //dprint("impossible shot, adjusting\n");
1209 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1210 outvelocity = mdirection * outspeed;
1212 else if(nstyle == 3)
1214 // pseudo-Newtonian:
1215 outspeed = mspeed + mdirection * pvelocity;
1216 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
1217 outvelocity = mdirection * outspeed;
1219 else if(nstyle == 4)
1222 outspeed = mspeed + vlen(pvelocity);
1223 outvelocity = mdirection * outspeed;
1226 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
1231 void W_SetupProjectileVelocity(entity missile)
1233 if(missile.owner == world)
1234 error("Unowned missile");
1236 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
1239 void W_AttachToShotorg(entity flash, vector offset)
1243 flash.angles_z = random() * 360;
1244 if(qcweaponanimation)
1246 setorigin(flash, w_shotorg + w_shotdir * 50);
1247 flash.angles = vectoangles(w_shotdir);
1248 flash.angles_z = random() * 360;
1252 setattachment(flash, self.weaponentity, "shot");
1253 setorigin(flash, offset);
1256 copyentity(flash, xflash);
1258 flash.viewmodelforclient = self;
1260 if(self.weaponentity.oldorigin_x > 0)
1262 setattachment(xflash, self.exteriorweaponentity, "");
1263 setorigin(xflash, self.weaponentity.oldorigin + offset);
1267 setattachment(xflash, self.exteriorweaponentity, "shot");