]> git.sesse.net Git - stockfish/blobdiff - src/movepick.cpp
Generate pseudo-legal moves in generate_evasions()
[stockfish] / src / movepick.cpp
index ea435026241f21b72275af2fd0585b35480ff9a8..a2ea3a7354f951dcb9edce6007118874db36e832 100644 (file)
@@ -23,7 +23,6 @@
 //// Includes
 ////
 
-#include <algorithm>
 #include <cassert>
 
 #include "history.h"
@@ -123,7 +122,6 @@ void MovePicker::go_next_phase() {
   case PH_GOOD_CAPTURES:
       lastMove = generate_captures(pos, moves);
       score_captures();
-      std::sort(moves, lastMove);
       return;
 
   case PH_KILLERS:
@@ -134,7 +132,7 @@ void MovePicker::go_next_phase() {
   case PH_NONCAPTURES:
       lastMove = generate_noncaptures(pos, moves);
       score_noncaptures();
-      std::sort(moves, lastMove);
+      sort_moves(moves, lastMove);
       return;
 
   case PH_BAD_CAPTURES:
@@ -142,20 +140,17 @@ void MovePicker::go_next_phase() {
       // to get SEE move ordering.
       curMove = badCaptures;
       lastMove = lastBadCapture;
-      std::sort(badCaptures, lastMove);
       return;
 
   case PH_EVASIONS:
       assert(pos.is_check());
       lastMove = generate_evasions(pos, moves, pinned);
       score_evasions();
-      std::sort(moves, lastMove);
       return;
 
   case PH_QCAPTURES:
       lastMove = generate_captures(pos, moves);
       score_captures();
-      std::sort(moves, lastMove);
       return;
 
   case PH_QCHECKS:
@@ -202,8 +197,8 @@ void MovePicker::score_captures() {
       if (move_is_promotion(m))
           cur->score = QueenValueMidgame;
       else
-          cur->score = int(pos.midgame_value_of_piece_on(move_to(m)))
-                      -int(pos.type_of_piece_on(move_from(m)));
+          cur->score =  pos.midgame_value_of_piece_on(move_to(m))
+                      - pos.type_of_piece_on(move_from(m));
   }
 }
 
@@ -234,19 +229,25 @@ void MovePicker::score_noncaptures() {
 }
 
 void MovePicker::score_evasions() {
-
+  // Always try ttMove as first. Then try good captures ordered
+  // by MVV/LVA, then non-captures if destination square is not
+  // under attack, ordered by history value, and at the end
+  // bad-captures and non-captures with a negative SEE. This
+  // last group is ordered by the SEE score.
   Move m;
+  int seeScore;
 
   for (MoveStack* cur = moves; cur != lastMove; cur++)
   {
       m = cur->move;
       if (m == ttMoves[0].move)
           cur->score = 2 * HistoryMax;
-      else if (!pos.square_is_empty(move_to(m)))
-      {
-          int seeScore = pos.see(m);
-          cur->score = seeScore + (seeScore >= 0 ? HistoryMax : 0);
-      } else
+      else if ((seeScore = pos.see_sign(m)) < 0)
+          cur->score = seeScore;
+      else if (pos.move_is_capture(m))
+          cur->score =  pos.midgame_value_of_piece_on(move_to(m))
+                      - pos.type_of_piece_on(move_from(m)) + HistoryMax;
+      else
           cur->score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
   }
 }
@@ -268,17 +269,17 @@ Move MovePicker::get_next_move() {
   {
       while (curMove != lastMove)
       {
-          move = (curMove++)->move;
-
           switch (phase) {
 
           case PH_TT_MOVES:
+              move = (curMove++)->move;
               if (   move != MOVE_NONE
                   && move_is_legal(pos, move, pinned))
                   return move;
               break;
 
           case PH_GOOD_CAPTURES:
+              move = pick_best(curMove++, lastMove).move;
               if (   move != ttMoves[0].move
                   && move != ttMoves[1].move
                   && pos.pl_move_is_legal(move, pinned))
@@ -298,6 +299,7 @@ Move MovePicker::get_next_move() {
               break;
 
           case PH_KILLERS:
+              move = (curMove++)->move;
               if (   move != MOVE_NONE
                   && move != ttMoves[0].move
                   && move != ttMoves[1].move
@@ -307,6 +309,7 @@ Move MovePicker::get_next_move() {
               break;
 
           case PH_NONCAPTURES:
+              move = (curMove++)->move;
               if (   move != ttMoves[0].move
                   && move != ttMoves[1].move
                   && move != killers[0].move
@@ -315,13 +318,25 @@ Move MovePicker::get_next_move() {
                   return move;
               break;
 
-          case PH_EVASIONS:
           case PH_BAD_CAPTURES:
+              move = pick_best(curMove++, lastMove).move;
               return move;
 
+          case PH_EVASIONS:
+              move = pick_best(curMove++, lastMove).move;
+              if (pos.pl_move_is_legal(move, pinned))
+                  return move;
+              break;
+
           case PH_QCAPTURES:
+              move = pick_best(curMove++, lastMove).move;
+              if (   move != ttMoves[0].move
+                  && pos.pl_move_is_legal(move, pinned))
+                  return move;
+              break;
+
           case PH_QCHECKS:
-              // Maybe postpone the legality check until after futility pruning?
+              move = (curMove++)->move;
               if (   move != ttMoves[0].move
                   && pos.pl_move_is_legal(move, pinned))
                   return move;