mud/help/
mud/map/
mud/monsters/
mud/players/
sub loadattacks{
print "loading attacks\r\n";
%attack=loadfile("attacks.xml");
}
sub loadfile {
my $file=shift;
open(FILE, "$file");
local $/;
my $a=<FILE>;
my $hash=load_decend($a);
return %{$hash};
}
sub loadcommands {
print "loading commands\r\n";
unless (open(COMMANDS, "commands.lst")){
	print "can't open the commands file.";
	die;
	}
$/="\n";
if (defined(%command)){
	%command="";
}
while ($a=<COMMANDS>){
	chomp($a);
	if ($a=~/<commandset name=(\w*) failed="(.*?[^\\])">/i){
		$type=$1;
		$commandtype{$type}{failed}=$2;
		next;
		}
	if ($a=~/<commandset name=(\w*) type=(.*) sub=(.*)>/i){
		$commandtype{$1}{$2}=$3;
		next;
		}
	if ($a=~/<\/commandset>/i){
		my @a=sort(keys(%command));
		undef %command;
		$commandlist{$type}=[@a];
		$type=undef;
		next;
		}
	if ($a=~/<([^\/].*)>/i){
		$command=$1;
		$command{$command}=$command;
		next;
		}
	if (defined($command) and ($a=~/<\/$command>/i)){
		$command=undef;
		next;
		}
	$commands{$type}{$a}=$command;
	}
close (COMMANDS);
}
sub loadmonster {	
print "loading monsters\r\n";
$/="</monster>";
$monstertotal=1;

unless (open(MONSTER, "monster.xml")){
	print "can't open the monster file.";
	die;
	}
while ($a=<MONSTER>){
	unless ($a=~s/<monster num=(\d+)>\n?//i){
		print"bad monster #, probably done loading monsters.\r\n";
		next;
		}
	my $monster=$1;

	while($a=~s/<adjective>\n?(.*?)\n?<\/adjective>\n?//is){
		push (@{$monster[$monster]{adjectives}}, $1);
		}
	while ($a=~/<(.*?)>(.*?)<\/\1>/isg){
		$monster[$monster]{$1}=$2;	
		}
	while ($a=~/<(.*?)=(.*?)>/ig){
		$monster[$monster]{$1}=$2;	
		}
	}
}	
sub loadroom{
print "loading rooms\r\n";
my $filename=shift;
$/="</room>";
foreach $map (($filename or glob("map/map*.xml"))){
	print "$map";
	unless (open(MAP, "$map")){
		print "can't open the mapfile.";
		die;
		}
	while ($a=<MAP>){
		unless ($a=~s/<num=(\d+)>//i){
			print"bad room #, probably done loading map.\r\n";
			next;
			}
		my $room=$1;
		$a=~s/<room>//i;
		$a=~s/<\room>//i;
		$room[$room]=load_decend($a);
		#print keys %{$room[$room]};
		print each %{$room[$room]{longdesc}};
		}
	close MAP;
	}
}
sub loaditems {
print "loading items\r\n";
$/="</item>";
unless (open(ITEM, "item.xml")){
	print "can't open the mapfile.";
	die;
	}
while ($a=<ITEM>){
	unless ($a=~s/<item num=(.*?)>//i){
		print "bad item #, probably done loading items.\r\n";
		next;
		}
	my $item=$1;
	$itemtemplate{$item}=load_decend($a);
	}
close ITEM;
}
sub loadmuddata{
unless (open(MUD, "muddata.xml")){
		print "can't open the muddata file $a: $!.";
		next;
		}	
local $/;
$a=<MUD>;
while ($a=~/<(.*?)>(.*?)<\/\1>/isg){
	${$1}=$2;	
	}
while ($a=~/<(.*?)=(.*?)>/ig){
	${$1}=$2;	
	}	
close MUD;
}
sub savemuddata{
unless (open(MUD, "muddata.xml")){
		print "can't open the muddata file $a: $!.";
		next;
		}	
local $/;
my $a=<MUD>;
close MUD;
$a=~s/<itemtotal=.*?>/<itemtotal=$itemtotal>/is;
unless (open(MUD, ">muddata.xml")){
		print "can't open the muddata file $a: $!.";
		next;
		}	
print MUD $a;
close MUD;
}		
sub loadrealitems {
print "loading real items\r\n";
local $/;
my $a;
foreach $a (glob("items/*")){
	unless (open(ITEM, "$a")){
		print "can't open the realitem file $a: $!.";
		next;
		}
$item=$a;
$item=~s/items\///i;
	while ($a=<ITEM>){
		while ($a=~/<(.*?)>(.*?)<\/\1>/isg){
			$item{$item}{$1}=$2;	
			}
		while ($a=~/<(.*?)=(.*?)>/ig){
			$item{$item}{$1}=$2;	
			}
		}
	close ITEM;
	}
}
sub saverealitem {
$item=shift;
print "saving $item\n\n";
unless (open(ITEM, ">items/$item")){
	print "can't save realitem!!!! $!";
	return 0;
	}
local $string;
foreach $a (keys %{$item{$item}}){
	descend($item{$item}{$a},$a);
	}
print ITEM $string;
close ITEM;
return 1;
}	
sub loadplayer {
	my $user=shift;
	my $user2=shift;
	my $dir=(shift or "players");
	unless (open(FILE,"$dir/$user")){
		return 0;
		}
	local $/;
	my $c=<FILE>;
	close(FILE);
	if (defined($user{$user2})){
		$userold=$user{$user2};
		$user{$user2}=load_decend($c);	
		%{$user{$user2}}=(%{$userold},%{$user{$user2}});
	}else {
		$user{$user2}=load_decend($c);
	}
	print keys %{$user{$user2}};
	print "\n";
	foreach $body_part (keys %{$user{$user2}{health}}){
		foreach $a (keys %{$user{$user2}{health}{$body_part}{wear}}){
			if ($user{$user2}{health}{$body_part}{wear}{$a}=~/#(\d*)/i){
				print "make wear $1\n";
				$user{$user2}{health}{$body_part}{wear}{$a}=makeitem($1);
			}
		}
		if (defined($user{$user2}{health}{$body_part}{weapon})){
			if ($user{$user2}{health}{$body_part}{weapon}=~/#(\d*)/i){
				print "make weapon $1\n";
				$user{$user2}{health}{$body_part}{weapon}=makeitem($1);
				}
			}
		if (defined($user{$user2}{health}{$body_part}{hold})){
			push (@{$user{$user2}{hold}}, $body_part);
			if ($user{$user2}{health}{$body_part}{hold}=~/#(\d*)/i){
				print "make item $1\n";
				$user{$user2}{health}{$body_part}{hold}=makeitem($1);
				}
			}
		}
	return 1;
}


sub loaddesc {
	print "loading descriptions\r\n";
	unless (open(DESC, "describe.lst")){
		print "can't open the commands file.";
		die;
		}
	local $/;
	my $a=<DESC>;
	close DESC;
	while ($a=~s/<(.*?)>(.*?)<\/\1>//is){
		my $desc=$1;
		@{$desc{$desc}}=split(/\n/,$2);
	}
}
sub load_decend {
	my $string=shift;
	my %hash;
	my $a;
	while($string=~s/<(.*?)>(.*?)<\/\1>\n?//is){
		$a=1;
		my $b=$1;
		$hash{$b}=load_decend($2);
		if ($b=~/>/s){
			print "error1 $b, $c";
		}
	}
	while($string=~s/<([^>]*?)=([^>]*?)>\n?//is){
		$a=1;
		my $b=$1;
		$hash{$b}=$2;
		if ($b=~/>/s){
			print "error2 $b, $c, $&";
		}
	}
	unless ($a){
#		print "returning $string";
		return $string;
		}
	return \%hash;
		
}

return 1;