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.
20 my $server = "freechess.org";
22 # my $engine = "/usr/games/toga2";
23 my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
26 $SIG{ALRM} = sub { output_screen(); };
30 open(FICSLOG, ">ficslog.txt")
31 or die "ficslog.txt: $!";
32 print FICSLOG "Log starting.\n";
36 open(UCILOG, ">ucilog.txt")
37 or die "ucilog.txt: $!";
38 print UCILOG "Log starting.\n";
43 # open the chess engine
44 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
57 uciprint("setoption name UCI_AnalyseMode value true");
58 uciprint("setoption name NalimovPath value c:\\nalimov");
59 uciprint("setoption name NalimovUsage value Rarely");
60 uciprint("setoption name Hash value 1024");
61 uciprint("setoption name MultiPV value 3");
62 # uciprint("setoption name Contempt value 1000");
63 # uciprint("setoption name Outlook value Ultra Optimistic");
64 uciprint("ucinewgame");
66 print "Chess engine ready.\n";
69 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
70 $t->input_log(\*FICSLOG);
73 $t->waitfor('/Press return to enter the server/');
77 $t->cmd("set shout 0");
78 $t->cmd("set seek 0");
79 $t->cmd("set style 12");
80 $t->cmd("observe $target");
83 print "FICS ready.\n";
87 vec($rin, fileno(UCIREAD), 1) = 1;
88 vec($rin, fileno($t), 1) = 1;
90 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
94 my $line = $t->getline(Timeout => 0, errmode => 'return');
95 last if (!defined($line));
99 if ($line =~ /^<12> /) {
100 my $fen = style12_to_fen($line);
102 uciprint("position fen $fen");
103 uciprint("go infinite");
106 # Output a command every move to note that we're
107 # still paying attention -- this is a good tradeoff,
108 # since if no move has happened in the last half
109 # hour, the analysis/relay has most likely stopped
110 # and we should stop hogging server resources.
114 #print "FICS: [$line]\n";
118 # any fun on the UCI channel?
119 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
120 my $line = <UCIREAD>;
124 # don't update too often
125 Time::HiRes::alarm(0.2);
136 print UCILOG "<= $line\n";
137 if ($line =~ /^info/) {
138 my (@infos) = split / /, $line;
143 if ($line =~ /^id/) {
144 my (@ids) = split / /, $line;
155 while (scalar @x > 0) {
156 if ($x[0] =~ 'multipv') {
161 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
163 my $value = shift @x;
164 $uciinfo{$key} = $value;
167 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
169 my $value = shift @x;
170 $uciinfo{$key . $mpv} = $value;
173 if ($x[0] eq 'score') {
176 delete $uciinfo{'score_cp' . $mpv};
177 delete $uciinfo{'score_mate' . $mpv};
179 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
182 $uciinfo{'score_cp' . $mpv} = shift @x;
183 } elsif ($x[0] eq 'mate') {
185 $uciinfo{'score_mate' . $mpv} = shift @x;
193 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
196 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
200 #print "unknown info '$x[0]', trying to recover...\n";
202 die "Unknown info '" . join(',', @x) . "'";
210 while (scalar @x > 0) {
211 if ($x[0] =~ /^(name|author)$/) {
213 my $value = join(' ', @x);
214 $uciid{$key} = $value;
225 my (@x) = split / /, $str;
227 $ficsinfo{'board'} = [ @x[1..8] ];
228 $ficsinfo{'toplay'} = $x[9];
231 my (@board) = @x[1..8];
232 for my $rank (0..7) {
233 $board[$rank] =~ s/(-+)/length($1)/ge;
235 my $fen = join('/', @board);
237 # white/black to move
243 $castling .= "K" if ($x[11] == 1);
244 $castling .= "Q" if ($x[12] == 1);
245 $castling .= "k" if ($x[13] == 1);
246 $castling .= "q" if ($x[14] == 1);
247 $castling = "-" if ($castling eq "");
255 my $nep = (qw(a b c d e f g h))[$col];
264 # Showing the en passant square when actually no capture can be made
265 # seems to confuse at least Rybka. Thus, check if there's actually
266 # a pawn of the opposite side that can do the en passant move, and if
267 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
268 # "right" thing as per the standard, though.
271 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
272 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
274 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
275 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
293 my ($board, @pvs) = @_;
295 if (scalar @pvs == 0 || !defined($pvs[0])) {
302 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
303 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
304 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
305 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
308 my $piece = substr($board->[$from_row], $from_col, 1);
314 # white short castling
315 if ($pv eq 'e1g1' && $piece eq 'K') {
317 substr($nb[7], 4, 1, '-');
318 substr($nb[7], 6, 1, $piece);
321 substr($nb[7], 7, 1, '-');
322 substr($nb[7], 5, 1, 'R');
324 return ('0-0', prettyprint_pv(\@nb, @pvs));
327 # white long castling
328 if ($pv eq 'e1c1' && $piece eq 'K') {
330 substr($nb[7], 4, 1, '-');
331 substr($nb[7], 2, 1, $piece);
334 substr($nb[7], 0, 1, '-');
335 substr($nb[7], 3, 1, 'R');
337 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
340 # black short castling
341 if ($pv eq 'e8g8' && $piece eq 'k') {
343 substr($nb[0], 4, 1, '-');
344 substr($nb[0], 6, 1, $piece);
347 substr($nb[0], 7, 1, '-');
348 substr($nb[0], 5, 1, 'r');
350 return ('0-0', prettyprint_pv(\@nb, @pvs));
353 # black long castling
354 if ($pv eq 'e8c8' && $piece eq 'k') {
356 substr($nb[0], 4, 1, '-');
357 substr($nb[0], 2, 1, $piece);
360 substr($nb[0], 0, 1, '-');
361 substr($nb[0], 3, 1, 'r');
363 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
366 # check if the from-piece is a pawn
367 if (lc($piece) eq 'p') {
369 if ($from_col != $to_col) {
370 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
373 if (substr($board->[$to_row], $to_col, 1) eq '-') {
375 substr($nb[$to_row + 1], $to_col, 1, '-');
377 substr($nb[$to_row - 1], $to_col, 1, '-');
381 $pretty = substr($pv, 2, 2);
383 if (length($pv) == 5) {
386 $pretty .= uc(substr($pv, 4, 1));
389 $piece = substr($pv, 4, 1);
391 $piece = uc(substr($pv, 4, 1));
396 $pretty = uc($piece);
398 # see how many of these pieces could go here, in all
402 next unless (substr($board->[$row], $col, 1) eq $piece);
403 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
407 # see how many of these pieces from the given row could go here
410 next unless (substr($board->[$from_row], $col, 1) eq $piece);
411 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
414 # and same for columns
417 next unless (substr($board->[$row], $from_col, 1) eq $piece);
418 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
421 # see if we need to disambiguate
422 if ($num_total > 1) {
424 $pretty .= substr($pv, 0, 1);
425 } elsif ($num_row == 1) {
426 $pretty .= substr($pv, 1, 1);
428 $pretty .= substr($pv, 0, 2);
433 if (substr($board->[$to_row], $to_col, 1) ne '-') {
437 $pretty .= substr($pv, 2, 2);
441 substr($nb[$from_row], $from_col, 1, '-');
442 substr($nb[$to_row], $to_col, 1, $piece);
446 } elsif (in_check(\@nb) ne 'none') {
450 return ($pretty, prettyprint_pv(\@nb, @pvs));
457 # Check the PVs first. if they're invalid, just wait, as our data
458 # is most likely out of sync. This isn't a very good solution, as
459 # it can frequently miss stuff, but it's good enough for most users.
463 if (exists($uciinfo{'pv'})) {
464 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
468 while (exists($uciinfo{'pv' . $mpv})) {
469 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
477 print "
\e[H
\e[2J"; # clear the screen
478 if (exists($uciid{'name'})) {
479 print "Analysis by $uciid{'name'}:\n\n";
481 print "Analysis:\n\n";
484 return unless (exists($ficsinfo{'board'}));
486 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
489 while (exists($uciinfo{'pv' . $mpv})) {
490 printf " PV%2u", $mpv;
492 if (defined($uciinfo{'score_mate' . $mpv})) {
493 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
495 if (exists($uciinfo{'score_cp' . $mpv})) {
496 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
497 if ($ficsinfo{'toplay'} eq 'B') {
500 printf " (%+5.2f)", $score;
504 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
505 printf " (%5u kn, %3u kn/s, %2u ply)",
506 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
510 print " ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
516 # Some programs _always_ report MultiPV, even with only one PV.
517 # In this case, we simply use that data as if MultiPV was never
520 if (exists($uciinfo{'pv1'})) {
521 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
522 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
523 $uciinfo{$key} = $uciinfo{$key . '1'};
529 if (defined($uciinfo{'score_mate'})) {
530 my $mate = $uciinfo{'score_mate'};
531 if ($ficsinfo{'toplay'} eq 'B') {
535 printf " White mates in %u\n", $mate;
537 printf " Black mates in %u\n", -$mate;
540 if (exists($uciinfo{'score_cp'})) {
541 my $score = $uciinfo{'score_cp'} * 0.01;
542 if ($ficsinfo{'toplay'} eq 'B') {
545 printf " Score: %+5.2f\n", $score;
549 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
552 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
553 printf " %u nodes, %7u nodes/sec, depth %u ply",
554 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
556 if (exists($uciinfo{'tbhits'})) {
557 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
559 if (exists($uciinfo{'seldepth'})) {
560 printf " (%u selective)", $uciinfo{'seldepth'};
568 my ($wkr, $wkc, $bkr, $bkc);
572 my $piece = substr($board->[$row], $col, 1);
574 ($wkr, $wkc) = ($row, $col);
575 } elsif ($piece eq 'k') {
576 ($bkr, $bkc) = ($row, $col);
581 return ($wkr, $wkc, $bkr, $bkc);
586 my $check = in_check($board);
587 return 0 if ($check eq 'none');
589 # try all possible moves for the side in check
592 my $piece = substr($board->[$row], $col, 1);
593 next if ($piece eq '-');
595 if ($check eq 'white') {
596 next if ($piece eq lc($piece));
598 next if ($piece eq uc($piece));
601 for my $dest_row (0..7) {
602 for my $dest_col (0..7) {
603 next if ($row == $dest_row && $col == $dest_col);
604 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
607 substr($nb[$row], $col, 1, '-');
608 substr($nb[$dest_row], $dest_col, 1, $piece);
610 my $new_check = in_check(\@nb);
611 return 0 if ($new_check ne $check && $new_check ne 'both');
617 # nothing to do; mate
623 my ($black_check, $white_check) = (0, 0);
625 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
627 # check all pieces for the possibility of threatening the two kings
630 my $piece = substr($board->[$row], $col, 1);
631 next if ($piece eq '-');
633 if (uc($piece) eq $piece) {
635 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
638 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
643 if ($black_check && $white_check) {
645 } elsif ($black_check) {
647 } elsif ($white_check) {
655 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
657 # can't eat your own piece
658 my $dest_piece = substr($board->[$to_row], $to_col, 1);
659 if ($dest_piece ne '-') {
660 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
663 if (lc($piece) eq 'k') {
664 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
666 if (lc($piece) eq 'r') {
667 return 0 unless ($from_row == $to_row || $from_col == $to_col);
669 # check that there's a clear passage
670 if ($from_row == $to_row) {
671 if ($from_col > $to_col) {
672 ($to_col, $from_col) = ($from_col, $to_col);
675 for my $c (($from_col+1)..($to_col-1)) {
676 my $middle_piece = substr($board->[$to_row], $c, 1);
677 return 0 if ($middle_piece ne '-');
682 if ($from_row > $to_row) {
683 ($to_row, $from_row) = ($from_row, $to_row);
686 for my $r (($from_row+1)..($to_row-1)) {
687 my $middle_piece = substr($board->[$r], $to_col, 1);
688 return 0 if ($middle_piece ne '-');
694 if (lc($piece) eq 'b') {
695 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
697 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
698 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
700 my $r = $from_row + $dr;
701 my $c = $from_col + $dc;
703 while ($r != $to_row) {
704 my $middle_piece = substr($board->[$r], $c, 1);
705 return 0 if ($middle_piece ne '-');
713 if (lc($piece) eq 'n') {
714 my $diff_r = abs($from_row - $to_row);
715 my $diff_c = abs($from_col - $to_col);
716 return 1 if ($diff_r == 2 && $diff_c == 1);
717 return 1 if ($diff_r == 1 && $diff_c == 2);
721 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
722 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
725 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
726 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
730 if ($to_col == $from_col && $to_row == $from_row + 1) {
731 return ($dest_piece eq '-');
733 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
734 return ($dest_piece ne '-');
740 if ($to_col == $from_col && $to_row == $from_row - 1) {
741 return ($dest_piece eq '-');
743 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
744 return ($dest_piece ne '-');
755 print UCIWRITE "$msg\n";
756 print UCILOG "=> $msg\n";