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 .float savenextthink;
162 void thrown_wep_think()
165 float timeleft = self.savenextthink - time;
167 SUB_SetFade(self, self.savenextthink - 1, 1);
168 else if(timeleft > 0)
169 SUB_SetFade(self, time, timeleft);
171 SUB_VanishOrRemove(self);
174 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
175 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
178 float wa, thisammo, i, j;
180 var .float ammofield;
185 wep.classname = "droppedweapon";
187 wep.owner = wep.enemy = own;
188 wep.flags |= FL_TOSSED;
189 wep.colormap = own.colormap;
191 if(WepSet_FromWeapon(wpn) & WEPSET_SUPERWEAPONS)
193 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
195 wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
199 float superweapons = 1;
200 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
201 if(WepSet_FromWeapon(i) & WEPSET_SUPERWEAPONS)
202 if(own.weapons & WepSet_FromWeapon(i))
204 if(superweapons <= 1)
206 wep.superweapons_finished = own.superweapons_finished;
207 own.superweapons_finished = 0;
211 float timeleft = own.superweapons_finished - time;
212 float weptimeleft = timeleft / superweapons;
213 wep.superweapons_finished = time + weptimeleft;
214 own.superweapons_finished -= weptimeleft;
219 wa = W_AmmoItemCode(wpn);
224 weapon_defaultspawnfunc(wpn);
228 wep.glowmod = own.weaponentity_glowmod;
229 wep.think = thrown_wep_think;
230 wep.savenextthink = wep.nextthink;
231 wep.nextthink = min(wep.nextthink, time + 0.5);
232 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
240 weapon_defaultspawnfunc(wpn);
244 if(doreduce && g_weapon_stay == 2)
246 for(i = 0, j = 1; i < 24; ++i, j *= 2)
250 ammofield = Item_CounterField(j);
252 // if our weapon is loaded, give its load back to the player
253 if(self.(weapon_load[self.weapon]) > 0)
255 own.ammofield += self.(weapon_load[self.weapon]);
256 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
265 for(i = 0, j = 1; i < 24; ++i, j *= 2)
269 ammofield = Item_CounterField(j);
271 // if our weapon is loaded, give its load back to the player
272 if(self.(weapon_load[self.weapon]) > 0)
274 own.ammofield += self.(weapon_load[self.weapon]);
275 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
278 thisammo = min(own.ammofield, wep.ammofield);
279 wep.ammofield = thisammo;
280 own.ammofield -= thisammo;
281 s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
284 s = substring(s, 5, -1);
286 wep.glowmod = own.weaponentity_glowmod;
287 wep.think = thrown_wep_think;
288 wep.savenextthink = wep.nextthink;
289 wep.nextthink = min(wep.nextthink, time + 0.5);
290 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
296 float W_IsWeaponThrowable(float w)
300 if (!autocvar_g_pickup_items)
304 if (g_nexball && w == WEP_GRENADE_LAUNCHER)
309 wa = W_AmmoItemCode(w);
310 if(start_weapons & WepSet_FromWeapon(w))
312 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
313 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
322 // toss current weapon
323 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
330 return; // just in case
331 if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
333 if(!autocvar_g_weapon_throwable)
335 if(self.weaponentity.state != WS_READY)
337 if(!W_IsWeaponThrowable(w))
340 if(!(self.weapons & WepSet_FromWeapon(w)))
342 self.weapons &= ~WepSet_FromWeapon(w);
344 W_SwitchWeapon_Force(self, w_getbestweapon(self));
345 a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
348 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
351 float forbidWeaponUse()
353 if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
355 if(round_handler_IsActive() && !round_handler_IsRoundStarted())
357 if(self.player_blocked)
359 if(self.freezetag_frozen)
369 self.weapon_frametime = frametime;
371 if (!self.weaponentity || self.health < 1)
372 return; // Dead player can't use weapons and injure impulse commands
374 if(forbidWeaponUse())
375 if(self.weaponentity.state != WS_CLEAR)
381 if(!self.switchweapon)
384 self.switchingweapon = 0;
385 self.weaponentity.state = WS_CLEAR;
386 self.weaponname = "";
387 self.items &= ~IT_AMMO;
391 makevectors(self.v_angle);
392 fo = v_forward; // save them in case the weapon think functions change it
397 if (self.weapon != self.switchweapon)
399 if (self.weaponentity.state == WS_CLEAR)
402 self.switchingweapon = self.switchweapon;
404 entity newwep = get_weaponinfo(self.switchweapon);
406 //setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
407 self.weaponentity.state = WS_RAISE;
408 weapon_action(self.switchweapon, WR_SETUP);
410 // set our clip load to the load of the weapon we switched to, if it's reloadable
411 if(newwep.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", newwep.netname, "_reload_ammo"))) // prevent accessing undefined cvars
413 self.clip_load = self.(weapon_load[self.switchweapon]);
414 self.clip_size = cvar(strcat("g_balance_", newwep.netname, "_reload_ammo"));
417 self.clip_load = self.clip_size = 0;
419 // VorteX: add player model weapon select frame here
420 // setcustomframe(PlayerWeaponRaise);
421 weapon_thinkf(WFRAME_IDLE, cvar(sprintf("g_balance_%s_switchdelay_raise", newwep.netname)), w_ready);
422 //printf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_raise", newwep.netname), cvar(sprintf("g_balance_%s_switchdelay_raise", newwep.netname)));
423 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
425 else if (self.weaponentity.state == WS_DROP)
427 // in dropping phase we can switch at any time
428 self.switchingweapon = self.switchweapon;
430 else if (self.weaponentity.state == WS_READY)
433 self.switchingweapon = self.switchweapon;
435 entity oldwep = get_weaponinfo(self.weapon);
437 #ifndef INDEPENDENT_ATTACK_FINISHED
438 if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
441 sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTEN_NORM);
442 self.weaponentity.state = WS_DROP;
443 // set up weapon switch think in the future, and start drop anim
444 weapon_thinkf(WFRAME_DONTCHANGE, cvar(sprintf("g_balance_%s_switchdelay_drop", oldwep.netname)), w_clear);
445 //printf("W_WeaponFrame(): cvar: %s, value: %f\n", sprintf("g_balance_%s_switchdelay_drop", oldwep.netname), cvar(sprintf("g_balance_%s_switchdelay_drop", oldwep.netname)));
446 weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
447 #ifndef INDEPENDENT_ATTACK_FINISHED
453 // LordHavoc: network timing test code
455 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
460 // call the think code which may fire the weapon
461 // and do so multiple times to resolve framerate dependency issues if the
462 // server framerate is very low and the weapon fire rate very high
465 while (c < W_TICSPERFRAME)
468 if(w && !(self.weapons & WepSet_FromWeapon(w)))
470 if(self.weapon == self.switchweapon)
471 W_SwitchWeapon_Force(self, w_getbestweapon(self));
480 weapon_action(self.weapon, WR_THINK);
482 weapon_action(self.weapon, WR_GONETHINK);
484 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
486 if(self.weapon_think)
494 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
498 // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
499 //if (ATTACK_FINISHED(self) < time)
500 // ATTACK_FINISHED(self) = time;
502 //if (self.weapon_nextthink < time)
503 // self.weapon_nextthink = time;
505 // update currentammo incase it has changed
507 if (self.items & IT_CELLS)
508 self.currentammo = self.ammo_cells;
509 else if (self.items & IT_ROCKETS)
510 self.currentammo = self.ammo_rockets;
511 else if (self.items & IT_NAILS)
512 self.currentammo = self.ammo_nails;
513 else if (self.items & IT_SHELLS)
514 self.currentammo = self.ammo_shells;
516 self.currentammo = 1;
520 string W_Apply_Weaponreplace(string in)
522 float n = tokenize_console(in);
525 for(i = 0; i < n; ++i)
528 string r = cvar_string(strcat("g_weaponreplace_", s));
530 out = strcat(out, " ", s);
532 out = strcat(out, " ", r);
534 return substring(out, 1, -1);