#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#include <colors>
#tryinclude <shop>

public Plugin:myinfo = 
{
	name = "Happy New Year 2k15",
	author = "Danyas, original plugin by Impulse",
	description = "Recoded version of New Year Tree (http://hlmod.ru/forum/novye-plaginy/14463-new-year-tree-2-2-a.html)\n - Fixed all errors \n - Removed a lot useless globals \n - Removed bugged bear \n - Removed HP regen",
	version = "3.1",
	url = "http://hlmod.ru/forum/showthread.php?p=226775"
}

new
		g_iAccount,
		active,
Handle:	file	[70],
String:	answ	[64],
		snow	[64];


public OnPluginStart()
{
	HookEvent("player_death", Death);
	HookEvent("round_start", Start);
	g_iAccount = FindSendPropOffs("CCSPlayer", "m_iAccount");
	RegAdminCmd("tree", CreateTree, ADMFLAG_ROOT);
	RegAdminCmd("del", DeleteTree, ADMFLAG_ROOT);
	RegAdminCmd("snowman", CreateSnowman, ADMFLAG_ROOT);
	RegAdminCmd("large", CreateLarge, ADMFLAG_ROOT);
	RegConsoleCmd("snow", Snow);
	RegConsoleCmd("sm_snow", Snow);
	RegAdminCmd("sm_gift",GiftMe, ADMFLAG_ROOT);
	CreateTimer(60.0, Question, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
	HookEvent("player_say", say);
}

public OnConfigsExecuted()
{	
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.dx80.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.dx90.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.mdl");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.phy");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.sw.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowball_16.vvd");
	AddFileToDownloadsTable("materials/models/logandougall/cel/snow.vmt");
	AddFileToDownloadsTable("materials/models/logandougall/cel/snow.vtf");

	
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.dx80.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.dx90.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.mdl");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.phy");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.sw.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.vvd");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree.xbox.vtx");

	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.dx80.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.dx90.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.mdl");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.phy");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.sw.vtx");
	AddFileToDownloadsTable("models/logandougall/cel/cel_xmas_snowman.vvd");
	AddFileToDownloadsTable("materials/models/logandougall/cel/carrot.vtf");
	AddFileToDownloadsTable("materials/models/logandougall/cel/carrot.vmt");
	AddFileToDownloadsTable("materials/models/logandougall/cel/toolsblack.vmt");
	AddFileToDownloadsTable("materials/models/logandougall/cel/bark.vmt");
	AddFileToDownloadsTable("materials/models/logandougall/cel/bark.vtf");
	AddFileToDownloadsTable("materials/models/logandougall/cel/snow.vmt");
	AddFileToDownloadsTable("materials/models/logandougall/cel/snow.vtf");

	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.dx80.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.dx90.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.mdl");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.phy");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.sw.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.vvd");
	AddFileToDownloadsTable("models/models_kit/xmas/giftbox_mini_quad.xbox.vtx");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/giftbox.vmt");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/giftbox.vtf");

	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.dx80.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.dx90.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.mdl");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.phy");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.sw.vtx");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.vvd");
	AddFileToDownloadsTable("models/models_kit/xmas/xmastree_mini.xbox.vtx");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscA.vmt");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscA.vtf");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscA_skin2.vmt");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscA_skin2.vtf");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscB.vmt");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscB.vtf");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscB_skin2.vmt");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscB_skin2.vtf");
	AddFileToDownloadsTable("materials/models/models_kit/xmas/xmastree_miscB_spec.vtf");
}

public OnMapStart()
{		
	PrecacheModel("sprites/glow01.spr",true); 
	PrecacheModel("models/models_kit/xmas/giftbox_mini_quad.mdl",true); 
	PrecacheModel("models/models_kit/xmas/xmastree_mini.mdl",true); 
	PrecacheModel("models/logandougall/cel/cel_xmas_snowman.mdl",true); 
	PrecacheModel("models/models_kit/xmas/xmas_teddybear.mdl",true); 
	PrecacheModel("models/models_kit/xmas/xmastree.mdl",true); 
	PrecacheModel("models/logandougall/cel/cel_xmas_snowball_16.mdl",true); 
}

