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 local vector startorg;
27 local vector idealorg;
29 traceline_hitcorpse(self, self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * 8192, MOVE_NOMONSTERS, self);
30 trueaimpoint = trace_endpos;
32 // if aiming at a player and the original trace won't hit that player
33 // anymore, try aiming at the player's new position
35 if (self.cursor_trace_ent)
36 if (self.cursor_trace_ent.takedamage)
37 if (trace_ent != self.cursor_trace_ent)
38 if (cvar("g_antilag"))
39 trueaimpoint = self.cursor_trace_ent.origin;
41 startorg = ent.origin + ent.view_ofs;// - '0 0 8';
42 idealorg = ent.origin + ent.view_ofs + v_forward * vecs_x + v_right * vecs_y + v_up * vecs_z;
44 // don't allow the shot to start inside a wall
45 traceline_hitcorpse (ent, startorg, idealorg, MOVE_NOMONSTERS, ent);
46 w_shotorg = trace_endpos;
48 w_shotdir = normalize(trueaimpoint - w_shotorg);
50 if (!cvar("g_norecoil"))
51 self.punchangle_x = recoil * -1;
54 sound (self, CHAN_WEAPON, snd, 1, ATTN_NORM);
56 if (self.items & IT_STRENGTH)
57 if (!cvar("g_minstagib"))
58 sound (self, CHAN_AUTO, "weapons/strength_fire.ogg", 1, ATTN_NORM);
61 void LaserTarget_Think()
68 // list of weapons that will use the laser, and the options that enable it
69 if(self.owner.laser_on && self.owner.weapon == WEP_ROCKET_LAUNCHER && cvar("g_laserguided_missile"))
72 //if(self.owner.weapon == WEP_ELECTRO && cvar("g_laserguided_electro"))
77 // if a laser-enabled weapon isn't selected, delete any existing laser and quit
80 // rocket launcher isn't selected, so no laser target.
81 if(self.lasertarget != world)
83 remove(self.lasertarget);
84 self.lasertarget = world;
91 // we don't have a lasertarget entity, so spawn one
92 //bprint("create laser target\n");
93 e = self.lasertarget = spawn();
94 e.owner = self.owner; // Its owner is my owner
95 e.classname = "laser_target";
96 e.movetype = MOVETYPE_NOCLIP; // don't touch things
97 setmodel(e, "models/laser_dot.mdl"); // what it looks like
98 e.scale = 1.25; // make it larger
99 e.alpha = 0.25; // transparency
100 e.colormod = '255 0 0' * (1/255) * 8; // change colors
101 e.effects = EF_FULLBRIGHT;
102 // make it dynamically glow
103 // you should avoid over-using this, as it can slow down the player's computer.
104 e.glow_color = 251; // red color
108 e = self.lasertarget;
110 // move the laser dot to where the player is looking
112 makevectors(self.owner.v_angle); // set v_forward etc to the direction the player is looking
113 offset = '0 0 26' + v_right*3;
114 traceline(self.owner.origin + offset, self.owner.origin + offset + v_forward * 8192, FALSE, self); // trace forward until you hit something, like a player or wall
115 setorigin(e, trace_endpos + v_forward*8); // move me to where the traceline ended
116 if(trace_plane_normal != '0 0 0')
117 e.angles = vectoangles(trace_plane_normal);
119 e.angles = vectoangles(v_forward);
123 void() CL_Weaponentity_Think =
125 self.nextthink = time;
126 if (self.owner.weaponentity != self)
131 if (self.owner.deadflag != DEAD_NO)
136 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
138 self.cnt = self.owner.weapon;
139 self.dmg = self.owner.modelindex;
140 self.deadflag = self.owner.deadflag;
141 if (self.owner.weaponname != "")
142 setmodel(self, strcat("models/weapons/w_", self.owner.weaponname, ".zym"));
146 self.effects = self.owner.effects;
148 if (self.flags & FL_FLY)
149 // owner is currently being teleported, so don't apply EF_NODRAW otherwise the viewmodel would "blink"
150 self.effects = self.effects - (self.effects & EF_NODRAW);
152 self.alpha = self.owner.alpha;
154 self.angles = '0 0 0';
157 if (self.state == WS_RAISE)
158 f = (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
159 else if (self.state == WS_DROP)
160 f = 1 - (self.owner.weapon_nextthink - time) / cvar("g_balance_weaponswitchdelay");
161 else if (self.state == WS_CLEAR)
163 self.angles_x = -90 * f * f;
165 // create or update the lasertarget entity
169 void() CL_ExteriorWeaponentity_Think =
171 self.nextthink = time;
172 if (self.owner.exteriorweaponentity != self)
177 if (self.owner.deadflag != DEAD_NO)
182 if (self.cnt != self.owner.weapon || self.dmg != self.owner.modelindex || self.deadflag != self.owner.deadflag)
184 self.cnt = self.owner.weapon;
185 self.dmg = self.owner.modelindex;
186 self.deadflag = self.owner.deadflag;
187 if (self.owner.weaponname != "")
188 setmodel(self, strcat("models/weapons/v_", self.owner.weaponname, ".md3"));
191 setattachment(self, self.owner, "bip01 r hand");
192 // if that didn't find a tag, hide the exterior weapon model
196 self.effects = self.owner.effects;
199 // spawning weaponentity for client
200 void() CL_SpawnWeaponentity =
202 if (self.weaponentity)
207 self.weaponentity = spawn();
208 self.weaponentity.solid = SOLID_NOT;
209 self.weaponentity.owner = self;
210 self.weaponentity.weaponentity = self.weaponentity;
211 setmodel(self.weaponentity, "");
212 self.weaponentity.origin = '0 0 0';
213 self.weaponentity.angles = '0 0 0';
214 self.weaponentity.viewmodelforclient = self;
215 self.weaponentity.flags = 0;
216 self.weaponentity.think = CL_Weaponentity_Think;
217 self.weaponentity.nextthink = time;
218 self.weaponentity.scale = 0.61;
220 self.exteriorweaponentity = spawn();
221 self.exteriorweaponentity.solid = SOLID_NOT;
222 self.exteriorweaponentity.exteriorweaponentity = self.exteriorweaponentity;
223 self.exteriorweaponentity.owner = self;
224 self.exteriorweaponentity.origin = '0 0 0';
225 self.exteriorweaponentity.angles = '0 0 0';
226 self.exteriorweaponentity.think = CL_ExteriorWeaponentity_Think;
227 self.exteriorweaponentity.nextthink = time;
230 // convertion from index (= impulse) to flag in .items
231 float(float index) weapon_translateindextoflag =
233 if (index == WEP_LASER)
235 else if (index == WEP_SHOTGUN)
237 else if (index == WEP_UZI)
239 else if (index == WEP_GRENADE_LAUNCHER)
240 return IT_GRENADE_LAUNCHER;
241 else if (index == WEP_ELECTRO)
243 else if (index == WEP_CRYLINK)
245 else if (index == WEP_NEX)
247 else if (index == WEP_HAGAR)
249 else if (index == WEP_ROCKET_LAUNCHER)
250 return IT_ROCKET_LAUNCHER;
251 else if (index == WEP_LASER)
257 float(entity cl, float wpn, float andammo, float complain) client_hasweapon =
259 local float itemcode, f;
260 local entity oldself;
262 if (wpn < WEP_FIRST || wpn > WEP_LAST)
265 sprint(self, "Invalid weapon\n");
268 itemcode = weapon_translateindextoflag(wpn);
269 if (cl.items & itemcode)
275 f = weapon_action(wpn, WR_CHECKAMMO1);
276 f = f + weapon_action(wpn, WR_CHECKAMMO2);
281 sprint(self, "You don't have any ammo for that weapon\n");
288 sprint(self, "You don't own that weapon\n");
295 if (self.weapon != -1)
297 if (self.weaponentity)
299 self.weaponentity.state = WS_CLEAR;
300 setmodel(self.weaponentity, "");
301 self.weaponentity.effects = 0;
307 self.weaponentity.state = WS_READY;
308 weapon_thinkf(WFRAME_IDLE, 0.1, w_ready);
311 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
312 float(entity e) w_getbestweapon
313 {// add new weapons here
314 if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
315 return WEP_ROCKET_LAUNCHER;
316 else if (client_hasweapon(e, WEP_NEX, TRUE, FALSE))
318 else if (client_hasweapon(e, WEP_HAGAR, TRUE, FALSE))
320 else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE, FALSE))
321 return WEP_GRENADE_LAUNCHER;
322 else if (client_hasweapon(e, WEP_ELECTRO, TRUE, FALSE))
324 else if (client_hasweapon(e, WEP_CRYLINK, TRUE, FALSE))
326 else if (client_hasweapon(e, WEP_UZI, TRUE, FALSE))
328 else if (client_hasweapon(e, WEP_SHOTGUN, TRUE, FALSE))
330 else if (client_hasweapon(e, WEP_LASER, FALSE, FALSE))
336 // Setup weapon for client (after this raise frame will be launched)
337 void(float windex, string wname, float hudammo) weapon_setup =
339 self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
340 self.items = self.items | hudammo;
342 // the two weapon entities will notice this has changed and update their models
343 self.weapon = windex;
344 self.weaponname = wname;
347 // perform weapon to attack (weaponstate and attack_finished check is here)
348 float(float secondary, float attacktime) weapon_prepareattack =
350 if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
352 self.switchweapon = w_getbestweapon(self);
353 if (self.switchweapon != self.weapon)
354 self.cnt = self.weapon;
357 // don't fire if previous attack is not finished
358 if (time < self.attack_finished)
360 // don't fire while changing weapon
361 if (self.weaponentity.state != WS_READY)
363 self.weaponentity.state = WS_INUSE;
364 // FIXME: this does not allow multiple shots per frame and disgards cumulative error
365 self.attack_finished = time + attacktime;
369 void(float fr, float t, void() func) weapon_thinkf =
373 if (self.weaponentity != world)
374 self.weaponentity.frame = fr;
377 if(cvar("g_runematch"))
379 if(self.runes & RUNE_SPEED)
381 if(self.runes & CURSE_SLOW)
382 t = t * cvar("g_balance_rune_speed_combo_atkrate");
384 t = t * cvar("g_balance_rune_speed_atkrate");
386 else if(self.runes & CURSE_SLOW)
388 t = t * cvar("g_balance_curse_slow_atkrate");
392 // VorteX: haste can be added here
393 self.weapon_nextthink = time + t;
394 self.weapon_think = func;
397 void(float spd, vector org) weapon_boblayer1 =
399 // VorteX: haste can be added here