# stores percent with lowest percent of HP in $FormPerson, hll heals that person
# combwatch - watches for combs to move, clans room/zone when they do
# clanwhere grep
# auto eat
# calc numbers from where
# Fortunes, yay!
# - Josh

$HP = "";
$MANA = "";
$FormPerson = "";
$FormPercent = 100;
$CombWatch = 0;
$CombLevel = 0;
$LastCombRoom = "";
$CombZone = "";
$CombRoom = "";
$GrepName = "";
$AutoEat = 1;
$Eating = 1;
$Calcing = 0;
@People = "";
%CPKNumbers = ();
@ClanNumbers = "";
$NumString = "";
$Fortunes = 1;
$AutoFortune = 0;

sub checkclan
{
	my $Finding = $_[0];
	if($ClanNumbers[0] eq "")
	{
		return 1;
	}
	foreach $clan(@ClanNumbers)
	{
		if($Finding == $clan)
		{
			return 1;
		}
	}
	return 0;
}

sub cmd_fortunes
{
	if($_ eq "on")
	{
		$Fortunes=1;
	}
	if($_ eq "off")
	{
		$Fortunes=0;
	}
}

sub makenumbers
{
	my $Count = 0;
	while ( ($k,$v) = each %CPKNumbers) 
	{ 
		if(checkclan($k))
		{
			$NumString = "Clan $k\($v\) ".$NumString;
			$Count += $v;
		}
	}
	$NumString = "Numbers from Where: $NumString  Total($Count)";
}


sub cmd_printnums
{
	while ( ($k,$v) = each %CPKNumbers) 
	{ 
		print "$k => $v\n"; 
	}
}

sub cmd_clnumbers
{
	makenumbers;
	mcl_send("clan $NumString");
}

sub cmd_saynumbers
{
	makenumbers;
	mcl_send("say $NumString");
}

sub cmd_chnumbers 
{
	makenumbers;
	run("/chat.all $NumString");
}

sub cmd_hll
{
	printf("\n%s[%sHealing %s%s%s at %s%d%s%%%s]\n",$BYellow,$BBlue,$BWhite,$FormPerson,$BBlue,$Cyan,$FormPercent,$BCyan,$BYellow);
	mcl_send("cast heal $FormPerson");	
}

sub cmd_calcnumbers
{	
	@ClanNumbers = "";
	@People = "";
	%CPKNumbers = ();
	$NumString = "";
	if($_)
	{
		@ClanNumbers = split ' ';
		print "Getting numbers for @ClanNumbers\n";
	}
	$Calcing = 2;
	mcl_send("where");
}

sub cmd_cwgrep
{
	if($_ eq "")
	{
		print "You must supply a name to grep for\n";
		return;
	}
	$GrepName=$_;
	print "\n${BYellow}\[${BBlue}Grepping for ${BWhite}$GrepName${BYellow}\]\n";
	mcl_send("clanwhere");
}

sub dowhere
{
	mcl_send("where");
}

sub cmd_combwatch
{
	if($_ eq "on")
	{
		if($CombWatch == 0)
		{
			$CombWatch = 1;
			mcl_send("where");
			callout_add(\&dowhere,180);
		}
		print "Comb watcher is now on\n";
	}
	elsif($_ eq "off")
	{
		if($CombWatch)
		{
			$CombWatch = 0;
			callout_remove(\&dowhere);
		}
		print "Comb watcher is now off\n";
	}
	elsif($_ eq "")
	{
		print "Must supply on or off switch\n";
		return;
	}
}

sub getpeepinfo
{
	foreach $person(@People)
	{
		mcl_send("who -n $person");
	}
}

sub cmd_autoeat
{
	if($_ eq "")
	{
		print "You must supply on/off.\n";
		return;
	}
	elsif($_ eq "on")
	{
		$AutoEat = 1;
		print "Auto eating is no on.\n";
	}
	elsif($_ eq "off")
	{
		$AutoEat = 1;
		print "Auto eating is no off.\n";
	}
}

sub cmd_sayfortune
{
	@WholeFortune = `fortune -s $_`;
	print "Saying fortune\n@WholeFortune";
	$FortuneString = "";
	foreach $fortline(@WholeFortune)
	{
		$fortline =~ s/\n//g;
		if($fortline =~ /\S/)
		{
			if((length($FortuneString)+length($fortline)) > 250)
			{
				mcl_send("say $FortuneString");
				$FortuneString = $fortline;
			}
			else
			{
				$FortuneString = "$FortuneString $fortline";
			}
		}
	}
	mcl_send("say $FortuneString");
}

sub cmd_clanfortune
{
	@WholeFortune = `/usr/games/fortune -s $_`;
	print "Saying fortune\n";
	$FortuneString = "";
	foreach $fortline(@WholeFortune)
	{
		$fortline =~ s/\n//g;
		if($fortline =~ /\S/)
		{
			if((length($FortuneString)+length($fortline)) > 250)
			{
				mcl_send("clan $FortuneString");
				$FortuneString = $fortline;
			}
			else
			{
				$FortuneString = "$FortuneString $fortline";
			}
		}
	}
	mcl_send("clan $FortuneString");
}


sub cmd_chfortune
{
	@WholeFortune = `fortune -s $_`;
	print "Saying fortune\n";
	$FortuneString = "";
	foreach $fortline(@WholeFortune)
	{
		$fortline =~ s/\n//g;
		if($fortline =~ /\S/)
		{
			if((length($FortuneString)+length($fortline)) > 250)
			{
				mcl_send("clan $FortuneString");
				$FortuneString = $fortline;
			}
			else
			{
				$FortuneString = "$FortuneString $fortline";
			}
		}
	}
	run("/chatall $FortuneString");
}

