/////////////////////////////////////////////////////////
// BotFilling version 1.6 by ATAMAH (ICQ: 305-839) //////
/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
// You can edit the lines below as you wish, but ////////
// please save my contact up there. /////////////////////
/////////////////////////////////////////////////////////
//        , //
//       . /////
/////////////////////////////////////////////////////////

block load
{
	es_xset botfilling BotFilling_1.6
	es_xmakepublic botfilling

	es_xset max_players 0
	es_getmaxplayercount max_players

	//       Mani Admin
	//          
	//      
	//Max players count with Mani Admin reserve slots checking
	//In result left minimum one free slot for any user
	es_math max_players - server_var(mani_reserve_slots_number_of_slots)
	es_math max_players - 1

	
	//    (  )
	//Min needed players count (load from config)
	es_xset min_players_count 0
	
	//     ? (  )
	//Randomize bot skills? (load from config)
	es_xset rand_bot_diff 0

	es_mexec ../addons/eventscripts/botfilling/botfilling.cfg

	if (server_var(min_players_count) > server_var(max_players)) do
	{
		es_math min_players_count = server_var(max_players)
	}

	es echo min_players server_var(min_players_count)

	es_xset temp_var 0

	es_xset all_players 0

	//   (    T  CT)
	//Active players count (CT + T count)
	es_xset active_count 0
	
	//    
	//Total humans count
	es_xset humans 0
	//   
	//Total bots count
	es_xset bots 0

	es_xset bot_join_after_player 0

	es bot_add	
}

block unload
{
	es bot_kick all
}

//   -
//Working on connect anybody
event player_connect
{
	//,    
	//Checking that's not bot
	if (event_var(es_steamid) != BOT) do
	{
		getplayercount bots #bot	

		es_getplayercount all_players

		//      
		//If free slots is no more
		if (server_var(all_players) > server_var(max_players)) do
		{	
			//    
			//      ,
			//     unassigned
			//     
			//If bots are exist
			//This may be when map changed and connected humans
			//in unassigned state
			if (server_var(bots) > 0) do
			{
				// ,   
				//Kicking the bot for clear the slot
				getrandplayer temp_var #bot

				if (server_var(temp_var) > 0) do
				{
					es_xsetinfo botname 0	
					es getbotname botname server_var(temp_var)
	
					es bot_kick server_var(botname)
				}
			}
			else do
			{
				//  ,     
				//Else kicking any spectator
				getrandplayer temp_var #spec
			
				if (server_var(temp_var) > 0) do
				{
					es kickid server_var(temp_var) Kicking spectator for clearing slot
				}
			}
		}
	}
}

event player_team
{
	getplayercount bots #bot	

	es_getplayercount active_count 2
	es_getplayercount temp_var 3

	es_mathparse active_count "active_count + temp_var"

	getplayercount humans #human#ct
	getplayercount temp_var #human#t

	es_mathparse humans "humans + temp_var"

	es_getplayercount all_players

	if (server_var(active_count) < server_var(min_players_count)) do
	{
		if (server_var(all_players) < server_var(max_players)) do
		{
			//     ,
			//  
			if (server_var(active_count) < server_var(min_players_count)) do
			{
				//if (server_var(humans) > 0) do
				//{
					if (server_var(rand_bot_diff) > 0) do
					{
						es_xset temp_var 0
						es_rand temp_var 1 3
						es bot_difficulty server_var(temp_var)
					}
					es bot_add
				//}
			}
		}
	}
}

//      
//Adding or removing bots at end of round
event round_end
{
	getplayercount bots #bot	

	es_getplayercount active_count 2
	es_getplayercount temp_var 3

	es_mathparse active_count "active_count + temp_var"

	getplayercount humans #human#ct
	getplayercount temp_var #human#t

	es_mathparse humans "humans + temp_var"

	es_getplayercount all_players

	//    min_players_count
	//If active players greater than min_players_count
	if (server_var(active_count) > server_var(min_players_count)) do
	{
		//   
		//And bots are exist
		if (server_var(bots) > 0) do
		{
			//   
			//      min_players_count,
			//    player_disconnect
			//   
			//Kicking the bot for clear the slot
			//If active players still greater than min_players_count
			//then they will kick on player_disconnect event
			getrandplayer temp_var #bot

			if (server_var(temp_var) > 0) do
			{
				//      
				// 
				//This is only one working algorithm
				//for kicking bots from ES
				es_xsetinfo botname 0	
				es getbotname botname server_var(temp_var)

				es bot_kick server_var(botname)
			}
		}
	}
	else do
	{
		if (server_var(all_players) < server_var(max_players)) do
		{
			//     ,
			//  
			if (server_var(active_count) < server_var(min_players_count)) do
			{
				//if (server_var(humans) > 0) do
				//{
					if (server_var(rand_bot_diff) > 0) do
					{
						es_xset temp_var 0
						es_rand temp_var 1 3
						es bot_difficulty server_var(temp_var)
					}
					es bot_add
				//}
			}
		}
	}
}

//         
//Adding or removing bots on disconnect anybody
event player_disconnect
{
	getplayercount bots #bot	

	es_getplayercount active_count 2
	es_getplayercount temp_var 3

	es_mathparse active_count "active_count + temp_var"

	getplayercount humans #human#ct
	getplayercount temp_var #human#t

	es_mathparse humans "humans + temp_var"

	es_getplayercount all_players

	//    min_players_count
	//If active players greater than min_players_count
	if (server_var(active_count) > server_var(min_players_count)) do
	{
		//   
		//And bots are exist
		if (server_var(bots) > 0) do
		{
			//   
			//      min_players_count,
			//    player_disconnect
			//   
			//Kicking the bot for clear the slot
			//If active players still greater than min_players_count
			//then they will kick on player_disconnect event
			getrandplayer temp_var #bot

			if (server_var(temp_var) > 0) do
			{
				//      
				// 
				//This is only one working algorithm
				//for kicking bots from ES
				es_xsetinfo botname 0	
				es getbotname botname server_var(temp_var)

				es bot_kick server_var(botname)
			}
		}
	}
	else do
	{
		if (server_var(all_players) < server_var(max_players)) do
		{
			//     ,
			//  
			if (server_var(active_count) < server_var(min_players_count)) do
			{
				//if (server_var(humans) > 0) do
				//{
					if (server_var(rand_bot_diff) > 0) do
					{
						es_xset temp_var 0
						es_rand temp_var 1 3
						es bot_difficulty server_var(temp_var)
					}
					es bot_add
				//}
			}
		}
	}
}