From d500f525f7a416a4f03f97f39e02b2d50373e469 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Wed, 5 Nov 2025 23:47:00 +0100 Subject: [PATCH] update move generation for pinned pieces --- src/lookup.rs | 24 ++++++++++++++++++++++++ src/position.rs | 21 +++++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/lookup.rs b/src/lookup.rs index 64c7c13..26eeba6 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -136,6 +136,22 @@ static LINES: BySquare> = by_square!(a, BySquare([Bitboard(0) }) }); +static BISHOP_LINES: BySquare = by_square!(sq, Bitboard(0), { + let mut res = 0; + loop_bishop_directions!(d, { + res |= RAYS.get(sq).get(d).0; + }); + Bitboard(res) +}); + +static ROOK_LINES: BySquare = by_square!(sq, Bitboard(0), { + let mut res = 0; + loop_rook_directions!(d, { + res |= RAYS.get(sq).get(d).0; + }); + Bitboard(res) +}); + static SEGMENTS: BySquare> = by_square!(a, BySquare([Bitboard(0); 64]), { by_square!(b, Bitboard(0), { let mut res = 0; @@ -240,6 +256,14 @@ pub(crate) fn line(a: Square, b: Square) -> Bitboard { *LINES.get(a).get(b) } #[inline] +pub(crate) fn bishop_lines(square: Square) -> Bitboard { + *BISHOP_LINES.get(square) +} +#[inline] +pub(crate) fn rook_lines(square: Square) -> Bitboard { + *ROOK_LINES.get(square) +} +#[inline] pub(crate) fn segment(a: Square, b: Square) -> Bitboard { *SEGMENTS.get(a).get(b) } diff --git a/src/position.rs b/src/position.rs index 1b4fd3e..d86ed63 100644 --- a/src/position.rs +++ b/src/position.rs @@ -995,13 +995,10 @@ impl Position { } let blockers_x_ray = blockers & !(x | y); - let pinned_diagonally = (lookup::bishop(king_square, blockers_x_ray) & theirs.bishop()) - .map(|sq| lookup::segment(king_square, sq)) - .reduce_or(); - let pinned_horizontally = (lookup::rook(king_square, blockers_x_ray) & theirs.rook()) - .map(|sq| lookup::segment(king_square, sq)) - .reduce_or(); - let pinned = pinned_diagonally | pinned_horizontally; + let pinned = ((lookup::bishop(king_square, blockers_x_ray) & theirs.bishop()) + | (lookup::rook(king_square, blockers_x_ray) & theirs.rook())) + .map(|sq| lookup::segment(king_square, sq)) + .reduce_or(); let checker = checkers.first(); let block_check = checker @@ -1173,8 +1170,8 @@ impl Position { // pinned pieces { - let aux = |moves: &mut T, role, mask| { - for from in global_mask_from & *ours.get(role) & mask { + let aux = |moves: &mut T, role, role_mask| { + for from in global_mask_from & pinned & role_mask & *ours.get(role) { moves.extend( (global_mask_to & !us & pinned & lookup::line(king_square, from)).map( |to| RawMove { @@ -1188,13 +1185,13 @@ impl Position { } }; if moves.roles(Role::Bishop) { - aux(moves, Role::Bishop, pinned_diagonally); + aux(moves, Role::Bishop, lookup::bishop_lines(king_square)); } if moves.roles(Role::Rook) { - aux(moves, Role::Rook, pinned_horizontally); + aux(moves, Role::Rook, lookup::rook_lines(king_square)); } if moves.roles(Role::Queen) { - aux(moves, Role::Queen, pinned); + aux(moves, Role::Queen, !Bitboard::new()); } } }