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
48 self.cursor_trace_ent = nextent(self);
50 if (self.cursor_trace_ent) // client was aiming at someone
51 if (self.cursor_trace_ent != self) // just to make sure
52 if (self.cursor_trace_ent.takedamage) // and that person is killable
53 if (cvar("g_antilag"))
55 // verify that the shot would miss without antilag
56 // (avoids an issue where guns would always shoot at their origin)
57 traceline_hitcorpse(self, w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
58 if (!trace_ent.takedamage)
60 // verify that the shot would hit if altered
61 traceline_hitcorpse(self, w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
62 if (trace_ent == self.cursor_trace_ent)
64 // verify that the shot would hit in the past
65 if(self.antilag_debug)
66 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
68 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
70 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
71 antilag_restore(self.cursor_trace_ent);
73 if(trace_ent == self.cursor_trace_ent)
76 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
77 //dprint("ANTILAG HIT\n");
81 // prydon cursor aimbot or odd network conditions
82 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
89 self.punchangle_x = recoil * -1;
92 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
94 if (self.items & IT_STRENGTH)
96 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
99 void LaserTarget_Think()
106 // list of weapons that will use the laser, and the options that enable it
107 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
110 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
115 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
118 // rocket launcher isn't selected, so no laser target.
119 if(self.lasertarget != world)
121 remove(self.lasertarget);
122 self.lasertarget = world;
127 if(!self.lasertarget)
129 // we don't have a lasertarget entity, so spawn one
130 //bprint("create laser target\n");
131 e = self.lasertarget = spawn();
132 e.owner = self.owner; // Its owner is my owner
133 e.classname = "laser_target";
134 e.movetype = MOVETYPE_NOCLIP; // don't touch things
135 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
136 e.scale = 1.25; // make it larger
137 e.alpha = 0.25; // transparency
138 e.colormod = '255 0 0' * (1/255) * 8; // change colors
139 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
140 // make it dynamically glow
141 // you should avoid over-using this, as it can slow down the player's computer.
142 e.glow_color = 251; // red color
146 e = self.lasertarget;
148 // move the laser dot to where the player is looking
150 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
151 offset = '0 0 26' + v_right*3;
152 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
153 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
154 if(trace_plane_normal != '0 0 0')
155 e.angles = vectoangles(trace_plane_normal);
157 e.angles = vectoangles(v_forward);
160 float CL_Weaponentity_CustomizeEntityForClient()
162 self.viewmodelforclient = self.owner;
163 if(other.classname == "spectator")
164 if(other.enemy == self.owner)
165 self.viewmodelforclient = other;
170 void() CL_Weaponentity_Think =
172 self.nextthink = time;
173 if (intermission_running)
174 self.frame = WFRAME_IDLE;
175 if (self.owner.weaponentity != self)
180 if (self.owner.deadflag != DEAD_NO)
185 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
187 self.cnt = self.owner.weapon;
188 self.dmg = self.owner.modelindex;
189 self.deadflag = self.owner.deadflag;
190 if (self.owner.weaponname != "")
191 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
195 self.effects = self.owner.effects | EF_LOWPRECISION;
196 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
198 if (self.flags & FL_FLY)
199 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
200 self.effects = self.effects - (self.effects & EF_NODRAW);
202 if(self.owner.alpha >= 0)
203 self.alpha = self.owner.alpha;
206 self.colormap = self.owner.colormap;
208 self.angles = '0 0 0';
211 if (self.state == WS_RAISE)
212 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
213 else if (self.state == WS_DROP)
214 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
215 else if (self.state == WS_CLEAR)
217 self.angles_x = -90 * f * f;
219 // create or update the lasertarget entity
223 void() CL_ExteriorWeaponentity_Think =
226 self.nextthink = time;
227 if (self.owner.exteriorweaponentity != self)
232 if (self.owner.deadflag != DEAD_NO)
237 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
239 self.cnt = self.owner.weapon;
240 self.dmg = self.owner.modelindex;
241 self.deadflag = self.owner.deadflag;
242 if (self.owner.weaponname != "")
243 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
247 if((tag_found = gettagindex(self.owner, "tag_weapon")))
249 self.tag_index = tag_found;
250 self.tag_entity = self.owner;
253 setattachment(self, self.owner, "bip01 r hand");
255 // if that didn't find a tag, hide the exterior weapon model
259 self.effects = self.owner.effects | EF_LOWPRECISION;
260 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
261 if(self.owner.alpha >= 0)
262 self.alpha = self.owner.alpha;
265 self.colormap = self.owner.colormap;
268 // spawning weaponentity for client
269 void() CL_SpawnWeaponentity =
271 self.weaponentity = spawn();
272 self.weaponentity.solid = SOLID_NOT;
273 self.weaponentity.owner = self;
274 self.weaponentity.weaponentity = self.weaponentity;
275 setmodel(self.weaponentity, ""); // precision set when changed
276 self.weaponentity.origin = '0 0 0';
277 self.weaponentity.angles = '0 0 0';
278 self.weaponentity.viewmodelforclient = self;
279 self.weaponentity.flags = 0;
280 self.weaponentity.think = CL_Weaponentity_Think;
281 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
282 self.weaponentity.nextthink = time;
283 self.weaponentity.scale = 0.61;
285 self.exteriorweaponentity = spawn();
286 self.exteriorweaponentity.solid = SOLID_NOT;
287 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
288 self.exteriorweaponentity.owner = self;
289 self.exteriorweaponentity.origin = '0 0 0';
290 self.exteriorweaponentity.angles = '0 0 0';
291 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
292 self.exteriorweaponentity.nextthink = time;
295 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
297 local float itemcode, f;
298 local entity oldself;
300 if (wpn < WEP_FIRST || wpn > WEP_LAST)
303 sprint(self, "Invalid weapon\n");
306 itemcode = W_ItemCode(wpn);
307 if (cl.items & itemcode)
313 f = weapon_action(wpn, WR_CHECKAMMO1);
314 f = f + weapon_action(wpn, WR_CHECKAMMO2);
319 sprint(self, "You don't have any ammo for that weapon\n");
328 // Report Proper Weapon Status / Modified Weapon Ownership Message
329 if(itemsInMap & itemcode)
330 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
332 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
340 if (self.weapon != -1)
342 if (self.weaponentity)
344 self.weaponentity.state = WS_CLEAR;
345 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
346 self.weaponentity.effects = 0;
352 if (self.weaponentity)
354 self.weaponentity.state = WS_READY;
355 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
359 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
360 float(entity e) w_getbestweapon
361 {// add new weapons here
362 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
363 return WEP_ROCKET_LAUNCHER;
364 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
366 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
368 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
369 return WEP_GRENADE_LAUNCHER;
370 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
372 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
374 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
376 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
378 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
384 // Setup weapon for client (after this raise frame will be launched)
385 void(float windex, string wname, float hudammo) weapon_setup =
387 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
388 self.items = self.items | hudammo;
390 // the two weapon entities will notice this has changed and update their models
391 self.weapon = windex;
392 self.weaponname = wname;
395 // perform weapon to attack (weaponstate and attack_finished check is here)
396 float(float secondary, float attacktime) weapon_prepareattack =
398 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
400 self.switchweapon = w_getbestweapon(self);
401 if (self.switchweapon != self.weapon)
402 self.cnt = self.weapon;
405 // don't fire if previous attack is not finished
406 if (ATTACK_FINISHED(self) > time)
408 // don't fire while changing weapon
409 if (self.weaponentity.state != WS_READY)
411 self.weaponentity.state = WS_INUSE;
412 ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self) + attacktime);
416 void(float fr, float t, void() func) weapon_thinkf =
420 if (self.weaponentity != world)
421 self.weaponentity.frame = fr;
424 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
426 backtrace("Tried to override initial weapon think function - should this really happen?");
431 if(self.runes & RUNE_SPEED)
433 if(self.runes & CURSE_SLOW)
434 t = t * cvar("g_balance_rune_speed_combo_atkrate");
436 t = t * cvar("g_balance_rune_speed_atkrate");
438 else if(self.runes & CURSE_SLOW)
440 t = t * cvar("g_balance_curse_slow_atkrate");
444 // VorteX: haste can be added here
445 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
446 self.weapon_think = func;
448 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
450 if (!self.crouch) // shoot anim stands up, this looks bad
453 anim = self.anim_shoot;
455 player_setanim(anim, FALSE, TRUE, TRUE);
459 void(float spd, vector org) weapon_boblayer1 =
461 // VorteX: haste can be added here
464 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
472 mdirection = normalize(mvelocity);
473 mspeed = vlen(mvelocity);
475 nstyle = cvar("g_projectiles_newton_style");
479 outvelocity = mvelocity;
483 // true Newtonian projectiles
484 outvelocity = pvelocity + mvelocity;
488 // true Newtonian projectiles with automatic aim adjustment
490 // solve: |outspeed * mdirection - pvelocity| = mspeed
491 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
492 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
496 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
497 // velocity without mdirection component > mspeed
498 // fire at smallest possible mspeed that works?
499 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
504 p = mdirection * pvelocity;
505 q = pvelocity * pvelocity - mspeed * mspeed;
509 //dprint("impossible shot, adjusting\n");
512 outspeed = p + sqrt(D);
513 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
514 outvelocity = mdirection * outspeed;
519 outspeed = mspeed + mdirection * pvelocity;
520 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
521 outvelocity = mdirection * outspeed;
526 outspeed = mspeed + vlen(pvelocity);
527 outvelocity = mdirection * outspeed;
530 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
535 void(entity missile) W_SetupProjectileVelocity =
537 if(missile.owner == world)
538 error("Unowned missile");
540 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
543 void(float wepcode, float minammo) weapon_register =
547 s = strcat("register_bestweapon ", ftos(wepcode), " "); // char for bestweapon
548 s = strcat(s, ftos(wepcode), " "); // impulse
549 s = strcat(s, ftos(W_ItemCode(wepcode)), " "); // item code
550 s = strcat(s, ftos(wepcode), " "); // self.weapon code
553 itemcode = W_AmmoItemCode(wepcode);
554 if(itemcode == IT_SHELLS)
556 else if(itemcode == IT_NAILS)
558 else if(itemcode == IT_ROCKETS)
560 else // if(itemcode == IT_CELLS)
563 s = strcat(s, ftos(minammo), "\n");