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.
15 use Chess::PGN::Parse;
23 require 'Position.pm';
31 $SIG{ALRM} = sub { output(); };
32 my $latest_update = undef;
33 my $http_timer = undef;
34 my $tb_retry_timer = undef;
36 my $tb_lookup_running = 0;
40 open(FICSLOG, ">ficslog.txt")
41 or die "ficslog.txt: $!";
42 print FICSLOG "Log starting.\n";
46 open(UCILOG, ">ucilog.txt")
47 or die "ucilog.txt: $!";
48 print UCILOG "Log starting.\n";
52 open(TBLOG, ">tblog.txt")
53 or die "tblog.txt: $!";
54 print TBLOG "Log starting.\n";
60 # open the chess engine
61 my $engine = open_engine($remoteglotconf::engine_cmdline, 'E1', sub { handle_uci(@_, 1); });
62 my $engine2 = open_engine($remoteglotconf::engine2_cmdline, 'E2', sub { handle_uci(@_, 0); });
65 my ($pos_waiting, $pos_calculating, $pos_calculating_second_engine);
67 uciprint($engine, "setoption name UCI_AnalyseMode value true");
68 while (my ($key, $value) = each %remoteglotconf::engine_config) {
69 uciprint($engine, "setoption name $key value $value");
71 uciprint($engine, "ucinewgame");
73 if (defined($engine2)) {
74 uciprint($engine2, "setoption name UCI_AnalyseMode value true");
75 while (my ($key, $value) = each %remoteglotconf::engine2_config) {
76 uciprint($engine2, "setoption name $key value $value");
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);
87 $t->open($remoteglotconf::server);
88 $t->print($remoteglotconf::nick);
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");
97 my $ev1 = AnyEvent->io(
100 cb => sub { # what callback to execute
102 my $line = $t->getline(Timeout => 0, errmode => 'return');
103 return if (!defined($line));
111 if (defined($remoteglotconf::target)) {
112 if ($remoteglotconf::target =~ /^http:/) {
113 fetch_pgn($remoteglotconf::target);
115 $t->cmd("observe $remoteglotconf::target");
118 print "FICS ready.\n";
120 # Engine events have already been set up by Engine.pm.
124 my ($engine, $line, $primary) = @_;
126 return if $line =~ /(upper|lower)bound/;
128 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
129 print UCILOG localtime() . " $engine->{'tag'} <= $line\n";
130 if ($line =~ /^info/) {
131 my (@infos) = split / /, $line;
134 parse_infos($engine, @infos);
136 if ($line =~ /^id/) {
137 my (@ids) = split / /, $line;
140 parse_ids($engine, @ids);
142 if ($line =~ /^bestmove/) {
144 return if (!$remoteglotconf::uci_assume_full_compliance);
145 if (defined($pos_waiting)) {
146 uciprint($engine, "position fen " . $pos_waiting->fen());
147 uciprint($engine, "go infinite");
149 $pos_calculating = $pos_waiting;
150 $pos_waiting = undef;
153 $engine2->{'info'} = {};
154 my $pos = $pos_waiting // $pos_calculating;
155 uciprint($engine2, "position fen " . $pos->fen());
156 uciprint($engine2, "go infinite");
157 $pos_calculating_second_engine = $pos;
165 if ($line =~ /^<12> /) {
166 handle_position(Position->new($line));
168 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
169 my ($who, $msg) = ($1, $2);
171 next if (grep { $_ eq $who } (@remoteglotconf::masters) == 0);
173 if ($msg =~ /^fics (.*?)$/) {
174 $t->cmd("tell $who Executing '$1' on FICS.");
176 } elsif ($msg =~ /^uci (.*?)$/) {
177 $t->cmd("tell $who Sending '$1' to the engine.");
178 print { $engine->{'write'} } "$1\n";
179 } elsif ($msg =~ /^pgn (.*?)$/) {
181 $t->cmd("tell $who Starting to poll '$url'.");
183 } elsif ($msg =~ /^stoppgn$/) {
184 $t->cmd("tell $who Stopping poll.");
186 } elsif ($msg =~ /^quit$/) {
187 $t->cmd("tell $who Bye bye.");
190 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
193 #print "FICS: [$line]\n";
196 # Starts periodic fetching of PGNs from the given URL.
199 AnyEvent::HTTP::http_get($url, sub {
200 handle_pgn(@_, $url);
204 my ($last_pgn_white, $last_pgn_black);
205 my @last_pgn_uci_moves = ();
206 my $pgn_hysteresis_counter = 0;
209 my ($body, $header, $url) = @_;
210 my $pgn = Chess::PGN::Parse->new(undef, $body);
211 if (!defined($pgn) || !$pgn->read_game()) {
212 warn "Error in parsing PGN from $url\n";
214 $pgn->quick_parse_game;
215 my $pos = Position->start_pos($pgn->white, $pgn->black);
216 my $moves = $pgn->moves;
218 for my $move (@$moves) {
219 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
220 push @uci_moves, Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
221 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
223 $pos->{'history'} = \@uci_moves;
224 $pos->{'pretty_history'} = $moves;
226 # Sometimes, PGNs lose a move or two for a short while,
227 # or people push out new ones non-atomically.
228 # Thus, if we PGN doesn't change names but becomes
229 # shorter, we mistrust it for a few seconds.
231 if (defined($last_pgn_white) && defined($last_pgn_black) &&
232 $last_pgn_white eq $pgn->white &&
233 $last_pgn_black eq $pgn->black &&
234 scalar(@uci_moves) < scalar(@last_pgn_uci_moves)) {
235 if (++$pgn_hysteresis_counter < 3) {
240 $last_pgn_white = $pgn->white;
241 $last_pgn_black = $pgn->black;
242 @last_pgn_uci_moves = @uci_moves;
243 $pgn_hysteresis_counter = 0;
244 handle_position($pos);
248 $http_timer = AnyEvent->timer(after => 1.0, cb => sub {
253 sub handle_position {
256 # if this is already in the queue, ignore it
257 return if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen());
259 # if we're already chewing on this and there's nothing else in the queue,
261 return if (!defined($pos_waiting) && defined($pos_calculating) &&
262 $pos->fen() eq $pos_calculating->fen());
264 # if we're already thinking on something, stop and wait for the engine
266 if (defined($pos_calculating)) {
267 if (!defined($pos_waiting)) {
268 uciprint($engine, "stop");
270 if ($remoteglotconf::uci_assume_full_compliance) {
273 uciprint($engine, "position fen " . $pos->fen());
274 uciprint($engine, "go infinite");
275 $pos_calculating = $pos;
278 # it's wrong just to give the FEN (the move history is useful,
279 # and per the UCI spec, we should really have sent "ucinewgame"),
281 uciprint($engine, "position fen " . $pos->fen());
282 uciprint($engine, "go infinite");
283 $pos_calculating = $pos;
286 if (defined($engine2)) {
287 if (defined($pos_calculating_second_engine)) {
288 uciprint($engine2, "stop");
290 uciprint($engine2, "position fen " . $pos->fen());
291 uciprint($engine2, "go infinite");
292 $pos_calculating_second_engine = $pos;
294 $engine2->{'info'} = {};
297 $engine->{'info'} = {};
300 schedule_tb_lookup();
303 # Output a command every move to note that we're
304 # still paying attention -- this is a good tradeoff,
305 # since if no move has happened in the last half
306 # hour, the analysis/relay has most likely stopped
307 # and we should stop hogging server resources.
313 my ($engine, @x) = @_;
316 my $info = $engine->{'info'};
318 # Search for "multipv" first of all, since e.g. Stockfish doesn't put it first.
319 for my $i (0..$#x - 1) {
320 if ($x[$i] eq 'multipv') {
326 while (scalar @x > 0) {
327 if ($x[0] eq 'multipv') {
333 if ($x[0] eq 'currmove' || $x[0] eq 'currmovenumber' || $x[0] eq 'cpuload') {
335 my $value = shift @x;
336 $info->{$key} = $value;
339 if ($x[0] eq 'depth' || $x[0] eq 'seldepth' || $x[0] eq 'hashfull' ||
340 $x[0] eq 'time' || $x[0] eq 'nodes' || $x[0] eq 'nps' ||
343 my $value = shift @x;
344 $info->{$key . $mpv} = $value;
347 if ($x[0] eq 'score') {
350 delete $info->{'score_cp' . $mpv};
351 delete $info->{'score_mate' . $mpv};
353 while ($x[0] eq 'cp' || $x[0] eq 'mate') {
356 $info->{'score_cp' . $mpv} = shift @x;
357 } elsif ($x[0] eq 'mate') {
359 $info->{'score_mate' . $mpv} = shift @x;
367 $info->{'pv' . $mpv} = [ @x[1..$#x] ];
370 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
374 #print "unknown info '$x[0]', trying to recover...\n";
376 die "Unknown info '" . join(',', @x) . "'";
382 my ($engine, @x) = @_;
384 while (scalar @x > 0) {
385 if ($x[0] =~ /^(name|author)$/) {
387 my $value = join(' ', @x);
388 $engine->{'id'}{$key} = $value;
397 sub prettyprint_pv_no_cache {
398 my ($board, @pvs) = @_;
400 if (scalar @pvs == 0 || !defined($pvs[0])) {
405 my ($from_col, $from_row, $to_col, $to_row, $promo) = parse_uci_move($pv);
406 my ($pretty, $nb) = $board->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
407 return ( $pretty, prettyprint_pv_no_cache($nb, @pvs) );
411 my ($pos, @pvs) = @_;
413 my $cachekey = join('', @pvs);
414 if (exists($pos->{'prettyprint_cache'}{$cachekey})) {
415 return @{$pos->{'prettyprint_cache'}{$cachekey}};
417 my @res = prettyprint_pv_no_cache($pos->{'board'}, @pvs);
418 $pos->{'prettyprint_cache'}{$cachekey} = \@res;
426 return if (!defined($pos_calculating));
428 # Don't update too often.
429 my $age = Time::HiRes::tv_interval($latest_update);
430 if ($age < $remoteglotconf::update_max_interval) {
431 Time::HiRes::alarm($remoteglotconf::update_max_interval + 0.01 - $age);
435 my $info = $engine->{'info'};
438 # If we have tablebase data from a previous lookup, replace the
439 # engine data with the data from the tablebase.
441 my $fen = $pos_calculating->fen();
442 if (exists($tb_cache{$fen})) {
443 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
444 delete $info->{$key . '1'};
445 delete $info->{$key};
447 $info->{'nodes'} = 0;
449 $info->{'depth'} = 0;
450 $info->{'seldepth'} = 0;
451 $info->{'tbhits'} = 0;
453 my $t = $tb_cache{$fen};
455 my $matelen = int((1 + scalar @$pv) / 2);
456 if ($t->{'result'} eq '1/2-1/2') {
457 $info->{'score_cp'} = 0;
458 } elsif ($t->{'result'} eq '1-0') {
459 if ($pos_calculating->{'toplay'} eq 'B') {
460 $info->{'score_mate'} = -$matelen;
462 $info->{'score_mate'} = $matelen;
465 if ($pos_calculating->{'toplay'} eq 'B') {
466 $info->{'score_mate'} = $matelen;
468 $info->{'score_mate'} = -$matelen;
472 $info->{'tablebase'} = 1;
474 $info->{'tablebase'} = 0;
478 # Some programs _always_ report MultiPV, even with only one PV.
479 # In this case, we simply use that data as if MultiPV was never
482 if (exists($info->{'pv1'}) && !exists($info->{'pv2'})) {
483 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
484 if (exists($info->{$key . '1'})) {
485 $info->{$key} = $info->{$key . '1'};
491 # Check the PVs first. if they're invalid, just wait, as our data
492 # is most likely out of sync. This isn't a very good solution, as
493 # it can frequently miss stuff, but it's good enough for most users.
497 if (exists($info->{'pv'})) {
498 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv'}});
502 while (exists($info->{'pv' . $mpv})) {
503 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}});
508 $engine->{'info'} = {};
514 $latest_update = [Time::HiRes::gettimeofday];
518 my $info = $engine->{'info'};
519 my $id = $engine->{'id'};
521 my $text = 'Analysis';
522 if ($pos_calculating->{'last_move'} ne 'none') {
523 if ($pos_calculating->{'toplay'} eq 'W') {
524 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
526 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
528 if (exists($id->{'name'})) {
533 if (exists($id->{'name'})) {
534 $text .= " by $id->{'name'}:\n\n";
539 return unless (exists($pos_calculating->{'board'}));
541 if (exists($info->{'pv1'}) && exists($info->{'pv2'})) {
544 while (exists($info->{'pv' . $mpv})) {
545 $text .= sprintf " PV%2u", $mpv;
546 my $score = short_score($info, $pos_calculating, $mpv);
547 $text .= " ($score)" if (defined($score));
550 if (exists($info->{'tbhits' . $mpv}) && $info->{'tbhits' . $mpv} > 0) {
551 if ($info->{'tbhits' . $mpv} == 1) {
552 $tbhits = ", 1 tbhit";
554 $tbhits = sprintf ", %u tbhits", $info->{'tbhits' . $mpv};
558 if (exists($info->{'nodes' . $mpv}) && exists($info->{'nps' . $mpv}) && exists($info->{'depth' . $mpv})) {
559 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
560 $info->{'nodes' . $mpv} / 1000, $info->{'nps' . $mpv} / 1000, $info->{'depth' . $mpv};
564 $text .= " " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}})) . "\n";
570 my $score = long_score($info, $pos_calculating, '');
571 $text .= " $score\n" if defined($score);
572 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv'}}));
575 if (exists($info->{'nodes'}) && exists($info->{'nps'}) && exists($info->{'depth'})) {
576 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
577 $info->{'nodes'}, $info->{'nps'}, $info->{'depth'};
579 if (exists($info->{'seldepth'})) {
580 $text .= sprintf " (%u selective)", $info->{'seldepth'};
582 if (exists($info->{'tbhits'}) && $info->{'tbhits'} > 0) {
583 if ($info->{'tbhits'} == 1) {
584 $text .= ", one Syzygy hit";
586 $text .= sprintf ", %u Syzygy hits", $info->{'tbhits'};
592 #$text .= book_info($pos_calculating->fen(), $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
594 my @refutation_lines = ();
595 if (defined($engine2)) {
596 for (my $mpv = 1; $mpv < 500; ++$mpv) {
597 my $info = $engine2->{'info'};
598 last if (!exists($info->{'pv' . $mpv}));
600 my $pv = $info->{'pv' . $mpv};
602 my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine, $pv->[0]));
603 my @pretty_pv = prettyprint_pv($pos_calculating_second_engine, @$pv);
604 if (scalar @pretty_pv > 5) {
605 @pretty_pv = @pretty_pv[0..4];
606 push @pretty_pv, "...";
608 my $key = $pretty_move;
609 my $line = sprintf(" %-6s %6s %3s %s",
611 short_score($info, $pos_calculating_second_engine, $mpv),
612 "d" . $info->{'depth' . $mpv},
613 join(', ', @pretty_pv));
614 push @refutation_lines, [ $key, $line ];
619 if ($#refutation_lines >= 0) {
620 $text .= "Shallow search of all legal moves:\n\n";
621 for my $line (sort { $a->[0] cmp $b->[0] } @refutation_lines) {
622 $text .= $line->[1] . "\n";
627 if ($last_text ne $text) {
628 print "
\e[H
\e[2J"; # clear the screen
635 my $info = $engine->{'info'};
638 $json->{'position'} = $pos_calculating->to_json_hash();
639 $json->{'id'} = $engine->{'id'};
640 $json->{'score'} = long_score($info, $pos_calculating, '');
641 $json->{'short_score'} = short_score($info, $pos_calculating, '');
643 $json->{'nodes'} = $info->{'nodes'};
644 $json->{'nps'} = $info->{'nps'};
645 $json->{'depth'} = $info->{'depth'};
646 $json->{'tbhits'} = $info->{'tbhits'};
647 $json->{'seldepth'} = $info->{'seldepth'};
648 $json->{'tablebase'} = $info->{'tablebase'};
650 # single-PV only for now
651 $json->{'pv_uci'} = $info->{'pv'};
652 $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating, @{$info->{'pv'}}) ];
654 my %refutation_lines = ();
655 my @refutation_lines = ();
656 if (defined($engine2)) {
657 for (my $mpv = 1; $mpv < 500; ++$mpv) {
658 my $info = $engine2->{'info'};
659 my $pretty_move = "";
661 last if (!exists($info->{'pv' . $mpv}));
664 my $pv = $info->{'pv' . $mpv};
665 my $pretty_move = join('', prettyprint_pv($pos_calculating, $pv->[0]));
666 my @pretty_pv = prettyprint_pv($pos_calculating, @$pv);
667 $refutation_lines{$pv->[0]} = {
668 sort_key => $pretty_move,
669 depth => $info->{'depth' . $mpv},
670 score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
671 pretty_score => short_score($info, $pos_calculating, $mpv),
672 pretty_move => $pretty_move,
673 pv_pretty => \@pretty_pv,
675 $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
679 $json->{'refutation_lines'} = \%refutation_lines;
681 open my $fh, ">", $remoteglotconf::json_output . ".tmp"
683 print $fh JSON::XS::encode_json($json);
685 rename($remoteglotconf::json_output . ".tmp", $remoteglotconf::json_output);
689 my ($engine, $msg) = @_;
690 $engine->print($msg);
691 print UCILOG localtime() . " $engine->{'tag'} => $msg\n";
695 my ($info, $pos, $mpv) = @_;
697 my $invert = ($pos->{'toplay'} eq 'B');
698 if (defined($info->{'score_mate' . $mpv})) {
700 return sprintf "M%3d", -$info->{'score_mate' . $mpv};
702 return sprintf "M%3d", $info->{'score_mate' . $mpv};
705 if (exists($info->{'score_cp' . $mpv})) {
706 my $score = $info->{'score_cp' . $mpv} * 0.01;
708 if ($info->{'tablebase'}) {
717 return sprintf "%+5.2f", $score;
725 my ($info, $pos, $mpv, $invert) = @_;
727 if (defined($info->{'score_mate' . $mpv})) {
728 my $mate = $info->{'score_mate' . $mpv};
732 $score = 99999 - $mate;
734 # Side to move is getting mated (note the double negative for $mate)
735 $score = -99999 - $mate;
742 if (exists($info->{'score_cp' . $mpv})) {
743 my $score = $info->{'score_cp' . $mpv};
755 my ($info, $pos, $mpv) = @_;
757 if (defined($info->{'score_mate' . $mpv})) {
758 my $mate = $info->{'score_mate' . $mpv};
759 if ($pos->{'toplay'} eq 'B') {
763 return sprintf "White mates in %u", $mate;
765 return sprintf "Black mates in %u", -$mate;
768 if (exists($info->{'score_cp' . $mpv})) {
769 my $score = $info->{'score_cp' . $mpv} * 0.01;
771 if ($info->{'tablebase'}) {
772 return "Theoretical draw";
774 return "Score: 0.00";
777 if ($pos->{'toplay'} eq 'B') {
780 return sprintf "Score: %+5.2f", $score;
789 my ($fen, $board, $toplay) = @_;
791 if (exists($book_cache{$fen})) {
792 return $book_cache{$fen};
795 my $ret = `./booklook $fen`;
796 return "" if ($ret =~ /Not found/ || $ret eq '');
800 for my $m (split /\n/, $ret) {
801 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
805 $pmove = '(current)';
807 ($pmove) = prettyprint_pv_no_cache($board, $move);
808 $pmove .= $annotation;
812 if ($toplay eq 'W') {
813 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
815 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
817 my $n = $win + $draw + $lose;
823 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
826 push @moves, [ $pmove, $n, $percent, $rating ];
829 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
831 my $text = "Book moves:\n\n Perf. N Rating\n\n";
833 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]
839 sub schedule_tb_lookup {
840 return if (!defined($remoteglotconf::tb_serial_key));
841 my $pos = $pos_waiting // $pos_calculating;
842 return if (exists($tb_cache{$pos->fen()}));
844 # If there's more than seven pieces, there's not going to be an answer,
846 return if ($pos->num_pieces() > 7);
848 # Max one at a time. If it's still relevant when it returns,
849 # schedule_tb_lookup() will be called again.
850 return if ($tb_lookup_running);
852 $tb_lookup_running = 1;
853 my $url = 'http://158.250.18.203:6904/tasks/addtask?auth.login=' .
854 $remoteglotconf::tb_serial_key .
855 '&auth.password=aquarium&type=0&fen=' .
856 URI::Escape::uri_escape($pos->fen());
857 print TBLOG "Downloading $url...\n";
858 AnyEvent::HTTP::http_get($url, sub {
859 handle_tb_lookup_return(@_, $pos, $pos->fen());
863 sub handle_tb_lookup_return {
864 my ($body, $header, $pos, $fen) = @_;
865 print TBLOG "Response for [$fen]:\n";
866 print TBLOG $header . "\n\n";
867 print TBLOG $body . "\n\n";
869 my $response = JSON::XS::decode_json($body);
870 if ($response->{'ErrorCode'} != 0) {
871 die "Unknown tablebase server error: " . $response->{'ErrorDesc'};
873 my $state = $response->{'Response'}{'StateString'};
874 if ($state eq 'COMPLETE') {
875 my $pgn = Chess::PGN::Parse->new(undef, $response->{'Response'}{'Moves'});
876 if (!defined($pgn) || !$pgn->read_game()) {
877 warn "Error in parsing PGN\n";
879 $pgn->quick_parse_game;
881 my $moves = $pgn->moves;
883 for my $move (@$moves) {
884 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pvpos->parse_pretty_move($move);
885 push @uci_moves, Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
886 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
889 result => $pgn->result,
894 } elsif ($state =~ /QUEUED/ || $state =~ /PROCESSING/) {
895 # Try again in a second. Note that if we have changed
896 # position in the meantime, we might query a completely
897 # different position! But that's fine.
899 die "Unknown response state " . $state;
902 # Wait a second before we schedule another one.
903 $tb_retry_timer = AnyEvent->timer(after => 1.0, cb => sub {
904 $tb_lookup_running = 0;
905 schedule_tb_lookup();
909 warn "Error in tablebase lookup: $@";
911 # Don't try this one again, but don't block new lookups either.
912 $tb_lookup_running = 0;
917 my ($cmdline, $tag, $cb) = @_;
918 return undef if (!defined($cmdline));
919 return Engine->open($cmdline, $tag, $cb);
922 sub col_letter_to_num {
923 return ord(shift) - ord('a');
926 sub row_letter_to_num {
927 return 7 - (ord(shift) - ord('1'));
932 my $from_col = col_letter_to_num(substr($move, 0, 1));
933 my $from_row = row_letter_to_num(substr($move, 1, 1));
934 my $to_col = col_letter_to_num(substr($move, 2, 1));
935 my $to_row = row_letter_to_num(substr($move, 3, 1));
936 my $promo = substr($move, 4, 1);
937 return ($from_col, $from_row, $to_col, $to_row, $promo);