#the NorthlandsMUD a perl based mud. Please enjoy and share this great software with
#anyone who wants it. 
#Copyright (C) 2000  Peter Smith creator@operamail.com
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

sub feint {
	combat("feint")
}
sub bal {
	$outbuffer{$client}.="Balance: ".$desc{balance}[int($user{$client}{balance}/10)];
}
sub swap {
	$temp=$user{$client}{health}{"right hand"}{hold};
	$user{$client}{health}{"right hand"}{hold}=$user{$client}{health}{"left hand"}{hold};
	$user{$client}{health}{"left hand"}{hold}=$temp;
	$outbuffer{$client}.="\r\n$prompt";
}
sub sell {
	unless ($room[$user{$client}{room}]{buys}){
		$outbuffer{$client}.="Sorry no store here\r\n$prompt";
		return;
	}
	shift @input;
	my $string="@input";
	my ($item, $where)=locateitemhands($string);
	my $pass=0;
	foreach $a (keys %{$room[$user{$client}{room}]{buys}}){
		if (defined $item{$item}{$a}){
			if ($room[$user{$client}{room}]{buys}{$a}>$pass){
				$pass=$room[$user{$client}{room}]{buys}{$a};
			}
		}
	}
	unless ($pass){
		$outbuffer{$client}.="This store won't buy that\r\n$prompt";
		return;
	}
	my $price=$pass*$item{$item}{value};
	$user{gold}+=$price;
	removeitemfrominventory($where);
	my $total=scalar keys %{$room[$user{$client}{room}]{store}};
	$outbuffer{$client}.="You sell your $item{$item}{name} for $price gold pieces";
	if ($total>=$room[$user{$client}{room}]{maxstore}){
		delete $item{$item};		
	}else {
		$room[$user{$client}{room}]{store}{$item}=$item{$item}{value}
	}
}
sub buy {
	unless ($room[$user{$client}{room}]{store}){
		$outbuffer{$client}.="Sorry no store here\r\n$prompt";
		return;
	}
	shift @input;
	my $tobuy="@input";
	unless ($tobuy){
		$outbuffer{$client}.="\r\n$prompt";
		return;
	}
	foreach $a (keys %{$room[$user{$client}{room}]{store}}){
		if ($a =~s/^#//){
			if ($itemtemplate{$a}{name}=~/$tobuy/i){
				#this item now check for gold
				unless ($user{$client}{gold} > $room[$user{$client}{room}]{store}{"#$a"}){
					$outbuffer{$client}.="You don't have enough gold for that\r\n$prompt";
					return;
				}
				my $hold=findopenhold();
				unless ($hold){
					$outbuffer{$client}.="You don't have a free hand for that";
					return;
				}
				$outbuffer{$client}.="You buy a $itemtemplate{$a}{name}\r\n$prompt";
				$user{$client}{gold} -= $room[$user{$client}{room}]{store}{"#$a"};
				additemtoinventory(makeitem($a),$hold);	
				$item{$user{$client}{health}{$hold}{hold}}{value}=$room[$user{$client}{room}]{store}{"#$a"};
			return;
			}
		}else {
			if ($item{$a}{name}=~/$tobuy/i){
				unless ($user{$client}{gold} > $room[$user{$client}{room}]{store}{"$a"}){
					$outbuffer{$client}.="You don't have enough gold for that\r\n$prompt";
					return;
				}
				my $hold=findopenhold();
				unless ($hold){
					$outbuffer{$client}.="You don't have a free hand for that";
					return;
				}
				$user{$client}{gold} -= $room[$user{$client}{room}]{store}{"$a"};
				additemtoinventory(makeitem($a),$hold);
				$item{$user{$client}{health}{$hold}{hold}}{value}=$room[$user{$client}{room}]{store}{"$a"};
				delete ($room[$user{$client}{room}]{store}{"$a"});
	
				$outbuffer{$client}.="You buy a $item{$a}{name} \r\n$prompt";
			return;
			}
		}
	}
	$outbuffer{$client}.= "Sorry I don't carry that\r\n$prompt";
}
sub cat {
	unless ($room[$user{$client}{room}]{store}){
		$outbuffer{$client}.="Sorry no store here\r\n$prompt";
		return;
	}
	foreach $a (keys %{$room[$user{$client}{room}]{store}}){
		if ($a =~s/^#//){
			$outbuffer{$client}.="$itemtemplate{$a}{name} - ".$room[$user{$client}{room}]{store}{"#$a"}." gold pieces\r\n";
		}else {
			$outbuffer{$client}.="$item{$a}{name} - ".$room[$user{$client}{room}]{store}{"$a"}." gold pieces\r\n";
		}
	}
}
sub train {
	unless ($room[$user{$client}{room}]{train}){
		$outbuffer{$client}.="Sorry you can't train here.\r\n";
		return 1;
	}
	my $in = $room[$user{$client}{room}]{train};
	$cost = ($user{$client}{$in}+1)*3;
	if ($user{$client}{train}<time){
		$outbuffer{$client}.="It will cost $cost Character Development Points to train $in to ".($user{$client}{$in}+1);
		$user{$client}{train}=time+60*3;
		return 1;
	}else {
		if ($cost>$user{$client}{cp}){
			$outbuffer{$client}.="You don't have enough CDPs to train that much\r\n";
			return 1;
		}
		$user{$client}{cp}-=$cost;
		$user{$client}{$in}++;
		$outbuffer{$client}.="You train hard and gain a point of $in";
		delete $user{$client}{train};
	
	}

}
sub skin {
	shift (@input);
	my $a=locateroomplayer("@input");
	unless ($user{$a}{dead}){
		$outbuffer{$client}.="You may only skin dead beings.\r\n$prompt";
		return;
	}
	unless ($user{$a}{skin}){
		$outbuffer{$client}.="You can't skin $user{$a}{pre}$user{$a}{name}. \r\n$prompt";
		return;
	}
	unless ($hold=findopenhold()){
		$outbuffer{$client}.="You don't have a free hand for the skin.\r\n$prompt";
		return;
	}
	if ($user{$a}{skinned}){
		$outbuffer{$client}.="Someone has already skinned that\r\n$prompt";
		return;
	}
	my $skill=($user{$client}{exp}{Survival}{skinning}{ranks} or 1);
	my $diff=$user{$a}{skindifficulty};
	my $chance=$skill/($skill+$diff);
	my $rand=rand(1);
	if ($rand<$chance){
		#success
		addexp("skinning", "Survival",((100-$chance)**1.45)*2);
		$outbuffer{$client}.="You have skinned $user{$a}{pre}$user{$a}{name}\r\n$prompt";
		additemtoinventory(makeitem($user{$a}{skin}), $hold);
		$user{$a}{skinned}=1;
	}elsif ($rand<$chance+.3){
		#partial success
		addexp("skinning", "Survival",((100-$chance)**1.35));
		$outbuffer{$client}.="You have skinned $user{$a}{pre}$user{$a}{name} but nearly ruin it.\r\n$prompt";
		additemtoinventory(makeitem($user{$a}{skin}), $hold);
		$item{$user{$client}{health}{$hold}{hold}}{value}/=3;
		$user{$a}{skinned}=1;
	}else {
		#failure
		addexp("skinning", "Survival",(100-$chance)*2);
		$outbuffer{$client}.="You have failed to skin $user{$a}{pre}$user{$a}{name}, making a horrible mess\r\n$prompt";
		$user{$a}{skinned}=1;
	}
	
}
sub weaponhand {
#choose weaponhand
shift (@input);
my $weapon="@input";
unless (defined($user{$client}{health}{$weapon})){
	$outbuffer{$client}.="You don't seem to have one of those.\r\n$prompt";
	return;
	}
$user{$client}{weapon}="$weapon";
$outbuffer{$client}.="You will now attack with your @input\r\n$prompt";

}
sub talk{
		shift (@input);
		if ($input[-1]=~/!$/){
			say("$user{$client}{pre}$user{$client}{name} exclaims, \"@input\"");
			return
			}
		if ($input[0]=~/\?$/){
			say("$user{$client}{pre}$user{$client}{name} asks, \"@input\"");
			return
			}
		
		say("$user{$client}{pre}$user{$client}{name} says, \"@input\"");
		}
