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(UCILOG, ">ucilog.txt")
29 or die "ucilog.txt: $!";
30 print UCILOG "Log starting.\n";
35 # open the chess engine
36 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
47 uciprint("setoption name UCI_AnalyseMode value true");
48 uciprint("setoption name NalimovPath value c:\\nalimov");
49 uciprint("setoption name NalimovUsage value Normally");
50 # uciprint("setoption name MultiPV value 3");
51 # uciprint("setoption name Contempt value 1000");
52 # uciprint("setoption name Outlook value Ultra Optimistic");
53 uciprint("ucinewgame");
55 print "Chess engine ready.\n";
58 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
59 #$t->input_log(\*STDOUT);
62 $t->waitfor('/Press return to enter the server/');
66 $t->cmd("set shout 0");
67 $t->cmd("set seek 0");
68 $t->cmd("set style 12");
69 $t->cmd("observe $target");
72 print "FICS ready.\n";
76 vec($rin, fileno(UCIREAD), 1) = 1;
77 vec($rin, fileno($t), 1) = 1;
79 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
83 my $line = $t->getline(Timeout => 0, errmode => 'return');
84 last if (!defined($line));
88 if ($line =~ /^<12> /) {
89 my $fen = style12_to_fen($line);
91 uciprint("position fen $fen");
92 uciprint("go infinite");
94 #print "FICS: [$line]\n";
98 # any fun on the UCI channel?
99 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
100 my $line = <UCIREAD>;
103 print UCILOG "<= $line\n";
104 if ($line =~ /^info/) {
105 my (@infos) = split / /, $line;
112 # don't update too often
113 Time::HiRes::alarm(0.2);
123 while (scalar @x > 0) {
124 if ($x[0] =~ 'multipv') {
129 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
131 my $value = shift @x;
132 $uciinfo{$key} = $value;
135 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
137 my $value = shift @x;
138 $uciinfo{$key . $mpv} = $value;
141 if ($x[0] eq 'score') {
144 delete $uciinfo{'score_cp' . $mpv};
145 delete $uciinfo{'score_mate' . $mpv};
147 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
150 $uciinfo{'score_cp' . $mpv} = shift @x;
151 } elsif ($x[0] eq 'mate') {
153 $uciinfo{'score_mate' . $mpv} = shift @x;
161 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
164 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
168 #print "unknown info '$x[0]', trying to recover...\n";
170 die "Unknown info '" . join(',', @x) . "'";
177 my (@x) = split / /, $str;
179 $ficsinfo{'board'} = [ @x[1..8] ];
180 $ficsinfo{'toplay'} = $x[9];
183 my (@board) = @x[1..8];
184 for my $rank (0..7) {
185 $board[$rank] =~ s/(-+)/length($1)/ge;
187 my $fen = join('/', @board);
189 # white/black to move
195 $castling .= "K" if ($x[11] == 1);
196 $castling .= "Q" if ($x[12] == 1);
197 $castling .= "k" if ($x[13] == 1);
198 $castling .= "q" if ($x[14] == 1);
199 $castling = "-" if ($castling eq "");
207 my $nep = (qw(a b c d e f g h))[$col];
216 # Showing the en passant square when actually no capture can be made
217 # seems to confuse at least Rybka. Thus, check if there's actually
218 # a pawn of the opposite side that can do the en passant move, and if
219 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
220 # "right" thing as per the standard, though.
223 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
224 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
226 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
227 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
245 my ($board, @pvs) = @_;
247 if (scalar @pvs == 0 || !defined($pvs[0])) {
252 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
253 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
254 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
255 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
258 my $piece = substr($board->[$from_row], $from_col, 1);
260 # white short castling
261 if ($pv eq 'e1g1' && $piece eq 'K') {
265 substr($nb[7], 4, 1, '-');
266 substr($nb[7], 6, 1, $piece);
269 substr($nb[7], 7, 1, '-');
270 substr($nb[7], 5, 1, 'R');
272 return ('0-0', prettyprint_pv(\@nb, @pvs));
275 # white long castling
276 if ($pv eq 'e1c1' && $piece eq 'K') {
280 substr($nb[7], 4, 1, '-');
281 substr($nb[7], 2, 1, $piece);
284 substr($nb[7], 0, 1, '-');
285 substr($nb[7], 3, 1, 'R');
287 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
290 # black short castling
291 if ($pv eq 'e8g8' && $piece eq 'k') {
295 substr($nb[0], 4, 1, '-');
296 substr($nb[0], 6, 1, $piece);
299 substr($nb[0], 7, 1, '-');
300 substr($nb[0], 5, 1, 'R');
302 return ('0-0', prettyprint_pv(\@nb, @pvs));
305 # black long castling
306 if ($pv eq 'e8c8' && $piece eq 'k') {
310 substr($nb[0], 4, 1, '-');
311 substr($nb[0], 2, 1, $piece);
314 substr($nb[0], 0, 1, '-');
315 substr($nb[0], 3, 1, 'R');
317 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
320 # check if the from-piece is a pawn
321 if (lc($piece) eq 'p') {
323 if (substr($board->[$to_row], $to_col, 1) ne '-') {
324 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
326 $pretty = substr($pv, 2, 2);
328 if (length($pv) == 5) {
331 $pretty .= uc(substr($pv, 4, 1));
334 $piece = substr($pv, 4, 1);
336 $piece = uc(substr($pv, 4, 1));
341 $pretty = uc($piece);
343 # see how many of these pieces could go here, in all
347 next unless (substr($board->[$row], $col, 1) eq $piece);
348 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
352 # see how many of these pieces from the given row could go here
355 next unless (substr($board->[$from_row], $col, 1) eq $piece);
356 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
359 # and same for columns
362 next unless (substr($board->[$row], $from_col, 1) eq $piece);
363 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
366 # see if we need to disambiguate
367 if ($num_total > 1) {
369 $pretty .= substr($pv, 0, 1);
370 } elsif ($num_row == 1) {
371 $pretty .= substr($pv, 1, 1);
373 $pretty .= substr($pv, 0, 2);
378 if (substr($board->[$to_row], $to_col, 1) ne '-') {
382 $pretty .= substr($pv, 2, 2);
387 substr($nb[$from_row], $from_col, 1, '-');
388 substr($nb[$to_row], $to_col, 1, $piece);
392 } elsif (in_check(\@nb) ne 'none') {
396 return ($pretty, prettyprint_pv(\@nb, @pvs));
402 print "
\ecAnalysis:\n";
404 return unless (exists($ficsinfo{'board'}));
406 if (exists($uciinfo{'pv1'})) {
409 while (exists($uciinfo{'pv' . $mpv})) {
410 printf " PV%2u", $mpv;
412 if (defined($uciinfo{'score_mate' . $mpv})) {
413 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
415 if (exists($uciinfo{'score_cp' . $mpv})) {
416 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
417 if ($ficsinfo{'toplay'} eq 'B') {
420 printf " (%+5.2f)", $score;
424 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
425 printf " (%5u kn, %3u kn/s, %2u ply)",
426 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
430 print " ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
436 if (defined($uciinfo{'score_mate'})) {
437 printf " Mate in %d\n", $uciinfo{'score_mate'};
439 if (exists($uciinfo{'score_cp'})) {
440 my $score = $uciinfo{'score_cp'} * 0.01;
441 if ($ficsinfo{'toplay'} eq 'B') {
444 printf " Score: %+5.2f\n", $score;
448 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
451 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
452 printf " %u nodes, %7u nodes/sec, depth %u ply",
453 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
455 if (exists($uciinfo{'tbhits'})) {
456 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
458 if (exists($uciinfo{'seldepth'})) {
459 printf " (%u selective)", $uciinfo{'seldepth'};
467 my ($wkr, $wkc, $bkr, $bkc);
471 my $piece = substr($board->[$row], $col, 1);
473 ($wkr, $wkc) = ($row, $col);
474 } elsif ($piece eq 'k') {
475 ($bkr, $bkc) = ($row, $col);
480 return ($wkr, $wkc, $bkr, $bkc);
485 my $check = in_check($board);
486 return 0 if ($check eq 'none');
488 # try all possible moves for the side in check
491 my $piece = substr($board->[$row], $col, 1);
492 next if ($piece eq '-');
494 if ($check eq 'white') {
495 next if ($piece eq lc($piece));
497 next if ($piece eq uc($piece));
500 for my $dest_row (0..7) {
501 for my $dest_col (0..7) {
502 next if ($row == $dest_row && $col == $dest_col);
503 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
506 substr($nb[$row], $col, 1, '-');
507 substr($nb[$dest_row], $dest_col, 1, $piece);
509 my $new_check = in_check(\@nb);
510 return 0 if ($new_check ne $check && $new_check ne 'both');
516 # nothing to do; mate
522 my ($black_check, $white_check) = (0, 0);
524 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
526 # check all pieces for the possibility of threatening the two kings
529 my $piece = substr($board->[$row], $col, 1);
530 next if ($piece eq '-' || lc($piece) eq 'k');
532 if (uc($piece) eq $piece) {
534 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
537 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
542 if ($black_check && $white_check) {
544 } elsif ($black_check) {
546 } elsif ($white_check) {
554 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
556 # can't eat your own piece
557 my $dest_piece = substr($board->[$to_row], $to_col, 1);
558 if ($dest_piece ne '-') {
559 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
562 if (lc($piece) eq 'k') {
563 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
565 if (lc($piece) eq 'r') {
566 return 0 unless ($from_row == $to_row || $from_col == $to_col);
568 # check that there's a clear passage
569 if ($from_row == $to_row) {
570 if ($from_col > $to_col) {
571 ($to_col, $from_col) = ($from_col, $to_col);
574 for my $c (($from_col+1)..($to_col-1)) {
575 my $middle_piece = substr($board->[$to_row], $c, 1);
576 return 0 if ($middle_piece ne '-');
581 if ($from_row > $to_row) {
582 ($to_row, $from_row) = ($from_row, $to_row);
585 for my $r (($from_row+1)..($to_row-1)) {
586 my $middle_piece = substr($board->[$r], $to_col, 1);
587 return 0 if ($middle_piece ne '-');
593 if (lc($piece) eq 'b') {
594 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
596 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
597 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
599 my $r = $from_row + $dr;
600 my $c = $from_col + $dc;
602 while ($r != $to_row) {
603 my $middle_piece = substr($board->[$r], $c, 1);
604 return 0 if ($middle_piece ne '-');
612 if (lc($piece) eq 'n') {
613 my $diff_r = abs($from_row - $to_row);
614 my $diff_c = abs($from_col - $to_col);
615 return 1 if ($diff_r == 2 && $diff_c == 1);
616 return 1 if ($diff_r == 1 && $diff_c == 2);
620 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
621 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
624 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
625 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
629 if ($to_col == $from_col && $to_row == $from_row + 1) {
630 return ($dest_piece eq '-');
632 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
633 return ($dest_piece ne '-');
639 if ($to_col == $from_col && $to_row == $from_row - 1) {
640 return ($dest_piece eq '-');
642 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
643 return ($dest_piece ne '-');
654 print UCIWRITE "$msg\n";
655 print UCILOG "=> $msg\n";