#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <xs>

/* 
Fake Lampa 
  Improven the Light on the dark map
*/

#define fm_find_ent_by_class(%1,%2) engfunc(EngFunc_FindEntityByString, %1, "classname", %2)

new const FK8888_model[][] = {	"models/FK8888.mdl" }            // model
new const FK8888_classname[] 	= "FK8888"

new g_FKlen;
new fsaytext;
new g_FKet[33] = 0;

new AccessFlag = ADMIN_CVAR

public plugin_init() 
{
	register_plugin("Fake LAMPA", "2.0", "Katakuna")
	
	register_clcmd("FKADD", "CFK8888", -1)
	register_clcmd("FKDEL", "RFK8888", -1)
	register_clcmd("FKSAVE", "SFK8888n", -1)
	/* Meniu More Great Ideea*/
	register_clcmd("say /FKMENU","FKOPEN_MENU",0)
	register_clcmd("say_team /FKMENU","FKOPEN_MENU",0)
	register_clcmd("FKMENU","FKOPEN_MENU",0)
	fsaytext = get_user_msgid("fsaytext")


}

public plugin_precache()
{
	precache_model("models/FK8888.mdl")
}

public plugin_menu(){
	g_FKlen = menu_create("\rFake Lampa Menu","FKMENU_HANDLER");
	
	menu_additem(g_FKlen,"\wFake Lampa \rAdd !","1",0);
	menu_additem(g_FKlen,"\wFake Lampa \rRemove !","2",0);
	menu_additem(g_FKlen,"\wFake Lampa \rSave !","3",0);
}

public FKOPEN_MENU(id){
	if(!is_user_connected(id)) return;
	menu_display(id,g_FKlen,0);
}

public FKMENU_HANDLER(id,menu,item){
	
	if(item == MENU_EXIT) return PLUGIN_HANDLED;

	static data[10],iName[64],iAccess,callback;
	menu_item_getinfo(menu,item,iAccess,data,9,iName,63,callback);
	new key = str_to_num(data);
	new Buffer[128];
	
	switch(key){
		
		case 1: {
			console_cmd(id, "FKADD") /* Add Fake Lampa */			
			formatex(Buffer,127,"^x03Fake Lampa Menu^x01 Fake Lampa Added !")
			g_FKet[id] = 1;
		}
		case 2: {
			console_cmd(id, "FKDEL") /* Remov Fake Lampa */
			formatex(Buffer,127,"^x03Fake Lampa Menu^x01 Fake Lampa Remove !")
			g_FKet[id] = 2;
		}
		case 3: {
			console_cmd(id, "FKSAVE") /* Save Fake Lampa */			
			formatex(Buffer,127,"^x03Fake Lampa Menu^x01 Fake Lampa Saved !")
			g_FKet[id] = 3;
		}
	}
	message_begin(MSG_ONE,fsaytext,_,id)
	write_byte(id);
	write_string(Buffer);
	message_end();
	
	return PLUGIN_HANDLED;
}


public plugin_cfg()
{
	static ConfigsDir[64], File[64], MapName[32]
	
	get_configsdir(ConfigsDir, sizeof ConfigsDir - 1)
	get_mapname(MapName, sizeof MapName - 1)
	
	formatex(File, sizeof File - 1, "%s/%s_FK8888.cfg", ConfigsDir, MapName)

	if(!file_exists(File))
		return 1
	
	static FileOrigin[3][32], FileAngles[3][32], Line, Length, Buffer[256]
	static Temp1[128], Temp2[128]
	static Float:fOrigin[3], Float:fAngles[3]
	
	while(read_file(File, Line++, Buffer, sizeof Buffer - 1, Length))
	{
		if((Buffer[0]==';') || !Length)
			continue
		
		strtok(Buffer, Temp1, sizeof Temp1 - 1, Temp2, sizeof Temp2 - 1, '|', 0)
		
		parse(Temp1, FileOrigin[0], sizeof FileOrigin[] - 1, FileOrigin[1], sizeof FileOrigin[] - 1, FileOrigin[2], sizeof FileOrigin[] - 1)
		
		fOrigin[0] = str_to_float(FileOrigin[0])
		fOrigin[1] = str_to_float(FileOrigin[1])
		fOrigin[2] = str_to_float(FileOrigin[2])
		
		parse(Temp2, FileAngles[0], sizeof FileAngles[] - 1, FileAngles[1], sizeof FileAngles[] - 1, FileAngles[2], sizeof FileAngles[] - 1)
		
		fAngles[0] = str_to_float(FileAngles[0])
		fAngles[1] = str_to_float(FileAngles[1])
		fAngles[2] = str_to_float(FileAngles[2])
		
		static ent
		ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
		
		if(!ent)
			return 0
		
		engfunc(EngFunc_SetOrigin, ent, fOrigin)
		set_pev(ent, pev_angles, fAngles)
		
		engfunc(EngFunc_SetModel, ent, FK8888_model[random_num(0,sizeof FK8888_model - 1)])
		set_pev(ent, pev_classname, FK8888_classname)
		
		dllfunc(DLLFunc_Spawn, ent)
		set_pev(ent, pev_solid, SOLID_BBOX)
		set_pev(ent, pev_takedamage,DAMAGE_NO)
		set_pev(ent, pev_movetype, MOVETYPE_NONE)
		set_pev(ent, pev_effects, (EF_BRIGHTLIGHT))
		engfunc(EngFunc_AllocString, ent ,"func_wall")
		engfunc(EngFunc_SetSize, ent, Float:{ -4.0, -4.0, -4.0 }, Float:{ 4.0, 4.0, 4.0 } );
	}
	return 1
}

