Skip to content

Commit

Permalink
Update position.cpp
Browse files Browse the repository at this point in the history
updated musketeer gating mechanic, taking into account some specific features (take-back, specific drop mechanics for Musketeer Pieces), etc
  • Loading branch information
musketeerchess authored Dec 19, 2023
1 parent 2bde37c commit b485cb3
Showing 1 changed file with 79 additions and 11 deletions.
90 changes: 79 additions & 11 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,13 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,

Rank r = max_rank();
Square sq = SQ_A1 + r * NORTH;
int commitFile = 0;
int rank = 0;

// 1. Piece placement
while ((ss >> token) && !isspace(token))
{
if (isdigit(token))
if (isdigit(token) && (!commit_gates() || (rank != 0 && rank != max_rank() + 2)))
{
#ifdef LARGEBOARDS
if (isdigit(ss.peek()))
Expand All @@ -284,11 +286,13 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,

else if (token == '/')
{
sq = SQ_A1 + --r * NORTH;
if(!commit_gates() || (rank != 0 && rank <= max_rank())) sq += 2 * SOUTH + (FILE_MAX - max_file()) * EAST;
++rank;
commitFile = 0;
if (!is_ok(sq))
break;
}

else if(token == '*') ++commitFile;
// Stop before pieces in hand
else if (token == '[')
break;
Expand All @@ -309,8 +313,15 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
{
if (ss.peek() == '~')
ss >> token;
put_piece(Piece(idx), sq, token == '~');
++sq;

if(v->commitGates && (rank == 0 || rank == max_rank() + 2)){
commit_piece(Piece(idx), File(commitFile));
++commitFile;
}
else{
put_piece(Piece(idx), sq, token == '~');
++sq;
}
}

// Promoted shogi pieces
Expand All @@ -319,6 +330,17 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
ss >> token;
put_piece(make_piece(color_of(Piece(idx)), promoted_piece_type(type_of(Piece(idx)))), sq, true, Piece(idx));
++sq;

if(v->commitGates && (rank == 0 || rank == max_rank() + 2)){
//std::cout << "C\n";
commit_piece(Piece(idx), File(commitFile));
//std::cout << "D\n";
++commitFile;
}
else {
put_piece(make_piece(color_of(Piece(idx)), promoted_piece_type(type_of(Piece(idx)))), sq, true, Piece(idx));
++sq;
}
}
}
// Pieces in hand
Expand Down Expand Up @@ -377,7 +399,7 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
}

// Set gates (and skip castling rights)
if (gating())
if (gating() && !commit_gates())
{
st->gatesBB[c] |= rsq;
if (token == 'K' || token == 'Q')
Expand Down Expand Up @@ -603,6 +625,7 @@ void Position::set_state(StateInfo* si) const {
si->pawnKey = Zobrist::noPawns;
si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
si->checkersBB = count<KING>(sideToMove) ? attackers_to(square<KING>(sideToMove), ~sideToMove) : Bitboard(0);
si->removedGatingType = NO_PIECE_TYPE;
si->move = MOVE_NONE;

set_check_info(si);
Expand Down Expand Up @@ -681,6 +704,15 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
int emptyCnt;
std::ostringstream ss;

if(commit_gates()){
for(File f = FILE_A; f < max_file(); ++f){
if(has_committed_piece(BLACK, f)) ss << piece_to_char()[make_piece(BLACK, committedGates[BLACK][f])];
else ss << "*";
}
ss << "/";
}


for (Rank r = max_rank(); r >= RANK_1; --r)
{
for (File f = FILE_A; f <= max_file(); ++f)
Expand Down Expand Up @@ -714,6 +746,14 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
ss << '/';
}

if(commit_gates()){
ss << "/";
for(File f = FILE_A; f <= max_file(); ++f){
if(has_committed_piece(WHITE, f)) ss << piece_to_char()[make_piece(WHITE, committedGates[WHITE][f])];
else ss << "*";
}
}

// SFEN
if (sfen)
{
Expand All @@ -733,7 +773,7 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
}

// pieces in hand
if (!variant()->freeDrops && (piece_drops() || seirawan_gating()))
if (piece_drops() || (seirawan_gating() && !commit_gates()) || arrow_gating())
{
ss << '[';
if (holdings != "-")
Expand Down Expand Up @@ -779,7 +819,7 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
if (can_castle(BLACK_OOO))
ss << (chess960 ? char('a' + file_of(castling_rook_square(BLACK_OOO))) : 'q');

if (gating() && gates(BLACK) && (!seirawan_gating() || count_in_hand(BLACK, ALL_PIECES) > 0 || captures_to_hand()))
if (gating() && !commit_gates() && gates(WHITE) && (!seirawan_gating() || count_in_hand(WHITE, ALL_PIECES) > 0 || captures_to_hand()))
for (File f = FILE_A; f <= max_file(); ++f)
if ( (gates(BLACK) & file_bb(f))
// skip gating flags redundant with castling flags
Expand All @@ -788,7 +828,7 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
&& !(can_castle(BLACK_OOO) && f == file_of(castling_rook_square(BLACK_OOO))))
ss << char('a' + f);

if (!can_castle(ANY_CASTLING) && !(gating() && (gates(WHITE) | gates(BLACK))))
if (!can_castle(ANY_CASTLING) && !(gating() && !commit_gates() && (gates(WHITE) | gates(BLACK))))
ss << '-';

// Counting limit or ep-square
Expand Down Expand Up @@ -1943,7 +1983,22 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
st->materialKey ^= Zobrist::psq[gating_piece][pieceCount[gating_piece]];
st->nonPawnMaterial[us] += PieceValue[MG][gating_piece];
}

if(st->removedGatingType > NO_PIECE_TYPE)
{commit_piece(piece_on(from), file_of(from));
}


// Musketeer gating
if(commit_gates()){
Rank r = rank_of(from);
if(r == RANK_1 && has_committed_piece(WHITE, file_of(from))){
st->removedGatingType = drop_committed_piece(WHITE, file_of(from));
} else if(r == max_rank() && has_committed_piece(BLACK, file_of(from))){
st->removedGatingType = drop_committed_piece(BLACK, file_of(from));
}
else st->removedGatingType = NO_PIECE_TYPE;
}

// Remove gates
if (gating())
{
Expand Down Expand Up @@ -2166,7 +2221,11 @@ void Position::undo_move(Move m) {
add_to_hand(gating_piece);
st->gatesBB[us] |= gating_square(m);
}


if(st->removedGatingType > NO_PIECE_TYPE){
commit_piece(piece_on(from), file_of(from));
}

if (type_of(m) == PROMOTION)
{
assert((promotion_zone(us) & to) || sittuyin_promotion());
Expand Down Expand Up @@ -2260,6 +2319,15 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ
Piece castlingKingPiece = piece_on(Do ? from : to);
Piece castlingRookPiece = piece_on(Do ? rfrom : rto);



if(commit_gates()){
if(has_committed_piece(us, file_of(rfrom))){
drop_committed_piece(us, file_of(rfrom));
}
}


if (Do && Eval::useNNUE)
{
auto& dp = st->dirtyPiece;
Expand Down

0 comments on commit b485cb3

Please sign in to comment.