};\r
\r
// perform weapon to attack (weaponstate and attack_finished check is here)\r
-.float race_penalty;\r
-float weapon_prepareattack(float secondary, float attacktime)\r
+void W_SwitchToOtherWeapon(entity pl)\r
{\r
- //if sv_ready_restart_after_countdown is set, don't allow the player to shoot\r
- //if all players readied up and the countdown is running\r
- if(time < game_starttime || time < self.race_penalty) {\r
- return FALSE;\r
- }\r
+ // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)\r
+ float w, ww;\r
+ w = W_WeaponBit(pl.weapon);\r
+ pl.weapons &~= w;\r
+ ww = w_getbestweapon(pl);\r
+ pl.weapons |= w;\r
+ if(ww)\r
+ W_SwitchWeapon_Force(pl, ww);\r
+}\r
\r
+.float prevdryfire;\r
+float weapon_prepareattack_checkammo(float secondary)\r
+{\r
if not(self.items & IT_UNLIMITED_WEAPON_AMMO)\r
if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))\r
{\r
- // hack to ensure it switches to an OTHER weapon (in case the other fire mode still has ammo, we want that anyway)\r
- float w, ww;\r
- w = W_WeaponBit(self.weapon);\r
- self.weapons &~= w;\r
- ww = w_getbestweapon(self);\r
- self.weapons |= w;\r
- if(ww)\r
- W_SwitchWeapon_Force(self, ww);\r
+ if(self.weapon == self.switchweapon && time - self.prevdryfire > 1) // only play once BEFORE starting to switch weapons\r
+ {\r
+ sound (self, CHAN_AUTO, "weapons/dryfire.wav", VOL_BASE, ATTN_NORM);\r
+ self.prevdryfire = time;\r
+ }\r
+\r
+ W_SwitchToOtherWeapon(self);\r
+ return FALSE;\r
+ }\r
+ return TRUE;\r
+}\r
+.float race_penalty;\r
+float weapon_prepareattack_check(float secondary, float attacktime)\r
+{\r
+ if(!weapon_prepareattack_checkammo(secondary))\r
+ return FALSE;\r
+\r
+ //if sv_ready_restart_after_countdown is set, don't allow the player to shoot\r
+ //if all players readied up and the countdown is running\r
+ if(time < game_starttime || time < self.race_penalty) {\r
return FALSE;\r
}\r
\r
if (self.weaponentity.state != WS_READY)\r
return FALSE;\r
}\r
+\r
+ return TRUE;\r
+}\r
+float weapon_prepareattack_do(float secondary, float attacktime)\r
+{\r
self.weaponentity.state = WS_INUSE;\r
\r
self.spawnshieldtime = min(self.spawnshieldtime, time); // kill spawn shield when you fire\r
}\r
//dprint("attack finished ", ftos(ATTACK_FINISHED(self)), "\n");\r
return TRUE;\r
-};\r
+}\r
+float weapon_prepareattack(float secondary, float attacktime)\r
+{\r
+ if(weapon_prepareattack_check(secondary, attacktime))\r
+ {\r
+ weapon_prepareattack_do(secondary, attacktime);\r
+ return TRUE;\r
+ }\r
+ else\r
+ return FALSE;\r
+}\r
\r
void weapon_thinkf(float fr, float t, void() func)\r
{\r
\r
#define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"))\r
#define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"))\r
+\r
+void W_DecreaseAmmo(.float ammo_type, float ammo_use, float ammo_reload)\r
+{\r
+ if(self.items & IT_UNLIMITED_WEAPON_AMMO)\r
+ return;\r
+\r
+ // if this weapon is reloadable, decrease its load. Else decrease the player's ammo\r
+ if(ammo_reload)\r
+ {\r
+ self.clip_load -= ammo_use;\r
+ self.weapon_load[self.weapon] = self.clip_load;\r
+ }\r
+ else\r
+ self.(self.current_ammo) -= ammo_use;\r
+}\r
+\r
+// weapon reloading code\r
+\r
+.float reload_ammo_amount, reload_ammo_min, reload_time;\r
+.float reload_complain;\r
+.string reload_sound;\r
+\r
+float W_ReloadCheck()\r
+{\r
+ // check if we meet the necessary conditions to reload\r
+\r
+ entity e;\r
+ e = get_weaponinfo(self.weapon);\r
+\r
+ // don't reload weapons that don't have the RELOADABLE flag\r
+ if not(e.spawnflags & WEP_FLAG_RELOADABLE)\r
+ {\r
+ dprint("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");\r
+ return FALSE;\r
+ }\r
+\r
+ // return if reloading is disabled for this weapon\r
+ if(!self.reload_ammo_amount)\r
+ return FALSE;\r
+\r
+ // our weapon is fully loaded, no need to reload\r
+ if (self.clip_load >= self.reload_ammo_amount)\r
+ return FALSE;\r
+\r
+ // no ammo, so nothing to load\r
+ if(!self.(self.current_ammo) && self.reload_ammo_min)\r
+ {\r
+ if(clienttype(self) == CLIENTTYPE_REAL && self.reload_complain < time)\r
+ {\r
+ play2(self, "weapons/unavailable.wav");\r
+ sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));\r
+ self.reload_complain = time + 1;\r
+ }\r
+ // switch away if the amount of ammo is not enough to keep using this weapon\r
+ if not(weapon_action(self.weapon, WR_CHECKAMMO1) + weapon_action(self.weapon, WR_CHECKAMMO2))\r
+ {\r
+ self.clip_load = -1; // reload later\r
+ W_SwitchToOtherWeapon(self);\r
+ }\r
+ return FALSE;\r
+ }\r
+\r
+ if (self.weaponentity)\r
+ {\r
+ if (self.weaponentity.wframe == WFRAME_RELOAD)\r
+ return FALSE;\r
+\r
+ // allow switching away while reloading, but this will cause a new reload!\r
+ self.weaponentity.state = WS_READY;\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+void W_ReloadedAndReady()\r
+{\r
+ // finish the reloading process, and do the ammo transfer\r
+\r
+ self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading\r
+\r
+ // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load\r
+ if(!self.reload_ammo_min)\r
+ self.clip_load = self.reload_ammo_amount;\r
+ else\r
+ {\r
+ while(self.clip_load < self.reload_ammo_amount && self.(self.current_ammo)) // make sure we don't add more ammo than we have\r
+ {\r
+ self.clip_load += 1;\r
+ self.(self.current_ammo) -= 1;\r
+ }\r
+ }\r
+ self.weapon_load[self.weapon] = self.clip_load;\r
+\r
+ // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,\r
+ // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,\r
+ // so your weapon is disabled for a few seconds without reason\r
+\r
+ //ATTACK_FINISHED(self) -= self.reload_time - 1;\r
+\r
+ w_ready();\r
+}\r
+\r
+void W_Reload(float sent_ammo_min, float sent_ammo_amount, float sent_time, string sent_sound)\r
+{\r
+ // set global values to work with\r
+ self.reload_ammo_min = sent_ammo_min;\r
+ self.reload_ammo_amount = sent_ammo_amount;\r
+ self.reload_time = sent_time;\r
+ self.reload_sound = sent_sound;\r
+\r
+ if(!W_ReloadCheck())\r
+ return;\r
+\r
+ // now begin the reloading process\r
+\r
+ sound (self, CHAN_WEAPON2, self.reload_sound, VOL_BASE, ATTN_NORM);\r
+\r
+ // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,\r
+ // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,\r
+ // so your weapon is disabled for a few seconds without reason\r
+\r
+ //ATTACK_FINISHED(self) = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;\r
+\r
+ weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadedAndReady);\r
+\r
+ if(self.clip_load >= 0)\r
+ self.old_clip_load = self.clip_load;\r
+ self.clip_load = self.weapon_load[self.weapon] = -1;\r
+}
\ No newline at end of file