#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <BotTargeting>

bool g_bLateLoad = false;
bool g_bIsAutismBot[MAXPLAYERS+1] = { false, ... };

public Plugin myinfo =
{
	name         = "Bot Targeting",
	author       = "zaCade",
	description  = "Adds extra targeting methods",
	version      = "1.0"
};

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	g_bLateLoad = late;

	CreateNative("IsClientAutismBot", Native_IsAutismBot);

	RegPluginLibrary("BotTargeting");
	return APLRes_Success;
}

public void OnPluginStart()
{
	AddMultiTargetFilter("@autismbots", Filter_AutismBot, "Autism Bots", false);
	AddMultiTargetFilter("@!autismbots", Filter_NonAutismBot, "Non Autism Bots", false);

	if(g_bLateLoad)
	{
		char sAuthID[32];
		for(int i = 1; i <= MaxClients; i++)
		{
			if(IsClientInGame(i) && !IsFakeClient(i) && IsClientAuthorized(i) &&
				GetClientAuthId(i, AuthId_Steam2, sAuthID, sizeof(sAuthID)))
			{
				OnClientAuthorized(i, sAuthID);
			}
		}
	}
}

public void OnPluginEnd()
{
	RemoveMultiTargetFilter("@autismbots", Filter_AutismBot);
	RemoveMultiTargetFilter("@!autismbots", Filter_NonAutismBot);
}

public void OnClientAuthorized(int client, const char[] sAuthID)
{
	g_bIsAutismBot[client] = \
	StrEqual(sAuthID, "STEAM_0:1:60189040") || \
	StrEqual(sAuthID, "STEAM_0:0:204398871") || \
	StrEqual(sAuthID, "STEAM_0:0:518094602") || \
	StrEqual(sAuthID, "STEAM_0:0:610560766");
}

public void OnClientDisconnected(int client)
{
	g_bIsAutismBot[client] = false;
}

public bool Filter_AutismBot(const char[] sPattern, Handle hClients, int client)
{
	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsClientInGame(i) && !IsFakeClient(i) && g_bIsAutismBot[i])
		{
			PushArrayCell(hClients, i);
		}
	}

	return true;
}

public bool Filter_NonAutismBot(const char[] sPattern, Handle hClients, int client)
{
	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsClientInGame(i) && !IsFakeClient(i) && !g_bIsAutismBot[i])
		{
			PushArrayCell(hClients, i);
		}
	}

	return true;
}

public int Native_IsAutismBot(Handle hPlugin, int numParams)
{
	int client = GetNativeCell(1);

	if(client > MaxClients || client <= 0)
	{
		ThrowNativeError(SP_ERROR_NATIVE, "Client is not valid.");
		return false;
	}

	if(!IsClientInGame(client))
	{
		ThrowNativeError(SP_ERROR_NATIVE, "Client is not in-game.");
		return false;
	}

	if(!IsClientAuthorized(client))
	{
		ThrowNativeError(SP_ERROR_NATIVE, "Client is not authorized.");
		return false;
	}

	return g_bIsAutismBot[client];
}

/*
public Action ZR_OnClientMotherZombieEligible(int client)
{
	if (g_bIsAutismBot[client])
		return Plugin_Handled;

	return Plugin_Continue;
}
*/
