- # white short castling
- if ($pv eq 'e1g1' && $piece eq 'K') {
- # king
- substr($nb[7], 4, 1, '-');
- substr($nb[7], 6, 1, $piece);
-
- # rook
- substr($nb[7], 7, 1, '-');
- substr($nb[7], 5, 1, 'R');
-
- return ($prefix . '0-0', prettyprint_pv(\@nb, $next_move, $next_toplay, @pvs));
- }
-
- # white long castling
- if ($pv eq 'e1c1' && $piece eq 'K') {
- # king
- substr($nb[7], 4, 1, '-');
- substr($nb[7], 2, 1, $piece);
-
- # rook
- substr($nb[7], 0, 1, '-');
- substr($nb[7], 3, 1, 'R');
-
- return ($prefix . '0-0-0', prettyprint_pv(\@nb, $next_move, $next_toplay, @pvs));
- }
-
- # black short castling
- if ($pv eq 'e8g8' && $piece eq 'k') {
- # king
- substr($nb[0], 4, 1, '-');
- substr($nb[0], 6, 1, $piece);
-
- # rook
- substr($nb[0], 7, 1, '-');
- substr($nb[0], 5, 1, 'r');
-
- return ($prefix . '0-0', prettyprint_pv(\@nb, $next_move, $next_toplay, @pvs));
- }
-
- # black long castling
- if ($pv eq 'e8c8' && $piece eq 'k') {
- # king
- substr($nb[0], 4, 1, '-');
- substr($nb[0], 2, 1, $piece);
-
- # rook
- substr($nb[0], 0, 1, '-');
- substr($nb[0], 3, 1, 'r');
-
- return ($prefix . '0-0-0', prettyprint_pv(\@nb, $next_move, $next_toplay, @pvs));
- }
-
- # check if the from-piece is a pawn
- if (lc($piece) eq 'p') {
- # attack?
- 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, '-');
- }
+ #
+ # If we have tablebase data from a previous lookup, replace the
+ # engine data with the data from the tablebase.
+ #
+ my $fen = $pos_calculating->fen();
+ if (exists($tb_cache{$fen})) {
+ for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
+ delete $info->{$key . '1'};
+ delete $info->{$key};
+ }
+ $info->{'nodes'} = 0;
+ $info->{'nps'} = 0;
+ $info->{'depth'} = 0;
+ $info->{'seldepth'} = 0;
+ $info->{'tbhits'} = 0;
+
+ my $t = $tb_cache{$fen};
+ my $pv = $t->{'pv'};
+ my $matelen = int((1 + $t->{'score'}) / 2);
+ if ($t->{'result'} eq '1/2-1/2') {
+ $info->{'score_cp'} = 0;
+ } elsif ($t->{'result'} eq '1-0') {
+ if ($pos_calculating->{'toplay'} eq 'B') {
+ $info->{'score_mate'} = -$matelen;
+ } else {
+ $info->{'score_mate'} = $matelen;