/*
	Script generated by Pawn Studio

	Warmup plugin v1.0
	
	(c) Copyright 2008 by jas0n
	
	This file is provided as is (no warranties)
*/
/*
	Cvars:
		- amx_warmup <1|0> - enable/disable plugin. (default: 1)
		- amx_warmup_immunity <1|0> - enable/disable players immunity during warmup. (defaul: 1)
		- amx_warmup_time <15..120> - warmup duration in seconds. If value isn't get in the range it sets to default. (default: 45)
 	
	* You must restart server or change map for update cvar values.

	Modules:
		- amxmodx
		- fakemeta

	Changelog:
		v1.0 - Initial release
*/

#include <amxmodx>
#include <fakemeta>

new const PLUGIN[] = "Warmup"
new const VERSION[] = "1.0"
new const AUTHOR[] = "jas0n"

new const g_SyncHudMsg1[] = "Autorestart game in %d second(s)"
new const g_SyncHudMsg2[] = "LIVE!!! LIVE!!! LIVE!!!"

new fw_PreThink = -1

new g_IsActive, p_IsActive
new g_Immunity, p_Immunity
new g_Seconds, p_Seconds

new g_SyncHudMsgObj

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)

	g_IsActive = register_cvar("amx_warmup", "1")
	g_Immunity = register_cvar("amx_warmup_immunity", "1")
	g_Seconds = register_cvar("amx_warmup_time", "45")

	g_SyncHudMsgObj = CreateHudSyncObj()
}

public plugin_cfg()
{
	p_IsActive = get_pcvar_num(g_IsActive)
	p_Immunity = get_pcvar_num(g_Immunity)
	p_Seconds = get_pcvar_num(g_Seconds)

	if (p_IsActive == 1)
	{
		if (p_Seconds < 15 || p_Seconds > 120)
			p_Seconds = 45
		
		register_event("TextMsg", "setTimerTask", "a", "2=#Game_Commencing")
	}
	else
		pause("ad")
}

public setTimerTask()
{
	if (p_Immunity == 1)
		setFwRegistered(true)
	
	set_task(1.0, "showTimerMsg", 54162, _, _, "b")
}

public showTimerMsg()
{
	p_Seconds--
	
	if (p_Seconds > 0)
	{
		set_hudmessage(255, 0, 0, 0.02, 0.65, 0, 1.0, 1.0, 0.1, 0.1, -1)
		ShowSyncHudMsg(0, g_SyncHudMsgObj, g_SyncHudMsg1, p_Seconds)
	}
	else
	{
		if(task_exists(54162))
		{
			remove_task(54162)
			
			if (p_Immunity == 1)
				setFwRegistered(false)
			set_task(1.0, "doRestartRound")
			set_task(3.0, "doRestartRound")
			set_task(5.0, "doRestartRound")
			set_task(9.0, "showStartMsg")
			
			return
		}
	}
}

public doRestartRound()
{
	set_cvar_num("sv_restart", 1)
}

public showStartMsg()
{
	set_hudmessage(0, 255, 0, -1.0, 0.35, 0, 3.0, 6.0, 0.1, 0.2, -1)
	ShowSyncHudMsg(0, g_SyncHudMsgObj, g_SyncHudMsg2)

	pause("ad")
}

public setFwRegistered(bool:condition)
{
	if (condition && fw_PreThink == -1)
	{
		fw_PreThink = register_forward(FM_PlayerPreThink, "fwPlayerPreThink")
	}
	
	if (!condition && fw_PreThink != -1)
	{
		unregister_forward(FM_PlayerPreThink, fw_PreThink)
				
		fw_PreThink = -1
	}
	return PLUGIN_HANDLED
}

public fwPlayerPreThink(id)
{
	set_pev(id, pev_takedamage, 0.0)
}