
// dont actually use this...
stock bool:IsPlayer(client)
{
	if (client <= 0) return false;
	if (client > MAXPLAYERS) return false;
	if (!IsClientConnected(client)) return false;
	return IsClientInGame(client);
}

ResetPlayer(client, bool:resetView = false, bool:resetHead = true)
{
	if(resetHead)
		g_HeadEnt[client] = -1;
	g_HeadLastHolder[client] = -1;
	
	g_ParticleEnt[client] = -1;	
	g_HeldEnt[client] = -1;
	
	g_ThrowPosValid[client] = false;
	g_ThrowPosX[client] = 0.0;
	g_ThrowPosY[client] = 0.0;
	g_ThrowPosZ[client] = 0.0;
	g_ThrowCharge[client] = 0.0;
	
	//if(IsPlayer(client) == false) PrintToServer("[SM][DC] StoppingSound:FALSE");
	StopSound(client, SNDCHAN_AUTO, g_HoldSound);
	
	if(resetView)
		ResetView(client);
}

public Action:RemoveRagdoll(Handle:Timer, any:client)
{
	if (!IsValidEntity(client))
		return;
	
	new String:edictname[128];

	new BodyRagdoll = GetEntPropEnt(client, Prop_Send, "m_hRagdoll");
	if(IsValidEdict(BodyRagdoll))
	{
	
		//Find:
		GetEdictClassname(BodyRagdoll, edictname, sizeof(edictname)); 

		//Remove:
		if(StrEqual(edictname, "cs_ragdoll", false)) RemoveEdict(BodyRagdoll);
	}
}

DealDamage(victim,damage,attacker=0,dmg_type=0,String:weapon[]="")
{
	//if(IsPlayer(victim) == false) PrintToServer("[SM][DC] DealDamage(victim):FALSE");
	//if(IsPlayer(attacker) == false) PrintToServer("[SM][DC] StoppingSound(attacker):FALSE");
	if(victim>0 && IsValidEdict(victim) && IsClientInGame(victim) && IsPlayerAlive(victim) && damage>0)
    {
        new String:dmg_str[16];
        IntToString(damage,dmg_str,16);
        new String:dmg_type_str[32];
        IntToString(dmg_type,dmg_type_str,32);
        new PointHurt = CreateEntityByName("point_hurt");
        if(PointHurt)
        {
            DispatchKeyValue(victim,"targetname","dmged_target");
            DispatchKeyValue(PointHurt,"DamageTarget","dmged_target");
            DispatchKeyValue(PointHurt,"Damage",dmg_str);
            DispatchKeyValue(PointHurt,"DamageType",dmg_type_str);
            if(!StrEqual(weapon,""))
            {
                DispatchKeyValue(PointHurt,"classname",weapon);
            }
            DispatchSpawn(PointHurt);
            AcceptEntityInput(PointHurt,"Hurt",(attacker>0)?attacker:-1);
            DispatchKeyValue(PointHurt,"classname","point_hurt");
            DispatchKeyValue(victim,"targetname","nondmged_target");
            RemoveEdict(PointHurt);
        }
    }
}  