public Action:Gift(Handle:timer, any:i)
{
	if (!IsClientInGame(i))	return Plugin_Handled;	
	new iEntity = CreateEntityByName("prop_dynamic");
	if (iEntity)
    {
		decl Float:fPos[3], String:clr[20];
		new String:sTargetName[32],
			String:sBufer[70],
			Float:fTime = 30.0;
			
		GetClientAbsOrigin(i, fPos); fPos[0] += 50.0;
		if (!IsPlayerAlive(i))		fPos[2] -= 40.0;
		Format(sTargetName, sizeof(sTargetName), "xmasgift_%i", iEntity);
		DispatchKeyValueVector(iEntity, "origin", fPos);
		DispatchKeyValue(iEntity, "solid", "6");
		DispatchKeyValue(iEntity, "model", "models/models_kit/xmas/giftbox_mini_quad.mdl");
		DispatchKeyValue(iEntity, "targetname", sTargetName);
		SetVariantString(sTargetName);
		DispatchSpawn(iEntity);
		SetEntProp(iEntity, Prop_Send, "m_usSolidFlags", 8);
		SetEntProp(iEntity, Prop_Send, "m_CollisionGroup", 1);
		Format(sBufer, sizeof(sBufer), "OnUser1 !self:kill::%0.2f:-1", fTime);
		SetVariantString(sBufer);
		AcceptEntityInput(iEntity, "AddOutput");
		AcceptEntityInput(iEntity, "FireUser1");

        
		new iRotating = CreateEntityByName("func_rotating", -1);
		DispatchKeyValueVector(iRotating, "origin", fPos);
		DispatchKeyValue(iRotating, "targetname", sTargetName);
		DispatchKeyValue(iRotating, "maxspeed", "180");
		DispatchKeyValue(iRotating, "friction", "0");
		DispatchKeyValue(iRotating, "dmg", "0");
		DispatchKeyValue(iRotating, "solid", "0");
		DispatchKeyValue(iRotating, "spawnflags", "64");
		DispatchSpawn(iRotating);
		SetVariantString("!activator");
		AcceptEntityInput(iEntity, "SetParent", iRotating, iRotating);
		AcceptEntityInput(iRotating, "Start", -1, -1);
		SetEntProp(iRotating, Prop_Send, "m_CollisionGroup", 1);
		Format(sBufer, sizeof(sBufer), "OnUser1 !self:kill::%0.2f:-1", fTime);
		SetVariantString(sBufer);
		AcceptEntityInput(iRotating, "AddOutput");
		AcceptEntityInput(iRotating, "FireUser1");
		SetEntPropEnt(iEntity, Prop_Send, "m_hEffectEntity", iRotating);
		
		Format(clr, sizeof(clr), "%i %i %i", GetRandomInt(0,255),GetRandomInt(0,255),GetRandomInt(0,255));
		new index5 = CreateEntityByName("env_sprite");
		DispatchKeyValue(index5, "rendermode", "5");
		DispatchKeyValue(index5, "rendercolor", String:clr);
		DispatchKeyValue(index5, "renderamt", "255");
		DispatchKeyValue(index5, "scale", "1");
		DispatchKeyValue(index5, "model", "sprites/glow01.spr");
		DispatchKeyValueVector(index5, "origin", fPos);
		DispatchSpawn(index5);
		SetVariantString("!activator");
		AcceptEntityInput(index5, "SetParent", iEntity); 


		SDKHook(iEntity, SDKHook_StartTouch, Hook_GiftStartTouch);
	}
	return Plugin_Stop;
}

public Hook_GiftStartTouch(iEntity, iClient)
{
	if (iClient > 0 && iClient <= MaxClients)
	{
		OnBreak_Func(iClient);
		DelEnti(iEntity);
	}
}

