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'})) {
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 if (defined($uciinfo{'score_mate'})) {
506 my $mate = $uciinfo{'score_mate'};
507 if ($ficsinfo{'toplay'} eq 'B') {
511 printf " White mates in %u\n", $mate;
513 printf " Black mates in %u\n", -$mate;
516 if (exists($uciinfo{'score_cp'})) {
517 my $score = $uciinfo{'score_cp'} * 0.01;
518 if ($ficsinfo{'toplay'} eq 'B') {
521 printf " Score: %+5.2f\n", $score;
525 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
528 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
529 printf " %u nodes, %7u nodes/sec, depth %u ply",
530 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
532 if (exists($uciinfo{'tbhits'})) {
533 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
535 if (exists($uciinfo{'seldepth'})) {
536 printf " (%u selective)", $uciinfo{'seldepth'};
544 my ($wkr, $wkc, $bkr, $bkc);
548 my $piece = substr($board->[$row], $col, 1);
550 ($wkr, $wkc) = ($row, $col);
551 } elsif ($piece eq 'k') {
552 ($bkr, $bkc) = ($row, $col);
557 return ($wkr, $wkc, $bkr, $bkc);
562 my $check = in_check($board);
563 return 0 if ($check eq 'none');
565 # try all possible moves for the side in check
568 my $piece = substr($board->[$row], $col, 1);
569 next if ($piece eq '-');
571 if ($check eq 'white') {
572 next if ($piece eq lc($piece));
574 next if ($piece eq uc($piece));
577 for my $dest_row (0..7) {
578 for my $dest_col (0..7) {
579 next if ($row == $dest_row && $col == $dest_col);
580 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
583 substr($nb[$row], $col, 1, '-');
584 substr($nb[$dest_row], $dest_col, 1, $piece);
586 my $new_check = in_check(\@nb);
587 return 0 if ($new_check ne $check && $new_check ne 'both');
593 # nothing to do; mate
599 my ($black_check, $white_check) = (0, 0);
601 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
603 # check all pieces for the possibility of threatening the two kings
606 my $piece = substr($board->[$row], $col, 1);
607 next if ($piece eq '-');
609 if (uc($piece) eq $piece) {
611 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
614 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
619 if ($black_check && $white_check) {
621 } elsif ($black_check) {
623 } elsif ($white_check) {
631 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
633 # can't eat your own piece
634 my $dest_piece = substr($board->[$to_row], $to_col, 1);
635 if ($dest_piece ne '-') {
636 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
639 if (lc($piece) eq 'k') {
640 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
642 if (lc($piece) eq 'r') {
643 return 0 unless ($from_row == $to_row || $from_col == $to_col);
645 # check that there's a clear passage
646 if ($from_row == $to_row) {
647 if ($from_col > $to_col) {
648 ($to_col, $from_col) = ($from_col, $to_col);
651 for my $c (($from_col+1)..($to_col-1)) {
652 my $middle_piece = substr($board->[$to_row], $c, 1);
653 return 0 if ($middle_piece ne '-');
658 if ($from_row > $to_row) {
659 ($to_row, $from_row) = ($from_row, $to_row);
662 for my $r (($from_row+1)..($to_row-1)) {
663 my $middle_piece = substr($board->[$r], $to_col, 1);
664 return 0 if ($middle_piece ne '-');
670 if (lc($piece) eq 'b') {
671 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
673 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
674 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
676 my $r = $from_row + $dr;
677 my $c = $from_col + $dc;
679 while ($r != $to_row) {
680 my $middle_piece = substr($board->[$r], $c, 1);
681 return 0 if ($middle_piece ne '-');
689 if (lc($piece) eq 'n') {
690 my $diff_r = abs($from_row - $to_row);
691 my $diff_c = abs($from_col - $to_col);
692 return 1 if ($diff_r == 2 && $diff_c == 1);
693 return 1 if ($diff_r == 1 && $diff_c == 2);
697 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
698 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
701 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
702 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
706 if ($to_col == $from_col && $to_row == $from_row + 1) {
707 return ($dest_piece eq '-');
709 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
710 return ($dest_piece ne '-');
716 if ($to_col == $from_col && $to_row == $from_row - 1) {
717 return ($dest_piece eq '-');
719 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
720 return ($dest_piece ne '-');
731 print UCIWRITE "$msg\n";
732 print UCILOG "=> $msg\n";