]> git.sesse.net Git - remoteglot/blobdiff - remoteglot.pl
Print out who's doing the analysis.
[remoteglot] / remoteglot.pl
index 1bdfea08b8fa9f7169a9be157fad25f945341a0d..d457e9297eb3cb6c2135092db68cae7d2fd99eb5 100755 (executable)
@@ -41,6 +41,7 @@ select(STDOUT);
 # open the chess engine
 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
 my %uciinfo = ();
+my %uciid = ();
 my %ficsinfo = ();
 
 uciprint("uci");
@@ -48,6 +49,7 @@ uciprint("uci");
 # gobble the options
 while (<UCIREAD>) {
        /uciok/ && last;
+       handle_uci($_);
 }
 
 uciprint("setoption name UCI_AnalyseMode value true");
@@ -105,15 +107,7 @@ while (1) {
        # any fun on the UCI channel?
        if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
                my $line = <UCIREAD>;
-               chomp $line;
-               $line =~ tr/\r//d;
-               print UCILOG "<= $line\n";
-               if ($line =~ /^info/) {
-                       my (@infos) = split / /, $line;
-                       shift @infos;
-
-                       parse_infos(@infos);
-               }
+               handle_uci($line);
                $sleep = 0;
 
                # don't update too often
@@ -123,6 +117,26 @@ while (1) {
        sleep $sleep;
 }
 
+sub handle_uci {
+       my ($line) = @_;
+
+       chomp $line;
+       $line =~ tr/\r//d;
+       print UCILOG "<= $line\n";
+       if ($line =~ /^info/) {
+               my (@infos) = split / /, $line;
+               shift @infos;
+
+               parse_infos(@infos);
+       }
+       if ($line =~ /^id/) {
+               my (@ids) = split / /, $line;
+               shift @ids;
+
+               parse_ids(@ids);
+       }
+}
+
 sub parse_infos {
        my (@x) = @_;
        my $mpv = '';
@@ -179,6 +193,22 @@ sub parse_infos {
        }
 }
 
+sub parse_ids {
+       my (@x) = @_;
+
+       while (scalar @x > 0) {
+               if ($x[0] =~ /^(name|author)$/) {
+                       my $key = shift @x;
+                       my $value = join(' ', @x);
+                       $uciid{$key} = $value;
+                       last;
+               }
+
+               # unknown
+               shift @x;
+       }
+}
+
 sub style12_to_fen {
        my $str = shift; 
        my (@x) = split / /, $str;
@@ -254,6 +284,8 @@ sub prettyprint_pv {
        if (scalar @pvs == 0 || !defined($pvs[0])) {
                return ();
        }
+       
+       my @nb = @$board;
 
        my $pv = shift @pvs;
        my $from_col = ord(substr($pv, 0, 1)) - ord('a');
@@ -270,8 +302,6 @@ sub prettyprint_pv {
 
        # white short castling
        if ($pv eq 'e1g1' && $piece eq 'K') {
-               my @nb = @$board;
-
                # king
                substr($nb[7], 4, 1, '-');
                substr($nb[7], 6, 1, $piece);
@@ -285,8 +315,6 @@ sub prettyprint_pv {
 
        # white long castling
        if ($pv eq 'e1c1' && $piece eq 'K') {
-               my @nb = @$board;
-
                # king
                substr($nb[7], 4, 1, '-');
                substr($nb[7], 2, 1, $piece);
@@ -300,8 +328,6 @@ sub prettyprint_pv {
 
        # black short castling
        if ($pv eq 'e8g8' && $piece eq 'k') {
-               my @nb = @$board;
-
                # king
                substr($nb[0], 4, 1, '-');
                substr($nb[0], 6, 1, $piece);
@@ -315,8 +341,6 @@ sub prettyprint_pv {
 
        # black long castling
        if ($pv eq 'e8c8' && $piece eq 'k') {
-               my @nb = @$board;
-
                # king
                substr($nb[0], 4, 1, '-');
                substr($nb[0], 2, 1, $piece);
@@ -331,8 +355,17 @@ sub prettyprint_pv {
        # check if the from-piece is a pawn
        if (lc($piece) eq 'p') {
                # attack?
-               if (substr($board->[$to_row], $to_col, 1) ne '-') {
+               if ($from_col != $to_col) {
                        $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
+
+                       # en passant?
+                       if (substr($board->[$to_row], $to_col, 1) eq '-') {
+                               if ($piece eq 'p') {
+                                       substr($nb[$to_row + 1], $to_col, 1, '-');
+                               } else {
+                                       substr($nb[$to_row - 1], $to_col, 1, '-');
+                               }
+                       }
                } else {
                        $pretty = substr($pv, 2, 2);
 
@@ -394,7 +427,6 @@ sub prettyprint_pv {
        }
 
        # update the board
-       my @nb = @$board;
        substr($nb[$from_row], $from_col, 1, '-');
        substr($nb[$to_row], $to_col, 1, $piece);
 
@@ -431,7 +463,12 @@ sub output_screen {
                return;
        }
 
-       print  "\e[H\e[2JAnalysis:\n\n";
+       print "\e[H\e[2J"; # clear the screen
+       if (exists($uciid{'name'})) {
+               print "Analysis by $uciid{'name'}:\n\n";
+       } else {
+               print "Analysis:\n\n";
+       }
 
        return unless (exists($ficsinfo{'board'}));