Skip to content

Commit cc2139d

Browse files
authored
Fix bin unpacking (#25)
1 parent 844e52c commit cc2139d

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/tools/sfen_packer.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ namespace Stockfish::Tools {
304304

305305
// First the position of the ball
306306
for (auto c : Colors)
307-
pos.board[from_variant_square(Square(stream.read_n_bit(7)), pos)] = make_piece(c, pos.nnue_king());
307+
{
308+
Square king_sq = from_variant_square(Square(stream.read_n_bit(7)), pos);
309+
if (pos.nnue_king())
310+
pos.board[king_sq] = make_piece(c, pos.nnue_king());
311+
}
308312

309313
// Piece placement
310314
for (Rank r = pos.max_rank(); r >= RANK_1; --r)
@@ -315,17 +319,17 @@ namespace Stockfish::Tools {
315319

316320
// it seems there are already balls
317321
Piece pc;
318-
if (type_of(pos.board[sq]) != pos.nnue_king())
319-
{
320-
assert(pos.board[sq] == NO_PIECE);
321-
pc = packer.read_board_piece_from_stream(pos);
322-
}
323-
else
322+
if (pos.nnue_king() && type_of(pos.board[sq]) == pos.nnue_king())
324323
{
325324
pc = pos.board[sq];
326325
// put_piece() will catch ASSERT unless you remove it all.
327326
pos.board[sq] = NO_PIECE;
328327
}
328+
else
329+
{
330+
assert(pos.board[sq] == NO_PIECE);
331+
pc = packer.read_board_piece_from_stream(pos);
332+
}
329333

330334
// There may be no pieces, so skip in that case.
331335
if (pc == NO_PIECE)
@@ -338,6 +342,16 @@ namespace Stockfish::Tools {
338342
}
339343
}
340344

345+
// Hand pieces - read the counts for each color and piece type
346+
for(auto c: Colors)
347+
for (PieceSet ps = pos.piece_types(); ps;)
348+
{
349+
PieceType pt = pop_lsb(ps);
350+
int count = stream.read_n_bit(DATA_SIZE > 512 ? 7 : 5);
351+
for (int i = 0; i < count; ++i)
352+
pos.add_to_hand(make_piece(c, pt));
353+
}
354+
341355
// Castling availability.
342356
// TODO(someone): Support chess960.
343357
pos.st->castlingRights = 0;

0 commit comments

Comments
 (0)