sub stats {
		$outbuffer{$client}.="You are $user{$client}{name}, a $elements[$user{$client}{element}] Elemental\r\n";
#		$outbuffer{$client}.=sprintf("%-10.10s  %3.1d\r\n","strength", $user{$client}{str},"agility", $user{$client}{agi});
		$outbuffer{$client}.="Strength: $user{$client}{str}\r\nAgility: $user{$client}{agi}\r\nRelfex: $user{$client}{reflex}\r\nConstitution: $user{$client}{con}\r\nWisdom: $user{$client}{wis}\r\nIntelligence: $user{$client}{int}\r\nDiscipline: $user{$client}{disc}\r\n";
		$outbuffer{$client}.="Character Development Points: $user{$client}{cp}";
		$outbuffer{$client}.="\r\nGold: $user{$client}{gold}";
		$outbuffer{$client}.="\r\n$prompt";
		}
sub experience {
		foreach $b (keys %{$user{$client}{exp}}){
			$outbuffer{$client}.="\u$b Skills:\r\n";
			foreach $a (keys %{$user{$client}{exp}{$b}}){
				if ($a eq "total"){
					next;
					}
				my $z;
				$z=$user{$client}{exp}{$b}{$a}{ranks};
				$outbuffer{$client}.="   \u$a:  $z  -  ";
				unless (defined($user{$client}{exp}{$b}{$a}{expcur}) and defined($user{$client}{exp}{$b}{$a}{expneeded})){
					$outbuffer{$client}.="\r\n";
					next;
					}
				$z=int(($user{$client}{exp}{$b}{$a}{expcur}/$user{$client}{exp}{$b}{$a}{expneeded})*100);
				$outbuffer{$client}.="$z% ";
#				$outbuffer{$client}.=" $user{$client}{exp}{$b}{$a}{expcur}/$user{$client}{exp}{$b}{$a}{expneeded} ";
				my $maxpool=$user{$client}{disc}*600;
			
				if ($user{$client}{exp}{$b}{$a}{pool}){
					if ($user{$client}{exp}{$b}{$a}{pool}>=$maxpool){
						$z="locked";
						}
					else{
						$z="learning";
						if ($user{$client}{exp}{$b}{$a}{pool}>=600){
							$z="considering";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=900){
							$z="thinking";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=1200){
							$z="pondering";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=1500){
							$z="contemplating";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=1800){
							$z="reasoning";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=2100){
							$z="reflecting";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=2400){
							$z="speculating";
							}
						if ($user{$client}{exp}{$b}{$a}{pool}>=2700){
							$z="mastering";
							}
						}
					}
				else{
					$z="clear";
					}
				$outbuffer{$client}.=ucfirst $z."\r\n";
				}
			}
		
		$outbuffer{$client}.="$prompt";
		}	
