void W_TriggerReload() { // don't trigger reload if holding the weapon attack button if(self.BUTTON_ATCK2) return; weapon_action(self.weapon, WR_RELOAD); } // switch between weapons void W_SwitchWeapon(float imp) { if (self.switchweapon != imp) { if (client_hasweapon(self, imp, TRUE, TRUE)) W_SwitchWeapon_Force(self, imp); } else { W_TriggerReload(); } }; .float weaponcomplainindex; float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain) { // We cannot tokenize in this function, as GiveItems calls this // function. Thus we must use car/cdr. float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c; string rest; switchtonext = switchtolast = 0; first_valid = prev_valid = 0; if(dir == 0) switchtonext = 1; c = 0; rest = weaponorder; while(rest != "") { weaponwant = stof(car(rest)); rest = cdr(rest); if(imp >= 0) if((get_weaponinfo(weaponwant)).impulse != imp) continue; ++c; if(client_hasweapon(pl, weaponwant, TRUE, FALSE)) { if(switchtonext) return weaponwant; if(!first_valid) first_valid = weaponwant; if(weaponwant == pl.switchweapon) { if(dir >= 0) switchtonext = 1; else if(prev_valid) return prev_valid; else switchtolast = 1; } prev_valid = weaponwant; } } if(first_valid) { if(switchtolast) return prev_valid; else return first_valid; } // complain (but only for one weapon on the button that has been pressed) if(complain) { self.weaponcomplainindex += 1; c = mod(self.weaponcomplainindex, c) + 1; rest = weaponorder; while(rest != "") { weaponwant = stof(car(rest)); rest = cdr(rest); if(imp >= 0) if((get_weaponinfo(weaponwant)).impulse != imp) continue; --c; if(c == 0) { client_hasweapon(pl, weaponwant, TRUE, TRUE); break; } } } return 0; } void W_CycleWeapon(string weaponorder, float dir) { float w; w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1); if(w > 0) W_SwitchWeapon(w); } void W_NextWeaponOnImpulse(float imp) { float w; w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1); if(w > 0) W_SwitchWeapon(w); } // next weapon void W_NextWeapon(float list) { if(list == 0) W_CycleWeapon(weaponpriority_hudselector_0, -1); else if(list == 1) W_CycleWeapon(weaponpriority_hudselector_1, -1); else if(list == 2) W_CycleWeapon(self.cvar_cl_weaponpriority, -1); } // prev weapon void W_PreviousWeapon(float list) { if(list == 0) W_CycleWeapon(weaponpriority_hudselector_0, +1); else if(list == 1) W_CycleWeapon(weaponpriority_hudselector_1, +1); else if(list == 2) W_CycleWeapon(self.cvar_cl_weaponpriority, +1); } string W_FixWeaponOrder_AllowIncomplete(string order) { return W_FixWeaponOrder(order, 0); } string W_FixWeaponOrder_ForceComplete(string order) { if(order == "") order = W_NumberWeaponOrder(cvar_string("cl_weaponpriority")); return W_FixWeaponOrder(order, 1); } float w_getbestweapon(entity e) { return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, 0); }; // generic weapons table // TODO should they be macros instead? float weapon_action(float wpn, float wrequest) { return (get_weaponinfo(wpn)).weapon_func(wrequest); }; string W_Name(float weaponid) { return (get_weaponinfo(weaponid)).message; } float W_WeaponBit(float wpn) { return (get_weaponinfo(wpn)).weapons; } float W_AmmoItemCode(float wpn) { return (get_weaponinfo(wpn)).items; } void thrown_wep_think() { self.solid = SOLID_TRIGGER; self.owner = world; SUB_SetFade(self, time + 20, 1); } // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo) { entity oldself, wep; float wa, thisammo, i, j; string s; var .float ammofield; wep = spawn(); setorigin(wep, org); wep.classname = "droppedweapon"; wep.velocity = velo; wep.owner = wep.enemy = own; wep.flags |= FL_TOSSED; wep.colormap = own.colormap; wa = W_AmmoItemCode(wpn); if(wa == IT_SUPERWEAPON || wa == 0) { oldself = self; self = wep; weapon_defaultspawnfunc(wpn); self = oldself; if(startitem_failed) return string_null; wep.colormod = wep.owner.colormod; // used by the regurgitating colors wep.glowmod = own.weaponentity_glowmod; wep.think = thrown_wep_think; wep.nextthink = time + 0.5; return ""; } else { s = ""; oldself = self; self = wep; weapon_defaultspawnfunc(wpn); self = oldself; if(startitem_failed) return string_null; if(doreduce) { for(i = 0, j = 1; i < 24; ++i, j *= 2) { if(wa & j) { ammofield = Item_CounterField(j); thisammo = min(own.ammofield, wep.ammofield); wep.ammofield = thisammo; own.ammofield -= thisammo; s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j)); // if our weapon is loaded, give its load back to the player if(self.weapon_load[self.weapon] > 0) { own.ammofield += self.weapon_load[self.weapon]; self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading } } } s = substring(s, 5, -1); } wep.colormod = wep.owner.colormod; // used by the regurgitating colors wep.glowmod = own.weaponentity_glowmod; wep.think = thrown_wep_think; wep.nextthink = time + 0.5; return s; } } float W_IsWeaponThrowable(float w) { float wb, wa; wb = W_WeaponBit(w); if(!wb) return 0; wa = W_AmmoItemCode(w); if(start_weapons & wb) { if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS) return 0; if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO) return 0; // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo) if(wa == 0) return 0; } return 1; } // toss current weapon void W_ThrowWeapon(vector velo, vector delta, float doreduce) { local float w, wb; string a; w = self.weapon; if (w == 0) return; // just in case if(self.stat_eaten && self.dropweapon_check) return; // can't drop weapons from the stomach if (g_lms) return; if (!cvar("g_pickup_items")) return; if(!cvar("g_weapon_throwable")) return; if(cvar("g_weapon_stay") == 1) return; if(!W_IsWeaponThrowable(w)) return; wb = W_WeaponBit(w); if(self.weapons & wb != wb) return; self.weapons &~= wb; W_SwitchWeapon_Force(self, w_getbestweapon(self)); a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo); if not(a) return; if(self.health >= 1) { if(a == "") sprint(self, strcat("You dropped the ^2", W_Name(w), "\n")); else sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n")); } }; .float display_setup; void W_DisplayDigitThink() { self.nextthink = time; float w_load, w_ammo; w_load = self.owner.weapon_load[self.owner.weapon]; w_ammo = self.owner.(self.owner.current_ammo); // the owner has switched to another weapon, remove the digits if(!self.owner.display_setup || !self.owner.weapon || self.owner.deadflag != DEAD_NO) { self.nextthink = 0; remove(self); self = world; return; } // copy all properties of the weapon to the digit self.origin = self.weaponentity.origin; self.angles = self.weaponentity.angles; self.scale = self.weaponentity.scale; self.effects = self.weaponentity.effects; self.alpha = self.weaponentity.alpha; self.colormap = self.weaponentity.colormap; self.colormod = self.weaponentity.colormod; // used by the regurgitating colors self.glowmod = self.weaponentity.glowmod; string txt; if(self.team) // weapon load display { if(w_load <= 0) { self.skin = 11; // unavailable digit return; } else { txt = ftos(floor(w_load)); txt = substring(txt, self.cnt - 1, 1); } entity e; e = get_weaponinfo(self.owner.weapon); if(self.owner.weapon_load[self.owner.weapon] <= cvar(strcat("g_gundisplay_warn_weapon_", e.netname))) { // in warning mode, only keep red color if(!self.colormod) self.colormod = '1 1 1'; self.colormod_y = 0; self.colormod_z = 0; } } else // ammo display { txt = ftos(floor(w_ammo)); txt = substring(txt, self.cnt - 1, 1); if(w_ammo <= cvar(strcat("g_gundisplay_warn_ammo_", Item_CounterFieldName(W_AmmoItemCode(self.owner.weapon))))) { // in warning mode, only keep red color if(!self.colormod) self.colormod = '1 1 1'; self.colormod_y = 0; self.colormod_z = 0; } } if((!txt || txt == "")) self.skin = 10; // empty digit else self.skin = stof(txt); } void W_DisplayDigitSetup(entity own, float num, float load, float exterior) { entity digit, e; digit = spawn(); digit.owner = own; //digit.weapon = own.weapon; digit.team = load; digit.cnt = num; e = get_weaponinfo(own.weapon); if(exterior) // exterior weapon { // keep the digit attached to the same bone as the gun setattachment(digit, digit.owner, "bip01 r hand"); digit.weaponentity = digit.owner.exteriorweaponentity; } else // view weapon { // keep the digit attached to the same bone as the gun // TODO: Does this work with self-animated weapons too? if(gettagindex(digit.owner.weaponentity, "weapon")) setattachment(digit, digit.owner.weaponentity, "weapon"); else if(gettagindex(digit.owner.weaponentity, "tag_weapon")) setattachment(digit, digit.owner.weaponentity, "tag_weapon"); digit.weaponentity = digit.owner.weaponentity; } if(load) { // weapon load digit setmodel(digit, strcat("models/weapons/v_", e.netname, "_digit1-", ftos(num) , ".md3")); } else { // ammo count digit setmodel(digit, strcat("models/weapons/v_", e.netname, "_digit2-", ftos(num) , ".md3")); } digit.think = W_DisplayDigitThink; digit.nextthink = time; } // Bringed back weapon frame void W_WeaponFrame() { vector fo, ri, up; entity e; if (frametime) self.weapon_frametime = frametime; if(((arena_roundbased || g_ca) && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown"))) return; if (!self.weaponentity || self.health < 1) return; // Dead player can't use weapons and injure impulse commands if(!self.switchweapon) { self.weapon = 0; self.weaponentity.state = WS_CLEAR; self.weaponname = ""; self.items &~= IT_AMMO; return; } makevectors(self.v_angle); fo = v_forward; // save them in case the weapon think functions change it ri = v_right; up = v_up; // setup weapon display digits if(self.weapon != self.switchweapon || self.classname != "player") self.display_setup = FALSE; else if(!self.display_setup) { if(self.weaponentity.modelindex && self.exteriorweaponentity.modelindex) { float i; e = get_weaponinfo(self.weapon); for(i = 1; fexists(strcat("models/weapons/v_", e.netname, "_digit1-", ftos(i) , ".md3")); i++) { W_DisplayDigitSetup(self, i, TRUE, FALSE); // weapon load digit, view model W_DisplayDigitSetup(self, i, TRUE, TRUE); // weapon load digit, exterior model } for(i = 1; fexists(strcat("models/weapons/v_", e.netname, "_digit2-", ftos(i) , ".md3")); i++) { W_DisplayDigitSetup(self, i, FALSE, FALSE); // ammo count digit, view model W_DisplayDigitSetup(self, i, FALSE, TRUE); // ammo count digit, exterior model } self.display_setup = TRUE; } } // Change weapon if (self.weapon != self.switchweapon) { if (self.weaponentity.state == WS_CLEAR) { setanim(self, self.anim_draw, FALSE, TRUE, TRUE); self.weaponentity.state = WS_RAISE; weapon_action(self.switchweapon, WR_SETUP); // set our clip load to the load of the weapon we switched to, if it's reloadable e = get_weaponinfo(self.switchweapon); if(e.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", e.netname, "_reload_ammo"))) // prevent accessing undefined cvars { self.clip_load = self.weapon_load[self.switchweapon]; self.clip_size = cvar(strcat("g_balance_", e.netname, "_reload_ammo")); } else self.clip_load = self.clip_size = 0; // VorteX: add player model weapon select frame here // setcustomframe(PlayerWeaponRaise); weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready); weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0'); } else if (self.weaponentity.state == WS_READY) { #ifndef INDEPENDENT_ATTACK_FINISHED if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5) { #endif // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM); self.weaponentity.state = WS_DROP; // set up weapon switch think in the future, and start drop anim weapon_thinkf(WFRAME_DONTCHANGE, cvar("g_balance_weaponswitchdelay"), w_clear); weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE); #ifndef INDEPENDENT_ATTACK_FINISHED } #endif } } // LordHavoc: network timing test code //if (self.button0) // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n"); float wb; wb = W_WeaponBit(self.weapon); // call the think code which may fire the weapon // and do so multiple times to resolve framerate dependency issues if the // server framerate is very low and the weapon fire rate very high local float c; c = 0; while (c < 5) { c = c + 1; if(wb && ((self.weapons & wb) == 0)) { W_SwitchWeapon_Force(self, w_getbestweapon(self)); wb = 0; } if(wb) { v_forward = fo; v_right = ri; v_up = up; weapon_action(self.weapon, WR_THINK); } if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink) { if(self.weapon_think) { v_forward = fo; v_right = ri; v_up = up; self.weapon_think(); } else bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n"); } } // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!) //if (ATTACK_FINISHED(self) < time) // ATTACK_FINISHED(self) = time; //if (self.weapon_nextthink < time) // self.weapon_nextthink = time; };