DelEnti(iEnti)
{
	if(!IsValidEdict(iEnti))	return;
	new iRotating = GetEntPropEnt(iEnti, Prop_Send, "m_hEffectEntity");
	if(iRotating && IsValidEdict(iRotating))
		AcceptEntityInput(iRotating, "Kill");
	AcceptEntityInput(iEnti, "Kill");
}


public Action:GiftMe(client, args){CreateTimer(0.1, Gift, client);}

public OnBreak_Func(activator) 
{ 
	if (activator > 0 && activator <= MaxClients) 
	{
		switch (GetRandomInt(1,9))
		{
			case 1:
			{
				if(GetRandomInt(0,1))
				{
					new randommoney = GetRandomInt(0, 5000);
					new fMoney = (GetMoney(activator) + randommoney);	
					SetMoney(activator, fMoney);
					PrintToChat(activator,"\x04[Gift New Year]\x01 В подарке оказалась сумма, размером в \x03%i$", randommoney);
				}
				else 
				{
					new randommoney = GetRandomInt(0, 500);
					new fMoney = (GetMoney(activator) - randommoney);	
					SetMoney(activator, fMoney);
					PrintToChat(activator,"\x04[Gift New Year]\x01 В подарке оказался гопник который отжал \x03%i$", randommoney);
				}
			}
			
			case 2:
			{
				if(GetRandomInt(0,1))
				{
					SetEntProp(activator, Prop_Data, "m_iFrags", GetClientFrags(activator) + 2);
					PrintToChat(activator,"\x04[Gift New Year]\x01 Подарок прибавил к счету \x032\x01 фрага.");
				}
				else
				{
				SetEntProp(activator, Prop_Data, "m_iDeaths", GetClientDeaths(activator) - 2);
				PrintToChat(activator,"\x04[Gift New Year]\x01 Подарок убрал из счета \x032\x01 смерти.");
				}
			}
			
			case 3:
			{
				switch (GetRandomInt(0,2))
				{
					case 0: {SetEntProp(activator, Prop_Send, "m_iHealth", GetClientHealth(activator) + GetRandomInt(10,20)); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03средняя аптечка");}
					case 1: {SetEntProp(activator, Prop_Send, "m_iHealth", GetClientHealth(activator) + GetRandomInt(25,50)); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03большая аптечка");}
					default: {SetEntProp(activator, Prop_Send, "m_iHealth", GetClientHealth(activator) + GetRandomInt(5,10)); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03маленькая аптечка");}
				}
			}
			
			case 4:
			{
				switch (GetRandomInt(0,2))
				{
					case 1: {GivePlayerItem(activator, "weapon_flashbang"); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03ослепительная граната");}
					case 2: {GivePlayerItem(activator, "weapon_hegrenade"); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03взрываная граната");}
					default: {GivePlayerItem(activator, "weapon_smokegrenade"); PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась \x03дымовая граната");}
				}
				
			}
			
			case 5:
			{	
				new Float:randomspeed = GetRandomFloat(0.9, 1.2);
				SetEntPropFloat(activator, Prop_Data, "m_flLaggedMovementValue", GetEntPropFloat(activator, Prop_Send, "m_flLaggedMovementValue") * randomspeed);
				PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася напиток, который установил вашу скорость на \x03%.2f%%", 100.0*randomspeed);
			}
			
			#if defined _shop_included
			case 6:
			{
				new shopwin = GetRandomInt(0,100);
				
				if (shopwin <= 10)
					PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася порванный лотерейный билет с выигрышем  \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, GetRandomInt(1,5), CREDITS_BY_NATIVE));
					
				else if (shopwin <= 50)
					PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася дешевый лотерейный билет с выигрышем  \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, GetRandomInt(5,25), CREDITS_BY_NATIVE));

				else if (shopwin <= 70)
					PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася лотерейный билет с выигрышем  \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, GetRandomInt(25,75), CREDITS_BY_NATIVE));
				
				else if (shopwin <= 85)
					PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася дорогой лотерейный билет с выигрышем  \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, GetRandomInt(75,150), CREDITS_BY_NATIVE));
				
				else if (shopwin <= 99)
					PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася \x03VIP\x01 лотерейный билет с выигрышем  \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, GetRandomInt(150,250), CREDITS_BY_NATIVE));

				else 
				{
					switch (GetRandomInt(0,3))
					{
						case 1: {PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася лотерейный билет с \x03ДЖЕКПОТОМ\x01 размеров в \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, 250, CREDITS_BY_NATIVE));}
						case 2: {PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася лотерейный билет с \x03ДЖЕКПОТОМ\x01 размеров в \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, 500, CREDITS_BY_NATIVE));}
						case 3: {PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалася лотерейный билет с \x03ДЖЕКПОТОМ\x01 размеров в \x03%i\x01 кр! ", Shop_GiveClientCredits(activator, 1000, CREDITS_BY_NATIVE));}
						default: PrintToChat(activator,"\x04[Gift New Year]\x01 В Подарке оказалась испорченый лотерейный билет с \x03ДЖЕКПОТОМ\x01.");
					}
				}
			}
			#endif
			
			default: {PrintToChat(activator,"\x04[Gift New Year]\x01 К сожалению, подарок был пуст.");}
		}
	}
}

