sm-plugins/_Hide/scripting/Hide.sp

364 lines
12 KiB
SourcePawn

#include <clientprefs>
#include <multicolors>
#include <sdkhooks>
#include <sourcemod>
#include <zombiereloaded>
#include <leader>
/* BOOLS */
bool g_bHidePlayers[MAXPLAYERS+1][MAXPLAYERS+1];
bool g_bNewHidePlayers[MAXPLAYERS + 1][MAXPLAYERS + 1];
/* INTEGERS */
int g_iHideRange[MAXPLAYERS+1];
int g_iLeader = 0;
/* CONVARS */
ConVar g_hCVar_HideEnabled;
/* COOKIES */
Handle g_hCookie_HideRange;
#define DISABLED -1
#define SHORT_RANGE 120
#define MEDIUM_RANGE 250
#define LONG_RANGE 500
int SHORT = SHORT_RANGE * SHORT_RANGE;
int MEDIUM = MEDIUM_RANGE * MEDIUM_RANGE;
int LONG = LONG_RANGE * LONG_RANGE;
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
name = "Hide Teammates",
author = "Neon, minor edits by jenz",
description = "A plugin that can !hide teammates with individual distances",
version = "2.1.1",
url = "https://steamcommunity.com/id/n3ontm"
};
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
g_hCVar_HideEnabled = CreateConVar("sm_hide_enabled", "1", "", FCVAR_NONE, true, 0.0, true, 1.0);
g_hCVar_HideEnabled.AddChangeHook(OnConVarChanged);
AutoExecConfig(true);
g_hCookie_HideRange = RegClientCookie("hide_range", "", CookieAccess_Private);
RegConsoleCmd("sm_hide", OnHideSettings, "Hiding near humans");
for(int client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
OnClientPutInServer(client);
if(AreClientCookiesCached(client))
OnClientCookiesCached(client);
}
}
SetCookieMenuItem(MenuHandler_CookieMenu, 0, "Hide");
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
CreateTimer(0.5, UpdateHide, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
for(int client = 1; client <= MaxClients; client++)
{
for(int target = 1; target <= MaxClients; target++)
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
if(IsClientInGame(client))
{
if(g_hCVar_HideEnabled.BoolValue)
SDKHook(client, SDKHook_SetTransmit, Hook_SetTransmit);
else
SDKUnhook(client, SDKHook_SetTransmit, Hook_SetTransmit);
}
}
if(g_hCVar_HideEnabled.BoolValue)
CPrintToChatAll("{cyan}[Hide] {white}has been allowed.");
else
CPrintToChatAll("{cyan}[Hide] {white}has been disabled.");
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientPutInServer(int client)
{
if(!g_hCVar_HideEnabled.BoolValue)
return;
SDKHook(client, SDKHook_SetTransmit, Hook_SetTransmit);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
g_iHideRange[client] = 0;
for(int target = 1; target <= MaxClients; target++)
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientCookiesCached(int client)
{
char sBuffer[16];
GetClientCookie(client, g_hCookie_HideRange, sBuffer, sizeof(sBuffer));
if (sBuffer[0])
{
g_iHideRange[client] = StringToInt(sBuffer);
if (g_iHideRange[client] != SHORT && g_iHideRange[client] != MEDIUM && g_iHideRange[client] != LONG)
g_iHideRange[client] = DISABLED;
}
else
g_iHideRange[client] = DISABLED;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action UpdateHide(Handle timer)
{
if(!g_hCVar_HideEnabled.BoolValue)
return Plugin_Continue;
for(int client = 1; client <= MaxClients; client++)
{
if(g_iHideRange[client] == DISABLED)
{
for(int target = 1; target <= MaxClients; target++)
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
continue;
}
if(!IsClientInGame(client) || !IsPlayerAlive(client) || !ZR_IsClientHuman(client) || IsFakeClient(client))
{
for(int target = 1; target <= MaxClients; target++)
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
continue;
}
float fOriginClient[3];
float fOriginTarget[3];
GetClientAbsOrigin(client, fOriginClient);
for(int target = 1; target <= MaxClients; target++)
{
if(IsClientInGame(target) && IsPlayerAlive(target) && ZR_IsClientHuman(target) && target != client)
{
GetClientAbsOrigin(target, fOriginTarget);
//PrintToChatAll("%N--%N::::::%f", client, target, GetVectorDistance(fOriginTarget, fOriginClient, false));
if((GetVectorDistance(fOriginTarget, fOriginClient, true) <= float(g_iHideRange[client])) && (g_iLeader != target))
{
g_bHidePlayers[client][target] = true;
}
else
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
}
else
{
g_bHidePlayers[client][target] = false;
g_bNewHidePlayers[client][target] = false;
}
}
}
g_iLeader = Leader_CurrentLeader()
return Plugin_Continue;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Hook_SetTransmit(int target, int client)
{
if(g_bNewHidePlayers[client][target])
{
return Plugin_Handled;
}
return Plugin_Continue;
}
//October 2023 edit: using OnGameFrame to just hide 5 player per frame. supposedly (a rumor) that is good enough to actually work.
public void OnGameFrame()
{
static int client = 0;
int iterate_amount = 5;
for (int i = 0; i < iterate_amount; i++)
{
//restart from the beginning
if (client == MAXPLAYERS)
{
client = 0;
}
client++;
//hide other players for this player?
if (!IsClientInGame(client) || g_iHideRange[client] == DISABLED || !IsPlayerAlive(client) || !ZR_IsClientHuman(client))
{
continue;
}
int previous_client = client - ((i * 2) + 1); //example if client starts at 43 in loop. 43 - 1 = 42; 44 - 3 = 41; 45 - 5 = 40; 46 - 7 = 39; 47 - 9 = 38
if (previous_client < 1)
{
previous_client = MAXPLAYERS - (i + 1); //65 - 1 = 64; 65 - 2 = 63; 65 - 3 = 62 and so on
}
for (int target = 1; target <= MaxClients; target++)
{
if (IsClientInGame(target) && IsPlayerAlive(target))
{
if (g_bHidePlayers[client][target])
{
g_bNewHidePlayers[client][target] = true;
}
}
g_bNewHidePlayers[previous_client][target] = false;
}
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnHideSettings(int client, int args)
{
ShowSettingsMenu(client);
return Plugin_Handled;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ShowSettingsMenu(int client)
{
Menu menu = new Menu(MenuHandler_MainMenu);
menu.SetTitle("Hide", client);
char sBuffer[128];
Format(sBuffer, sizeof(sBuffer), "Disabled%s", (g_iHideRange[client] == DISABLED) ? " [Selected]" : "");
menu.AddItem("", sBuffer, (g_iHideRange[client] == DISABLED));
Format(sBuffer, sizeof(sBuffer), "Short Range%s", (g_iHideRange[client] == SHORT) ? " [Selected]" : "");
menu.AddItem("", sBuffer, (g_iHideRange[client] == SHORT));
Format(sBuffer, sizeof(sBuffer), "Medium Range%s", (g_iHideRange[client] == MEDIUM) ? " [Selected]" : "");
menu.AddItem("", sBuffer, (g_iHideRange[client] == MEDIUM));
Format(sBuffer, sizeof(sBuffer), "Long Range%s", (g_iHideRange[client] == LONG) ? " [Selected]" : "");
menu.AddItem("", sBuffer, (g_iHideRange[client] == LONG));
if(!g_hCVar_HideEnabled.BoolValue)
menu.AddItem("", "Warning: Hide is currently disabled on the server", ITEMDRAW_DISABLED);
menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int selection)
{
switch(action)
{
case(MenuAction_Select):
{
switch(selection)
{
case(0):
{
g_iHideRange[client] = DISABLED
}
case(1):
{
g_iHideRange[client] = SHORT
}
case(2):
{
g_iHideRange[client] = MEDIUM
}
case(3):
{
g_iHideRange[client] = LONG
}
}
char sBuffer[16];
Format(sBuffer, sizeof(sBuffer), "%d", g_iHideRange[client]);
SetClientCookie(client, g_hCookie_HideRange, sBuffer);
ShowSettingsMenu(client);
}
case(MenuAction_Cancel):
{
ShowCookieMenu(client);
}
case(MenuAction_End):
{
delete menu;
}
}
return 0;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void MenuHandler_CookieMenu(int client, CookieMenuAction action, any info, char[] buffer, int maxlen)
{
switch(action)
{
case(CookieMenuAction_DisplayOption):
{
Format(buffer, maxlen, "Hide", client);
}
case(CookieMenuAction_SelectOption):
{
ShowSettingsMenu(client);
}
}
}