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 W_SetupShot(entity ent, vector vecs, float antilag, float recoil, string snd)
27 float nudge = 1; // added to traceline target and subtracted from result
28 local vector trueaimpoint;
30 oldsolid = self.solid;
31 self.solid = SOLID_BBOX; // make all shot test traces bypass playerclip
32 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
33 trueaimpoint = trace_endpos;
35 if (cvar("g_shootfromeye"))
36 w_shotorg = ent.origin + ent.view_ofs;
37 else if (cvar("g_shootfromcenter"))
38 w_shotorg = ent.origin + ent.view_ofs + '0 0 1' * vecs_z;
40 w_shotorg = ent.origin + ent.view_ofs + v_right * vecs_y + v_up * vecs_z;
41 // now move the shotorg forward as much as requested if possible
42 traceline(w_shotorg, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, self);
43 w_shotorg = trace_endpos - v_forward * nudge;
44 // calculate the shotdir from the chosen shotorg
45 w_shotdir = normalize(trueaimpoint - w_shotorg);
47 // explanation of g_antilag:
48 // if client reports it was aiming at a player, and the serverside trace
49 // says it would miss, change the aim point to the player's new origin,
50 // but only if the shot at the player's new origin would hit of course
52 // FIXME: a much better method for bullet weapons would be to leave a
53 // trail of lagged 'ghosts' behind players, and see if the bullet hits the
54 // ghost corresponding to this player's ping time, and if so it would do
55 // damage to the real player
57 if (self.cursor_trace_ent) // client was aiming at someone
58 if (self.cursor_trace_ent != self) // just to make sure
59 if (self.cursor_trace_ent.takedamage) // and that person is killable
60 if (self.cursor_trace_ent.classname == "player") // and actually a player
61 if (cvar("g_antilag") == 1)
63 // verify that the shot would miss without antilag
64 // (avoids an issue where guns would always shoot at their origin)
65 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
66 if (!trace_ent.takedamage)
68 // verify that the shot would hit if altered
69 traceline(w_shotorg, self.cursor_trace_ent.origin, MOVE_NORMAL, self);
70 if (trace_ent == self.cursor_trace_ent)
72 // verify that the shot would hit in the past
73 if(self.antilag_debug)
74 antilag_takeback(self.cursor_trace_ent, time - self.antilag_debug);
76 antilag_takeback(self.cursor_trace_ent, time - self.ping * 0.001);
78 traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
79 antilag_restore(self.cursor_trace_ent);
81 if(trace_ent == self.cursor_trace_ent)
84 w_shotdir = normalize(self.cursor_trace_ent.origin - w_shotorg);
85 dprint("ANTILAG HIT for ", self.netname, "\n");
89 // prydon cursor aimbot or odd network conditions
90 dprint("WARNING: antilag ghost trace for ", self.netname, " failed!\n");
93 if(cvar("developer") >= 2)
97 v = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 ));
98 vplus = antilag_takebackorigin(self.cursor_trace_ent, time - (self.ping * 0.001 + 0.01));
99 vel = (vplus - v) * (1 / 0.01);
100 // solve: v + X * vel = closest to self.origin + self.view_ofs, v_forward axis
101 v -= (self.origin + self.view_ofs);
102 // solve: v + X * vel = closest to v_forward axis
103 // project into 2D by subtracting v_forward components:
104 v -= (v * v_forward) * v_forward;
105 vel -= (vel * v_forward) * v_forward;
106 // solve: v + X * vel = closest to origin
107 // (v + X * vel)^2 closest to 0
108 // v^2 + 2 * X * (v * vel) + X^2 * vel^2 closest to 0
109 X = -(v * vel) / (vel * vel);
110 dprint("dead center needs adjustment of ", ftos(X * 1000), " (that is, ", ftos(self.ping + X * 1000), " instead of ", ftos(self.ping), "\n");
116 self.solid = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
119 self.punchangle_x = recoil * -1;
122 sound (self, CHAN_WEAPON, snd, VOL_BASE, ATTN_NORM);
124 if (self.items & IT_STRENGTH)
126 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", VOL_BASE, ATTN_NORM);
129 void LaserTarget_Think()
136 // list of weapons that will use the laser, and the options that enable it
137 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && g_laserguided_missile)
140 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
145 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
148 // rocket launcher isn't selected, so no laser target.
149 if(self.lasertarget != world)
151 remove(self.lasertarget);
152 self.lasertarget = world;
157 if(!self.lasertarget)
159 // we don't have a lasertarget entity, so spawn one
160 //bprint("create laser target\n");
161 e = self.lasertarget = spawn();
162 e.owner = self.owner; // Its owner is my owner
163 e.classname = "laser_target";
164 e.movetype = MOVETYPE_NOCLIP; // don't touch things
165 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
166 e.scale = 1.25; // make it larger
167 e.alpha = 0.25; // transparency
168 e.colormod = '255 0 0' * (1/255) * 8; // change colors
169 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
170 // make it dynamically glow
171 // you should avoid over-using this, as it can slow down the player's computer.
172 e.glow_color = 251; // red color
176 e = self.lasertarget;
178 // move the laser dot to where the player is looking
180 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
181 offset = '0 0 26' + v_right*3;
182 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
183 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
184 if(trace_plane_normal != '0 0 0')
185 e.angles = vectoangles(trace_plane_normal);
187 e.angles = vectoangles(v_forward);
190 float CL_Weaponentity_CustomizeEntityForClient()
192 self.viewmodelforclient = self.owner;
193 if(other.classname == "spectator")
194 if(other.enemy == self.owner)
195 self.viewmodelforclient = other;
200 void CL_Weaponentity_Think()
202 self.nextthink = time;
203 if (intermission_running)
204 self.frame = WFRAME_IDLE;
205 if (self.owner.weaponentity != self)
210 if (self.owner.deadflag != DEAD_NO)
215 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
217 self.cnt = self.owner.weapon;
218 self.dmg = self.owner.modelindex;
219 self.deadflag = self.owner.deadflag;
220 if (self.owner.weaponname != "")
221 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
225 self.effects = self.owner.effects | EF_LOWPRECISION;
226 self.effects = self.effects - (self.effects & (EF_FULLBRIGHT)); // can mask team color, so get rid of it
228 if (self.flags & FL_FLY)
229 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
230 self.effects = self.effects - (self.effects & EF_NODRAW);
232 if(self.owner.alpha >= 0)
233 self.alpha = self.owner.alpha;
236 self.colormap = self.owner.colormap;
238 self.angles = '0 0 0';
241 if (self.state == WS_RAISE)
242 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
243 else if (self.state == WS_DROP)
244 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
245 else if (self.state == WS_CLEAR)
247 self.angles_x = -90 * f * f;
249 // create or update the lasertarget entity
253 void CL_ExteriorWeaponentity_Think()
256 self.nextthink = time;
257 if (self.owner.exteriorweaponentity != self)
262 if (self.owner.deadflag != DEAD_NO)
267 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
269 self.cnt = self.owner.weapon;
270 self.dmg = self.owner.modelindex;
271 self.deadflag = self.owner.deadflag;
272 if (self.owner.weaponname != "")
273 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
277 if((tag_found = gettagindex(self.owner, "tag_weapon")))
279 self.tag_index = tag_found;
280 self.tag_entity = self.owner;
283 setattachment(self, self.owner, "bip01 r hand");
285 // if that didn't find a tag, hide the exterior weapon model
289 self.effects = self.owner.effects | EF_LOWPRECISION;
290 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
291 if(self.owner.alpha >= 0)
292 self.alpha = self.owner.alpha;
295 self.colormap = self.owner.colormap;
298 // spawning weaponentity for client
299 void CL_SpawnWeaponentity()
301 self.weaponentity = spawn();
302 self.weaponentity.solid = SOLID_NOT;
303 self.weaponentity.owner = self;
304 self.weaponentity.weaponentity = self.weaponentity;
305 setmodel(self.weaponentity, ""); // precision set when changed
306 self.weaponentity.origin = '0 0 0';
307 self.weaponentity.angles = '0 0 0';
308 self.weaponentity.viewmodelforclient = self;
309 self.weaponentity.flags = 0;
310 self.weaponentity.think = CL_Weaponentity_Think;
311 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
312 self.weaponentity.nextthink = time;
313 self.weaponentity.scale = 0.61;
315 self.exteriorweaponentity = spawn();
316 self.exteriorweaponentity.solid = SOLID_NOT;
317 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
318 self.exteriorweaponentity.owner = self;
319 self.exteriorweaponentity.origin = '0 0 0';
320 self.exteriorweaponentity.angles = '0 0 0';
321 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
322 self.exteriorweaponentity.nextthink = time;
325 float client_hasweapon(entity cl, float wpn, float andammo, float complain)
327 local float weaponbit, f;
328 local entity oldself;
330 if (wpn < WEP_FIRST || wpn > WEP_LAST)
333 sprint(self, "Invalid weapon\n");
336 weaponbit = W_WeaponBit(wpn);
337 if (cl.weapons & weaponbit)
343 f = weapon_action(wpn, WR_CHECKAMMO1);
344 f = f + weapon_action(wpn, WR_CHECKAMMO2);
349 sprint(cl, strcat("You don't have any ammo for the ^2", W_Name(wpn), "\n"));
358 // Report Proper Weapon Status / Modified Weapon Ownership Message
359 if(weaponsInMap & weaponbit)
360 sprint(cl, strcat("You do not have the ^2", W_Name(wpn), "\n") );
362 sprint(cl, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
370 if (self.weapon != -1)
372 if (self.weaponentity)
374 self.weaponentity.state = WS_CLEAR;
375 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
376 self.weaponentity.effects = 0;
382 if (self.weaponentity)
383 self.weaponentity.state = WS_READY;
384 weapon_thinkf(WFRAME_IDLE, 1000000, w_ready);
387 // Setup weapon for client (after this raise frame will be launched)
388 void weapon_setup(float windex)
391 e = get_weaponinfo(windex);
392 self.items = self.items - (self.items & IT_AMMO);
393 self.items = self.items | e.items;
395 // the two weapon entities will notice this has changed and update their models
396 self.weapon = windex;
397 self.weaponname = e.mdl;
400 // perform weapon to attack (weaponstate and attack_finished check is here)
402 float weapon_prepareattack(float secondary, float attacktime)
404 //if sv_ready_restart_after_countdown is set, don't allow the player to shoot
405 //if all players readied up and the countdown is running
406 if (cvar("sv_ready_restart_after_countdown"))
407 if(time < restart_countdown || time < self.race_penalty) {
411 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
413 self.switchweapon = w_getbestweapon(self);
414 if (self.switchweapon != self.weapon)
415 self.cnt = self.weapon;
419 if (timeoutStatus == 2) //don't allow the player to shoot while game is paused
422 // do not even think about shooting if switching
423 if(self.switchweapon != self.weapon)
426 // don't fire if previous attack is not finished
427 if (ATTACK_FINISHED(self) > time + frametime * 0.5)
429 // don't fire while changing weapon
430 if (self.weaponentity.state != WS_READY)
432 self.weaponentity.state = WS_INUSE;
433 // if the weapon hasn't been firing continuously, reset the timer
434 if (ATTACK_FINISHED(self) < time - frametime * 1.5)
436 ATTACK_FINISHED(self) = time;
437 //dprint("resetting attack finished to ", ftos(time), "\n");
439 ATTACK_FINISHED(self) = ATTACK_FINISHED(self) + attacktime;
440 //dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");
444 void weapon_thinkf(float fr, float t, void() func)
448 if (self.weaponentity != world)
449 self.weaponentity.frame = fr;
452 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
454 backtrace("Tried to override initial weapon think function - should this really happen?");
459 if(self.runes & RUNE_SPEED)
461 if(self.runes & CURSE_SLOW)
462 t = t * cvar("g_balance_rune_speed_combo_atkrate");
464 t = t * cvar("g_balance_rune_speed_atkrate");
466 else if(self.runes & CURSE_SLOW)
468 t = t * cvar("g_balance_curse_slow_atkrate");
472 // VorteX: haste can be added here
473 if (self.weapon_think == w_ready)
475 self.weapon_nextthink = time;
476 //dprint("started firing at ", ftos(time), "\n");
478 if (self.weapon_nextthink < time - frametime * 1.5 || self.weapon_nextthink > time + frametime * 1.5)
480 self.weapon_nextthink = time;
481 //dprint("reset weapon animation timer at ", ftos(time), "\n");
483 self.weapon_nextthink = self.weapon_nextthink + t;
484 self.weapon_think = func;
485 //dprint("next ", ftos(self.weapon_nextthink), "\n");
487 if (fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2)
489 if (!self.crouch) // shoot anim stands up, this looks bad
492 anim = self.anim_shoot;
494 player_setanim(anim, FALSE, TRUE, TRUE);
498 void weapon_boblayer1(float spd, vector org)
500 // VorteX: haste can be added here
503 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity)
511 mdirection = normalize(mvelocity);
512 mspeed = vlen(mvelocity);
514 nstyle = cvar("g_projectiles_newton_style");
518 outvelocity = mvelocity;
522 // true Newtonian projectiles
523 outvelocity = pvelocity + mvelocity;
527 // true Newtonian projectiles with automatic aim adjustment
529 // solve: |outspeed * mdirection - pvelocity| = mspeed
530 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
531 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
535 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
536 // velocity without mdirection component > mspeed
537 // fire at smallest possible mspeed that works?
538 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
543 p = mdirection * pvelocity;
544 q = pvelocity * pvelocity - mspeed * mspeed;
548 //dprint("impossible shot, adjusting\n");
551 outspeed = p + sqrt(D);
552 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
553 outvelocity = mdirection * outspeed;
558 outspeed = mspeed + mdirection * pvelocity;
559 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
560 outvelocity = mdirection * outspeed;
565 outspeed = mspeed + vlen(pvelocity);
566 outvelocity = mdirection * outspeed;
569 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
574 void W_SetupProjectileVelocity(entity missile)
576 if(missile.owner == world)
577 error("Unowned missile");
579 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);
583 // WEAPON PLUGIN SYSTEM
584 entity weapon_info[24];
585 entity dummy_weapon_info;
586 .float(float) weapon_func;
587 void register_weapon(float id, float(float) func, float ammotype, string modelname, string shortname, string name)
590 weapon_info[id - 1] = e = spawn();
591 e.classname = "weapon_info";
593 e.weapons = power2of(id - 1);
594 e.netname = shortname;
597 e.weapon_func = func;
599 e.model = strzone(strcat("models/weapons/g_", modelname, ".md3"));
601 float w_null(float dummy)
605 void register_dummy_weapon()
607 dummy_weapon_info = spawn();
608 dummy_weapon_info.classname = "weapon_info";
609 dummy_weapon_info.weapon = 0;
610 dummy_weapon_info.weapons = 0;
611 dummy_weapon_info.netname = "@!#%'n Tuba";
612 dummy_weapon_info.items = 0;
613 dummy_weapon_info.weapon_func = w_null;
614 dummy_weapon_info.mdl = "";
615 dummy_weapon_info.model = "";
617 entity get_weaponinfo(float id)
620 w = weapon_info[id - 1];
623 return dummy_weapon_info;