- // Use MVV/LVA ordering
- for (int i = 0; i < numOfMoves; i++)
- {
- Move m = moves[i].move;
- if (move_is_promotion(m))
- moves[i].score = QueenValueMidgame;
- else
- moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
- -int(pos.type_of_piece_on(move_from(m)));
+/// generate_next_stage() generates, scores and sorts the next bunch of moves,
+/// when there are no more moves to try for the current stage.
+
+void MovePicker::generate_next_stage() {
+
+ cur = moves;
+
+ switch (++stage) {
+
+ 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;
+
+ killers[0].move = ss->killers[0];
+ killers[1].move = ss->killers[1];
+ killers[2].move = killers[3].move = MOVE_NONE;
+ killers[4].move = killers[5].move = MOVE_NONE;
+
+ // Please note that following code is racy and could yield to rare (less
+ // than 1 out of a million) duplicated entries in SMP case. This is harmless.
+
+ // Be sure countermoves are different from killers
+ for (int i = 0; i < 2; ++i)
+ if ( countermoves[i] != (cur+0)->move
+ && countermoves[i] != (cur+1)->move)
+ (end++)->move = countermoves[i];
+
+ // Be sure followupmoves are different from killers and countermoves
+ for (int i = 0; i < 2; ++i)
+ if ( followupmoves[i] != (cur+0)->move
+ && followupmoves[i] != (cur+1)->move
+ && followupmoves[i] != (cur+2)->move
+ && followupmoves[i] != (cur+3)->move)
+ (end++)->move = followupmoves[i];
+ return;
+
+ case QUIETS_1_S1:
+ endQuiets = end = generate<QUIETS>(pos, moves);
+ score<QUIETS>();
+ end = std::partition(cur, end, has_positive_value);
+ 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:
+ stage = STOP;
+ /* Fall through */
+
+ case STOP:
+ end = cur + 1; // Avoid another next_phase() call
+ return;
+
+ default:
+ assert(false);