//////////////////////////////////////////////////////////////////
// Smoke Teamcolor By HSFighter / www.hsfighter.net
//////////////////////////////////////////////////////////////////

#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#pragma semicolon 1
#define PLUGIN_VERSION "1.0"

//////////////////////////////////////////////////////////////////
// Declare variables and handles
//////////////////////////////////////////////////////////////////

new Handle:g_SmokecolorEnabled;

new Handle:g_hCVTColor;
new Handle:g_hCVCTColor;

//////////////////////////////////////////////////////////////////
// Plugin info
//////////////////////////////////////////////////////////////////

public Plugin:myinfo = 
{
	name = "Smoke Teamcolor",
	author = "HSFighter",
	description = "Adds teamcolor to smoke from grenade",
	version = PLUGIN_VERSION,
	url = "www.hsfighter.net"
}

//////////////////////////////////////////////////////////////////
// Start plugin
//////////////////////////////////////////////////////////////////

public OnPluginStart()
{
	CreateConVar("sm_smoketeamcolor_version", PLUGIN_VERSION, "Smoke Teamcolor Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
	
	g_SmokecolorEnabled = CreateConVar("sm_smoketeamcolor_enable", "1", "Enable/Disable Plugin");
	g_hCVTColor = CreateConVar("sm_smoketeamcolor_t_color", "255 0 0", "What color should the terrorist smoke be? Format: \"red green blue\" from 0 - 255.", FCVAR_PLUGIN);	
	g_hCVCTColor = CreateConVar("sm_smoketeamcolor_ct_color", "0 0 255", "What color should the counter-terrorist smoke be? Format: \"red green blue\" from 0 - 255.", FCVAR_PLUGIN);	
	
	HookEvent("smokegrenade_detonate", smokegrenade_detonate);

	AutoExecConfig(true, "plugin.smoketeamcolor");
	
}

//////////////////////////////////////////////////////////////////
// Start Map
//////////////////////////////////////////////////////////////////

public OnMapStart()
{ 	
	

}


//////////////////////////////////////////////////////////////////
// Hook event grenade detonate 
//////////////////////////////////////////////////////////////////

public smokegrenade_detonate(Handle:event, const String:name[], bool:dontBroadcast)
{
	// Check if plugin is enabled
	if(GetConVarInt(g_SmokecolorEnabled) != 1) return;
	
	// Get client ID of this event
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if (!IsClientOK(client)) return;
	
	// Get coordinates of this event
	new Float:a[3], Float:b[3];
	a[0] = GetEventFloat(event, "x");
	a[1] = GetEventFloat(event, "y");
	a[2] = GetEventFloat(event, "z");
	
	new checkok = 0;
	new ent = -1;
	
	// List all entitys by classname
	while((ent = FindEntityByClassname(ent, "env_particlesmokegrenade")) != -1)
	{
		// Get entity coordinates
		GetEntPropVector(ent, Prop_Send, "m_vecOrigin", b);
		
		// If entity same coordinates some event coordinates
		if(a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
		{		
			checkok = 1;
			break;
		}
	}
    
	if (checkok == 1)
	{
		
		decl String:sBuffer[64];
		
		// Get client team
		new player_team_index = GetClientTeam(client);
		switch (player_team_index) 
		{
			case 2:
			{
				GetConVarString(g_hCVTColor, sBuffer, sizeof(sBuffer));
			}
			case 3:
			{
				GetConVarString(g_hCVCTColor, sBuffer, sizeof(sBuffer));
				
			}
			default:
			{
				
			}
		}		
		
		// Create light
		new iEntity = CreateEntityByName("light_dynamic");
		
		DispatchKeyValue(iEntity, "_light", sBuffer);
		Format(sBuffer, sizeof(sBuffer), "smokelight_%d", iEntity);
		DispatchKeyValue(iEntity,"targetname", sBuffer);
		Format(sBuffer, sizeof(sBuffer), "%f %f %f", a[0], a[1], a[2]);
		DispatchKeyValue(iEntity, "origin", sBuffer);
		DispatchKeyValue(iEntity, "iEntity", "-90 0 0");
		DispatchKeyValue(iEntity, "pitch","-90");
		DispatchKeyValue(iEntity, "distance","256");
		DispatchKeyValue(iEntity, "spotlight_radius","96");
		DispatchKeyValue(iEntity, "brightness","3");
		DispatchKeyValue(iEntity, "style","6");
		DispatchKeyValue(iEntity, "spawnflags","1");
		DispatchSpawn(iEntity);
		AcceptEntityInput(iEntity, "DisableShadow");
		AcceptEntityInput(iEntity, "TurnOn");
		
		CreateTimer(20.0, Delete, iEntity, TIMER_FLAG_NO_MAPCHANGE);
		
	}

}


//////////////////////////////////////////////////////////////////
// Check client
//////////////////////////////////////////////////////////////////

public bool:IsClientOK(client)
{
		
	if (client != 0)
	{
		// Get all clients on the server
		for (new i = 1; i <= MaxClients; i++)
		{
			// Check if player ok
			if (IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(client))
			{
				// Check if client is a player on the server
				if (i == client) return true;			
			}
		}	
	}	
	return false;
}


//////////////////////////////////////////////////////////////////
// Delete entity
//////////////////////////////////////////////////////////////////


public Action:Delete(Handle:timer, any:entity)
{
    if(IsValidEdict(entity)) AcceptEntityInput(entity, "kill");
}  


//////////////////////////////////////////////////////////////////
// End Plugin
//////////////////////////////////////////////////////////////////