+/// Position::set_castle() is an helper function used to set
+/// correct castling related flags.
+
+void Position::set_castle(int f, Square ksq, Square rsq) {
+
+ st->castleRights |= f;
+ castleRightsMask[ksq] ^= f;
+ castleRightsMask[rsq] ^= f;
+ castleRookSquare[f] = rsq;
+}
+
+
+/// Position::set_castling_rights() sets castling parameters castling avaiability.
+/// This function is compatible with 3 standards: Normal FEN standard, Shredder-FEN
+/// that uses the letters of the columns on which the rooks began the game instead
+/// of KQkq and also X-FEN standard that, in case of Chess960, if an inner Rook is
+/// associated with the castling right, the traditional castling tag will be replaced
+/// by the file letter of the involved rook as for the Shredder-FEN.
+
+void Position::set_castling_rights(char token) {
+
+ Color c = islower(token) ? BLACK : WHITE;
+
+ Square sqA = relative_square(c, SQ_A1);
+ Square sqH = relative_square(c, SQ_H1);
+
+ Square rsq, ksq = king_square(c);
+
+ if (toupper(token) == 'K')
+ {
+ for (rsq = sqH; piece_on(rsq) != make_piece(c, ROOK); rsq--) {}
+ set_castle(WHITE_OO << c, ksq, rsq);
+ }
+ else if (toupper(token) == 'Q')
+ {
+ for (rsq = sqA; piece_on(rsq) != make_piece(c, ROOK); rsq++) {}
+ set_castle(WHITE_OOO << c, ksq, rsq);
+ }
+ else if (toupper(token) >= 'A' && toupper(token) <= 'H')
+ {
+ Square rsq = make_square(File(toupper(token) - 'A'), RANK_1);
+
+ if (square_file(rsq) < square_file(ksq))
+ set_castle(WHITE_OOO << c, ksq, rsq);
+ else
+ set_castle(WHITE_OO << c, ksq, rsq);
+ }
+}
+
+
+/// Position::to_fen() returns a FEN representation of the position. In case
+/// of Chess960 the Shredder-FEN notation is used. Mainly a debugging function.