[position] Factor out the update_moves_with_ray! as ray_in_direction!
This macro is implemented in every sight method. Factor it out, rename it, and use it in all these macros.
This commit is contained in:
parent
722a90b860
commit
f1a05f33c9
1 changed files with 30 additions and 61 deletions
|
@ -4,6 +4,20 @@ use crate::position::piece_sets::PieceBitBoards;
|
|||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{Color, Direction, PlacedPiece, Shape, Square};
|
||||
|
||||
macro_rules! ray_in_direction {
|
||||
($square:expr, $blockers:expr, $direction:ident, $occupied_squares:tt) => {{
|
||||
let ray = BitBoard::ray($square, Direction::$direction);
|
||||
if let Some(first_occupied_square) = BitBoard::$occupied_squares(&(ray & $blockers)).next()
|
||||
{
|
||||
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
|
||||
let attack_ray = ray & !remainder;
|
||||
attack_ray
|
||||
} else {
|
||||
*ray
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) trait SightExt {
|
||||
fn sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard;
|
||||
|
||||
|
@ -92,25 +106,10 @@ impl SightExt for PlacedPiece {
|
|||
|
||||
let blockers = pieces.all_pieces();
|
||||
|
||||
macro_rules! update_moves_with_ray {
|
||||
($direction:ident, $occupied_squares:tt) => {
|
||||
let ray = BitBoard::ray(square, Direction::$direction);
|
||||
if let Some(first_occupied_square) =
|
||||
BitBoard::$occupied_squares(&(ray & blockers)).next()
|
||||
{
|
||||
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
|
||||
let attack_ray = ray & !remainder;
|
||||
sight |= attack_ray;
|
||||
} else {
|
||||
sight |= ray;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
update_moves_with_ray!(NorthEast, occupied_squares_trailing);
|
||||
update_moves_with_ray!(NorthWest, occupied_squares_trailing);
|
||||
update_moves_with_ray!(SouthEast, occupied_squares);
|
||||
update_moves_with_ray!(SouthWest, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, NorthEast, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, NorthWest, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, SouthEast, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, SouthWest, occupied_squares);
|
||||
|
||||
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
||||
sight & !friendly_pieces
|
||||
|
@ -123,25 +122,10 @@ impl SightExt for PlacedPiece {
|
|||
|
||||
let blockers = pieces.all_pieces();
|
||||
|
||||
macro_rules! update_moves_with_ray {
|
||||
($direction:ident, $occupied_squares:tt) => {
|
||||
let ray = BitBoard::ray(square, Direction::$direction);
|
||||
if let Some(first_occupied_square) =
|
||||
BitBoard::$occupied_squares(&(ray & blockers)).next()
|
||||
{
|
||||
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
|
||||
let attack_ray = ray & !remainder;
|
||||
sight |= attack_ray;
|
||||
} else {
|
||||
sight |= ray;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
update_moves_with_ray!(North, occupied_squares_trailing);
|
||||
update_moves_with_ray!(East, occupied_squares_trailing);
|
||||
update_moves_with_ray!(South, occupied_squares);
|
||||
update_moves_with_ray!(West, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, North, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, East, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, South, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, West, occupied_squares);
|
||||
|
||||
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
||||
sight & !friendly_pieces
|
||||
|
@ -154,29 +138,14 @@ impl SightExt for PlacedPiece {
|
|||
|
||||
let blockers = pieces.all_pieces();
|
||||
|
||||
macro_rules! update_moves_with_ray {
|
||||
($direction:ident, $occupied_squares:tt) => {
|
||||
let ray = BitBoard::ray(square, Direction::$direction);
|
||||
if let Some(first_occupied_square) =
|
||||
BitBoard::$occupied_squares(&(ray & blockers)).next()
|
||||
{
|
||||
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
|
||||
let attack_ray = ray & !remainder;
|
||||
sight |= attack_ray;
|
||||
} else {
|
||||
sight |= ray;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
update_moves_with_ray!(NorthWest, occupied_squares_trailing);
|
||||
update_moves_with_ray!(North, occupied_squares_trailing);
|
||||
update_moves_with_ray!(NorthEast, occupied_squares_trailing);
|
||||
update_moves_with_ray!(East, occupied_squares_trailing);
|
||||
update_moves_with_ray!(SouthEast, occupied_squares);
|
||||
update_moves_with_ray!(South, occupied_squares);
|
||||
update_moves_with_ray!(SouthWest, occupied_squares);
|
||||
update_moves_with_ray!(West, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, NorthWest, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, North, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, NorthEast, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, East, occupied_squares_trailing);
|
||||
sight |= ray_in_direction!(square, blockers, SouthEast, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, South, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, SouthWest, occupied_squares);
|
||||
sight |= ray_in_direction!(square, blockers, West, occupied_squares);
|
||||
|
||||
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
||||
sight & !friendly_pieces
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue