#include <core> 
#include <console> 
#include <string> 
#include <admin> 
#include <adminlib>
#include <plugin>
 
#define TERROR 1
#define CT 2
 
new STRING_VERSION[MAX_DATA_LENGTH] = "1.0";
 
/* Überprüfung, ob vault.ini existiert */
is_vaultfile_set(){
	new vault[MAX_DATA_LENGTH];
	getstrvar("admin_vault_file",vault,MAX_DATA_LENGTH);
	if(streq(vault,"0") || strlen(vault)==0){
		if(getvar("admin_debug")>0){
			log("[BK_BM] No vault file defined.");
		}
		return 0;
	}
	return 1;
}
 
public plugin_info(HLOldName, HLNewName, UserIndex){
	new Name[MAX_NAME_LENGTH];
	new UserID,WONID,Team,Dead;
	new sAuthID[MAX_AUTHID_LENGTH];
 
	if(UserIndex>0){
		playerinfo(UserIndex,Name,MAX_NAME_LENGTH,UserID,WONID,Team,Dead,sAuthID);
	}
	else{
		return PLUGIN_CONTINUE;
	}
 
	new sHLOldName[MAX_NAME_LENGTH];
	new sHLNewName[MAX_NAME_LENGTH];
	convert_string(HLOldName,sHLOldName,MAX_NAME_LENGTH);
	convert_string(HLNewName,sHLNewName,MAX_NAME_LENGTH);
	if(!streq(sHLOldName,sHLNewName)){
		return PLUGIN_CONTINUE;
	}
 
	new TBot=0;
	new CTBot=0;
	checkbot(TBot,CTBot);
	kickbot(TBot,CTBot);
 
	return PLUGIN_CONTINUE;
}
 
checkbot(&TBot,&CTBot){
	new Name[MAX_NAME_LENGTH];
	new UserID,WONID,Team,Dead;
	new sAuthID[MAX_AUTHID_LENGTH];
	new i;
	new maxplayers=maxplayercount();
 
	for(i=1;i<=maxplayers;i++){
		if(playerinfo(i,Name,MAX_NAME_LENGTH,UserID,WONID,Team,Dead,sAuthID) && streq(sAuthID,"BOT")){
			if(Team==1){
				TBot++;
			}
			else{
				CTBot++;
			}
		}
	}
 
	return PLUGIN_CONTINUE;
}
 
kickbot(TBot,CTBot){
	new iTplayers=getteamcount(TERROR);
	new iCTplayers=getteamcount(CT);
	new iBots;
 
	if(get_vaultnumdata("BK_BM_BOTS",iBots) && iBots<playercount() && TBot+CTBot!=0){
		if(iTplayers>=iCTplayers && TBot>0){
			removebot(TERROR);
		}
		else{
			removebot(CT);
		}
	}
 
	return PLUGIN_CONTINUE;
}
 
removebot(Team){
	new sPrefix[MAX_DATA_LENGTH];
	new sKick[MAX_DATA_LENGTH];
 
	get_vaultdata("BK_BM_PREFIX",sPrefix,MAX_DATA_LENGTH);
	if(Team==TERROR){
		get_vaultdata("BK_BM_KICKT",sKick,MAX_DATA_LENGTH);
		if(getvar("admin_debug")>0){
			log("[BK_BM] Removed bot from Terrorteam.");
		}
	}
	if(Team==CT){
		get_vaultdata("BK_BM_KICKCT",sKick,MAX_DATA_LENGTH);
		if(getvar("admin_debug")>0){
			log("[BK_BM] Removed bot from CTteam.");
		}
	}
	snprintf(sKick,MAX_DATA_LENGTH,"%s %s",sPrefix,sKick);
	exec(sKick);
 
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex){
	new TBot=0;
	new CTBot=0;
	new iBots;
 
	if(get_vaultnumdata("BK_BM_BOTS",iBots) && !iBots){
		if(getvar("admin_debug")>0){
			log("[BK_BM] Adding bots automatically disabled. No bot added.");
		}
		return PLUGIN_CONTINUE;
	}
 
	checkbot(TBot,CTBot);
	startbot(TBot,CTBot);
 
	return PLUGIN_CONTINUE;
}
 
startbot(TBot,CTBot){
	new iBots;
 
	if(get_vaultnumdata("BK_BM_BOTS",iBots) && iBots>TBot+CTBot && iBots>=playercount()){
		set_timer("addbot",1,iBots-playercount());
	}
 
	return PLUGIN_CONTINUE;
}
 
