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");
88 self.punchangle_x = recoil * -1;
91 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
93 if (self.items & IT_STRENGTH)
95 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
98 void LaserTarget_Think()
105 // list of weapons that will use the laser, and the options that enable it
106 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
109 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
114 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
117 // rocket launcher isn't selected, so no laser target.
118 if(self.lasertarget != world)
120 remove(self.lasertarget);
121 self.lasertarget = world;
126 if(!self.lasertarget)
128 // we don't have a lasertarget entity, so spawn one
129 //bprint("create laser target\n");
130 e = self.lasertarget = spawn();
131 e.owner = self.owner; // Its owner is my owner
132 e.classname = "laser_target";
133 e.movetype = MOVETYPE_NOCLIP; // don't touch things
134 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
135 e.scale = 1.25; // make it larger
136 e.alpha = 0.25; // transparency
137 e.colormod = '255 0 0' * (1/255) * 8; // change colors
138 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
139 // make it dynamically glow
140 // you should avoid over-using this, as it can slow down the player's computer.
141 e.glow_color = 251; // red color
145 e = self.lasertarget;
147 // move the laser dot to where the player is looking
149 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
150 offset = '0 0 26' + v_right*3;
151 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
152 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
153 if(trace_plane_normal != '0 0 0')
154 e.angles = vectoangles(trace_plane_normal);
156 e.angles = vectoangles(v_forward);
159 float CL_Weaponentity_CustomizeEntityForClient()
161 self.viewmodelforclient = self.owner;
162 if(other.classname == "spectator")
163 if(other.enemy == self.owner)
164 self.viewmodelforclient = other;
169 void() CL_Weaponentity_Think =
171 self.nextthink = time;
172 if (intermission_running)
173 self.frame = WFRAME_IDLE;
174 if (self.owner.weaponentity != self)
179 if (self.owner.deadflag != DEAD_NO)
184 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
186 self.cnt = self.owner.weapon;
187 self.dmg = self.owner.modelindex;
188 self.deadflag = self.owner.deadflag;
189 if (self.owner.weaponname != "")
190 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
194 self.effects = self.owner.effects | EF_LOWPRECISION;
195 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
197 if (self.flags & FL_FLY)
198 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
199 self.effects = self.effects - (self.effects & EF_NODRAW);
201 if(self.owner.alpha >= 0)
202 self.alpha = self.owner.alpha;
205 self.colormap = self.owner.colormap;
207 self.angles = '0 0 0';
210 if (self.state == WS_RAISE)
211 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
212 else if (self.state == WS_DROP)
213 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
214 else if (self.state == WS_CLEAR)
216 self.angles_x = -90 * f * f;
218 // create or update the lasertarget entity
222 void() CL_ExteriorWeaponentity_Think =
225 self.nextthink = time;
226 if (self.owner.exteriorweaponentity != self)
231 if (self.owner.deadflag != DEAD_NO)
236 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
238 self.cnt = self.owner.weapon;
239 self.dmg = self.owner.modelindex;
240 self.deadflag = self.owner.deadflag;
241 if (self.owner.weaponname != "")
242 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
246 if((tag_found = gettagindex(self.owner, "tag_weapon")))
248 self.tag_index = tag_found;
249 self.tag_entity = self.owner;
252 setattachment(self, self.owner, "bip01 r hand");
254 // if that didn't find a tag, hide the exterior weapon model
258 self.effects = self.owner.effects | EF_LOWPRECISION;
259 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
260 if(self.owner.alpha >= 0)
261 self.alpha = self.owner.alpha;
264 self.colormap = self.owner.colormap;
267 // spawning weaponentity for client
268 void() CL_SpawnWeaponentity =
270 self.weaponentity = spawn();
271 self.weaponentity.solid = SOLID_NOT;
272 self.weaponentity.owner = self;
273 self.weaponentity.weaponentity = self.weaponentity;
274 setmodel(self.weaponentity, ""); // precision set when changed
275 self.weaponentity.origin = '0 0 0';
276 self.weaponentity.angles = '0 0 0';
277 self.weaponentity.viewmodelforclient = self;
278 self.weaponentity.flags = 0;
279 self.weaponentity.think = CL_Weaponentity_Think;
280 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
281 self.weaponentity.nextthink = time;
282 self.weaponentity.scale = 0.61;
284 self.exteriorweaponentity = spawn();
285 self.exteriorweaponentity.solid = SOLID_NOT;
286 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
287 self.exteriorweaponentity.owner = self;
288 self.exteriorweaponentity.origin = '0 0 0';
289 self.exteriorweaponentity.angles = '0 0 0';
290 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
291 self.exteriorweaponentity.nextthink = time;
294 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
296 local float itemcode, f;
297 local entity oldself;
299 if (wpn < WEP_FIRST || wpn > WEP_LAST)
302 sprint(self, "Invalid weapon\n");
305 itemcode = W_ItemCode(wpn);
306 if (cl.items & itemcode)
312 f = weapon_action(wpn, WR_CHECKAMMO1);
313 f = f + weapon_action(wpn, WR_CHECKAMMO2);
318 sprint(self, "You don't have any ammo for that weapon\n");
327 // Report Proper Weapon Status / Modified Weapon Ownership Message
328 if(itemsInMap & itemcode)
329 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
331 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
339 if (self.weapon != -1)
341 if (self.weaponentity)
343 self.weaponentity.state = WS_CLEAR;
344 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
345 self.weaponentity.effects = 0;
351 if (self.weaponentity)
353 self.weaponentity.state = WS_READY;
354 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
358 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
359 float(entity e) w_getbestweapon
360 {// add new weapons here
361 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
362 return WEP_ROCKET_LAUNCHER;
363 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
365 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
367 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
368 return WEP_GRENADE_LAUNCHER;
369 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
371 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
373 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
375 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
377 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
383 // Setup weapon for client (after this raise frame will be launched)
384 void(float windex, string wname, float hudammo) weapon_setup =
386 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
387 self.items = self.items | hudammo;
389 // the two weapon entities will notice this has changed and update their models
390 self.weapon = windex;
391 self.weaponname = wname;
394 // perform weapon to attack (weaponstate and attack_finished check is here)
395 float(float secondary, float attacktime) weapon_prepareattack =
397 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
399 self.switchweapon = w_getbestweapon(self);
400 if (self.switchweapon != self.weapon)
401 self.cnt = self.weapon;
404 // don't fire if previous attack is not finished
405 if (ATTACK_FINISHED(self) > time)
407 // don't fire while changing weapon
408 if (self.weaponentity.state != WS_READY)
410 self.weaponentity.state = WS_INUSE;
411 ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
415 void(float fr, float t, void() func) weapon_thinkf =
419 if (self.weaponentity != world)
420 self.weaponentity.frame = fr;
423 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
425 backtrace("Tried to override initial weapon think function - should this really happen?");
430 if(self.runes & RUNE_SPEED)
432 if(self.runes & CURSE_SLOW)
433 t = t * cvar("g_balance_rune_speed_combo_atkrate");
435 t = t * cvar("g_balance_rune_speed_atkrate");
437 else if(self.runes & CURSE_SLOW)
439 t = t * cvar("g_balance_curse_slow_atkrate");
443 // VorteX: haste can be added here
444 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
445 self.weapon_think = func;
447 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
449 if (!self.crouch) // shoot anim stands up, this looks bad
452 anim = self.anim_shoot;
454 player_setanim(anim, FALSE, TRUE, TRUE);
458 void(float spd, vector org) weapon_boblayer1 =
460 // VorteX: haste can be added here
463 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
471 mdirection = normalize(mvelocity);
472 mspeed = vlen(mvelocity);
474 nstyle = cvar("g_projectiles_newton_style");
478 outvelocity = mvelocity;
482 // true Newtonian projectiles
483 outvelocity = pvelocity + mvelocity;
487 // true Newtonian projectiles with automatic aim adjustment
489 // solve: |outspeed * mdirection - pvelocity| = mspeed
490 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
491 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
495 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
496 // velocity without mdirection component > mspeed
497 // fire at smallest possible mspeed that works?
498 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
503 p = mdirection * pvelocity;
504 q = pvelocity * pvelocity - mspeed * mspeed;
508 //dprint("impossible shot, adjusting\n");
511 outspeed = p + sqrt(D);
512 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
513 outvelocity = mdirection * outspeed;
518 outspeed = mspeed + mdirection * pvelocity;
519 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
520 outvelocity = mdirection * outspeed;
525 outspeed = mspeed + vlen(pvelocity);
526 outvelocity = mdirection * outspeed;
529 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
534 void(entity missile) W_SetupProjectileVelocity =
536 if(missile.owner == world)
537 error("Unowned missile");
539 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
542 void(float wepcode, float minammo) weapon_register =
546 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
547 s = strcat(s, ftos(wepcode), " "); // impulse
548 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
549 s = strcat(s, ftos(wepcode), " "); // self.weapon code
552 itemcode = W_AmmoItemCode(wepcode);
553 if(itemcode == IT_SHELLS)
555 else if(itemcode == IT_NAILS)
557 else if(itemcode == IT_ROCKETS)
559 else // if(itemcode == IT_CELLS)
562 s = strcat(s, ftos(minammo), "\n");