public Start(Handle:event, const String:name[], bool:dontBroadcast)
{

	decl String:MapName[30]; new q = 0, e = 0, el = 0;
	GetCurrentMap(String:MapName, sizeof(MapName));
	Format(String:file, sizeof(file), "cfg/newyear/%s.cfg", MapName);
	new Handle:kv = CreateKeyValues("tree");
	if (FileToKeyValues(kv, String:file) && KvGotoFirstSubKey(kv)) 
	{ 
		decl String:KeyName[50]; 
		do 
		{ 
			if (KvGetSectionName(kv, KeyName, 50)) 
			{
				decl Float:g_fOrigin[3], Float:g_fAngles[3], String:type[20];
				KvGetVector(kv, "1", g_fOrigin);
				KvGetVector(kv, "2", g_fAngles);
				KvGetString(Handle:kv, "type", String:type, sizeof(type));
				if (StrEqual(String:type, "tree") || StrEqual(String:type, ""))
				{
					SpawnTree(g_fOrigin, g_fAngles);
					Light(g_fOrigin, g_fAngles);
					el++;
				}
				if (StrEqual(String:type, "snowman"))
				{
					SpawnSnowMan(g_fOrigin, g_fAngles);
					q++;
				}
				if (StrEqual(String:type, "large"))
				{
					SpawnLarge(g_fOrigin, g_fAngles);
					e++;
				}
			}
         }
         while (KvGotoNextKey(kv));
	}
	PrintToServer("-----------------------");
	PrintToServer("Spawned %i tress on map", el);
	PrintToServer("Spawned %i snowmans on map", q);
	PrintToServer("Spawned %i large tress on map", e);
	PrintToServer("-----------------------");
	CloseHandle(kv);
}

public Action:CreateTree(client, args)
{
	decl Float:g_fOrigin[3], Float:g_fAngles[3];
	GetClientEyePosition(client, g_fOrigin);
	GetClientEyeAngles(client, g_fAngles);	
	TR_TraceRayFilter(g_fOrigin, g_fAngles, MASK_SOLID, RayType_Infinite, Trace_FilterPlayers, client);
	PrintToServer("CreateTree called");
	if(TR_DidHit(INVALID_HANDLE))
	{
		PrintToServer("CreateTree_Hit = Invalid Handle");
		TR_GetEndPosition(g_fOrigin, INVALID_HANDLE);
		TR_GetPlaneNormal(INVALID_HANDLE, g_fAngles);
		GetVectorAngles(g_fAngles, g_fAngles);
		g_fAngles[0] += 90.0;
		SpawnTree(g_fOrigin, g_fAngles);
		Add(g_fOrigin, g_fAngles, "tree");
	}
	
}

