2 ===========================================================================
4 CLIENT WEAPONSYSTEM CODE
5 Bring back W_Weaponframe
7 ===========================================================================
10 // VorteX: static frame globals
11 float WFRAME_FIRE1 = 0;
12 float WFRAME_FIRE2 = 1;
13 float WFRAME_IDLE = 2;
15 void(float fr, float t, void() func) weapon_thinkf;
20 // this function calculates w_shotorg and w_shotdir based on the weapon model
21 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
22 // make sure you call makevectors first (FIXME?)
23 void(entity ent, vector vecs, float antilag, float recoil, string snd) W_SetupShot =
25 local vector trueaimpoint;
26 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
27 trueaimpoint = trace_endpos;
29 if (cvar("g_shootfromeye"))
30 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x;
31 else if (cvar("g_shootfromcenter"))
32 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + '0 0 1' * vecs_z;
34 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
35 w_shotdir = normalize(trueaimpoint - w_shotorg);
37 // explanation of g_antilag:
38 // if client reports it was aiming at a player, and the serverside trace
39 // says it would miss, change the aim point to the player's new origin,
40 // but only if the shot at the player's new origin would hit of course
42 // FIXME: a much better method for bullet weapons would be to leave a
43 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
44 // ghost corresponding to this player's ping time, and if so it would do
45 // damage to the real player
47 if (self.cursor_trace_ent) // client was aiming at someone
48 if (self.cursor_trace_ent != self) // just to make sure
49 if (self.cursor_trace_ent.takedamage) // and that person is killable
50 if (cvar("g_antilag"))
52 // verify that the shot would miss without antilag
53 // (avoids an issue where guns would always shoot at their origin)
54 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
55 if (!trace_ent.takedamage)
57 // verify that the shot would hit if altered
58 traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
59 if (trace_ent == self.cursor_trace_ent)
61 // verify that the shot would hit in the past
62 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
64 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
65 if(trace_ent == self.cursor_trace_ent)
68 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
72 // prydon cursor aimbot or odd network conditions
73 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
76 antilag_restore(self.cursor_trace_ent);
82 self.punchangle_x = recoil * -1;
85 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
87 if (self.items & IT_STRENGTH)
89 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
92 void LaserTarget_Think()
99 // list of weapons that will use the laser, and the options that enable it
100 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
103 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
108 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
111 // rocket launcher isn't selected, so no laser target.
112 if(self.lasertarget != world)
114 remove(self.lasertarget);
115 self.lasertarget = world;
120 if(!self.lasertarget)
122 // we don't have a lasertarget entity, so spawn one
123 //bprint("create laser target\n");
124 e = self.lasertarget = spawn();
125 e.owner = self.owner; // Its owner is my owner
126 e.classname = "laser_target";
127 e.movetype = MOVETYPE_NOCLIP; // don't touch things
128 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
129 e.scale = 1.25; // make it larger
130 e.alpha = 0.25; // transparency
131 e.colormod = '255 0 0' * (1/255) * 8; // change colors
132 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
133 // make it dynamically glow
134 // you should avoid over-using this, as it can slow down the player's computer.
135 e.glow_color = 251; // red color
139 e = self.lasertarget;
141 // move the laser dot to where the player is looking
143 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
144 offset = '0 0 26' + v_right*3;
145 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
146 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
147 if(trace_plane_normal != '0 0 0')
148 e.angles = vectoangles(trace_plane_normal);
150 e.angles = vectoangles(v_forward);
153 float CL_Weaponentity_CustomizeEntityForClient()
155 self.viewmodelforclient = self.owner;
156 if(other.classname == "spectator")
157 if(other.enemy == self.owner)
158 self.viewmodelforclient = other;
163 void() CL_Weaponentity_Think =
165 self.nextthink = time;
166 if (intermission_running)
167 self.frame = WFRAME_IDLE;
168 if (self.owner.weaponentity != self)
173 if (self.owner.deadflag != DEAD_NO)
178 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
180 self.cnt = self.owner.weapon;
181 self.dmg = self.owner.modelindex;
182 self.deadflag = self.owner.deadflag;
183 if (self.owner.weaponname != "")
184 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
188 self.effects = self.owner.effects | EF_LOWPRECISION;
189 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
191 if (self.flags & FL_FLY)
192 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
193 self.effects = self.effects - (self.effects & EF_NODRAW);
195 if(self.owner.alpha >= 0)
196 self.alpha = self.owner.alpha;
199 self.colormap = self.owner.colormap;
201 self.angles = '0 0 0';
204 if (self.state == WS_RAISE)
205 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
206 else if (self.state == WS_DROP)
207 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
208 else if (self.state == WS_CLEAR)
210 self.angles_x = -90 * f * f;
212 // create or update the lasertarget entity
216 void() CL_ExteriorWeaponentity_Think =
219 self.nextthink = time;
220 if (self.owner.exteriorweaponentity != self)
225 if (self.owner.deadflag != DEAD_NO)
230 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
232 self.cnt = self.owner.weapon;
233 self.dmg = self.owner.modelindex;
234 self.deadflag = self.owner.deadflag;
235 if (self.owner.weaponname != "")
236 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
240 if((tag_found = gettagindex(self.owner, "tag_weapon")))
242 self.tag_index = tag_found;
243 self.tag_entity = self.owner;
246 setattachment(self, self.owner, "bip01 r hand");
248 // if that didn't find a tag, hide the exterior weapon model
252 self.effects = self.owner.effects | EF_LOWPRECISION;
253 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
254 if(self.owner.alpha >= 0)
255 self.alpha = self.owner.alpha;
258 self.colormap = self.owner.colormap;
261 // spawning weaponentity for client
262 void() CL_SpawnWeaponentity =
264 self.weaponentity = spawn();
265 self.weaponentity.solid = SOLID_NOT;
266 self.weaponentity.owner = self;
267 self.weaponentity.weaponentity = self.weaponentity;
268 setmodel(self.weaponentity, ""); // precision set when changed
269 self.weaponentity.origin = '0 0 0';
270 self.weaponentity.angles = '0 0 0';
271 self.weaponentity.viewmodelforclient = self;
272 self.weaponentity.flags = 0;
273 self.weaponentity.think = CL_Weaponentity_Think;
274 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
275 self.weaponentity.nextthink = time;
276 self.weaponentity.scale = 0.61;
278 self.exteriorweaponentity = spawn();
279 self.exteriorweaponentity.solid = SOLID_NOT;
280 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
281 self.exteriorweaponentity.owner = self;
282 self.exteriorweaponentity.origin = '0 0 0';
283 self.exteriorweaponentity.angles = '0 0 0';
284 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
285 self.exteriorweaponentity.nextthink = time;
288 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
290 local float itemcode, f;
291 local entity oldself;
293 if (wpn < WEP_FIRST || wpn > WEP_LAST)
296 sprint(self, "Invalid weapon\n");
299 itemcode = W_ItemCode(wpn);
300 if (cl.items & itemcode)
306 f = weapon_action(wpn, WR_CHECKAMMO1);
307 f = f + weapon_action(wpn, WR_CHECKAMMO2);
312 sprint(self, "You don't have any ammo for that weapon\n");
321 // Report Proper Weapon Status / Modified Weapon Ownership Message
322 if(itemsInMap & itemcode)
323 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
325 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
333 if (self.weapon != -1)
335 if (self.weaponentity)
337 self.weaponentity.state = WS_CLEAR;
338 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
339 self.weaponentity.effects = 0;
345 if (self.weaponentity)
347 self.weaponentity.state = WS_READY;
348 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
352 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
353 float(entity e) w_getbestweapon
354 {// add new weapons here
355 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
356 return WEP_ROCKET_LAUNCHER;
357 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
359 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
361 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
362 return WEP_GRENADE_LAUNCHER;
363 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
365 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
367 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
369 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
371 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
377 // Setup weapon for client (after this raise frame will be launched)
378 void(float windex, string wname, float hudammo) weapon_setup =
380 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
381 self.items = self.items | hudammo;
383 // the two weapon entities will notice this has changed and update their models
384 self.weapon = windex;
385 self.weaponname = wname;
388 // perform weapon to attack (weaponstate and attack_finished check is here)
389 float(float secondary, float attacktime) weapon_prepareattack =
391 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
393 self.switchweapon = w_getbestweapon(self);
394 if (self.switchweapon != self.weapon)
395 self.cnt = self.weapon;
398 // don't fire if previous attack is not finished
399 if (ATTACK_FINISHED(self) > time)
401 // don't fire while changing weapon
402 if (self.weaponentity.state != WS_READY)
404 self.weaponentity.state = WS_INUSE;
405 ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
409 void(float fr, float t, void() func) weapon_thinkf =
413 if (self.weaponentity != world)
414 self.weaponentity.frame = fr;
417 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
419 backtrace("Tried to override initial weapon think function - should this really happen?");
424 if(self.runes & RUNE_SPEED)
426 if(self.runes & CURSE_SLOW)
427 t = t * cvar("g_balance_rune_speed_combo_atkrate");
429 t = t * cvar("g_balance_rune_speed_atkrate");
431 else if(self.runes & CURSE_SLOW)
433 t = t * cvar("g_balance_curse_slow_atkrate");
437 // VorteX: haste can be added here
438 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
439 self.weapon_think = func;
441 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
443 if (!self.crouch) // shoot anim stands up, this looks bad
446 anim = self.anim_shoot;
448 player_setanim(anim, FALSE, TRUE, TRUE);
452 void(float spd, vector org) weapon_boblayer1 =
454 // VorteX: haste can be added here
457 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
465 mdirection = normalize(mvelocity);
466 mspeed = vlen(mvelocity);
468 nstyle = cvar("g_projectiles_newton_style");
472 outvelocity = mvelocity;
476 // true Newtonian projectiles
477 outvelocity = pvelocity + mvelocity;
481 // true Newtonian projectiles with automatic aim adjustment
483 // solve: |outspeed * mdirection - pvelocity| = mspeed
484 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
485 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
489 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
490 // velocity without mdirection component > mspeed
491 // fire at smallest possible mspeed that works?
492 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
497 p = mdirection * pvelocity;
498 q = pvelocity * pvelocity - mspeed * mspeed;
502 //dprint("impossible shot, adjusting\n");
505 outspeed = p + sqrt(D);
506 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
507 outvelocity = mdirection * outspeed;
512 outspeed = mspeed + mdirection * pvelocity;
513 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
514 outvelocity = mdirection * outspeed;
519 outspeed = mspeed + vlen(pvelocity);
520 outvelocity = mdirection * outspeed;
523 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
528 void(entity missile) W_SetupProjectileVelocity =
530 if(missile.owner == world)
531 error("Unowned missile");
533 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
536 void(float wepcode, float minammo) weapon_register =
540 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
541 s = strcat(s, ftos(wepcode), " "); // impulse
542 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
543 s = strcat(s, ftos(wepcode), " "); // self.weapon code
546 itemcode = W_AmmoItemCode(wepcode);
547 if(itemcode == IT_SHELLS)
549 else if(itemcode == IT_NAILS)
551 else if(itemcode == IT_ROCKETS)
553 else // if(itemcode == IT_CELLS)
556 s = strcat(s, ftos(minammo), "\n");