#!/usr/local/bin/perl

print "Content-type:text/html\n\n";
&chugCGI(*input);


#set up (sideways) bit strings for each of the terrain types...
$tilepattern{0} = "00000000"; #space
$tilepattern{1} = "11111111"; #dirt
$tilepattern{2} = "01000000"; #rope
$tilepattern{3} = "01001001"; #ladder
$tilepattern{4} = "00001111"; #gold
$tilepattern{5} = "10110110"; #rock


print "<pre>\n";

for($i = 0; $i < 6; $i++){
#    print "tilepattern $i\n";
for($j = 0; $j < 8; $j++){
 #   print getTilePatternRow($i,$j);


}
  #  print "\n";
}
#print "\n\n\n\n";




for($y=0; $y < 20; $y++) {
	for($x=0; $x < 32; $x++) {
	    if(getTileValue($x,$y) ne ""){
		print ";($x,$y)=" . getTileValue($x,$y)."\n";
	    }	
	}
    }


for($y = 11; $y >= 0; $y--){

    #finish with 11 lines of blank

    $bufpf0left .= "\t.byte #%00000000\n";
    $bufpf1left .= "\t.byte #%00000000\n";
    $bufpf2left .= "\t.byte #%00000000\n";
    $bufpf0right .= "\t.byte #%00000000\n";
    $bufpf1right .= "\t.byte #%00000000\n";
    $bufpf2right .= "\t.byte #%00000000\n";
    }



#loop upsidown through the tiles....

for($y = 19; $y >= 0; $y--){

    #it's one line blank followed 8 lines of possible data

    $bufpf0left .= "\t.byte #%00000000\n";
    $bufpf1left .= "\t.byte #%00000000\n";
    $bufpf2left .= "\t.byte #%00000000\n";
    $bufpf0right .= "\t.byte #%00000000\n";
    $bufpf1right .= "\t.byte #%00000000\n";
    $bufpf2right .= "\t.byte #%00000000\n";

    for($p = 0; $p < 8; $p++){
	$bufpf0left .= "\t.byte #%00000000\n";

	$bufpf1left .=
	    "\t.byte #%".
		bitForTileRow(0,$y,$p).
		    bitForTileRow(1,$y,$p).
			bitForTileRow(2,$y,$p).
			    bitForTileRow(3,$y,$p).
				bitForTileRow(4,$y,$p).
				    bitForTileRow(5,$y,$p).
					bitForTileRow(6,$y,$p).
					    bitForTileRow(7,$y,$p).
						"\n";
     $bufpf2left .=
            "\t.byte #%".
                bitForTileRow(15,$y,$p).
                    bitForTileRow(14,$y,$p).
                        bitForTileRow(13,$y,$p).
                            bitForTileRow(12,$y,$p).
                                bitForTileRow(11,$y,$p).
                                    bitForTileRow(10,$y,$p).
                                        bitForTileRow(9,$y,$p).
                                            bitForTileRow(8,$y,$p).
                                                "\n";


        $bufpf0right .= 
	    "\t.byte #%".
		bitForTileRow(19,$y,$p).
		    bitForTileRow(18,$y,$p).
			bitForTileRow(17,$y,$p).
			    bitForTileRow(16,$y,$p).
				"0000\n";
	

	$bufpf1right .=
	    "\t.byte #%".
		bitForTileRow(20,$y,$p).
		    bitForTileRow(21,$y,$p).
			bitForTileRow(22,$y,$p).
			    bitForTileRow(23,$y,$p).
				bitForTileRow(24,$y,$p).
				    bitForTileRow(25,$y,$p).
					bitForTileRow(26,$y,$p).
					    bitForTileRow(27,$y,$p).
						"\n";


        $bufpf2right .= 
	    "\t.byte #%".
		bitForTileRow(31,$y,$p).
		    bitForTileRow(30,$y,$p).
			bitForTileRow(29,$y,$p).
			    bitForTileRow(28,$y,$p).
				"0000\n";


    }
    


}




$data =  <<__EOQ__;

PFData0Left
$bufpf0left

PFData0Right
$bufpf0right

PFData1Left
$bufpf1left

PFData1Right
$bufpf1right

PFData2Left
$bufpf2left

PFData2Right
$bufpf2right

__EOQ__



    $vcs = "";
    open(READ,"vcs.h");
while(defined($next=<READ>)){
    $vcs .= $next;
}
close READ;


    open(READ, "asm.txt");
while($next = <READ>){
    $next =~ s/\;\[\[DATA\]\]\;/$data/;
    $next =~ s/\[\[vcs\.h\]\]/$vcs/;
    print $next;
}
close READ;












sub getTileValue{
    my($x,$y) = @_;
    return $input{"x".$x."y".$y};
}




sub bitForTileRow{
    my($x,$y,$l) = @_;
    if(getTileValue($x,$y) ne "" && getTileValue($x,$y) ne "0"){
	return getTilePatternRow( getTileValue($x,$y),7-$l);
    } else {
	return 0;
    }
    
}

sub getTilePatternRow {
    my($tilenum,$row) = @_;

    return substr(($tilepattern{$tilenum}),$row,1);

}



sub chugCGI { local (*in) = @_ if @_; local ($i, $key, $val);
if($ENV{'REQUEST_METHOD'}eq"POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
 }else {$in = $ENV{'QUERY_STRING'};} @in = split(/&/,$in);
foreach $i (0 .. $#in) { $in[$i] =~ s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2);$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge; $in{$key}.= $val;}return length($in);}

