-
-/// MovePicker::go_next_phase() generates, scores and sorts the next bunch
-/// of moves when there are no more moves to try for the current phase.
-
-void MovePicker::go_next_phase() {
-
- curMove = moves;
- phase = *(++phasePtr);
- switch (phase) {
-
- case PH_TT_MOVE:
- lastMove = curMove + 1;
- return;
-
- case PH_GOOD_CAPTURES:
- case PH_GOOD_PROBCUT:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case PH_KILLERS:
- curMove = killers;
- lastMove = curMove + 2;
- return;
-
- case PH_NONCAPTURES_1:
- lastNonCapture = lastMove = generate<MV_NON_CAPTURE>(pos, moves);
- score_noncaptures();
- sort_moves(moves, lastNonCapture, &lastMove);
- return;
-
- case PH_NONCAPTURES_2:
- curMove = lastMove;
- lastMove = lastNonCapture;
- if (depth >= 3 * ONE_PLY)
- insertion_sort<MoveStack>(curMove, lastMove);
- return;
-
- case PH_BAD_CAPTURES:
- // Bad captures SEE value is already calculated so just pick
- // them in order to get SEE move ordering.
- curMove = badCaptures;
- lastMove = moves + MAX_MOVES;
- return;
-
- case PH_EVASIONS:
- assert(pos.in_check());
- lastMove = generate<MV_EVASION>(pos, moves);
- score_evasions();
- return;
-
- case PH_QCAPTURES:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case PH_QRECAPTURES:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- return;
-
- case PH_QCHECKS:
- lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
- return;
-
- case PH_STOP:
- lastMove = curMove + 1; // Avoid another go_next_phase() call
- return;
-
- default:
- assert(false);
- return;
- }
+// Assigns a numerical value to each move in a list, used
+// for sorting. Captures are ordered by Most Valuable Victim (MVV), preferring
+// captures with a good history. Quiets moves are ordered using the history tables.
+template<GenType Type>
+void MovePicker::score() {
+
+ static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
+
+ [[maybe_unused]] Bitboard threatenedByPawn, threatenedByMinor, threatenedByRook,
+ threatenedPieces;
+ if constexpr (Type == QUIETS)
+ {
+ Color us = pos.side_to_move();
+
+ threatenedByPawn = pos.attacks_by<PAWN>(~us);
+ threatenedByMinor =
+ pos.attacks_by<KNIGHT>(~us) | pos.attacks_by<BISHOP>(~us) | threatenedByPawn;
+ threatenedByRook = pos.attacks_by<ROOK>(~us) | threatenedByMinor;
+
+ // Pieces threatened by pieces of lesser material value
+ threatenedPieces = (pos.pieces(us, QUEEN) & threatenedByRook)
+ | (pos.pieces(us, ROOK) & threatenedByMinor)
+ | (pos.pieces(us, KNIGHT, BISHOP) & threatenedByPawn);
+ }
+
+ for (auto& m : *this)
+ if constexpr (Type == CAPTURES)
+ m.value =
+ (7 * int(PieceValue[pos.piece_on(to_sq(m))])
+ + (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))])
+ / 16;
+
+ else if constexpr (Type == QUIETS)
+ {
+ Piece pc = pos.moved_piece(m);
+ PieceType pt = type_of(pos.moved_piece(m));
+ Square from = from_sq(m);
+ Square to = to_sq(m);
+
+ // histories
+ m.value = 2 * (*mainHistory)[pos.side_to_move()][from_to(m)];
+ m.value += 2 * (*pawnHistory)[pawn_structure(pos)][pc][to];
+ m.value += 2 * (*continuationHistory[0])[pc][to];
+ m.value += (*continuationHistory[1])[pc][to];
+ m.value += (*continuationHistory[2])[pc][to] / 4;
+ m.value += (*continuationHistory[3])[pc][to];
+ m.value += (*continuationHistory[5])[pc][to];
+
+ // bonus for checks
+ m.value += bool(pos.check_squares(pt) & to) * 16384;
+
+ // bonus for escaping from capture
+ m.value += threatenedPieces & from ? (pt == QUEEN && !(to & threatenedByRook) ? 50000
+ : pt == ROOK && !(to & threatenedByMinor) ? 25000
+ : !(to & threatenedByPawn) ? 15000
+ : 0)
+ : 0;
+
+ // malus for putting piece en prise
+ m.value -= !(threatenedPieces & from)
+ ? (pt == QUEEN ? bool(to & threatenedByRook) * 50000
+ + bool(to & threatenedByMinor) * 10000
+ + bool(to & threatenedByPawn) * 20000
+ : pt == ROOK ? bool(to & threatenedByMinor) * 25000
+ + bool(to & threatenedByPawn) * 10000
+ : pt != PAWN ? bool(to & threatenedByPawn) * 15000
+ : 0)
+ : 0;
+ }
+
+ else // Type == EVASIONS
+ {
+ if (pos.capture_stage(m))
+ m.value = PieceValue[pos.piece_on(to_sq(m))] - Value(type_of(pos.moved_piece(m)))
+ + (1 << 28);
+ else
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ + (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ + (*pawnHistory)[pawn_structure(pos)][pos.moved_piece(m)][to_sq(m)];
+ }