public addbot(Timer,Repeat,HLParam){
	new iTplayers=getteamcount(TERROR);
	new iCTplayers=getteamcount(CT);
	new iBots;
	new sPrefix[MAX_DATA_LENGTH];
	new sAdd[MAX_DATA_LENGTH];
 
	if(get_vaultnumdata("BK_BM_BOTS",iBots) && iBots<=playercount()){
		return PLUGIN_CONTINUE;
	}
 
	get_vaultdata("BK_BM_PREFIX",sPrefix,MAX_DATA_LENGTH);
	if(iTplayers<=iCTplayers){
		get_vaultdata("BK_BM_ADDT",sAdd,MAX_DATA_LENGTH);
		if(getvar("admin_debug")>0){
			log("[BK_BM] Added bot to Terrorteam.");
		}
	}
	else{
		get_vaultdata("BK_BM_ADDCT",sAdd,MAX_DATA_LENGTH);
		if(getvar("admin_debug")>0){
			log("[BK_BM] Added bot to CTteam.");
		}
	}
	snprintf(sAdd,MAX_DATA_LENGTH,"%s %s",sPrefix,sAdd);
	exec(sAdd);
 
	return PLUGIN_CONTINUE;
}
 
public addbotsmc(Timer,Repeat,HLParam) {
	new TBot=0;
	new CTBot=0;
 
	checkbot(TBot,CTBot);
	startbot(TBot,CTBot);
 
	return PLUGIN_CONTINUE;
}
 
public kickbots(Timer,Repeat,HLParam) {
	new TBot=0;
	new CTBot=0;
 
	checkbot(TBot,CTBot);
	kickbot(TBot,CTBot);
 
	return PLUGIN_CONTINUE;
}
 
public admin_bot_addct(HLCommand,HLData,HLUserName,UserIndex){
	new sAdd[MAX_DATA_LENGTH];
	new sPrefix[MAX_DATA_LENGTH];
 
	if(maxplayercount()==playercount()){
		selfmessage("Server full! Adding Bot failed.");
		return PLUGIN_HANDLED;
	}
	get_vaultdata("BK_BM_PREFIX",sPrefix,MAX_DATA_LENGTH);
	get_vaultdata("BK_BM_ADDCT",sAdd,MAX_DATA_LENGTH);
	snprintf(sAdd,MAX_DATA_LENGTH,"%s %s",sPrefix,sAdd);
	exec(sAdd);
	if(getvar("admin_debug")>0){
		log("[BK_BM] Added bot to CTteam.");
	}
 
	return PLUGIN_HANDLED;
}
 
public admin_bot_addt(HLCommand,HLData,HLUserName,UserIndex){
	new sAdd[MAX_DATA_LENGTH];
	new sPrefix[MAX_DATA_LENGTH];
 
	if(maxplayercount()==playercount()){
		selfmessage("Server full! Adding Bot failed.");
		return PLUGIN_HANDLED;
	}
	get_vaultdata("BK_BM_PREFIX",sPrefix,MAX_DATA_LENGTH);
	get_vaultdata("BK_BM_ADDT",sAdd,MAX_DATA_LENGTH);
	snprintf(sAdd,MAX_DATA_LENGTH,"%s %s",sPrefix,sAdd);
	exec(sAdd);
	if(getvar("admin_debug")>0){
		log("[BK_BM] Added bot to Terrorteam.");
	}
 
 
	return PLUGIN_HANDLED;
}
 
public admin_bot_removect(HLCommand,HLData,HLUserName,UserIndex){
 
	removebot(CT);
 
	return PLUGIN_HANDLED;
}
 
public admin_bot_removet(HLCommand,HLData,HLUserName,UserIndex){
 
	removebot(TERROR);
 
	return PLUGIN_HANDLED;
}
 
public admin_bot_command(HLCommand,HLData,HLUserName,UserIndex){
	new sValue[MAX_DATA_LENGTH];
	new sPrefix[MAX_DATA_LENGTH];
 
	get_vaultdata("BK_BM_PREFIX",sPrefix,MAX_DATA_LENGTH);
	convert_string(HLData,sValue,MAX_DATA_LENGTH);
 
 
	snprintf(sValue,MAX_DATA_LENGTH,"%s %s",sPrefix,sValue);
	exec(sValue);
 
	return PLUGIN_HANDLED;
}
 
