- ss->eval = staticValue;
- update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval);
- }
-
- // Initialize "stand pat score", and return it immediately if it is
- // at least beta.
- bestValue = staticValue;
-
- if (bestValue >= beta)
- {
- // Store the score to avoid a future costly evaluation() call
- if (!isCheck && !tte)
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, Depth(-127*OnePly), MOVE_NONE, ss->eval, ei.kingDanger[pos.side_to_move()]);
-
- return bestValue;
- }
-
- if (bestValue > alpha)
- alpha = bestValue;
-
- // If we are near beta then try to get a cutoff pushing checks a bit further
- bool deepChecks = (depth == -OnePly && staticValue >= beta - PawnValueMidgame / 8);
-
- // Initialize a MovePicker object for the current position, and prepare
- // to search the moves. Because the depth is <= 0 here, only captures,
- // queen promotions and checks (only if depth == 0 or depth == -OnePly
- // and we are near beta) will be generated.
- MovePicker mp = MovePicker(pos, ttMove, deepChecks ? Depth(0) : depth, H);
- CheckInfo ci(pos);
- enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame;
- futilityBase = staticValue + FutilityMarginQS + ei.kingDanger[pos.side_to_move()];
-
- // Loop through the moves until no moves remain or a beta cutoff occurs
- while ( alpha < beta
- && (move = mp.get_next_move()) != MOVE_NONE)
- {
- assert(move_is_ok(move));
-
- moveIsCheck = pos.move_is_check(move, ci);
-
- // Update current move
- moveCount++;
- ss->currentMove = move;
-
- // Futility pruning
- if ( !PvNode
- && enoughMaterial
- && !isCheck
- && !moveIsCheck
- && move != ttMove
- && !move_is_promotion(move)
- && !pos.move_is_passed_pawn_push(move))
- {
- futilityValue = futilityBase
- + pos.endgame_value_of_piece_on(move_to(move))
- + (move_is_ep(move) ? PawnValueEndgame : Value(0));
-
- if (futilityValue < alpha)
- {
- if (futilityValue > bestValue)
- bestValue = futilityValue;
- continue;
- }
- }
-
- // Detect blocking evasions that are candidate to be pruned
- evasionPrunable = isCheck
- && bestValue > value_mated_in(PLY_MAX)
- && !pos.move_is_capture(move)
- && pos.type_of_piece_on(move_from(move)) != KING
- && !pos.can_castle(pos.side_to_move());
-
- // Don't search moves with negative SEE values
- if ( !PvNode
- && (!isCheck || evasionPrunable)
- && move != ttMove
- && !move_is_promotion(move)
- && pos.see_sign(move) < 0)
- continue;