CPG/Chess/nimfish/nimfishpkg/rays.nim

35 lines
1.2 KiB
Nim

import bitboards
import magics
# Stolen from https://github.com/Ciekce/voidstar/blob/main/src/rays.rs :D
proc computeRaysBetweenSquares: array[64, array[64, Bitboard]] =
## Computes all sliding rays between each pair of squares
## in the chessboard
for i in 0..63:
let
source = Square(i)
sourceBitboard = source.toBitboard()
rooks = getRookMoves(source, Bitboard(0))
bishops = getBishopMoves(source, Bitboard(0))
for j in 0..63:
let target = Square(j)
if target == source:
result[i][j] = Bitboard(0)
else:
let targetBitboard = target.toBitboard()
if rooks.contains(target):
result[i][j] = getRookMoves(source, targetBitboard) and getRookMoves(target, sourceBitboard)
elif bishops.contains(target):
result[i][j] = getBishopMoves(source, targetBitboard) and getBishopMoves(target, sourceBitboard)
else:
result[i][j] = Bitboard(0)
let BETWEEN_RAYS = computeRaysBetweenSquares()
proc getRayBetween*(source, target: Square): Bitboard {.inline.} = BETWEEN_RAYS[source.int][target.int]