/* Plugin generated by AMXX-Studio */
/*			           */
/*	     WebSiteBot	           */
/*	      By Parker;           */
/*				   */
/*	CvarS;			   */
/* bw_botname "www.YourWebSite.com */
/* bw_kickbot "4"		   */			
/* bw_enable   "1"                  */
/*---------------------------------*/
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <xs>

#define PLUGIN "WebSiteBot"
#define VERSION "1.0"
#define AUTHOR "Parker;"

#define OFFSET_LINUX 5


new bw_botname
new bw_botnumber
new cvar_enable
new g_oldbotname[32]
new g_oldbotid
new g_teaminfo

enum (+= 1000)
{
	TASK_ID_BOTS
};

enum CS_Internal_Models 
{
	CS_DONTCHANGE = 0
}

enum CsTeams 
{
	CS_TEAM_UNASSIGNED = 0,
	CS_TEAM_T = 1,
	CS_TEAM_CT = 2,
	CS_TEAM_SPECTATOR = 3
};

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	bw_botname = register_cvar("bw_botname", "www.ffclan.webaltera.ru")
	bw_botnumber = register_cvar("bw_kickbot", "10")
	cvar_enable = register_cvar("bw_enable", "1")
	set_task(5.0, "Createbot", TASK_ID_BOTS, _, _, "b")
	g_teaminfo = get_user_msgid("TeamInfo")
	
}
public Createbot()
{
	if( get_pcvar_num(cvar_enable) != 1 )
		return;
		
	new botname[32]
	get_pcvar_string(bw_botname, botname, 31)
	
	new id = find_player("ia", botname)
	
	new playersn = get_playersnum()

	if( !equali(botname, g_oldbotname) )
	{
		if( is_user_connected(g_oldbotid) && is_user_bot(g_oldbotid) )
		{
			set_user_info(g_oldbotid, "name", botname)
			return;
		}
	}
	
	
	if( playersn < get_pcvar_num(bw_botnumber) && !id )
	{
		g_oldbotid = id = engfunc(EngFunc_CreateFakeClient, botname)
		
		g_oldbotname = botname
		
		if( pev_valid( id ) )
		{
			engfunc( EngFunc_FreeEntPrivateData, id)
			dllfunc( MetaFunc_CallGameEntity, "player", id)
			set_user_info( id, "rate", "3500" )
			set_user_info( id, "cl_updaterate", "25" )
			set_user_info( id, "cl_lw", "1" )
			set_user_info( id, "cl_lc", "1" )
			set_user_info( id, "cl_dlmax", "128" )
			set_user_info( id, "cl_righthand", "1" )
			set_user_info( id, "_vgui_menus", "0" )
			set_user_info( id, "_ah", "0" )
			set_user_info( id, "dm", "0" )
			set_user_info( id, "tracker", "0" )
			set_user_info( id, "friends", "0" )
			set_user_info( id, "*bot", "1" )
			set_pev( id, pev_flags, pev( id, pev_flags ) | FL_FAKECLIENT )
			set_pev( id, pev_colormap, id )
			
			new msg[128]
			dllfunc( DLLFunc_ClientConnect, id, botname, "127.0.0.1", msg )
			dllfunc( DLLFunc_ClientPutInServer, id )
			engfunc( EngFunc_RunPlayerMove, id, Float:{0.0,0.0,0.0}, 0.0, 0.0, 0.0, 0, 0, 76 )
			
			fm_set_user_team(id, CS_TEAM_CT);
			dllfunc(DLLFunc_Spawn, id)
		}
	}
	
	else if( playersn > get_pcvar_num(bw_botnumber) && id )
	{
		set_pev(id, pev_effects, pev(id, pev_effects) & ~EF_NODRAW)
		server_cmd( "kick #%d", get_user_userid(id) )
	}
	
	else if( id )
	{
		if( !is_user_alive(id) )
			dllfunc(DLLFunc_Spawn, id)
		
		set_pev(id, pev_effects, pev(id, pev_effects) & EF_NODRAW)
		set_pev(id, pev_solid, SOLID_NOT)
		set_pev(id, pev_takedamage, DAMAGE_NO)
		
		fix_score_team(id,"SPECTATOR")
		
	}
}

stock bool:bw_botis_valid(id)
{	
	return ( is_user_connected(g_oldbotid) && is_user_bot(g_oldbotid) && id == g_oldbotid )
}
stock fm_set_user_team(id, {CsTeams,_}:team, {CS_Internal_Models,_}:model = CS_DONTCHANGE)
{
	if( !is_user_connected(id) )
		return 0;
	
	set_pdata_int(id, 114, _:team, OFFSET_LINUX);

	if(model)
	{
		set_pdata_int( id, 126, _:model, OFFSET_LINUX);
	}

	dllfunc( DLLFunc_ClientUserInfoChanged, id, engfunc( EngFunc_GetInfoKeyBuffer, id ) );

	static const teams[] = { "UNASSIGNED", "TERRORIST", "CT", "SPECTATOR" };

	emessage_begin(MSG_BROADCAST, g_teaminfo);
	ewrite_byte(id);
	ewrite_string(teams[_:team]);
	emessage_end();
    
	return 1;

}

stock fix_score_team(id, const TEAM[])
{
	emessage_begin(MSG_BROADCAST, g_teaminfo);
	ewrite_byte(id);
	ewrite_string(TEAM);
	emessage_end();
}
stock bw_make_cvarexec(const config[])
{
	new f = fopen(config, "wt");
	new stringscvars[192]
	
	fprintf(f, "// Bot WebSite^n")
	fprintf(f, "// -_-_-_-_-_-_-^n")
	get_pcvar_string(bw_botname, stringscvars, 191)
	fprintf(f, "bw_botname ^"%s^" // Bot name, Default( ^"www.YourWebSite.com.ar^" )^n", stringscvars)
	fprintf(f, "bw_kickbot %i // Number of Player to Kick the Bot^n", get_pcvar_num(bw_botnumber))
	fprintf(f, "^n")
	
	fclose(f);
	
	server_cmd("exec %s", config)
	server_exec()
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang11274\\ f0\\ fs16 \n\\ par }
*/