public Action:CreateLarge(client, args)
{
	decl Float:g_fOrigin[3], Float:g_fAngles[3];
	GetClientEyePosition(client, g_fOrigin);
	GetClientEyeAngles(client, g_fAngles);	
	TR_TraceRayFilter(g_fOrigin, g_fAngles, MASK_SOLID, RayType_Infinite, Trace_FilterPlayers, client);
	if(TR_DidHit(INVALID_HANDLE))
	{
		TR_GetEndPosition(g_fOrigin, INVALID_HANDLE);
		TR_GetPlaneNormal(INVALID_HANDLE, g_fAngles);
		GetVectorAngles(g_fAngles, g_fAngles);
		g_fAngles[0] += 90.0;
		SpawnLarge(g_fOrigin, g_fAngles);
		Add(g_fOrigin, g_fAngles, "large");
	}
}

public Action:CreateSnowman(client, args)
{
	decl Float:g_fOrigin[3], Float:g_fAngles[3];
	GetClientEyePosition(client, g_fOrigin);
	GetClientEyeAngles(client, g_fAngles);	
	TR_TraceRayFilter(g_fOrigin, g_fAngles, MASK_SOLID, RayType_Infinite, Trace_FilterPlayers, client);
	if(TR_DidHit(INVALID_HANDLE))
	{
		TR_GetEndPosition(g_fOrigin, INVALID_HANDLE);
		TR_GetPlaneNormal(INVALID_HANDLE, g_fAngles);
		GetVectorAngles(g_fAngles, g_fAngles);
		g_fAngles[0] += 90.0;
		SpawnSnowMan(g_fOrigin, g_fAngles);
		Add(g_fOrigin, g_fAngles, "snowman");
	}
}

public bool:Trace_FilterPlayers(entity, contentsMask, any:data){
	if(entity != data && entity > MaxClients) 
		return true;
	return false;
}


SpawnTree(const Float:g_fOrigin[3], const Float:g_fAngles[3])
{
	PrintToServer("CreateTree: Spawn");
	new index = CreateEntityByName("prop_dynamic");
	DispatchKeyValue(index, "model", "models/models_kit/xmas/xmastree_mini.mdl");
	DispatchKeyValue(index, "Solid", "6");
	DispatchSpawn(index);
	TeleportEntity(index, g_fOrigin, g_fAngles, NULL_VECTOR);
	SetEntityMoveType(index, MOVETYPE_VPHYSICS);
}

SpawnLarge(const Float:g_fOrigin[3], const Float:g_fAngles[3])
{
	new index = CreateEntityByName("prop_dynamic_override");
	if (index == -1) return;
	DispatchKeyValue(index, "model", "models/models_kit/xmas/xmastree.mdl");
	DispatchKeyValue(index, "Solid", "6");
	DispatchSpawn(index);
	TeleportEntity(index, g_fOrigin, g_fAngles, NULL_VECTOR);
	SetEntityMoveType(index, MOVETYPE_VPHYSICS);
}

SpawnSnowMan(const Float:g_fOrigin[3], const Float:g_fAngles[3])
{
	new index = CreateEntityByName("prop_dynamic");
	DispatchKeyValue(index, "model", "models/logandougall/cel/cel_xmas_snowman.mdl");
	DispatchKeyValue(index, "Solid", "6");
	DispatchSpawn(index);
	TeleportEntity(index, g_fOrigin, g_fAngles, NULL_VECTOR);
	SetEntityMoveType(index, MOVETYPE_VPHYSICS);
}

public Add(const Float:g_fOrigin[3], const Float:g_fAngles[3],String:type[15])
{	
	decl String:info[60];
	Format(info, sizeof(info), "%f,%f",g_fOrigin[0], g_fOrigin[1]);
	new Handle:kv = CreateKeyValues("tree");
	FileToKeyValues(kv, String:file);
	KvJumpToKey(kv, info, true); 
	KvSetVector(kv, "1", g_fOrigin); 
	KvSetVector(kv, "2", g_fAngles);
	KvSetString(Handle:kv, "type", String:type);
	KvRewind(kv);
	KeyValuesToFile(kv, String:file);
	CloseHandle(kv);
}

