/* Plugin generated by AMXX-Studio */

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

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

#define SCORE_ID 100
#define RESTART_ID 200

new score[3], won

new pScore

stock get_halo_team[][] ={
	"***Error! Please Contact Author.***",
	"Red",
	"Blue"
}
public plugin_precache()
{
	precache_generic("sound/halomodv3/teamslayer.mp3")
	precache_generic("sound/halomodv3/gameover.mp3")
	precache_generic("sound/halomodv3/betrayal.mp3")
	precache_generic("sound/halomodv3/betrayed.mp3")
}

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

public newRound()
{
	if(get_halo_type() != HALO_TEAM_SLAYER)
		return PLUGIN_HANDLED
	
	show_scores()
	
	play_sound(0, "teamslayer")
	
	return PLUGIN_HANDLED
}


public onDeath()
{
	if(!is_halo_on() || get_halo_type() != HALO_TEAM_SLAYER)
		return PLUGIN_HANDLED
	
	new attacker = read_data(1)
	new id = read_data(2)
	
	if(!is_user_connected(id) || !is_user_connected(attacker))
		return PLUGIN_HANDLED
	
	new attacker_team = get_user_team(attacker)
	new id_team = get_user_team(id)
	
	if(attacker == id)
	{
		score[attacker_team]--
	}
	
	else if(attacker_team == id_team)
	{
		score[attacker_team]--
		play_sound(id, "betrayed")
		play_sound(attacker, "betrayal")
	}
	else
		score[attacker_team]++
	
	check_scores()
	
	return PLUGIN_HANDLED
}

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

public reset_scores()
{
	score[t] = 0
	score[ct] = 0
	
	won = 0
	
	show_scores()
}

public check_scores()
{
	for(new x; x < sizeof score; x++)
	{
		if(score[x] >= get_pcvar_num(pScore) && !won)
			team_win(x)
	}
	
	show_scores()
}

public team_win(team)
{
	won = 1
	client_print(0, print_center, "[Halo] %s Team Wins", get_halo_team[team])
	client_print(0, print_chat, "[Halo] %s Team Wins", get_halo_team[team])
	client_print(0, print_console, "[Halo] %s Team Wins", get_halo_team[team])
	play_sound(0, "gameover")
	set_task(5.0, "restartRound", RESTART_ID)
}

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

public show_scores()
{
	if(get_halo_type() != HALO_TEAM_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^nTeam Slayer^nRed  Blue^n%d     %d",VERSION, AUTHOR, score[t], score[ct])
	
	set_task(2.0, "show_scores", SCORE_ID)
	
	return PLUGIN_HANDLED
}
