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(debug_shotorg != '0 0 0')
45 if (cvar("g_shootfromeye"))
46 w_shotorg = ent.origin + ent.view_ofs;
47 else if (cvar("g_shootfromcenter"))
48 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
50 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
51 // now move the shotorg forward as much as requested if possible
52 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
53 w_shotorg = trace_endpos - v_forward * nudge;
54 // calculate the shotdir from the chosen shotorg
55 w_shotdir = normalize(trueaimpoint - w_shotorg);
58 // explanation of g_antilag:
59 // if client reports it was aiming at a player, and the serverside trace
60 // says it would miss, change the aim point to the player's new origin,
61 // but only if the shot at the player's new origin would hit of course
63 // FIXME: a much better method for bullet weapons would be to leave a
64 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
65 // ghost corresponding to this player's ping time, and if so it would do
66 // damage to the real player
68 if (self.cursor_trace_ent) // client was aiming at someone
69 if (self.cursor_trace_ent != self) // just to make sure
70 if (self.cursor_trace_ent.takedamage) // and that person is killable
71 if (self.cursor_trace_ent.classname == "player") // and actually a player
72 if (cvar("g_antilag") == 1)
74 // verify that the shot would miss without antilag
75 // (avoids an issue where guns would always shoot at their origin)
76 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
77 if (!trace_ent.takedamage)
79 // verify that the shot would hit if altered
80 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
81 if (trace_ent == self.cursor_trace_ent)
83 // verify that the shot would hit in the past
84 if(self.antilag_debug)
85 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
87 antilag_takeback(self.cursor_trace_ent, time - ANTILAG_LATENCY(self));
89 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
90 antilag_restore(self.cursor_trace_ent);
92 if(trace_ent == self.cursor_trace_ent)
95 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
96 dprint("ANTILAG HIT for ", self.netname, "\n");
100 // prydon cursor aimbot or odd network conditions
101 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
104 if(cvar("developer") >= 2)
106 vector v, vplus, vel;
108 v = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self) ));
109 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (ANTILAG_LATENCY(self) + 0.01));
110 vel = (vplus - v) * (1 / 0.01);
111 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
112 v -= (self.origin + self.view_ofs);
113 // solve: v + X * vel = closest to v_forward axis
114 // project into 2D by subtracting v_forward components:
115 v -= (v * v_forward) * v_forward;
116 vel -= (vel * v_forward) * v_forward;
117 // solve: v + X * vel = closest to origin
118 // (v + X * vel)^2 closest to 0
119 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
120 X = -(v * vel) / (vel * vel);
121 dprint("dead center needs adjustment of ", ftos(X), " (that is, ", ftos(ANTILAG_LATENCY(self) + X), " instead of ", ftos(ANTILAG_LATENCY(self)), "\n");
129 if (cvar("g_antilag") == 1) // switch to "ghost" if not hitting original
131 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
132 if (!trace_ent.takedamage)
134 traceline_antilag_force (self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self, ANTILAG_LATENCY(self));
135 if (trace_ent.takedamage && trace_ent.classname == "player")
139 traceline(w_shotorg, e.origin, MOVE_NORMAL, self);
141 w_shotdir = normalize(trace_ent.origin - w_shotorg);
145 else if(cvar("g_antilag") == 3) // client side hitscan
147 if (self.cursor_trace_ent) // client was aiming at someone
148 if (self.cursor_trace_ent != self) // just to make sure
149 if (self.cursor_trace_ent.takedamage) // and that person is killable
150 if (self.cursor_trace_ent.classname == "player") // and actually a player
152 // verify that the shot would miss without antilag
153 // (avoids an issue where guns would always shoot at their origin)
154 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
155 if (!trace_ent.takedamage)
157 // verify that the shot would hit if altered
158 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
159 if (trace_ent == self.cursor_trace_ent)
160 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
162 print("antilag fail\n");
169 self.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
172 self.punchangle_x = recoil * -1;
176 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
179 if (self.items & IT_STRENGTH)
181 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
184 void LaserTarget_Think()
191 // list of weapons that will use the laser, and the options that enable it
192 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
195 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
200 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
203 // rocket launcher isn't selected, so no laser target.
204 if(self.lasertarget != world)
206 remove(self.lasertarget);
207 self.lasertarget = world;
212 if(!self.lasertarget)
214 // we don't have a lasertarget entity, so spawn one
215 //bprint("create laser target\n");
216 e = self.lasertarget = spawn();
217 e.owner = self.owner; // Its owner is my owner
218 e.classname = "laser_target";
219 e.movetype = MOVETYPE_NOCLIP; // don't touch things
220 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
221 e.scale = 1.25; // make it larger
222 e.alpha = 0.25; // transparency
223 e.colormod = '255 0 0' * (1/255) * 8; // change colors
224 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
225 // make it dynamically glow
226 // you should avoid over-using this, as it can slow down the player's computer.
227 e.glow_color = 251; // red color
231 e = self.lasertarget;
233 // move the laser dot to where the player is looking
235 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
236 offset = '0 0 26' + v_right*3;
237 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
238 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
239 if(trace_plane_normal != '0 0 0')
240 e.angles = vectoangles(trace_plane_normal);
242 e.angles = vectoangles(v_forward);
245 float CL_Weaponentity_CustomizeEntityForClient()
247 self.viewmodelforclient = self.owner;
248 if(other.classname == "spectator")
249 if(other.enemy == self.owner)
250 self.viewmodelforclient = other;
255 void CL_Weaponentity_Think()
258 self.nextthink = time;
259 if (intermission_running)
260 self.frame = WFRAME_IDLE;
261 if (self.owner.weaponentity != self)
266 if (self.owner.deadflag != DEAD_NO)
271 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
273 self.cnt = self.owner.weapon;
274 self.dmg = self.owner.modelindex;
275 self.deadflag = self.owner.deadflag;
276 if (self.owner.weaponname != "")
277 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
282 tb = (self.effects & EF_TELEPORT_BIT);
283 self.effects = self.owner.effects | EF_LOWPRECISION;
284 self.effects &~= EF_FULLBRIGHT; // can mask team color, so get rid of it
285 self.effects &~= EF_TELEPORT_BIT;
288 if(self.owner.alpha != 0)
289 self.alpha = self.owner.alpha;
293 self.colormap = self.owner.colormap;
295 self.angles = '0 0 0';
298 if (self.state == WS_RAISE)
299 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
300 else if (self.state == WS_DROP)
301 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
302 else if (self.state == WS_CLEAR)
304 self.angles_x = -90 * f * f;
306 // create or update the lasertarget entity
310 void CL_ExteriorWeaponentity_Think()
313 self.nextthink = time;
314 if (self.owner.exteriorweaponentity != self)
319 if (self.owner.deadflag != DEAD_NO)
324 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
326 self.cnt = self.owner.weapon;
327 self.dmg = self.owner.modelindex;
328 self.deadflag = self.owner.deadflag;
329 if (self.owner.weaponname != "")
330 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
334 if((tag_found = gettagindex(self.owner, "tag_weapon")))
336 self.tag_index = tag_found;
337 self.tag_entity = self.owner;
340 setattachment(self, self.owner, "bip01 r hand");
342 // if that didn't find a tag, hide the exterior weapon model
346 self.effects = self.owner.effects | EF_LOWPRECISION;
347 self.effects = self.effects & EFMASK_CHEAP; // eat performance
348 if(self.owner.alpha != 0)
349 self.alpha = self.owner.alpha;
353 self.colormap = self.owner.colormap;
356 // spawning weaponentity for client
357 void CL_SpawnWeaponentity()
359 self.weaponentity = spawn();
360 self.weaponentity.classname = "weaponentity";
361 self.weaponentity.solid = SOLID_NOT;
362 self.weaponentity.owner = self;
363 self.weaponentity.weaponentity = self.weaponentity;
364 setmodel(self.weaponentity, ""); // precision set when changed
365 self.weaponentity.origin = '0 0 0';
366 self.weaponentity.angles = '0 0 0';
367 self.weaponentity.viewmodelforclient = self;
368 self.weaponentity.flags = 0;
369 self.weaponentity.think = CL_Weaponentity_Think;
370 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
371 self.weaponentity.nextthink = time;
372 self.weaponentity.scale = 0.61;
374 self.exteriorweaponentity = spawn();
375 self.exteriorweaponentity.classname = "exteriorweaponentity";
376 self.exteriorweaponentity.solid = SOLID_NOT;
377 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
378 self.exteriorweaponentity.owner = self;
379 self.exteriorweaponentity.origin = '0 0 0';
380 self.exteriorweaponentity.angles = '0 0 0';
381 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
382 self.exteriorweaponentity.nextthink = time;
385 .float hasweapon_complain_spam;
387 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
389 local float weaponbit, f;
390 local entity oldself;
392 if(time < self.hasweapon_complain_spam)
395 self.hasweapon_complain_spam = time + 0.2;
397 if (wpn < WEP_FIRST || wpn > WEP_LAST)
400 sprint(self, "Invalid weapon\n");
403 weaponbit = W_WeaponBit(wpn);
404 if (cl.weapons & weaponbit)
408 if(cl.items & IT_UNLIMITED_WEAPON_AMMO)
416 f = weapon_action(wpn, WR_CHECKAMMO1);
417 f = f + weapon_action(wpn, WR_CHECKAMMO2);
423 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
432 // Report Proper Weapon Status / Modified Weapon Ownership Message
433 if(weaponsInMap & weaponbit)
435 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
437 if(cvar("g_showweaponspawns"))
442 e = get_weaponinfo(wpn);
445 for(e = world; (e = findfloat(e, weapons, weaponbit)); )
447 if(e.classname == "droppedweapon")
449 if not(e.flags & FL_ITEM)
451 WaypointSprite_Spawn(
463 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
471 if (self.weapon != -1)
473 if (self.weaponentity)
475 self.weaponentity.state = WS_CLEAR;
476 self.weaponentity.effects = 0;
482 if (self.weaponentity)
483 self.weaponentity.state = WS_READY;
484 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
487 // Setup weapon for client (after this raise frame will be launched)
488 void weapon_setup(float windex)
491 e = get_weaponinfo(windex);
492 self.items &~= IT_AMMO;
493 self.items = self.items | e.items;
495 // the two weapon entities will notice this has changed and update their models
496 self.weapon = windex;
497 self.weaponname = e.mdl;
498 self.bulletcounter = 0;
501 // perform weapon to attack (weaponstate and attack_finished check is here)
503 float weapon_prepareattack(float secondary, float attacktime)
505 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
506 //if all players readied up and the countdown is running
507 if (cvar("sv_ready_restart_after_countdown"))
508 if(time < game_starttime || time < self.race_penalty) {
512 if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
513 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
515 W_SwitchWeapon_Force(self, w_getbestweapon(self));
519 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
522 // do not even think about shooting if switching
523 if(self.switchweapon != self.weapon)
526 // don't fire if previous attack is not finished
528 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
530 // don't fire while changing weapon
531 if (self.weaponentity.state != WS_READY)
533 self.weaponentity.state = WS_INUSE;
535 self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire
537 // if the weapon hasn't been firing continuously, reset the timer
540 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
542 ATTACK_FINISHED(self) = time;
543 //dprint("resetting attack finished to ", ftos(time), "\n");
545 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
547 self.bulletcounter += 1;
548 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
552 void weapon_thinkf(float fr, float t, void() func)
556 if (self.weaponentity != world)
558 if(fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
560 BITXOR_ASSIGN(self.weaponentity.effects, EF_TELEPORT_BIT);
562 self.weaponentity.frame = fr;
566 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
568 backtrace("Tried to override initial weapon think function - should this really happen?");
573 if(self.runes & RUNE_SPEED)
575 if(self.runes & CURSE_SLOW)
576 t = t * cvar("g_balance_rune_speed_combo_atkrate");
578 t = t * cvar("g_balance_rune_speed_atkrate");
580 else if(self.runes & CURSE_SLOW)
582 t = t * cvar("g_balance_curse_slow_atkrate");
586 // VorteX: haste can be added here
587 if (self.weapon_think == w_ready)
589 self.weapon_nextthink = time;
590 //dprint("started firing at ", ftos(time), "\n");
592 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
594 self.weapon_nextthink = time;
595 //dprint("reset weapon animation timer at ", ftos(time), "\n");
597 self.weapon_nextthink = self.weapon_nextthink + t;
598 self.weapon_think = func;
599 //dprint("next ", ftos(self.weapon_nextthink), "\n");
601 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
603 if (!self.crouch) // shoot anim stands up, this looks bad
606 anim = self.anim_shoot;
608 player_setanim(anim, FALSE, TRUE, TRUE);
612 void weapon_boblayer1(float spd, vector org)
614 // VorteX: haste can be added here
617 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
625 mdirection = normalize(mvelocity);
626 mspeed = vlen(mvelocity);
628 nstyle = cvar("g_projectiles_newton_style");
632 outvelocity = mvelocity;
636 // true Newtonian projectiles
637 outvelocity = pvelocity + mvelocity;
641 // true Newtonian projectiles with automatic aim adjustment
643 // solve: |outspeed * mdirection - pvelocity| = mspeed
644 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
645 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
649 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
650 // velocity without mdirection component > mspeed
651 // fire at smallest possible mspeed that works?
652 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
657 p = mdirection * pvelocity;
658 q = pvelocity * pvelocity - mspeed * mspeed;
662 //dprint("impossible shot, adjusting\n");
665 outspeed = p + sqrt(D);
666 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
667 outvelocity = mdirection * outspeed;
672 outspeed = mspeed + mdirection * pvelocity;
673 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
674 outvelocity = mdirection * outspeed;
679 outspeed = mspeed + vlen(pvelocity);
680 outvelocity = mdirection * outspeed;
683 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
688 void W_SetupProjectileVelocity(entity missile)
690 if(missile.owner == world)
691 error("Unowned missile");
693 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);