/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <halomod>

#define PLUGIN "HaloMod Slayer"
#define VERSION HALOVERSION
#define AUTHOR "Soccdoodcss"

#define SCORE_ID 100
#define RESTART_ID 200

new score[33], won

new pScore

new topPlayer

public plugin_precache()
{
	precache_generic("sound/halomodv3/slayer.mp3")
	precache_generic("sound/halomodv3/gameover.mp3")
}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	pScore = register_cvar("halo_slayer_score", "25", FCVAR_SERVER)
	
	register_event("DeathMsg", "onDeath", "a")
	
	register_clcmd("halo_slayer_restart", "adminResetScores", ADMIN_LEVEL_H, "Reset the scores for Halo Slayer.")
	
	register_logevent("onRound",2,"0=World triggered","1=Round_Start")
}

public onRound()
{
	if(get_halo_type() != HALO_SLAYER)
		return PLUGIN_HANDLED
	
	show_scores()
	
	play_sound(0, "slayer")
	
	return PLUGIN_HANDLED
}

public onDeath()
{
	if(!is_halo_on() || get_halo_type() != HALO_SLAYER)
		return PLUGIN_HANDLED
	
	new attacker = read_data(1)
	new id = read_data(2)
	
	if(attacker == id)
		score[id]--
	
	else
		score[attacker]++
	
	check_scores()
	
	return PLUGIN_HANDLED
}

public adminResetScores(id, level, cid)
{
	if(!cmd_access(id,level,cid,2) || get_halo_type() != HALO_SLAYER)
		return PLUGIN_HANDLED
	
	reset_scores()
	
	return PLUGIN_HANDLED
}

public check_scores()
{
	new temp = -9999
	new tempid
	for(new x; x < sizeof score; x++)
	{
		if(score[x] > temp)
		{
			temp = score[x]
			tempid = x
		}
		
		if(score[x] >= get_pcvar_num(pScore) && !won)
			user_win(x)
	}
	
	if(is_user_connected(tempid))
		topPlayer = tempid
	
	show_scores()
}

public reset_scores()
{
	for(new x; x <= 32; x++)
		score[x] = 0
	
	won = 0
	
	show_scores()
}

public show_scores()
{
	if(get_halo_type() != HALO_SLAYER || !is_halo_on())
		return PLUGIN_HANDLED
	
	if(task_exists(SCORE_ID))
		remove_task(SCORE_ID)
	
	set_hudmessage(7, 177, 220, -1.0, 0.75, 0, 6.0, 12.0, 0.1, 0.2, 2)
	show_hudmessage(0, "Halo Mod v%s^nAuthor: %s^nSlayer^nTop Player^n%d/%d",VERSION, AUTHOR, score[topPlayer], get_pcvar_num(pScore))
	
	set_task(2.0, "show_scores", SCORE_ID)
	
	return PLUGIN_HANDLED
}

public user_win(id)
{
	won = 1
	new name[33]
	get_user_name(id, name, 32)
	client_print(0, print_center, "[Halo] %s Wins", name)
	client_print(0, print_chat, "[Halo] %s Wins", name)
	client_print(0, print_console, "[Halo] %s Wins", name)
	play_sound(0, "gameover")
	set_task(5.0, "restartRound", RESTART_ID)
}

public restartRound()
{
	reset_scores()
	server_cmd("sv_restart 1")
}
