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);
53 uciprint("setoption name UCI_AnalyseMode value true");
54 uciprint("setoption name NalimovPath value c:\\nalimov");
55 uciprint("setoption name NalimovUsage value Rarely");
56 uciprint("setoption name Hash value 1024");
57 # uciprint("setoption name MultiPV value 3");
58 # uciprint("setoption name Contempt value 1000");
59 # uciprint("setoption name Outlook value Ultra Optimistic");
60 uciprint("ucinewgame");
62 print "Chess engine ready.\n";
65 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
66 $t->input_log(\*FICSLOG);
69 $t->waitfor('/Press return to enter the server/');
73 $t->cmd("set shout 0");
74 $t->cmd("set seek 0");
75 $t->cmd("set style 12");
76 $t->cmd("observe $target");
79 print "FICS ready.\n";
83 vec($rin, fileno(UCIREAD), 1) = 1;
84 vec($rin, fileno($t), 1) = 1;
86 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
90 my $line = $t->getline(Timeout => 0, errmode => 'return');
91 last if (!defined($line));
95 if ($line =~ /^<12> /) {
96 my $fen = style12_to_fen($line);
98 uciprint("position fen $fen");
99 uciprint("go infinite");
101 #print "FICS: [$line]\n";
105 # any fun on the UCI channel?
106 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
107 my $line = <UCIREAD>;
110 print UCILOG "<= $line\n";
111 if ($line =~ /^info/) {
112 my (@infos) = split / /, $line;
119 # don't update too often
120 Time::HiRes::alarm(0.2);
130 while (scalar @x > 0) {
131 if ($x[0] =~ 'multipv') {
136 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
138 my $value = shift @x;
139 $uciinfo{$key} = $value;
142 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
144 my $value = shift @x;
145 $uciinfo{$key . $mpv} = $value;
148 if ($x[0] eq 'score') {
151 delete $uciinfo{'score_cp' . $mpv};
152 delete $uciinfo{'score_mate' . $mpv};
154 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
157 $uciinfo{'score_cp' . $mpv} = shift @x;
158 } elsif ($x[0] eq 'mate') {
160 $uciinfo{'score_mate' . $mpv} = shift @x;
168 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
171 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
175 #print "unknown info '$x[0]', trying to recover...\n";
177 die "Unknown info '" . join(',', @x) . "'";
184 my (@x) = split / /, $str;
186 $ficsinfo{'board'} = [ @x[1..8] ];
187 $ficsinfo{'toplay'} = $x[9];
190 my (@board) = @x[1..8];
191 for my $rank (0..7) {
192 $board[$rank] =~ s/(-+)/length($1)/ge;
194 my $fen = join('/', @board);
196 # white/black to move
202 $castling .= "K" if ($x[11] == 1);
203 $castling .= "Q" if ($x[12] == 1);
204 $castling .= "k" if ($x[13] == 1);
205 $castling .= "q" if ($x[14] == 1);
206 $castling = "-" if ($castling eq "");
214 my $nep = (qw(a b c d e f g h))[$col];
223 # Showing the en passant square when actually no capture can be made
224 # seems to confuse at least Rybka. Thus, check if there's actually
225 # a pawn of the opposite side that can do the en passant move, and if
226 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
227 # "right" thing as per the standard, though.
230 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
231 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
233 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
234 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
252 my ($board, @pvs) = @_;
254 if (scalar @pvs == 0 || !defined($pvs[0])) {
261 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
262 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
263 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
264 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
267 my $piece = substr($board->[$from_row], $from_col, 1);
273 # white short castling
274 if ($pv eq 'e1g1' && $piece eq 'K') {
276 substr($nb[7], 4, 1, '-');
277 substr($nb[7], 6, 1, $piece);
280 substr($nb[7], 7, 1, '-');
281 substr($nb[7], 5, 1, 'R');
283 return ('0-0', prettyprint_pv(\@nb, @pvs));
286 # white long castling
287 if ($pv eq 'e1c1' && $piece eq 'K') {
289 substr($nb[7], 4, 1, '-');
290 substr($nb[7], 2, 1, $piece);
293 substr($nb[7], 0, 1, '-');
294 substr($nb[7], 3, 1, 'R');
296 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
299 # black short castling
300 if ($pv eq 'e8g8' && $piece eq 'k') {
302 substr($nb[0], 4, 1, '-');
303 substr($nb[0], 6, 1, $piece);
306 substr($nb[0], 7, 1, '-');
307 substr($nb[0], 5, 1, 'r');
309 return ('0-0', prettyprint_pv(\@nb, @pvs));
312 # black long castling
313 if ($pv eq 'e8c8' && $piece eq 'k') {
315 substr($nb[0], 4, 1, '-');
316 substr($nb[0], 2, 1, $piece);
319 substr($nb[0], 0, 1, '-');
320 substr($nb[0], 3, 1, 'r');
322 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
325 # check if the from-piece is a pawn
326 if (lc($piece) eq 'p') {
328 if ($from_col != $to_col) {
329 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
332 if (substr($board->[$to_row], $to_col, 1) eq '-') {
334 substr($nb[$to_row + 1], $to_col, 1, '-');
336 substr($nb[$to_row - 1], $to_col, 1, '-');
340 $pretty = substr($pv, 2, 2);
342 if (length($pv) == 5) {
345 $pretty .= uc(substr($pv, 4, 1));
348 $piece = substr($pv, 4, 1);
350 $piece = uc(substr($pv, 4, 1));
355 $pretty = uc($piece);
357 # see how many of these pieces could go here, in all
361 next unless (substr($board->[$row], $col, 1) eq $piece);
362 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
366 # see how many of these pieces from the given row could go here
369 next unless (substr($board->[$from_row], $col, 1) eq $piece);
370 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
373 # and same for columns
376 next unless (substr($board->[$row], $from_col, 1) eq $piece);
377 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
380 # see if we need to disambiguate
381 if ($num_total > 1) {
383 $pretty .= substr($pv, 0, 1);
384 } elsif ($num_row == 1) {
385 $pretty .= substr($pv, 1, 1);
387 $pretty .= substr($pv, 0, 2);
392 if (substr($board->[$to_row], $to_col, 1) ne '-') {
396 $pretty .= substr($pv, 2, 2);
400 substr($nb[$from_row], $from_col, 1, '-');
401 substr($nb[$to_row], $to_col, 1, $piece);
405 } elsif (in_check(\@nb) ne 'none') {
409 return ($pretty, prettyprint_pv(\@nb, @pvs));
416 # Check the PVs first. if they're invalid, just wait, as our data
417 # is most likely out of sync. This isn't a very good solution, as
418 # it can frequently miss stuff, but it's good enough for most users.
422 if (exists($uciinfo{'pv'})) {
423 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
427 while (exists($uciinfo{'pv' . $mpv})) {
428 $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
436 print "
\e[H
\e[2JAnalysis:\n\n";
438 return unless (exists($ficsinfo{'board'}));
440 if (exists($uciinfo{'pv1'})) {
443 while (exists($uciinfo{'pv' . $mpv})) {
444 printf " PV%2u", $mpv;
446 if (defined($uciinfo{'score_mate' . $mpv})) {
447 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
449 if (exists($uciinfo{'score_cp' . $mpv})) {
450 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
451 if ($ficsinfo{'toplay'} eq 'B') {
454 printf " (%+5.2f)", $score;
458 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
459 printf " (%5u kn, %3u kn/s, %2u ply)",
460 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
464 print " ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
470 if (defined($uciinfo{'score_mate'})) {
471 my $mate = $uciinfo{'score_mate'};
472 if ($ficsinfo{'toplay'} eq 'B') {
476 printf " White mates in %u\n", $mate;
478 printf " Black mates in %u\n", -$mate;
481 if (exists($uciinfo{'score_cp'})) {
482 my $score = $uciinfo{'score_cp'} * 0.01;
483 if ($ficsinfo{'toplay'} eq 'B') {
486 printf " Score: %+5.2f\n", $score;
490 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
493 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
494 printf " %u nodes, %7u nodes/sec, depth %u ply",
495 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
497 if (exists($uciinfo{'tbhits'})) {
498 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
500 if (exists($uciinfo{'seldepth'})) {
501 printf " (%u selective)", $uciinfo{'seldepth'};
509 my ($wkr, $wkc, $bkr, $bkc);
513 my $piece = substr($board->[$row], $col, 1);
515 ($wkr, $wkc) = ($row, $col);
516 } elsif ($piece eq 'k') {
517 ($bkr, $bkc) = ($row, $col);
522 return ($wkr, $wkc, $bkr, $bkc);
527 my $check = in_check($board);
528 return 0 if ($check eq 'none');
530 # try all possible moves for the side in check
533 my $piece = substr($board->[$row], $col, 1);
534 next if ($piece eq '-');
536 if ($check eq 'white') {
537 next if ($piece eq lc($piece));
539 next if ($piece eq uc($piece));
542 for my $dest_row (0..7) {
543 for my $dest_col (0..7) {
544 next if ($row == $dest_row && $col == $dest_col);
545 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
548 substr($nb[$row], $col, 1, '-');
549 substr($nb[$dest_row], $dest_col, 1, $piece);
551 my $new_check = in_check(\@nb);
552 return 0 if ($new_check ne $check && $new_check ne 'both');
558 # nothing to do; mate
564 my ($black_check, $white_check) = (0, 0);
566 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
568 # check all pieces for the possibility of threatening the two kings
571 my $piece = substr($board->[$row], $col, 1);
572 next if ($piece eq '-');
574 if (uc($piece) eq $piece) {
576 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
579 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
584 if ($black_check && $white_check) {
586 } elsif ($black_check) {
588 } elsif ($white_check) {
596 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
598 # can't eat your own piece
599 my $dest_piece = substr($board->[$to_row], $to_col, 1);
600 if ($dest_piece ne '-') {
601 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
604 if (lc($piece) eq 'k') {
605 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
607 if (lc($piece) eq 'r') {
608 return 0 unless ($from_row == $to_row || $from_col == $to_col);
610 # check that there's a clear passage
611 if ($from_row == $to_row) {
612 if ($from_col > $to_col) {
613 ($to_col, $from_col) = ($from_col, $to_col);
616 for my $c (($from_col+1)..($to_col-1)) {
617 my $middle_piece = substr($board->[$to_row], $c, 1);
618 return 0 if ($middle_piece ne '-');
623 if ($from_row > $to_row) {
624 ($to_row, $from_row) = ($from_row, $to_row);
627 for my $r (($from_row+1)..($to_row-1)) {
628 my $middle_piece = substr($board->[$r], $to_col, 1);
629 return 0 if ($middle_piece ne '-');
635 if (lc($piece) eq 'b') {
636 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
638 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
639 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
641 my $r = $from_row + $dr;
642 my $c = $from_col + $dc;
644 while ($r != $to_row) {
645 my $middle_piece = substr($board->[$r], $c, 1);
646 return 0 if ($middle_piece ne '-');
654 if (lc($piece) eq 'n') {
655 my $diff_r = abs($from_row - $to_row);
656 my $diff_c = abs($from_col - $to_col);
657 return 1 if ($diff_r == 2 && $diff_c == 1);
658 return 1 if ($diff_r == 1 && $diff_c == 2);
662 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
663 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
666 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
667 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
671 if ($to_col == $from_col && $to_row == $from_row + 1) {
672 return ($dest_piece eq '-');
674 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
675 return ($dest_piece ne '-');
681 if ($to_col == $from_col && $to_row == $from_row - 1) {
682 return ($dest_piece eq '-');
684 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
685 return ($dest_piece ne '-');
696 print UCIWRITE "$msg\n";
697 print UCILOG "=> $msg\n";