]> git.sesse.net Git - stockfish/blobdiff - src/position.h
Greatly simplify castling rights handling
[stockfish] / src / position.h
index d822260dd768fb6eca1776435d80fb39cb7ad318..406dca5aa82b5da3b91014f7860a093df8c91389 100644 (file)
@@ -47,7 +47,7 @@ struct CheckInfo {
 
 /// Castle rights, encoded as bit fields
 
-enum CastleRights {
+enum CastleRight {
   CASTLES_NONE = 0,
   WHITE_OO     = 1,
   BLACK_OO     = 2,
@@ -154,11 +154,9 @@ public:
   Square king_square(Color c) const;
 
   // Castling rights
-  bool can_castle_kingside(Color c) const;
-  bool can_castle_queenside(Color c) const;
+  bool can_castle(CastleRight f) const;
   bool can_castle(Color c) const;
-  Square initial_kr_square(Color c) const;
-  Square initial_qr_square(Color c) const;
+  Square castle_rook_square(CastleRight f) const;
 
   // Bitboards for pinned pieces and discovered check candidates
   Bitboard discovered_check_candidates(Color c) const;
@@ -251,8 +249,7 @@ private:
   void clear();
   void detach();
   void put_piece(Piece p, Square s);
-  void set_castle_kingside(Color c);
-  void set_castle_queenside(Color c);
+  void set_castle(int f, Square ksq, Square rsq);
   void set_castling_rights(char token);
   bool move_is_pl_slow(const Move m) const;
 
@@ -292,8 +289,8 @@ private:
   Color sideToMove;
   Key history[MaxGameLength];
   int castleRightsMask[64];
+  Square castleRookSquare[16]; // [CastleRights]
   StateInfo startState;
-  File initialKFile, initialKRFile, initialQRFile;
   bool chess960;
   int fullMoves;
   int threadID;
@@ -381,32 +378,16 @@ inline Square Position::king_square(Color c) const {
   return pieceList[c][KING][0];
 }
 
-inline bool Position::can_castle_kingside(Color c) const {
-  return st->castleRights & (WHITE_OO << c);
-}
-
-inline bool Position::can_castle_queenside(Color c) const {
-  return st->castleRights & (WHITE_OOO << c);
+inline bool Position::can_castle(CastleRight f) const {
+  return st->castleRights & f;
 }
 
 inline bool Position::can_castle(Color c) const {
   return st->castleRights & ((WHITE_OO | WHITE_OOO) << c);
 }
 
-inline void Position::set_castle_kingside(Color c) {
-  st->castleRights |= (WHITE_OO << c);
-}
-
-inline void Position::set_castle_queenside(Color c) {
-  st->castleRights |= (WHITE_OOO << c);
-}
-
-inline Square Position::initial_kr_square(Color c) const {
-  return relative_square(c, make_square(initialKRFile, RANK_1));
-}
-
-inline Square Position::initial_qr_square(Color c) const {
-  return relative_square(c, make_square(initialQRFile, RANK_1));
+inline Square Position::castle_rook_square(CastleRight f) const {
+  return castleRookSquare[f];
 }
 
 template<>