Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,49 @@ void Position::set_castling_right(Color c, Square rfrom) {

void Position::set_check_info(StateInfo* si) const {

if (var->fastCheckDetection)
{
si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), count<KING>(WHITE) ? square<KING>(WHITE) : SQ_NONE, si->pinners[BLACK], BLACK);
si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), count<KING>(BLACK) ? square<KING>(BLACK) : SQ_NONE, si->pinners[WHITE], WHITE);

std::fill(std::begin(si->checkSquares), std::end(si->checkSquares), Bitboard(0));

Square ksq = count<KING>(~sideToMove) ? square<KING>(~sideToMove) : SQ_NONE;
if (ksq != SQ_NONE)
{
Bitboard occupied = pieces();
Bitboard pawnChecks = pawn_attacks_bb(~sideToMove, ksq);
Bitboard knightChecks = attacks_bb<KNIGHT>(ksq);
Bitboard bishopChecks = attacks_bb<BISHOP>(ksq, occupied);
Bitboard rookChecks = attacks_bb<ROOK>(ksq, occupied);
Bitboard kingChecks = attacks_bb<KING>(ksq);

si->checkSquares[PAWN] = pawnChecks;
si->checkSquares[KNIGHT] = knightChecks;
si->checkSquares[BISHOP] = bishopChecks;
si->checkSquares[ROOK] = rookChecks;
si->checkSquares[QUEEN] = bishopChecks | rookChecks;
si->checkSquares[KING] = kingChecks;
si->checkSquares[ARCHBISHOP] = bishopChecks | knightChecks;
si->checkSquares[CHANCELLOR] = rookChecks | knightChecks;
si->checkSquares[COMMONER] = kingChecks;
}

si->nonSlidingRiders = 0;
for (PieceType pt : {PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, ARCHBISHOP, CHANCELLOR, COMMONER})
if (AttackRiderTypes[pt == KING ? king_type() : pt] & NON_SLIDING_RIDERS)
si->nonSlidingRiders |= pieces(pt);

si->shak = si->checkersBB & (byTypeBB[KNIGHT] | byTypeBB[ROOK] | byTypeBB[BERS]);
si->bikjang = false;
si->chased = Bitboard(0);
si->legalCapture = NO_VALUE;
si->pseudoRoyalCandidates = 0;
si->pseudoRoyals = 0;

return;
}

si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), count<KING>(WHITE) ? square<KING>(WHITE) : SQ_NONE, si->pinners[BLACK], BLACK);
si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), count<KING>(BLACK) ? square<KING>(BLACK) : SQ_NONE, si->pinners[WHITE], WHITE);

Expand Down Expand Up @@ -1448,6 +1491,64 @@ bool Position::gives_check(Move m) const {
if (!count<KING>(~sideToMove))
return false;

if (var->fastCheckDetection)
{
MoveType mt = type_of(m);

if (mt == NORMAL || mt == EN_PASSANT || mt == PROMOTION || mt == CASTLING)
{
Square ksq = square<KING>(~sideToMove);
Bitboard kingBB = square_bb(ksq);
Bitboard occupied = pieces();
occupied ^= from;

Bitboard movedSquares = square_bb(from);

if (mt == CASTLING)
{
Square kto = make_square(to > from ? castling_kingside_file() : castling_queenside_file(),
castling_rank(sideToMove));
Square rto = kto + (to > from ? WEST : EAST);
PieceType rookType = type_of(piece_on(to));

occupied ^= to;
occupied |= kto;
occupied |= rto;

if (attacks_bb(sideToMove, king_type(), kto, occupied ^ kto) & kingBB)
return true;

if (attacks_bb(sideToMove, rookType, rto, occupied ^ rto) & kingBB)
return true;

movedSquares |= square_bb(to);
}
else
{
Square capsq = mt == EN_PASSANT ? capture_square(to) : to;

if (piece_on(capsq) != NO_PIECE)
occupied ^= capsq;

occupied |= to;

if (mt == PROMOTION)
{
if (attacks_bb(sideToMove, promotion_type(m), to, occupied ^ to) & kingBB)
return true;
}
else if (check_squares(type_of(moved_piece(m))) & to)
return true;
}

if (((blockers_for_king(~sideToMove) & movedSquares) || (non_sliding_riders() & pieces(sideToMove)))
&& (attackers_to(ksq, occupied) & occupied & pieces(sideToMove)))
return true;

return false;
}
}

Bitboard occupied = (type_of(m) != DROP ? pieces() ^ from : pieces()) | to;
Bitboard janggiCannons = pieces(JANGGI_CANNON);
if (type_of(moved_piece(m)) == JANGGI_CANNON)
Expand Down
37 changes: 37 additions & 0 deletions src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,43 @@ Variant* Variant::conclude() {
&& !restrictedMobility
&& !cambodianMoves
&& !diagonalLines;
fastCheckDetection = fastAttacks
&& !twoBoards
&& !pieceDrops
&& !capturesToHand
&& !dropLoop
&& !mustDrop
&& !gating
&& !seirawanGating
&& wallingRule == NO_WALLING
&& !wallOrMove
&& !freeDrops
&& !selfCapture
&& !pass[WHITE] && !pass[BLACK]
&& !passOnStalemate[WHITE] && !passOnStalemate[BLACK]
&& !makpongRule
&& !flyingGeneral
&& !cambodianMoves
&& !diagonalLines
&& petrifyOnCaptureTypes == NO_PIECE_SET
&& !blastOnCapture
&& !pieceDemotion
&& !piecePromotionOnCapture
&& !dropPromoted
&& dropNoDoubled == NO_PIECE_TYPE
&& !dropOppositeColoredBishop
&& !promotionZonePawnDrops
&& !immobilityIllegal
&& !checkCounting
&& flipEnclosedPieces == NO_ENCLOSING
&& !flagMove
&& !bikjangRule
&& extinctionValue == VALUE_NONE
&& !extinctionClaim
&& !extinctionPseudoRoyal
&& connectN == 0
&& countingRule == NO_COUNTING
&& chasingRule == NO_CHASING;

// Initialize calculated NNUE properties
nnueKing = pieceTypes & KING ? KING
Expand Down
1 change: 1 addition & 0 deletions src/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct Variant {
// Derived properties
bool fastAttacks = true;
bool fastAttacks2 = true;
bool fastCheckDetection = false;
std::string nnueAlias = "";
PieceType nnueKing = KING;
int nnueDimensions;
Expand Down
Loading