// search<>() is the main search function for both PV and non-PV nodes and for // normal and SplitPoint nodes. When called just after a split point the search // is simpler because we have already probed the hash table, done a null move // search, and searched the first move before splitting, we don't have to repeat // all this work again. We also don't need to store anything to the hash table // here: This is taken care of after we return from the split point. internal static int search(int NT, Position pos, Stack[] ss, int ssPos, int alpha, int beta, int depth) { var PvNode = (NT == NodeTypeC.PV || NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointRoot); var SpNode = (NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointNonPV || NT == NodeTypeC.SplitPointRoot); var RootNode = (NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointRoot); Debug.Assert(alpha >= -ValueC.VALUE_INFINITE && alpha < beta && beta <= ValueC.VALUE_INFINITE); Debug.Assert((PvNode || alpha == beta - 1)); Debug.Assert(depth > DepthC.DEPTH_ZERO); var ms = MovesSearchedBroker.GetObject(); var movesSearched = ms.movesSearched; StateInfo st = null; var tte = TT.StaticEntry; var tteHasValue = false; uint ttePos = 0; ulong posKey = 0; int ttMove, move, excludedMove, bestMove, threatMove; int ext, newDepth; int bestValue, value, ttValue; int eval = 0, nullValue, futilityValue; bool inCheck, givesCheck, pvMove, singularExtensionNode; bool captureOrPromotion, dangerous, doFullDepthSearch; int moveCount = 0, playedMoveCount = 0; SplitPoint sp = null; // Step 1. Initialize node var thisThread = pos.this_thread(); //var threatExtension = false; inCheck = pos.in_check(); if (SpNode) { sp = ss[ssPos].sp; bestMove = sp.bestMove; threatMove = sp.threatMove; bestValue = sp.bestValue; ttMove = excludedMove = MoveC.MOVE_NONE; ttValue = ValueC.VALUE_NONE; Debug.Assert(sp.bestValue > -ValueC.VALUE_INFINITE && sp.moveCount > 0); goto split_point_start; } bestValue = -ValueC.VALUE_INFINITE; ss[ssPos].currentMove = threatMove = ss[ssPos + 1].excludedMove = bestMove = MoveC.MOVE_NONE; ss[ssPos].ply = ss[ssPos - 1].ply + 1; ss[ssPos + 1].skipNullMove = 0; ss[ssPos + 1].reduction = DepthC.DEPTH_ZERO; ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = MoveC.MOVE_NONE; // Used to send selDepth info to GUI if (PvNode && thisThread.maxPly < ss[ssPos].ply) { thisThread.maxPly = ss[ssPos].ply; } if (!RootNode) { // Step 2. Check for aborted search and immediate draw if ((SignalsStop || pos.is_draw(false) || ss[ssPos].ply > Constants.MAX_PLY)) { MovesSearchedBroker.Free(); return DrawValue[pos.sideToMove]; } // Step 3. Mate distance pruning. Even if we mate at the next move our score // would be at best mate_in(ss->ply+1), but if alpha is already bigger because // a shorter mate was found upward in the tree then there is no need to search // further, we will never beat current alpha. Same logic but with reversed signs // applies also in the opposite condition of being mated instead of giving mate, // in this case return a fail-high score. alpha = Math.Max(Utils.mated_in(ss[ssPos].ply), alpha); beta = Math.Min(Utils.mate_in(ss[ssPos].ply + 1), beta); if (alpha >= beta) { MovesSearchedBroker.Free(); return alpha; } } // Step 4. Transposition table lookup // We don't want the score of a partial search to overwrite a previous full search // TT value, so we use a different position key in case of an excluded move. excludedMove = ss[ssPos].excludedMove; posKey = (excludedMove != 0) ? pos.exclusion_key() : pos.key(); tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tteHasValue ? tte.move() : MoveC.MOVE_NONE; ttValue = tteHasValue ? value_from_tt(tte.value(), ss[ssPos].ply) : ValueC.VALUE_NONE; // At PV nodes we check for exact scores, while at non-PV nodes we check for // a fail high/low. Biggest advantage at probing at PV nodes is to have a // smooth experience in analysis mode. We don't probe at Root nodes otherwise // we should also update RootMoveList to avoid bogus output. if (!RootNode && tteHasValue && tte.depth() >= depth && ttValue != ValueC.VALUE_NONE // Only in case of TT access race && (PvNode ? tte.type() == Bound.BOUND_EXACT : ttValue >= beta ? ((tte.type() & Bound.BOUND_LOWER) != 0 ) : ((tte.type() & Bound.BOUND_UPPER) != 0))) { Debug.Assert(ttValue != ValueC.VALUE_NONE); // Due to depth > DEPTH_NONE TT.table[ttePos].set_generation(TT.generation); ss[ssPos].currentMove = ttMove; // Can be MOVE_NONE if (ttValue >= beta && (ttMove != 0) && !pos.is_capture_or_promotion(ttMove) && ttMove != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = ttMove; } MovesSearchedBroker.Free(); return ttValue; } // Step 5. Evaluate the position statically and update parentSplitPoint's gain statistics if (inCheck) { ss[ssPos].staticEval = ss[ssPos].evalMargin = eval = ValueC.VALUE_NONE; } else if (tteHasValue) { // Never assume anything on values stored in TT if ((ss[ssPos].staticEval = eval = tte.eval_value()) == ValueC.VALUE_NONE || (ss[ssPos].evalMargin = tte.eval_margin()) == ValueC.VALUE_NONE) { eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin); } // Can ttValue be used as a better position evaluation? if (ttValue != ValueC.VALUE_NONE) { if ((((tte.type() & Bound.BOUND_LOWER) != 0) && ttValue > eval) || (((tte.type() & Bound.BOUND_UPPER) != 0) && ttValue < eval)) { eval = ttValue; } } } else { eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin); TT.store( posKey, ValueC.VALUE_NONE, Bound.BOUND_NONE, DepthC.DEPTH_NONE, MoveC.MOVE_NONE, ss[ssPos].staticEval, ss[ssPos].evalMargin); } // Update gain for the parentSplitPoint non-capture move given the static position // evaluation before and after the move. if ((move = ss[ssPos - 1].currentMove) != MoveC.MOVE_NULL && ss[ssPos - 1].staticEval != ValueC.VALUE_NONE && ss[ssPos].staticEval != ValueC.VALUE_NONE && (pos.captured_piece_type() == 0) && Utils.type_of_move(move) == MoveTypeC.NORMAL) { var to = Utils.to_sq(move); H.update_gain(pos.piece_on(to), to, -ss[ssPos - 1].staticEval - ss[ssPos].staticEval); } // Step 6. Razoring (is omitted in PV nodes) if (!PvNode && !inCheck && depth < 4 * DepthC.ONE_PLY && eval + razor_margin(depth) < beta && ttMove == MoveC.MOVE_NONE && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && !pos.pawn_on_7th(pos.sideToMove)) { var rbeta = beta - razor_margin(depth); var v = qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos, rbeta - 1, rbeta, DepthC.DEPTH_ZERO); if (v < rbeta) { // Logically we should return (v + razor_margin(depth)), but // surprisingly this did slightly weaker in tests. MovesSearchedBroker.Free(); return v; } } // Step 7. Static null move pruning (is omitted in PV nodes) // We're betting that the opponent doesn't have a move that will reduce // the score by more than futility_margin(depth) if we do a null move. if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth < 4 * DepthC.ONE_PLY && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && eval - FutilityMargins[depth][0] >= beta && (pos.non_pawn_material(pos.sideToMove) != 0)) { MovesSearchedBroker.Free(); return eval - FutilityMargins[depth][0]; } // Step 8. Null move search with verification search (is omitted in PV nodes) if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth > DepthC.ONE_PLY && eval >= beta && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && (pos.non_pawn_material(pos.sideToMove) != 0)) { ss[ssPos].currentMove = MoveC.MOVE_NULL; // Null move dynamic reduction based on depth Depth R = 3 * DepthC.ONE_PLY + depth / 4; // Null move dynamic reduction based on value if (eval - Constants.PawnValueMidgame > beta) { R += DepthC.ONE_PLY; } if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_null_move(st); ss[ssPos + 1].skipNullMove = 1; nullValue = depth - R < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, depth - R); ss[ssPos + 1].skipNullMove = 0; pos.undo_null_move(st); if (nullValue >= beta) { // Do not return unproven mate scores if (nullValue >= ValueC.VALUE_MATE_IN_MAX_PLY) { nullValue = beta; } if (depth < 6 * DepthC.ONE_PLY) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } // Do verification search at high depths ss[ssPos].skipNullMove = 1; var v = search(NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, depth - R); ss[ssPos].skipNullMove = 0; if (v >= beta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } } else { // The null move failed low, which means that we may be faced with // some kind of threat. If the previous move was reduced, check if // the move that refuted the null move was somehow connected to the // the move that refuted the null move was somehow connected to the // move which was reduced. If a connection is found extend moves that // defend against threat. threatMove = ss[ssPos + 1].currentMove; if (depth < 5 * DepthC.ONE_PLY && (ss[ssPos - 1].reduction != 0) && threatMove != MoveC.MOVE_NONE && allows(pos, ss[ssPos - 1].currentMove, threatMove)) { //threatExtension = true; if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return beta - 1; } } } // Step 9. ProbCut (is omitted in PV nodes) // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type]) // and a reduced search returns a value much above beta, we can (almost) safely // prune the previous move. if (!PvNode && !inCheck && excludedMove == MoveC.MOVE_NONE && depth >= 4 * DepthC.ONE_PLY + DepthC.ONE_PLY && (ss[ssPos].skipNullMove == 0) && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY) { var rbeta = beta + 200; var rdepth = depth - DepthC.ONE_PLY - 3 * DepthC.ONE_PLY; Debug.Assert(rdepth >= DepthC.ONE_PLY); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NONE); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NULL); var mp2 = MovePickerBroker.GetObject(); mp2.MovePickerC(pos, ttMove, H, pos.captured_piece_type()); var ci2 = CheckInfoBroker.GetObject(); ci2.CreateCheckInfo(pos); while ((move = mp2.next_move()) != MoveC.MOVE_NONE) { if (pos.pl_move_is_legal(move, ci2.pinned)) { ss[ssPos].currentMove = move; if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci2, pos.move_gives_check(move, ci2)); value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth); pos.undo_move(move); if (value >= rbeta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); MovesSearchedBroker.Free(); return value; } } } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); } // Step 10. Internal iterative deepening if (ttMove == MoveC.MOVE_NONE && depth >= (PvNode ? 5 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY) && (PvNode || (!inCheck && ss[ssPos].staticEval + 256 >= beta))) { var d = (PvNode ? depth - 2 * DepthC.ONE_PLY : depth / 2); ss[ssPos].skipNullMove = 1; search(PvNode ? NodeTypeC.PV : NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, d); ss[ssPos].skipNullMove = 0; tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = (tteHasValue) ? tte.move() : MoveC.MOVE_NONE; } else { // Re-read (needed as TTEntry is a struct in the port) if ((tteHasValue) && (TT.table[ttePos].key == tte.key)) { tte = TT.table[ttePos]; } } split_point_start: // At split points actual search starts from here var mp = MovePickerBroker.GetObject(); mp.MovePickerC( pos, ttMove, depth, H, ss[ssPos], PvNode ? -ValueC.VALUE_INFINITE : beta, SpNode ? ss[ssPos].sp.movePicker : null); var ci = CheckInfoBroker.GetObject(); ci.CreateCheckInfo(pos); value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc singularExtensionNode = !RootNode && !SpNode && depth >= (PvNode ? 6 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY) && ttMove != MoveC.MOVE_NONE && (excludedMove == 0) // Recursive singular search is not allowed && ((tte.type() & Bound.BOUND_LOWER) != 0) // FIXME: uninitialized! && tte.depth() >= depth - 3 * DepthC.ONE_PLY; // Step 11. Loop through moves // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs while ((move = mp.next_move()) != MoveC.MOVE_NONE && !thisThread.cutoff_occurred() && !SignalsStop) { Debug.Assert(Utils.is_ok_M(move)); if (move == excludedMove) { continue; } // At root obey the "searchmoves" option and skip moves not listed in Root // Move List, as a consequence any illegal move is also skipped. In MultiPV // mode we also skip PV moves which have been already searched. // If we find none, it means !count if (RootNode && (find(RootMoves, PVIdx, RootMoves.Count, move) == -1)) { continue; } if (SpNode) { // Shared counter cannot be decremented later if move turns out to be illegal if (!pos.pl_move_is_legal(move, ci.pinned)) { continue; } moveCount = ++sp.moveCount; ThreadHelper.lock_release(sp.Lock); } else { moveCount++; } if (RootNode) { SignalsFirstRootMove = (moveCount == 1); if (thisThread == Threads.main_thread() && SearchTime.ElapsedMilliseconds > 3000) { Plug.Write("info depth "); Plug.Write((depth / DepthC.ONE_PLY).ToString()); Plug.Write(" currmove "); Plug.Write(Utils.move_to_uci(move, pos.chess960)); Plug.Write(" nodes "); Plug.Write(pos.nodes.ToString()); Plug.Write(" currmovenumber "); Plug.Write((moveCount + PVIdx).ToString()); Plug.Write(Constants.endl); } } ext = DepthC.DEPTH_ZERO; captureOrPromotion = pos.is_capture_or_promotion(move); givesCheck = pos.move_gives_check(move, ci); dangerous = givesCheck || pos.is_passed_pawn_push(move) || Utils.type_of_move(move) == MoveTypeC.CASTLING || (captureOrPromotion // Entering a pawn endgame? && Utils.type_of(pos.piece_on(Utils.to_sq(move))) != PieceTypeC.PAWN && Utils.type_of_move(move) == MoveTypeC.NORMAL && (pos.non_pawn_material(ColorC.WHITE) + pos.non_pawn_material(ColorC.BLACK) - Position.PieceValue[PhaseC.MG][pos.piece_on(Utils.to_sq(move))] == ValueC.VALUE_ZERO)); // Step 12. Extend checks and, in PV nodes, also dangerous moves if (PvNode && dangerous) { ext = DepthC.ONE_PLY; } // else if (threatExtension && refutes(pos, move, threatMove)) // { // ext = DepthC.ONE_PLY; // } else if (givesCheck && pos.see(move, true) >= 0) { ext = DepthC.ONE_PLY / 2; } // Singular extension search. If all moves but one fail low on a search of // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move // is singular and should be extended. To verify this we do a reduced search // on all the other moves but the ttMove, if result is lower than ttValue minus // a margin then we extend ttreMove. if (singularExtensionNode && move == ttMove && (ext == 0) && pos.pl_move_is_legal(move, ci.pinned)) { Debug.Assert(ttValue != ValueC.VALUE_NONE); var rBeta = ttValue - depth; ss[ssPos].excludedMove = move; ss[ssPos].skipNullMove = 1; value = search(NodeTypeC.NonPV, pos, ss, ssPos, rBeta - 1, rBeta, depth / 2); ss[ssPos].skipNullMove = 0; ss[ssPos].excludedMove = MoveC.MOVE_NONE; if (value < rBeta) { ext = DepthC.ONE_PLY; } } // Update current move (this must be done after singular extension search) newDepth = depth - DepthC.ONE_PLY + ext; // Step 13. Futility pruning (is omitted in PV nodes) if (!PvNode && !captureOrPromotion && !inCheck && !dangerous && move != ttMove && (bestValue > ValueC.VALUE_MATED_IN_MAX_PLY || (bestValue == -ValueC.VALUE_INFINITE && alpha > ValueC.VALUE_MATED_IN_MAX_PLY))) { // Move count based pruning if (depth < 16 * DepthC.ONE_PLY && moveCount >= FutilityMoveCounts[depth] && ((threatMove == 0) || !refutes(pos, move, threatMove))) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } // Value based pruning // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth, // but fixing this made program slightly weaker. var predictedDepth = newDepth - reduction(PvNode, depth, moveCount); futilityValue = ss[ssPos].staticEval + ss[ssPos].evalMargin + futility_margin(predictedDepth, moveCount) + H.gain(pos.piece_moved(move), Utils.to_sq(move)); if (futilityValue < beta) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } // Prune moves with negative SEE at low depths if (predictedDepth < 2 * DepthC.ONE_PLY && pos.see(move, true) < 0) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } } // Check for legality only before to do the move if (!RootNode && !SpNode && !pos.pl_move_is_legal(move, ci.pinned)) { moveCount--; continue; } pvMove = (PvNode && moveCount == 1); ss[ssPos].currentMove = move; if (!SpNode && !captureOrPromotion && playedMoveCount < 64) { movesSearched[playedMoveCount++] = move; } // Step 14. Make the move if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci, givesCheck); // Step 15. Reduced depth search (LMR). If the move fails high will be // re-searched at full depth. if (depth > 3 * DepthC.ONE_PLY && !pvMove && !captureOrPromotion && !dangerous && move != ttMove && move != ss[ssPos].killers0 && move != ss[ssPos].killers1) { ss[ssPos].reduction = reduction(PvNode, depth, moveCount); var d = Math.Max(newDepth - ss[ssPos].reduction, DepthC.ONE_PLY); alpha = SpNode ? sp.alpha : alpha; value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, d); doFullDepthSearch = (value > alpha && ss[ssPos].reduction != DepthC.DEPTH_ZERO); ss[ssPos].reduction = DepthC.DEPTH_ZERO; } else { doFullDepthSearch = !pvMove; } // Step 16. Full depth search, when LMR is skipped or fails high if (doFullDepthSearch) { alpha = SpNode ? sp.alpha : alpha; value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, givesCheck, pos, ss, ssPos + 1, -(alpha + 1), -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth); } // Only for PV nodes do a full PV search on the first move or after a fail // high, in the latter case search only if value < beta, otherwise let the // parentSplitPoint node to fail low with value <= alpha and to try another move. if (PvNode && (pvMove || (value > alpha && (RootNode || value < beta)))) { value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.PV, givesCheck, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, newDepth); } // Step 17. Undo move pos.undo_move(move); Debug.Assert(value > -ValueC.VALUE_INFINITE && value < ValueC.VALUE_INFINITE); // Step 18. Check for new best move if (SpNode) { ThreadHelper.lock_grab(sp.Lock); bestValue = sp.bestValue; alpha = sp.alpha; } // Finished searching the move. If Signals.stop is true, the search // was aborted because the user interrupted the search or because we // ran out of time. In this case, the return value of the search cannot // be trusted, and we don't update the best move and/or PV. if (SignalsStop || thisThread.cutoff_occurred()) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return value; // To avoid returning VALUE_INFINITE } // Finished searching the move. If Signals.stop is true, the search // was aborted because the user interrupted the search or because we // ran out of time. In this case, the return value of the search cannot // be trusted, and we don't update the best move and/or PV. if (RootNode) { var rmPos = find(RootMoves, 0, RootMoves.Count, move); // PV move or new best move ? if (pvMove || value > alpha) { RootMoves[rmPos].score = value; RootMoves[rmPos].extract_pv_from_tt(pos); // We record how often the best move has been changed in each // iteration. This information is used for time management: When // the best move changes frequently, we allocate some more time. if (!pvMove) { BestMoveChanges++; } } else { // All other moves but the PV are set to the lowest value, this // is not a problem when sorting becuase sort is stable and move // position in the list is preserved, just the PV is pushed up. RootMoves[rmPos].score = -ValueC.VALUE_INFINITE; } } if (value > bestValue) { bestValue = value; if (SpNode) sp.bestValue = value; if (value > alpha) { bestMove = move; if (SpNode) sp.bestMove = move; if (PvNode && value < beta) { alpha = value; // Update alpha here! Always alpha < beta if (SpNode) sp.alpha = value; } else { Debug.Assert(value >= beta); // Fail high if (SpNode) sp.cutoff = true; break; } } } // Step 19. Check for split if (!SpNode && depth >= Threads.minimumSplitDepth && Threads.available_slave(thisThread) != null && thisThread.splitPointsSize < Constants.MAX_SPLITPOINTS_PER_THREAD) { Debug.Assert(bestValue < beta); Threads.split( Constants.FakeSplit, pos, ss, ssPos, alpha, beta, ref bestValue, ref bestMove, depth, threatMove, moveCount, mp, NT); if (bestValue >= beta) { break; } } } // Step 20. Check for mate and stalemate // All legal moves have been searched and if there are no legal moves, it // must be mate or stalemate. Note that we can have a false positive in // case of Signals.stop or thread.cutoff_occurred() are set, but this is // harmless because return value is discarded anyhow in the parentSplitPoint nodes. // If we are in a singular extension search then return a fail low score. // A split node has at least one move, the one tried before to be splitted. if (!SpNode && moveCount == 0) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return (excludedMove != 0) ? alpha : inCheck ? Utils.mated_in(ss[ssPos].ply) : DrawValue[pos.sideToMove]; } // If we have pruned all the moves without searching return a fail-low score if (bestValue == -ValueC.VALUE_INFINITE) { Debug.Assert(playedMoveCount == 0); bestValue = alpha; } if (bestValue >= beta) // Failed high { TT.store( posKey, value_to_tt(bestValue, ss[ssPos].ply), Bound.BOUND_LOWER, depth, bestMove, ss[ssPos].staticEval, ss[ssPos].evalMargin); if (!pos.is_capture_or_promotion(bestMove) && !inCheck) { if (bestMove != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = bestMove; } // Increase history value of the cut-off move var bonus = (depth * depth); H.add(pos.piece_moved(bestMove), Utils.to_sq(bestMove), bonus); // Decrease history of all the other played non-capture moves for (var i = 0; i < playedMoveCount - 1; i++) { var m = movesSearched[i]; H.add(pos.piece_moved(m), Utils.to_sq(m), -bonus); } } } else // Failed low or PV search { TT.store(posKey, value_to_tt(bestValue, ss[ssPos].ply), PvNode && bestMove != MoveC.MOVE_NONE ? Bound.BOUND_EXACT : Bound.BOUND_UPPER, depth, bestMove, ss[ssPos].staticEval, ss[ssPos].evalMargin); } Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && bestValue < ValueC.VALUE_INFINITE); if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return bestValue; }