semi-fixed marlinformat

This commit is contained in:
rektdie committed 2025-04-02 05:35:12 +02:00
1 parent 114ef9728a
commit 63f5c61063
3 files changed
+45 -92

No files matched your search

+24 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "board.h"
#include "utils.h"
#include <fstream>
namespace DATAGEN {
@@ -19,15 +20,37 @@ struct MarlinFormat {
uint8_t extra; // 1 byte: Reserved for future use or additional data
void packFrom(Board& board, int16_t eval, uint8_t wdl) {
const uint8_t stm = board.sideToMove == Black ? (1 << 7) : 0;
occupancy = board.occupied;
pieces = UTILS::CompressPieces(board);
uint8_t stm_ep_square = (board.sideToMove<< 7) | (board.enPassantTarget & 0x3F);
stmEPSquare = stm | (board.enPassantTarget == noEPTarget ? 64 : board.enPassantTarget);
halfmoveClock = board.halfMoves;
fullmoveNumber = board.fullMoves;
this->eval = eval;
this->wdl = wdl;
extra = 0;
}
void writeToFile(const std::string& filename) const {
std::ofstream outFile(filename, std::ios::binary);
if (!outFile) {
std::cerr << "Error opening file for writing!" << std::endl;
return;
}
outFile.write(reinterpret_cast<const char*>(&occupancy), sizeof(occupancy));
outFile.write(reinterpret_cast<const char*>(&pieces), sizeof(pieces));
outFile.write(reinterpret_cast<const char*>(&stmEPSquare), sizeof(stmEPSquare));
outFile.write(reinterpret_cast<const char*>(&halfmoveClock), sizeof(halfmoveClock));
outFile.write(reinterpret_cast<const char*>(&fullmoveNumber), sizeof(fullmoveNumber));
outFile.write(reinterpret_cast<const char*>(&eval), sizeof(eval));
outFile.write(reinterpret_cast<const char*>(&wdl), sizeof(wdl));
outFile.write(reinterpret_cast<const char*>(&extra), sizeof(extra));
outFile.close();
}
};
struct ScoredMove {
+21 -26
View File
@@ -148,38 +148,33 @@ Move parseMove(Board &board, std::string_view str) {
}
std::array<uint8_t, 16> CompressPieces(Board &board) {
const uint8_t UNMOVED_ROOK = 0x08;
const uint8_t UNMOVED_ROOK = 6;
std::array<uint8_t, 16> compressed{};
Bitboard occupancy = board.occupied;
int index = 0;
for (int square = 0; square < 64; square++) {
int pieceCode = 0;
while (occupancy) {
int square = occupancy.getLS1BIndex();
int pieceType = board.GetPieceType(square);
int pieceCode = pieceType;
for (int pieceType = Pawn; pieceType <= King; pieceType++) {
Bitboard pieceBitboard = board.pieces[pieceType];
if (pieceBitboard.IsSet(square)) {
if (board.colors[White] & board.pieces[pieceType]) {
pieceCode = pieceType; // White: 1-6
} else {
pieceCode = pieceType + 6; // Black: 7-12
}
if (pieceType == Rook) {
if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) {
pieceCode |= UNMOVED_ROOK;
}
if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) {
pieceCode |= UNMOVED_ROOK;
}
}
break;
}
if (pieceType == Rook) {
if ((square == a1 && (board.castlingRights & whiteQueenRight))
|| (square == h1 && (board.castlingRights & whiteKingRight))
|| (square == a8 && (board.castlingRights & blackQueenRight))
|| (square == h8 && (board.castlingRights & blackKingRight)))
pieceCode = UNMOVED_ROOK;
}
int byteIndex = square / 4;
int shift = (square % 4) * 4;
pieceCode |= (board.sideToMove == Black ? (1 << 3) : 0);
compressed[byteIndex] |= (pieceCode & 0x0F) << shift;
int byteIndex = index / 2;
int bitShift = (index % 2) * 4;
compressed[byteIndex] |= (pieceCode & 0x0F) << bitShift;
index++;
occupancy.PopBit(square);
}
return compressed;