/* This plugin contains commands specific for Counterstrike with PodBot2.5 */
 
/* $Id: plugin_PBBalance.sma,v 1.2.3.4 2002/09/17 R.Hoffner */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
 
#define MAX_INFO_LENGTH 32
 
 
new STRING_VERSION[] = "2.50.0";
 
public CheckTeams() 
{
	new iPlayersToAdd;
	new i;
	new iTeam;
	new strName[MAX_DATA_LENGTH];
	new strText[MAX_DATA_LENGTH];
	new iSessionID;
	new iWONID;
	new iDead;
	new iTeamKick;
	new iMaxPlayers = maxplayercount();
	new iTeamDiff =  getteamcount( 1 ) - getteamcount(2);
	new iPlayers  = playercount();
	new sAuthID[MAX_AUTHID_LENGTH];
 
	if( !iPlayers )
		return PLUGIN_CONTINUE;
 
	// not all players on server( maxplayers -2 ) --> BOTs must be added
	if( iPlayers < (iMaxPlayers-2))
	{
		iPlayersToAdd = (iMaxPlayers-iPlayers-2);
		if( iTeamDiff > 0 )
		{
			exec("pb addbot 2");
			if( iPlayersToAdd > 1 )
			{
				set_timer("CheckTeams", 10, 1 );
				return PLUGIN_CONTINUE;
			}
		}
		else if( iTeamDiff < 0 )
		{
			exec("pb addbot 1");
			if( iPlayersToAdd > 1 )
			{
				set_timer("CheckTeams", 10, 1 );
				return PLUGIN_CONTINUE;
			}
 
		}
		else if( iPlayersToAdd > 0 )
		{
 
			exec("pb addbot 1");
			if( iPlayersToAdd > 1 )
			{
				set_timer("CheckTeams", 10, 1 );
				return PLUGIN_CONTINUE;
			}
		}
	}
	// check if theres some team-difference
	// if one team has more players than another, a bot will be kicked
	else if( iTeamDiff != 0 )
	{
		/* teams are uneven, so notify the players */
		snprintf( strText, MAX_DATA_LENGTH, "[Teams Uneven] Team 1: %i Team 2: %i", getteamcount( 1 ), getteamcount( 2 ) );
		say( strText );
 
		if( iTeamDiff > 0 )
		{
			iTeamKick = 1;
		}
		else 
		{
			iTeamKick = 2;
		}
		for( i=1;i<=iMaxPlayers;i++)
		{
 
			if( playerinfo(i,strName,MAX_DATA_LENGTH,iSessionID,iWONID,iTeam,iDead,sAuthID) == 1 
				&& streq(sAuthID,"BOT")==1 
				&& iTeam == iTeamKick )
			{
				snprintf( strText, MAX_DATA_LENGTH, "[Teams Uneven] Player : %s, %i  will be kicked", strName, iTeam );
				say( strText );
				kick(strName);
				return PLUGIN_CONTINUE;
			}
		}
	}
	else
	{
		for( i=1;i<=iMaxPlayers;i++)
		{
			if( playerinfo(i,strName,MAX_DATA_LENGTH,iSessionID,iWONID,iTeam,iDead,sAuthID) == 1 && streq(sAuthID,"BOT")!=1 )
			{
				// Theres an human player --> o.k.
				return PLUGIN_CONTINUE;
			}
		}
		// No human players --> remove all bots
		exec( "pb removebots");
	}
 
 
	return PLUGIN_CONTINUE;
}
 
 
 
public plugin_init() 
{
	plugin_registerinfo("Bot Balance Plugin","Balance Teams with Podbot",STRING_VERSION);
	set_timer( "CheckTeams", 30, 99999 ); // repeat forever
	return PLUGIN_CONTINUE;
}