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(entity ent, vector vecs, float antilag, float recoil, string snd) W_SetupShot =
27 local vector trueaimpoint;
28 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
29 trueaimpoint = trace_endpos;
31 if (cvar("g_shootfromeye"))
32 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x;
33 else if (cvar("g_shootfromcenter"))
34 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + '0 0 1' * vecs_z;
36 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
37 w_shotdir = normalize(trueaimpoint - w_shotorg);
39 // explanation of g_antilag:
40 // if client reports it was aiming at a player, and the serverside trace
41 // says it would miss, change the aim point to the player's new origin,
42 // but only if the shot at the player's new origin would hit of course
44 // FIXME: a much better method for bullet weapons would be to leave a
45 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
46 // ghost corresponding to this player's ping time, and if so it would do
47 // damage to the real player
49 if (self.cursor_trace_ent) // client was aiming at someone
50 if (self.cursor_trace_ent != self) // just to make sure
51 if (self.cursor_trace_ent.takedamage) // and that person is killable
52 if (cvar("g_antilag"))
54 // verify that the shot would miss without antilag
55 // (avoids an issue where guns would always shoot at their origin)
56 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
57 if (!trace_ent.takedamage)
59 // verify that the shot would hit if altered
60 traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
61 if (trace_ent == self.cursor_trace_ent)
63 // verify that the shot would hit in the past
64 if(self.antilag_debug)
65 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
67 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
69 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
70 antilag_restore(self.cursor_trace_ent);
72 if(trace_ent == self.cursor_trace_ent)
75 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
76 //dprint("ANTILAG HIT\n");
80 // prydon cursor aimbot or odd network conditions
81 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
82 if(cvar("developer") >= 2)
86 v = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 ));
87 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 + 0.01));
88 vel = (vplus - v) * (1 / 0.01);
89 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
90 v -= (self.origin + self.view_ofs);
91 // solve: v + X * vel = closest to v_forward axis
92 // project into 2D by subtracting v_forward components:
93 v -= (v * v_forward) * v_forward;
94 vel -= (vel * v_forward) * v_forward;
95 // solve: v + X * vel = closest to origin
96 // (v + X * vel)^2 closest to 0
97 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
98 X = -(v * vel) / (vel * vel);
99 dprint("... could have adjusted antilag by ", ftos(X), "\n");
100 dprint(" (that is, ", ftos(self.ping + X * 1000), " instead of ", ftos(self.ping), "\n");
108 self.punchangle_x = recoil * -1;
111 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
113 if (self.items & IT_STRENGTH)
115 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
118 void LaserTarget_Think()
125 // list of weapons that will use the laser, and the options that enable it
126 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
129 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
134 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
137 // rocket launcher isn't selected, so no laser target.
138 if(self.lasertarget != world)
140 remove(self.lasertarget);
141 self.lasertarget = world;
146 if(!self.lasertarget)
148 // we don't have a lasertarget entity, so spawn one
149 //bprint("create laser target\n");
150 e = self.lasertarget = spawn();
151 e.owner = self.owner; // Its owner is my owner
152 e.classname = "laser_target";
153 e.movetype = MOVETYPE_NOCLIP; // don't touch things
154 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
155 e.scale = 1.25; // make it larger
156 e.alpha = 0.25; // transparency
157 e.colormod = '255 0 0' * (1/255) * 8; // change colors
158 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
159 // make it dynamically glow
160 // you should avoid over-using this, as it can slow down the player's computer.
161 e.glow_color = 251; // red color
165 e = self.lasertarget;
167 // move the laser dot to where the player is looking
169 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
170 offset = '0 0 26' + v_right*3;
171 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
172 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
173 if(trace_plane_normal != '0 0 0')
174 e.angles = vectoangles(trace_plane_normal);
176 e.angles = vectoangles(v_forward);
179 float CL_Weaponentity_CustomizeEntityForClient()
181 self.viewmodelforclient = self.owner;
182 if(other.classname == "spectator")
183 if(other.enemy == self.owner)
184 self.viewmodelforclient = other;
189 void() CL_Weaponentity_Think =
191 self.nextthink = time;
192 if (intermission_running)
193 self.frame = WFRAME_IDLE;
194 if (self.owner.weaponentity != self)
199 if (self.owner.deadflag != DEAD_NO)
204 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
206 self.cnt = self.owner.weapon;
207 self.dmg = self.owner.modelindex;
208 self.deadflag = self.owner.deadflag;
209 if (self.owner.weaponname != "")
210 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
214 self.effects = self.owner.effects | EF_LOWPRECISION;
215 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
217 if (self.flags & FL_FLY)
218 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
219 self.effects = self.effects - (self.effects & EF_NODRAW);
221 if(self.owner.alpha >= 0)
222 self.alpha = self.owner.alpha;
225 self.colormap = self.owner.colormap;
227 self.angles = '0 0 0';
230 if (self.state == WS_RAISE)
231 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
232 else if (self.state == WS_DROP)
233 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
234 else if (self.state == WS_CLEAR)
236 self.angles_x = -90 * f * f;
238 // create or update the lasertarget entity
242 void() CL_ExteriorWeaponentity_Think =
245 self.nextthink = time;
246 if (self.owner.exteriorweaponentity != self)
251 if (self.owner.deadflag != DEAD_NO)
256 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
258 self.cnt = self.owner.weapon;
259 self.dmg = self.owner.modelindex;
260 self.deadflag = self.owner.deadflag;
261 if (self.owner.weaponname != "")
262 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
266 if((tag_found = gettagindex(self.owner, "tag_weapon")))
268 self.tag_index = tag_found;
269 self.tag_entity = self.owner;
272 setattachment(self, self.owner, "bip01 r hand");
274 // if that didn't find a tag, hide the exterior weapon model
278 self.effects = self.owner.effects | EF_LOWPRECISION;
279 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
280 if(self.owner.alpha >= 0)
281 self.alpha = self.owner.alpha;
284 self.colormap = self.owner.colormap;
287 // spawning weaponentity for client
288 void() CL_SpawnWeaponentity =
290 self.weaponentity = spawn();
291 self.weaponentity.solid = SOLID_NOT;
292 self.weaponentity.owner = self;
293 self.weaponentity.weaponentity = self.weaponentity;
294 setmodel(self.weaponentity, ""); // precision set when changed
295 self.weaponentity.origin = '0 0 0';
296 self.weaponentity.angles = '0 0 0';
297 self.weaponentity.viewmodelforclient = self;
298 self.weaponentity.flags = 0;
299 self.weaponentity.think = CL_Weaponentity_Think;
300 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
301 self.weaponentity.nextthink = time;
302 self.weaponentity.scale = 0.61;
304 self.exteriorweaponentity = spawn();
305 self.exteriorweaponentity.solid = SOLID_NOT;
306 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
307 self.exteriorweaponentity.owner = self;
308 self.exteriorweaponentity.origin = '0 0 0';
309 self.exteriorweaponentity.angles = '0 0 0';
310 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
311 self.exteriorweaponentity.nextthink = time;
314 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
316 local float itemcode, f;
317 local entity oldself;
319 if (wpn < WEP_FIRST || wpn > WEP_LAST)
322 sprint(self, "Invalid weapon\n");
325 itemcode = W_ItemCode(wpn);
326 if (cl.items & itemcode)
332 f = weapon_action(wpn, WR_CHECKAMMO1);
333 f = f + weapon_action(wpn, WR_CHECKAMMO2);
338 sprint(self, "You don't have any ammo for that weapon\n");
347 // Report Proper Weapon Status / Modified Weapon Ownership Message
348 if(itemsInMap & itemcode)
349 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
351 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
359 if (self.weapon != -1)
361 if (self.weaponentity)
363 self.weaponentity.state = WS_CLEAR;
364 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
365 self.weaponentity.effects = 0;
371 if (self.weaponentity)
373 self.weaponentity.state = WS_READY;
374 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
378 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
379 float(entity e) w_getbestweapon
380 {// add new weapons here
381 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
382 return WEP_ROCKET_LAUNCHER;
383 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
385 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
387 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
388 return WEP_GRENADE_LAUNCHER;
389 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
391 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
393 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
395 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
397 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
403 // Setup weapon for client (after this raise frame will be launched)
404 void(float windex, string wname, float hudammo) weapon_setup =
406 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
407 self.items = self.items | hudammo;
409 // the two weapon entities will notice this has changed and update their models
410 self.weapon = windex;
411 self.weaponname = wname;
414 // perform weapon to attack (weaponstate and attack_finished check is here)
415 float(float secondary, float attacktime) weapon_prepareattack =
417 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
419 self.switchweapon = w_getbestweapon(self);
420 if (self.switchweapon != self.weapon)
421 self.cnt = self.weapon;
424 // don't fire if previous attack is not finished
425 if (ATTACK_FINISHED(self) > time)
427 // don't fire while changing weapon
428 if (self.weaponentity.state != WS_READY)
430 self.weaponentity.state = WS_INUSE;
431 ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
435 void(float fr, float t, void() func) weapon_thinkf =
439 if (self.weaponentity != world)
440 self.weaponentity.frame = fr;
443 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
445 backtrace("Tried to override initial weapon think function - should this really happen?");
450 if(self.runes & RUNE_SPEED)
452 if(self.runes & CURSE_SLOW)
453 t = t * cvar("g_balance_rune_speed_combo_atkrate");
455 t = t * cvar("g_balance_rune_speed_atkrate");
457 else if(self.runes & CURSE_SLOW)
459 t = t * cvar("g_balance_curse_slow_atkrate");
463 // VorteX: haste can be added here
464 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
465 self.weapon_think = func;
467 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
469 if (!self.crouch) // shoot anim stands up, this looks bad
472 anim = self.anim_shoot;
474 player_setanim(anim, FALSE, TRUE, TRUE);
478 void(float spd, vector org) weapon_boblayer1 =
480 // VorteX: haste can be added here
483 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
491 mdirection = normalize(mvelocity);
492 mspeed = vlen(mvelocity);
494 nstyle = cvar("g_projectiles_newton_style");
498 outvelocity = mvelocity;
502 // true Newtonian projectiles
503 outvelocity = pvelocity + mvelocity;
507 // true Newtonian projectiles with automatic aim adjustment
509 // solve: |outspeed * mdirection - pvelocity| = mspeed
510 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
511 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
515 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
516 // velocity without mdirection component > mspeed
517 // fire at smallest possible mspeed that works?
518 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
523 p = mdirection * pvelocity;
524 q = pvelocity * pvelocity - mspeed * mspeed;
528 //dprint("impossible shot, adjusting\n");
531 outspeed = p + sqrt(D);
532 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
533 outvelocity = mdirection * outspeed;
538 outspeed = mspeed + mdirection * pvelocity;
539 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
540 outvelocity = mdirection * outspeed;
545 outspeed = mspeed + vlen(pvelocity);
546 outvelocity = mdirection * outspeed;
549 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
554 void(entity missile) W_SetupProjectileVelocity =
556 if(missile.owner == world)
557 error("Unowned missile");
559 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
562 void(float wepcode, float minammo) weapon_register =
566 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
567 s = strcat(s, ftos(wepcode), " "); // impulse
568 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
569 s = strcat(s, ftos(wepcode), " "); // self.weapon code
572 itemcode = W_AmmoItemCode(wepcode);
573 if(itemcode == IT_SHELLS)
575 else if(itemcode == IT_NAILS)
577 else if(itemcode == IT_ROCKETS)
579 else // if(itemcode == IT_CELLS)
582 s = strcat(s, ftos(minammo), "\n");