Fix custom ep captures
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 3 May 2025 15:59:55 +0000 (17:59 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sun, 4 May 2025 15:47:15 +0000 (17:47 +0200)
Pick the frontmost piece to be captured.

Closes #876.

src/position.h

index 9818ede..242a4c9 100644 (file)
@@ -1395,8 +1395,18 @@ inline bool Position::capture(Move m) const {
 inline Square Position::capture_square(Square to) const {
   assert(is_ok(to));
   // The capture square of en passant is either the marked ep piece or the closest piece behind the target square
-  Bitboard b = ep_squares() & pieces() ? ep_squares() & pieces() : pieces(~sideToMove) & forward_file_bb(~sideToMove, to);
-  return sideToMove == WHITE ? msb(b) : lsb(b);
+  Bitboard customEp = ep_squares() & pieces();
+  if (customEp)
+  {
+      // For longer custom en passant paths, we take the frontmost piece
+      return sideToMove == WHITE ? lsb(customEp) : msb(customEp);
+  }
+  else
+  {
+      // The capture square of normal en passant is the closest piece behind the target square
+      Bitboard epCandidates = pieces(~sideToMove) & forward_file_bb(~sideToMove, to);
+      return sideToMove == WHITE ? msb(epCandidates) : lsb(epCandidates);
+  }
 }
 
 inline bool Position::virtual_drop(Move m) const {