#!/usr/bin/perl

#/*
#    fixed_hash.pl demonstrates the use of hashes in perl
#
#    Copyright (C) 2008 Timothy Jones
#
#    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 3 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, see <http://www.gnu.org/licenses/>.
#*/




$file="Deepblue06.epd";

open(IN, $file) or die "Can't read file\n";

@filearray = <IN>;
$num=@filearray;

for($i=0; $i<$num; $i++){

    $file2=">".($i+1000)."PIECES.txt";
   open(OUT, $file2) or die "Can't read file\n";

  
   @epd_sections=split(/\s+/, $filearray[$i]);
  # print "$epd_sections[0] \n";
   @ro=split('\/',$epd_sections[0]);

   $num2=@ro;
 
for($j=0; $j<$num2; $j++){
#    print "Move ".($i+1).":  Row ".(8-$j).":  $ro[$j] \n";
    @col=split(//,$ro[$j]);
    
    $num3=@col;
    $out="";
    $pos="";
#    print "\n";
$imat=-3.5;
#rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR 
    for($k=0; $k<$num3; $k++){
     #   print "$col[$k] ";
        
        for ($col[$k]) {
        if    (/1/)  {$out.="X"; $imat+=1; }     # do something
        elsif (/2/)  {$out.="XX";$imat+=2; }     # do something else
        elsif (/3/)  {$out.="XXX"; $imat+=3;}     # do something else
        elsif (/4/)  {$out.="XXXX";$imat+=4; }     # do something else
        elsif (/5/)  {$out.="XXXXX";$imat+=5; }     # do something els
        elsif (/6/)  {$out.="XXXXXX";$imat+=6; }     # do something else
        elsif (/7/)  {$out.="XXXXXXX";$imat+=7;   }     # do something else
        elsif (/8/)  {$out.="XXXXXXXX";$imat+=8; }     # do something else
        elsif (/p/)  {$pos.="object { O_Pawn translate < ".(7-$j).",0,-".$imat." >  texture{ T_Black }}"; $imat++; print OUT "$pos \n"; $pos=""; }
	elsif (/r/)  {$pos.="object { O_Rook translate < ".(7-$j).",0,-".$imat." >  texture{ T_Black }}"; $imat++; print OUT "$pos \n"; $pos=""; }
        elsif (/n/)  {$pos.="object { O_Knight translate < ".(7-$j).",0,-".$imat." > texture{ T_Black }}"; $imat++; print OUT"$pos \n"; $pos=""; }
        elsif (/b/)  {$pos.="object { O_Bishop translate < ".(7-$j).",0,-".$imat." > texture{ T_Black }}"; $imat++;  print OUT "$pos \n"; $pos="";}
        elsif (/q/)  {$pos.="object { O_Queen translate < ".(7-$j).",0,-".$imat." > texture{ T_Black }}"; $imat++;  print OUT "$pos \n"; $pos="";}
        elsif (/k/)  {$pos.="object { O_King translate < ".(7-$j).",0,-".$imat." >  texture{ T_Black }}"; $imat++;  print OUT "$pos \n"; $pos="";}
        elsif (/P/)  {$pos.="object { O_Pawn translate < ".(7-$j).",0,-".$imat." >  texture{ T_White }}"; $imat++; print OUT "$pos \n"; $pos=""; }
	elsif (/R/)  {$pos.="object { O_Rook translate < ".(7-$j).",0,-".$imat." >  texture{ T_White }}"; $imat++; print OUT "$pos \n"; $pos=""; }
        elsif (/N/)  {$pos.="object { O_Knight translate < ".(7-$j).",0,-".$imat." > texture{ T_White }}"; $imat++; print OUT"$pos \n"; $pos=""; }
        elsif (/B/)  {$pos.="object { O_Bishop translate < ".(7-$j).",0,-".$imat." > texture{ T_White }}"; $imat++;  print OUT "$pos \n"; $pos="";}
        elsif (/Q/)  {$pos.="object { O_Queen translate < ".(7-$j).",0,-".$imat." > texture{ T_White }}"; $imat++;  print OUT "$pos \n"; $pos="";}
        elsif (/K/)  {$pos.="object { O_King translate < ".(7-$j).",0,-".$imat." >  texture{ T_White }}"; $imat++;  print OUT "$pos \n"; $pos="";}


        else     {print "ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!" }     # default}
    }}

   }
 
   close(OUT);
#   print "\n  END GAME \n \n";

}


#open(IN, $file) or die "Can't read file\n";


$count0=0;
$count2=0;

for($z=0; $z<($num-1); $z++){

$file5=($z+1000)."PIECES.txt";
$file6=($z+1001)."PIECES.txt";

$file7=">".($z+1000)."HASH.txt";
open(OUT2, $file7) or die "Can't read file\n";
$file8=">".($z+1000)."MOVED1.txt";
open(OUT3, $file8) or die "Can't read file\n";
$file8=">".($z+1000)."MOVED2.txt";
open(OUT4, $file8) or die "Can't read file\n";




#$infile1="1037PIECES.txt";
#$infile2="1038PIECES.txt";

$infile1=$file5;
$infile2=$file6;

open(IN1, $file5) or die "Can't read file\n";
open(IN2, $file6) or die "Can't read file\n";

@ahash1=<IN1>;
@ahash2=<IN2>;

 #   while ( ($key, $value) = each(%hash1) ) {
 #       print "$key => $value\n";
 #   }

#}

#my %hash1;  my %hash2;
$hash0{$_}++ for @ahash1;
$hash1{$_}++ for @ahash1;
$hash1{$_}++ for @ahash2;
$hash2{$_}++ for @ahash2;

while ( ($key, $value) = each(%hash1) ) {
    @parts=split(/\s+/, $key);  #2=O_Piece #5 x,y,z #8 T_Black
    if($value>1){print OUT2 $key;}  ###### NO CHANGE BETWEEN
    if($value==1){                  ###### CHANGES BETWEEN FILES
         $id=$parts[2]." ".$parts[8];   #If no show up twice, gone
	if($hash0{$key}==1){        ###### IS IN FIRST FILE?
	    print OUT3 "$id $parts[5] \n"; #tart of first file \n";
#            $hash_move1 {$id}=$parts[5];          ###If iterated, in here
 #           $
            $count0++;}
        if($hash2{$key}==1){        ###### IS IN SECOND FILE?
             print OUT4 "$id $parts[5] \n"; #tart of first file \n";
#	    print OUT3 "$key is Part of second file \n";
  #          $hash_move2{$id}=$parts[5];   
            #$hash_test{$id}++;
	    $count1++;}

     }

}


%hash0 = ();
%hash1 = ();
%hash2 = ();
%hash_test = ();
$count0=0;
$count1=0;
close(OUT2);
close(OUT3);
close(OUT4);

}








for($z=0; $z<($num-1); $z++){

$file111="".($z+1000)."HASH.txt";
$file11=">".($z+1000)."FINAL.txt";
$fin=($z+1000)."FINAL.txt";
open(ALMOST, $file11) or die "Can't read file\n";

#cat $top.pov $file11 $fin 


$file18="".($z+1000)."MOVED1.txt";
open(IN3, $file18) or die "Can't read file\n";
$file19="".($z+1000)."MOVED2.txt";
open(IN4, $file19) or die "Can't read file\n";

@moved1=<IN3>;
@moved2=<IN4>;
$num_moved1=@moved1;
$num_moved2=@moved2;

#O_Pawn T_Black 5,0,0.5 


for($az=0; $az<$num_moved1; $az++){
$flag_match=0;
@m1parts=split(/\s+/, @moved1[$az]); 
@xyz=split(/,/,$m1parts[2]);
$positionx1="$xyz[0]*(1-clock)";
$positiony1="$xyz[1]*(1-clock)";
$positionz1="$xyz[2]*(1-clock)";


for($bz=0; $bz<$num_moved2; $bz++){
@m2parts=split(/\s+/, @moved2[$bz]); 
if($m2parts[0] eq $m1parts[0]){  if($m2parts[1] eq $m1parts[1]){
    
    @xyz2=split(/,/,$m2parts[2]);
    $positionx2="$xyz2[0]*(clock)";
    $positiony2="$xyz2[1]*(clock)";
    $positionz2="$xyz2[2]*(clock)";

    $positionx=$positionx1."+".$positionx2;
    $positiony=$positiony1."+".$positiony2;
    $positionz=$positionz1."+".$positionz2;
    print ALMOST "object { $m2parts[0] translate < $positionx, $positiony +1.0*sin(1*3.14159*clock) , $positionz > texture { $m2parts[1] }}\n";  $flag_match=100;}}

if($flag_match==0){  #NO MATCH
                     $positionx=$positionx1."+ 0*clock";
		     $positiony=$positiony1."+ 100*clock"; 
		     $positionz=$positionz1."+ 0*clock";
		     print ALMOST "object { $m1parts[0] translate < $positionx, $positiony, $positionz > texture{ $m1parts[1] }}\n";}
}}

system ("cat $fin >> $file111");
system("cat top.pov $file111 > $fin.pov");

#system("cat top.pov $fin > $fin.pov");
system("povray $fin.pov sample.ini Display=False");

close(ALMOST);
close(IN3);
close(IN4);
}



