//@unloadnpc Versus
//@unloadnpc PvP Ranking
//@unloadnpc KDR_Calc
//@loadnpc npc/thor/pvp.txt



-	script	Versus	-1,{
OnInit:
	//--Configuration
	set .System,1; 			//Toggle PvP System (0:Disable|1:Enable)
	set .ServerType,1; 		//Are you using TXT or SQL? (0:TXT|1:SQL)
	set .GMLevel,99; 		//GM levels above or equal this level will be ignored by the system
	setarray .PvPMap$,"pvp_y_1-1","pvp_y_1-2","pvp_y_1-3","pvp_y_1-4","pvp_y_1-5","pvp_y_2-1","pvp_y_2-2","pvp_y_2-3","pvp_y_2-4","pvp_y_2-5","pvp_y_3-1","pvp_y_3-2","pvp_y_3-3","pvp_y_3-4","pvp_y_3-5","pvp_y_4-1","pvp_y_4-2","pvp_y_4-3","pvp_y_4-4","pvp_y_4-5","pvp_y_5-1","pvp_y_5-2","pvp_y_5-3","pvp_y_5-4","pvp_y_5-5","pvp_y_6-1","pvp_y_6-2","pvp_y_6-3","pvp_y_6-4","pvp_y_6-5","pvp_y_8-1","pvp_y_8-2","pvp_y_8-3","pvp_y_8-4","pvp_y_8-5";

	set .Gfx, 1;			//Enable Cutin count graphics features? (0:Disable|1:Enable)
	set .Announce,0;

	//PvP Ladder
	set .Top,10; 			//What is the length of the list?
	set .reset,1; 			//Allow players to reset their PvP statistics? (0:Disable|1:Zeny|2:Cashpoint|3:Skull)
	set .ZenyFee,1000000; 		//How much zeny you want to charge to reset their pvp stat?
	set .CashFee,1000; 		//How much cashpoint you want to charge to reset their pvp stat?
	set .SkullFee,1000; 		//How much skull you want to charge to reset their pvp stat?
	set .AnnouncerFee,2000; 	//How much cashpoint per announcer? (DotA announcer is set as default)

	//Reward Setting (Every Kill)
	set .MaxZeny,1000000000; 	//What is the max zeny of the server?
	set .zeny,0; 			//Earn zeny each kill? (0:Disable)
	set .Cash,0; 			//How much #CASHPOINT every kill? (0:Disable)
	set .Skull,0; 			//Collect player's skull each kill? (0:Disable|1:Enable)
	setarray .ItemID,0; 		//Get items each kill? (0:Disable)
	setarray .Amount,0; 		//How many items per kill?

	//Penalty Setting
	set .Feed,10; 			//How many kills before feeding is called? (10:Recommended)
	set .Penalty,1; 		//What is the penalty for the feedee? (0:Disable|1:Kill Rollback|2:Server Kick|3:Server Block)
	set .Kick,3; 			//Who to kick? (1:Feedee|2:Feeder|3:Both)
	set .Rollback,10; 		//How many times should the kill count rollback when feeding is called? (10:Recommended)

	//Announcer Setting
	set .Broadcast,	bc_map; 	//Where do you want to broadcast? ( bc_all | bc_map )
	set .Streak,1; 			//Show number of killing streak in broadcast? e.g. Ghost is Beyond Godlike(8)
	set .Multiple,1; 		//Show number of multiple kill in broadcast? e.g. Makizushi just got an Ownage(5)
	set .Nemesis,1; 		//Show number of nemesis in broadcast? e.g. Epoque is owning Jguy(9)
	set .Feeder,1; 			//Show number of feed in broadcast? e.g. Cookies is feeding `KeiKun(10)
	set .RandKillWord,1; 		//Randomize the kill words? e.g. Arcenciel "Kill Word" jTynne (0:Disable|Default:"pwned")
	setarray .KillWord$,
		"pwned",
		"killed",
		"disintegrated",
		"neutralized"; 		//Enable above setting for this to work

	//Broadcast Color | See HexTable: http://johncfish.com/bggallery/otherchart/hextable.gif
	set .KColor$,"0xFFFF00"; 	//Kill Broadcast Color | e.g. Jguy pwned Mercurial 
	set .KSColor$,"0xFFFF00"; 	//Killing Streak Broadcast Color | e.g. jTynne is on a Killing Spree
	set .MKColor$,"0x33FF00"; 	//Multiple Kill Broadcast Color | e.g. Mercurial just got a Double Kill
	set .ESColor$,"0xFF0000"; 	//End Streak Broadcast Color | e.g. BrianL just ended Jguy's Killing Spree
	set .NMColor$,"0x0099FF"; 	//Nemesis Broadcast Color | e.g. Xazax is owning Cookies
	set .PBColor$,"0xFF0000"; 	//Payback Broadcast Color | e.g. Makizushi just have a payback againts lilcooldude69
	set .FDColor$,"0xFF00CC"; 	//Feeder Broadcast Color | e.g. Cookies is feeding Z3R0

	//--Warning: Critical Setting | Killing Streaks and Multiple Kills Announcer
	setarray .KSDotA$, "is on a Killing Spree", "is Dominating", "has a Mega-Kill", "is Unstoppable", "is Wicked Sick", "has a M-m-m-m....Monster Kill", "is Godlike", "is Beyond Godlike";
	setarray .KSHoN$,  "is a Serial Killer", "is an Ultimate Warrior", "is Legendary", "is on an Onslaught!", "is Savage Sick", "is Dominating", "has a Bloodbath", "is Immortal"; //Took out #9. Champion of Newearth
	setarray .MKDotA$, "just got a Double Kill", "just got a Triple Kill", "just got an Ultra Kill", "is on a Rampage", "is Owning his enemies";
	setarray .MKHoN$,  "just got a Double Tap", "just got a Hat Trick", "just got an Quad Kill", "annihilated his enemies", "just got a Genocide";
	setarray .ESDotA$, "Killing Spree", "Dominating", "Mega-Kill", "Unstoppable", "Wicked Sick", "Monster Kill", "Godlike", "Beyond Godlike";
	setarray .ESHoN$, "Serial Killer", "Ultimate Warrior", "Legendary", "Onslaught", "Savage Sick", "Dominating", "Bloodbath", "Immortal"; //Took out #9. Champion of Newearth
	setarray .EMDotA$, "Double Kill", "Triple Kill", "Ultra Kill", "Rampage", "Ownage";
	setarray .EMHoN$,  "Double Tap", "Hat Trick", "Quad Kill", "Annihilation", "Genocide";

	//--Warning: Fail Safe | Do Not Touch
	set .System, ( .System < 0 ) ? 0 : .System;
	set .ServerType, ( .ServerType < 0 ) ? 0 : .ServerType;
	set .Ladder, ( .Ladder < 0 ) ? 0 : .Ladder;
	set .Top, ( .Top < 0 ) ? 0 : .Top;
	//set .GMLevel, ( .GMLevel < 0 ) ? 0 : ( ( .GMLevel > 99 ) ? 99 : .GMLevel );
	set .reset, ( .reset < 0 ) ? 0 : ( ( .reset > 3 ) ? 3 : .reset );
	set .ZenyFee, ( .ZenyFee < 0 ) ? 0 : ( ( .ZenyFee > .MaxZeny ) ? .MaxZeny : .ZenyFee );
	set .CashFee, ( .CashFee < 0 ) ? 0 : .CashFee;
	set .SkullFee, ( .SkullFee < 0 ) ? 0 : .SkullFee;
	set .zeny, ( .zeny < 0 ) ? 0 : ( ( .zeny > .MaxZeny ) ? .MaxZeny : .zeny );
	set .Cash, ( .Cash < 0 ) ? 0 : .Cash;
	if( .Feed < 1){ set .Feed, 0; }
	if( .Penalty > 3){ set .Penalty, 3; }
	if( .Penalty < 1){ set .Penalty, 0; }
	if( .Rollback < 1){ set .Rollback, 0; }
	if( .Streak > 1){ set .Streak, 1; }
	if( .Streak < 1){ set .Streak, 0; }
	if( .Multiple > 1){ set .Multiple, 1; }
	if( .Multiple < 1){ set .Multiple, 0; }
	if( .Nemesis > 1){ set .Nemesis, 1; }
	if( .Nemesis < 0){ set .Nemesis, 0; }
	if( .Feeder > 1){ set .Feeder, 1; }
	if( .Feeder < 1){ set .Feeder, 0; }
	if( .RandKillWord > 1 ) set .RandKillWord, 1;
	if( .RandKillWord < 0 ) set .RandKillWord, 0;
	
	//SQL Bits
	query_sql (
		"CREATE TABLE IF NOT EXISTS `pvp_rank` ("+
		"`char_id` int(11) NOT NULL,"+
		"`account_id` int(11) NOT NULL,"+
		"`char` varchar(30) NOT NULL,"+
		"`kill` int(11) NOT NULL,"+
		"`death` int(11) NOT NULL,"+
		"`kdr` varchar(30) NOT NULL,"+
		"`killingstreak` int(11) NOT NULL,"+
		"`multikill` int(11) NOT NULL,"+
		"`killingspree` int(11) NOT NULL,"+
		"`dominating` int(11) NOT NULL,"+
		"`megakill` int(11) NOT NULL,"+
		"`unstoppable` int(11) NOT NULL,"+
		"`wickedsick` int(11) NOT NULL,"+
		"`monsterkill` int(11) NOT NULL,"+
		"`godlike` int(11) NOT NULL,"+
		"`beyondgodlike` int(11) NOT NULL,"+
		"`doublekill` int(11) NOT NULL,"+
		"`triplekill` int(11) NOT NULL,"+
		"`ultrakill` int(11) NOT NULL,"+
		"`rampage` int(11) NOT NULL,"+
		"`ownage` int(11) NOT NULL,"+
		"`nemesiskill` int(11) NOT NULL,"+
		"`feedcount` int(11) NOT NULL,"+
		"PRIMARY KEY (`char_id`)"+
		") ENGINE=MyISAM;"
	);
	end;

OnPCKillEvent:
	if( .System ) {

		if( getgmlevel() >= .GMLevel ) end;
		if( !compare( strcharinfo(3), "pvp_y_" )) end;

		if( @nm == .Feed && .Penalty ) {
			dispbottom "Penalized";
			callsub OnClearData;
			setarray .@penalty_cmd$[2], "kick", "block";
			switch ( .Penalty ) {
				case 1: callsub OnRollback; break;
				default: callsub OnRollback;
					if( .Kick & 1 ) atcommand "@"+.@penalty_cmd$[.Penalty]+" "+strcharinfo(0);
					if( .Kick & 2 ) atcommand "@"+.@penalty_cmd$[.Penalty]+" "+rid2name(killedrid);
					break;
			}
		}
		
		//Rewards Per-kill.
		if( @nm < .Feed ) {
			if( .zeny && ( ( Zeny + .zeny ) < .MaxZeny ) )
				set Zeny, Zeny + .zeny;
			if( .Cash )
				dispbottom "[Point System] +1 Cashpoint."; set #CASHPOINT, #CASHPOINT + .Cash;
			if( .Skull )
				getnameditem 7420, rid2name(killedrid);
			for( set .@i, 0; .@i < getarraysize(.ItemID); set .@i, .@i + 1 )
				if(.ItemID) getitem .ItemID[.@i], .Amount[.@i];
		}
		if (.Announce) { announce strcharinfo(0)+" "+( .RandKillWord ? .KillWord$[ rand( getarraysize( .KillWord$ ) ) ] : "pwned" )+" "+rid2name(killedrid)+"'s head", .Broadcast, .KColor$; } else {}
		set Kill, Kill + 1;

		if( @ks < 10 )
			set @ks, @ks + 1;
		if( @mk < 6 )
			set @mk, @mk + 1;
		if( !NemesisKilled )
			set NemesisKilled, killedrid;
		if( NemesisKilled == killedrid )
			set @nm, @nm + 1;
		deltimer "Versus::OnMKReset"; 
		addtimer 18000, "Versus::OnMKReset";
		callsub OnRecordStreak;
		//dispbottom "KS: "+@ks;
		callsub OnAnnounce; 
	}
	end;

OnPCDieEvent:
	if( .System ) {
		if( getgmlevel() >= .GMLevel || killerrid < 2000000 || killerrid > 3000000 )
			end;
		if( !compare( strcharinfo(3), "pvp_y_" )) end;
		set Death, Death + 1;
		callsub OnDeath;
	}
	end;

OnPCLogoutEvent:
	if( .System ) {
		if( getgmlevel() >= .GMLevel )
			end;
		set NemesisKilled, 0; 
		if( .ServerType && Kill )
			query_sql("REPLACE INTO `pvp_rank` (`char_id`, `account_id`, `char`, `kill`, `death`, `kdr`, `killingstreak`, `multikill`,  `nemesiskill`, `feedcount`,`killingspree`, `dominating`, `megakill`, `unstoppable`, `wickedsick`, `monsterkill`, `godlike`, `beyondgodlike`, `doublekill`, `triplekill`, `ultrakill`, `rampage`, `ownage`) VALUES ("+getcharid(0)+","+getcharid(3)+", '"+rid2name(getcharid(3))+"', "+Kill+", "+Death+", '"+callfunc("KDR_Calc", Kill, ( !Death ? 1 : Death ))+"', "+KillingStreak+", "+MultiKill+",  "+NemesisKill+", "+FeedCount+", "+KillingSpree+", "+Dominating+", "+MegaKill+", "+Unstoppable+", "+WickedSick+", "+MonsterKill+", "+Godlike+", "+BeyondGodlike+", "+DoubleKill+", "+TripleKill+", "+UltraKill+", "+Rampage+", "+Ownage+")");
	}
	end;

OnRecordStreak:
	if( @ks > 2 ) {
		set KillingStreak, KillingStreak + 1;
		switch( @ks ) {
			case 3: set KillingSpree, KillingSpree + 1;		break; 
			case 4: set Dominating, Dominating + 1;			break;
			case 5: set MegaKill, MegaKill + 1;			break; 
			case 6: set Unstoppable, Unstoppable + 1;		break; 
			case 7: set WickedSick, WickedSick + 1;			break;
			case 8: set MonsterKill, MonsterKill + 1;		break; 
			case 9: set Godlike, Godlike + 1;			break; 
			case 10: set BeyondGodlike, BeyondGodlike + 1;		break;
			default: 						break;
		}
	}
	if( @mk > 1 ) {
		set MultiKill, MultiKill + 1;
		switch( @mk ) {
			case 2: set DoubleKill, DoubleKill + 1;			break; 
			case 3: set TripleKill, TripleKill + 1;			break;
			case 4: set UltraKill, UltraKill + 1;			break;
			case 5: set Rampage, Rampage + 1;			break; 
			case 6: set Ownage, Ownage + 1;				break;
			default: 						break;
		}
	}
	if( @nm > 3 && @nm < .Feed )
		set NemesisKill, NemesisKill + 1;
	if( @nm >= .Feed )
		set FeedCount, FeedCount + 1;
	return;

OnAnnounce:
	if( @ks == 1 && Kill == 1 ) {
		if (.Announce) { announce strcharinfo(0)+( !#Announcer ? " drew first blood" : " got bloodlust" ), .Broadcast, .KSColor$; } else {}
		sleep2 1000;
		//soundeffectall ""+#Announcer+"_KS"+1+".wav", 0, strcharinfo(3);
	}

	if(@ks == 2 && .Gfx){ cutin "ks2.bmp",0; sleep2 1000; cutin "",255; } else { } 

	if( @ks > 2 ) {
		if (.Announce) { announce strcharinfo(0)+" "+( !#Announcer ? .KSDotA$[@ks - 3] : .KSHoN$[@ks - 3] )+" "+((.Streak)?"("+@ks+")":"")+"", .Broadcast, .KSColor$; } else {}
		if(.Gfx){ cutin "ks"+@ks+".bmp",0; } else { }
		sleep2 1500;
		//soundeffectall ""+#Announcer+"_KS"+@ks+".wav", 0, strcharinfo(3);
		cutin "",255;
	}
	if( @mk > 1 ) {
		if (.Announce) { announce strcharinfo(0)+" "+( !#Announcer ? .MKDotA$[@mk - 2] : .MKHoN$[@mk - 2] )+" "+((.Multiple)?"("+@mk+")":"")+"", .Broadcast, .MKColor$; } else {}
		if(.Gfx){ cutin "mk"+@mk+".bmp",0; } else { }
		sleep2 1500;
		//soundeffectall ""+#Announcer+"_MK"+@mk+".wav", 0, strcharinfo(3);
		cutin "",255;
	}
	if( @nm > 3 && @nm < .Feed ) {
		if (.Announce) { announce strcharinfo(0)+" is owning "+rid2name(NemesisKilled)+" "+((.Nemesis)?"("+@nm+")":"")+"", .Broadcast, .NMColor$; } else {}
		sleep2 2000;
		//if( #Announcer > 0 ) { soundeffectall #Announcer+"_Nemesis.wav", 0, strcharinfo(3); } else { soundeffectall "1_Nemesis.wav", 0; }
	}
	if( @nm >= .Feed ) {
		if (.Announce) { announce rid2name(NemesisKilled)+" is feeding "+strcharinfo(0)+" "+((.Feeder)?"("+@nm+")":"")+"", .Broadcast, .FDColor$; } else {}
		sleep2 1500;
		//soundeffectall "Feeder.wav", 0, strcharinfo(3);
	}
	return;

OnDeath:
	if( @ks > 2 )
		if (.Announce) { announce rid2name( killerrid )+" just ended "+strcharinfo(0)+"'s "+( !#Announcer ? .ESDotA$[@ks - 3] : .ESHoN$[@ks - 3] )+" "+((.Streak)?"("+@ks+")":"")+"", .Broadcast, .ESColor$; } else {} 
		if(.Gfx){ cutin "streakbreak.bmp",0; sleep2 1000; cutin "",255; } else { }
	if( @nm > 3 ) {
		//soundeffectall #Announcer+"_Payback.wav", 0, rid2name(killedrid);
		if (.Announce) { announce rid2name( killerrid )+" just got payback against "+strcharinfo(0)+" "+((.Nemesis)?"("+@nm+")":"")+"", .Broadcast, .PBColor$; } else {}
		if(.Gfx){ cutin "payback.bmp",4; } else { }
	}
	callsub OnClearData;
	return;

OnMKReset:
	set @mk, 0;
	end;
	
OnClearData:
	sleep2 1000;
	set NemesisKilled, 0;
	set @ks, 0;
	set @mk, 0;
	set @nm, 0;
	return;

OnRollback:
	if( .zeny )
		set Zeny, Zeny - (.zeny * .Rollback);
	if( .Cash ) {
		dispbottom "[Point System] -"+(.Cash * .Rollback)+" Cashpoint.";
		set #CASHPOINT, #CASHPOINT - ( .Cash * .Rollback );
	}
	if( .Skull )
		delitem 7420, .Rollback;
	for( set .@i, 0; .@i < getarraysize(.ItemID); set .@i, .@i + 1 )
		if( .ItemID ) delitem .ItemID[.@i], .Amount[.@i] * .Rollback;
		
	set Kill,		( ( Kill - .Rollback ) > 0 )			? ( Kill - .Rollback )				: 0;
	set KillingSpree,	( ( KillingSpree - 1 ) > 0 )			? ( KillingSpree - 1 )				: 0;
	set Dominating,		( ( Dominating - 1 ) > 0 ) 			? ( Dominating - 1 )				: 0;
	set MegaKill,		( ( MegaKill - 1 ) > 0 ) 			? ( MegaKill - 1 )				: 0;
	set Unstoppable,	( ( Unstoppable - 1 ) > 0 )			? ( Unstoppable - 1 )				: 0;
	set WickedSick,		( ( WickedSick - 1 ) > 0 ) 			? ( WickedSick - 1 )				: 0;
	set MonsterKill,	( ( MonsterKill - 1 ) > 0 )			? ( MonsterKill - 1 )				: 0;
	set Godlike,		( ( Godlike - 1 ) > 0 ) 			? ( Godlike - 1 )				: 0;
	set BeyondGodlike,	( ( BeyondGodlike - 1 ) > 0 ) 			? ( BeyondGodlike - 1 )				: 0;
	set DoubleKill, 	( ( DoubleKill - 1 ) > 0 ) 			? ( DoubleKill - 1 )				: 0;
	set TripleKill,		( ( TripleKill - 1 ) > 0 )			? ( TripleKill - 1 )				: 0;
	set UltraKill,		( ( UltraKill - 1 ) > 0 ) 			? ( UltraKill - 1 )				: 0;
	set Rampage,		( ( Rampage - 1 ) > 0 ) 			? ( Rampage - 1 )				: 0;
	set Ownage,		( ( Ownage - 1 ) > 0 ) 				? ( Ownage - 1 )				: 0;
	set NemesisKill,	( ( NemesisKill - (.Rollback - 3) ) > 0 ) 	? ( NemesisKill - (.Rollback - 3) )		: 0;
	announce "[Anti-Feed] Your kill count has been rollbacked "+.Rollback+" times.",bc_self,0xFF0000;
	return;
	
//Added Functions
function broadcast { return ( ( getarg(0) ) ? bc_all : bc_map ); }
}

//Ghost's KDR Algorithm
function	script	KDR_Calc	{
	if(!getarg(1)) { debugmes "Error in KDR function, getarg(1) invalid value"; return 0; } // [Lighta]
	if(getarg(0) >= getarg(1)) {
		if(getarg(1)) {
			set .@i, getarg(0)/getarg(1);
			if((getarg(0)%getarg(1)) == 0) { return .@i+".00"; }
			set .@j, (getarg(0)%getarg(1))*100/getarg(1);
			return .@i+"."+.@j;
		} else {
			return getarg(0)+".00";
		}
	}
	if( getarg(0) < getarg(1) ) {
		if( getarg(0) ) { 
			set .@i, (getarg(0)*10)/getarg(1);
			set .@j, ((getarg(0)*10)%getarg(1))*10/getarg(1);
		return "0."+.@i +.@j;
		} else {
			return "0.00";
		}
	 }
 }
