+ // Find all moves featuring the given piece attacking the given square
+ // (using symmetry of all non-pawn-or-castle moves, we simply generate
+ // moves backwards). Does not support kings or pawns. Assumes there's
+ // not already a piece of our own color on the destination square.
+ function find_attacking_moves(to, piece, us, sloppy) {
+ let moves = [];
+
+ function add_move(board, moves, from, to, flags, rook_sq) {
+ moves.push(build_move(board, from, to, flags, undefined, rook_sq));
+ }
+ for (let offset of PIECE_OFFSETS[piece]) {
+ var square = to;
+
+ while (true) {
+ square += offset;
+ if (square & 0x88) break;
+
+ if (board[square] != null) {
+ if (board[square].color !== us) break;
+ if (board[to] == null) {
+ add_move(board, moves, square, to, BITS.NORMAL);
+ } else {
+ add_move(board, moves, square, to, BITS.CAPTURE);
+ }
+ break;
+ }
+
+ /* break if knight */
+ if (piece === 'n') break;
+ }
+ }
+
+ return possibly_filter_moves(moves, us, !sloppy);
+ }
+