

#include <amxmodx>
#include <fakemeta>
#include <engine>
#include <hamsandwich>
#include <zombieplague>
#include <amxmisc>

#define ice_model "models/Gyiove/dd_iceblock.mdl"
new iceent[33]
const PEV_NADE_TYPE = pev_flTimeStepSound
const NADE_TYPE_FROST = 3333
const Float:NADE_EXPLOSION_RADIUS = 240.0

public plugin_init( ) 
{
	register_plugin( "[ZP] ZP++ Ice Cube", "1.0", "DareDevil (Gyiove)" )
	RegisterHam( Ham_Killed, "player", "pl_killed" )
	register_forward(FM_EmitSound, "fw_EmitSound")
	RegisterHam(Ham_Spawn, "player", "fw_PlayerSpawn_Post", 1)
	register_logevent("round_ended", 2, "1=Round_End")
}


public plugin_precache() 
{
	engfunc(EngFunc_PrecacheModel, ice_model)
	RegisterHam(Ham_Think, "grenade", "fw_ThinkGrenade") // First one to cross the finish line! Take that zp! :D
}
public pl_killed(id) ice_entity( id, 0 )
public client_disconnect(id) ice_entity( id, 0 )
public client_connect(id) iceent[id] = -1
public round_ended()
{
	static a
	for( a = 1; a < 33; a++ ) ice_entity(a, 0 )
}
public zp_user_infected_pre(id) ice_entity(id, 0 )
public zp_user_humanized_pre(id) ice_entity(id, 0 )
public zp_user_unfrozen(id) ice_entity(id, 0 )
public fw_PlayerSpawn_Post(id) ice_entity(id, 0 )

public fw_ThinkGrenade(entity)
{
	if (!pev_valid(entity)) return HAM_IGNORED;
	static Float:dmgtime, Float:current_time
	pev(entity, pev_dmgtime, dmgtime)
	current_time = get_gametime()
	if (dmgtime > current_time) return HAM_IGNORED;
	if( pev(entity, PEV_NADE_TYPE) == NADE_TYPE_FROST )
	{
		static Float:originF[3], victim; victim = -1
		pev(entity, pev_origin, originF)
		
		while ((victim = engfunc(EngFunc_FindEntityInSphere, victim, originF, NADE_EXPLOSION_RADIUS)) != 0)
		{
			// Only effect alive unfrozen zombies
			if (!is_user_alive(victim) || !zp_get_user_zombie(victim) ) continue;
			
			// since we cant hook madness somehow because emit_sound wont call the FM_EmitSound, we have to do things the hard way.
			set_task( 0.1, "ami_frozen", victim ) 
		}
	}
	return HAM_IGNORED;
}

public ami_frozen(id)
{
	static Float:m
	pev(id, pev_maxspeed, m)
	if(m == 1.0) 
	{
		ice_entity( id, 1 ) 
	}
}

stock ice_entity( id, status ) 
{
	if(status)
	{
		static ent, Float:o[3]
		if( !is_user_alive(id) || !zp_get_user_zombie(id) || zp_get_user_nemesis(id) )
		{
			ice_entity( id, 0 )
			return
		}
		
		if( is_valid_ent(iceent[id]) )
		{
			if( pev( iceent[id], pev_iuser3 ) != id )
			{
				if( pev(iceent[id], pev_team) == 6969 ) remove_entity(iceent[id])
			}
			else
			{
				pev( id, pev_origin, o )
				if( pev( id, pev_flags ) & FL_DUCKING  ) o[2] -= 15.0
				else o[2] -= 35.0
				entity_set_origin(iceent[id], o)
				return
			}
		}
		
		pev( id, pev_origin, o )
		if( pev( id, pev_flags ) & FL_DUCKING  ) o[2] -= 15.0
		else o[2] -= 35.0
		ent = create_entity("info_target")
		set_pev( ent, pev_classname, "DareDevil" )
		
		entity_set_model(ent, ice_model)
		dllfunc(DLLFunc_Spawn, ent)
		set_pev(ent, pev_solid, SOLID_NOT)
		set_pev(ent, pev_movetype, MOVETYPE_FLY)
		entity_set_origin(ent, o)
		entity_set_size(ent, Float:{ -3.0, -3.0, -3.0 }, Float:{ 3.0, 3.0, 3.0 } )
		set_pev( ent, pev_iuser3, id )
		set_pev( ent, pev_team, 6969 )
		set_rendering(ent, kRenderFxNone, 255, 255, 255, kRenderTransAdd, 255)
		iceent[id] = ent
	}
	else
	{
		if( is_valid_ent(iceent[id]) )
		{
			if( pev(iceent[id], pev_team) == 6969 ) remove_entity(iceent[id])
			iceent[id] = -1
		}
	}
}

