void Weapon_ItemPostFrame(void* pvItem)
{
static int iId;
static int iClip;
static int iInReload;
static int iAmmoPrimary;
static int iAmmoSecondary;
static float flNextAttack;
static float flNextPrimaryAttack;
static float flNextSecondaryAttack;
static edict_t* pWeapon;
static edict_t* pPlayer;
pWeapon = PrivateToEdict(pvItem);
if (!IsValidPev(pWeapon))
{
return;
}
iId = GetPrivateInt(pWeapon, pvData_iId);
if (WEAPON_IS_DEFAULT(iId))
{
WEAPON_REF_ITEM_POST_FRAME(pvItem);
return;
}
pPlayer = GetPrivateCbase(pWeapon, pvData_pPlayer);
if (!IsValidPev(pPlayer))
{
return;
}
flNextAttack = GetPrivateFloat(pPlayer, pvData_flNextAttack);
flNextPrimaryAttack = GetPrivateFloat(pWeapon, pvData_flNextPrimaryAttack);
flNextSecondaryAttack = GetPrivateFloat(pWeapon, pvData_flNextSecondaryAttack);
iAmmoPrimary = GetAmmoInventory(pPlayer, PrimaryAmmoIndex(pWeapon));
iAmmoSecondary = GetAmmoInventory(pPlayer, SecondaryAmmoIndex(pWeapon));
iClip = GetPrivateInt(pWeapon, pvData_iClip);
iInReload = GetPrivateInt(pWeapon, pvData_fInReload);
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_ItemPostFrame, pWeapon, pPlayer);
if (iInReload && flNextAttack <= gpGlobals->time)
{
// complete the reload.
int j = min(WEAPON_GET_MAX_CLIP(iId) - iClip, iAmmoPrimary);
iClip += j;
iAmmoPrimary -= j;
// Add them to the clip
SetPrivateInt(pWeapon, pvData_iClip, iClip);
SetPrivateInt(pWeapon, pvData_fInReload, FALSE);
SetAmmoInventory(pPlayer, PrimaryAmmoIndex(pWeapon), iAmmoPrimary);
}
if ((pPlayer->v.button & IN_ATTACK2) && flNextSecondaryAttack < 0.0)
{
if (WEAPON_GET_AMMO2(iId) && !iAmmoSecondary)
{
SetPrivateInt(pWeapon, pvData_fFireOnEmpty, TRUE);
}
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_SecondaryAttack, pWeapon, pPlayer);
pPlayer->v.button &= ~IN_ATTACK2;
}
else if ((pPlayer->v.button & IN_ATTACK) && flNextPrimaryAttack < 0.0)
{
if ((!iClip && WEAPON_GET_AMMO1(iId)) || (WEAPON_GET_MAX_CLIP(iId) == -1 && !iAmmoPrimary))
{
SetPrivateInt(pWeapon, pvData_fFireOnEmpty, TRUE);
}
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_PrimaryAttack, pWeapon, pPlayer);
pPlayer->v.button &= ~IN_ATTACK;
}
else if (pPlayer->v.button & IN_RELOAD && WEAPON_GET_MAX_CLIP(iId) != -1 && !iInReload)
{
// reload when reload is pressed, or if no buttons are down and weapon is empty.
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_Reload, pWeapon, pPlayer);
}
else if (!(pPlayer->v.button & (IN_ATTACK | IN_ATTACK2)))
{
// no fire buttons down
SetPrivateInt(pWeapon, pvData_fFireOnEmpty, FALSE);
if (!IS_USEABLE(pWeapon) && flNextPrimaryAttack < 0.0)
{
// weapon isn't useable, switch.
if (!(WEAPON_GET_FLAGS(iId) & ITEM_FLAG_NOAUTOSWITCHEMPTY) && GetNextBestWeapon(pPlayer, pWeapon))
{
SetPrivateFloat(pWeapon, pvData_flNextPrimaryAttack, 0.3);
return;
}
}
else
{
// weapon is useable. Reload if empty and weapon has waited as long as it has to after firing
if (!iClip && !(WEAPON_GET_FLAGS(iId) & ITEM_FLAG_NOAUTORELOAD) && flNextPrimaryAttack < 0.0)
{
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_Reload, pWeapon, pPlayer);
return;
}
}
WEAPON_FORWARD_EXECUTE(iId, Fwd_Wpn_Idle, pWeapon, pPlayer);
}
}