Compare commits
16 Commits
v104
...
Engine/pse
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
659413ca31 | ||
|
|
4de505bc21 | ||
|
|
7c58f0d508 | ||
|
|
c9246d1342 | ||
|
|
eab795b6df | ||
|
|
f8ab14a026 | ||
|
|
274897a070 | ||
|
|
8ecbeb9c41 | ||
|
|
a1482d11f2 | ||
|
|
f9a302c9a0 | ||
|
|
ad530e9155 | ||
|
|
182aa59ee1 | ||
|
|
296f1f6c3a | ||
|
|
f7355f8e74 | ||
|
|
cd58a7a321 | ||
|
|
2beb8ab303 |
@@ -1,3 +1,7 @@
|
||||
mod attackmaps;
|
||||
mod utils;
|
||||
mod legality;
|
||||
mod checkinfo;
|
||||
mod attacks;
|
||||
|
||||
pub mod board;
|
||||
89
engine/src/bitboard/attacks.rs
Normal file
89
engine/src/bitboard/attacks.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use super::board::Board;
|
||||
use super::attackmaps::*;
|
||||
|
||||
impl Board {
|
||||
|
||||
const RANK_2: u64 = 0x0000_0000_0000_FF00;
|
||||
const RANK_7: u64 = 0x00FF_0000_0000_0000;
|
||||
|
||||
pub fn get_pseudo_pawn_moves(&self, sq: u32) -> u64 {
|
||||
let pawn: u64 = 1 << sq;
|
||||
let mut move_mask: u64 = 0u64;
|
||||
let move_offset: i8 = 8 - 16 * self.side_to_move as i8;
|
||||
|
||||
let next_sq: u64 = if move_offset > 0 {pawn << move_offset} else {pawn >> -move_offset};
|
||||
if (self.occupancy[2] & next_sq) == 0 {
|
||||
move_mask |= next_sq;
|
||||
|
||||
if (self.side_to_move == 0 && pawn & Self::RANK_2 != 0)
|
||||
|| (self.side_to_move == 1 && pawn & Self::RANK_7 != 0) {
|
||||
|
||||
let next_sq: u64 = if move_offset > 0 {next_sq << move_offset} else {next_sq >> -move_offset};
|
||||
if (self.occupancy[2] & next_sq) == 0 {
|
||||
move_mask |= next_sq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return move_mask;
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_knight_moves(&self, sq: u32) -> u64 {
|
||||
return KNIGHT_ATTACK_MAP[sq as usize];
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_king_moves(&self, sq: u32) -> u64 {
|
||||
return KING_ATTACK_MAP[sq as usize];
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_pawn_captures(&self, sq: u32) -> u64 {
|
||||
return PAWN_ATTACK_MAP[sq as usize][self.side_to_move as usize];
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_opponent_pawn_captures(&self, sq: u32) -> u64 {
|
||||
return PAWN_ATTACK_MAP[sq as usize][1 - self.side_to_move as usize];
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_bishop_moves(&self, sq: u32) -> u64 {
|
||||
let mut moves = 0u64;
|
||||
let sq = sq as usize;
|
||||
let occupancy = self.occupancy[2];
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 1);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 3);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 5);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 7);
|
||||
|
||||
return moves;
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_pseudo_rook_moves(&self, sq: u32) -> u64 {
|
||||
let mut moves: u64 = 0u64;
|
||||
let occupancy = self.occupancy[2];
|
||||
let sq = sq as usize;
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 0);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 2);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 4);
|
||||
moves |= get_raycast_from_square_in_direction(occupancy, sq, 6);
|
||||
|
||||
return moves;
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn get_pseudo_queen_moves(&self, sq: u32) -> u64 {
|
||||
return self.get_pseudo_bishop_moves(sq) | self.get_pseudo_rook_moves(sq);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_raycast_from_square_in_direction(occupancy: u64, sq: usize, dir: usize) -> u64 {
|
||||
let is_up: bool = dir / 4 == 0;
|
||||
let mut ray: u64 = RAY_TABLE[sq][dir];
|
||||
let blockers: u64 = occupancy & ray;
|
||||
|
||||
if blockers != 0 {
|
||||
let first_blocker: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||
|
||||
ray &= !RAY_TABLE[first_blocker as usize][dir];
|
||||
}
|
||||
|
||||
return ray;
|
||||
}
|
||||
@@ -115,7 +115,7 @@ impl Board {
|
||||
}
|
||||
}
|
||||
}
|
||||
// board.calc_pinned_squares();
|
||||
board.calc_pinned_squares();
|
||||
board.calc_piece_board();
|
||||
|
||||
return board;
|
||||
|
||||
21
engine/src/bitboard/checkinfo.rs
Normal file
21
engine/src/bitboard/checkinfo.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
pub struct CheckInfo {
|
||||
pub check_count: u8,
|
||||
pub move_mask: u64
|
||||
}
|
||||
|
||||
impl CheckInfo {
|
||||
|
||||
pub fn new() -> Self {
|
||||
return Self {
|
||||
check_count: 0,
|
||||
move_mask: 0xFFFF_FFFF_FFFF_FFFF
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn add_checker(&mut self, move_mask: u64) {
|
||||
self.move_mask &= move_mask;
|
||||
self.check_count += 1;
|
||||
}
|
||||
}
|
||||
48
engine/src/bitboard/legality.rs
Normal file
48
engine/src/bitboard/legality.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use super::board::Board;
|
||||
use super::attackmaps::RAY_TABLE;
|
||||
|
||||
impl Board {
|
||||
|
||||
pub(in super) fn calc_pinned_squares(&mut self) {
|
||||
self.pinned_squares = [4; 64];
|
||||
self.pin_mask = 0u64;
|
||||
|
||||
let friendly_pieces: u64 = self.occupancy[self.side_to_move as usize];
|
||||
let offset: usize = 6 * self.side_to_move as usize;
|
||||
let king_board: u64 = self.bitboards[5 + offset];
|
||||
let king_sq: u32 = king_board.trailing_zeros();
|
||||
let opponent_queen_bishop_mask: u64 = self.bitboards[8 - offset] | self.bitboards[10 - offset];
|
||||
let opponent_queen_rook_mask: u64 = self.bitboards[9 - offset] | self.bitboards[10 - offset];
|
||||
|
||||
// Queen-Rook directions
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 0);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 2);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 4);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 6);
|
||||
|
||||
// Queen-Bishop directions
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 1);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 3);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 5);
|
||||
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 7);
|
||||
}
|
||||
|
||||
pub(in super) fn set_pinned_in_ray_direction(&mut self, king_sq: u32, friendly_pieces: u64, attackers: u64, dir: u8) {
|
||||
let is_up: bool = dir / 4 == 0;
|
||||
let mask: u64 = RAY_TABLE[king_sq as usize][dir as usize];
|
||||
let blockers: u64 = self.occupancy[2] & mask;
|
||||
if blockers == 0 { return; }
|
||||
let first_blocker_sq: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||
if (friendly_pieces & 1 << first_blocker_sq) != 0 {
|
||||
let blockers: u64 = blockers & !(1 << first_blocker_sq);
|
||||
if blockers == 0 { return; }
|
||||
let second_blocker_sq: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||
|
||||
if (attackers & 1 << second_blocker_sq) != 0 {
|
||||
self.pinned_squares[first_blocker_sq as usize] = dir % 4;
|
||||
self.pin_mask |= 1 << first_blocker_sq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user