/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>
#include <halomod>

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

#define PLAYERS 32
#define SCORE_ID 100
#define RESTART_ID 200

new juggernaut
new pHealth, pSpeed, pScore, pRandom
new players, jug_kills, won = 1, pre_jug

public plugin_precache()
{
	precache_generic("sound/halomodv3/juggernaut.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)
	
	pHealth = register_cvar("halo_jug_health", "500", FCVAR_SERVER)
	pSpeed = register_cvar("halo_jug_speed", "500", FCVAR_SERVER)
	pScore = register_cvar("halo_jug_score", "10", FCVAR_SERVER)
	pRandom = register_cvar("halo_jug_random", "0", FCVAR_SERVER)
	
	register_logevent("onRound",2,"0=World triggered","1=Round_Start")
	
	register_event("CurWeapon", "onWeapon", "be")
	register_event("DeathMsg", "onDeath", "a")
	
	register_clcmd("halo_jug_restart", "adminResetScores", ADMIN_LEVEL_H, "Reset the scores for Halo Juggernaut.")
}

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

public onRound()
{
	if(get_halo_type() != HALO_JUGGERNAUT)
		return PLUGIN_HANDLED
	
	if(won)
	{
		play_sound(0, "juggernaut")
		reset_scores()
		won = 0
	}
	
	if(!is_user_alive(pre_jug))
		find_juggernaut()
	else
		juggernaut = pre_jug
	set_teams()
	give_skills()
	
	show_scores()
	
	return PLUGIN_HANDLED
}

public onWeapon(id)
{
	if(get_halo_type() != HALO_JUGGERNAUT || !is_user_alive(id))
		return PLUGIN_HANDLED
	
	if(id != juggernaut && get_user_team(id) == 1)
	{
		cs_set_user_team(id, CS_TEAM_CT, CS_CT_SAS)
		return PLUGIN_HANDLED
	}
	
	else if(id == juggernaut)
		set_speed(id)
	
	return PLUGIN_HANDLED
}

public find_juggernaut()
{
	new crap[32]
	get_players( crap, players, "a")
	
	if(players < 1)
		return PLUGIN_HANDLED
	
	new chosen = random_num(1, players)
	
	new cycle
	
	for(new x; x <= PLAYERS; x++)
	{
		if(is_user_alive(x))
		{
			cycle++
			if(cycle==chosen)
			{
				juggernaut = x
				return PLUGIN_HANDLED
			}
		}
	}
	
	return PLUGIN_HANDLED
}

public set_teams()
{
	if(!is_user_alive(juggernaut))
		return PLUGIN_HANDLED
	
	for(new x; x <= PLAYERS; x++)
	{
		if(is_user_connected(x) && get_user_team(x) < 3)
			cs_set_user_team(x, CS_TEAM_CT, CS_CT_SAS)
	}
	
	cs_set_user_team(juggernaut, CS_TEAM_T, CS_T_GUERILLA)
	
	return PLUGIN_HANDLED
}

public give_skills()
{
	if(!is_user_alive(juggernaut))
		return PLUGIN_HANDLED
	
	set_health(juggernaut)
	set_speed(juggernaut)
	
	return PLUGIN_HANDLED
}

public set_health(id)
{
	set_pev(id, pev_health, get_pcvar_float(pHealth) * players)
}

public set_speed(id)
{
	set_pev(id, pev_maxspeed, get_pcvar_float(pSpeed))
}

public onDeath()
{
	if(!is_halo_on() || get_halo_type() != HALO_JUGGERNAUT)
		return PLUGIN_HANDLED
	
	new attacker = read_data(1)
	new id = read_data(2)
	
	if(id==juggernaut)
	{
		team_win(2)
		if(!get_pcvar_num(pRandom) && juggernaut != attacker)
			pre_jug = attacker
	}
	
	else if(attacker == id || get_user_team(id) == get_user_team(attacker))
	{
		play_sound(id, "betrayed")
		play_sound(attacker, "betrayal")
	}
	else
		jug_kills++
	
	check_scores()
	
	return PLUGIN_HANDLED
}

public check_scores()
{
	if(jug_kills >= get_pcvar_num(pScore) && !won)
		team_win(1)
	
	show_scores()
}

public team_win(team)
{
	won = 1
	new wins[5]
	
	if(team == 1)
		wins = "Wins"
	
	else
		wins = "Lost"
	
	client_print(0, print_center, "[Halo] Juggernaut %s", wins)
	client_print(0, print_chat, "[Halo] Juggernaut %s", wins)
	client_print(0, print_console, "[Halo] Juggernaut %s", wins)
	
	play_sound(0, "gameover")
	reset_scores()
	set_task(3.0, "restartRound", RESTART_ID)
	
	pre_jug = 0
}

public show_scores()
{
	if(get_halo_type() != HALO_JUGGERNAUT || !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^nJuggernaut^nKills: %d/%d^nHealth: %d",VERSION, AUTHOR, jug_kills, get_pcvar_num(pScore), get_user_health(juggernaut))
	
	set_task(0.5, "show_scores", SCORE_ID)
	
	return PLUGIN_HANDLED
}

public reset_scores()
{
	jug_kills = 0
	
	show_scores()
}

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