#include <sourcemod>
#include <zombiereloaded>

/* BOOLS */
bool g_bBlockRespawn[MAXPLAYERS+1];

/* FLOATS */
float g_fDeathTime[MAXPLAYERS+1];

/* CONVARS */
ConVar g_hRespawnDelay;
ConVar g_hRespawnTreshold;

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name = "Repeat kill detector",
	author = "zaCade",
	description = "Disables respawning on maps with repeat killers",
	version = "2.0.0",
	url = "http://www.sourcemod.net/"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	if((g_hRespawnDelay = FindConVar("zr_respawn_delay")) == INVALID_HANDLE)
		SetFailState("Failed to find zr_respawn_delay cvar.");
	
	g_hRespawnTreshold = CreateConVar("zr_repeatkill_threshold", "1.0", "Zombie Reloaded Repeat Kill Detector Threshold", 0, true, 0.0, true, 10.0);
	
	HookEvent("round_start",  OnRoundStart);
	HookEvent("player_death", OnClientDeath);
	
	AutoExecConfig(true, "plugin.RepeatKillDetector");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bBlockRespawn[client] = false;
	g_fDeathTime[client]    = 0.0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRoundStart(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	for (int client; client < MaxClients; client++)
		g_bBlockRespawn[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDeath(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	char sWeapon[32];
	hEvent.GetString("weapon", sWeapon, sizeof(sWeapon))
	
	int victim = GetClientOfUserId(hEvent.GetInt("userid"));
	int client = GetClientOfUserId(hEvent.GetInt("attacker"));
	
	if (g_bBlockRespawn[victim])
		return;
	
	if (victim && !client && StrEqual(sWeapon, "trigger_hurt"))
	{
		float fGameTime = GetGameTime();
		
		if (fGameTime - g_fDeathTime[victim] - g_hRespawnDelay.FloatValue <= g_hRespawnTreshold.FloatValue)
		{
			PrintToChat(victim, "\x04[ZR]\x01 Repeat killer detected. Disabling your respawn for this round.");
			g_bBlockRespawn[victim] = true;
		}
		
		g_fDeathTime[victim] = fGameTime;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action ZR_OnClientRespawn(&client, &ZR_RespawnCondition:condition)
{
	if (g_bBlockRespawn[client])
		return Plugin_Handled;
	
	return Plugin_Continue;
}