public CFK8888(id)
{
	if(!access(id, AccessFlag))
		return 1
	
	static Float:fAimOrigin[3]
	fm_get_aimorigin(id, fAimOrigin)
	
	// create a new entity 
	// set a name to the entity
	if((engfunc(EngFunc_PointContents, fAimOrigin) != CONTENTS_SKY) && (engfunc(EngFunc_PointContents, fAimOrigin) != CONTENTS_SOLID))
	{
		static ent
		ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
		
		if(!ent)
			return 0
		
		engfunc(EngFunc_SetOrigin, ent, fAimOrigin)
		engfunc(EngFunc_SetModel, ent, FK8888_model[random_num(0,sizeof FK8888_model - 1)])
	
		set_pev(ent, pev_classname, FK8888_classname)
	
		dllfunc(DLLFunc_Spawn, ent)
		set_pev(ent, pev_solid, SOLID_BBOX)
		set_pev(ent, pev_takedamage,DAMAGE_NO)
		set_pev(ent, pev_movetype, MOVETYPE_NONE)
		set_pev(ent, pev_effects, (EF_BRIGHTLIGHT))
		engfunc(EngFunc_AllocString, ent ,"func_wall")
		engfunc(EngFunc_SetSize, ent, Float:{ -4.0, -4.0, -4.0 }, Float:{ 4.0, 4.0, 4.0 } );
		engfunc(EngFunc_DropToFloor, ent)
	}
	else
		client_print(id, print_chat, "This location is forbite")
	return 0
}

public RFK8888(id)
{
	if(!access(id, AccessFlag))
		return 1
	
	static ent, iBody
	get_user_aiming(id, ent, iBody)
	
	if(pev_valid(ent))
	{
		static sClassname[32]
		pev(ent, pev_classname, sClassname, sizeof sClassname - 1)
		
		if(!strcmp(sClassname, FK8888_classname, 1))
		{
			engfunc(EngFunc_RemoveEntity, ent)
			
			client_print(id, print_chat, "FK8888 DELETE")
		}
		else
			client_print(id, print_chat, "FK8888 NOT FOUND")
	}
	return 0
}	

public SFK8888n(id)
{
	if(!access(id, AccessFlag))
		return 1

	
	static ConfigsDir[64], File[64], Buffer[256], MapName[32]
	
	get_configsdir(ConfigsDir, sizeof ConfigsDir - 1)
	get_mapname(MapName, sizeof MapName - 1)
	
	formatex(File, sizeof File - 1, "%s/%s_FK8888.cfg", ConfigsDir, MapName)
	
	if(file_exists(File))
		delete_file(File)
	
	new ent = -1, Float:fEntOrigin[3], Float:fEntAngles[3]
	
	while((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", FK8888_classname)))
	{
		pev(ent, pev_origin, fEntOrigin)
		pev(ent, pev_angles, fEntAngles)
		
		formatex(Buffer, sizeof Buffer - 1, "%f %f %f | %f %f %f", fEntOrigin[0], fEntOrigin[1], fEntOrigin[2], fEntAngles[0], fEntAngles[1], fEntAngles[2])
		
		write_file(File, Buffer, -1)
	}
	client_print(id, print_chat, "FK8888 SAVED %s", MapName)
	
	return 0
}


/* credited to VEN - fakemeta_util.inc */
stock fm_get_aimorigin(index, Float:origin[3])
{
    new Float:start[3], Float:view_ofs[3];
    pev(index, pev_origin, start);
    pev(index, pev_view_ofs, view_ofs);
    xs_vec_add(start, view_ofs, start);
    
    new Float:dest[3];
    pev(index, pev_v_angle, dest);
    engfunc(EngFunc_MakeVectors, dest);
    global_get(glb_v_forward, dest);
    xs_vec_mul_scalar(dest, 9999.0, dest);
    xs_vec_add(start, dest, dest);
    
    engfunc(EngFunc_TraceLine, start, dest, 0, index, 0);
    get_tr2(0, TR_vecEndPos, origin);
    
    return 1;
}  
