use strict;
use warnings;
-$SIG{ALRM} = sub { output_screen(); };
-
-$| = 1;
-
+# Configuration
my $server = "freechess.org";
my $target = "Sesse";
# my $engine = "/usr/games/toga2";
my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
+# Program starts here
+$SIG{ALRM} = sub { output_screen(); };
+
+$| = 1;
+
open(FICSLOG, ">ficslog.txt")
or die "ficslog.txt: $!";
print FICSLOG "Log starting.\n";
# open the chess engine
my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
my %uciinfo = ();
+my %uciid = ();
my %ficsinfo = ();
uciprint("uci");
# gobble the options
while (<UCIREAD>) {
/uciok/ && last;
+ handle_uci($_);
}
uciprint("setoption name UCI_AnalyseMode value true");
uciprint("setoption name NalimovPath value c:\\nalimov");
uciprint("setoption name NalimovUsage value Rarely");
uciprint("setoption name Hash value 1024");
-# uciprint("setoption name MultiPV value 3");
+uciprint("setoption name MultiPV value 3");
# uciprint("setoption name Contempt value 1000");
# uciprint("setoption name Outlook value Ultra Optimistic");
uciprint("ucinewgame");
uciprint("stop");
uciprint("position fen $fen");
uciprint("go infinite");
+
+ #
+ # Output a command every move to note that we're
+ # still paying attention -- this is a good tradeoff,
+ # since if no move has happened in the last half
+ # hour, the analysis/relay has most likely stopped
+ # and we should stop hogging server resources.
+ #
+ $t->cmd("date");
}
#print "FICS: [$line]\n";
$sleep = 0;
# any fun on the UCI channel?
if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
my $line = <UCIREAD>;
- chomp $line;
- $line =~ tr/\r//d;
- print UCILOG "<= $line\n";
- if ($line =~ /^info/) {
- my (@infos) = split / /, $line;
- shift @infos;
-
- parse_infos(@infos);
- }
+ handle_uci($line);
$sleep = 0;
# don't update too often
sleep $sleep;
}
+sub handle_uci {
+ my ($line) = @_;
+
+ chomp $line;
+ $line =~ tr/\r//d;
+ print UCILOG "<= $line\n";
+ if ($line =~ /^info/) {
+ my (@infos) = split / /, $line;
+ shift @infos;
+
+ parse_infos(@infos);
+ }
+ if ($line =~ /^id/) {
+ my (@ids) = split / /, $line;
+ shift @ids;
+
+ parse_ids(@ids);
+ }
+}
+
sub parse_infos {
my (@x) = @_;
my $mpv = '';
}
}
+sub parse_ids {
+ my (@x) = @_;
+
+ while (scalar @x > 0) {
+ if ($x[0] =~ /^(name|author)$/) {
+ my $key = shift @x;
+ my $value = join(' ', @x);
+ $uciid{$key} = $value;
+ last;
+ }
+
+ # unknown
+ shift @x;
+ }
+}
+
sub style12_to_fen {
my $str = shift;
my (@x) = split / /, $str;
if (scalar @pvs == 0 || !defined($pvs[0])) {
return ();
}
+
+ my @nb = @$board;
my $pv = shift @pvs;
my $from_col = ord(substr($pv, 0, 1)) - ord('a');
my $pretty;
my $piece = substr($board->[$from_row], $from_col, 1);
+ if ($piece eq '-') {
+ die "Invalid move";
+ }
+
# white short castling
if ($pv eq 'e1g1' && $piece eq 'K') {
- my @nb = @$board;
-
# king
substr($nb[7], 4, 1, '-');
substr($nb[7], 6, 1, $piece);
# white long castling
if ($pv eq 'e1c1' && $piece eq 'K') {
- my @nb = @$board;
-
# king
substr($nb[7], 4, 1, '-');
substr($nb[7], 2, 1, $piece);
# black short castling
if ($pv eq 'e8g8' && $piece eq 'k') {
- my @nb = @$board;
-
# king
substr($nb[0], 4, 1, '-');
substr($nb[0], 6, 1, $piece);
# black long castling
if ($pv eq 'e8c8' && $piece eq 'k') {
- my @nb = @$board;
-
# king
substr($nb[0], 4, 1, '-');
substr($nb[0], 2, 1, $piece);
# check if the from-piece is a pawn
if (lc($piece) eq 'p') {
# attack?
- if (substr($board->[$to_row], $to_col, 1) ne '-') {
+ if ($from_col != $to_col) {
$pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
+
+ # en passant?
+ if (substr($board->[$to_row], $to_col, 1) eq '-') {
+ if ($piece eq 'p') {
+ substr($nb[$to_row + 1], $to_col, 1, '-');
+ } else {
+ substr($nb[$to_row - 1], $to_col, 1, '-');
+ }
+ }
} else {
$pretty = substr($pv, 2, 2);
}
# update the board
- my @nb = @$board;
substr($nb[$from_row], $from_col, 1, '-');
substr($nb[$to_row], $to_col, 1, $piece);
sub output_screen {
#return;
- print "\ecAnalysis:\n";
+ #
+ # Check the PVs first. if they're invalid, just wait, as our data
+ # is most likely out of sync. This isn't a very good solution, as
+ # it can frequently miss stuff, but it's good enough for most users.
+ #
+ eval {
+ my $dummy;
+ if (exists($uciinfo{'pv'})) {
+ $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
+ }
+
+ my $mpv = 1;
+ while (exists($uciinfo{'pv' . $mpv})) {
+ $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
+ ++$mpv;
+ }
+ };
+ if ($@) {
+ return;
+ }
+
+ print "\e[H\e[2J"; # clear the screen
+ if (exists($uciid{'name'})) {
+ print "Analysis by $uciid{'name'}:\n\n";
+ } else {
+ print "Analysis:\n\n";
+ }
return unless (exists($ficsinfo{'board'}));
- if (exists($uciinfo{'pv1'})) {
+ if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
# multi-PV
my $mpv = 1;
while (exists($uciinfo{'pv' . $mpv})) {
++$mpv;
}
} else {
+ #
+ # Some programs _always_ report MultiPV, even with only one PV.
+ # In this case, we simply use that data as if MultiPV was never
+ # specified.
+ #
+ if (exists($uciinfo{'pv1'})) {
+ for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
+ if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
+ $uciinfo{$key} = $uciinfo{$key . '1'};
+ }
+ }
+ }
+
# single-PV
if (defined($uciinfo{'score_mate'})) {
my $mate = $uciinfo{'score_mate'};