#!/usr/athena/bin/perl

$db = "/mit/mkgray/project/dnd/encounterdb";
if($ARGV[0] eq "-html"){
    shift(@ARGV);
    $htmlmode = 1;
}
open(DB, $db);

$env = $ARGV[0];
$clim = $ARGV[1];
$when = $ARGV[2];

while(<DB>){
    chop;
    ($monster, $e, $c, $f, $w) = split(/\//);
    if(($e =~ /$env/) || ($e eq "Any")){
	if(($c =~ /$clim/) || ($c eq "Any")){
	    if(($w =~ /$when/) || ($w eq "Any")){
		$monst{$monster}=1;
		if($f eq "C"){
		    push(@common, $monster);
		}
		elsif($f eq "UC"){
		    push(@uncommon, $monster);
		}
		elsif($f eq "R"){
		    push(@rare, $monster);
		}
		elsif($f eq "VR"){
		    push(@veryrare, $monster);
		}
	    }
	}
    }
}

if($htmlmode){
    print("<table border>\n");
}
else{
print("Monsters for Climate=$clim Terrain=$env Activity=$when\n");
print("Common: ", $#common+1, "\n");
print("Unommon: ", $#uncommon+1, "\n");
print("Rare: ", $#rare+1, "\n");
print("Very Rare: ", $#veryrare+1, "\n");
}

$per = int((65/($#common+1))+1);

#print("Per $per\n");

$post = $to = 0;
srand;
while($to < 65){
    $post = $to;
    $to = $post+$per;
    $post++;
    $findm = 1;
    while($findm){
	$r = int(rand()*($#common+1));
#	print("Monster $common[$r]\n");
	if($monst{$common[$r]}){
	    $m = $common[$r];
	    $findm = 0;
	    $monst{$common[$r]} = 0;
	}
    }
    if($htmlmode){
	print("<tr><td align=right>$post - $to</td><td>$m</td></tr>\n") unless $post==$to;
	print("<tr><td align=right>$post</td><td>$m</td></tr>\n") if $post==$to;
    }
    else{
	print("$post - $to\t$m\n") unless $post==$to;
	print("$post     \t$m\n") if $post==$to;
    }
}

$per = int((20/($#uncommon+1))+1);

#print("Per $per\n");

srand;
while($to < 85){
    $post = $to;
    $to = $post+$per;
    $post++;
    $findm = 1;
    while($findm){
	$r = int(rand()*($#uncommon+1));
#	print("Monster $common[$r]\n");
	if($monst{$uncommon[$r]}){
	    $m = $uncommon[$r];
	    $findm = 0;
	    $monst{$uncommon[$r]} = 0;
	}
    }
    if($htmlmode){
	print("<tr><td align=right>$post - $to</td><td>$m</td></tr>\n") unless $post==$to;
	print("<tr><td align=right>$post</td><td>$m</td></tr>\n") if $post==$to;
    }
    else{
	print("$post - $to\t$m\n") unless $post==$to;
	print("$post     \t$m\n") if $post==$to;
    }
}

$per = int((10/($#rare+1))+1);

#print("Per $per\n");

srand;
while($to < 95){
    $post = $to;
    $to = $post+$per;
    $post++;
    $findm = 1;
    while($findm){
	$r = int(rand()*($#rare+1));
#	print("Monster $rare[$r]\n");
	if($monst{$rare[$r]}){
	    $m = $rare[$r];
	    $findm = 0;
	    $monst{$rare[$r]} = 0;
	}
    }
    if($htmlmode){
	print("<tr><td align=right>$post - $to</td><td>$m</td></tr>\n") unless $post==$to;
	print("<tr><td align=right>$post</td><td>$m</td></tr>\n") if $post==$to;
    }
    else{
	print("$post - $to\t$m\n") unless $post==$to;
	print("$post     \t$m\n") if $post==$to;
    }
}
$per = int((5/($#veryrare+1))+1);

#print("Per $per\n");

srand;
while($to < 100){
    $post = $to;
    $to = $post+$per;
    $post++;
    $findm = 1;
    while($findm){
	$r = int(rand()*($#veryrare+1));
#	print("Monster $common[$r]\n");
	if($monst{$veryrare[$r]}){
	    $m = $veryrare[$r];
	    $findm = 0;
	    $monst{$veryrare[$r]} = 0;
	}
    }
    if($to == 100) { $to = "00"; }
    if($post == 100) { $post = "00"; }
    if($htmlmode){
	print("<tr><td align=right>$post - $to</td><td>$m</td></tr>\n") unless $post==$to;
	print("<tr><td align=right>$post</td><td>$m</td></tr>\n") if $post==$to;
    }
    else{
	print("$post - $to\t$m\n") unless $post==$to;
	print("$post     \t$m\n") if $post==$to;
    }
    if($to eq "00") {last; }
}
print("</table>\n") if $htmlmode;



