3 if(self.switchweapon == self.weapon)
4 if(self.weaponentity.state == WS_READY)
5 weapon_action(self.weapon, WR_RELOAD);
8 // switch between weapons
9 void W_SwitchWeapon(float imp)
11 if (self.switchweapon != imp)
13 if (client_hasweapon(self, imp, TRUE, TRUE))
14 W_SwitchWeapon_Force(self, imp);
16 self.selectweapon = imp; // update selectweapon ANYWAY
24 .float weaponcomplainindex;
25 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
27 // We cannot tokenize in this function, as GiveItems calls this
28 // function. Thus we must use car/cdr.
29 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
31 switchtonext = switchtolast = 0;
32 first_valid = prev_valid = 0;
35 if(skipmissing || pl.selectweapon == 0)
36 weaponcur = pl.switchweapon;
38 weaponcur = pl.selectweapon;
48 weaponwant = stof(car(rest)); rest = cdr(rest);
50 if((get_weaponinfo(weaponwant)).impulse != imp)
55 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
60 first_valid = weaponwant;
61 if(weaponwant == weaponcur)
70 prev_valid = weaponwant;
80 // complain (but only for one weapon on the button that has been pressed)
83 self.weaponcomplainindex += 1;
84 c = mod(self.weaponcomplainindex, c) + 1;
88 weaponwant = stof(car(rest)); rest = cdr(rest);
90 if((get_weaponinfo(weaponwant)).impulse != imp)
96 client_hasweapon(pl, weaponwant, TRUE, TRUE);
104 void W_CycleWeapon(string weaponorder, float dir)
107 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
112 void W_NextWeaponOnImpulse(float imp)
115 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
121 void W_NextWeapon(float list)
124 W_CycleWeapon(weaponorder_byid, -1);
126 W_CycleWeapon(self.weaponorder_byimpulse, -1);
128 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
132 void W_PreviousWeapon(float list)
135 W_CycleWeapon(weaponorder_byid, +1);
137 W_CycleWeapon(self.weaponorder_byimpulse, +1);
139 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
142 float w_getbestweapon(entity e)
144 return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
147 // generic weapons table
148 // TODO should they be macros instead?
149 float weapon_action(float wpn, float wrequest)
151 return (get_weaponinfo(wpn)).weapon_func(wrequest);
154 string W_Name(float weaponid)
156 return (get_weaponinfo(weaponid)).message;
159 float W_WeaponBit(float wpn)
161 return (get_weaponinfo(wpn)).weapons;
164 float W_AmmoItemCode(float wpn)
166 return (get_weaponinfo(wpn)).items;
169 void thrown_wep_think()
171 self.solid = SOLID_TRIGGER;
173 SUB_SetFade(self, time + 20, 1);
176 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
177 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
180 float wa, thisammo, i, j;
182 var .float ammofield;
187 wep.classname = "droppedweapon";
189 wep.owner = wep.enemy = own;
190 wep.flags |= FL_TOSSED;
191 wep.colormap = own.colormap;
193 wa = W_AmmoItemCode(wpn);
194 if(wa == IT_SUPERWEAPON || wa == 0)
198 weapon_defaultspawnfunc(wpn);
202 wep.think = thrown_wep_think;
203 wep.nextthink = time + 0.5;
211 weapon_defaultspawnfunc(wpn);
217 for(i = 0, j = 1; i < 24; ++i, j *= 2)
221 ammofield = Item_CounterField(j);
222 thisammo = min(own.ammofield, wep.ammofield);
223 wep.ammofield = thisammo;
224 own.ammofield -= thisammo;
225 s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
228 s = substring(s, 5, -1);
230 wep.think = thrown_wep_think;
231 wep.nextthink = time + 0.5;
236 float W_IsWeaponThrowable(float w)
242 wa = W_AmmoItemCode(w);
243 if(start_weapons & wb)
245 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
247 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
249 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
257 // toss current weapon
258 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
265 return; // just in case
266 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
272 if (g_nexball && w == WEP_GRENADE_LAUNCHER)
274 if (!autocvar_g_pickup_items)
278 if(!autocvar_g_weapon_throwable)
280 if(autocvar_g_weapon_stay == 1)
282 if(!W_IsWeaponThrowable(w))
284 if(self.deadflag == DEAD_NO && self.weaponentity.state != WS_READY)
288 if(self.weapons & wb != wb)
292 W_SwitchWeapon_Force(self, w_getbestweapon(self));
293 a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
299 sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
301 sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
305 // Bringed back weapon frame
311 self.weapon_frametime = frametime;
313 if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !autocvar_sv_ready_restart_after_countdown))
316 if(g_freezetag && self.freezetag_frozen == 1)
319 if (!self.weaponentity || self.health < 1)
320 return; // Dead player can't use weapons and injure impulse commands
322 if(!self.switchweapon)
325 self.weaponentity.state = WS_CLEAR;
326 self.weaponname = "";
327 self.items &~= IT_AMMO;
331 makevectors(self.v_angle);
332 fo = v_forward; // save them in case the weapon think functions change it
337 if (self.weapon != self.switchweapon)
339 if (self.weaponentity.state == WS_CLEAR)
341 setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
342 self.weaponentity.state = WS_RAISE;
343 weapon_action(self.switchweapon, WR_SETUP);
344 // VorteX: add player model weapon select frame here
345 // setcustomframe(PlayerWeaponRaise);
346 weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
347 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
349 else if (self.weaponentity.state == WS_READY)
351 #ifndef INDEPENDENT_ATTACK_FINISHED
352 if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
355 // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
356 sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
357 self.weaponentity.state = WS_DROP;
358 // set up weapon switch think in the future, and start drop anim
359 weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
360 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
361 #ifndef INDEPENDENT_ATTACK_FINISHED
367 // LordHavoc: network timing test code
369 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
372 wb = W_WeaponBit(self.weapon);
374 // call the think code which may fire the weapon
375 // and do so multiple times to resolve framerate dependency issues if the
376 // server framerate is very low and the weapon fire rate very high
379 while (c < W_TICSPERFRAME)
382 if(wb && ((self.weapons & wb) == 0))
384 W_SwitchWeapon_Force(self, w_getbestweapon(self));
392 self.weaponentity.weaponentity.glowmod = '0 0 0'; // reset glowmod, weapon think function only *might* set it
393 weapon_action(self.weapon, WR_THINK);
394 self.exteriorweaponentity.glowmod = self.weaponentity.weaponentity.glowmod; // exterior weaponmodel has the same glowmod
396 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
398 if(self.weapon_think)
406 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
410 // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
411 //if (ATTACK_FINISHED(self) < time)
412 // ATTACK_FINISHED(self) = time;
414 //if (self.weapon_nextthink < time)
415 // self.weapon_nextthink = time;
417 // update currentammo incase it has changed
419 if (self.items & IT_CELLS)
420 self.currentammo = self.ammo_cells;
421 else if (self.items & IT_ROCKETS)
422 self.currentammo = self.ammo_rockets;
423 else if (self.items & IT_NAILS)
424 self.currentammo = self.ammo_nails;
425 else if (self.items & IT_SHELLS)
426 self.currentammo = self.ammo_shells;
428 self.currentammo = 1;