#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <cstrike>

#define TER_MODEL	"models/player/ics/t_leet_kking/t_leet.mdl"
#define CT_MODEL	"models/player/ics/ct_gign_kking/ct_gign.mdl"

public Plugin:myinfo = 
{
	name = "Svinorez",
	author = "NoTiCE",
	description = "Svinorez",
	version = "1.0",
	url = "http://novgames.ru/"
};

new g_iSvin = -1;
new String:g_sLastModel[PLATFORM_MAX_PATH];

public OnPluginStart()
{
	HookEvent("player_spawn", Event_PlayerSpawn);
	HookEvent("player_death", Event_PlayerDeath);
}

public OnMapStart()
{
	AddFileToDownloadsTable("models/player/ics/ct_gign_kking/ct_gign.dx80.vtx");
	AddFileToDownloadsTable("models/player/ics/ct_gign_kking/ct_gign.dx90.vtx");
	AddFileToDownloadsTable(TER_MODEL);
	AddFileToDownloadsTable("models/player/ics/ct_gign_kking/ct_gign.phy");
	AddFileToDownloadsTable("models/player/ics/ct_gign_kking/ct_gign.sw.vtx");
	AddFileToDownloadsTable("models/player/ics/ct_gign_kking/ct_gign.vvd");
	AddFileToDownloadsTable("materials/models/player/ics/ct_gign_kking/ct_gign.vmt");
	AddFileToDownloadsTable("materials/models/player/ics/ct_gign_kking/ct_gign.vtf");
	AddFileToDownloadsTable("materials/models/player/ics/ct_gign_kking/ct_gign_glass.vmt");
	
	AddFileToDownloadsTable("models/player/ics/t_leet_kking/t_leet.dx80.vtx");
	AddFileToDownloadsTable("models/player/ics/t_leet_kking/t_leet.dx90.vtx");
	AddFileToDownloadsTable(CT_MODEL);
	AddFileToDownloadsTable("models/player/ics/t_leet_kking/t_leet.phy");
	AddFileToDownloadsTable("models/player/ics/t_leet_kking/t_leet.sw.vtx");
	AddFileToDownloadsTable("models/player/ics/t_leet_kking/t_leet.vvd");
	AddFileToDownloadsTable("materials/models/player/ics/t_leet_kking/t_leet.vmt");
	AddFileToDownloadsTable("materials/models/player/ics/t_leet_kking/t_leet.vtf");
	AddFileToDownloadsTable("materials/models/player/ics/t_leet_kking/t_leet_glass.vmt");
	
	PrecacheModel(TER_MODEL);
	PrecacheModel(CT_MODEL);
}

public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
	decl String:sWeapon[32];
	GetEventString(event, "weapon", sWeapon, sizeof(sWeapon));
	if(StrEqual(sWeapon, "knife", false))
	{
		new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
		if(!IsClientIndex(attacker) || !IsClientInGame(attacker))
			return;
		
		new victim = GetClientOfUserId(GetEventInt(event, "userid"));
		if(!IsClientIndex(victim) || !IsClientInGame(victim))
			return;
		
		if(GetClientTeam(attacker) == GetClientTeam(victim))
			return;
		
		decl String:buffer[256];
		Format(buffer, sizeof(buffer), "\x03%N: \x01Хрю-Хрюююю...", victim);
		for(new target = 1; target <= MaxClients; target++)
			if(IsClientInGame(target))
				CSendMessage(target, buffer, victim);
		
		if(g_iSvin == attacker)
			return;
		
		if(IsClientIndex(g_iSvin) && IsClientInGame(g_iSvin))
		{
			if(g_iSvin == victim)
			{
				new ragdoll = GetEntPropEnt(victim, Prop_Send, "m_hRagdoll");
				if(IsValidEntity(ragdoll))
					SetEntityModel(ragdoll, g_sLastModel);
			}
			else if(IsPlayerAlive(g_iSvin))
				SetEntityModel(g_iSvin, g_sLastModel);
		}
		
		g_iSvin = attacker;
		CreateTimer(0.0, Timer_PostSpawn, attacker, TIMER_FLAG_NO_MAPCHANGE);
	}
}

public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	if(client && client == g_iSvin)
		CreateTimer(0.5, Timer_PostSpawn, client, TIMER_FLAG_NO_MAPCHANGE);
}

public Action:Timer_PostSpawn(Handle:timer, any:client)
{
	if(client == g_iSvin && IsClientInGame(client) && IsPlayerAlive(client))
	{
		GetClientModel(client, g_sLastModel, sizeof(g_sLastModel));
		
		switch(GetClientTeam(client))
		{
			case CS_TEAM_T:
			{
				if(IsModelPrecached(TER_MODEL))
					SetEntityModel(client, TER_MODEL);
			}
			case CS_TEAM_CT:
			{
				if(IsModelPrecached(CT_MODEL))
					SetEntityModel(client, CT_MODEL);
			}
		}
	}
	
	return Plugin_Stop;
}

public OnClientDisconnect(client)
{
	if(client == g_iSvin)
		g_iSvin = -1;
}

bool:IsClientIndex(index)
{
	return (index > 0) && (index <= MaxClients);
}

stock CSendMessage(client, const String:message[], author=0)
{
	if(author == 0)
		author = client;
	
	new UserMsg:index = GetUserMessageId("SayText2");
	if(index == INVALID_MESSAGE_ID)
	{
		PrintToChat(client, "%s", message);
		return;
	}
	new Handle:bf = StartMessageOne("SayText2", client, USERMSG_RELIABLE|USERMSG_BLOCKHOOKS);
	BfWriteByte(bf, author); // Message author
	BfWriteByte(bf, true);
	BfWriteString(bf, message);
	EndMessage();
}