Hey everyone!
I tried to change weapons ammo.
Setting clip:
PHP Code:
SetEntProp(wpid, Prop_Send, "m_iClip1", clipAmmo);
Setting reserve:
PHP Code:
SetEntProp(wpid, Prop_Send, "m_iPrimaryReserveAmmoCount", reserveAmmo);
Question 1:
This works well, until reserve ammo is exceeding the default max size.
If my P90 got 200 bullets reserve and i am reloading an empty clip, it should end up in 150 bullets reserve. But after reload, the reserve ammo is resetting to 100 (default max size).
Anybody know how to fix this in a nice way? (Posted my dirty solution below)
Question 2:
How can i modify max clip size on shotguns after reloading?
Is it possible to extend the amount of shots reloaded into weapon while animating?
This code is not able to change shotguns clip size on reload.
(I know its old syntax, but this gamemode is far too big to change it quickly.)
PHP Code:
public OnEntityCreated(entity, const String:classname[])
{
PrintAsDebug(STATE_FREQ, "OnEntityCreated");
if(StrEqual(classname, "hegrenade_projectile"))
SDKHook(entity, SDKHook_SpawnPost, OnThrownGrenadeSpawned);
if(StrContains(classname, "weapon_") != -1)
SDKHook(entity, SDKHook_SpawnPost, OnWeaponSpawn);
}
stock OnWeaponSpawn(entity)
{
PrintAsDebug(STATE_INFO, "OnWeaponSpawn");
SDKHook(entity, SDKHook_Reload, OnWeaponReload);
SDKHook(entity, SDKHook_ReloadPost, OnWeaponReloadPost);
decl String:wpname[64];
GetEdictClassname(entity, wpname, 64);
new clip = GetConstWeaponClip(wpname);
if(clip != -1)
SetWeaponAmmo(entity, clip, 0);
}
new g_PlayerTempAmmo[MAXPLAYERS+1][4];
public Action:OnWeaponReload(weapon)
{
PrintAsDebug(STATE_INFO, "OnWeaponReload");
decl String:wpname[64];
GetEdictClassname(weapon, wpname, 64);
new currClip = GetWeaponClipAmmo(weapon);
new client = GetEntDataEnt2(weapon, m_hOwner);
if(client > 0)
{
if(g_DBEntries[client][dUpgradeAmmo] != 0 && currClip == GetConstWeaponClip(wpname)) // Force weapon reload
SetWeaponAmmo(weapon, currClip - 1, GetWeaponReserveAmmo(weapon) + 1);
else if(g_DBEntries[client][dUpgradeAmmo] != 0 && currClip == GetUpgradedIntValue(client, GetConstWeaponClip(wpname), UPGRADE_AMMO))
return Plugin_Handled;
}
return Plugin_Continue;
}
public OnWeaponReloadPost(weapon, bool:success)
{
PrintAsDebug(STATE_INFO, "OnWeaponReloadPost");
if(!success) return;
new client = GetEntDataEnt2(weapon, m_hOwner);
if(client > 0 && (GetPlayerWeaponSlot(client, 0) == weapon || GetPlayerWeaponSlot(client, 1) == weapon))
{
decl String:wpname[64];
GetEdictClassname(weapon, wpname, 64);
g_PlayerTempAmmo[client][0] = EntIndexToEntRef(weapon);
g_PlayerTempAmmo[client][1] = GetWeaponClipAmmo(weapon);
g_PlayerTempAmmo[client][2] = GetWeaponReserveAmmo(weapon);
g_PlayerTempAmmo[client][3] = (StrEqual(wpname, "weapon_nova") || StrEqual(wpname, "weapon_xm1014") || StrEqual(wpname, "weapon_sawedoff")) ? 1 : 0;
CreateTimer(0.1, OnWeaponReloadedCheck, GetClientSerial(client), TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action:OnWeaponReloadedCheck(Handle:timer, client)
{
PrintAsDebug(STATE_FREQ, "OnWeaponReloadedCheck");
client = GetClientFromSerial(client);
if(client == 0) return Plugin_Stop;
new weapon = EntRefToEntIndex(g_PlayerTempAmmo[client][0]);
if(weapon == INVALID_ENT_REFERENCE) return Plugin_Stop;
if(!g_PlayerTempAmmo[client][3] && GetEntProp(weapon, Prop_Data, "m_bInReload")
|| g_PlayerTempAmmo[client][3] && GetEntProp(weapon, Prop_Send, "m_reloadState") > 0)
{
return Plugin_Continue;
}
new currClip = g_PlayerTempAmmo[client][1];
new currReserve = g_PlayerTempAmmo[client][2];
if(GetWeaponClipAmmo(weapon) > currClip)
{
decl String:wpname[64];
GetEdictClassname(weapon, wpname, 64);
new maxClip = GetUpgradedIntValue(client, GetConstWeaponClip(wpname), UPGRADE_AMMO);
new clip = currReserve;
new reserve = currReserve - maxClip + currClip;
if(clip > maxClip) clip = maxClip;
if(reserve < 0) reserve = 0;
SetWeaponAmmo(weapon, clip, reserve);
}
return Plugin_Stop;
}