public admin_bot_set(HLCommand,HLData,HLUserName,UserIndex){
	new sValue[MAX_DATA_LENGTH];
	new iValue,iBots;
	new sBotCvar[MAX_DATA_LENGTH];
 
	convert_string(HLData,sValue,MAX_DATA_LENGTH);
	strbreak(sValue,sBotCvar,sValue,MAX_DATA_LENGTH);
	strtrim(sValue," ",2);
	strtrim(sBotCvar," ",2);	
 
	if(strcasecmp(sBotCvar,"bots")==0){
		iValue=min(max(strtonum(sValue),0),maxplayercount());
		if(get_vaultnumdata("BK_BM_BOTS",iBots) && iBots<iValue){
			set_vaultnumdata("BK_BM_BOTS",iValue);
			new TBot=0;
			new CTBot=0;
			checkbot(TBot,CTBot);
			startbot(TBot,CTBot);
			numtostr(iValue,sValue);
			if(getvar("admin_debug")>0){
				log("[BK_BM] Maximum bot count changed. Filling up.");
			}
		}
		else if(iBots>iValue){
			set_vaultnumdata("BK_BM_BOTS",iValue);
			set_timer("kickbots",1,iBots-iValue);
			numtostr(iValue,sValue);
			if(getvar("admin_debug")>0){
				log("[BK_BM] Maximum bot count changed. Removing.");
			}
		}
	}
 
 
	setorinfo(sBotCvar,sValue);
 
	return PLUGIN_HANDLED;
}
 
/* Rückgabe und Schreiben von Pluginsettings */
setorinfo(sBotCvar[],sValue[]){
	new aCommands[6][MAX_COMMAND_LENGTH]={"PREFIX","ADDCT","ADDT","KICKCT","KICKT","BOTS"};
	new i;
	new Text[MAX_TEXT_LENGTH];
 
	selfmessage("Bot-Einstellungen:");
	for(i=0;i<=5;i++){
		strcpy(Text,"BK_BM_",MAX_TEXT_LENGTH);
		strcat(Text,aCommands[i],MAX_TEXT_LENGTH);
		/* Überprüfen, ob Variable gesetzt werden muss */
		if(!strcasecmp(sBotCvar,aCommands[i]) && strlen(sValue)!=0) {
			set_vaultdata(Text,sValue);
		}
		/* Aktuellen Wert ausgeben */
		get_vaultdata(Text,Text,MAX_TEXT_LENGTH);
		snprintf(Text,MAX_TEXT_LENGTH,"%s %s",aCommands[i],Text);
		selfmessage(Text);
	}
 
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
	/* Plugin Registrierung */
	plugin_registerinfo("BK Botmanager","Adds and removes bots automatically.",STRING_VERSION);
	plugin_registercmd("admin_bot_addct","admin_bot_addct",ACCESS_CONFIG,"admin_bot_addct");
	plugin_registercmd("admin_bot_addt","admin_bot_addt",ACCESS_CONFIG,"admin_bot_addt");
	plugin_registercmd("admin_bot_removect","admin_bot_removect",ACCESS_CONFIG,"admin_bot_removect");
	plugin_registercmd("admin_bot_removet","admin_bot_removet",ACCESS_CONFIG,"admin_bot_removet");
	plugin_registercmd("admin_bot_kickct","admin_bot_removect",ACCESS_CONFIG,"admin_bot_kickct");
	plugin_registercmd("admin_bot_kickt","admin_bot_removet",ACCESS_CONFIG,"admin_bot_kickt");
	plugin_registercmd("admin_bot_set","admin_bot_set",ACCESS_CONFIG,"admin_bot_set <option>");
	plugin_registercmd("admin_bot_command","admin_bot_command",ACCESS_CONFIG,"admin_bot_command <option>");
 
	if(!is_vaultfile_set()){
		new sPath[MAX_DATA_LENGTH];
		getstrvar("amv_default_config_dir",sPath,MAX_DATA_LENGTH);
		snprintf(sPath,MAX_DATA_LENGTH,"%s/%s",sPath,"vault.ini");
		setstrvar("admin_vault_file",sPath);
		plugin_exec("admin_reload","");
		if(getvar("admin_debug")>0){
			log("[BK_BM] Defined vault.ini myself.");
		}
	}
 
	new iBots;
	if(!get_vaultnumdata("BK_BM_BOTS",iBots)){
		set_vaultnumdata("BK_BM_BOTS",0);
		if(getvar("admin_debug")>0){
			log("[BK_BM] Basic setting missing in vault.ini. Creating new.");
		}
	}
 
	set_timer("addbotsmc",15,0);
 
 	return PLUGIN_CONTINUE; 
}