public Action:DeleteTree(client, args)
{
	new index = GetClientAimTarget(client, false);
	if (index  == -1)	return Plugin_Handled;
	
	decl Float:vec[3]; decl String:coord[15]; decl String:coord1[15], String:buf3[2][25], String:buf4[2][25]; 
	GetEntPropVector(index, Prop_Data, "m_vecAbsOrigin", vec);
	FloatToString(vec[0], coord, sizeof(coord));
	FloatToString(vec[1], coord1, sizeof(coord1));
	ExplodeString(String:coord, ".", String:buf3, 2, 25);
	ExplodeString(String:coord1, ".", String:buf4, 2, 25);
	
	new Handle:kv = CreateKeyValues("tree");
	if (FileToKeyValues(kv, String:file) && KvGotoFirstSubKey(kv)) 
	{ 
		decl String:KeyName[35]; 
		do 
		{ 
			if (KvGetSectionName(kv, KeyName, 35)) 
			{ 
				decl String:buf[2][25],String:buf1[2][25],String:buf2[2][25];
				ExplodeString(String:KeyName, ",", String:buf, 2, 25);
				ExplodeString(String:buf[0], ".", String:buf1, 2, 25);
				ExplodeString(String:buf[1], ".", String:buf2, 2, 25);
				if (StrEqual(buf1[0], buf3[0]) && StrEqual(buf2[0], buf4[0]))
				{
					AcceptEntityInput(index, "Kill");
					KvDeleteThis(kv); 
					KvRewind(kv); 
					KeyValuesToFile(kv, String:file);
				}
			}
         } 
         while (KvGotoNextKey(kv));
	
	}
	CloseHandle(kv);
	return Plugin_Continue;
}

public Action:Snow(client, args)
{

	if (GetClientTeam(client) > 1 && IsPlayerAlive(client))
	{
		if (snow[client] == 0)	Attack(client);
		else PrintToChat(client, "У вас закончились снежки.");
	}
	else PrintToChat(client, "Вы должны быть живы и не находится в наблюдателях");
}

Attack(client)
{
	snow[client] = 1;
	CreateTimer(10.0, Sn, client);
	decl Float:clienteyeangle[3], Float:anglevector[3], Float:clienteyeposition[3], Float:resultposition[3], entity;
	GetClientEyeAngles(client, clienteyeangle);
	GetClientEyePosition(client, clienteyeposition);
	GetAngleVectors(clienteyeangle, anglevector, NULL_VECTOR, NULL_VECTOR);
	NormalizeVector(anglevector, anglevector);
	AddVectors(clienteyeposition, anglevector, resultposition);
	NormalizeVector(anglevector, anglevector);
	ScaleVector(anglevector, 800.0);
	entity = CreateEntityByName("hegrenade_projectile");
	SetEntityMoveType(entity, MOVETYPE_FLY);
	DispatchSpawn(entity);
	SetEntityModel(entity, "models/logandougall/cel/cel_xmas_snowball_16.mdl");
	TeleportEntity(entity, resultposition, clienteyeangle, anglevector);
	CreateTimer(3.0, Go, entity);
}