sub lookcommand {
		if (@input==1){
			look();
			return 1;
			}
		if (@input==3){
			shift @input;
			}
		elsif (@input==2){
			$input[0]="at";
			}
		else {
			$outbuffer{$clinet}.="can't go deep yet\r\n$prompt";
			return;
			}
		my $a;
		my $location;
		if ($a=locateitem($user{$client}{room},$input[1])){
			
			if ($input[0] ne "at"){
				$location="$input[0]"."desc";	
				}
			else {
				$location="desc";
				}	
			if ($item{$a}{$location}){
				$outbuffer{$client}.="$item{$a}{$location}\r\n$prompt";
				}
			else {
				$outbuffer{$client}.="Can't look there\r\n$prompt";
				}
			if (defined $item{$a}{container}){
				my $first;
				foreach $b (keys %{$item{$a}{container}}){
					if (!$first){
					$outbuffer{$client}.="$item{$a}{name} contains $item{$a}{container}{$b}";
					$first=1;
					}else {
					$outbuffer{$client}.=", $item{$a}{container}{$b}";
					}
				}
			}	
			return 1;
			}
		foreach $a (keys %{$room[$user{$client}{room}]{in}}){
				if ($user{$a}{name}=~/$input[1]/i){
					$outbuffer{$client}.="$user{$a}{desc}\r\n";
					my $b=int($user{$a}{vitality}/$user{$a}{vitalitymax}*100);
		my $string=&health2("$a");
		$string =~s/\[name\]/\u$user{$a}{pre}$user{$a}{name} is/i;
		$outbuffer{$client}.="$string";
					return 1;
					}
				}
		if (($a,$b)=locateitemhands($input[1])){
			$outbuffer{$client}.="$item{$a}{desc}\r\n$prompt";
			return 1;
			}
		if ($a=locateitembody($input[1])){
			$outbuffer{$client}.="$item{$a}{desc}\r\n$prompt";
			return 1;
			}
		$outbuffer{$client}.="I couldn't find what you were refering too, be sure to specifiy, look at/in/on/under/behind etc.\r\n$prompt";	
		}
sub locateroomplayer {
my $string=shift;
my $skip=shift;
foreach $a (keys %{$room[$user{$client}{room}]{in}}){
	if ($user{$a}{name}=~/$string/i){
		if ($skip){
			$skip--;
			next;
			}
		return $a;
		}
	}
return 0;		
}
sub unhide {
		if (&roundcheck) {
			return 0;
			}
		unless ($user{$client}{hiding} eq "yes"){
			$outbuffer{$client}.="You aren't hiding\r\n$prompt";
			return 1;
			}
		$outbuffer{$client}.="You come out of hiding\r\n$prompt";
		$user{$client}{hiding}=undef;
		}
sub hide {
		if (&roundcheck) {
			return 0;
			}
		if ($user{$client}{hiding} eq "yes"){
			$outbuffer{$client}.="You are already hiding\r\n$prompt";
			return 1;
			}
		unless ($room[$user{$client}{room}]{"hidingdifficulty"}){
			$outbuffer{$client}.="you can't hide here.\r\n$prompt";
			return 1;
			}
		if (defined($user{$client}{exp}{streetsmarts}{hiding})){
			if (rand($room[$user{$client}{room}]{hidedifficulty})<($user{$client}{exp}{streetsmarts}{hiding}{ranks} or 1)){
				$outbuffer{$client}.="You slip into the shadows stealthily\r\nIt took 5 seconds\r\n$prompt";
				$user{$client}{round}+=5;
				$user{$client}{hiding}="yes";
				my $e=(($room[$user{$client}{room}]{hidingdifficulty}-($user{$client}{exp}{streetsmarts}{hiding}{ranks}))*10);			
				if ($e<=0){
					$outbuffer{$client}.="You didn't find any challenge in that at all\r\n$prompt";
					#return 1;
					}
				else {
					addexp("hiding","streetsmarts",$e*10);
					
					}
				return 1;
				}
			else {
				$outbuffer{$client}.="You fail to slip into the shadows\r\n$prompt";
				$user{$client}{round}+=2;
				addexp("hiding","streetsmarts",$room[$user{$client}{room}]{hidingdifficulty}-$user{$client}{exp}{streetsmarts}{hiding}{ranks});		
				
				}
			}
		else {
			$outbuffer{$client}.="You fail to slip into the shadows.\r\n$prompt";
			addexp("hiding","streetsmarts",$room[$user{$client}{room}]{hidingdifficulty}*2);
			}
		}
