//////////////////////////////////////////////////////////////////
// Smoke Color By HSFighter / www.hsfighter.net
//////////////////////////////////////////////////////////////////

#include <sourcemod>
#include <sdktools>
#pragma semicolon 1
#define PLUGIN_VERSION "1.2"

//////////////////////////////////////////////////////////////////
// Declare variables and handles
//////////////////////////////////////////////////////////////////

new Handle:g_SmokecolorEnabled;
new Handle:g_SmokecolorMode;

new Handle:TimeHandle;

new Handle:g_hCVTColor;
new Handle:g_hCVCTColor;

new Float:g_HSV_Temp = 0.0;

//////////////////////////////////////////////////////////////////
// Plugin info
//////////////////////////////////////////////////////////////////

public Plugin:myinfo = 
{
	name = "Smoke Color",
	author = "HSFighter",
	description = "Adds color 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_smokecolor_enable",   "1",       "Enable/Disable Plugin");
	g_hCVTColor            = CreateConVar("sm_smokecolor_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_smokecolor_ct_color", "0 0 255", "What color should the counter-terrorist smoke be ? Format: \"red green blue\" from 0 - 255.", FCVAR_PLUGIN);	
	g_SmokecolorMode       = CreateConVar("sm_smokecolor_mode",   	"0",       "???", FCVAR_PLUGIN, true, 0.0, true, 2.0);
	
	HookEvent("smokegrenade_detonate", smokegrenade_detonate);

	AutoExecConfig(true, "plugin.smokecolor");
	
}

//////////////////////////////////////////////////////////////////
// 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)
	{
		
		
		// Create light
		new iEntity = CreateEntityByName("light_dynamic");
		decl String:sBuffer[64];
		
		
		
		// Select Action Mode
		switch (GetConVarInt(g_SmokecolorMode))
		{
			case 0:
			{
				// 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));
					}
				}	
				DispatchKeyValue(iEntity, "_light", sBuffer);
			}
			case 1:
			{
				g_HSV_Temp = GetRandomFloat(1.0, 360.0);
				
				new Float:flRed, Float:flGreen, Float:flBlue;
				HSVtoRGB(g_HSV_Temp, 1.0, 1.0, flRed, flGreen, flBlue );	
				Format(sBuffer, sizeof(sBuffer), "%i %i %i", RoundFloat(flRed*255.0), RoundFloat(flGreen*255.0), RoundFloat(flBlue*255.0));
				DispatchKeyValue(iEntity, "_light", sBuffer);
			}			
			case 2:
			{
				TimeHandle = CreateTimer(0.1, Checktime, iEntity, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
			}
		}	
			
		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);
	}

}

public Action:Checktime(Handle:colortimer, any:entity){

	decl String:sBuffer[64];

	g_HSV_Temp = g_HSV_Temp + 3.0;
	
	new Float:flRed, Float:flGreen, Float:flBlue;
	HSVtoRGB(g_HSV_Temp, 1.0, 1.0, flRed, flGreen, flBlue );	
	//PrintHintTextToAll ("Debug: %i -->> r=%i g=%i b=%i", RoundFloat(g_HSV_Temp), RoundFloat(flRed*255.0), RoundFloat(flGreen*255.0), RoundFloat(flBlue*255.0));
	Format(sBuffer, sizeof(sBuffer), "%i %i %i", RoundFloat(flRed*255.0), RoundFloat(flGreen*255.0), RoundFloat(flBlue*255.0));
	if (g_HSV_Temp >= 360.0) g_HSV_Temp = 0.0;
	DispatchKeyValue(entity, "_light", sBuffer);

}

//////////////////////////////////////////////////////////////////
// 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;
}



//////////////////////////////////////////////////////////////////
// HSV color to RGB color
//////////////////////////////////////////////////////////////////


HSVtoRGB(&Float:h, Float:s, Float:v, &Float:r, &Float:g, &Float:b){

	if (s == 0)
	{
		r = v;  g = v;  b = v;
	} else {
		
		new Float:fHue, Float:fValue, Float:fSaturation;
		new Float:f;  new Float:p,Float:q,Float:t;
		if (h == 360.0) h = 0.0;
		fHue = h / 60.0;
		new i = RoundToFloor(fHue);
		f = fHue - i;
		fValue = v;
		fSaturation = s;
		p = fValue * (1.0 - fSaturation);
		q = fValue * (1.0 - (fSaturation * f));
		t = fValue * (1.0 - (fSaturation * (1.0 - f)));
		switch (i) 
		{
			case 1: 
			{
				r = q; g = fValue; b = p; 
			}
			case 2: 
			{
				r = p; g = fValue; b = t;
			}
			case 3: 
			{
				r = p; g = q; b = fValue;
			}
			case 4:
			{
				r = t; g = p; b = fValue;
			}
			case 5:
			{
				r = fValue; g = p; b = q; 
			}
			default:
			{
				r = fValue; g = t; b = p; 
			}	
		}
	}
}


//////////////////////////////////////////////////////////////////
// Delete entity
//////////////////////////////////////////////////////////////////


public Action:Delete(Handle:timer, any:entity)
{
	if (IsValidEdict(entity)) AcceptEntityInput(entity, "kill");
	if (GetConVarInt(g_SmokecolorMode) == 2) CloseHandle(TimeHandle);
}  


//////////////////////////////////////////////////////////////////
// End Plugin
//////////////////////////////////////////////////////////////////