#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <zombiereloaded>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =
{
	name 			= "RadarSpotAll",
	author 			= "BotoX",
	description 	= "Always spot every player on the radar.",
	version 		= "1.0",
	url 			= ""
};

ConVar g_cRadarMode;
int g_RadarMode;

int g_hPlayerSpotted;
int g_aSpottedPlayers[MAXPLAYERS+1];

public void OnPluginStart()
{
	g_hPlayerSpotted = FindSendPropInfo("CCSPlayerResource", "m_bPlayerSpotted");
	if(g_hPlayerSpotted == -1)
		SetFailState("Couldn't find CCSPlayerResource::m_bPlayerSpotted");

	g_cRadarMode = CreateConVar("sm_radar_mode", "2", "RadarSpotAll mode: 0 = Default, 1 = Show zombies, 2 = Show all, 3 = Show none", FCVAR_NONE, true, 0.0, true, 3.0);
	g_cRadarMode.AddChangeHook(RadarModeChanged);

	HookEvent("player_spawn", Event_Spawn, EventHookMode_Post);

	AutoExecConfig(true);
	RadarModeChanged(g_cRadarMode, "", "");
}

public void RadarModeChanged(ConVar cvar, const char[] sOldVal, const char[] sNewVal)
{
	g_RadarMode = cvar.IntValue;

	for(int client = 1; client < MaxClients; client++)
	{
		if(g_RadarMode == 0)
		{
			g_aSpottedPlayers[client] = 0;
		}
		else if(g_RadarMode == 1)
		{
			if(IsClientInGame(client) && IsPlayerAlive(client) && ZR_IsClientZombie(client))
			{
				g_aSpottedPlayers[client] = 1;
			}
			else
			{
				g_aSpottedPlayers[client] = 0;
			}
		}
		else if(g_RadarMode == 2)
		{
			g_aSpottedPlayers[client] = 1;
		}
		else if(g_RadarMode == 3)
		{
			g_aSpottedPlayers[client] = 0;
		}
	}
}

public void OnMapStart()
{
	int entity = FindEntityByClassname(MaxClients+1, "cs_player_manager");
	if(entity == -1)
		SetFailState("Unable to find cs_player_manager entity");

	SDKHook(entity, SDKHook_ThinkPost, OnThinkPost);
}

public void OnThinkPost(int entity)
{
	if(!g_RadarMode)
		return;

	if(g_RadarMode == 1)
	{
		for(int client = 1; client < MaxClients; client++)
		{
			if(g_aSpottedPlayers[client])
				SetEntData(entity, g_hPlayerSpotted + client, 1, 1, true);
		}
	}
	else
	{
		SetEntDataArray(entity, g_hPlayerSpotted, g_aSpottedPlayers, MAXPLAYERS+1, 1, true);
	}
}


public void Event_Spawn(Event event, const char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(GetEventInt(event, "userid"));
	if(!client)
		return;

	if(g_RadarMode == 1)
	{
		g_aSpottedPlayers[client] = 0;
	}
}

public void ZR_OnClientInfected(int client, int attacker, bool motherInfect, bool respawnOverride, bool respawn)
{
	if(g_RadarMode == 1)
	{
		g_aSpottedPlayers[client] = 1;
	}
}

public void ZR_OnClientHumanPost(int client, bool respawn, bool protect)
{
	if(g_RadarMode == 1)
	{
		g_aSpottedPlayers[client] = 0;
	}
}
