4 # remoteglot - Connects an abitrary UCI-speaking engine to ICS for easier post-game
5 # analysis, or for live analysis of relayed games. (Do not use for
6 # cheating! Cheating is bad for your karma, and your abuser flag.)
8 # Copyright 2007 Steinar H. Gunderson <sgunderson@bigfoot.com>
9 # Licensed under the GNU General Public License, version 2.
19 $SIG{ALRM} = sub { output_screen(); };
23 my $server = "freechess.org";
25 # my $engine = "/usr/games/toga2";
26 my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
28 open(FICSLOG, ">ficslog.txt")
29 or die "ficslog.txt: $!";
30 print FICSLOG "Log starting.\n";
34 open(UCILOG, ">ucilog.txt")
35 or die "ucilog.txt: $!";
36 print UCILOG "Log starting.\n";
41 # open the chess engine
42 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
55 uciprint("setoption name UCI_AnalyseMode value true");
56 uciprint("setoption name NalimovPath value c:\\nalimov");
57 uciprint("setoption name NalimovUsage value Rarely");
58 uciprint("setoption name Hash value 1024");
59 # uciprint("setoption name MultiPV value 3");
60 # uciprint("setoption name Contempt value 1000");
61 # uciprint("setoption name Outlook value Ultra Optimistic");
62 uciprint("ucinewgame");
64 print "Chess engine ready.\n";
67 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
68 $t->input_log(\*FICSLOG);
71 $t->waitfor('/Press return to enter the server/');
75 $t->cmd("set shout 0");
76 $t->cmd("set seek 0");
77 $t->cmd("set style 12");
78 $t->cmd("observe $target");
81 print "FICS ready.\n";
85 vec($rin, fileno(UCIREAD), 1) = 1;
86 vec($rin, fileno($t), 1) = 1;
88 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
92 my $line = $t->getline(Timeout => 0, errmode => 'return');
93 last if (!defined($line));
97 if ($line =~ /^<12> /) {
98 my $fen = style12_to_fen($line);
100 uciprint("position fen $fen");
101 uciprint("go infinite");
103 #print "FICS: [$line]\n";
107 # any fun on the UCI channel?
108 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
109 my $line = <UCIREAD>;
113 # don't update too often
114 Time::HiRes::alarm(0.2);
125 print UCILOG "<= $line\n";
126 if ($line =~ /^info/) {
127 my (@infos) = split / /, $line;
132 if ($line =~ /^id/) {
133 my (@ids) = split / /, $line;
144 while (scalar @x > 0) {
145 if ($x[0] =~ 'multipv') {
150 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
152 my $value = shift @x;
153 $uciinfo{$key} = $value;
156 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
158 my $value = shift @x;
159 $uciinfo{$key . $mpv} = $value;
162 if ($x[0] eq 'score') {
165 delete $uciinfo{'score_cp' . $mpv};
166 delete $uciinfo{'score_mate' . $mpv};
168 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
171 $uciinfo{'score_cp' . $mpv} = shift @x;
172 } elsif ($x[0] eq 'mate') {
174 $uciinfo{'score_mate' . $mpv} = shift @x;
182 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
185 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
189 #print "unknown info '$x[0]', trying to recover...\n";
191 die "Unknown info '" . join(',', @x) . "'";
199 while (scalar @x > 0) {
200 if ($x[0] =~ /^(name|author)$/) {
202 my $value = join(' ', @x);
203 $uciid{$key} = $value;
214 my (@x) = split / /, $str;
216 $ficsinfo{'board'} = [ @x[1..8] ];
217 $ficsinfo{'toplay'} = $x[9];
220 my (@board) = @x[1..8];
221 for my $rank (0..7) {
222 $board[$rank] =~ s/(-+)/length($1)/ge;
224 my $fen = join('/', @board);
226 # white/black to move
232 $castling .= "K" if ($x[11] == 1);
233 $castling .= "Q" if ($x[12] == 1);
234 $castling .= "k" if ($x[13] == 1);
235 $castling .= "q" if ($x[14] == 1);
236 $castling = "-" if ($castling eq "");
244 my $nep = (qw(a b c d e f g h))[$col];
253 # Showing the en passant square when actually no capture can be made
254 # seems to confuse at least Rybka. Thus, check if there's actually
255 # a pawn of the opposite side that can do the en passant move, and if
256 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
257 # "right" thing as per the standard, though.
260 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
261 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
263 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
264 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
282 my ($board, @pvs) = @_;
284 if (scalar @pvs == 0 || !defined($pvs[0])) {
291 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
292 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
293 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
294 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
297 my $piece = substr($board->[$from_row], $from_col, 1);
303 # white short castling
304 if ($pv eq 'e1g1' && $piece eq 'K') {
306 substr($nb[7], 4, 1, '-');
307 substr($nb[7], 6, 1, $piece);
310 substr($nb[7], 7, 1, '-');
311 substr($nb[7], 5, 1, 'R');
313 return ('0-0', prettyprint_pv(\@nb, @pvs));
316 # white long castling
317 if ($pv eq 'e1c1' && $piece eq 'K') {
319 substr($nb[7], 4, 1, '-');
320 substr($nb[7], 2, 1, $piece);
323 substr($nb[7], 0, 1, '-');
324 substr($nb[7], 3, 1, 'R');
326 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
329 # black short castling
330 if ($pv eq 'e8g8' && $piece eq 'k') {
332 substr($nb[0], 4, 1, '-');
333 substr($nb[0], 6, 1, $piece);
336 substr($nb[0], 7, 1, '-');
337 substr($nb[0], 5, 1, 'r');
339 return ('0-0', prettyprint_pv(\@nb, @pvs));
342 # black long castling
343 if ($pv eq 'e8c8' && $piece eq 'k') {
345 substr($nb[0], 4, 1, '-');
346 substr($nb[0], 2, 1, $piece);
349 substr($nb[0], 0, 1, '-');
350 substr($nb[0], 3, 1, 'r');
352 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
355 # check if the from-piece is a pawn
356 if (lc($piece) eq 'p') {
358 if ($from_col != $to_col) {
359 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
362 if (substr($board->[$to_row], $to_col, 1) eq '-') {
364 substr($nb[$to_row + 1], $to_col, 1, '-');
366 substr($nb[$to_row - 1], $to_col, 1, '-');
370 $pretty = substr($pv, 2, 2);
372 if (length($pv) == 5) {
375 $pretty .= uc(substr($pv, 4, 1));
378 $piece = substr($pv, 4, 1);
380 $piece = uc(substr($pv, 4, 1));
385 $pretty = uc($piece);
387 # see how many of these pieces could go here, in all
391 next unless (substr($board->[$row], $col, 1) eq $piece);
392 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
396 # see how many of these pieces from the given row could go here
399 next unless (substr($board->[$from_row], $col, 1) eq $piece);
400 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
403 # and same for columns
406 next unless (substr($board->[$row], $from_col, 1) eq $piece);
407 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
410 # see if we need to disambiguate
411 if ($num_total > 1) {
413 $pretty .= substr($pv, 0, 1);
414 } elsif ($num_row == 1) {
415 $pretty .= substr($pv, 1, 1);
417 $pretty .= substr($pv, 0, 2);
422 if (substr($board->[$to_row], $to_col, 1) ne '-') {
426 $pretty .= substr($pv, 2, 2);
430 substr($nb[$from_row], $from_col, 1, '-');
431 substr($nb[$to_row], $to_col, 1, $piece);
435 } elsif (in_check(\@nb) ne 'none') {
439 return ($pretty, prettyprint_pv(\@nb, @pvs));
446 # Check the PVs first. if they're invalid, just wait, as our data
447 # is most likely out of sync. This isn't a very good solution, as
448 # it can frequently miss stuff, but it's good enough for most users.
452 if (exists($uciinfo{'pv'})) {
453 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
457 while (exists($uciinfo{'pv' . $mpv})) {
458 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
466 print "
\e[H
\e[2J"; # clear the screen
467 if (exists($uciid{'name'})) {
468 print "Analysis by $uciid{'name'}:\n\n";
470 print "Analysis:\n\n";
473 return unless (exists($ficsinfo{'board'}));
475 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
478 while (exists($uciinfo{'pv' . $mpv})) {
479 printf " PV%2u", $mpv;
481 if (defined($uciinfo{'score_mate' . $mpv})) {
482 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
484 if (exists($uciinfo{'score_cp' . $mpv})) {
485 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
486 if ($ficsinfo{'toplay'} eq 'B') {
489 printf " (%+5.2f)", $score;
493 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
494 printf " (%5u kn, %3u kn/s, %2u ply)",
495 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
499 print " ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
505 # Some programs _always_ report MultiPV, even with only one PV.
506 # In this case, we simply use that data as if MultiPV was never
509 if (exists($uciinfo{'pv1'})) {
510 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
511 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
512 $uciinfo{$key} = $uciinfo{$key . '1'};
518 if (defined($uciinfo{'score_mate'})) {
519 my $mate = $uciinfo{'score_mate'};
520 if ($ficsinfo{'toplay'} eq 'B') {
524 printf " White mates in %u\n", $mate;
526 printf " Black mates in %u\n", -$mate;
529 if (exists($uciinfo{'score_cp'})) {
530 my $score = $uciinfo{'score_cp'} * 0.01;
531 if ($ficsinfo{'toplay'} eq 'B') {
534 printf " Score: %+5.2f\n", $score;
538 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
541 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
542 printf " %u nodes, %7u nodes/sec, depth %u ply",
543 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
545 if (exists($uciinfo{'tbhits'})) {
546 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
548 if (exists($uciinfo{'seldepth'})) {
549 printf " (%u selective)", $uciinfo{'seldepth'};
557 my ($wkr, $wkc, $bkr, $bkc);
561 my $piece = substr($board->[$row], $col, 1);
563 ($wkr, $wkc) = ($row, $col);
564 } elsif ($piece eq 'k') {
565 ($bkr, $bkc) = ($row, $col);
570 return ($wkr, $wkc, $bkr, $bkc);
575 my $check = in_check($board);
576 return 0 if ($check eq 'none');
578 # try all possible moves for the side in check
581 my $piece = substr($board->[$row], $col, 1);
582 next if ($piece eq '-');
584 if ($check eq 'white') {
585 next if ($piece eq lc($piece));
587 next if ($piece eq uc($piece));
590 for my $dest_row (0..7) {
591 for my $dest_col (0..7) {
592 next if ($row == $dest_row && $col == $dest_col);
593 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
596 substr($nb[$row], $col, 1, '-');
597 substr($nb[$dest_row], $dest_col, 1, $piece);
599 my $new_check = in_check(\@nb);
600 return 0 if ($new_check ne $check && $new_check ne 'both');
606 # nothing to do; mate
612 my ($black_check, $white_check) = (0, 0);
614 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
616 # check all pieces for the possibility of threatening the two kings
619 my $piece = substr($board->[$row], $col, 1);
620 next if ($piece eq '-');
622 if (uc($piece) eq $piece) {
624 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
627 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
632 if ($black_check && $white_check) {
634 } elsif ($black_check) {
636 } elsif ($white_check) {
644 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
646 # can't eat your own piece
647 my $dest_piece = substr($board->[$to_row], $to_col, 1);
648 if ($dest_piece ne '-') {
649 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
652 if (lc($piece) eq 'k') {
653 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
655 if (lc($piece) eq 'r') {
656 return 0 unless ($from_row == $to_row || $from_col == $to_col);
658 # check that there's a clear passage
659 if ($from_row == $to_row) {
660 if ($from_col > $to_col) {
661 ($to_col, $from_col) = ($from_col, $to_col);
664 for my $c (($from_col+1)..($to_col-1)) {
665 my $middle_piece = substr($board->[$to_row], $c, 1);
666 return 0 if ($middle_piece ne '-');
671 if ($from_row > $to_row) {
672 ($to_row, $from_row) = ($from_row, $to_row);
675 for my $r (($from_row+1)..($to_row-1)) {
676 my $middle_piece = substr($board->[$r], $to_col, 1);
677 return 0 if ($middle_piece ne '-');
683 if (lc($piece) eq 'b') {
684 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
686 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
687 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
689 my $r = $from_row + $dr;
690 my $c = $from_col + $dc;
692 while ($r != $to_row) {
693 my $middle_piece = substr($board->[$r], $c, 1);
694 return 0 if ($middle_piece ne '-');
702 if (lc($piece) eq 'n') {
703 my $diff_r = abs($from_row - $to_row);
704 my $diff_c = abs($from_col - $to_col);
705 return 1 if ($diff_r == 2 && $diff_c == 1);
706 return 1 if ($diff_r == 1 && $diff_c == 2);
710 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
711 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
714 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
715 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
719 if ($to_col == $from_col && $to_row == $from_row + 1) {
720 return ($dest_piece eq '-');
722 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
723 return ($dest_piece ne '-');
729 if ($to_col == $from_col && $to_row == $from_row - 1) {
730 return ($dest_piece eq '-');
732 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
733 return ($dest_piece ne '-');
744 print UCIWRITE "$msg\n";
745 print UCILOG "=> $msg\n";