// PITCH BLACK!

/* CVARS - copy and paste to shconfig.cfg

pitch_level 0
pitch_time 7				//Ammount of time the darkness lasts
pitch_cooldown 60			//Seconds till next available use of power

*/

//LOADS OF THNX TO FREECODE FOR GETTING THIS WORKING

#include <amxmod>
#include <Vexd_Utilities>
#include <superheromod>

// GLOBAL VARIABLES
new gHeroName[]="Pitch Black"
new gHasPitchPower[SH_MAXSLOTS+1]
new PitchTimer[SH_MAXSLOTS+1]
new bool:NightVisionUse[SH_MAXSLOTS+1]
new bool:lightsOut = false
new bool:FirstSpawn = true
new NVGToggle, numOfPB = 0
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Pitch Black","1.2","[FTW]-S.W.A.T")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("pitch_level", "0" )
	register_cvar("pitch_time", "7" )
	register_cvar("pitch_cooldown", "60" )

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Total Darkness", "Make everything Pitch Black. Get Riddick's sight.", true, "pitch_level" )

	// REGISTER EVENTS THIS HERO WILL RESPOND TO! (AND SERVER COMMANDS)
	register_srvcmd("pitch_init", "pitch_init")
	shRegHeroInit(gHeroName, "pitch_init")

	// KEY DOWN
	register_srvcmd("pitch_kd", "pitch_kd")
	shRegKeyDown(gHeroName, "pitch_kd")

	register_event("ResetHUD","newSpawn","b")
	register_event("DeathMsg", "pitch_death", "a")
	register_logevent("round_end", 2, "1=Round_End")
	register_logevent("round_end", 2, "1&Restart_Round_")

	set_task(1.0, "pitch_loop", 0, "", 0, "b")

	// NightVision
	register_clcmd("nightvision","ToggleNVG")
	NVGToggle = get_user_msgid("NVGToggle")
}
//----------------------------------------------------------------------------------------------
public pitch_init()
{
	// First Argument is an id
	new temp[6]
	read_argv(1,temp,5)
	new id=str_to_num(temp)

	// 2nd Argument is 0 or 1 depending on whether the id has Pitch Black
	read_argv(2,temp,5)
	new hasPowers = str_to_num(temp)

	gHasPitchPower[id] = (hasPowers != 0)

	PitchTimer[id] = -1
	gPlayerUltimateUsed[id] = false

	if (!gHasPitchPower[id] && PitchTimer[id] > 0) {
		numOfPB -= 1
		StopNVG(id)
		if (numOfPB <= 0) {
			numOfPB = 0
			lightsOn()
		}
	}
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
	precache_sound( "items/nvg_on.wav" )
	precache_sound( "items/nvg_off.wav" )
}
//----------------------------------------------------------------------------------------------
public pitch_kd()
{
	// First Argument is an id with Pitch Powers!
	new temp[6]
	read_argv(1,temp,5)
	new id = str_to_num(temp)

	if ( !is_user_alive(id) || !gHasPitchPower[id]) return PLUGIN_HANDLED

	if ( gPlayerUltimateUsed[id] || PitchTimer[id] >= 0) {
		playSoundDenySelect(id)
		return PLUGIN_HANDLED
	}

	PitchTimer[id] = get_cvar_num("pitch_time")
	ultimateTimer(id, get_cvar_float("pitch_cooldown"))

	if (!lightsOut) {
		lightsOut = true
		#if defined AMXX_VERSION
		set_lights("a")
		#else
		SetLights("a")
		#endif
	}

	StartNVG(id)
	numOfPB += 1

	return PLUGIN_HANDLED
}
//----------------------------------------------------------------------------------------------
public round_end()
{
	FirstSpawn = true
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	gPlayerUltimateUsed[id] = false
	PitchTimer[id] = -1

	if (NightVisionUse[id]) StopNVG(id)

	if (lightsOut && FirstSpawn) {
		numOfPB = 0
		FirstSpawn = false
		lightsOn()
	}
}
//----------------------------------------------------------------------------------------------
public client_disconnect(id)
{
	PitchTimer[id] = -1
	gPlayerUltimateUsed[id] = false
	remove_task(id)
	NightVisionUse[id] = false
}
//----------------------------------------------------------------------------------------------
public pitch_death()
{
	new id = read_data(2)

	if (PitchTimer[id] > 0) {
		PitchTimer[id] = -1
		gPlayerUltimateUsed[id] = false
		numOfPB -= 1
		StopNVG(id)

		if (numOfPB <= 0) {
			numOfPB = 0
			lightsOn()
		}
	}
}
//----------------------------------------------------------------------------------------------
public lightsOn()
{
	lightsOut = false

	#if defined AMXX_VERSION
	set_lights("#OFF")
	#else
	SetLights("#OFF")
	#endif

	new players[SH_MAXSLOTS], player_num, id
	get_players(players, player_num, "ac")
	for (new i = 0; i < player_num; i++) {
		id = players[i]
		set_hudmessage(0, 128, 0, -1.0, 0.65, 2, 0.02, 6.0, 0.01, 0.1, 62)
		show_hudmessage(id,"Normal Lighting Resotred")
		if (NightVisionUse[id]) StopNVG(id)
	}
}
//----------------------------------------------------------------------------------------------
public ToggleNVG(id)
{
	if ( !shModActive() || !gHasPitchPower[id] || !is_user_alive(id) ) return PLUGIN_CONTINUE

	if (NightVisionUse[id])		StopNVG(id)
	else						StartNVG(id)

	return PLUGIN_HANDLED
}
//----------------------------------------------------------------------------------------------
public StartNVG(id)
{
	if ( !gHasPitchPower[id] || !shModActive() || !is_user_alive(id) ) return

	message_begin(MSG_ONE, NVGToggle, {0,0,0}, id)
	write_byte( 0 )
	message_end()

	NightVisionUse[id] = true
	emit_sound(id,CHAN_ITEM,"items/nvg_on.wav",VOL_NORM,ATTN_NORM,0,PITCH_NORM)
	set_task(0.1,"RunNVG",id,"",0,"b")

}
//----------------------------------------------------------------------------------------------
public StopNVG(id)
{
	emit_sound(id,CHAN_ITEM,"items/nvg_off.wav",VOL_NORM,ATTN_NORM,0,PITCH_NORM)
	remove_task(id)
	NightVisionUse[id] = false
}
//----------------------------------------------------------------------------------------------
public RunNVG(id)
{
	if (!is_user_alive(id) || !NightVisionUse[id]) {
		StopNVG(id)
		return
	}

	new origin[3]
	get_user_origin(id,origin)

	//NVG Entity
	message_begin(MSG_ONE,SVC_TEMPENTITY,{0,0,0},id)
	write_byte(27)			//entity type
	write_coord(origin[0])	//x
	write_coord(origin[1])	//y
	write_coord(origin[2])	//z
	write_byte(125)		//visible distance
	write_byte(98)			//red
	write_byte(0)			//blue
	write_byte(76)			//green
	write_byte(3)			//deciseconds
	write_byte(10)			//distance flicker
	message_end()

	//Darkens it a little
	//setScreenFlash(id, 107, 0, 83, 12, 100)
}
//----------------------------------------------------------------------------------------------
public pitch_loop()
{
	if (!lightsOut) return

	new players[SH_MAXSLOTS], player_num, id
	get_players(players, player_num, "ac")
	for (new i = 0; i < player_num; i++) {
		if (!lightsOut) return
		id = players[i]
		if(PitchTimer[id] > 0) {
			new message[64]
			format(message, 63, "PitchBlack - %d seconds left", PitchTimer[id])
			set_hudmessage(255, 255, 255, -1.0, 0.3, 0, 1.0, 1.1, 0.0, 0.0, 4)
			show_hudmessage(id, message)
			PitchTimer[id]--
			continue
		}
		else if ( PitchTimer[id] == 0 ) {
			PitchTimer[id] = -1
			numOfPB -= 1

			if (numOfPB <= 0) {
				numOfPB = 0
				lightsOn()
			}
			continue
		}
		else {
			set_hudmessage(255, 255, 255, -1.0, 0.65, 2, 0.02, 2.0, 0.01, 0.1, 62)
			show_hudmessage(id,"Pitch Black Mode - Total Darkness")
			continue
		}
	}
}
//----------------------------------------------------------------------------------------------