-
-/// MovePicker::score_captures(), MovePicker::score_noncaptures() and
-/// MovePicker::score_evasions() assign a numerical move ordering score
-/// to each move in a move list. The moves with highest scores will be
-/// picked first by next_move().
-
-void MovePicker::score_captures() {
- // Winning and equal captures in the main search are ordered by MVV/LVA.
- // Suprisingly, this appears to perform slightly better than SEE based
- // move ordering. The reason is probably that in a position with a winning
- // capture, capturing a more valuable (but sufficiently defended) piece
- // first usually doesn't hurt. The opponent will have to recapture, and
- // the hanging piece will still be hanging (except in the unusual cases
- // where it is possible to recapture with the hanging piece). Exchanging
- // big pieces before capturing a hanging piece probably helps to reduce
- // the subtree size.
- // In main search we want to push captures with negative SEE values to
- // badCaptures[] array, but instead of doing it now we delay till when
- // the move has been picked up in pick_move_from_list(), this way we save
- // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
- Move m;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m));
-
- if (is_promotion(m))
- cur->score += PieceValueMidgame[promotion_type(m)];
- }
-}
-
-void MovePicker::score_noncaptures() {
-
- Move m;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- cur->score = H.value(pos.piece_moved(m), to_sq(m));
- }
-}
-
-void MovePicker::score_evasions() {
- // Try good captures ordered by MVV/LVA, then non-captures if destination square
- // is not under attack, ordered by history value, then bad-captures and quiet
- // moves with a negative SEE. This last group is ordered by the SEE score.
- Move m;
- int seeScore;
-
- if (lastMove < moves + 2)
- return;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- if ((seeScore = pos.see_sign(m)) < 0)
- cur->score = seeScore - History::MaxValue; // Be sure we are at the bottom
- else if (pos.is_capture(m))
- cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m)) + History::MaxValue;
- else
- cur->score = H.value(pos.piece_moved(m), to_sq(m));
- }
-}
-
-
-/// MovePicker::generate_next() generates, scores and sorts the next bunch of moves,
-/// when there are no more moves to try for the current phase.
-
-void MovePicker::generate_next() {
-
- curMove = moves;
-
- switch (++phase) {
-
- case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case KILLERS_S1:
- curMove = killers;
- lastMove = curMove + 2;
- return;
-
- case QUIETS_1_S1:
- lastQuiet = lastMove = generate<MV_QUIET>(pos, moves);
- score_noncaptures();
- lastMove = std::partition(curMove, lastMove, has_positive_score);
- sort<MoveStack>(curMove, lastMove);
- return;
-
- case QUIETS_2_S1:
- curMove = lastMove;
- lastMove = lastQuiet;
- if (depth >= 3 * ONE_PLY)
- sort<MoveStack>(curMove, lastMove);
- return;
-
- case BAD_CAPTURES_S1:
- // Just pick them in reverse order to get MVV/LVA ordering
- curMove = moves + MAX_MOVES - 1;
- lastMove = lastBadCapture;
- return;
-
- case EVASIONS_S2:
- lastMove = generate<MV_EVASION>(pos, moves);
- score_evasions();
- return;
-
- case QUIET_CHECKS_S3:
- lastMove = generate<MV_QUIET_CHECK>(pos, moves);
- return;
-
- case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
- phase = STOP;
- case STOP:
- lastMove = curMove + 1; // Avoid another next_phase() call
- return;
-
- default:
- assert(false);
- }
+/// score() assigns a numerical value to each move in a list, used for sorting.
+/// Captures are ordered by Most Valuable Victim (MVV), preferring captures
+/// near our home rank. Quiets are ordered using the histories.
+template<GenType T>
+void MovePicker::score() {
+
+ for (auto& m : *this)
+ if (T == CAPTURES || (T == EVASIONS && pos.capture(m)))
+ m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ - (T == EVASIONS ? Value(type_of(pos.moved_piece(m)))
+ : Value(200 * relative_rank(pos.side_to_move(), to_sq(m))));
+ else if (T == QUIETS)
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ + (*contHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ + (*contHistory[1])[pos.moved_piece(m)][to_sq(m)]
+ + (*contHistory[3])[pos.moved_piece(m)][to_sq(m)];
+
+ else // Quiet evasions
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28);