/* plugin_logd_cavey_tk.sma version 1.2, By Caveman ICQ# 70710878
                    Please read the readme                          
	Also available from http://gosh.ex.ac.uk/~py99jan/          
			00:23 23/06/2002
 
		modified by spin-doc 20/11/2005
			contact: spin-doc@gmx.de
 
		version 1.2
			-bugfix: corrected the script to
			detect tks correct
			-add: added a bury function for punish-
			ment in LEVEL2
 
								*/
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CONSOLE 131072
#define MAX_MENU_LENGTH 512
 
new STRING_VERSION[MAX_DATA_LENGTH] = "1.2.0";
 
new Level_One;
new Level_Two;
new Level_Three;
new Level_Four;
new Level_Five;
 
/*TeamKills[1][2] - Number of times 2 has killed 1*/
new TeamKills[MAX_PLAYERS][MAX_PLAYERS];
 
new ForgivingTK[MAX_PLAYERS];
new ForgivingTKTo[MAX_PLAYERS];
 
new UsingForgiveTK[MAX_PLAYERS];
new ForgivingTKPage[MAX_PLAYERS];
 
new PlayerUserID[MAX_PLAYERS][MAX_PLAYERS];
 
new REDUCE_ON_REVENGE = 0;
new INSTANT_SLAY = 0;
new SlayTheUnt[MAX_PLAYERS];
new BuryTheUnt[MAX_PLAYERS];
 
public logd_kill(HLCommand,HLData,HLUserName,UserIndex){
	new iIDA;
	new iIDV;
	new iUserID;
	new iTeamA;
	new iTeamV;
	new iWONID;
 
	new Data[MAX_NAME_LENGTH];
	new sIDA[MAX_NUMBER_LENGTH];
	new sIDV[MAX_NUMBER_LENGTH];
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	strbreak(Data, sIDA, Data, MAX_NUMBER_LENGTH );
	strbreak(Data, sIDV, Data, MAX_NUMBER_LENGTH );
	/*strbreak(Data, Weapon, Data, MAX_NUMBER_LENGTH );*/
 
	iIDA = strtonum( sIDA );
	iIDV = strtonum( sIDV );
 
	if(playerinfo(iIDA, Data, MAX_NAME_LENGTH, iUserID, iWONID, iTeamA)){
		if(playerinfo(iIDV, Data, MAX_NAME_LENGTH, iUserID, iWONID, iTeamV)){
			if(iTeamV != iTeamA){
				/*They are not on the same team, so it is okay*/
				return PLUGIN_CONTINUE;
			} else {
				TeamKills[iIDV][iIDA] = TeamKills[iIDV][iIDA] + 1;
				punish(iIDV,iIDA);
				return PLUGIN_CONTINUE;
			}
		}
	}
 
	return PLUGIN_CONTINUE;
}
 
