PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#include <clientprefs>
#pragma semicolon 1
#pragma newdecls required
#define KNIFE 0
#define FIST 1
#define NONE 2
Handle
g_hUseFist,
g_hClientTimer[MAXPLAYERS + 1]
;
bool
g_bClientPostAdminChecked[MAXPLAYERS + 1] = {false, false, ...},
g_bLate = false
;
int
g_iClientUseFist[MAXPLAYERS + 1] = {0, 0, ...},
g_iClientFistPhase[MAXPLAYERS + 1]
;
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
g_bLate = late;
}
public void OnPluginStart()
{
g_hUseFist = RegClientCookie("cookie_usefists", "Spawn with fists", CookieAccess_Private);
HookEvent("player_spawn", Event_PlayerSpawn);
RegConsoleCmd("sm_fists", ShowFistMenu, "Toggle spawning with knife or fists in menu");
if (g_bLate)
{
for (int i = 1; i < MaxClients; ++i)
{
if (IsClientInGame(i))
{
SDKHook(i, SDKHook_OnTakeDamage, Event_OnTakeDamage);
SDKHook(i, SDKHook_WeaponDrop, Event_WeaponDrop);
}
}
}
}
public Action OnPlayerRunCmd(int client, int &buttons)
{
if(!(buttons & IN_ATTACK || buttons & IN_ATTACK2))
{
return Plugin_Continue;
}
char sWeapon[32];
GetClientWeapon(client, sWeapon, sizeof(sWeapon));
if(StrEqual(sWeapon, "weapon_fists", false))
{
if (g_hClientTimer[client] != INVALID_HANDLE)
{
KillTimer(g_hClientTimer[client]);
}
g_hClientTimer[client] = CreateTimer(0.8, ResetPhase, client);
buttons &= ~IN_ATTACK2;
buttons |= IN_ATTACK;
g_iClientFistPhase[client]++;
return Plugin_Changed;
}
return Plugin_Continue;
}
public Action ResetPhase(Handle timer, int client)
{
g_iClientFistPhase[client] = -1;
g_hClientTimer[client] = INVALID_HANDLE;
return Plugin_Handled;
}
public Action Event_OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype)
{
if (!attacker || attacker > MaxClients || !IsClientInGame(attacker))
{
return Plugin_Continue;
}
char sWeapon[32];
GetClientWeapon(attacker, sWeapon, sizeof(sWeapon));
if(StrContains(sWeapon, "fists", false) != -1)
{
if (g_iClientFistPhase[attacker] % 3 == 0) damage = 40.0;
else damage = 30.0;
return Plugin_Changed;
}
return Plugin_Continue;
}
public Action Event_WeaponDrop(int client, int weapon)
{
if (!IsValidEntity(weapon) || !IsValidEdict(weapon))
{
return Plugin_Handled;
}
char sWeapon[32];
GetEdictClassname(weapon, sWeapon, sizeof(sWeapon));
if (strcmp(sWeapon, "weapon_knife") == 0)
{
if (GetEntProp(client, Prop_Send, "m_iAmmo", _, 2) == 0)
{
RemoveEntity(weapon);
EquipFists(GetClientUserId(client));
}
}
return Plugin_Continue;
}
public void Event_PlayerSpawn(Handle event, const char[] name, bool dontBroadcast)
{
RequestFrame(SetMeleeWeapon, GetEventInt(event, "userid"));
}
public void OnMapStart()
{
for (int client = 0; client < sizeof(g_iClientFistPhase); client++)
{
g_iClientFistPhase[client] = -1;
}
}
public Action ShowFistMenu(int client, int args)
{
Menu menu = new Menu(MenuHandlerFist);
menu.SetTitle("Fist Settings");
if (!g_iClientUseFist[client])
{
menu.AddItem("0", "Spawn With Knife [ENABLED]");
menu.AddItem("1", "Spawn With Fists");
}
else
{
menu.AddItem("0", "Spawn With Knife");
menu.AddItem("1", "Spawn With Fists [ENABLED]");
}
menu.Display(client, MENU_TIME_FOREVER);
return Plugin_Handled;
}
int MenuHandlerFist(Menu menu, MenuAction action, int client, int option)
{
if (action == MenuAction_End)
{
delete menu;
}
else if (action == MenuAction_Select)
{
char menuItem[32];
menu.GetItem(option, menuItem, sizeof(menuItem));
g_iClientUseFist[client] = StringToInt(menuItem);
SetMeleeWeapon(GetClientUserId(client));
SetClientCookie(client, g_hUseFist, menuItem);
}
}
public void OnClientPostAdminCheck(int client)
{
g_bClientPostAdminChecked[client] = true;
if (AreClientCookiesCached(client))
{
OnClientCookiesCached(client);
}
SDKHook(client, SDKHook_OnTakeDamage, Event_OnTakeDamage);
SDKHook(client, SDKHook_WeaponDrop, Event_WeaponDrop);
}
public void OnClientCookiesCached(int client)
{
if (client && IsClientInGame(client) && g_bClientPostAdminChecked[client])
{
char buffer[24];
GetClientCookie(client, g_hUseFist, buffer, sizeof(buffer));
if (strlen(buffer) > 0)
{
g_iClientUseFist[client] = StringToInt(buffer);
}
}
}
public void OnClientDisconnect(int client)
{
g_bClientPostAdminChecked[client] = false;
if (g_hClientTimer[client] != INVALID_HANDLE)
{
KillTimer(g_hClientTimer[client]);
g_hClientTimer[client] = INVALID_HANDLE;
}
g_iClientFistPhase[client] = -1;
}
void EquipFists(int userId)
{
int client = GetClientOfUserId(userId);
if(client && client < MaxClients && IsClientInGame(client) && IsPlayerAlive(client))
{
int fists = GivePlayerItem(client, "weapon_fists");
EquipPlayerWeapon(client, fists);
}
}
int GetClientMeleeWeapon(int client)
{
int entity = GetPlayerWeaponSlot(client, 2);
if (entity != -1)
{
char weaponName[32];
GetEntityClassname(entity, weaponName, sizeof(weaponName));
if (strcmp(weaponName, "weapon_knife") == 0)
{
return KNIFE;
}
else if (strcmp(weaponName, "weapon_fists") == 0)
{
return FIST;
}
else
{
return NONE;
}
}
return -1;
}
void RemoveClientKnife(int client)
{
int array_size = GetEntPropArraySize(client, Prop_Send, "m_hMyWeapons");
int entity;
char weaponName[32];
for (int i = 0; i < array_size; i++)
{
entity = GetEntPropEnt(client, Prop_Send, "m_hMyWeapons", i);
if (entity != -1 && GetEntSendPropOffs(entity, "m_bStartArming") == -1)
{
GetEdictClassname(entity, weaponName, sizeof(weaponName));
if (strcmp(weaponName, "weapon_knife") == 0)
{
CS_DropWeapon(client, entity, false, true);
RemoveEntity(entity);
break;
}
}
}
}
void SetMeleeWeapon(int userId)
{
int client = GetClientOfUserId(userId);
if (client && client < MaxClients && IsClientInGame(client) && IsPlayerAlive(client))
{
if (g_iClientUseFist[client])
{
int weapon = GetClientMeleeWeapon(client);
if (weapon == KNIFE)
{
RemoveClientKnife(client);
}
else if(weapon == NONE)
{
EquipFists(userId);
}
}
else
{
int weapon = GetClientMeleeWeapon(client);
if (weapon == FIST)
{
PrintToChat(client, "Fists cannot be immediately removed.");
PrintToChat(client, "You will spawn with a knife next time you die.");
}
else if(weapon == NONE)
{
GivePlayerItem(client, "weapon_knife");
}
}
}
}