- Depth r = reduction<PvNode>(improving, depth, moveCount);
-
- // Increase reduction for cut nodes and moves with a bad history
- if ( (!PvNode && cutNode)
- || ( thisThread->history[pos.piece_on(to_sq(move))][to_sq(move)] < VALUE_ZERO
- && cmh[pos.piece_on(to_sq(move))][to_sq(move)] <= VALUE_ZERO))
- r += ONE_PLY;
-
- // Decrease reduction for moves with a good history
- if ( thisThread->history[pos.piece_on(to_sq(move))][to_sq(move)] > VALUE_ZERO
- && cmh[pos.piece_on(to_sq(move))][to_sq(move)] > VALUE_ZERO)
- r = std::max(DEPTH_ZERO, r - ONE_PLY);
-
- // Decrease reduction for moves that escape a capture
- if ( r
- && type_of(move) == NORMAL
- && type_of(pos.piece_on(to_sq(move))) != PAWN
- && pos.see(make_move(to_sq(move), from_sq(move))) < VALUE_ZERO)
- r = std::max(DEPTH_ZERO, r - ONE_PLY);
+ int mch = std::max(1, moveCount - (ss-1)->moveCount / 16);
+ Depth r = reduction<PvNode>(improving, depth, mch);
+
+ if (captureOrPromotion)
+ r -= r ? ONE_PLY : DEPTH_ZERO;
+ else
+ {
+ // Increase reduction if ttMove is a capture
+ if (ttCapture)
+ r += ONE_PLY;
+
+ // Increase reduction for cut nodes
+ if (cutNode)
+ r += 2 * ONE_PLY;
+
+ // Decrease reduction for moves that escape a capture. Filter out
+ // castling moves, because they are coded as "king captures rook" and
+ // hence break make_move().
+ else if ( type_of(move) == NORMAL
+ && !pos.see_ge(make_move(to_sq(move), from_sq(move))))
+ r -= 2 * ONE_PLY;
+
+ ss->statScore = thisThread->mainHistory[~pos.side_to_move()][from_to(move)]
+ + (*contHist[0])[movedPiece][to_sq(move)]
+ + (*contHist[1])[movedPiece][to_sq(move)]
+ + (*contHist[3])[movedPiece][to_sq(move)]
+ - 4000;
+
+ // Decrease/increase reduction by comparing opponent's stat score
+ if (ss->statScore > 0 && (ss-1)->statScore < 0)
+ r -= ONE_PLY;
+
+ else if (ss->statScore < 0 && (ss-1)->statScore > 0)
+ r += ONE_PLY;
+
+ // Decrease/increase reduction for moves with a good/bad history
+ r = std::max(DEPTH_ZERO, (r / ONE_PLY - ss->statScore / 20000) * ONE_PLY);
+ }