2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
12 // VorteX: static frame globals
13 float WFRAME_FIRE1 = 0;
14 float WFRAME_FIRE2 = 1;
15 float WFRAME_IDLE = 2;
17 void(float fr, float t, void() func) weapon_thinkf;
22 // this function calculates w_shotorg and w_shotdir based on the weapon model
23 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
24 // make sure you call makevectors first (FIXME?)
25 void W_SetupShot(entity ent, vector vecs, float antilag, float recoil, string snd)
27 float nudge = 1; // added to traceline target and subtracted from result
28 local vector trueaimpoint;
30 oldsolid = self.solid;
31 self.solid = SOLID_BBOX; // make all shot test traces bypass playerclip
32 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
33 trueaimpoint = trace_endpos;
35 if (cvar("g_shootfromeye"))
36 w_shotorg = ent.origin + ent.view_ofs;
37 else if (cvar("g_shootfromcenter"))
38 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
40 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
41 // now move the shotorg forward as much as requested if possible
42 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
43 w_shotorg = trace_endpos - v_forward * nudge;
44 // calculate the shotdir from the chosen shotorg
45 w_shotdir = normalize(trueaimpoint - w_shotorg);
47 // explanation of g_antilag:
48 // if client reports it was aiming at a player, and the serverside trace
49 // says it would miss, change the aim point to the player's new origin,
50 // but only if the shot at the player's new origin would hit of course
52 // FIXME: a much better method for bullet weapons would be to leave a
53 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
54 // ghost corresponding to this player's ping time, and if so it would do
55 // damage to the real player
57 if (self.cursor_trace_ent) // client was aiming at someone
58 if (self.cursor_trace_ent != self) // just to make sure
59 if (self.cursor_trace_ent.takedamage) // and that person is killable
60 if (self.cursor_trace_ent.classname == "player") // and actually a player
61 if (cvar("g_antilag") == 1)
63 // verify that the shot would miss without antilag
64 // (avoids an issue where guns would always shoot at their origin)
65 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
66 if (!trace_ent.takedamage)
68 // verify that the shot would hit if altered
69 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
70 if (trace_ent == self.cursor_trace_ent)
72 // verify that the shot would hit in the past
73 if(self.antilag_debug)
74 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
76 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
78 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
79 antilag_restore(self.cursor_trace_ent);
81 if(trace_ent == self.cursor_trace_ent)
84 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
85 dprint("ANTILAG HIT for ", self.netname, "\n");
89 // prydon cursor aimbot or odd network conditions
90 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
93 if(cvar("developer") >= 2)
97 v = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 ));
98 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 + 0.01));
99 vel = (vplus - v) * (1 / 0.01);
100 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
101 v -= (self.origin + self.view_ofs);
102 // solve: v + X * vel = closest to v_forward axis
103 // project into 2D by subtracting v_forward components:
104 v -= (v * v_forward) * v_forward;
105 vel -= (vel * v_forward) * v_forward;
106 // solve: v + X * vel = closest to origin
107 // (v + X * vel)^2 closest to 0
108 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
109 X = -(v * vel) / (vel * vel);
110 dprint("dead center needs adjustment of ", ftos(X * 1000), " (that is, ", ftos(self.ping + X * 1000), " instead of ", ftos(self.ping), "\n");
116 self.solid = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
119 self.punchangle_x = recoil * -1;
122 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
124 if (self.items & IT_STRENGTH)
126 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
129 void LaserTarget_Think()
136 // list of weapons that will use the laser, and the options that enable it
137 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
140 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
145 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
148 // rocket launcher isn't selected, so no laser target.
149 if(self.lasertarget != world)
151 remove(self.lasertarget);
152 self.lasertarget = world;
157 if(!self.lasertarget)
159 // we don't have a lasertarget entity, so spawn one
160 //bprint("create laser target\n");
161 e = self.lasertarget = spawn();
162 e.owner = self.owner; // Its owner is my owner
163 e.classname = "laser_target";
164 e.movetype = MOVETYPE_NOCLIP; // don't touch things
165 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
166 e.scale = 1.25; // make it larger
167 e.alpha = 0.25; // transparency
168 e.colormod = '255 0 0' * (1/255) * 8; // change colors
169 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
170 // make it dynamically glow
171 // you should avoid over-using this, as it can slow down the player's computer.
172 e.glow_color = 251; // red color
176 e = self.lasertarget;
178 // move the laser dot to where the player is looking
180 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
181 offset = '0 0 26' + v_right*3;
182 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
183 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
184 if(trace_plane_normal != '0 0 0')
185 e.angles = vectoangles(trace_plane_normal);
187 e.angles = vectoangles(v_forward);
190 float CL_Weaponentity_CustomizeEntityForClient()
192 self.viewmodelforclient = self.owner;
193 if(other.classname == "spectator")
194 if(other.enemy == self.owner)
195 self.viewmodelforclient = other;
200 void CL_Weaponentity_Think()
202 self.nextthink = time;
203 if (intermission_running)
204 self.frame = WFRAME_IDLE;
205 if (self.owner.weaponentity != self)
210 if (self.owner.deadflag != DEAD_NO)
215 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
217 self.cnt = self.owner.weapon;
218 self.dmg = self.owner.modelindex;
219 self.deadflag = self.owner.deadflag;
220 if (self.owner.weaponname != "")
221 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
225 self.effects = self.owner.effects | EF_LOWPRECISION;
226 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
228 if (self.flags & FL_FLY)
229 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
230 self.effects = self.effects - (self.effects & EF_NODRAW);
232 if(self.owner.alpha >= 0)
233 self.alpha = self.owner.alpha;
236 self.colormap = self.owner.colormap;
238 self.angles = '0 0 0';
241 if (self.state == WS_RAISE)
242 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
243 else if (self.state == WS_DROP)
244 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
245 else if (self.state == WS_CLEAR)
247 self.angles_x = -90 * f * f;
249 // create or update the lasertarget entity
253 void CL_ExteriorWeaponentity_Think()
256 self.nextthink = time;
257 if (self.owner.exteriorweaponentity != 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/v_", self.owner.weaponname, ".md3")); // precision set below
277 if((tag_found = gettagindex(self.owner, "tag_weapon")))
279 self.tag_index = tag_found;
280 self.tag_entity = self.owner;
283 setattachment(self, self.owner, "bip01 r hand");
285 // if that didn't find a tag, hide the exterior weapon model
289 self.effects = self.owner.effects | EF_LOWPRECISION;
290 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
291 if(self.owner.alpha >= 0)
292 self.alpha = self.owner.alpha;
295 self.colormap = self.owner.colormap;
298 // spawning weaponentity for client
299 void CL_SpawnWeaponentity()
301 self.weaponentity = spawn();
302 self.weaponentity.solid = SOLID_NOT;
303 self.weaponentity.owner = self;
304 self.weaponentity.weaponentity = self.weaponentity;
305 setmodel(self.weaponentity, ""); // precision set when changed
306 self.weaponentity.origin = '0 0 0';
307 self.weaponentity.angles = '0 0 0';
308 self.weaponentity.viewmodelforclient = self;
309 self.weaponentity.flags = 0;
310 self.weaponentity.think = CL_Weaponentity_Think;
311 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
312 self.weaponentity.nextthink = time;
313 self.weaponentity.scale = 0.61;
315 self.exteriorweaponentity = spawn();
316 self.exteriorweaponentity.solid = SOLID_NOT;
317 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
318 self.exteriorweaponentity.owner = self;
319 self.exteriorweaponentity.origin = '0 0 0';
320 self.exteriorweaponentity.angles = '0 0 0';
321 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
322 self.exteriorweaponentity.nextthink = time;
325 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
327 local float itemcode, f;
328 local entity oldself;
330 if (wpn < WEP_FIRST || wpn > WEP_LAST)
333 sprint(self, "Invalid weapon\n");
336 itemcode = W_ItemCode(wpn);
337 if (cl.items & itemcode)
343 f = weapon_action(wpn, WR_CHECKAMMO1);
344 f = f + weapon_action(wpn, WR_CHECKAMMO2);
349 sprint(self, "You don't have any ammo for that weapon\n");
358 // Report Proper Weapon Status / Modified Weapon Ownership Message
359 if(itemsInMap & itemcode)
360 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
362 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
370 if (self.weapon != -1)
372 if (self.weaponentity)
374 self.weaponentity.state = WS_CLEAR;
375 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
376 self.weaponentity.effects = 0;
382 if (self.weaponentity)
383 self.weaponentity.state = WS_READY;
384 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
387 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
388 float w_getbestweapon(entity e)
390 // add new weapons here
391 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
392 return WEP_ROCKET_LAUNCHER;
393 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
395 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
397 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
398 return WEP_GRENADE_LAUNCHER;
399 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
401 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
403 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
405 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
407 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
413 // Setup weapon for client (after this raise frame will be launched)
414 void weapon_setup(float windex, string wname, float hudammo)
416 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
417 self.items = self.items | hudammo;
419 // the two weapon entities will notice this has changed and update their models
420 self.weapon = windex;
421 self.weaponname = wname;
424 // perform weapon to attack (weaponstate and attack_finished check is here)
426 float weapon_prepareattack(float secondary, float attacktime)
428 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
429 //if all players readied up and the countdown is running
430 if (cvar("sv_ready_restart_after_countdown"))
431 if(time < restart_countdown || time < self.race_penalty) {
435 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
437 self.switchweapon = w_getbestweapon(self);
438 if (self.switchweapon != self.weapon)
439 self.cnt = self.weapon;
443 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
446 // don't fire if previous attack is not finished
447 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
449 // don't fire while changing weapon
450 if (self.weaponentity.state != WS_READY)
452 self.weaponentity.state = WS_INUSE;
453 // if the weapon hasn't been firing continuously, reset the timer
454 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
456 ATTACK_FINISHED(self) = time;
457 //dprint("resetting attack finished to ", ftos(time), "\n");
459 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
460 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
464 void weapon_thinkf(float fr, float t, void() func)
468 if (self.weaponentity != world)
469 self.weaponentity.frame = fr;
472 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
474 backtrace("Tried to override initial weapon think function - should this really happen?");
479 if(self.runes & RUNE_SPEED)
481 if(self.runes & CURSE_SLOW)
482 t = t * cvar("g_balance_rune_speed_combo_atkrate");
484 t = t * cvar("g_balance_rune_speed_atkrate");
486 else if(self.runes & CURSE_SLOW)
488 t = t * cvar("g_balance_curse_slow_atkrate");
492 // VorteX: haste can be added here
493 if (self.weapon_think == w_ready)
495 self.weapon_nextthink = time;
496 //dprint("started firing at ", ftos(time), "\n");
498 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
500 self.weapon_nextthink = time;
501 //dprint("reset weapon animation timer at ", ftos(time), "\n");
503 self.weapon_nextthink = self.weapon_nextthink + t;
504 self.weapon_think = func;
505 //dprint("next ", ftos(self.weapon_nextthink), "\n");
507 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
509 if (!self.crouch) // shoot anim stands up, this looks bad
512 anim = self.anim_shoot;
514 player_setanim(anim, FALSE, TRUE, TRUE);
518 void weapon_boblayer1(float spd, vector org)
520 // VorteX: haste can be added here
523 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
531 mdirection = normalize(mvelocity);
532 mspeed = vlen(mvelocity);
534 nstyle = cvar("g_projectiles_newton_style");
538 outvelocity = mvelocity;
542 // true Newtonian projectiles
543 outvelocity = pvelocity + mvelocity;
547 // true Newtonian projectiles with automatic aim adjustment
549 // solve: |outspeed * mdirection - pvelocity| = mspeed
550 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
551 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
555 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
556 // velocity without mdirection component > mspeed
557 // fire at smallest possible mspeed that works?
558 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
563 p = mdirection * pvelocity;
564 q = pvelocity * pvelocity - mspeed * mspeed;
568 //dprint("impossible shot, adjusting\n");
571 outspeed = p + sqrt(D);
572 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
573 outvelocity = mdirection * outspeed;
578 outspeed = mspeed + mdirection * pvelocity;
579 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
580 outvelocity = mdirection * outspeed;
585 outspeed = mspeed + vlen(pvelocity);
586 outvelocity = mdirection * outspeed;
589 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
594 void W_SetupProjectileVelocity(entity missile)
596 if(missile.owner == world)
597 error("Unowned missile");
599 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
602 void weapon_register(float wepcode, float minammo)
606 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
607 s = strcat(s, ftos(wepcode), " "); // impulse
608 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
609 s = strcat(s, ftos(wepcode), " "); // self.weapon code
612 itemcode = W_AmmoItemCode(wepcode);
613 if(itemcode == IT_SHELLS)
615 else if(itemcode == IT_NAILS)
617 else if(itemcode == IT_ROCKETS)
619 else // if(itemcode == IT_CELLS)
622 s = strcat(s, ftos(minammo), "\n");