Skip to content

Commit dc821f0

Browse files
committed
spell-chess: treat commoner as royal for checks/castling
1 parent d41ead9 commit dc821f0

File tree

5 files changed

+74
-58
lines changed

5 files changed

+74
-58
lines changed

src/movegen.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace {
8585
if (color_of(target) == ~us)
8686
{
8787
int bonus = int(CapturePieceValue[MG][target]);
88-
if (type_of(target) == pos.king_type())
88+
if (type_of(target) == pos.royal_piece_type())
8989
bonus += PotionKingBonus;
9090
scores[blocker] += bonus;
9191
}
@@ -216,6 +216,7 @@ namespace {
216216
constexpr Direction Up = pawn_push(Us);
217217
constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
218218
constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
219+
const PieceType royal = pos.royal_piece_type();
219220

220221
const bool allowFriendlyCaptures = pos.self_capture()
221222
&& (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS);
@@ -228,7 +229,7 @@ namespace {
228229
const Bitboard frozen = pos.freeze_squares();
229230
const Bitboard pawns = pos.pieces(Us, PAWN) & ~frozen;
230231
const Bitboard movable = pos.board_bb(Us, PAWN) & ~pos.pieces();
231-
const Bitboard friendlyCapturable = pos.pieces(Us) & ~pos.pieces(Us, KING);
232+
const Bitboard friendlyCapturable = pos.pieces(Us) & ~pos.pieces(Us, royal);
232233
const Bitboard capturable = pos.board_bb(Us, PAWN)
233234
& (allowFriendlyCaptures ? (pos.pieces(Them) | friendlyCapturable)
234235
: pos.pieces(Them));
@@ -258,12 +259,12 @@ namespace {
258259
blc &= ~standardPromotionZone;
259260
}
260261

261-
if (Type == QUIET_CHECKS && pos.count<KING>(Them))
262+
if (Type == QUIET_CHECKS && pos.count(Them, royal))
262263
{
263264
// To make a quiet check, you either make a direct check by pushing a pawn
264265
// or push a blocker pawn that is not on the same file as the enemy king.
265266
// Discovered check promotion has been already generated amongst the captures.
266-
Square ksq = pos.square<KING>(Them);
267+
Square ksq = pos.square(Them, royal);
267268
Bitboard dcCandidatePawns = pos.blockers_for_king(Them) & ~file_bb(ksq);
268269
b1 &= pawn_attacks_bb(Them, ksq) | shift< Up>(dcCandidatePawns);
269270
b2 &= pawn_attacks_bb(Them, ksq) | shift<Up+Up>(dcCandidatePawns);
@@ -473,7 +474,8 @@ namespace {
473474
static_assert(Type != LEGAL, "Unsupported type in generate_all()");
474475

475476
constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
476-
const Square ksq = pos.count<KING>(Us) ? pos.square<KING>(Us) : SQ_NONE;
477+
const PieceType royal = pos.royal_piece_type();
478+
const Square ksq = pos.count(Us, royal) ? pos.square(Us, royal) : SQ_NONE;
477479
Bitboard target;
478480
Bitboard captureTarget = Type == EVASIONS ? ~pos.pieces(Us) : Bitboard(0);
479481
Bitboard jumpForbidden = pos.spell_jump_removed();
@@ -492,7 +494,7 @@ namespace {
492494
target = ~pos.pieces(Us);
493495
// Leaper attacks can not be blocked
494496
Square checksq = lsb(pos.checkers());
495-
if (LeaperAttacks[~Us][type_of(pos.piece_on(checksq))][checksq] & pos.square<KING>(Us))
497+
if (LeaperAttacks[~Us][type_of(pos.piece_on(checksq))][checksq] & ksq)
496498
target = pos.checkers();
497499
}
498500

@@ -505,18 +507,18 @@ namespace {
505507
if (jumpForbidden)
506508
captureTarget &= ~jumpForbidden;
507509
if (pos.self_capture() && (Type == NON_EVASIONS || Type == CAPTURES))
508-
captureTarget |= pos.pieces(Us) & ~pos.pieces(Us, KING);
510+
captureTarget |= pos.pieces(Us) & ~pos.pieces(Us, royal);
509511

510512
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
511-
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | KING); ps;)
513+
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | royal); ps;)
512514
moveList = generate_moves<Us, Type>(pos, moveList, pop_lsb(ps), target, captureTarget);
513515
// generate drops
514516
if (pos.piece_drops() && Type != CAPTURES && (pos.can_drop(Us, ALL_PIECES) || pos.two_boards()))
515517
for (PieceSet ps = pos.piece_types(); ps;)
516518
moveList = generate_drops<Us, Type>(pos, moveList, pop_lsb(ps), target & ~pos.pieces(~Us));
517519

518520
// Castling with non-king piece
519-
if (!pos.count<KING>(Us) && Type != CAPTURES && pos.can_castle(Us & ANY_CASTLING))
521+
if (!pos.count(Us, royal) && Type != CAPTURES && pos.can_castle(Us & ANY_CASTLING))
520522
{
521523
Square from = pos.castling_king_square(Us);
522524
for(CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
@@ -527,9 +529,9 @@ namespace {
527529
// Special moves
528530
if (pos.cambodian_moves() && pos.gates(Us) && Type != CAPTURES)
529531
{
530-
if (Type != EVASIONS && (pos.pieces(Us, KING) & pos.gates(Us)))
532+
if (Type != EVASIONS && (pos.pieces(Us, royal) & pos.gates(Us)))
531533
{
532-
Square from = pos.square<KING>(Us);
534+
Square from = pos.square(Us, royal);
533535
Bitboard b = PseudoAttacks[WHITE][KNIGHT][from] & rank_bb(rank_of(from + (Us == WHITE ? NORTH : SOUTH)))
534536
& target & ~pos.pieces();
535537
while (b)
@@ -547,7 +549,7 @@ namespace {
547549
}
548550

549551
// Workaround for passing: Execute a non-move with any piece
550-
if (pos.pass(Us) && !pos.count<KING>(Us) && pos.pieces(Us))
552+
if (pos.pass(Us) && !pos.count(Us, royal) && pos.pieces(Us))
551553
*moveList++ = make<SPECIAL>(lsb(pos.pieces(Us)), lsb(pos.pieces(Us)));
552554

553555
//if "wall or move", generate walling action with null move
@@ -558,13 +560,13 @@ namespace {
558560
}
559561

560562
// King moves
561-
if (pos.count<KING>(Us) && (!Checks || pos.blockers_for_king(~Us) & ksq))
563+
if (pos.count(Us, royal) && (!Checks || pos.blockers_for_king(~Us) & ksq))
562564
{
563-
Bitboard kingAttacks = pos.attacks_from(Us, KING, ksq) & pos.pieces();
564-
Bitboard kingMoves = pos.moves_from (Us, KING, ksq) & ~pos.pieces();
565+
Bitboard kingAttacks = pos.attacks_from(Us, royal, ksq) & pos.pieces();
566+
Bitboard kingMoves = pos.moves_from (Us, royal, ksq) & ~pos.pieces();
565567
Bitboard kingCaptureMask = Type == EVASIONS ? ~pos.pieces(Us) : captureTarget;
566568
if (Type == EVASIONS && pos.self_capture())
567-
kingCaptureMask |= pos.pieces(Us) & ~pos.pieces(Us, KING);
569+
kingCaptureMask |= pos.pieces(Us) & ~pos.pieces(Us, royal);
568570
Bitboard kingQuietMask = Type == EVASIONS ? ~pos.pieces(Us) : target;
569571
Bitboard b = (kingAttacks & kingCaptureMask) | (kingMoves & kingQuietMask);
570572
while (b)
@@ -590,10 +592,11 @@ namespace {
590592
return baseEnd;
591593

592594
const Variant* var = pos.variant();
595+
const PieceType royal = pos.royal_piece_type();
593596
ExtMove* cur = baseEnd;
594597
const Bitboard baseFrozen = pos.freeze_squares();
595598
const Bitboard allPieces = pos.pieces();
596-
const Square ksq = pos.count<KING>(Us) ? pos.square<KING>(Us) : SQ_NONE;
599+
const Square ksq = pos.count(Us, royal) ? pos.square(Us, royal) : SQ_NONE;
597600
const bool allowNonKing = Type != EVASIONS
598601
|| !more_than_one(pos.checkers() & ~pos.non_sliding_riders());
599602
bool baseMovesSorted = false;
@@ -707,7 +710,7 @@ namespace {
707710
target = ~pos.pieces(Us);
708711
// Leaper attacks can not be blocked
709712
Square checksq = lsb(pos.checkers());
710-
if (LeaperAttacks[~Us][type_of(pos.piece_on(checksq))][checksq] & pos.square<KING>(Us))
713+
if (LeaperAttacks[~Us][type_of(pos.piece_on(checksq))][checksq] & ksq)
711714
target = pos.checkers();
712715
}
713716

@@ -718,11 +721,11 @@ namespace {
718721
Bitboard captureTarget = target;
719722
captureTarget &= ~jumpRemoved;
720723
if (pos.self_capture() && (Type == NON_EVASIONS || Type == CAPTURES))
721-
captureTarget |= pos.pieces(Us) & ~pos.pieces(Us, KING);
724+
captureTarget |= pos.pieces(Us) & ~pos.pieces(Us, royal);
722725

723726
static thread_local ExtMove extraMoves[MAX_MOVES];
724727
ExtMove* extraEnd = extraMoves;
725-
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | KING); ps;)
728+
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | royal); ps;)
726729
{
727730
PieceType sliderPt = pop_lsb(ps);
728731
if (!(MoveRiderTypes[0][sliderPt] | MoveRiderTypes[1][sliderPt] | AttackRiderTypes[sliderPt]))
@@ -777,8 +780,7 @@ namespace {
777780
Bitboard enemies = zone & pos.pieces(~Us);
778781
while (enemies)
779782
score += int(CapturePieceValue[MG][pos.piece_on(pop_lsb(enemies))]);
780-
PieceType kingType = pos.king_type();
781-
if (kingType != NO_PIECE_TYPE && (zone & pos.pieces(~Us, kingType)))
783+
if (royal != NO_PIECE_TYPE && (zone & pos.pieces(~Us, royal)))
782784
score += PotionKingBonus;
783785
}
784786
else

0 commit comments

Comments
 (0)