-template<>
-void MovePicker::score<QUIETS>() {
-
- Move m;
-
- for (MoveStack* it = moves; it != end; ++it)
- {
- m = it->move;
- it->score = Hist[pos.piece_moved(m)][to_sq(m)];
- }
-}
-
-template<>
-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;
-
- for (MoveStack* it = moves; it != end; ++it)
- {
- m = it->move;
- if ((seeScore = pos.see_sign(m)) < 0)
- it->score = seeScore - History::Max; // At the bottom
-
- else if (pos.is_capture(m))
- it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m)) + History::Max;
- else
- it->score = Hist[pos.piece_moved(m)][to_sq(m)];
- }
-}
-
-
-/// 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() {
-
- cur = moves;
-
- switch (++phase) {
-
- case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
- end = generate<CAPTURES>(pos, moves);
- score<CAPTURES>();
- return;
-
- case KILLERS_S1:
- cur = killers;
- end = cur + 2;
- return;
-
- case QUIETS_1_S1:
- endQuiets = end = generate<QUIETS>(pos, moves);
- score<QUIETS>();
- end = std::partition(cur, end, has_positive_score);
- insertion_sort(cur, end);
- return;
-
- case QUIETS_2_S1:
- cur = end;
- end = endQuiets;
- if (depth >= 3 * ONE_PLY)
- insertion_sort(cur, end);
- return;
-
- case BAD_CAPTURES_S1:
- // Just pick them in reverse order to get MVV/LVA ordering
- cur = moves + MAX_MOVES - 1;
- end = endBadCaptures;
- return;
-
- case EVASIONS_S2:
- end = generate<EVASIONS>(pos, moves);
- if (end > moves + 1)
- score<EVASIONS>();
- return;
-
- case QUIET_CHECKS_S3:
- end = generate<QUIET_CHECKS>(pos, moves);
- return;
-
- case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
- phase = STOP;
- case STOP:
- end = cur + 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 Type>
+void MovePicker::score() {
+
+ static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
+
+ for (auto& m : *this)
+ if (Type == CAPTURES)
+ m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ - Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
+
+ else if (Type == 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 // Type == EVASIONS
+ {
+ if (pos.capture(m))
+ m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ - Value(type_of(pos.moved_piece(m)));
+ else
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28);
+ }