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;
27 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, self);
28 trueaimpoint = trace_endpos;
30 // if aiming at a player and the original trace won't hit that player
31 // anymore, try aiming at the player's new position
33 if (!trace_ent.takedamage) // shot would miss without antilag
34 if (self.cursor_trace_ent) // but it HAD hit on the client
35 if (self.cursor_trace_ent.takedamage) // and even something evil
36 // if (trace_ent != self.cursor_trace_ent) // and it is different (redundant check, see takedamage)
37 if (cvar("g_antilag"))
38 trueaimpoint = self.cursor_trace_ent.origin;
41 // don't allow the shot to start inside a wall
42 local vector startorg;
43 local vector idealorg;
44 startorg = ent.origin + ent.view_ofs;// - '0 0 8';
45 idealorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
46 traceline_hitcorpse (ent, startorg, idealorg, MOVE_NOMONSTERS, ent);
47 w_shotorg = trace_endpos;
49 // all positions in the code are now required to be inside the player box
50 w_shotorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
52 w_shotdir = normalize(trueaimpoint - w_shotorg);
54 if (!cvar("g_norecoil"))
55 self.punchangle_x = recoil * -1;
58 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
60 if (self.items & IT_STRENGTH)
61 if (!cvar("g_minstagib"))
62 sound (self, CHAN_AUTO, "weapons/strength_fire.wav", 1, ATTN_NORM);
65 void LaserTarget_Think()
72 // list of weapons that will use the laser, and the options that enable it
73 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_laserguided_missile"))
76 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
81 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
84 // rocket launcher isn't selected, so no laser target.
85 if(self.lasertarget != world)
87 remove(self.lasertarget);
88 self.lasertarget = world;
95 // we don't have a lasertarget entity, so spawn one
96 //bprint("create laser target\n");
97 e = self.lasertarget = spawn();
98 e.owner = self.owner; // Its owner is my owner
99 e.classname = "laser_target";
100 e.movetype = MOVETYPE_NOCLIP; // don't touch things
101 setmodel(e, "models/laser_dot.mdl"); // what it looks like, precision set below
102 e.scale = 1.25; // make it larger
103 e.alpha = 0.25; // transparency
104 e.colormod = '255 0 0' * (1/255) * 8; // change colors
105 e.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
106 // make it dynamically glow
107 // you should avoid over-using this, as it can slow down the player's computer.
108 e.glow_color = 251; // red color
112 e = self.lasertarget;
114 // move the laser dot to where the player is looking
116 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
117 offset = '0 0 26' + v_right*3;
118 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
119 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
120 if(trace_plane_normal != '0 0 0')
121 e.angles = vectoangles(trace_plane_normal);
123 e.angles = vectoangles(v_forward);
126 float CL_Weaponentity_CustomizeEntityForClient()
128 self.viewmodelforclient = self.owner;
129 if(other.classname == "spectator")
130 if(other.enemy == self.owner)
131 self.viewmodelforclient = other;
136 void() CL_Weaponentity_Think =
138 self.nextthink = time;
139 if (intermission_running)
140 self.frame = WFRAME_IDLE;
141 if (self.owner.weaponentity != self)
146 if (self.owner.deadflag != DEAD_NO)
151 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
153 self.cnt = self.owner.weapon;
154 self.dmg = self.owner.modelindex;
155 self.deadflag = self.owner.deadflag;
156 if (self.owner.weaponname != "")
157 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym")); // precision set below
161 self.effects = self.owner.effects | EF_LOWPRECISION;
163 if (self.flags & FL_FLY)
164 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
165 self.effects = self.effects - (self.effects & EF_NODRAW);
167 if(self.owner.alpha >= 0)
168 self.alpha = self.owner.alpha;
171 self.colormap = self.owner.colormap;
173 self.angles = '0 0 0';
176 if (self.state == WS_RAISE)
177 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
178 else if (self.state == WS_DROP)
179 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
180 else if (self.state == WS_CLEAR)
182 self.angles_x = -90 * f * f;
184 // create or update the lasertarget entity
188 void() CL_ExteriorWeaponentity_Think =
190 self.nextthink = time;
191 if (self.owner.exteriorweaponentity != self)
196 if (self.owner.deadflag != DEAD_NO)
201 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
203 self.cnt = self.owner.weapon;
204 self.dmg = self.owner.modelindex;
205 self.deadflag = self.owner.deadflag;
206 if (self.owner.weaponname != "")
207 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3")); // precision set below
210 setattachment(self, self.owner, "bip01 r hand");
211 // if that didn't find a tag, hide the exterior weapon model
215 self.effects = self.owner.effects | EF_LOWPRECISION;
216 self.effects = self.effects - (self.effects & (EF_BLUE | EF_RED)); // eat performance
217 if(self.owner.alpha >= 0)
218 self.alpha = self.owner.alpha;
221 self.colormap = self.owner.colormap;
224 // spawning weaponentity for client
225 void() CL_SpawnWeaponentity =
227 self.weaponentity = spawn();
228 self.weaponentity.solid = SOLID_NOT;
229 self.weaponentity.owner = self;
230 self.weaponentity.weaponentity = self.weaponentity;
231 setmodel(self.weaponentity, ""); // precision set when changed
232 self.weaponentity.origin = '0 0 0';
233 self.weaponentity.angles = '0 0 0';
234 self.weaponentity.viewmodelforclient = self;
235 self.weaponentity.flags = 0;
236 self.weaponentity.think = CL_Weaponentity_Think;
237 self.weaponentity.customizeentityforclient = CL_Weaponentity_CustomizeEntityForClient;
238 self.weaponentity.nextthink = time;
239 self.weaponentity.scale = 0.61;
241 self.exteriorweaponentity = spawn();
242 self.exteriorweaponentity.solid = SOLID_NOT;
243 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
244 self.exteriorweaponentity.owner = self;
245 self.exteriorweaponentity.origin = '0 0 0';
246 self.exteriorweaponentity.angles = '0 0 0';
247 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
248 self.exteriorweaponentity.nextthink = time;
251 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
253 local float itemcode, f;
254 local entity oldself;
256 if (wpn < WEP_FIRST || wpn > WEP_LAST)
259 sprint(self, "Invalid weapon\n");
262 itemcode = W_ItemCode(wpn);
263 if (cl.items & itemcode)
269 f = weapon_action(wpn, WR_CHECKAMMO1);
270 f = f + weapon_action(wpn, WR_CHECKAMMO2);
275 sprint(self, "You don't have any ammo for that weapon\n");
284 // Report Proper Weapon Status / Modified Weapon Ownership Message
285 if(itemsInMap & itemcode)
286 sprint(self, strcat("You do not have the ^2", W_Name(wpn), "\n") );
288 sprint(self, strcat("The ^2", W_Name(wpn), "^7 is ^1NOT AVAILABLE^7 in this map\n") );
296 if (self.weapon != -1)
298 if (self.weaponentity)
300 self.weaponentity.state = WS_CLEAR;
301 // self.weaponname = ""; // next frame will setmodel it to "" when needed anyway
302 self.weaponentity.effects = 0;
308 if (self.weaponentity)
310 self.weaponentity.state = WS_READY;
311 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
315 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
316 float(entity e) w_getbestweapon
317 {// add new weapons here
318 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
319 return WEP_ROCKET_LAUNCHER;
320 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
322 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
324 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
325 return WEP_GRENADE_LAUNCHER;
326 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
328 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
330 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
332 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
334 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
340 // Setup weapon for client (after this raise frame will be launched)
341 void(float windex, string wname, float hudammo) weapon_setup =
343 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
344 self.items = self.items | hudammo;
346 // the two weapon entities will notice this has changed and update their models
347 self.weapon = windex;
348 self.weaponname = wname;
350 // might fire faster after switch
351 self.attack_finished = min(max(time, self.attack_finished_old + 1), self.attack_finished);
354 // perform weapon to attack (weaponstate and attack_finished check is here)
355 float(float secondary, float attacktime) weapon_prepareattack =
357 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
359 self.switchweapon = w_getbestweapon(self);
360 if (self.switchweapon != self.weapon)
361 self.cnt = self.weapon;
364 // don't fire if previous attack is not finished
365 if (self.attack_finished > time)
367 // don't fire while changing weapon
368 if (self.weaponentity.state != WS_READY)
370 self.weaponentity.state = WS_INUSE;
371 self.attack_finished_old = self.attack_finished;
372 self.attack_finished = max(time, self.attack_finished + attacktime);
376 void(float fr, float t, void() func) weapon_thinkf =
380 if (self.weaponentity != world)
381 self.weaponentity.frame = fr;
384 if(self.weapon_think == w_ready && func != w_ready && self.weaponentity.state == WS_RAISE)
386 backtrace("Tried to override initial weapon think function - should this really happen?");
389 if(cvar("g_runematch"))
391 if(self.runes & RUNE_SPEED)
393 if(self.runes & CURSE_SLOW)
394 t = t * cvar("g_balance_rune_speed_combo_atkrate");
396 t = t * cvar("g_balance_rune_speed_atkrate");
398 else if(self.runes & CURSE_SLOW)
400 t = t * cvar("g_balance_curse_slow_atkrate");
404 // VorteX: haste can be added here
405 self.weapon_nextthink = max(time, self.weapon_nextthink_lastframe + t);
406 self.weapon_think = func;
409 void(float spd, vector org) weapon_boblayer1 =
411 // VorteX: haste can be added here
414 vector(vector pvelocity, vector mvelocity) W_CalculateProjectileVelocity =
422 mdirection = normalize(mvelocity);
423 mspeed = vlen(mvelocity);
425 nstyle = cvar("g_projectiles_newton_style");
429 outvelocity = mvelocity;
433 // true Newtonian projectiles
434 outvelocity = pvelocity + mvelocity;
438 // true Newtonian projectiles with automatic aim adjustment
440 // solve: |outspeed * mdirection - pvelocity| = mspeed
441 // outspeed^2 - 2 * outspeed * (mdirection * pvelocity) + pvelocity^2 - mspeed^2 = 0
442 // outspeed = (mdirection * pvelocity) +- sqrt((mdirection * pvelocity)^2 - pvelocity^2 + mspeed^2)
446 // pvelocity^2 - (mdirection * pvelocity)^2 > mspeed^2
447 // velocity without mdirection component > mspeed
448 // fire at smallest possible mspeed that works?
449 // |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
454 p = mdirection * pvelocity;
455 q = pvelocity * pvelocity - mspeed * mspeed;
459 //dprint("impossible shot, adjusting\n");
462 outspeed = p + sqrt(D);
463 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
464 outvelocity = mdirection * outspeed;
469 outspeed = mspeed + mdirection * pvelocity;
470 outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
471 outvelocity = mdirection * outspeed;
476 outspeed = mspeed + vlen(pvelocity);
477 outvelocity = mdirection * outspeed;
480 error("g_projectiles_newton_style must be 0 (absolute), 1 (Newtonian), 2 (Newtonian + aimfix), 3 (pseudo Newtonian) or 4 (tZorkian)!");
485 void(entity missile) W_SetupProjectileVelocity =
487 if(missile.owner == world)
488 error("Unowned missile");
490 missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, missile.velocity);