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_FIRE1 = 0;
20 float WFRAME_FIRE2 = 1;
21 float WFRAME_IDLE = 2;
22 float WFRAME_RELOAD = 3;
24 void(float fr, float t, void() func) weapon_thinkf;
29 // this function calculates w_shotorg and w_shotdir based on the weapon model
30 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
31 // make sure you call makevectors first (FIXME?)
32 void W_SetupShot(entity ent, vector vecs, float antilag, float recoil, string snd)
34 float nudge = 1; // added to traceline target and subtracted from result
35 local vector trueaimpoint;
37 oldsolid = self.dphitcontentsmask;
38 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
39 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
40 trueaimpoint = trace_endpos;
42 if (cvar("g_shootfromeye"))
43 w_shotorg = ent.origin + ent.view_ofs;
44 else if (cvar("g_shootfromcenter"))
45 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
47 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
48 // now move the shotorg forward as much as requested if possible
49 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
50 w_shotorg = trace_endpos - v_forward * nudge;
51 // calculate the shotdir from the chosen shotorg
52 w_shotdir = normalize(trueaimpoint - w_shotorg);
55 // explanation of g_antilag:
56 // if client reports it was aiming at a player, and the serverside trace
57 // says it would miss, change the aim point to the player's new origin,
58 // but only if the shot at the player's new origin would hit of course
60 // FIXME: a much better method for bullet weapons would be to leave a
61 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
62 // ghost corresponding to this player's ping time, and if so it would do
63 // damage to the real player
65 if (self.cursor_trace_ent) // client was aiming at someone
66 if (self.cursor_trace_ent != self) // just to make sure
67 if (self.cursor_trace_ent.takedamage) // and that person is killable
68 if (self.cursor_trace_ent.classname == "player") // and actually a player
69 if (cvar("g_antilag") == 1)
71 // verify that the shot would miss without antilag
72 // (avoids an issue where guns would always shoot at their origin)
73 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
74 if (!trace_ent.takedamage)
76 // verify that the shot would hit if altered
77 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
78 if (trace_ent == self.cursor_trace_ent)
80 // verify that the shot would hit in the past
81 if(self.antilag_debug)
82 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
84 antilag_takeback(self.cursor_trace_ent, time - ANTILAG_LATENCY(self));
86 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
87 antilag_restore(self.cursor_trace_ent);
89 if(trace_ent == self.cursor_trace_ent)
92 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
93 dprint("ANTILAG HIT for ", self.netname, "\n");
97 // prydon cursor aimbot or odd network conditions
98 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
101 if(cvar("developer") >= 2)
103 vector v, vplus, vel;
105 v = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self) ));
106 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self) + 0.01));
107 vel = (vplus - v) * (1 / 0.01);
108 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
109 v -= (self.origin + self.view_ofs);
110 // solve: v + X * vel = closest to v_forward axis
111 // project into 2D by subtracting v_forward components:
112 v -= (v * v_forward) * v_forward;
113 vel -= (vel * v_forward) * v_forward;
114 // solve: v + X * vel = closest to origin
115 // (v + X * vel)^2 closest to 0
116 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
117 X = -(v * vel) / (vel * vel);
118 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(self) + X), " instead of ", ftos(ANTILAG_LATENCY(self)), "\n");
126 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
128 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
129 if (!trace_ent.takedamage)
131 traceline_antilag_force (self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self, ANTILAG_LATENCY(self));
132 if (trace_ent.takedamage && trace_ent.classname == "player")
136 traceline(w_shotorg, e.origin, MOVE_NORMAL, self);
138 w_shotdir = normalize(trace_ent.origin - w_shotorg);
142 else if(cvar("g_antilag") == 3) // client side hitscan
144 if (self.cursor_trace_ent) // client was aiming at someone
145 if (self.cursor_trace_ent != self) // just to make sure
146 if (self.cursor_trace_ent.takedamage) // and that person is killable
147 if (self.cursor_trace_ent.classname == "player") // and actually a player
149 // verify that the shot would miss without antilag
150 // (avoids an issue where guns would always shoot at their origin)
151 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
152 if (!trace_ent.takedamage)
154 // verify that the shot would hit if altered
155 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
156 if (trace_ent == self.cursor_trace_ent)
157 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
159 print("antilag fail\n");
166 self.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
169 self.punchangle_x = recoil * -1;
173 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
176 if (self.items & IT_STRENGTH)
178 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
181 void LaserTarget_Think()
188 // list of weapons that will use the laser, and the options that enable it
189 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
192 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
197 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
200 // rocket launcher isn't selected, so no laser target.
201 if(self.lasertarget != world)
203 remove(self.lasertarget);
204 self.lasertarget = world;
209 if(!self.lasertarget)
211 // we don't have a lasertarget entity, so spawn one
212 //bprint("create laser target\n");
213 e = self.lasertarget = spawn();
214 e.owner = self.owner; // Its owner is my owner
215 e.classname = "laser_target";
216 e.movetype = MOVETYPE_NOCLIP; // don't touch things
217 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
218 e.scale = 1.25; // make it larger
219 e.alpha = 0.25; // transparency
220 e.colormod = '255 0 0' * (1/255) * 8; // change colors
221 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
222 // make it dynamically glow
223 // you should avoid over-using this, as it can slow down the player's computer.
224 e.glow_color = 251; // red color
228 e = self.lasertarget;
230 // move the laser dot to where the player is looking
232 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
233 offset = '0 0 26' + v_right*3;
234 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
235 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
236 if(trace_plane_normal != '0 0 0')
237 e.angles = vectoangles(trace_plane_normal);
239 e.angles = vectoangles(v_forward);
242 float CL_Weaponentity_CustomizeEntityForClient()
244 self.viewmodelforclient = self.owner;
245 if(other.classname == "spectator")
246 if(other.enemy == self.owner)
247 self.viewmodelforclient = other;
252 void CL_Weaponentity_Think()
254 self.nextthink = time;
255 if (intermission_running)
256 self.frame = WFRAME_IDLE;
257 if (self.owner.weaponentity != self)
262 if (self.owner.deadflag != DEAD_NO)
267 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
269 self.cnt = self.owner.weapon;
270 self.dmg = self.owner.modelindex;
271 self.deadflag = self.owner.deadflag;
272 if (self.owner.weaponname != "")
273 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
277 self.effects = self.owner.effects | EF_LOWPRECISION;
278 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
280 if (self.flags & FL_FLY)
281 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
282 self.effects = self.effects - (self.effects & EF_NODRAW);
284 if(self.owner.alpha >= 0)
285 self.alpha = self.owner.alpha;
288 self.colormap = self.owner.colormap;
290 self.angles = '0 0 0';
293 if (self.state == WS_RAISE)
294 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
295 else if (self.state == WS_DROP)
296 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
297 else if (self.state == WS_CLEAR)
299 self.angles_x = -90 * f * f;
301 // create or update the lasertarget entity
305 void CL_ExteriorWeaponentity_Think()
308 self.nextthink = time;
309 if (self.owner.exteriorweaponentity != self)
314 if (self.owner.deadflag != DEAD_NO)
319 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
321 self.cnt = self.owner.weapon;
322 self.dmg = self.owner.modelindex;
323 self.deadflag = self.owner.deadflag;
324 if (self.owner.weaponname != "")
325 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
329 if((tag_found = gettagindex(self.owner, "tag_weapon")))
331 self.tag_index = tag_found;
332 self.tag_entity = self.owner;
335 setattachment(self, self.owner, "bip01 r hand");
337 // if that didn't find a tag, hide the exterior weapon model
341 self.effects = self.owner.effects | EF_LOWPRECISION;
342 self.effects = self.effects & EFMASK_CHEAP; // eat performance
343 if(self.owner.alpha >= 0)
344 self.alpha = self.owner.alpha;
347 self.colormap = self.owner.colormap;
350 // spawning weaponentity for client
351 void CL_SpawnWeaponentity()
353 self.weaponentity = spawn();
354 self.weaponentity.solid = SOLID_NOT;
355 self.weaponentity.owner = self;
356 self.weaponentity.weaponentity = self.weaponentity;
357 setmodel(self.weaponentity, ""); // precision set when changed
358 self.weaponentity.origin = '0 0 0';
359 self.weaponentity.angles = '0 0 0';
360 self.weaponentity.viewmodelforclient = self;
361 self.weaponentity.flags = 0;
362 self.weaponentity.think = CL_Weaponentity_Think;
363 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
364 self.weaponentity.nextthink = time;
365 self.weaponentity.scale = 0.61;
367 self.exteriorweaponentity = spawn();
368 self.exteriorweaponentity.solid = SOLID_NOT;
369 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
370 self.exteriorweaponentity.owner = self;
371 self.exteriorweaponentity.origin = '0 0 0';
372 self.exteriorweaponentity.angles = '0 0 0';
373 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
374 self.exteriorweaponentity.nextthink = time;
377 .float hasweapon_complain_spam;
379 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
381 local float weaponbit, f;
382 local entity oldself;
384 if(time < self.hasweapon_complain_spam)
387 self.hasweapon_complain_spam = time + 0.2;
389 if (wpn < WEP_FIRST || wpn > WEP_LAST)
392 sprint(self, "Invalid weapon\n");
395 weaponbit = W_WeaponBit(wpn);
396 if (cl.weapons & weaponbit)
400 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
408 f = weapon_action(wpn, WR_CHECKAMMO1);
409 f = f + weapon_action(wpn, WR_CHECKAMMO2);
415 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
424 // Report Proper Weapon Status / Modified Weapon Ownership Message
425 if(weaponsInMap & weaponbit)
427 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
429 if(cvar("g_showweaponspawns"))
434 e = get_weaponinfo(wpn);
437 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
439 if(e.classname == "droppedweapon")
441 if not(e.flags & FL_ITEM)
443 WaypointSprite_Spawn(
455 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
463 if (self.weapon != -1)
465 if (self.weaponentity)
467 self.weaponentity.state = WS_CLEAR;
468 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
469 self.weaponentity.effects = 0;
475 if (self.weaponentity)
476 self.weaponentity.state = WS_READY;
477 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
480 // Setup weapon for client (after this raise frame will be launched)
481 void weapon_setup(float windex)
484 e = get_weaponinfo(windex);
485 self.items = self.items - (self.items & IT_AMMO);
486 self.items = self.items | e.items;
488 // the two weapon entities will notice this has changed and update their models
489 self.weapon = windex;
490 self.weaponname = e.mdl;
491 self.bulletcounter = 0;
494 // perform weapon to attack (weaponstate and attack_finished check is here)
496 float weapon_prepareattack(float secondary, float attacktime)
498 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
499 //if all players readied up and the countdown is running
500 if (cvar("sv_ready_restart_after_countdown"))
501 if(time < game_starttime || time < self.race_penalty) {
505 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
506 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
508 W_SwitchWeapon_Force(self, w_getbestweapon(self));
512 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
515 // do not even think about shooting if switching
516 if(self.switchweapon != self.weapon)
519 // don't fire if previous attack is not finished
521 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
523 // don't fire while changing weapon
524 if (self.weaponentity.state != WS_READY)
526 self.weaponentity.state = WS_INUSE;
527 // if the weapon hasn't been firing continuously, reset the timer
530 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
532 ATTACK_FINISHED(self) = time;
533 //dprint("resetting attack finished to ", ftos(time), "\n");
535 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
537 self.bulletcounter += 1;
538 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
542 void weapon_thinkf(float fr, float t, void() func)
546 if (self.weaponentity != world)
547 self.weaponentity.frame = fr;
550 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
552 backtrace("Tried to override initial weapon think function - should this really happen?");
557 if(self.runes & RUNE_SPEED)
559 if(self.runes & CURSE_SLOW)
560 t = t * cvar("g_balance_rune_speed_combo_atkrate");
562 t = t * cvar("g_balance_rune_speed_atkrate");
564 else if(self.runes & CURSE_SLOW)
566 t = t * cvar("g_balance_curse_slow_atkrate");
570 // VorteX: haste can be added here
571 if (self.weapon_think == w_ready)
573 self.weapon_nextthink = time;
574 //dprint("started firing at ", ftos(time), "\n");
576 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
578 self.weapon_nextthink = time;
579 //dprint("reset weapon animation timer at ", ftos(time), "\n");
581 self.weapon_nextthink = self.weapon_nextthink + t;
582 self.weapon_think = func;
583 //dprint("next ", ftos(self.weapon_nextthink), "\n");
585 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
587 if (!self.crouch) // shoot anim stands up, this looks bad
590 anim = self.anim_shoot;
592 player_setanim(anim, FALSE, TRUE, TRUE);
596 void weapon_boblayer1(float spd, vector org)
598 // VorteX: haste can be added here
601 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
609 mdirection = normalize(mvelocity);
610 mspeed = vlen(mvelocity);
612 nstyle = cvar("g_projectiles_newton_style");
616 outvelocity = mvelocity;
620 // true Newtonian projectiles
621 outvelocity = pvelocity + mvelocity;
625 // true Newtonian projectiles with automatic aim adjustment
627 // solve: |outspeed * mdirection - pvelocity| = mspeed
628 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
629 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
633 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
634 // velocity without mdirection component > mspeed
635 // fire at smallest possible mspeed that works?
636 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
641 p = mdirection * pvelocity;
642 q = pvelocity * pvelocity - mspeed * mspeed;
646 //dprint("impossible shot, adjusting\n");
649 outspeed = p + sqrt(D);
650 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
651 outvelocity = mdirection * outspeed;
656 outspeed = mspeed + mdirection * pvelocity;
657 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
658 outvelocity = mdirection * outspeed;
663 outspeed = mspeed + vlen(pvelocity);
664 outvelocity = mdirection * outspeed;
667 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
672 void W_SetupProjectileVelocity(entity missile)
674 if(missile.owner == world)
675 error("Unowned missile");
677 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);