Fix web layer
This commit is contained in:
@@ -1,25 +1,37 @@
|
||||
/* The purpose of this file is to be a simple wasm helper layer, all state is global */
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
|
||||
static struct search_option g_tt_buf[TT_ENTRIES];
|
||||
static struct board g_board;
|
||||
static uint16_t g_legal_moves_buf[MOVE_MAX];
|
||||
static uint32_t g_legal_moves_len;
|
||||
|
||||
static struct board g_board;
|
||||
void wb_init(void)
|
||||
{
|
||||
g_board = BOARD_INIT;
|
||||
g_board.tt.entries = g_tt_buf;
|
||||
g_board.tt.mask = TT_MASK;
|
||||
board_init(&g_board);
|
||||
}
|
||||
|
||||
static inline uint32_t move_serialize(struct move m)
|
||||
static inline uint32_t move_serialize(struct move m)
|
||||
{
|
||||
_Static_assert(sizeof m.from * CHAR_BIT == 8,
|
||||
"this must be checked if struct move's `from` changes");
|
||||
_Static_assert(sizeof m.to * CHAR_BIT == 8,
|
||||
"this must be checked if struct move's `to` changes");
|
||||
return ((uint32_t)m.appeal << 24ULL)
|
||||
| ((uint32_t)m.attr << 16ULL)
|
||||
| ((uint32_t)m.from << 8ULL)
|
||||
| ((uint32_t)m.to);
|
||||
|
||||
return ((uint32_t)m.appeal << 24U)
|
||||
| ((uint32_t)m.attr << 16U)
|
||||
| ((uint32_t)m.from << 8U)
|
||||
| ((uint32_t)m.to << 0U);
|
||||
}
|
||||
|
||||
static inline struct move move_deserialize(uint64_t m)
|
||||
static inline struct move move_deserialize(uint32_t m)
|
||||
{
|
||||
return (struct move) {
|
||||
/* appeal and attributes are ignored regardless */
|
||||
@@ -34,14 +46,14 @@ static inline struct move move_deserialize(uint64_t m)
|
||||
|
||||
uint64_t wb_search(int8_t max_depth)
|
||||
{
|
||||
struct search_result const sr = search(&g_board, g_board.pos.player, max_depth);
|
||||
return move_serialize(sr.move);
|
||||
struct search_result const sr = search(&g_board, g_board.pos.moving_side, max_depth);
|
||||
return (uint64_t)move_serialize(sr.move);
|
||||
}
|
||||
|
||||
int32_t wb_move(uint32_t move)
|
||||
{
|
||||
struct move const m = move_deserialize(move);
|
||||
enum move_result const mr = board_move_2(&g_board, m);
|
||||
enum move_result const mr = board_move(&g_board, m);
|
||||
|
||||
/* TODO: this checkmate/stalemate check needs to be abstracted better */
|
||||
if (mr == MR_STALEMATE) {
|
||||
@@ -49,35 +61,56 @@ int32_t wb_move(uint32_t move)
|
||||
}
|
||||
struct move moves[MOVE_MAX];
|
||||
size_t move_count = 0ULL;
|
||||
all_moves(&g_board.pos, opposite_player(g_board.pos.player), &move_count, moves);
|
||||
all_pseudolegal_moves(&g_board.pos, MG_ALL, other_side(g_board.pos.moving_side), &move_count, moves);
|
||||
if (move_count == 0ULL) {
|
||||
return MR_CHECKMATE;
|
||||
return (int32_t)MR_CHECKMATE;
|
||||
}
|
||||
|
||||
return (int32_t)mr;
|
||||
}
|
||||
|
||||
void wb_init()
|
||||
{
|
||||
g_board = BOARD_INIT;
|
||||
g_board.tt.entries = g_tt_buf;
|
||||
}
|
||||
|
||||
static int32_t player_piece_serialize(enum player c, enum piece pz)
|
||||
static int32_t side_piece_serialize(Side8 c, Piece8 pz)
|
||||
{
|
||||
return ((c & 0xFF) << 8U)
|
||||
| (pz & 0xFF);
|
||||
}
|
||||
|
||||
int32_t wb_board_at(index at)
|
||||
int32_t wb_board_at(uint8_t at)
|
||||
{
|
||||
bitboard const m = SQ_MASK_FROM_INDEX(at);
|
||||
for (enum player pl = PLAYER_BEGIN; pl < PLAYER_COUNT; ++pl) {
|
||||
for (enum piece pz = PIECE_BEGIN; pz < PIECE_COUNT; ++pz) {
|
||||
if (g_board.pos.pieces[pl][pz] & m) {
|
||||
return player_piece_serialize(pl, pz);
|
||||
Bb64 const m = MASK_FROM_SQ((Sq8)at);
|
||||
for (Side8 side = SIDE_BEGIN; side < SIDE_COUNT; ++side) {
|
||||
for (Piece8 pz = PIECE_BEGIN; pz < PIECE_COUNT; ++pz) {
|
||||
if (g_board.pos.pieces[side][pz] & m) {
|
||||
return side_piece_serialize(side, pz);
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t wb_all_moves_len(void)
|
||||
{
|
||||
struct move moves[MOVE_MAX];
|
||||
size_t cnt = 0;
|
||||
all_pseudolegal_moves(&g_board.pos, MG_ALL, g_board.pos.moving_side, &cnt, moves);
|
||||
|
||||
if (cnt > MOVE_MAX) {
|
||||
cnt = MOVE_MAX;
|
||||
}
|
||||
|
||||
g_legal_moves_len = (uint32_t)cnt;
|
||||
for (size_t i = 0; i < cnt; ++i) {
|
||||
g_legal_moves_buf[i] = (uint16_t)(((uint32_t)moves[i].from << 8U)
|
||||
| (uint32_t)moves[i].to);
|
||||
}
|
||||
|
||||
return g_legal_moves_len;
|
||||
}
|
||||
|
||||
uint32_t wb_all_moves_get(uint32_t i)
|
||||
{
|
||||
if (i >= g_legal_moves_len) return 0U;
|
||||
return (uint32_t)g_legal_moves_buf[i];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user