sub cmd_writefortune
{
	@WholeFortune = `fortune $_`;
	print "Writing fortune\n";
	foreach $fortline(@WholeFortune)
	{
		print "$fortline";
		mcl_send(".$fortline");
	}
}

sub mischook
{
	s/POLLS\! polls\!\!  Go vote for your favorites\!\! www\.medievia\.com\/facts\.html \!\!//;
	if(/Your formation consists of:/)
	{
		$FormPerson = "";
		$FormPercent = 100;
	}
	if($AutoEat)
	{
		if($Eating)
		{
			if(/You fumble over the correct inflection used to cast create food./)
			{
				mcl_send("cast create food");
				mcl_send("eat food");
			}
		}
	}
	if($Fortunes)
	{
		if(/${BCyan}\[CLAN\] (\w*) has ENTERED the game\./ and $AutoFortune)
		{
			$Person = $1;
			@WholeFortune = `fortune -s`;
			$FortuneString = "";
			print "Telling $Person his fortune\n";
			foreach $fortline(@WholeFortune)
			{
				$fortline =~ s/\n//g;
				if($fortline =~ /\S/)
				{
					if((length($FortuneString)+length($fortline)) > 250)
					{
						mcl_send("tell $Person $FortuneString");
						$FortuneString = $fortline;
					}
					else
					{
						$FortuneString = "$FortuneString $fortline";
					}
				}
			}
			mcl_send("tell $Person $FortuneString");
			mcl_send("tell $Person PS sac your key");
		}
		elsif(/${BRed}(\w*) telepaths you, 'fortune'./)
		{
			$Person = $1;
			@WholeFortune = `fortune -s`;
			$FortuneString = "";
			print "Telling $Person his fortune\n";
			foreach $fortline(@WholeFortune)
			{
				$fortline =~ s/\n//g;
				if($fortline =~ /\S/)
				{
					if((length($FortuneString)+length($fortline)) > 250)
					{
						mcl_send("tell $Person $FortuneString");
						$FortuneString = $fortline;
					}
					else
					{
						$FortuneString = "$FortuneString $fortline";
					}
				}
			}
			mcl_send("tell $Person $FortuneString");
		}
	}
	($SearchString = $_) =~ tr/a-zA-Z0-9hpmvbr\/\[\]\|\!%<>//dcs;
#[Testi 	 ][CLE][lv 30|	258/ 258hp  186/ 186m	19/ 382mv]
#	if($SearchString =~ /^\[(\w*)\]\[(.*)\]\[(.*)\|(\d*)\/(\d*)hp(.*)mv\]/)
#	if($SearchString =~ /^\<\[(.*)\%\](.*)\>\<(.*)\>/)
#	if($SearchString =~ /^<\[(.*?)%\](.*?)>/)
#	if( $SearchString =~ /^<\[(d+)%\](w+)><(.*)>/ )
	if($SearchString =~ /^<(\d+)hp (\d+)m (.*)>/)
#<[79%]Jin><457hp 141m 330MountMV 100MountBR>
# <[100%]Test><26hp 100m 45mv 100br>
	{
# sub cmd_pr {printf("\n%s[%sHealing %s%s%s at %s%d%s%%%s]\n",$BYellow,$BBlue,$BWhite$FormPerson,$BBlue,$Cyan,$FormPercent,$BCyan,$BYellow); }
		$HP = $1;
		$MANA = $2;
			$FormPerson = $2;
			run("/macro f1 hll"); 
#			$FormPercent = $4*100/$5;
			$FormPercent = $1;
			printf("test $2");
		}
	if($GrepName ne "")
	{
		if(/\[(\s*)(.*)\] (Off|   )  (AFK|   )	 (.*)/)
		{
			if(!/$GrepName/i)
			{
				$_ = "";
			}
		}
	}
	if($Calcing == 2)
	{
		if($SearchString =~ /\[(.*)\](.*)/)
		{	
			if($People[0] ne "")
			{
				push(@People,$1);
			}
			else
			{
				$People[0]=$1;
			}
		}
	}
	elsif($Calcing == 1)
	{
		if($SearchString =~ /\[TL(\d*)LV(\d*)\]\[(.*)PKS(\d*)\]\[Clan(\d*)\](.*)/)
		{
			my $Clan = $5;
			if($CPKNumbers{$Clan})
			{
				$CPKNumbers{$Clan}=$CPKNumbers{$Clan}+1;
			}
			else
			{
				$CPKNumbers{$Clan}=1;
			}
			if($People[$#People] eq $6)
			{
				$Calcing = 0;
			}
		}
	}
	if($CombWatch)
	{
		if($CombLevel > 0)
		{
			$CombLevel++;
		}
		if(/The wizard says, 'let's see here...I see an entrance at...'/)
		{
			$CombLevel++;
		}
		if($CombLevel == 3)
		{
			$CombRoom = $_;
		}
		if($CombLevel == 5)
		{
			$CombZone = $_;
			if($LastCombRoom ne $CombRoom)
			{
				# If empty, this is first time to get location of combs, dont clan it
				if($LastCombRoom ne "")
				{
					mcl_send("clan combs just moved to $CombRoom inside $CombZone");
				}
				$LastCombRoom=$CombRoom;
			}
			$CombLevel = 0;
		}
	}
}

sub mprmpthook
{
	if($GrepName ne "")
	{
		$GrepName = "";
	}
	if($Calcing == 2)
	{
		if(/Press return or Q to abort/)
		{
			mcl_send("");
		}
		else
		{
			$Calcing = 1;
			getpeepinfo;
		}
	}
}
&output_add(\&mischook);
&prompt_add(\&mprmpthook);
1;
