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.
17 require 'Position.pm';
22 my $server = "freechess.org";
23 my $target = "GMCarlsen";
24 my $engine_cmdline = "'./Deep Rybka 4 SSE42 x64'";
25 my $engine2_cmdline = "./stockfish_13111119_x64_modern_sse42"; # undef for none
26 my $telltarget = undef; # undef to be silent
27 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960); # after each move
28 my $uci_assume_full_compliance = 0; # dangerous :-)
29 my $update_max_interval = 1.0;
39 $SIG{ALRM} = sub { output(); };
40 my $latest_update = undef;
44 open(FICSLOG, ">ficslog.txt")
45 or die "ficslog.txt: $!";
46 print FICSLOG "Log starting.\n";
50 open(UCILOG, ">ucilog.txt")
51 or die "ucilog.txt: $!";
52 print UCILOG "Log starting.\n";
57 # open the chess engine
58 my $engine = open_engine($engine_cmdline, 'E1');
59 my $engine2 = open_engine($engine2_cmdline, 'E2');
60 my ($last_move, $last_tell);
62 my $last_told_text = '';
63 my ($pos_waiting, $pos_calculating, $pos_calculating_second_engine);
65 uciprint($engine, "setoption name UCI_AnalyseMode value true");
66 # uciprint($engine, "setoption name NalimovPath value /srv/tablebase");
67 uciprint($engine, "setoption name NalimovUsage value Rarely");
68 uciprint($engine, "setoption name Hash value 1024");
69 # uciprint($engine, "setoption name MultiPV value 2");
70 uciprint($engine, "ucinewgame");
72 if (defined($engine2)) {
73 uciprint($engine2, "setoption name UCI_AnalyseMode value true");
74 # uciprint($engine2, "setoption name NalimovPath value /srv/tablebase");
75 uciprint($engine2, "setoption name NalimovUsage value Rarely");
76 uciprint($engine2, "setoption name Hash value 1024");
77 uciprint($engine2, "setoption name Threads value 8");
78 uciprint($engine2, "setoption name MultiPV value 500");
79 uciprint($engine2, "ucinewgame");
82 print "Chess engine ready.\n";
85 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
86 $t->input_log(\*FICSLOG);
88 $t->print("SesseBOT");
89 $t->waitfor('/Press return to enter the server/');
93 $t->cmd("set shout 0");
94 $t->cmd("set seek 0");
95 $t->cmd("set style 12");
96 $t->cmd("observe $target");
99 print "FICS ready.\n";
103 vec($rin, fileno($engine->{'read'}), 1) = 1;
104 if (defined($engine2)) {
105 vec($rin, fileno($engine2->{'read'}), 1) = 1;
107 vec($rin, fileno($t), 1) = 1;
109 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
113 my $line = $t->getline(Timeout => 0, errmode => 'return');
114 last if (!defined($line));
118 if ($line =~ /^<12> /) {
119 my $pos = Position->new($line);
121 # if this is already in the queue, ignore it
122 next if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen());
124 # if we're already chewing on this and there's nothing else in the queue,
126 next if (!defined($pos_waiting) && defined($pos_calculating) &&
127 $pos->fen() eq $pos_calculating->fen());
129 # if we're already thinking on something, stop and wait for the engine
131 if (defined($pos_calculating)) {
132 if (!defined($pos_waiting)) {
133 uciprint($engine, "stop");
135 if ($uci_assume_full_compliance) {
138 uciprint($engine, "position fen " . $pos->fen());
139 uciprint($engine, "go infinite");
140 $pos_calculating = $pos;
143 # it's wrong just to give the FEN (the move history is useful,
144 # and per the UCI spec, we should really have sent "ucinewgame"),
146 uciprint($engine, "position fen " . $pos->fen());
147 uciprint($engine, "go infinite");
148 $pos_calculating = $pos;
151 if (defined($engine2)) {
152 if (defined($pos_calculating_second_engine)) {
153 uciprint($engine2, "stop");
155 uciprint($engine2, "position fen " . $pos->fen());
156 uciprint($engine2, "go infinite");
157 $pos_calculating_second_engine = $pos;
159 $engine2->{'info'} = {};
162 $engine->{'info'} = {};
166 # Output a command every move to note that we're
167 # still paying attention -- this is a good tradeoff,
168 # since if no move has happened in the last half
169 # hour, the analysis/relay has most likely stopped
170 # and we should stop hogging server resources.
174 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
175 my ($who, $msg) = ($1, $2);
177 next if (grep { $_ eq $who } (@masters) == 0);
179 if ($msg =~ /^fics (.*?)$/) {
180 $t->cmd("tell $who Executing '$1' on FICS.");
182 } elsif ($msg =~ /^uci (.*?)$/) {
183 $t->cmd("tell $who Sending '$1' to the engine.");
184 print { $engine->{'write'} } "$1\n";
186 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
189 #print "FICS: [$line]\n";
193 # any fun on the UCI channel?
194 if ($nfound > 0 && vec($rout, fileno($engine->{'read'}), 1) == 1) {
195 my @lines = read_lines($engine);
196 for my $line (@lines) {
197 next if $line =~ /(upper|lower)bound/;
198 handle_uci($engine, $line, 1);
204 if (defined($engine2) && $nfound > 0 && vec($rout, fileno($engine2->{'read'}), 1) == 1) {
205 my @lines = read_lines($engine2);
206 for my $line (@lines) {
207 next if $line =~ /(upper|lower)bound/;
208 handle_uci($engine2, $line, 0);
219 my ($engine, $line, $primary) = @_;
223 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
224 print UCILOG localtime() . " $engine->{'tag'} <= $line\n";
225 if ($line =~ /^info/) {
226 my (@infos) = split / /, $line;
229 parse_infos($engine, @infos);
231 if ($line =~ /^id/) {
232 my (@ids) = split / /, $line;
235 parse_ids($engine, @ids);
237 if ($line =~ /^bestmove/) {
239 return if (!$uci_assume_full_compliance);
240 if (defined($pos_waiting)) {
241 uciprint($engine, "position fen " . $pos_waiting->fen());
242 uciprint($engine, "go infinite");
244 $pos_calculating = $pos_waiting;
245 $pos_waiting = undef;
248 $engine2->{'info'} = {};
249 my $pos = $pos_waiting // $pos_calculating;
250 uciprint($engine2, "position fen " . $pos->fen());
251 uciprint($engine2, "go infinite");
252 $pos_calculating_second_engine = $pos;
258 my ($engine, @x) = @_;
261 my $info = $engine->{'info'};
263 # Search for "multipv" first of all, since e.g. Stockfish doesn't put it first.
264 for my $i (0..$#x - 1) {
265 if ($x[$i] =~ 'multipv') {
271 while (scalar @x > 0) {
272 if ($x[0] =~ 'multipv') {
278 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
280 my $value = shift @x;
281 $info->{$key} = $value;
284 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
286 my $value = shift @x;
287 $info->{$key . $mpv} = $value;
290 if ($x[0] eq 'score') {
293 delete $info->{'score_cp' . $mpv};
294 delete $info->{'score_mate' . $mpv};
296 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
299 $info->{'score_cp' . $mpv} = shift @x;
300 } elsif ($x[0] eq 'mate') {
302 $info->{'score_mate' . $mpv} = shift @x;
310 $info->{'pv' . $mpv} = [ @x[1..$#x] ];
313 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
317 #print "unknown info '$x[0]', trying to recover...\n";
319 die "Unknown info '" . join(',', @x) . "'";
325 my ($engine, @x) = @_;
327 while (scalar @x > 0) {
328 if ($x[0] =~ /^(name|author)$/) {
330 my $value = join(' ', @x);
331 $engine->{'id'}{$key} = $value;
341 my ($board, @pvs) = @_;
343 if (scalar @pvs == 0 || !defined($pvs[0])) {
348 my $from_col = col_letter_to_num(substr($pv, 0, 1));
349 my $from_row = row_letter_to_num(substr($pv, 1, 1));
350 my $to_col = col_letter_to_num(substr($pv, 2, 1));
351 my $to_row = row_letter_to_num(substr($pv, 3, 1));
352 my $promo = substr($pv, 4, 1);
354 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
355 my $piece = $board->[$from_row][$from_col];
358 die "Invalid move $pv";
361 # white short castling
362 if ($pv eq 'e1g1' && $piece eq 'K') {
363 return ('0-0', prettyprint_pv($nb, @pvs));
366 # white long castling
367 if ($pv eq 'e1c1' && $piece eq 'K') {
368 return ('0-0-0', prettyprint_pv($nb, @pvs));
371 # black short castling
372 if ($pv eq 'e8g8' && $piece eq 'k') {
373 return ('0-0', prettyprint_pv($nb, @pvs));
376 # black long castling
377 if ($pv eq 'e8c8' && $piece eq 'k') {
378 return ('0-0-0', prettyprint_pv($nb, @pvs));
383 # check if the from-piece is a pawn
384 if (lc($piece) eq 'p') {
386 if ($from_col != $to_col) {
387 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
389 $pretty = substr($pv, 2, 2);
391 if (length($pv) == 5) {
394 $pretty .= uc(substr($pv, 4, 1));
397 $piece = substr($pv, 4, 1);
399 $piece = uc(substr($pv, 4, 1));
404 $pretty = uc($piece);
406 # see how many of these pieces could go here, in all
410 next unless ($board->[$row][$col] eq $piece);
411 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
415 # see how many of these pieces from the given row could go here
418 next unless ($board->[$from_row][$col] eq $piece);
419 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
422 # and same for columns
425 next unless ($board->[$row][$from_col] eq $piece);
426 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
429 # see if we need to disambiguate
430 if ($num_total > 1) {
432 $pretty .= substr($pv, 0, 1);
433 } elsif ($num_row == 1) {
434 $pretty .= substr($pv, 1, 1);
436 $pretty .= substr($pv, 0, 2);
441 if ($board->[$to_row][$to_col] ne '-') {
445 $pretty .= substr($pv, 2, 2);
448 if ($nb->in_mate()) {
450 } elsif ($nb->in_check() ne 'none') {
453 return ($pretty, prettyprint_pv($nb, @pvs));
459 return if (!defined($pos_calculating));
461 # Don't update too often.
462 my $age = Time::HiRes::tv_interval($latest_update);
463 if ($age < $update_max_interval) {
464 Time::HiRes::alarm($update_max_interval + 0.01 - $age);
468 my $info = $engine->{'info'};
471 # Some programs _always_ report MultiPV, even with only one PV.
472 # In this case, we simply use that data as if MultiPV was never
475 if (exists($info->{'pv1'}) && !exists($info->{'pv2'})) {
476 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
477 if (exists($info->{$key . '1'})) {
478 $info->{$key} = $info->{$key . '1'};
484 # Check the PVs first. if they're invalid, just wait, as our data
485 # is most likely out of sync. This isn't a very good solution, as
486 # it can frequently miss stuff, but it's good enough for most users.
490 if (exists($info->{'pv'})) {
491 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}});
495 while (exists($info->{'pv' . $mpv})) {
496 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv' . $mpv}});
501 $engine->{'info'} = {};
507 $latest_update = [Time::HiRes::gettimeofday];
511 my $info = $engine->{'info'};
512 my $id = $engine->{'id'};
514 my $text = 'Analysis';
515 if ($pos_calculating->{'last_move'} ne 'none') {
516 if ($pos_calculating->{'toplay'} eq 'W') {
517 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
519 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
521 if (exists($id->{'name'})) {
526 if (exists($id->{'name'})) {
527 $text .= " by $id->{'name'}:\n\n";
532 return unless (exists($pos_calculating->{'board'}));
534 if (exists($info->{'pv1'}) && exists($info->{'pv2'})) {
537 while (exists($info->{'pv' . $mpv})) {
538 $text .= sprintf " PV%2u", $mpv;
539 my $score = short_score($info, $pos_calculating, $mpv);
540 $text .= " ($score)" if (defined($score));
543 if (exists($info->{'tbhits' . $mpv}) && $info->{'tbhits' . $mpv} > 0) {
544 if ($info->{'tbhits' . $mpv} == 1) {
545 $tbhits = ", 1 tbhit";
547 $tbhits = sprintf ", %u tbhits", $info->{'tbhits' . $mpv};
551 if (exists($info->{'nodes' . $mpv}) && exists($info->{'nps' . $mpv}) && exists($info->{'depth' . $mpv})) {
552 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
553 $info->{'nodes' . $mpv} / 1000, $info->{'nps' . $mpv} / 1000, $info->{'depth' . $mpv};
557 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv' . $mpv}})) . "\n";
563 my $score = long_score($info, $pos_calculating, '');
564 $text .= " $score\n" if defined($score);
565 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}}));
568 if (exists($info->{'nodes'}) && exists($info->{'nps'}) && exists($info->{'depth'})) {
569 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
570 $info->{'nodes'}, $info->{'nps'}, $info->{'depth'};
572 if (exists($info->{'seldepth'})) {
573 $text .= sprintf " (%u selective)", $info->{'seldepth'};
575 if (exists($info->{'tbhits'}) && $info->{'tbhits'} > 0) {
576 if ($info->{'tbhits'} == 1) {
577 $text .= ", one Syzygy hit";
579 $text .= sprintf ", %u Syzygy hits", $info->{'tbhits'};
585 #$text .= book_info($pos_calculating->fen(), $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
587 my @refutation_lines = ();
588 if (defined($engine2)) {
589 for (my $mpv = 1; $mpv < 500; ++$mpv) {
590 my $info = $engine2->{'info'};
591 last if (!exists($info->{'pv' . $mpv}));
593 my $pv = $info->{'pv' . $mpv};
595 my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine->{'board'}, $pv->[0]));
596 my @pretty_pv = prettyprint_pv($pos_calculating_second_engine->{'board'}, @$pv);
597 if (scalar @pretty_pv > 5) {
598 @pretty_pv = @pretty_pv[0..4];
599 push @pretty_pv, "...";
601 my $key = $pretty_move;
602 my $line = sprintf(" %-6s %6s %3s %s",
604 short_score($info, $pos_calculating_second_engine, $mpv, 0),
605 "d" . $info->{'depth' . $mpv},
606 join(', ', @pretty_pv));
607 push @refutation_lines, [ $key, $line ];
612 if ($#refutation_lines >= 0) {
613 $text .= "Shallow search of all legal moves:\n\n";
614 for my $line (sort { $a->[0] cmp $b->[0] } @refutation_lines) {
615 $text .= $line->[1] . "\n";
620 if ($last_text ne $text) {
621 print "
\e[H
\e[2J"; # clear the screen
626 # Now construct the tell text, if any
627 return if (!defined($telltarget));
631 if (exists($id->{'name'})) {
632 $tell_text .= "Analysis by $id->{'name'} -- see http://analysis.sesse.net/ for more information\n";
634 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
637 if (exists($info->{'pv1'}) && exists($info->{'pv2'})) {
640 while (exists($info->{'pv' . $mpv})) {
641 $tell_text .= sprintf " PV%2u", $mpv;
642 my $score = short_score($info, $pos_calculating, $mpv);
643 $tell_text .= " ($score)" if (defined($score));
645 if (exists($info->{'depth' . $mpv})) {
646 $tell_text .= sprintf " (%2u ply)", $info->{'depth' . $mpv};
650 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv' . $mpv}}));
656 my $score = long_score($info, $pos_calculating, '');
657 $tell_text .= " $score\n" if defined($score);
658 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}}));
659 if (exists($info->{'depth'})) {
660 $tell_text .= sprintf " (depth %u ply)", $info->{'depth'};
665 # see if a new tell is called for -- it is if the delay has expired _and_
666 # this is not simply a repetition of the last one
667 if ($last_told_text ne $tell_text) {
669 for my $iv (@tell_intervals) {
670 last if ($now - $last_move < $iv);
671 next if ($last_tell - $last_move >= $iv);
673 for my $line (split /\n/, $tell_text) {
674 $t->print("tell $telltarget [$target] $line");
677 $last_told_text = $text;
686 my $info = $engine->{'info'};
689 $json->{'position'} = $pos_calculating->to_json_hash();
690 $json->{'id'} = $engine->{'id'};
691 $json->{'score'} = long_score($info, $pos_calculating, '');
693 $json->{'nodes'} = $info->{'nodes'};
694 $json->{'nps'} = $info->{'nps'};
695 $json->{'depth'} = $info->{'depth'};
696 $json->{'tbhits'} = $info->{'tbhits'};
697 $json->{'seldepth'} = $info->{'seldepth'};
699 # single-PV only for now
700 $json->{'pv_uci'} = $info->{'pv'};
701 $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}}) ];
703 my %refutation_lines = ();
704 my @refutation_lines = ();
705 if (defined($engine2)) {
706 for (my $mpv = 1; $mpv < 500; ++$mpv) {
707 my $info = $engine2->{'info'};
708 my $pretty_move = "";
710 last if (!exists($info->{'pv' . $mpv}));
713 my $pv = $info->{'pv' . $mpv};
714 my $pretty_move = join('', prettyprint_pv($pos_calculating->{'board'}, $pv->[0]));
715 my @pretty_pv = prettyprint_pv($pos_calculating->{'board'}, @$pv);
716 $refutation_lines{$pv->[0]} = {
717 sort_key => $pretty_move,
718 depth => $info->{'depth' . $mpv},
719 score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
720 pretty_score => short_score($info, $pos_calculating, $mpv, 0),
721 pretty_move => $pretty_move,
722 pv_pretty => \@pretty_pv,
724 $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
728 $json->{'refutation_lines'} = \%refutation_lines;
730 open my $fh, ">/srv/analysis.sesse.net/www/analysis.json.tmp"
732 print $fh JSON::XS::encode_json($json);
734 rename("/srv/analysis.sesse.net/www/analysis.json.tmp", "/srv/analysis.sesse.net/www/analysis.json");
738 my ($engine, $msg) = @_;
739 print { $engine->{'write'} } "$msg\n";
740 print UCILOG localtime() . " $engine->{'tag'} => $msg\n";
744 my ($info, $pos, $mpv, $invert) = @_;
747 if ($pos->{'toplay'} eq 'B') {
751 if (defined($info->{'score_mate' . $mpv})) {
753 return sprintf "M%3d", -$info->{'score_mate' . $mpv};
755 return sprintf "M%3d", $info->{'score_mate' . $mpv};
758 if (exists($info->{'score_cp' . $mpv})) {
759 my $score = $info->{'score_cp' . $mpv} * 0.01;
766 return sprintf "%+5.2f", $score;
774 my ($info, $pos, $mpv, $invert) = @_;
776 if (defined($info->{'score_mate' . $mpv})) {
778 return 99999 - $info->{'score_mate' . $mpv};
780 return -(99999 - $info->{'score_mate' . $mpv});
783 if (exists($info->{'score_cp' . $mpv})) {
784 my $score = $info->{'score_cp' . $mpv};
796 my ($info, $pos, $mpv) = @_;
798 if (defined($info->{'score_mate' . $mpv})) {
799 my $mate = $info->{'score_mate' . $mpv};
800 if ($pos->{'toplay'} eq 'B') {
804 return sprintf "White mates in %u", $mate;
806 return sprintf "Black mates in %u", -$mate;
809 if (exists($info->{'score_cp' . $mpv})) {
810 my $score = $info->{'score_cp' . $mpv} * 0.01;
812 return "Score: 0.00";
814 if ($pos->{'toplay'} eq 'B') {
817 return sprintf "Score: %+5.2f", $score;
826 my ($fen, $board, $toplay) = @_;
828 if (exists($book_cache{$fen})) {
829 return $book_cache{$fen};
832 my $ret = `./booklook $fen`;
833 return "" if ($ret =~ /Not found/ || $ret eq '');
837 for my $m (split /\n/, $ret) {
838 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
842 $pmove = '(current)';
844 ($pmove) = prettyprint_pv($board, $move);
845 $pmove .= $annotation;
849 if ($toplay eq 'W') {
850 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
852 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
854 my $n = $win + $draw + $lose;
860 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
863 push @moves, [ $pmove, $n, $percent, $rating ];
866 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
868 my $text = "Book moves:\n\n Perf. N Rating\n\n";
870 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]
877 my ($cmdline, $tag) = @_;
879 return undef if (!defined($cmdline));
881 my ($uciread, $uciwrite);
882 my $pid = IPC::Open2::open2($uciread, $uciwrite, $cmdline);
894 uciprint($engine, "uci");
899 handle_uci($engine, $_);
909 # Read until we've got a full line -- if the engine sends part of
910 # a line and then stops we're pretty much hosed, but that should
913 while ($engine->{'readbuf'} !~ /\n/) {
915 my $ret = sysread $engine->{'read'}, $tmp, 4096;
917 if (!defined($ret)) {
919 die "error in reading from the UCI engine: $!";
920 } elsif ($ret == 0) {
921 die "EOF from UCI engine";
924 $engine->{'readbuf'} .= $tmp;
929 while ($engine->{'readbuf'} =~ s/^([^\n]*)\n//) {
937 sub col_letter_to_num {
938 return ord(shift) - ord('a');
941 sub row_letter_to_num {
942 return 7 - (ord(shift) - ord('1'));