PHP Code:
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <cstrike>
char g_szPrimaryWeaponNames[][24] =
{
"weapon_ak47",
"weapon_m4a1",
"weapon_m4a1_silencer",
"weapon_sg556",
"weapon_aug",
"weapon_galilar",
"weapon_famas",
"weapon_awp",
"weapon_ssg08",
"weapon_g3sg1",
"weapon_scar20",
"weapon_m249",
"weapon_negev",
"weapon_nova",
"weapon_xm1014",
"weapon_sawedoff",
"weapon_mag7",
"weapon_mac10",
"weapon_mp9",
"weapon_mp7",
"weapon_ump45",
"weapon_p90",
"weapon_bizon"
};
char g_szBackPackWeapon[MAXPLAYERS + 1][24];
char g_iBackPackAmmo[MAXPLAYERS + 1];
char g_szTempWeapon[MAXPLAYERS + 1][24];
char g_iTempAmmo[MAXPLAYERS + 1];
int g_iLastButton[MAXPLAYERS + 1];
public Action OnPlayerRunCmd(int client, int &iButtons, int &iImpulse, float fVelocity[3], float fAngles[3], int &iWeapon)
{
if (!IsValidClient(client) || !IsPlayerAlive(client))
return Plugin_Continue;
if ((iButtons & IN_USE) && !(g_iLastButton[client] & IN_USE ))
{
SwitchWeapon(client);
}
g_iLastButton[client] = iButtons;
return Plugin_Continue;
}
public void SwitchWeapon(int client)
{
if (!strlen(g_szBackPackWeapon[client]))
return;
int iActiveWeapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
if (!IsValidEdict(iActiveWeapon))
return;
char szActiveWeapon[24];
GetEntityClassname(iActiveWeapon, szActiveWeapon, sizeof(szActiveWeapon));
if (!IsWeaponSwitchable(szActiveWeapon))
return;
if (StrEqual(g_szBackPackWeapon[client], szActiveWeapon))
return;
strcopy(g_szTempWeapon[client], sizeof(g_szTempWeapon[]), szActiveWeapon);
g_iTempAmmo[client] = GetEntProp(iActiveWeapon, Prop_Data, "m_iClip1");
int weapon = -1;
weapon = GetPlayerWeaponSlot(client, CS_SLOT_PRIMARY);
if (weapon != -1)
RemovePlayerItem(client, weapon);
int iNewWeapon = GivePlayerItem(client, g_szBackPackWeapon[client]);
FakeClientCommandEx(client, "use %s", g_szBackPackWeapon[client]);
if (g_iBackPackAmmo[client] != 0)
SetEntProp(iNewWeapon, Prop_Data, "m_iClip1", g_iBackPackAmmo[client]);
strcopy(g_szBackPackWeapon[client], sizeof(g_szBackPackWeapon[]), g_szTempWeapon[client]);
g_iBackPackAmmo[client] = g_iTempAmmo[client];
g_szTempWeapon[client][0] = '\0';
g_iTempAmmo[client] = 0;
}
public void OnClientPostAdminCheck(int client)
{
g_iLastButton[client] = 0;
SetPlayerBackpackWeapon(client, "weapon_ak47");
}
public void OnClientDisconnect(int client)
{
g_iLastButton[client] = 0;
}
stock bool IsWeaponSwitchable(char[] szWeapon)
{
for(int i = 0; i < sizeof(g_szPrimaryWeaponNames); i ++)
{
if (StrEqual(szWeapon, g_szPrimaryWeaponNames[i]))
return true;
}
return false;
}
SetPlayerBackpackWeapon(int client, char[] szWeapon)
{
strcopy(g_szBackPackWeapon[client], sizeof(g_szBackPackWeapon[]), szWeapon);
}
bool IsValidClient(int client)
{
if (!(0 < client <= MaxClients)) return false;
if (!IsClientInGame(client)) return false;
if (IsFakeClient(client)) return false;
return true;
}