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";
24 my $target = "GMCarlsen";
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 Normally");
56 uciprint("setoption name MultiPV value 3");
57 # uciprint("setoption name Contempt value 1000");
58 # uciprint("setoption name Outlook value Ultra Optimistic");
59 uciprint("ucinewgame");
61 print "Chess engine ready.\n";
64 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
65 $t->input_log(\*FICSLOG);
68 $t->waitfor('/Press return to enter the server/');
72 $t->cmd("set shout 0");
73 $t->cmd("set seek 0");
74 $t->cmd("set style 12");
75 $t->cmd("observe $target");
78 print "FICS ready.\n";
82 vec($rin, fileno(UCIREAD), 1) = 1;
83 vec($rin, fileno($t), 1) = 1;
85 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
89 my $line = $t->getline(Timeout => 0, errmode => 'return');
90 last if (!defined($line));
94 if ($line =~ /^<12> /) {
95 my $fen = style12_to_fen($line);
97 uciprint("position fen $fen");
98 uciprint("go infinite");
100 #print "FICS: [$line]\n";
104 # any fun on the UCI channel?
105 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
106 my $line = <UCIREAD>;
109 print UCILOG "<= $line\n";
110 if ($line =~ /^info/) {
111 my (@infos) = split / /, $line;
118 # don't update too often
119 Time::HiRes::alarm(0.2);
129 while (scalar @x > 0) {
130 if ($x[0] =~ 'multipv') {
135 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
137 my $value = shift @x;
138 $uciinfo{$key} = $value;
141 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
143 my $value = shift @x;
144 $uciinfo{$key . $mpv} = $value;
147 if ($x[0] eq 'score') {
150 delete $uciinfo{'score_cp' . $mpv};
151 delete $uciinfo{'score_mate' . $mpv};
153 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
156 $uciinfo{'score_cp' . $mpv} = shift @x;
157 } elsif ($x[0] eq 'mate') {
159 $uciinfo{'score_mate' . $mpv} = shift @x;
167 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
170 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
174 #print "unknown info '$x[0]', trying to recover...\n";
176 die "Unknown info '" . join(',', @x) . "'";
183 my (@x) = split / /, $str;
185 $ficsinfo{'board'} = [ @x[1..8] ];
186 $ficsinfo{'toplay'} = $x[9];
189 my (@board) = @x[1..8];
190 for my $rank (0..7) {
191 $board[$rank] =~ s/(-+)/length($1)/ge;
193 my $fen = join('/', @board);
195 # white/black to move
201 $castling .= "K" if ($x[11] == 1);
202 $castling .= "Q" if ($x[12] == 1);
203 $castling .= "k" if ($x[13] == 1);
204 $castling .= "q" if ($x[14] == 1);
205 $castling = "-" if ($castling eq "");
213 my $nep = (qw(a b c d e f g h))[$col];
222 # Showing the en passant square when actually no capture can be made
223 # seems to confuse at least Rybka. Thus, check if there's actually
224 # a pawn of the opposite side that can do the en passant move, and if
225 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
226 # "right" thing as per the standard, though.
229 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
230 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
232 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
233 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
251 my ($board, @pvs) = @_;
253 if (scalar @pvs == 0 || !defined($pvs[0])) {
258 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
259 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
260 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
261 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
264 my $piece = substr($board->[$from_row], $from_col, 1);
266 # white short castling
267 if ($pv eq 'e1g1' && $piece eq 'K') {
271 substr($nb[7], 4, 1, '-');
272 substr($nb[7], 6, 1, $piece);
275 substr($nb[7], 7, 1, '-');
276 substr($nb[7], 5, 1, 'R');
278 return ('0-0', prettyprint_pv(\@nb, @pvs));
281 # white long castling
282 if ($pv eq 'e1c1' && $piece eq 'K') {
286 substr($nb[7], 4, 1, '-');
287 substr($nb[7], 2, 1, $piece);
290 substr($nb[7], 0, 1, '-');
291 substr($nb[7], 3, 1, 'R');
293 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
296 # black short castling
297 if ($pv eq 'e8g8' && $piece eq 'k') {
301 substr($nb[0], 4, 1, '-');
302 substr($nb[0], 6, 1, $piece);
305 substr($nb[0], 7, 1, '-');
306 substr($nb[0], 5, 1, 'R');
308 return ('0-0', prettyprint_pv(\@nb, @pvs));
311 # black long castling
312 if ($pv eq 'e8c8' && $piece eq 'k') {
316 substr($nb[0], 4, 1, '-');
317 substr($nb[0], 2, 1, $piece);
320 substr($nb[0], 0, 1, '-');
321 substr($nb[0], 3, 1, 'R');
323 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
326 # check if the from-piece is a pawn
327 if (lc($piece) eq 'p') {
329 if (substr($board->[$to_row], $to_col, 1) ne '-') {
330 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
332 $pretty = substr($pv, 2, 2);
334 if (length($pv) == 5) {
337 $pretty .= uc(substr($pv, 4, 1));
340 $piece = substr($pv, 4, 1);
342 $piece = uc(substr($pv, 4, 1));
347 $pretty = uc($piece);
349 # see how many of these pieces could go here, in all
353 next unless (substr($board->[$row], $col, 1) eq $piece);
354 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
358 # see how many of these pieces from the given row could go here
361 next unless (substr($board->[$from_row], $col, 1) eq $piece);
362 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
365 # and same for columns
368 next unless (substr($board->[$row], $from_col, 1) eq $piece);
369 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
372 # see if we need to disambiguate
373 if ($num_total > 1) {
375 $pretty .= substr($pv, 0, 1);
376 } elsif ($num_row == 1) {
377 $pretty .= substr($pv, 1, 1);
379 $pretty .= substr($pv, 0, 2);
384 if (substr($board->[$to_row], $to_col, 1) ne '-') {
388 $pretty .= substr($pv, 2, 2);
393 substr($nb[$from_row], $from_col, 1, '-');
394 substr($nb[$to_row], $to_col, 1, $piece);
398 } elsif (in_check(\@nb) ne 'none') {
402 return ($pretty, prettyprint_pv(\@nb, @pvs));
408 print "
\ecAnalysis:\n";
410 return unless (exists($ficsinfo{'board'}));
412 if (exists($uciinfo{'pv1'})) {
415 while (exists($uciinfo{'pv' . $mpv})) {
416 printf " PV%2u", $mpv;
418 if (defined($uciinfo{'score_mate' . $mpv})) {
419 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
421 if (exists($uciinfo{'score_cp' . $mpv})) {
422 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
423 if ($ficsinfo{'toplay'} eq 'B') {
426 printf " (%+5.2f)", $score;
430 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
431 printf " (%5u kn, %3u kn/s, %2u ply)",
432 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
436 print " ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
442 if (defined($uciinfo{'score_mate'})) {
443 printf " Mate in %d\n", $uciinfo{'score_mate'};
445 if (exists($uciinfo{'score_cp'})) {
446 my $score = $uciinfo{'score_cp'} * 0.01;
447 if ($ficsinfo{'toplay'} eq 'B') {
450 printf " Score: %+5.2f\n", $score;
454 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
457 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
458 printf " %u nodes, %7u nodes/sec, depth %u ply",
459 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
461 if (exists($uciinfo{'tbhits'})) {
462 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
464 if (exists($uciinfo{'seldepth'})) {
465 printf " (%u selective)", $uciinfo{'seldepth'};
473 my ($wkr, $wkc, $bkr, $bkc);
477 my $piece = substr($board->[$row], $col, 1);
479 ($wkr, $wkc) = ($row, $col);
480 } elsif ($piece eq 'k') {
481 ($bkr, $bkc) = ($row, $col);
486 return ($wkr, $wkc, $bkr, $bkc);
491 my $check = in_check($board);
492 return 0 if ($check eq 'none');
494 # try all possible moves for the side in check
497 my $piece = substr($board->[$row], $col, 1);
498 next if ($piece eq '-');
500 if ($check eq 'white') {
501 next if ($piece eq lc($piece));
503 next if ($piece eq uc($piece));
506 for my $dest_row (0..7) {
507 for my $dest_col (0..7) {
508 next if ($row == $dest_row && $col == $dest_col);
509 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
512 substr($nb[$row], $col, 1, '-');
513 substr($nb[$dest_row], $dest_col, 1, $piece);
515 my $new_check = in_check(\@nb);
516 return 0 if ($new_check ne $check && $new_check ne 'both');
522 # nothing to do; mate
528 my ($black_check, $white_check) = (0, 0);
530 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
532 # check all pieces for the possibility of threatening the two kings
535 my $piece = substr($board->[$row], $col, 1);
536 next if ($piece eq '-' || lc($piece) eq 'k');
538 if (uc($piece) eq $piece) {
540 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
543 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
548 if ($black_check && $white_check) {
550 } elsif ($black_check) {
552 } elsif ($white_check) {
560 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
562 # can't eat your own piece
563 my $dest_piece = substr($board->[$to_row], $to_col, 1);
564 if ($dest_piece ne '-') {
565 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
568 if (lc($piece) eq 'k') {
569 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
571 if (lc($piece) eq 'r') {
572 return 0 unless ($from_row == $to_row || $from_col == $to_col);
574 # check that there's a clear passage
575 if ($from_row == $to_row) {
576 if ($from_col > $to_col) {
577 ($to_col, $from_col) = ($from_col, $to_col);
580 for my $c (($from_col+1)..($to_col-1)) {
581 my $middle_piece = substr($board->[$to_row], $c, 1);
582 return 0 if ($middle_piece ne '-');
587 if ($from_row > $to_row) {
588 ($to_row, $from_row) = ($from_row, $to_row);
591 for my $r (($from_row+1)..($to_row-1)) {
592 my $middle_piece = substr($board->[$r], $to_col, 1);
593 return 0 if ($middle_piece ne '-');
599 if (lc($piece) eq 'b') {
600 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
602 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
603 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
605 my $r = $from_row + $dr;
606 my $c = $from_col + $dc;
608 while ($r != $to_row) {
609 my $middle_piece = substr($board->[$r], $c, 1);
610 return 0 if ($middle_piece ne '-');
618 if (lc($piece) eq 'n') {
619 my $diff_r = abs($from_row - $to_row);
620 my $diff_c = abs($from_col - $to_col);
621 return 1 if ($diff_r == 2 && $diff_c == 1);
622 return 1 if ($diff_r == 1 && $diff_c == 2);
626 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
627 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
630 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
631 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
635 if ($to_col == $from_col && $to_row == $from_row + 1) {
636 return ($dest_piece eq '-');
638 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
639 return ($dest_piece ne '-');
645 if ($to_col == $from_col && $to_row == $from_row - 1) {
646 return ($dest_piece eq '-');
648 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
649 return ($dest_piece ne '-');
660 print UCIWRITE "$msg\n";
661 print UCILOG "=> $msg\n";