punish(iIDV,iIDA){
	TeamKills[0][iIDA] = 0;
	for (new i=0;i<MAX_PLAYERS;i++){
		TeamKills[0][iIDA] = TeamKills[0][iIDA] + TeamKills[i][iIDA];
	}
 
	/*Building the menu*/
	new MenuText[MAX_MENU_LENGTH];
	/*Key monitor*/
	new act_keys = 0;
	new mKeys[10] = {512,1,2,4,8,16,32,64,128,256};
 
	new UserName[MAX_NAME_LENGTH];
 
	strinit(MenuText);
	strcat(MenuText, "Choose the punnishment for ", MAX_TEXT_LENGTH);
 
	playerinfo(iIDA,UserName,MAX_NAME_LENGTH);
 
	strcat(MenuText, UserName, MAX_MENU_LENGTH);
	strcat(MenuText, ": -^n", MAX_MENU_LENGTH);
	strcat(MenuText, "1) Forgive TK\n", MAX_MENU_LENGTH);
	act_keys = act_keys + mKeys[1];
	strcat(MenuText, "2) Nothing now\n", MAX_MENU_LENGTH);
	act_keys = act_keys + mKeys[2];
	if (TeamKills[0][iIDA] >= Level_One){
		strcat(MenuText, "3) Slap\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[3];
	}
	/* Bury function implemented by spin-doc */
	if (TeamKills[0][iIDA] >= Level_Two){
		strcat(MenuText, "4) Bury\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[4];
	}
 
	if (TeamKills[0][iIDA] >= Level_Two){
		strcat(MenuText, "5) Kill\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[5];
	}
	if (TeamKills[0][iIDA] >= Level_Three){
		strcat(MenuText, "6) Kick\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[6];
	}
	if (TeamKills[0][iIDA] >= Level_Four){
		strcat(MenuText, "7) 10 min ban\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[7];
	}
	if (TeamKills[0][iIDA] >= Level_Five){
		strcat(MenuText, "8) Permanantly ban\n", MAX_MENU_LENGTH);
		act_keys = act_keys + mKeys[8];
	}	
	strcat(MenuText, "\n0) Cancel\n", MAX_MENU_LENGTH);
	act_keys = act_keys + mKeys[0];
 
	playerinfo(iIDV,UserName,MAX_NAME_LENGTH);
	UsingForgiveTK[iIDV] = 0;
	ForgivingTKTo[iIDV] = iIDA;
	ForgivingTK[iIDV] = TeamKills[0][iIDA];
	menu(UserName,MenuText,act_keys);
 
	return 1;		
}
 
public logd_roundstart(HLCommand,HLData,HLUserName,UserIndex){
	new Data[MAX_DATA_LENGTH];
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
 
	if (strstr(Data,"Round_start")){
		new i;
		for (i=0;i<MAX_PLAYERS;i++){
			if (SlayTheUnt[i] == 1){
				new UserName[MAX_NAME_LENGTH];
				if (playerinfo(i, UserName, MAX_NAME_LENGTH)){
					slay(UserName);
				}
			}
			if (BuryTheUnt[i] == 1){
				new UserName[MAX_NAME_LENGTH];
				if (playerinfo(i, UserName, MAX_NAME_LENGTH)){
					BuryUser(UserName);
				}
			}
		}
	}
	return PLUGIN_CONTINUE;
}
 
public admin_tkmenu(HLCommand,HLData,HLUserName,UserIndex){
	UsingForgiveTK[UserIndex] = 1;
	ForgivingTKPage[UserIndex] = 0;
	ForgivingTKTo[UserIndex] = 0;
	ForgivingTK[UserIndex] = 0;
	show_playermenu(UserIndex);
	return PLUGIN_HANDLED;	
}
 
show_playermenu(UserIndex){
	new MenuText[MAX_MENU_LENGTH];
	new MenuTemp[MAX_DATA_LENGTH];
	new act_keys = 0;
	new mKeys[10] = {1,2,4,8,16,32,64,128,256,512};
	new PlayerArray[MAX_PLAYERS][MAX_NAME_LENGTH];
 
	new Page = ForgivingTKPage[UserIndex];
	new pages_num = 0;
	new list_begin;
	new list_end;
	new i=0;
	new j=0;
 
	GetPlayersList(UserIndex, PlayerArray); /*Build list of players and get number of players*/
 
	while(pages_num * 7 < PlayerArray[0][0]){ /*Count number of pages*/
		++pages_num;
	}
 
	list_begin = 1;
	list_begin += Page * 7;
 
	snprintf(MenuText, MAX_MENU_LENGTH, "The following players have TK'd you ( Page %d/%d )^n^n" ,Page+1,pages_num);
 
	if ( PlayerArray[0][0] > list_begin + 6 ){ /*If the rest of the players will fit on one page*/
		list_end = list_begin + 6;
	} else {
		list_end = PlayerArray[0][0];
	}
 
	/*Set menu items 1 through 7*/
	for (i = list_begin; i <= list_end; ++i)	{
		snprintf(MenuTemp,MAX_DATA_LENGTH, "%d) %s^n", j+1, PlayerArray[i] );
		strcat(MenuText, MenuTemp, MAX_MENU_LENGTH);
		act_keys += mKeys[j];
		j++;
	}
 
	if ( Page > 0 )	{
		strcat(MenuText,"^n8. Previous...",MAX_MENU_LENGTH);
		act_keys += mKeys[7];
	}
	if ( list_end < PlayerArray[0][0] ){
		strcat(MenuText,"^n9. Next...^n",MAX_MENU_LENGTH);
		act_keys += mKeys[8];
	}
 
	strcat(MenuText,"^n0. Cancel^n",MAX_MENU_LENGTH);
	act_keys += mKeys[9];
 
	new UserName[MAX_NAME_LENGTH];
	playerinfo(UserIndex,UserName,MAX_NAME_LENGTH);
	menu(UserName,MenuText,act_keys);
 
	return 0;
}
 
public handlemenuselect(HLCommand,HLData,HLUserName,UserIndex){
	if (ForgivingTK[UserIndex] == 0 && UsingForgiveTK[UserIndex] == 0){
		/*Passing the buck*/
		return PLUGIN_CONTINUE;
	}
	new User[MAX_NAME_LENGTH];
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	new iMenu = strtonum(Data);
 
	if (iMenu == 0){
		/*Always bail => Reset all*/
		ForgivingTK[UserIndex] = 0;
		ForgivingTKTo[UserIndex] = 0;
		UsingForgiveTK[UserIndex] = 0;
		ForgivingTKPage[UserIndex] = 0;
		return PLUGIN_HANDLED;
	}
	if (ForgivingTKTo[UserIndex] != 0){
		/*This is selecting a punnishment*/
		new TargetName[MAX_NAME_LENGTH];
		playerinfo(ForgivingTKTo[UserIndex],TargetName,MAX_NAME_LENGTH);
		new Level = ForgivingTK[UserIndex];
		ForgivingTK[UserIndex] = 0;
		if (iMenu == 1){
			/*Forgive*/
			TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "%s forgave %s for TKing him", User,TargetName );
			typesay(Temp,10,10,250,10);
			return PLUGIN_HANDLED;	
		} else if (iMenu == 2){
			/*Nothing*/
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "%s saved his judgement on %s until later...", User,TargetName );
			typesay(Temp,10,255,148,9);
			messageex(TargetName,"At any point in time, use admin_tkmenu to exact revenge.",print_chat);
			messageex(TargetName,"You may ^"charge^" up your TKs for greater effectiveness.",print_chat);
			return PLUGIN_HANDLED;	
		} else if (iMenu == 3 && (Level >= Level_One)){
			/*Slap*/
			slap(TargetName);
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "%s slapped %s for tking him.", User,TargetName );
			typesay(Temp,10,255,1,115);
			if (REDUCE_ON_REVENGE != 0){
				TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;	
			}
			return PLUGIN_HANDLED;	
		/* Bury function by spin-doc */
                } else if (iMenu == 4 && (Level >= Level_Two)){
                        /*Bury*/
                        new Temp[MAX_TEXT_LENGTH];
                        if (INSTANT_SLAY == 1){
                                snprintf(Temp,MAX_DATA_LENGTH, "%s was burried by %s for tking him.", TargetName,User );
                                BuryUser(TargetName);
                        } else {
                                snprintf(Temp,MAX_DATA_LENGTH, "%s will be burried next round.", TargetName,User );
                                BuryTheUnt[ForgivingTKTo[UserIndex]] = 1;
                        }
                        typesay(Temp,10,255,1,1);
                        if (REDUCE_ON_REVENGE != 0){
                                TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;
                        }
                        return PLUGIN_HANDLED;
		} else if (iMenu == 5 && (Level >= Level_Two)){
			/*Slay*/
			new Temp[MAX_TEXT_LENGTH];
			if (INSTANT_SLAY == 1){
				snprintf(Temp,MAX_DATA_LENGTH, "%s was slain by %s for tking him.", TargetName,User );
				slay(TargetName);
			} else {
				snprintf(Temp,MAX_DATA_LENGTH, "%s will be slain next round.", TargetName,User );
				SlayTheUnt[ForgivingTKTo[UserIndex]] = 1;
			}
			typesay(Temp,10,255,1,1);
			if (REDUCE_ON_REVENGE != 0){
				TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;	
			}
			return PLUGIN_HANDLED;	
		} else if (iMenu == 6 && (Level >= Level_Three)){
			/*Kick*/
			messageex(TargetName,"You have been kicked for TKing. Take the hint.",print_console);
			messageex(TargetName,"You have been kicked for TKing. Take the hint.",print_chat);
			kick(TargetName);
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "% was kicked by %s as punishment for a TK.", TargetName,User );
			typesay(Temp,10,255,1,1);
			if (REDUCE_ON_REVENGE != 0){
				TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;	
			}
			return PLUGIN_HANDLED;	
		} else if (iMenu == 7 && (Level >= Level_Four)){
			/*Ban*/
			messageex(TargetName,"You have been temporally banned.  Take 10 minutes to cool off.",print_console);
			messageex(TargetName,"You have been temporally banned.  Take 10 minutes to cool off.",print_chat);
			ban(TargetName,10,getvar("sv_lan"));
			kick(TargetName);
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "% was temporarially banned by %s as punishment for TKing.", TargetName,User );
			typesay(Temp,10,255,1,1);
			if (REDUCE_ON_REVENGE != 0){
				TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;	
			}
			return PLUGIN_HANDLED;	
		} else if (iMenu == 8 &&(Level >= Level_Five)){
			/*Perm Ban*/
			messageex(TargetName,"You have been permanantly banned.  Have a nice day!",print_console);
			messageex(TargetName,"You have been permanantly banned.  Have a nice day!",print_chat);
			ban(TargetName,0,getvar("sv_lan"));
			kick(TargetName);
			new Temp[MAX_TEXT_LENGTH];
			snprintf(Temp,MAX_DATA_LENGTH, "% was permanantly banned by %s as punishment for a TKing.", TargetName,User );
			typesay(Temp,10,255,1,1);
			if (REDUCE_ON_REVENGE != 0){
				TeamKills[UserIndex][ForgivingTKTo[UserIndex]] = TeamKills[UserIndex][ForgivingTKTo[UserIndex]] - 1;	
			}
			return PLUGIN_HANDLED;	
		}
		return PLUGIN_HANDLED;
	} else if (UsingForgiveTK[UserIndex] != 0){
		/*This is selecting a player*/
		if (iMenu == 8){
			ForgivingTKPage[UserIndex] = ForgivingTKPage[UserIndex] - 1;
			show_playermenu(UserIndex);
			return PLUGIN_HANDLED;	
		} else if (iMenu == 9){
			ForgivingTKPage[UserIndex] = ForgivingTKPage[UserIndex] + 1;
			show_playermenu(UserIndex);
			return PLUGIN_HANDLED;	
		} else {
			/*He chose a player, so it must be good*/
			UsingForgiveTK[UserIndex] = 0;
			ForgivingTKTo[UserIndex] = PlayerUserID[UserIndex][ForgivingTKPage[UserIndex]*7+iMenu];
			punish(UserIndex,ForgivingTKTo[UserIndex]);
		}
		return PLUGIN_HANDLED;
	} else {
		return PLUGIN_CONTINUE;
	}
	return PLUGIN_CONTINUE;
}
 
 
/*Get indexes of players for menu*/
GetPlayersList(UserIndex, PlayerArray[MAX_PLAYERS][MAX_NAME_LENGTH]){
	new maxplayers = maxplayercount();
	new isvalid[MAX_NAME_LENGTH]; /*To check if player is in game*/
	new i, j = 0;
 
	for (i = 1; i <=maxplayers ; ++i){
		if (playerinfo(i,isvalid,MAX_NAME_LENGTH) && (TeamKills[UserIndex][i] > 0) ){
			j++;
			PlayerArray[0][0]=j;
			snprintf(PlayerArray[j],MAX_NAME_LENGTH,"%s",isvalid);
			PlayerUserID[UserIndex][j] = i;
		}
	}
	return j; /*return number of players*/
}
 
 
public plugin_init(){
	plugin_registerinfo("Cavey's TeamKill Script, modified by Spin-Doc","TeamKill and Punishment WITH MENUS",STRING_VERSION);
 
	new Text[MAX_TEXT_LENGTH];
	strinit(Text);
	get_vaultdata("TK_LEVEL_ONE",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		Level_One = 0;
	} else {
		Level_One = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",Level_One);
	set_vaultdata("TK_LEVEL_ONE",Text);
 
 
	strinit(Text);
	get_vaultdata("TK_LEVEL_TWO",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		Level_Two = 3;
	} else {
		Level_Two = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",Level_Two);
	set_vaultdata("TK_LEVEL_TWO",Text);
 
	strinit(Text);
	get_vaultdata("TK_LEVEL_THREE",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		Level_Three = 9;
	} else {
		Level_Three = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",Level_Three);
	set_vaultdata("TK_LEVEL_THREE",Text);
 
	strinit(Text);
	get_vaultdata("TK_LEVEL_FOUR",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		Level_Four = 10;
	} else {
		Level_Four = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",Level_Four);
	set_vaultdata("TK_LEVEL_FOUR",Text);
 
	strinit(Text);
	get_vaultdata("TK_LEVEL_FIVE",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		Level_Five = 20;
	} else {
		Level_Five = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",Level_Five);
	set_vaultdata("TK_LEVEL_FIVE",Text);
 
	strinit(Text);
	get_vaultdata("TK_REDUCE_ON_REVENGE",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		/*Default is on*/
		REDUCE_ON_REVENGE = 1;
	} else {
		REDUCE_ON_REVENGE = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",REDUCE_ON_REVENGE);
	set_vaultdata("TK_REDUCE_ON_REVENGE",Text);
 
	strinit(Text);
	get_vaultdata("TK_INSTANT_SLAY",Text, MAX_TEXT_LENGTH);
	if (strlen(Text) == 0){
		/*Default is off*/
		INSTANT_SLAY = 0;
	} else {
		INSTANT_SLAY = strtonum(Text);
	}
	snprintf(Text,MAX_TEXT_LENGTH,"%d",REDUCE_ON_REVENGE);
	set_vaultdata("TK_INSTANT_SLAY",Text);
 
	plugin_registercmd("admin_tkmenu", "admin_tkmenu", ACCESS_ALL,"admin_tkmenu : Forgive or punish those tk-ing hoes");
	plugin_registercmd("menuselect", "handlemenuselect", ACCESS_ALL);
 
	/*LogD bits*/
	plugin_registercmd("logd_kill", "logd_kill", ACCESS_CONSOLE);
	plugin_registercmd("logd_roundstart", "logd_roundstart", ACCESS_CONSOLE);
	exec( "logd_reg 57 admin_command logd_kill" );
	exec( "logd_reg 62 admin_command logd_roundstart" );
	return PLUGIN_CONTINUE;
}
 
 
public plugin_connect(HLUserName, HLIP, UserIndex){
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		for (new i=0;i<MAX_PLAYERS;i++){
			TeamKills[i][UserIndex]=0;
			TeamKills[UserIndex][i]=0;
			PlayerUserID[UserIndex][i] = 0;
		}
		ForgivingTK[UserIndex] = 0;
		ForgivingTKTo[UserIndex] = 0;
 
		UsingForgiveTK[UserIndex] = 0;
		ForgivingTKPage[UserIndex] = 0;
		SlayTheUnt[UserIndex] = 0;
		BuryTheUnt[UserIndex] = 0;
 
	}
 
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex){
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		for (new i=0;i<MAX_PLAYERS;i++){
			TeamKills[i][UserIndex]=0;
			TeamKills[UserIndex][i]=0;
			PlayerUserID[UserIndex][i] = 0;
		}
		ForgivingTK[UserIndex] = 0;
		ForgivingTKTo[UserIndex] = 0;
 
		UsingForgiveTK[UserIndex] = 0;
		ForgivingTKPage[UserIndex] = 0;
		SlayTheUnt[UserIndex] = 0;
		BuryTheUnt[UserIndex] = 0;
 
	}
	return PLUGIN_CONTINUE;
}
 
 
/* Bury function by spin-doc
   taken from Desslocks plugin_logd_ffmon */
BuryUser(sNAME[])
{
	new x;
	new y;
	new z;
 
	execclient(sNAME, "slot1"); // drop his first weapon
	execclient(sNAME, "+attack");
	execclient(sNAME, "-attack");
	execclient(sNAME, "drop");
	execclient(sNAME, "slot2"); // drop his pistol (if any)
	execclient(sNAME, "+attack");
	execclient(sNAME, "-attack");
	execclient(sNAME, "drop");
	execclient(sNAME, "slot5"); // drop the bomb (if any)
	execclient(sNAME, "+attack");
	execclient(sNAME, "-attack");
	execclient(sNAME, "drop");
	execclient(sNAME, "say Help! I've been buried and I can't get up!");
 
	get_userorigin(sNAME, x, y, z);
	teleport(sNAME, x, y, (z-20));    
}