#include <sourcemod>
#include <sdktools>
#define TO_HEALTH 20

new bloodDecal[13];
static const String:particleName22[14][]={"blood_impact_heavy", "blood_impact_goop_heavy", "blood_impact_red_01_chunk", "blood_impact_headshot_01c", "blood_impact_headshot_01d", "blood_impact_basic", "blood_impact_medium", "blood_impact_red_01_goop_a", "blood_impact_red_01_goop_b", "blood_impact_goop_medium", "blood_impact_red_01_goop_c", "blood_impact_red_01_drops", "blood_impact_red_01_backspray", "blood_impact_drops1"}
public OnPluginStart()
{
	HookEvent("player_footstep", Event_Step)
	RegAdminCmd("sm_decalsetgore", DSG, ADMFLAG_ROOT)
//	OnMapStart()
}

public Action DSG(client,args)
{
	decl String:arg[256]
	GetCmdArg(1, arg, sizeof(arg))
	GoreDecalNum(client, StringToInt(arg))
}

public Action ForcePrecacheAll(Handle timer)
{
	bloodDecal[0] = PrecacheDecal("decals/blood_splatter.vtf");
	bloodDecal[1] = PrecacheDecal("decals/bloodstain_003.vtf");
	bloodDecal[2] = PrecacheDecal("decals/bloodstain_101.vtf");
	bloodDecal[3] = PrecacheDecal("decals/bloodstain_002.vtf");
	bloodDecal[4] = PrecacheDecal("decals/bloodstain_001.vtf");
	bloodDecal[5] = PrecacheDecal("decals/blood8.vtf");
	bloodDecal[6] = PrecacheDecal("decals/blood7.vtf");
	bloodDecal[7] = PrecacheDecal("decals/blood6.vtf");
	bloodDecal[8] = PrecacheDecal("decals/blood5.vtf");
	bloodDecal[9] = PrecacheDecal("decals/blood4.vtf");
	bloodDecal[10] = PrecacheDecal("decals/blood3.vtf");
	bloodDecal[11] = PrecacheDecal("decals/blood2.vtf");
	bloodDecal[12] = PrecacheDecal("decals/blood1.vtf");
	ForcePrecache("blood_impact_heavy");
	ForcePrecache("blood_impact_goop_heavy");
	ForcePrecache("blood_impact_red_01_chunk");
	ForcePrecache("blood_impact_headshot_01c");
	ForcePrecache("blood_impact_headshot_01b");
	ForcePrecache("blood_impact_headshot_01d");
	ForcePrecache("blood_impact_basic");
	ForcePrecache("blood_impact_medium");
	ForcePrecache("blood_impact_red_01_goop_a");
	ForcePrecache("blood_impact_red_01_goop_b");
	ForcePrecache("blood_impact_goop_medium");
	ForcePrecache("blood_impact_red_01_goop_c");
	ForcePrecache("blood_impact_red_01_drops");
	ForcePrecache("blood_impact_drops1");
	ForcePrecache("blood_impact_red_01_backspray");
}

public OnMapStart()
{
	CreateTimer(1.0, ForcePrecacheAll)
	
}

GoreDecal(client, count)
{
	new decal;
	new Float:origin[3];
	
	GetClientAbsOrigin(client, origin);
	
	for (new i = 0; i < count; i++)
	{
		origin[0] += GetRandomFloat(-30.0, 30.0);
		origin[1] += GetRandomFloat(-30.0, 30.0);
	
		if (GetRandomInt(1, 20) == 20)
			decal = GetRandomInt(2, 4);
		else
			decal = GetRandomInt(5, 12);
//		PrintToConsole(client, "Blood decal number %i", decal)
		TE_Start("World Decal");
		TE_WriteVector("m_vecOrigin", origin);
		TE_WriteNum("m_nIndex", bloodDecal[decal]);
		TE_SendToAll();
	}
}
 
GoreDecalNum(client, num)
{
	new Float:origin[3];
	GetClientAbsOrigin(client, origin);
	origin[0] += GetRandomFloat(-30.0, 30.0);
	origin[1] += GetRandomFloat(-30.0, 30.0);
//	PrintToConsole(client, "Blood decal number %i", decal)
	TE_Start("World Decal");
	TE_WriteVector("m_vecOrigin", origin);
	TE_WriteNum("m_nIndex", bloodDecal[num]);
	TE_SendToAll();
}



ForcePrecache(String:particleName[])
{
	new particle;
	particle = CreateEntityByName("info_particle_system");
	
	if(IsValidEdict(particle))
	{
		DispatchKeyValue(particle, "effect_name", particleName);
		
		DispatchSpawn(particle);
		ActivateEntity(particle);
		AcceptEntityInput(particle, "start");
		
		CreateTimer(1.0, DeleteParticle, particle, TIMER_FLAG_NO_MAPCHANGE);
	}
}

public Action Event_Step(Handle event, char[] name, bool dbc)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"))
	if(!client||!IsClientInGame(client))
		return Plugin_Continue;
	if(GetClientHealth(client)>TO_HEALTH)
		return Plugin_Continue
	new Float:pos[3]
	GetClientEyePosition(client, pos)
	pos[2]-=GetRandomFloat(25.0,75.0)
	new particle = CreateEntityByName("info_particle_system");
	if(!particle||!IsValidEntity(particle))
		return Plugin_Continue;
	TeleportEntity(particle, pos, NULL_VECTOR, NULL_VECTOR);
	DispatchKeyValue(particle, "targetname", "CSGOParticle");
	DispatchKeyValue(particle, "effect_name", particleName22[GetRandomInt(0,13)]);
	DispatchSpawn(particle);
	ActivateEntity(particle);
	AcceptEntityInput(particle, "start");
	CreateTimer(0.5, DeleteParticle, particle);
	if(GetEntProp(client, Prop_Send, "m_nWaterLevel")==0)
		GoreDecal(client, 5)
	return Plugin_Continue;
}

public Action:DeleteParticle(Handle:Timer, any:particle)
{
	if (IsValidEdict(particle))
	{	
		new String:className[64];

		GetEdictClassname(particle, className, sizeof(className));

		if(StrEqual(className, "info_particle_system", false))
			RemoveEdict(particle);
	}
}