3 weapon_action(self.weapon, WR_RELOAD);
6 // switch between weapons
7 void W_SwitchWeapon(float imp)
9 if (self.switchweapon != imp)
11 if (client_hasweapon(self, imp, TRUE, TRUE))
12 W_SwitchWeapon_Force(self, imp);
14 self.selectweapon = imp; // update selectweapon ANYWAY
22 .float weaponcomplainindex;
23 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
25 // We cannot tokenize in this function, as GiveItems calls this
26 // function. Thus we must use car/cdr.
27 float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
29 switchtonext = switchtolast = 0;
30 first_valid = prev_valid = 0;
33 if(skipmissing || pl.selectweapon == 0)
34 weaponcur = pl.switchweapon;
36 weaponcur = pl.selectweapon;
46 weaponwant = stof(car(rest)); rest = cdr(rest);
48 if((get_weaponinfo(weaponwant)).impulse != imp)
53 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
58 first_valid = weaponwant;
59 if(weaponwant == weaponcur)
68 prev_valid = weaponwant;
78 // complain (but only for one weapon on the button that has been pressed)
81 self.weaponcomplainindex += 1;
82 c = mod(self.weaponcomplainindex, c) + 1;
86 weaponwant = stof(car(rest)); rest = cdr(rest);
88 if((get_weaponinfo(weaponwant)).impulse != imp)
94 client_hasweapon(pl, weaponwant, TRUE, TRUE);
102 void W_CycleWeapon(string weaponorder, float dir)
105 w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
110 void W_NextWeaponOnImpulse(float imp)
113 w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
119 void W_NextWeapon(float list)
122 W_CycleWeapon(weaponorder_byid, -1);
124 W_CycleWeapon(self.weaponorder_byimpulse, -1);
126 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
130 void W_PreviousWeapon(float list)
133 W_CycleWeapon(weaponorder_byid, +1);
135 W_CycleWeapon(self.weaponorder_byimpulse, +1);
137 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
140 // previously used if exists and has ammo, (second) best otherwise
143 if(client_hasweapon(self, self.cnt, TRUE, FALSE))
144 W_SwitchWeapon(self.cnt);
146 W_SwitchToOtherWeapon(self);
149 float w_getbestweapon(entity e)
151 return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
154 // generic weapons table
155 // TODO should they be macros instead?
156 float weapon_action(float wpn, float wrequest)
158 return (get_weaponinfo(wpn)).weapon_func(wrequest);
161 string W_Name(float weaponid)
163 return (get_weaponinfo(weaponid)).message;
166 float W_WeaponBit(float wpn)
168 return (get_weaponinfo(wpn)).weapons;
171 float W_AmmoItemCode(float wpn)
173 return (get_weaponinfo(wpn)).items & IT_AMMO;
176 .float savenextthink;
177 void thrown_wep_think()
180 float timeleft = self.savenextthink - time;
182 SUB_SetFade(self, self.savenextthink - 1, 1);
183 else if(timeleft > 0)
184 SUB_SetFade(self, time, timeleft);
186 SUB_VanishOrRemove(self);
189 void thrown_wep_ode_think()
191 self.nextthink = time;
192 if(!self.owner.modelindex)
193 remove(self); // the real weapon is gone, remove this
196 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
197 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
200 float wa, thisammo, i, j;
202 var .float ammofield;
207 wep.classname = "droppedweapon";
209 wep.owner = wep.enemy = own;
210 wep.flags |= FL_TOSSED;
211 wep.colormap = own.colormap;
213 if(W_WeaponBit(wpn) & WEPBIT_SUPERWEAPONS)
215 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
217 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
221 float superweapons = 1;
222 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
223 if(own.weapons & WEPBIT_SUPERWEAPONS & W_WeaponBit(i))
225 if(superweapons <= 1)
227 wep.superweapons_finished = own.superweapons_finished;
228 own.superweapons_finished = 0;
232 float timeleft = own.superweapons_finished - time;
233 float weptimeleft = timeleft / superweapons;
234 wep.superweapons_finished = time + weptimeleft;
235 own.superweapons_finished -= weptimeleft;
240 wa = W_AmmoItemCode(wpn);
245 weapon_defaultspawnfunc(wpn);
249 wep.glowmod = own.weaponentity_glowmod;
250 wep.think = thrown_wep_think;
251 wep.savenextthink = wep.nextthink;
252 wep.nextthink = min(wep.nextthink, time + 0.5);
253 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
261 weapon_defaultspawnfunc(wpn);
265 if(doreduce && g_weapon_stay == 2)
267 for(i = 0, j = 1; i < 24; ++i, j *= 2)
271 ammofield = Item_CounterField(j);
273 // if our weapon is loaded, give its load back to the player
274 if(self.(weapon_load[self.weapon]) > 0)
276 own.ammofield += self.(weapon_load[self.weapon]);
277 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
286 for(i = 0, j = 1; i < 24; ++i, j *= 2)
290 ammofield = Item_CounterField(j);
292 // if our weapon is loaded, give its load back to the player
293 if(self.(weapon_load[self.weapon]) > 0)
295 own.ammofield += self.(weapon_load[self.weapon]);
296 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
299 thisammo = min(own.ammofield, wep.ammofield);
300 wep.ammofield = thisammo;
301 own.ammofield -= thisammo;
302 s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
305 s = substring(s, 5, -1);
307 wep.glowmod = own.weaponentity_glowmod;
308 wep.think = thrown_wep_think;
309 wep.savenextthink = wep.nextthink;
310 wep.nextthink = min(wep.nextthink, time + 0.5);
311 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
313 // make the dropped weapon physical
314 if(autocvar_physics_ode && autocvar_physics_ode_droppedweapon)
318 setsize(wep2, wep.mins, wep.maxs);
319 setorigin(wep2, wep.origin);
320 wep2.angles = wep.angles;
321 wep2.velocity = velo;
323 wep2.classname = "droppedweapon2";
325 wep2.solid = SOLID_CORPSE;
326 wep2.movetype = MOVETYPE_PHYSICS;
327 wep2.takedamage = DAMAGE_AIM;
328 wep2.damageforcescale = autocvar_physics_ode_droppedweapon_damageforcescale;
330 wep2.think = thrown_wep_ode_think;
331 wep2.nextthink = time;
333 wep.effects |= EF_NOMODELFLAGS; // disable the spinning
334 wep.movetype = MOVETYPE_FOLLOW;
342 float W_IsWeaponThrowable(float w)
346 if (!autocvar_g_pickup_items)
356 if (g_nexball && w == WEP_GRENADE_LAUNCHER)
362 wa = W_AmmoItemCode(w);
363 if(start_weapons & wb)
365 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
366 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
375 // toss current weapon
376 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
383 return; // just in case
384 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
386 if(!autocvar_g_weapon_throwable)
388 if(self.weaponentity.state != WS_READY)
390 if(!W_IsWeaponThrowable(w))
394 if(self.weapons & wb != wb)
398 W_SwitchWeapon_Force(self, w_getbestweapon(self));
399 a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
403 sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
405 sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
408 // Bringed back weapon frame
414 self.weapon_frametime = frametime;
416 if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !autocvar_sv_ready_restart_after_countdown))
419 if(self.freezetag_frozen == 1)
422 if (!self.weaponentity || self.health < 1)
423 return; // Dead player can't use weapons and injure impulse commands
425 if(!self.switchweapon)
428 self.switchingweapon = 0;
429 self.weaponentity.state = WS_CLEAR;
430 self.weaponname = "";
431 self.items &~= IT_AMMO;
435 makevectors(self.v_angle);
436 fo = v_forward; // save them in case the weapon think functions change it
441 if (self.weapon != self.switchweapon)
443 if (self.weaponentity.state == WS_CLEAR)
446 self.switchingweapon = self.switchweapon;
448 //setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
449 self.weaponentity.state = WS_RAISE;
450 weapon_action(self.switchweapon, WR_SETUP);
452 // set our clip load to the load of the weapon we switched to, if it's reloadable
454 e = get_weaponinfo(self.switchweapon);
455 if(e.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", e.netname, "_reload_ammo"))) // prevent accessing undefined cvars
457 self.clip_load = self.(weapon_load[self.switchweapon]);
458 self.clip_size = cvar(strcat("g_balance_", e.netname, "_reload_ammo"));
461 self.clip_load = self.clip_size = 0;
463 // VorteX: add player model weapon select frame here
464 // setcustomframe(PlayerWeaponRaise);
465 weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
466 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
468 else if (self.weaponentity.state == WS_DROP)
470 // in dropping phase we can switch at any time
471 self.switchingweapon = self.switchweapon;
473 else if (self.weaponentity.state == WS_READY)
476 self.switchingweapon = self.switchweapon;
478 #ifndef INDEPENDENT_ATTACK_FINISHED
479 if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
482 sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
483 self.weaponentity.state = WS_DROP;
484 // set up weapon switch think in the future, and start drop anim
485 weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
486 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
487 #ifndef INDEPENDENT_ATTACK_FINISHED
493 // LordHavoc: network timing test code
495 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
498 wb = W_WeaponBit(self.weapon);
500 // call the think code which may fire the weapon
501 // and do so multiple times to resolve framerate dependency issues if the
502 // server framerate is very low and the weapon fire rate very high
505 while (c < W_TICSPERFRAME)
508 if(wb && ((self.weapons & wb) == 0))
510 if(self.weapon == self.switchweapon)
511 W_SwitchWeapon_Force(self, w_getbestweapon(self));
520 weapon_action(self.weapon, WR_THINK);
522 weapon_action(self.weapon, WR_GONETHINK);
524 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
526 if(self.weapon_think)
534 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
538 // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
539 //if (ATTACK_FINISHED(self) < time)
540 // ATTACK_FINISHED(self) = time;
542 //if (self.weapon_nextthink < time)
543 // self.weapon_nextthink = time;
545 // update currentammo incase it has changed
547 if (self.items & IT_CELLS)
548 self.currentammo = self.ammo_cells;
549 else if (self.items & IT_ROCKETS)
550 self.currentammo = self.ammo_rockets;
551 else if (self.items & IT_NAILS)
552 self.currentammo = self.ammo_nails;
553 else if (self.items & IT_SHELLS)
554 self.currentammo = self.ammo_shells;
556 self.currentammo = 1;