Light(Float:g_fOrigin[3], Float:g_fAngles[3])
{
	decl w_color[4]; decl color[20],Float:Ori[3];
	g_fOrigin[2] += 10;
	w_color[3] = 255;
	
	for (new i = 1; i<= 10; i++)
	{
		for (new q = 0; q < 3; q++) w_color[q] = GetRandomInt(10, 255);
		for (new a = 0; a < 3; a++) Ori[a] = g_fOrigin[a]
		
		Format(String:color, sizeof(color), "%i %i %i", w_color[0], w_color[1], w_color[2]);
		
		new index = CreateEntityByName("env_sprite");
		DispatchKeyValue(index, "rendermode", "5");
		DispatchKeyValue(index, "rendercolor", String:color);
		DispatchKeyValue(index, "renderamt", "255");
		DispatchKeyValue(index, "scale", "0.6");
		DispatchKeyValue(index, "model", "sprites/glow01.spr");
		DispatchSpawn(index);
		AcceptEntityInput(index, "ShowSprite");
		new Float:crd;
		switch(i)
		{
			case 1: crd = 27.0;
			case 2: crd = -27.0;
			case 3: crd = 22.0;
			case 4: crd = -22.0;
			case 5: crd = 17.0;
			case 6: crd = -17.0;
			case 7: crd = 12.0;
			case 8: crd = -12.0;
			case 9: crd = 7.0;
			case 10: crd = -7.0;
		}
		
		Ori[2] += 10*i;
		Ori[0] += crd; 
		Ori[1] +=  crd;
		TeleportEntity(index, Ori, g_fAngles, NULL_VECTOR);
	}
}

public Action:Go2(Handle:timer)
{
	new Handle:kv = CreateKeyValues("tree");
	if (FileToKeyValues(kv, String:file) && KvGotoFirstSubKey(kv)) 
	{ 
		decl String:KeyName[50]; 
		do 
		{ 
			if (KvGetSectionName(kv, KeyName, 50)) 
			{
				decl Float:g_fOrigin[3], Float:g_fAngles[3],String:type[20];
				KvGetVector(kv, "1", g_fOrigin);
				KvGetVector(kv, "2", g_fAngles);
				KvGetString(Handle:kv, "type", String:type, sizeof(type));
				if (StrEqual(String:type, "tree") || StrEqual(String:type, ""))	Light(g_fOrigin, g_fAngles);
			}
		}
		while (KvGotoNextKey(kv));
	}
	CloseHandle(kv);
	return Plugin_Continue;
}

public GetMoney(Client)
{
	if(g_iAccount != -1)
	{
		return GetEntData(Client, g_iAccount);
	}
	return 0;
}


public Action:Question(Handle:timer)
{
	new a = GetRandomInt(50,250);
	new b = GetRandomInt(1,50);
	new op = GetRandomInt(0,1);
	active = 1;
	if (op)
	{
		new sum = a + b;
		Format(String:answ, sizeof(answ), "%i", sum);
		CPrintToChatAll("{red}Дед мороз {default}: Хо-Хо-Хо! Сколько будет %i прибавить %i?", a , b);
	}
	else
	{
		new sum = a - b;
		Format(String:answ, sizeof(answ), "%i", sum);
		CPrintToChatAll("{red}Дед мороз {default}: Хо-Хо-Хо! Сколько будет %i отнять %i?", a , b);
	}
	
	return Plugin_Continue;
}

public say(Handle:event, const String:name[], bool:dontBroadcast)
{
	CreateTimer(15.0, unhook);
	new String:s[20];
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	GetEventString(event, "text", s, sizeof(s));
	if (StrEqual(String:s, String:answ) && active == 1)
	{
		CreateTimer(1.0, Gift, client);
		CPrintToChat(client, "{blue}Вы первым успешно ответили на вопрос! Подарок около Вас!");
		active = 0;
		answ = "";
	}
}
		
public Action:Go(Handle:timer, any:val)	{if (IsValidEntity(val))AcceptEntityInput(val, "Kill");}
public Action:unhook(Handle:timer)		{active = 0;}
public Action:Sn(Handle:timer, any:val)	{snow[val] = 0;}
public SetMoney(Client, amount){if(g_iAccount != -1){SetEntData(Client, g_iAccount, amount);}}
public Death(Handle:e, const String:nse[], bool:db){new i = GetClientOfUserId(GetEventInt(e, "userid"));if (IsClientInGame(i))CreateTimer(2.0, Gift, i);}