]> git.sesse.net Git - remoteglot/blob - remoteglot.pl
5409d7bc9645e776629eab76c085799e4bb1a828
[remoteglot] / remoteglot.pl
1 #! /usr/bin/perl
2
3 #
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.)
7 #
8 # Copyright 2007 Steinar H. Gunderson <sgunderson@bigfoot.com>
9 # Licensed under the GNU General Public License, version 2.
10 #
11
12 use Net::Telnet;
13 use FileHandle;
14 use IPC::Open2;
15 use Time::HiRes;
16 use strict;
17 use warnings;
18
19 # Configuration
20 my $server = "freechess.org";
21 my $target = "Sesse";
22 # my $engine = "/usr/games/toga2";
23 my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
24 my $telltarget = undef;   # undef to be silent
25 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960);  # after each move
26
27 # Program starts here
28 $SIG{ALRM} = sub { output_screen(); };
29
30 $| = 1;
31
32 open(FICSLOG, ">ficslog.txt")
33         or die "ficslog.txt: $!";
34 print FICSLOG "Log starting.\n";
35 select(FICSLOG);
36 $| = 1;
37
38 open(UCILOG, ">ucilog.txt")
39         or die "ucilog.txt: $!";
40 print UCILOG "Log starting.\n";
41 select(UCILOG);
42 $| = 1;
43 select(STDOUT);
44
45 # open the chess engine
46 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
47 my %uciinfo = ();
48 my %uciid = ();
49 my %ficsinfo = ();
50 my ($last_move, $last_tell);
51 my $last_text = '';
52 my $last_told_text = '';
53
54 uciprint("uci");
55
56 # gobble the options
57 while (<UCIREAD>) {
58         /uciok/ && last;
59         handle_uci($_);
60 }
61
62 uciprint("setoption name UCI_AnalyseMode value true");
63 uciprint("setoption name NalimovPath value c:\\nalimov");
64 uciprint("setoption name NalimovUsage value Rarely");
65 uciprint("setoption name Hash value 1024");
66 # uciprint("setoption name MultiPV value 3");
67 # uciprint("setoption name Contempt value 1000");
68 # uciprint("setoption name Outlook value Ultra Optimistic");
69 uciprint("ucinewgame");
70
71 print "Chess engine ready.\n";
72
73 # now talk to FICS
74 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
75 $t->input_log(\*FICSLOG);
76 $t->open($server);
77 $t->print("guest");
78 $t->waitfor('/Press return to enter the server/');
79 $t->cmd("");
80
81 # set some options
82 $t->cmd("set shout 0");
83 $t->cmd("set seek 0");
84 $t->cmd("set style 12");
85 $t->cmd("observe $target");
86
87 # main loop
88 print "FICS ready.\n";
89 while (1) {
90         my $rin = '';
91         my $rout;
92         vec($rin, fileno(UCIREAD), 1) = 1;
93         vec($rin, fileno($t), 1) = 1;
94
95         my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
96         my $sleep = 1.0;
97
98         while (1) {
99                 my $line = $t->getline(Timeout => 0, errmode => 'return');
100                 last if (!defined($line));
101
102                 chomp $line;
103                 $line =~ tr/\r//d;
104                 if ($line =~ /^<12> /) {
105                         my $fen = style12_to_fen($line);
106                         uciprint("stop");
107                         uciprint("position fen $fen");
108                         uciprint("go infinite");
109
110                         $last_move = time;
111
112                         # 
113                         # Output a command every move to note that we're
114                         # still paying attention -- this is a good tradeoff,
115                         # since if no move has happened in the last half
116                         # hour, the analysis/relay has most likely stopped
117                         # and we should stop hogging server resources.
118                         #
119                         $t->cmd("date");
120                 }
121                 #print "FICS: [$line]\n";
122                 $sleep = 0;
123         }
124         
125         # any fun on the UCI channel?
126         if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
127                 my $line = <UCIREAD>;
128                 handle_uci($line);
129                 $sleep = 0;
130
131                 # don't update too often
132                 Time::HiRes::alarm(0.2);
133         }
134
135         sleep $sleep;
136 }
137
138 sub handle_uci {
139         my ($line) = @_;
140
141         chomp $line;
142         $line =~ tr/\r//d;
143         print UCILOG "<= $line\n";
144         if ($line =~ /^info/) {
145                 my (@infos) = split / /, $line;
146                 shift @infos;
147
148                 parse_infos(@infos);
149         }
150         if ($line =~ /^id/) {
151                 my (@ids) = split / /, $line;
152                 shift @ids;
153
154                 parse_ids(@ids);
155         }
156 }
157
158 sub parse_infos {
159         my (@x) = @_;
160         my $mpv = '';
161
162         while (scalar @x > 0) {
163                 if ($x[0] =~ 'multipv') {
164                         shift @x;
165                         $mpv = shift @x;
166                         next;
167                 }
168                 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
169                         my $key = shift @x;
170                         my $value = shift @x;
171                         $uciinfo{$key} = $value;
172                         next;
173                 }
174                 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
175                         my $key = shift @x;
176                         my $value = shift @x;
177                         $uciinfo{$key . $mpv} = $value;
178                         next;
179                 }
180                 if ($x[0] eq 'score') {
181                         shift @x;
182
183                         delete $uciinfo{'score_cp' . $mpv};
184                         delete $uciinfo{'score_mate' . $mpv};
185
186                         while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
187                                 if ($x[0] eq 'cp') {
188                                         shift @x;
189                                         $uciinfo{'score_cp' . $mpv} = shift @x;
190                                 } elsif ($x[0] eq 'mate') {
191                                         shift @x;
192                                         $uciinfo{'score_mate' . $mpv} = shift @x;
193                                 } else {
194                                         shift @x;
195                                 }
196                         }
197                         next;
198                 }
199                 if ($x[0] eq 'pv') {
200                         $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
201                         last;
202                 }
203                 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
204                         last;
205                 }
206
207                 #print "unknown info '$x[0]', trying to recover...\n";
208                 #shift @x;
209                 die "Unknown info '" . join(',', @x) . "'";
210
211         }
212 }
213
214 sub parse_ids {
215         my (@x) = @_;
216
217         while (scalar @x > 0) {
218                 if ($x[0] =~ /^(name|author)$/) {
219                         my $key = shift @x;
220                         my $value = join(' ', @x);
221                         $uciid{$key} = $value;
222                         last;
223                 }
224
225                 # unknown
226                 shift @x;
227         }
228 }
229
230 sub style12_to_fen {
231         my $str = shift; 
232         my (@x) = split / /, $str;
233         
234         $ficsinfo{'board'} = [ @x[1..8] ];
235         $ficsinfo{'toplay'} = $x[9];
236         
237         # the board itself
238         my (@board) = @x[1..8];
239         for my $rank (0..7) {
240                 $board[$rank] =~ s/(-+)/length($1)/ge;
241         }
242         my $fen = join('/', @board);
243
244         # white/black to move
245         $fen .= " ";
246         $fen .= lc($x[9]);
247
248         # castling
249         my $castling = "";
250         $castling .= "K" if ($x[11] == 1);
251         $castling .= "Q" if ($x[12] == 1);
252         $castling .= "k" if ($x[13] == 1);
253         $castling .= "q" if ($x[14] == 1);
254         $castling = "-" if ($castling eq "");
255         $fen .= " ";
256         $fen .= $castling;
257
258         # en passant
259         my $ep = "-";
260         if ($x[10] != -1) {
261                 my $col = $x[10];
262                 my $nep = (qw(a b c d e f g h))[$col];
263
264                 if ($x[9] eq 'B') {
265                         $nep .= "3";
266                 } else {
267                         $nep .= "6";
268                 }
269                 
270                 #
271                 # Showing the en passant square when actually no capture can be made
272                 # seems to confuse at least Rybka. Thus, check if there's actually
273                 # a pawn of the opposite side that can do the en passant move, and if
274                 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
275                 # "right" thing as per the standard, though.
276                 #
277                 if ($x[9] eq 'B') {
278                         $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
279                         $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
280                 } else {
281                         $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
282                         $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
283                 }
284         }
285         $fen .= " ";
286         $fen .= $ep;
287
288         # half-move clock
289         $fen .= " ";
290         $fen .= $x[15];
291
292         # full-move clock
293         $fen .= " ";
294         $fen .= $x[26];
295
296         return $fen;
297 }
298
299 sub prettyprint_pv {
300         my ($board, @pvs) = @_;
301         
302         if (scalar @pvs == 0 || !defined($pvs[0])) {
303                 return ();
304         }
305         
306         my @nb = @$board;
307
308         my $pv = shift @pvs;
309         my $from_col = ord(substr($pv, 0, 1)) - ord('a');
310         my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
311         my $to_col   = ord(substr($pv, 2, 1)) - ord('a');
312         my $to_row   = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
313
314         my $pretty;
315         my $piece = substr($board->[$from_row], $from_col, 1);
316
317         if ($piece eq '-') {
318                 die "Invalid move";
319         }
320
321         # white short castling
322         if ($pv eq 'e1g1' && $piece eq 'K') {
323                 # king
324                 substr($nb[7], 4, 1, '-');
325                 substr($nb[7], 6, 1, $piece);
326                 
327                 # rook
328                 substr($nb[7], 7, 1, '-');
329                 substr($nb[7], 5, 1, 'R');
330                                 
331                 return ('0-0', prettyprint_pv(\@nb, @pvs));
332         }
333
334         # white long castling
335         if ($pv eq 'e1c1' && $piece eq 'K') {
336                 # king
337                 substr($nb[7], 4, 1, '-');
338                 substr($nb[7], 2, 1, $piece);
339                 
340                 # rook
341                 substr($nb[7], 0, 1, '-');
342                 substr($nb[7], 3, 1, 'R');
343                                 
344                 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
345         }
346
347         # black short castling
348         if ($pv eq 'e8g8' && $piece eq 'k') {
349                 # king
350                 substr($nb[0], 4, 1, '-');
351                 substr($nb[0], 6, 1, $piece);
352                 
353                 # rook
354                 substr($nb[0], 7, 1, '-');
355                 substr($nb[0], 5, 1, 'r');
356                                 
357                 return ('0-0', prettyprint_pv(\@nb, @pvs));
358         }
359
360         # black long castling
361         if ($pv eq 'e8c8' && $piece eq 'k') {
362                 # king
363                 substr($nb[0], 4, 1, '-');
364                 substr($nb[0], 2, 1, $piece);
365                 
366                 # rook
367                 substr($nb[0], 0, 1, '-');
368                 substr($nb[0], 3, 1, 'r');
369                                 
370                 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
371         }
372
373         # check if the from-piece is a pawn
374         if (lc($piece) eq 'p') {
375                 # attack?
376                 if ($from_col != $to_col) {
377                         $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
378
379                         # en passant?
380                         if (substr($board->[$to_row], $to_col, 1) eq '-') {
381                                 if ($piece eq 'p') {
382                                         substr($nb[$to_row + 1], $to_col, 1, '-');
383                                 } else {
384                                         substr($nb[$to_row - 1], $to_col, 1, '-');
385                                 }
386                         }
387                 } else {
388                         $pretty = substr($pv, 2, 2);
389
390                         if (length($pv) == 5) {
391                                 # promotion
392                                 $pretty .= "=";
393                                 $pretty .= uc(substr($pv, 4, 1));
394
395                                 if ($piece eq 'p') {
396                                         $piece = substr($pv, 4, 1);
397                                 } else {
398                                         $piece = uc(substr($pv, 4, 1));
399                                 }
400                         }
401                 }
402         } else {
403                 $pretty = uc($piece);
404
405                 # see how many of these pieces could go here, in all
406                 my $num_total = 0;
407                 for my $col (0..7) {
408                         for my $row (0..7) {
409                                 next unless (substr($board->[$row], $col, 1) eq $piece);
410                                 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
411                         }
412                 }
413
414                 # see how many of these pieces from the given row could go here
415                 my $num_row = 0;
416                 for my $col (0..7) {
417                         next unless (substr($board->[$from_row], $col, 1) eq $piece);
418                         ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
419                 }
420                 
421                 # and same for columns
422                 my $num_col = 0;
423                 for my $row (0..7) {
424                         next unless (substr($board->[$row], $from_col, 1) eq $piece);
425                         ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
426                 }
427                 
428                 # see if we need to disambiguate
429                 if ($num_total > 1) {
430                         if ($num_col == 1) {
431                                 $pretty .= substr($pv, 0, 1);
432                         } elsif ($num_row == 1) {
433                                 $pretty .= substr($pv, 1, 1);
434                         } else {
435                                 $pretty .= substr($pv, 0, 2);
436                         }
437                 }
438
439                 # attack?
440                 if (substr($board->[$to_row], $to_col, 1) ne '-') {
441                         $pretty .= 'x';
442                 }
443
444                 $pretty .= substr($pv, 2, 2);
445         }
446
447         # update the board
448         substr($nb[$from_row], $from_col, 1, '-');
449         substr($nb[$to_row], $to_col, 1, $piece);
450
451         if (in_mate(\@nb)) {
452                 $pretty .= '#';
453         } elsif (in_check(\@nb) ne 'none') {
454                 $pretty .= '+';
455         }
456
457         return ($pretty, prettyprint_pv(\@nb, @pvs));
458 }
459
460 sub output_screen {
461         #return;
462
463         #
464         # Check the PVs first. if they're invalid, just wait, as our data
465         # is most likely out of sync. This isn't a very good solution, as
466         # it can frequently miss stuff, but it's good enough for most users.
467         #
468         eval {
469                 my $dummy;
470                 if (exists($uciinfo{'pv'})) {
471                         $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
472                 }
473         
474                 my $mpv = 1;
475                 while (exists($uciinfo{'pv' . $mpv})) {
476                         $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
477                         ++$mpv;
478                 }
479         };
480         if ($@) {
481                 return;
482         }
483
484         my $text = '';
485
486         if (exists($uciid{'name'})) {
487                 $text .= "Analysis by $uciid{'name'}:\n\n";
488         } else {
489                 $text .= "Analysis:\n\n";
490         }
491
492         return unless (exists($ficsinfo{'board'}));
493
494         if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
495                 # multi-PV
496                 my $mpv = 1;
497                 while (exists($uciinfo{'pv' . $mpv})) {
498                         $text .= sprintf "  PV%2u", $mpv;
499                         my $score = short_score(\%uciinfo, \%ficsinfo, $mpv);
500                         $text .= "  ($score)" if (!defined($score));
501
502                         if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
503                                 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply)",
504                                         $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
505                         }
506
507                         $text .= ":\n";
508                         $text .= "  " . join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
509                         $text .= "\n";
510                         ++$mpv;
511                 }
512         } else {
513                 #
514                 # Some programs _always_ report MultiPV, even with only one PV.
515                 # In this case, we simply use that data as if MultiPV was never
516                 # specified.
517                 #
518                 if (exists($uciinfo{'pv1'})) {
519                         for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
520                                 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
521                                         $uciinfo{$key} = $uciinfo{$key . '1'};
522                                 }
523                         }
524                 }
525
526                 # single-PV
527                 my $score = long_score(\%uciinfo, \%ficsinfo, '');
528                 $text .= "  $score\n" if defined($score);
529                 $text .=  "  PV: " . join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
530                 $text .=  "\n";
531
532                 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
533                         $text .= sprintf "  %u nodes, %7u nodes/sec, depth %u ply",
534                                 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
535                 }
536                 if (exists($uciinfo{'tbhits'})) {
537                         $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
538                 }
539                 if (exists($uciinfo{'seldepth'})) {
540                         $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
541                 }
542                 $text .=  "\n\n";
543         }
544
545         if ($last_text ne $text) {
546                 print "\e[H\e[2J"; # clear the screen
547                 print $text;
548                 $last_text = $text;
549         }
550
551         # see if a new tell is called for -- it is if the delay has expired _and_
552         # this is not simply a repetition of the last one
553         if ($last_told_text ne $text && defined($telltarget)) {
554                 my $now = time;
555                 for my $iv (@tell_intervals) {
556                         last if ($now - $last_move < $iv);
557                         next if ($last_tell - $last_move >= $iv);
558
559                         for my $line (split /\n/, $text) {
560                                 $t->print("tell $telltarget [$target] $line");
561                         }
562
563                         $last_told_text = $text;
564                         $last_tell = $now;
565
566                         last;
567                 }
568         }
569 }
570
571 sub find_kings {
572         my $board = shift;
573         my ($wkr, $wkc, $bkr, $bkc);
574
575         for my $row (0..7) {
576                 for my $col (0..7) {
577                         my $piece = substr($board->[$row], $col, 1);
578                         if ($piece eq 'K') {
579                                 ($wkr, $wkc) = ($row, $col);
580                         } elsif ($piece eq 'k') {
581                                 ($bkr, $bkc) = ($row, $col);
582                         }
583                 }
584         }
585
586         return ($wkr, $wkc, $bkr, $bkc);
587 }
588
589 sub in_mate {
590         my $board = shift;
591         my $check = in_check($board);
592         return 0 if ($check eq 'none');
593
594         # try all possible moves for the side in check
595         for my $row (0..7) {
596                 for my $col (0..7) {
597                         my $piece = substr($board->[$row], $col, 1);
598                         next if ($piece eq '-');
599
600                         if ($check eq 'white') {
601                                 next if ($piece eq lc($piece));
602                         } else {
603                                 next if ($piece eq uc($piece));
604                         }
605
606                         for my $dest_row (0..7) {
607                                 for my $dest_col (0..7) {
608                                         next if ($row == $dest_row && $col == $dest_col);
609                                         next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
610
611                                         my @nb = @$board;
612                                         substr($nb[$row], $col, 1, '-');
613                                         substr($nb[$dest_row], $dest_col, 1, $piece);
614
615                                         my $new_check = in_check(\@nb);
616                                         return 0 if ($new_check ne $check && $new_check ne 'both');
617                                 }
618                         }
619                 }
620         }
621
622         # nothing to do; mate
623         return 1;
624 }
625
626 sub in_check {
627         my $board = shift;
628         my ($black_check, $white_check) = (0, 0);
629
630         my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
631
632         # check all pieces for the possibility of threatening the two kings
633         for my $row (0..7) {
634                 for my $col (0..7) {
635                         my $piece = substr($board->[$row], $col, 1);
636                         next if ($piece eq '-');
637                 
638                         if (uc($piece) eq $piece) {
639                                 # white piece
640                                 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
641                         } else {
642                                 # black piece
643                                 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
644                         }
645                 }
646         }
647
648         if ($black_check && $white_check) {
649                 return 'both';
650         } elsif ($black_check) {
651                 return 'black';
652         } elsif ($white_check) {
653                 return 'white';
654         } else {
655                 return 'none';
656         }
657 }
658
659 sub can_reach {
660         my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
661         
662         # can't eat your own piece
663         my $dest_piece = substr($board->[$to_row], $to_col, 1);
664         if ($dest_piece ne '-') {
665                 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
666         }
667
668         if (lc($piece) eq 'k') {
669                 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
670         }
671         if (lc($piece) eq 'r') {
672                 return 0 unless ($from_row == $to_row || $from_col == $to_col);
673
674                 # check that there's a clear passage
675                 if ($from_row == $to_row) {
676                         if ($from_col > $to_col) {
677                                 ($to_col, $from_col) = ($from_col, $to_col);
678                         }
679
680                         for my $c (($from_col+1)..($to_col-1)) {
681                                 my $middle_piece = substr($board->[$to_row], $c, 1);
682                                 return 0 if ($middle_piece ne '-');     
683                         }
684
685                         return 1;
686                 } else {
687                         if ($from_row > $to_row) {
688                                 ($to_row, $from_row) = ($from_row, $to_row);
689                         }
690
691                         for my $r (($from_row+1)..($to_row-1)) {
692                                 my $middle_piece = substr($board->[$r], $to_col, 1);
693                                 return 0 if ($middle_piece ne '-');     
694                         }
695
696                         return 1;
697                 }
698         }
699         if (lc($piece) eq 'b') {
700                 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
701
702                 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
703                 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
704
705                 my $r = $from_row + $dr;
706                 my $c = $from_col + $dc;
707
708                 while ($r != $to_row) {
709                         my $middle_piece = substr($board->[$r], $c, 1);
710                         return 0 if ($middle_piece ne '-');
711                         
712                         $r += $dr;
713                         $c += $dc;
714                 }
715
716                 return 1;
717         }
718         if (lc($piece) eq 'n') {
719                 my $diff_r = abs($from_row - $to_row);
720                 my $diff_c = abs($from_col - $to_col);
721                 return 1 if ($diff_r == 2 && $diff_c == 1);
722                 return 1 if ($diff_r == 1 && $diff_c == 2);
723                 return 0;
724         }
725         if ($piece eq 'q') {
726                 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
727                         can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
728         }
729         if ($piece eq 'Q') {
730                 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
731                         can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
732         }
733         if ($piece eq 'p') {
734                 # black pawn
735                 if ($to_col == $from_col && $to_row == $from_row + 1) {
736                         return ($dest_piece eq '-');
737                 }
738                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
739                         return ($dest_piece ne '-');
740                 }
741                 return 0;
742         }
743         if ($piece eq 'P') {
744                 # white pawn
745                 if ($to_col == $from_col && $to_row == $from_row - 1) {
746                         return ($dest_piece eq '-');
747                 }
748                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
749                         return ($dest_piece ne '-');
750                 }
751                 return 0;
752         }
753         
754         # unknown piece
755         return 0;
756 }
757
758 sub uciprint {
759         my $msg = shift;
760         print UCIWRITE "$msg\n";
761         print UCILOG "=> $msg\n";
762 }
763
764 sub short_score {
765         my ($uciinfo, $ficsinfo, $mpv) = @_;
766
767         if (defined($uciinfo{'score_mate' . $mpv})) {
768                 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
769         } else {
770                 if (exists($uciinfo{'score_cp' . $mpv})) {
771                         my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
772                         if ($ficsinfo{'toplay'} eq 'B') {
773                                 $score = -$score;
774                         }
775                         return sprintf "%+5.2f", $score;
776                 }
777         }
778
779         return undef;
780 }
781
782 sub long_score {
783         my ($uciinfo, $ficsinfo, $mpv) = @_;
784
785         if (defined($uciinfo{'score_mate' . $mpv})) {
786                 my $mate = $uciinfo{'score_mate' . $mpv};
787                 if ($ficsinfo{'toplay'} eq 'B') {
788                         $mate = -$mate;
789                 }
790                 if ($mate > 0) {
791                         return sprintf "White mates in %u", $mate;
792                 } else {
793                         return sprintf "Black mates in %u", -$mate;
794                 }
795         } else {
796                 if (exists($uciinfo{'score_cp' . $mpv})) {
797                         my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
798                         if ($ficsinfo{'toplay'} eq 'B') {
799                                 $score = -$score;
800                         }
801                         return sprintf "Score: %+5.2f", $score;
802                 }
803         }
804
805         return undef;
806 }