2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 void W_SwitchWeapon_Force(entity e, float w)
12 e.cnt = e.weapon ? e.weapon : e.switchweapon;
18 // VorteX: static frame globals
19 float WFRAME_FIRE1 = 0;
20 float WFRAME_FIRE2 = 1;
21 float WFRAME_IDLE = 2;
23 void(float fr, float t, void() func) weapon_thinkf;
28 // this function calculates w_shotorg and w_shotdir based on the weapon model
29 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
30 // make sure you call makevectors first (FIXME?)
31 void W_SetupShot(entity ent, vector vecs, float antilag, float recoil, string snd)
33 float nudge = 1; // added to traceline target and subtracted from result
34 local vector trueaimpoint;
36 oldsolid = self.solid;
37 self.solid = SOLID_BBOX; // make all shot test traces bypass playerclip
38 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
39 trueaimpoint = trace_endpos;
41 if (cvar("g_shootfromeye"))
42 w_shotorg = ent.origin + ent.view_ofs;
43 else if (cvar("g_shootfromcenter"))
44 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
46 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
47 // now move the shotorg forward as much as requested if possible
48 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
49 w_shotorg = trace_endpos - v_forward * nudge;
50 // calculate the shotdir from the chosen shotorg
51 w_shotdir = normalize(trueaimpoint - w_shotorg);
53 // explanation of g_antilag:
54 // if client reports it was aiming at a player, and the serverside trace
55 // says it would miss, change the aim point to the player's new origin,
56 // but only if the shot at the player's new origin would hit of course
58 // FIXME: a much better method for bullet weapons would be to leave a
59 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
60 // ghost corresponding to this player's ping time, and if so it would do
61 // damage to the real player
63 if (self.cursor_trace_ent) // client was aiming at someone
64 if (self.cursor_trace_ent != self) // just to make sure
65 if (self.cursor_trace_ent.takedamage) // and that person is killable
66 if (self.cursor_trace_ent.classname == "player") // and actually a player
67 if (cvar("g_antilag") == 1)
69 // verify that the shot would miss without antilag
70 // (avoids an issue where guns would always shoot at their origin)
71 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
72 if (!trace_ent.takedamage)
74 // verify that the shot would hit if altered
75 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
76 if (trace_ent == self.cursor_trace_ent)
78 // verify that the shot would hit in the past
79 if(self.antilag_debug)
80 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
82 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
84 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
85 antilag_restore(self.cursor_trace_ent);
87 if(trace_ent == self.cursor_trace_ent)
90 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
91 dprint("ANTILAG HIT for ", self.netname, "\n");
95 // prydon cursor aimbot or odd network conditions
96 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
99 if(cvar("developer") >= 2)
101 vector v, vplus, vel;
103 v = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 ));
104 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 + 0.01));
105 vel = (vplus - v) * (1 / 0.01);
106 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
107 v -= (self.origin + self.view_ofs);
108 // solve: v + X * vel = closest to v_forward axis
109 // project into 2D by subtracting v_forward components:
110 v -= (v * v_forward) * v_forward;
111 vel -= (vel * v_forward) * v_forward;
112 // solve: v + X * vel = closest to origin
113 // (v + X * vel)^2 closest to 0
114 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
115 X = -(v * vel) / (vel * vel);
116 dprint("dead center needs adjustment of ", ftos(X * 1000), " (that is, ", ftos(self.ping + X * 1000), " instead of ", ftos(self.ping), "\n");
122 self.solid = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
125 self.punchangle_x = recoil * -1;
128 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
130 if (self.items & IT_STRENGTH)
132 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
135 void LaserTarget_Think()
142 // list of weapons that will use the laser, and the options that enable it
143 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
146 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
151 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
154 // rocket launcher isn't selected, so no laser target.
155 if(self.lasertarget != world)
157 remove(self.lasertarget);
158 self.lasertarget = world;
163 if(!self.lasertarget)
165 // we don't have a lasertarget entity, so spawn one
166 //bprint("create laser target\n");
167 e = self.lasertarget = spawn();
168 e.owner = self.owner; // Its owner is my owner
169 e.classname = "laser_target";
170 e.movetype = MOVETYPE_NOCLIP; // don't touch things
171 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
172 e.scale = 1.25; // make it larger
173 e.alpha = 0.25; // transparency
174 e.colormod = '255 0 0' * (1/255) * 8; // change colors
175 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
176 // make it dynamically glow
177 // you should avoid over-using this, as it can slow down the player's computer.
178 e.glow_color = 251; // red color
182 e = self.lasertarget;
184 // move the laser dot to where the player is looking
186 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
187 offset = '0 0 26' + v_right*3;
188 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
189 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
190 if(trace_plane_normal != '0 0 0')
191 e.angles = vectoangles(trace_plane_normal);
193 e.angles = vectoangles(v_forward);
196 float CL_Weaponentity_CustomizeEntityForClient()
198 self.viewmodelforclient = self.owner;
199 if(other.classname == "spectator")
200 if(other.enemy == self.owner)
201 self.viewmodelforclient = other;
206 void CL_Weaponentity_Think()
208 self.nextthink = time;
209 if (intermission_running)
210 self.frame = WFRAME_IDLE;
211 if (self.owner.weaponentity != self)
216 if (self.owner.deadflag != DEAD_NO)
221 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
223 self.cnt = self.owner.weapon;
224 self.dmg = self.owner.modelindex;
225 self.deadflag = self.owner.deadflag;
226 if (self.owner.weaponname != "")
227 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
231 self.effects = self.owner.effects | EF_LOWPRECISION;
232 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
234 if (self.flags & FL_FLY)
235 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
236 self.effects = self.effects - (self.effects & EF_NODRAW);
238 if(self.owner.alpha >= 0)
239 self.alpha = self.owner.alpha;
242 self.colormap = self.owner.colormap;
244 self.angles = '0 0 0';
247 if (self.state == WS_RAISE)
248 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
249 else if (self.state == WS_DROP)
250 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
251 else if (self.state == WS_CLEAR)
253 self.angles_x = -90 * f * f;
255 // create or update the lasertarget entity
259 void CL_ExteriorWeaponentity_Think()
262 self.nextthink = time;
263 if (self.owner.exteriorweaponentity != self)
268 if (self.owner.deadflag != DEAD_NO)
273 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
275 self.cnt = self.owner.weapon;
276 self.dmg = self.owner.modelindex;
277 self.deadflag = self.owner.deadflag;
278 if (self.owner.weaponname != "")
279 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
283 if((tag_found = gettagindex(self.owner, "tag_weapon")))
285 self.tag_index = tag_found;
286 self.tag_entity = self.owner;
289 setattachment(self, self.owner, "bip01 r hand");
291 // if that didn't find a tag, hide the exterior weapon model
295 self.effects = self.owner.effects | EF_LOWPRECISION;
296 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
297 if(self.owner.alpha >= 0)
298 self.alpha = self.owner.alpha;
301 self.colormap = self.owner.colormap;
304 // spawning weaponentity for client
305 void CL_SpawnWeaponentity()
307 self.weaponentity = spawn();
308 self.weaponentity.solid = SOLID_NOT;
309 self.weaponentity.owner = self;
310 self.weaponentity.weaponentity = self.weaponentity;
311 setmodel(self.weaponentity, ""); // precision set when changed
312 self.weaponentity.origin = '0 0 0';
313 self.weaponentity.angles = '0 0 0';
314 self.weaponentity.viewmodelforclient = self;
315 self.weaponentity.flags = 0;
316 self.weaponentity.think = CL_Weaponentity_Think;
317 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
318 self.weaponentity.nextthink = time;
319 self.weaponentity.scale = 0.61;
321 self.exteriorweaponentity = spawn();
322 self.exteriorweaponentity.solid = SOLID_NOT;
323 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
324 self.exteriorweaponentity.owner = self;
325 self.exteriorweaponentity.origin = '0 0 0';
326 self.exteriorweaponentity.angles = '0 0 0';
327 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
328 self.exteriorweaponentity.nextthink = time;
331 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
333 local float weaponbit, f;
334 local entity oldself;
336 if (wpn < WEP_FIRST || wpn > WEP_LAST)
339 sprint(self, "Invalid weapon\n");
342 weaponbit = W_WeaponBit(wpn);
343 if (cl.weapons & weaponbit)
349 f = weapon_action(wpn, WR_CHECKAMMO1);
350 f = f + weapon_action(wpn, WR_CHECKAMMO2);
355 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
364 // Report Proper Weapon Status / Modified Weapon Ownership Message
365 if(weaponsInMap & weaponbit)
366 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
368 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
376 if (self.weapon != -1)
378 if (self.weaponentity)
380 self.weaponentity.state = WS_CLEAR;
381 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
382 self.weaponentity.effects = 0;
388 if (self.weaponentity)
389 self.weaponentity.state = WS_READY;
390 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
393 // Setup weapon for client (after this raise frame will be launched)
394 void weapon_setup(float windex)
397 e = get_weaponinfo(windex);
398 self.items = self.items - (self.items & IT_AMMO);
399 self.items = self.items | e.items;
401 // the two weapon entities will notice this has changed and update their models
402 self.weapon = windex;
403 self.weaponname = e.mdl;
406 // perform weapon to attack (weaponstate and attack_finished check is here)
408 float weapon_prepareattack(float secondary, float attacktime)
410 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
411 //if all players readied up and the countdown is running
412 if (cvar("sv_ready_restart_after_countdown"))
413 if(time < restart_countdown || time < self.race_penalty) {
417 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
419 W_SwitchWeapon_Force(self, w_getbestweapon(self));
423 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
426 // do not even think about shooting if switching
427 if(self.switchweapon != self.weapon)
430 // don't fire if previous attack is not finished
431 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
433 // don't fire while changing weapon
434 if (self.weaponentity.state != WS_READY)
436 self.weaponentity.state = WS_INUSE;
437 // if the weapon hasn't been firing continuously, reset the timer
438 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
440 ATTACK_FINISHED(self) = time;
441 //dprint("resetting attack finished to ", ftos(time), "\n");
443 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
444 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
448 void weapon_thinkf(float fr, float t, void() func)
452 if (self.weaponentity != world)
453 self.weaponentity.frame = fr;
456 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
458 backtrace("Tried to override initial weapon think function - should this really happen?");
463 if(self.runes & RUNE_SPEED)
465 if(self.runes & CURSE_SLOW)
466 t = t * cvar("g_balance_rune_speed_combo_atkrate");
468 t = t * cvar("g_balance_rune_speed_atkrate");
470 else if(self.runes & CURSE_SLOW)
472 t = t * cvar("g_balance_curse_slow_atkrate");
476 // VorteX: haste can be added here
477 if (self.weapon_think == w_ready)
479 self.weapon_nextthink = time;
480 //dprint("started firing at ", ftos(time), "\n");
482 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
484 self.weapon_nextthink = time;
485 //dprint("reset weapon animation timer at ", ftos(time), "\n");
487 self.weapon_nextthink = self.weapon_nextthink + t;
488 self.weapon_think = func;
489 //dprint("next ", ftos(self.weapon_nextthink), "\n");
491 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
493 if (!self.crouch) // shoot anim stands up, this looks bad
496 anim = self.anim_shoot;
498 player_setanim(anim, FALSE, TRUE, TRUE);
502 void weapon_boblayer1(float spd, vector org)
504 // VorteX: haste can be added here
507 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
515 mdirection = normalize(mvelocity);
516 mspeed = vlen(mvelocity);
518 nstyle = cvar("g_projectiles_newton_style");
522 outvelocity = mvelocity;
526 // true Newtonian projectiles
527 outvelocity = pvelocity + mvelocity;
531 // true Newtonian projectiles with automatic aim adjustment
533 // solve: |outspeed * mdirection - pvelocity| = mspeed
534 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
535 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
539 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
540 // velocity without mdirection component > mspeed
541 // fire at smallest possible mspeed that works?
542 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
547 p = mdirection * pvelocity;
548 q = pvelocity * pvelocity - mspeed * mspeed;
552 //dprint("impossible shot, adjusting\n");
555 outspeed = p + sqrt(D);
556 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
557 outvelocity = mdirection * outspeed;
562 outspeed = mspeed + mdirection * pvelocity;
563 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
564 outvelocity = mdirection * outspeed;
569 outspeed = mspeed + vlen(pvelocity);
570 outvelocity = mdirection * outspeed;
573 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
578 void W_SetupProjectileVelocity(entity missile)
580 if(missile.owner == world)
581 error("Unowned missile");
583 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
587 // WEAPON PLUGIN SYSTEM
588 entity weapon_info[24];
589 entity dummy_weapon_info;
590 .float(float) weapon_func;
591 string weaponpriority_hudselector_0;
592 string weaponpriority_hudselector_1;
594 void register_weapon(float id, float(float) func, float ammotype, float i, float normalweapon, float pickupbasevalue, string modelname, string shortname, string name)
597 weapon_info[id - 1] = e = spawn();
598 e.classname = "weapon_info";
600 e.weapons = power2of(id - 1);
601 e.netname = shortname;
604 e.weapon_func = func;
606 e.model = strzone(strcat("models/weapons/g_", modelname, ".md3"));
607 e.spawnflags = normalweapon;
609 e.bot_pickupbasevalue = pickupbasevalue;
611 float w_null(float dummy)
615 void register_weapons_done()
617 dummy_weapon_info = spawn();
618 dummy_weapon_info.classname = "weapon_info";
619 dummy_weapon_info.weapon = 0;
620 dummy_weapon_info.weapons = 0;
621 dummy_weapon_info.netname = "@!#%'n Tuba";
622 dummy_weapon_info.items = 0;
623 dummy_weapon_info.weapon_func = w_null;
624 dummy_weapon_info.mdl = "";
625 dummy_weapon_info.model = "";
626 dummy_weapon_info.spawnflags = 0;
627 dummy_weapon_info.impulse = -1;
628 dummy_weapon_info.bot_pickupbasevalue = 0;
631 weaponpriority_hudselector_0 = "";
632 for(i = 24; i >= 1; --i)
634 weaponpriority_hudselector_0 = strcat(weaponpriority_hudselector_0, " ", ftos(i));
637 weaponpriority_hudselector_1 = "";
638 for(i = 1; i <= 24; ++i)
639 if(weapon_info[i-1] && weapon_info[i-1].impulse == 0)
640 weaponpriority_hudselector_1 = strcat(weaponpriority_hudselector_1, " ", ftos(i));
641 for(imp = 9; imp > 0; --imp)
642 for(i = 1; i <= 24; ++i)
643 if(weapon_info[i-1] && weapon_info[i-1].impulse == imp)
644 weaponpriority_hudselector_1 = strcat(weaponpriority_hudselector_1, " ", ftos(i));
646 weaponpriority_hudselector_0 = strzone(substring(weaponpriority_hudselector_0, 1, strlen(weaponpriority_hudselector_0) - 1));
647 weaponpriority_hudselector_1 = strzone(substring(weaponpriority_hudselector_1, 1, strlen(weaponpriority_hudselector_1) - 1));
649 entity get_weaponinfo(float id)
652 if(id < WEP_FIRST || id > WEP_LAST)
653 return dummy_weapon_info;
654 w = weapon_info[id - 1];
657 return dummy_weapon_info;