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, time - self.ping * 0.001);
63 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
65 //te_spark(self.origin, '0 0 0', 1000);
66 //te_spark(self.cursor_trace_ent.origin, '0 0 0', 1000);
68 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);
71 antilag_restore(self.cursor_trace_ent);
73 if(trace_ent == self.cursor_trace_ent)
74 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
77 // wallhack or odd network conditions
78 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
85 self.punchangle_x = recoil * -1;
88 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
90 if (self.items & IT_STRENGTH)
92 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
95 void LaserTarget_Think()
102 // list of weapons that will use the laser, and the options that enable it
103 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
106 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
111 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
114 // rocket launcher isn't selected, so no laser target.
115 if(self.lasertarget != world)
117 remove(self.lasertarget);
118 self.lasertarget = world;
123 if(!self.lasertarget)
125 // we don't have a lasertarget entity, so spawn one
126 //bprint("create laser target\n");
127 e = self.lasertarget = spawn();
128 e.owner = self.owner; // Its owner is my owner
129 e.classname = "laser_target";
130 e.movetype = MOVETYPE_NOCLIP; // don't touch things
131 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
132 e.scale = 1.25; // make it larger
133 e.alpha = 0.25; // transparency
134 e.colormod = '255 0 0' * (1/255) * 8; // change colors
135 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
136 // make it dynamically glow
137 // you should avoid over-using this, as it can slow down the player's computer.
138 e.glow_color = 251; // red color
142 e = self.lasertarget;
144 // move the laser dot to where the player is looking
146 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
147 offset = '0 0 26' + v_right*3;
148 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
149 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
150 if(trace_plane_normal != '0 0 0')
151 e.angles = vectoangles(trace_plane_normal);
153 e.angles = vectoangles(v_forward);
156 float CL_Weaponentity_CustomizeEntityForClient()
158 self.viewmodelforclient = self.owner;
159 if(other.classname == "spectator")
160 if(other.enemy == self.owner)
161 self.viewmodelforclient = other;
166 void() CL_Weaponentity_Think =
168 self.nextthink = time;
169 if (intermission_running)
170 self.frame = WFRAME_IDLE;
171 if (self.owner.weaponentity != self)
176 if (self.owner.deadflag != DEAD_NO)
181 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
183 self.cnt = self.owner.weapon;
184 self.dmg = self.owner.modelindex;
185 self.deadflag = self.owner.deadflag;
186 if (self.owner.weaponname != "")
187 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
191 self.effects = self.owner.effects | EF_LOWPRECISION;
192 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
194 if (self.flags & FL_FLY)
195 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
196 self.effects = self.effects - (self.effects & EF_NODRAW);
198 if(self.owner.alpha >= 0)
199 self.alpha = self.owner.alpha;
202 self.colormap = self.owner.colormap;
204 self.angles = '0 0 0';
207 if (self.state == WS_RAISE)
208 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
209 else if (self.state == WS_DROP)
210 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
211 else if (self.state == WS_CLEAR)
213 self.angles_x = -90 * f * f;
215 // create or update the lasertarget entity
219 void() CL_ExteriorWeaponentity_Think =
222 self.nextthink = time;
223 if (self.owner.exteriorweaponentity != self)
228 if (self.owner.deadflag != DEAD_NO)
233 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
235 self.cnt = self.owner.weapon;
236 self.dmg = self.owner.modelindex;
237 self.deadflag = self.owner.deadflag;
238 if (self.owner.weaponname != "")
239 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
243 if((tag_found = gettagindex(self.owner, "tag_weapon")))
245 self.tag_index = tag_found;
246 self.tag_entity = self.owner;
249 setattachment(self, self.owner, "bip01 r hand");
251 // if that didn't find a tag, hide the exterior weapon model
255 self.effects = self.owner.effects | EF_LOWPRECISION;
256 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
257 if(self.owner.alpha >= 0)
258 self.alpha = self.owner.alpha;
261 self.colormap = self.owner.colormap;
264 // spawning weaponentity for client
265 void() CL_SpawnWeaponentity =
267 self.weaponentity = spawn();
268 self.weaponentity.solid = SOLID_NOT;
269 self.weaponentity.owner = self;
270 self.weaponentity.weaponentity = self.weaponentity;
271 setmodel(self.weaponentity, ""); // precision set when changed
272 self.weaponentity.origin = '0 0 0';
273 self.weaponentity.angles = '0 0 0';
274 self.weaponentity.viewmodelforclient = self;
275 self.weaponentity.flags = 0;
276 self.weaponentity.think = CL_Weaponentity_Think;
277 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
278 self.weaponentity.nextthink = time;
279 self.weaponentity.scale = 0.61;
281 self.exteriorweaponentity = spawn();
282 self.exteriorweaponentity.solid = SOLID_NOT;
283 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
284 self.exteriorweaponentity.owner = self;
285 self.exteriorweaponentity.origin = '0 0 0';
286 self.exteriorweaponentity.angles = '0 0 0';
287 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
288 self.exteriorweaponentity.nextthink = time;
291 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
293 local float itemcode, f;
294 local entity oldself;
296 if (wpn < WEP_FIRST || wpn > WEP_LAST)
299 sprint(self, "Invalid weapon\n");
302 itemcode = W_ItemCode(wpn);
303 if (cl.items & itemcode)
309 f = weapon_action(wpn, WR_CHECKAMMO1);
310 f = f + weapon_action(wpn, WR_CHECKAMMO2);
315 sprint(self, "You don't have any ammo for that weapon\n");
324 // Report Proper Weapon Status / Modified Weapon Ownership Message
325 if(itemsInMap & itemcode)
326 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
328 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
336 if (self.weapon != -1)
338 if (self.weaponentity)
340 self.weaponentity.state = WS_CLEAR;
341 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
342 self.weaponentity.effects = 0;
348 if (self.weaponentity)
350 self.weaponentity.state = WS_READY;
351 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
355 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
356 float(entity e) w_getbestweapon
357 {// add new weapons here
358 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
359 return WEP_ROCKET_LAUNCHER;
360 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
362 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
364 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
365 return WEP_GRENADE_LAUNCHER;
366 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
368 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
370 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
372 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
374 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
380 // Setup weapon for client (after this raise frame will be launched)
381 void(float windex, string wname, float hudammo) weapon_setup =
383 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
384 self.items = self.items | hudammo;
386 // the two weapon entities will notice this has changed and update their models
387 self.weapon = windex;
388 self.weaponname = wname;
391 // perform weapon to attack (weaponstate and attack_finished check is here)
392 float(float secondary, float attacktime) weapon_prepareattack =
394 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
396 self.switchweapon = w_getbestweapon(self);
397 if (self.switchweapon != self.weapon)
398 self.cnt = self.weapon;
401 // don't fire if previous attack is not finished
402 if (ATTACK_FINISHED(self) > time)
404 // don't fire while changing weapon
405 if (self.weaponentity.state != WS_READY)
407 self.weaponentity.state = WS_INUSE;
408 ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
412 void(float fr, float t, void() func) weapon_thinkf =
416 if (self.weaponentity != world)
417 self.weaponentity.frame = fr;
420 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
422 backtrace("Tried to override initial weapon think function - should this really happen?");
427 if(self.runes & RUNE_SPEED)
429 if(self.runes & CURSE_SLOW)
430 t = t * cvar("g_balance_rune_speed_combo_atkrate");
432 t = t * cvar("g_balance_rune_speed_atkrate");
434 else if(self.runes & CURSE_SLOW)
436 t = t * cvar("g_balance_curse_slow_atkrate");
440 // VorteX: haste can be added here
441 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
442 self.weapon_think = func;
444 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
446 if (!self.crouch) // shoot anim stands up, this looks bad
449 anim = self.anim_shoot;
451 player_setanim(anim, FALSE, TRUE, TRUE);
455 void(float spd, vector org) weapon_boblayer1 =
457 // VorteX: haste can be added here
460 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
468 mdirection = normalize(mvelocity);
469 mspeed = vlen(mvelocity);
471 nstyle = cvar("g_projectiles_newton_style");
475 outvelocity = mvelocity;
479 // true Newtonian projectiles
480 outvelocity = pvelocity + mvelocity;
484 // true Newtonian projectiles with automatic aim adjustment
486 // solve: |outspeed * mdirection - pvelocity| = mspeed
487 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
488 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
492 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
493 // velocity without mdirection component > mspeed
494 // fire at smallest possible mspeed that works?
495 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
500 p = mdirection * pvelocity;
501 q = pvelocity * pvelocity - mspeed * mspeed;
505 //dprint("impossible shot, adjusting\n");
508 outspeed = p + sqrt(D);
509 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
510 outvelocity = mdirection * outspeed;
515 outspeed = mspeed + mdirection * pvelocity;
516 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
517 outvelocity = mdirection * outspeed;
522 outspeed = mspeed + vlen(pvelocity);
523 outvelocity = mdirection * outspeed;
526 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
531 void(entity missile) W_SetupProjectileVelocity =
533 if(missile.owner == world)
534 error("Unowned missile");
536 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
539 void(float wepcode, float minammo) weapon_register =
543 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
544 s = strcat(s, ftos(wepcode), " "); // impulse
545 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
546 s = strcat(s, ftos(wepcode), " "); // self.weapon code
549 itemcode = W_AmmoItemCode(wepcode);
550 if(itemcode == IT_SHELLS)
552 else if(itemcode == IT_NAILS)
554 else if(itemcode == IT_ROCKETS)
556 else // if(itemcode == IT_CELLS)
559 s = strcat(s, ftos(minammo), "\n");