// game events
public Action:EventPlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new hitgroup = GetEventInt(event, "hitgroup");
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new doDecap = false;
	
	//if(IsPlayer(victim) == false) PrintToServer("[SM][DC] EventPlayerHurt(victim):FALSE");
	//if(IsPlayer(attacker) == false) PrintToServer("[SM][DC] EventPlayerHurt(attacker):FALSE");
	
	new String:weapon[64];
	GetEventString(event, "weapon", weapon, 64);

	if((hitgroup == 1) && (GetConVarInt(cvDecapHeadshot)==1))
		doDecap = true;
	if((StrEqual(weapon, "knife", false)) && (GetConVarInt(cvDecapKnife)==1))
		doDecap = true;
	if((StrEqual(weapon, "hegrenade", false)) && (GetConVarInt(cvDecapHE)==1))
		doDecap = true;
	
	if((GetConVarBool(cvTKEnabled) == false) && (GetClientTeam(attacker) == GetClientTeam(victim)))
		doDecap = false;

	if (doDecap)
	{
		new String:oldmodel[256];
		new String:bodymodel[256];
		new String:headmodel[256];

		GetClientModel(victim, oldmodel, 256);

		if (StrEqual(oldmodel, "models/player/ct_gign.mdl", false))
		{
			bodymodel = "models/player/ct_gign1_dc.mdl";
			headmodel = "models/player/ct_gign_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/ct_gsg9.mdl", false))
		{
			bodymodel = "models/player/ct_gsg91_dc.mdl";
			headmodel = "models/player/ct_gsg9_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/ct_sas.mdl", false))
		{
			bodymodel = "models/player/ct_sas1_dc.mdl";
			headmodel = "models/player/ct_sas_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/ct_urban.mdl", false))
		{
			bodymodel = "models/player/ct_urban1_dc.mdl";
			headmodel = "models/player/ct_urban_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/t_arctic.mdl", false))
		{
			bodymodel = "models/player/t_arctic1_dc.mdl";
			headmodel = "models/player/t_arctic_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/t_guerilla.mdl", false))
		{
			bodymodel = "models/player/t_guerilla1_dc.mdl";
			headmodel = "models/player/t_guerilla_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/t_leet.mdl", false))
		{
			bodymodel = "models/player/t_leet1_dc.mdl";
			headmodel = "models/player/t_leet_head.mdl";
		}
		else if (StrEqual(oldmodel, "models/player/t_phoenix.mdl", false))
		{
			bodymodel = "models/player/t_phoenix1_dc.mdl";
			headmodel = "models/player/t_phoenix_head.mdl";
		}
		else
		{
			doDecap = false;
		}

		if (IsModelPrecached(bodymodel) && IsModelPrecached(headmodel) && doDecap)
		{
			new String:attacker_name[64];
			new String:victim_name[64];
			GetClientName(attacker, attacker_name, sizeof(attacker_name));
			GetClientName(victim, victim_name, sizeof(victim_name));
			
			if(IsPlayer(attacker))
				PrintToChatAll("%s decapinated %s!", attacker_name, victim_name);
			else
				PrintToChatAll("%s lost their head!!!", victim_name);
			
			SetEntityModel(victim, bodymodel);
			GiveHead(victim, headmodel);
		}
	}
	return Plugin_Continue;
}

public Action:EventSpawn(Handle:Event, const String:Name[], bool:Broadcast)
{
	new client = GetClientOfUserId(GetEventInt(Event, "userid"));
	
	if (g_SpurtTimer[client] != INVALID_HANDLE)
	{
		KillTimer(g_SpurtTimer[client]);
		g_SpurtTimer[client] = INVALID_HANDLE;
	}
	
	ResetPlayer(client);
	
	return Plugin_Continue;
}

public Action:EventDeath(Handle:Event, const String:Name[], bool:Broadcast)
{
	new client = GetClientOfUserId(GetEventInt(Event, "userid"));
	//if(IsPlayer(client) == false) PrintToServer("[SM][DC] EventDeath:FALSE");
	
	if (g_SpurtTimer[client] != INVALID_HANDLE)
	{
		KillTimer(g_SpurtTimer[client]);
		g_SpurtTimer[client] = INVALID_HANDLE;
	}
	
	ResetPlayer(client,true,false);
	
	return Plugin_Continue;
}

public Action:EventTeam(Handle:Event, const String:Name[], bool:Broadcast)
{
	new client = GetClientOfUserId(GetEventInt(Event, "userid"));
	
	if (g_SpurtTimer[client] != INVALID_HANDLE)
	{
		KillTimer(g_SpurtTimer[client]);
		g_SpurtTimer[client] = INVALID_HANDLE;
	}
	
	ResetPlayer(client);
	
	return Plugin_Continue;
}
