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.takedamage) // and that person is killable
49 if (cvar("g_antilag"))
51 // verify that the shot would miss without antilag
52 // (avoids an issue where guns would always shoot at their origin)
53 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
54 if (!trace_ent.takedamage)
56 // verify that the shot would hit if altered
57 traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
58 if (trace_ent == self.cursor_trace_ent)
59 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
63 if (!cvar("g_norecoil"))
64 self.punchangle_x = recoil * -1;
67 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
69 if (self.items & IT_STRENGTH)
70 if (!cvar("g_minstagib"))
71 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
74 void LaserTarget_Think()
81 // list of weapons that will use the laser, and the options that enable it
82 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_laserguided_missile"))
85 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
90 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
93 // rocket launcher isn't selected, so no laser target.
94 if(self.lasertarget != world)
96 remove(self.lasertarget);
97 self.lasertarget = world;
102 if(!self.lasertarget)
104 // we don't have a lasertarget entity, so spawn one
105 //bprint("create laser target\n");
106 e = self.lasertarget = spawn();
107 e.owner = self.owner; // Its owner is my owner
108 e.classname = "laser_target";
109 e.movetype = MOVETYPE_NOCLIP; // don't touch things
110 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
111 e.scale = 1.25; // make it larger
112 e.alpha = 0.25; // transparency
113 e.colormod = '255 0 0' * (1/255) * 8; // change colors
114 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
115 // make it dynamically glow
116 // you should avoid over-using this, as it can slow down the player's computer.
117 e.glow_color = 251; // red color
121 e = self.lasertarget;
123 // move the laser dot to where the player is looking
125 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
126 offset = '0 0 26' + v_right*3;
127 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
128 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
129 if(trace_plane_normal != '0 0 0')
130 e.angles = vectoangles(trace_plane_normal);
132 e.angles = vectoangles(v_forward);
135 float CL_Weaponentity_CustomizeEntityForClient()
137 self.viewmodelforclient = self.owner;
138 if(other.classname == "spectator")
139 if(other.enemy == self.owner)
140 self.viewmodelforclient = other;
145 void() CL_Weaponentity_Think =
147 self.nextthink = time;
148 if (intermission_running)
149 self.frame = WFRAME_IDLE;
150 if (self.owner.weaponentity != self)
155 if (self.owner.deadflag != DEAD_NO)
160 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
162 self.cnt = self.owner.weapon;
163 self.dmg = self.owner.modelindex;
164 self.deadflag = self.owner.deadflag;
165 if (self.owner.weaponname != "")
166 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
170 self.effects = self.owner.effects | EF_LOWPRECISION;
172 if (self.flags & FL_FLY)
173 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
174 self.effects = self.effects - (self.effects & EF_NODRAW);
176 if(self.owner.alpha >= 0)
177 self.alpha = self.owner.alpha;
180 self.colormap = self.owner.colormap;
182 self.angles = '0 0 0';
185 if (self.state == WS_RAISE)
186 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
187 else if (self.state == WS_DROP)
188 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
189 else if (self.state == WS_CLEAR)
191 self.angles_x = -90 * f * f;
193 // create or update the lasertarget entity
197 void() CL_ExteriorWeaponentity_Think =
199 self.nextthink = time;
200 if (self.owner.exteriorweaponentity != self)
205 if (self.owner.deadflag != DEAD_NO)
210 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
212 self.cnt = self.owner.weapon;
213 self.dmg = self.owner.modelindex;
214 self.deadflag = self.owner.deadflag;
215 if (self.owner.weaponname != "")
216 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
219 setattachment(self, self.owner, "bip01 r hand");
220 // if that didn't find a tag, hide the exterior weapon model
224 self.effects = self.owner.effects | EF_LOWPRECISION;
225 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
226 if(self.owner.alpha >= 0)
227 self.alpha = self.owner.alpha;
230 self.colormap = self.owner.colormap;
233 // spawning weaponentity for client
234 void() CL_SpawnWeaponentity =
236 self.weaponentity = spawn();
237 self.weaponentity.solid = SOLID_NOT;
238 self.weaponentity.owner = self;
239 self.weaponentity.weaponentity = self.weaponentity;
240 setmodel(self.weaponentity, ""); // precision set when changed
241 self.weaponentity.origin = '0 0 0';
242 self.weaponentity.angles = '0 0 0';
243 self.weaponentity.viewmodelforclient = self;
244 self.weaponentity.flags = 0;
245 self.weaponentity.think = CL_Weaponentity_Think;
246 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
247 self.weaponentity.nextthink = time;
248 self.weaponentity.scale = 0.61;
250 self.exteriorweaponentity = spawn();
251 self.exteriorweaponentity.solid = SOLID_NOT;
252 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
253 self.exteriorweaponentity.owner = self;
254 self.exteriorweaponentity.origin = '0 0 0';
255 self.exteriorweaponentity.angles = '0 0 0';
256 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
257 self.exteriorweaponentity.nextthink = time;
260 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
262 local float itemcode, f;
263 local entity oldself;
265 if (wpn < WEP_FIRST || wpn > WEP_LAST)
268 sprint(self, "Invalid weapon\n");
271 itemcode = W_ItemCode(wpn);
272 if (cl.items & itemcode)
278 f = weapon_action(wpn, WR_CHECKAMMO1);
279 f = f + weapon_action(wpn, WR_CHECKAMMO2);
284 sprint(self, "You don't have any ammo for that weapon\n");
293 // Report Proper Weapon Status / Modified Weapon Ownership Message
294 if(itemsInMap & itemcode)
295 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
297 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
305 if (self.weapon != -1)
307 if (self.weaponentity)
309 self.weaponentity.state = WS_CLEAR;
310 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
311 self.weaponentity.effects = 0;
317 if (self.weaponentity)
319 self.weaponentity.state = WS_READY;
320 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
324 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
325 float(entity e) w_getbestweapon
326 {// add new weapons here
327 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
328 return WEP_ROCKET_LAUNCHER;
329 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
331 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
333 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
334 return WEP_GRENADE_LAUNCHER;
335 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
337 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
339 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
341 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
343 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
349 // Setup weapon for client (after this raise frame will be launched)
350 void(float windex, string wname, float hudammo) weapon_setup =
352 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
353 self.items = self.items | hudammo;
355 // the two weapon entities will notice this has changed and update their models
356 self.weapon = windex;
357 self.weaponname = wname;
359 // might fire faster after switch
360 self.attack_finished = min(max(time, self.attack_finished_old + 1), self.attack_finished);
363 // perform weapon to attack (weaponstate and attack_finished check is here)
364 float(float secondary, float attacktime) weapon_prepareattack =
366 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
368 self.switchweapon = w_getbestweapon(self);
369 if (self.switchweapon != self.weapon)
370 self.cnt = self.weapon;
373 // don't fire if previous attack is not finished
374 if (self.attack_finished > time)
376 // don't fire while changing weapon
377 if (self.weaponentity.state != WS_READY)
379 self.weaponentity.state = WS_INUSE;
380 self.attack_finished_old = self.attack_finished;
381 self.attack_finished = max(time, self.attack_finished + attacktime);
385 void(float fr, float t, void() func) weapon_thinkf =
389 if (self.weaponentity != world)
390 self.weaponentity.frame = fr;
393 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
395 backtrace("Tried to override initial weapon think function - should this really happen?");
398 if(cvar("g_runematch"))
400 if(self.runes & RUNE_SPEED)
402 if(self.runes & CURSE_SLOW)
403 t = t * cvar("g_balance_rune_speed_combo_atkrate");
405 t = t * cvar("g_balance_rune_speed_atkrate");
407 else if(self.runes & CURSE_SLOW)
409 t = t * cvar("g_balance_curse_slow_atkrate");
413 // VorteX: haste can be added here
414 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
415 self.weapon_think = func;
418 void(float spd, vector org) weapon_boblayer1 =
420 // VorteX: haste can be added here
423 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
431 mdirection = normalize(mvelocity);
432 mspeed = vlen(mvelocity);
434 nstyle = cvar("g_projectiles_newton_style");
438 outvelocity = mvelocity;
442 // true Newtonian projectiles
443 outvelocity = pvelocity + mvelocity;
447 // true Newtonian projectiles with automatic aim adjustment
449 // solve: |outspeed * mdirection - pvelocity| = mspeed
450 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
451 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
455 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
456 // velocity without mdirection component > mspeed
457 // fire at smallest possible mspeed that works?
458 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
463 p = mdirection * pvelocity;
464 q = pvelocity * pvelocity - mspeed * mspeed;
468 //dprint("impossible shot, adjusting\n");
471 outspeed = p + sqrt(D);
472 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
473 outvelocity = mdirection * outspeed;
478 outspeed = mspeed + mdirection * pvelocity;
479 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
480 outvelocity = mdirection * outspeed;
485 outspeed = mspeed + vlen(pvelocity);
486 outvelocity = mdirection * outspeed;
489 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
494 void(entity missile) W_SetupProjectileVelocity =
496 if(missile.owner == world)
497 error("Unowned missile");
499 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);