diff --git a/source/datagen.cpp b/source/datagen.cpp index 373dc89..b3edbcc 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -42,6 +43,79 @@ static bool IsGameOver(Board &board) { return false; } + +// Serialize MarlinFormat +std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) { + os.write(reinterpret_cast(&format), sizeof(MarlinFormat)); + return os; +} + +// Deserialize MarlinFormat +std::istream &operator>>(std::istream &is, MarlinFormat &format) { + is.read(reinterpret_cast(&format), sizeof(MarlinFormat)); + return is; +} + +// Serialize ScoredMove +std::ostream &operator<<(std::ostream &os, const ScoredMove &move) { + os.write(reinterpret_cast(&move), sizeof(ScoredMove)); + return os; +} + +// Deserialize ScoredMove +std::istream &operator>>(std::istream &is, ScoredMove &move) { + is.read(reinterpret_cast(&move), sizeof(ScoredMove)); + return is; +} + +// Serialize Game +std::ostream &operator<<(std::ostream &os, const Game &game) { + os << game.format; // Write MarlinFormat + + uint32_t moveCount = game.moves.size(); + os.write(reinterpret_cast(&moveCount), sizeof(moveCount)); // Write move count + + for (const auto &move : game.moves) { + os << move; // Write each move + } + + return os; +} + +// Deserialize Game +std::istream &operator>>(std::istream &is, Game &game) { + is >> game.format; // Read MarlinFormat + + uint32_t moveCount; + is.read(reinterpret_cast(&moveCount), sizeof(moveCount)); // Read move count + + game.moves.resize(moveCount); + for (auto &move : game.moves) { + is >> move; // Read each move + } + + return is; +} + +void WriteToFile(const Game &game, const std::string &filename) { + std::ofstream file(filename, std::ios::binary); + if (!file) throw std::runtime_error("Failed to open file for writing"); + + // Write MarlinFormat (full format) + file.write(reinterpret_cast(&game.format), sizeof(MarlinFormat)); + + // Write each ScoredMove + for (const auto &move : game.moves) { + file.write(reinterpret_cast(&move), sizeof(ScoredMove)); + } + + // Write 4 zero bytes at the end + uint32_t zero = 0; + file.write(reinterpret_cast(&zero), sizeof(zero)); + + file.close(); +} + void Run(int games) { for (int i = 0; i < games; i++) { Game game; @@ -76,6 +150,7 @@ void Run(int games) { } game.format.packFrom(board, staticEval, wdl); + WriteToFile(game, "data.binpack"); } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 41eb5fd..88eb057 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -34,6 +34,8 @@ struct ScoredMove { uint16_t move; int16_t score; + ScoredMove() : move(0), score(0) {} + ScoredMove(Move m, int s) { move = m; score = s; diff --git a/source/utils.cpp b/source/utils.cpp index a2ca6cb..599b3fc 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,7 +148,7 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks + const uint8_t UNMOVED_ROOK = 0x08; // We use bit 3 to mark unmoved rooks std::array compressed{}; // Zero-initialize for (int square = 0; square < 64; square++) { @@ -164,9 +164,12 @@ std::array CompressPieces(Board &board) { pieceCode = pieceType + 6; // Black: 7-12 } - // Check for unmoved rook + // Check for unmoved rooks (based solely on castling rights) if (pieceType == Rook) { - if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) { + if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) + } + if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) { pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) } }