sub north {
		
		if (defined($room[$user{$client}{room}]{exits}{North})){
move($room[$user{$client}{room}]{exits}{North}{dest},"$user{$client}{name} walks North","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub south {
		if (defined($room[$user{$client}{room}]{exits}{South})){
			move($room[$user{$client}{room}]{exits}{South}{dest},"$user{$client}{name} walks South","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub east {
		if (defined($room[$user{$client}{room}]{exits}{East})){
			move($room[$user{$client}{room}]{exits}{East}{dest},"$user{$client}{name} walks East","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub west {
		if (defined($room[$user{$client}{room}]{exits}{West})){
			move($room[$user{$client}{room}]{exits}{West}{dest},"$user{$client}{name} walks West","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub ne {
		if (defined($room[$user{$client}{room}]{exits}{NorthEast}{dest})){
			move($room[$user{$client}{room}]{exits}{NorthEast}{dest},"$user{$client}{name} walks North-East","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub se {
		if (defined($room[$user{$client}{room}]{exits}{SouthEast})){
			move($room[$user{$client}{room}]{exits}{SouthEast}{dest},"$user{$client}{name} walks South-East","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub nw {
		if (defined($room[$user{$client}{room}]{exits}{NorthWest})){
			move($room[$user{$client}{room}]{exits}{NorthWest}{dest},"$user{$client}{name} walks North-West",
			"$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub sw {
		if (defined($room[$user{$client}{room}]{exits}{SouthWest})){
			move($room[$user{$client}{room}]{exits}{SouthWest}{dest},"$user{$client}{name} walks South-West","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub out {
if (defined($room[$user{$client}{room}]{exits}{out})){
	move($room[$user{$client}{room}]{exits}{out}{dest},"$user{$client}{name} walks out","$user{$client}{name} walks in");
	}
else {
	$outbuffer{$client}.="You can't go that way\r\n$prompt";
	}	
}
sub quit {
		$outbuffer{$client}.="Thanx for playing, have fun now.\r\n";
		$client->send($outbuffer{$client},0);
		
		&delete($client);
		return;
		}


sub login {
	my $request=ucfirst("\L$input[0]");
	unless ($input[0]=~/^(['\w.]+$)/){
		#'
		$outbuffer{$client}.="possibly insecure data detected. Or invalid characters in name. Who are you again?\r\n$prompt";
		next;
		}
	$outbuffer{$client}.= "Hello, $request. ";
	unless (-e "players/$request"){
		$outbuffer{$client}.="I haven't seen you around here before\r\n";
		$outbuffer{$client}.="Ok, your gonna need a password then. please tell me it:";
		$user{$client}{name}=$request;
		$type{$client}="create1";
		}
	else {
		$outbuffer{$client}.= "What's your password?";
		$type{$client} ="password";
		$user{$client}{name}=$request;
		}
	return;
	}
sub create1 {
			$user{$client}{password}="$request";
			$outbuffer{$client}.="Very well, now tell me your element:\r\nValid Choices:\r\n1) Earth\r\n2) Air\r\n3) Water\r\n4) Fire\r\n5) Life\r\n";
			$type{$client}="create2";
			return;
			}
sub create2 {
	$request--;
	unless (defined($elements[$request])){
		$outbuffer{$client}.="Please choose from the list\r\n$prompt";
		return;
		}
	$user{$client}{element}="$request";
	$outbuffer{$client}.="Very well you are a $elements[$request] elemental then. You may choose one skill for a starting bonus. Choose wisely:\r\n1) Magic\r\n2) Weapons\r\n3) Armor\r\n4) Survival\r\n5) Streetsmarts\r\n6) Character points\r\n";
	$type{$client}="create3";
	return
	}
sub create3 {
	unless (loadplayer("new","$client")){
		print "error loading new\r\n";
	}
	if ($request==1){
		$outbuffer{$client}.="You recieve as your bonus 4 points of Primary Magic";
		addexp("General", "Magic", 3000, $client);
		$user{$client}{round}=0;
	}elsif ($request==2){
		$outbuffer{$client}.="You recieve as your bonus 4 points of General Weapons";
		addexp("general","weapons", 3000, $client);	
	}elsif ($request==3){	
		$outbuffer{$client}.="You recieve as your bonus 4 points of Armor";
		addexp("general", "Armor", 3000, $client);
	
	}elsif ($request==4){
		$outbuffer{$client}.="You recieve as your bonus 6 points of survival. 3 to foraging and 3 to skinning.";
		addexp("foraging", "Survival", 1600);							addexp("skinning", "Survival", 1600);
	}elsif ($request==5){
		$outbuffer{$client}.="You recieve as your bonus 4 points of Streetsmarts";
		addexp("perception","streetsmarts", 1600);						addexp("gambling","streetsmarts", 1600);
	}elsif ($request==6){
		$outbuffer{$client}.="You recieve as your bonus 30 Character Points";
		$user{$client}{cp}+=30;
	}else {
		$outbuffer{$client}.="huh?";
		return;
	}
			
	addtoroom("1",$client);
	$outbuffer{$client}.="\r\nWe're giving you some stats now. You can use your CP to train them more elsewhere.\r\nIn the future we'll give you some control here to, but for now everyone starts the same\r\n";
	createdone();
	return;
	}
sub createdone {
			$type{$client}="normal";
			say2("A young elemental spawns before your very eyes. Your spirit knows this elemental and it is called $user{$client}{name}.",$user{$client}{room},"",$client);
			look();
			return;
			}
sub password {
			unless (loadplayer("$user{$client}{name}","$client")){
				$outbuffer{$client}.="\r\nWhoops that was wrong who were you again?\r\n";
				$user{$client}="";
				$type{$client}="login";
				next;
				}
			else {
				unless ($request=~/^$user{$client}{password}$/i){
					$outbuffer{$client}.="Nope that's not your password\r\nWho are you?\r\n:";
					delete $user{$client};
					$type{$client}="login";
					return 0;
					}
				$outbuffer{$client}.="\r\nWelcome to the Northlands. \r\n";
				$type{$client}="normal";
				say("$user{$client}{name} apears in a flash of brilliant light.");
				$room[$user{$client}{room}]{in}{$client}=$user{$client}{name};
				look();
				return;
				}
			
			}
sub health {
$outbuffer{$client}.=&health2($client, 1);
}
sub health2 {
my $client=(shift or $client);
my $self=shift;
my $your;
if ($self){
	$your="Your";
}else {
	$your="Its";
	}
my $finalstring;
	if ($user{$client}{vitality}==0){
		$finalstring.="You are dead!";
		}
	else {
		my $a=int($user{$client}{vitality}/$user{$client}{vitalitymax}*100);
		my $string=$desc{vit}[$a];
		if ($self){
			$string=~s/\[name\]/You are/i;	
		}else {
			$string =~s/\[name\]/$user{$client}{pre}$user{$client}{name}/i;
			}
		$finalstring.="$string";
		}
	$finalstring.="\r\n";
	my $badhealth=0;
	foreach $a (keys %{$user{$client}{health}}){
		my $b=$user{$client}{health}{$a}{requires};
		if ($b){
			if ($user{$client}{health}{$b}{hp}>0){		
				if (my $line=healthcheck($a, $client)){
					$badhealth=1;
					$finalstring.="$your $a has $line. ";
					}
			}else {
				$finalstring.="$your $a is no longer attached. ";
			}
		}else {
			if (my $line=healthcheck($a, $client)){
				$badhealth=1;
				$finalstring.="$your $a has $line. ";
				}
			}
		}
		unless ($badhealth==1){
			$finalstring.="Perfect health\r\n";
			}
	return $finalstring;
	}
sub depart {
	playerdecay($client);
}
sub healthcheck {
my $a=shift;
my $client=(shift or $client);
my $line;
my $b="0";
my $c;
unless ($a){
	return;
}
unless ($user{$client}{health}{$a}{maxinthp}){
	next;
}
if ($user{$client}{health}{$a}{inthp}/$user{$client}{health}{$a}{maxinthp}<1){
	$c=int(($user{$client}{health}{$a}{inthp}/$user{$client}{health}{$a}{maxinthp})*100);
	$b=1;
	$line="internal wounds";
	}
if ($user{$client}{health}{$a}{intbleeding}){
	if ($b==1){
		$line.=" causing internal bleeding";
		}
	else {
		$b=1;
		$line= "internal bleeding ";
		}
	}
if ($user{$client}{health}{$a}{hp}/$user{$client}{health}{$a}{maxhp}<1){
	if ($b==1){
		my $a=int(($user{$client}{health}{$a}{hp}/$user{$client}{health}{$a}{maxhp})*100);
;		if ($a<0){
			$a=0;
		}
		$line.=" compounded by $desc{health}[$a]";
	}else {
		$b=1;
	my $a=int($user{$client}{health}{$a}{hp}/$user{$client}{health}{$a}{maxhp}*100);
		if ($a<0){
			$a=0;
		}
		if ($desc{health}[$a]){
			$line="$desc{health}[$a]";
		}else {
			$line = "$desc{health}[-1]";
			}
		}
	}
if ($user{$client}{health}{$a}{bleeding}){
	if ($b==1){
		$line.=" causing external bleeding";
		}
	else {
		$line= "External bleeding";
		}
	}
return $line;
}	
sub sit {
if (roundcheck()){
	return;
	}
if ($user{$client}{position} eq "sit"){
	$outbuffer{$client}.="You already are sitting\r\n$prompt";
	return;
	}
unless ($input[1]){
	say ("$user{$client}{name} sits down");
	$user{$client}{position} = "sit";
	$user{$client}{roomdesc}= "$user{$client}{name} is sitting here";
	return;
	}
if ($input[1] eq "on"){
	shift @input;
	}
#my $a=locateitemfloor($user{$client}{room},$input[1]);
my $a;
unless ($a=locateitemfloor($user{$client}{room},$input[1])){
	$outbuffer{$client}.="can't find that\r\n$prompt";
	return;
	}
unless ($a){
	return;
	}
if (defined($item{$a}{allowsit})){
	if (defined($item{$a}{sit})){
		&{$item{$a}{sit}};
		return;
		}
	else {
		say("$user{$client}{name} sits on $item{$a}{name}");
		$user{$client}{position} = "sit";
		$user{$client}{roomdesc}= "$user{$client}{name} is sitting on $item{$a}{name}";
		return;
		}
	}
else {
	$outbuffer{$client}.="You can't sit on that\r\n$prompt";
	return;
	}
}
sub stand {
if (roundcheck()){
	return;
	}
if (($user{$client}{position} eq "stand") or (!$user{$client}{position})){
	$outbuffer{$client}.="You already are standing\r\n$prompt";
	return;
	}
say ("$user{$client}{name} stands up");
$user{$client}{position} = "stand";
$user{$client}{roomdesc}= undef;
}
sub lay {
if (roundcheck()){
	return;
	}
if ($user{$client}{position} eq "lay"){
	$outbuffer{$client}.="You are already laying down.\r\n$prompt";
	return;
	}
say ("$user{$client}{name} lays down");
$user{$client}{position} = "lay";
$user{$client}{roomdesc}= "$user{$client}{name} is laying around lazily";
}
sub help {
unless ($input[1]){
	$input[1]="help";
	}
if ($input[1] eq "commands"){
	$outbuffer{$client}.="Current commands available to you are:\r\n @{$commandlist{$type{$client}}}";
	$outbuffer{$client}.="\r\nCurrent valid topics are:\r\n @{$commandlist{helptopics}}\r\n$prompt";
	return;
	}
if ($commands{helptopics}{$input[1]}) {
	do_help("help/$commands{helptopics}{$input[1]}");
	return;
	}
if ($commands{basic}{$input[1]}){
	do_help("help/$commands{basic}{$input[1]}");
	return;
	}
unless ($commands{$type{$client}}{$input[1]}){
	$outbuffer{$client}.="No help on that topic\r\n$prompt";
	return;
	}
unless (-e "help/$commands{$type{$client}}{$input[1]}"){
	$outbuffer{$client}.="No help for that command yet available\r\n$prompt";
	return;
	}
do_help("help/$commands{$type{$client}}{$input[1]}");
	
}
sub do_help {
my $a=shift;
unless (open(HELP, "$a")){
	$outbuffer{$client}.="error opening help file. $!";
	print "Error! error!\n\n";
	return;
	}
local $/;
unless (defined($a=<HELP>)){
	$outbuffer{$client}.="$!";
	}

$outbuffer{$client}.="$a\r\n$prompt";
close(HELP);
}
sub go {
if ($room[$user{$client}{room}]{exits}{$input[1]}{dest}){
			move($room[$user{$client}{room}]{exits}{$input[1]}{dest},"$user{$client}{name} walks out","$user{$client}{name} walks in");
			}
		else {
			$outbuffer{$client}.="You can't go that way\r\n$prompt";
			}	
		}
sub inventory{
my $a=$client;
my $e=0;
my $hold="";
my $line="";
my $f=0;
foreach $b (keys %{$user{$a}{health}}){
	foreach $c (keys %{$user{$a}{health}{$b}{wear}}){
		if ($user{$a}{health}{$b}{wear}{$c}){
			if ($e==1){
				$line.=", $item{$user{$a}{health}{$b}{wear}{$c}}{pre}$item{$user{$a}{health}{$b}{wear}{$c}}{name} on your $c";
				}
			else {
				$e=1;
				$line.="$item{$user{$a}{health}{$b}{wear}{$c}}{pre}$item{$user{$a}{health}{$b}{wear}{$c}}{name} on your $c";
				}
			}
		}
	if ($user{$a}{health}{$b}{hold}){
		if ($f==1){
			$hold.=", $item{$user{$a}{health}{$b}{hold}}{pre}$item{$user{$a}{health}{$b}{hold}}{name} in your $b";
			}
		else {
			$f=1;
			$hold.="$item{$user{$a}{health}{$b}{hold}}{pre}$item{$user{$a}{health}{$b}{hold}}{name} in your $b";
			}
		}
	}
if ($hold){
	$outbuffer{$client}.="You are holding $hold\r\n";	
	}

if ($line){
	$outbuffer{$client}.="You are wearing $line\r\n$prompt";
	}
else {
	$outbuffer{$client}.="You are naked!\r\n$prompt";
	}
}

sub get {
unless ($a=locateitemfloor($user{$client}{room},$input[1])){
	$outbuffer{$client}.="I can't find what your refering to.\r\n$prompt";
	return 0;
	}
if ($item{$a}{noget}){
	$outbuffer{$client}.="You can't get that.\r\n$prompt";
	return 0;
	}
$hold=&findopenhold;
unless ($hold){
	$outbuffer{$client}.="Your hands are full\r\n$prompt";
	return 0;
	}
removeitemfromroom($user{$client}{room},$a);
additemtoinventory($a,$hold);	
$outbuffer{$client}.="You pick up $item{$a}{pre}$item{$a}{name} with your $hold\r\n$prompt";
}
sub findopenhold {
my $client=(shift or $client);
foreach $a (@{$user{$client}{hold}}){
	if ($user{$client}{health}{$a}{hold}){
		next;
		}
	else {
		return $a;
		}
	return 0;
	}
}
sub additemtoinventory {
	$item=shift;
	$location=shift;
	$user{$client}{health}{$location}{hold}=$item;
	foreach $a (keys %{$item{$item}{holdeffect}}){
		$user{$client}{$a}+=$item{$item}{holdeffect}{$a};
	}
}
sub removeitemfrominventory{
	$location=shift;
	foreach $a (keys %{$item{$user{$client}{health}{$location}{hold}}{holdeffect}}){
		$user{$client}{$a}-=$item{$user{$client}{health}{$location}{hold}}{holdeffect}{$a};
	}
	$user{$client}{health}{$location}{hold}=undef;
}
sub locateitemhands {
$item=shift;
foreach $b (@{$user{$client}{hold}}){
	if ($user{$client}{health}{$b}{hold}) {
		if ($item{$user{$client}{health}{$b}{hold}}{name}=~/$item/i){
			if ($skip){
			$skip--;	
				}
			else {
				return ($user{$client}{health}{$b}{hold}, $b);
				}
			}
		}
	}
}

sub drop {
my @a;
my $item=$input[1];
@a=locateitemhands($item);
removeitemfrominventory($a[1]);
additemtoroom($user{$client}{room},$a[0]);
$outbuffer{$client}.="You drop $item{$a[0]}{name}.\r\n$prompt";
}
sub wear {
$item=$input[1];
my @a=locateitemhands($item);
unless ($a[0] and $a[1]){
	$outbuffer{$client}.="You aren't holding that.\r\n$prompt";
	return;
	}
@location=locatewearlocation($a[0]);	
unless ($location[0] and $location[1]){
	$outbuffer{$client}.="You have no where to wear that available.\r\n$prompt";
	return 0;
	}
removeitemfrominventory($a[1]);
additemtowear(@location,$a[0]);
$outbuffer{$client}.="You wear $item{$a[0]}{pre}$item{$a[0]}{name} on your $location[1].\r\n$prompt";
}
sub locatewearlocation {
my $item=shift;
unless ($item{$item}{wear}){
	return 0;
	}
foreach $a (keys %{$user{$client}{health}}){
	foreach $b (keys %{$user{$client}{health}{$a}{wear}}) {
		if ($b eq $item{$item}{wear}){
			if ($user{$client}{health}{$a}{wear}{$b}){
				next;
				}
			else {
				return ($a, $b);
				
				}
			}
		}
	}

}
sub additemtowear {
	$location=shift;
	$specific=shift;
	$item=shift;
	$user{$client}{health}{$location}{wear}{$specific}=$item;
	foreach $a (keys %{$item{$item}{weareffects}}){
		if (ref $item{$item}{weareffects}{$a}){
			foreach $b (keys %{$item{$item}{weareffects}{$a}}){
				$user{$client}{$a}{$b}+=$item{$item}{weareffects}{$a}{$b}
			}
		}else {
			$user{$client}{$a}+=$item{$item}{weareffects}{$a};	
		}
	}
}
sub unwear {
	my $item=$input[1];
	my @a=locateitembody($item);
	unless ($a[0] and $a[1]){
		$outbuffer{$client}.="Can't find what you were refering to\r\n$prompt";
		return;
		}
	my $a=&findopenhold;
	unless ($a){
		$outbuffer{$client}.="Your hands are full\r\n$prompt";
		return;
		}
	removefrombody(@a);
	
	additemtoinventory($a[2],$a);
	$outbuffer{$client}.="You remove $item{$a[2]}{pre}$item{$a[2]}{name} from your $a[0]\r\n$prompt";
	return 1;
	}
sub locateitembody {
$item=shift;
unless ($item){
return 0;
}
foreach $a (keys %{$user{$client}{health}}){
	foreach $b (keys %{$user{$client}{health}{$a}{wear}}) {
		if ($user{$client}{health}{$a}{wear}{$b}){
			if ($item{$user{$client}{health}{$a}{wear}{$b}}{name}=~/$item/i){
				return ($a,$b,$user{$client}{health}{$a}{wear}{$b});
				}
			}
		}
	}
}
sub removefrombody {
	my $hold=shift;
	my $hold2=shift;
	foreach $a (keys %{$item{$user{$client}{health}{$hold}{wear}{$hold2}}{weareffects}} ){
		if (ref $a){
			foreach $b (keys %{$item{$item}{weareffects}{$a}}){
				$user{$client}{$a}{$b}-=$item{$item}{weareffects}{$a}{$b}
			}
		
		}else {
			$user{$client}{$a}-=$item{$user{$client}{health}{$hold}{wear}{$hold2}}{weareffects}{$a};
		}
	}
	$user{$client}{health}{$hold}{wear}{$hold2}=undef;
return 1;
}
sub reloadcommand {
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
loadcommands();	
$outbuffer{$client}.="Commands reloaded\r\n$prompt";
}
sub reloadroom{
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
loadroom();	
$outbuffer{$client}.="room reloaded\r\n$prompt";
}
sub reloadmonster {
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
loadmonster();	
$outbuffer{$client}.="monsters reloaded\r\n$prompt";
}
sub reloaditem {
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
loaditems();	
$outbuffer{$client}.="items reloaded\r\n$prompt";
}
sub reload {
#this is really dangerous. really really dangerous, one misspelling and poof.
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
&{"reload$input[1]"};
}
sub donew {
unless (defined($user{$client}{god})){
	$outbuffer{$client}.="Huh?\r\n$prompt";
	return;
	}
if (newload($input[1])){
	$outbuffer{$client}.="Loaded $input[1]\r\n$prompt";
}else {
	$outbuffer{$client}.="failed\r\n$prompt";
	}
}
sub newload {
my $a=shift;
require("$a");
return 1;
}
sub engage {
if (&combatcheck($client)){
	$outbuffer{$client}.="Your already engaging something.\r\n$prompt";
	return;
	}
shift @input;
my $b=join(" ",@input);
my $skip=0;
my $a;
{
$a=locateroomplayer("$b",$skip);
unless ($a){
	$outbuffer{$client}.="I can't find that person\r\n$prompt";
	return;
	}
if (defined ($user{$a}{dead})){
	$skip++;
	redo;
	}
if (defined($user{$a}{noattack})){
	$skip++;
	next;
	}	
}	
$user{$client}{eng}{to}="$a";
$user{$client}{eng}{pos}=0;
if (defined($user{$a}{eng}{to}) and $user{$a}{eng}{to} eq $client){
	$user{$client}{eng}{distance}="$user{$a}{eng}{distance}";
}else {
	$user{$client}{eng}{distance}="missile";
}
unless ($user{$client}{eng}{distance} eq "melee"){
	my $line="You start to engage $user{$a}{pre}$user{$a}{name} from $user{$client}{eng}{distance} range\r\n$prompt";
	my $line2="$user{$client}{name} begins to advance on you from $user{$client}{eng}{distance}";
	my $line3="$user{$client}{name} advances toward $user{$a}{name} from $user{$client}{eng}{distance} range";
	say3($line3,$client,$a,$line,$line2);
	my $time=time+1+int(rand(10));
	push (@{$do{$time}},{sub=>"advance",args=>[$client]});
	}
else {
	$outbuffer{$client}.="You turn to face $user{$a}{pre}$user{$a}{name}\r\n";
	}
}
sub advance {
my $client=shift;
my $time=time;
unless ($a=&combatcheck($client)){
	return;
}
if ($a eq "melee"){
	return;
	}
if ($a eq "missile") {
	$user{$client}{eng}{distance}="pole";
	if ($user{$user{$client}{eng}{to}}{eng}{to} eq $client) {
		$user{$user{$client}{eng}{to}}{eng}{distance}="pole";
		}
	push (@{$do{$time+8+int(rand(10))}},{sub=>"advance",args=>[$client]});
	}
elsif ($a eq "pole"){
	if ($user{$user{$client}{eng}{to}}{eng}{to} eq $client) {
		$user{$user{$client}{eng}{to}}{eng}{distance}="melee";
		}
	
	$user{$client}{eng}{distance}="melee";
	}
unless (defined($user{$client}{pre})){
	$user{$client}{pre}="";
	}
my $string1="$user{$client}{pre}$user{$client}{name} closes to $user{$client}{eng}{distance} range on $user{$user{$client}{eng}{to}}{pre}$user{$user{$client}{eng}{to}}{name}";
my $string2="You close to $user{$client}{eng}{distance} range on $user{$user{$client}{eng}{to}}{pre}$user{$user{$client}{eng}{to}}{name}";
say2($string1,$user{$client}{room},$string2,$client);
}
sub set {
if ($input[1]=~/wid(t(h)?)?/i){
	if ($input[2]=~/^[0-9]+$/){
		if ($input[2]<15){
			$outbuffer{$client}.="Min size is 15 chars. ";
			$input[2]="15";
			}
		$user{$client}{screenwidth}=$input[2];
		$outbuffer{$client}.="Screenwidth set to $input[2]\r\n$prompt";
		}
	else {
		$outbuffer{$client}.="You must specify a numeric value\r\n$prompt";
		}
	}	
else {
	$outbuffer{$client}.="Current options:\r\n width:".($user{$client}{screenwidth} or "80")."\r\n$prompt";
	}
}
sub who {
$outbuffer{$client}.="Currently logged in people:\r\n";
$outbuffer{$client}.=sprintf("%-10.10s%-15.15s\n", "name","last command");
foreach $a ($select->handles){
	unless (defined $user{$a}){
		next;
		}
	$outbuffer{$client}.=sprintf("%-10.10s%-15.15s\n", $user{$a}{name},$lastrequest{$a});
#	$outbuffer{$client}.="$user{$a}{name}\r\n";
	}
my @a=glob("players/*");
$a=@a;
$outbuffer{$client}.="Currently $a total players.";
$outbuffer{$client}.="\r\n$prompt";
}

sub say3 {
my $message=shift;
my $client=shift;
my $client2=shift;
my $message2=shift;
my $message3=shift;
my $room=(shift or $user{$client}{room});
foreach $a (keys %{$room[$room]{in}}){
	if ($a eq "$client"){
		if ($message2){
			$outbuffer{$a}.="$message2";
			}
	}elsif ( $a eq "$client2"){
		if ($message3){
			$outbuffer{$a}.="$message3";
		}
	}else {
		$outbuffer{$a}.="$message";
		}
	}

}
return 1;