home / fornax-v3-0 / src / board.h

board.h



//
//  board.h
//  fornax3
//
//  Created by Anders on 15/12/2020.
//

#ifndef board_h
#define board_h

#include "bits.h"
#include "eval_defs.h"

typedef enum {
  WHITE = 0,
  BLACK
} color;
#define COLOR_OPPOSITE(c) (color) ( 1 - c)

typedef enum {
  KING,
  QUEEN,
  ROOK,
  BISHOP,
  KNIGHT,
  PAWN,
  NONE
} piecetype;

template<direction dir>
constexpr static direction dir_opposite() {
  switch (dir) {
    case N: return S;
    case E: return W;
    case S: return N;
    case W: return E;
    case NE: return SW;
    case SE: return NW;
    case SW: return NE;
    case NW: return SE;
  }
}

typedef struct {
  piecetype capture;
  bits64 enpassant;
  bits64 castlingrights;
  uint64_t hash;
} Plyinfo;

typedef struct {
  piecetype piece;
  bits64 bits;
} Attackmap;

typedef struct {
  bits64 pieces[6];
  bits64 piecemask;
  
  //auxiliary data
  bits64 attackmask;
  Attackmap attackkmaps[16];
  uint8_t attackmapsize;
  uint8_t eval_phase;
  eval eval_material;
  eval eval_mg_psqt;
  eval eval_eg_psqt;
} Side;

typedef struct {
  color active;
  color active_initial;// for pondering
  Side side[2];
  
  Plyinfo plyinfo[UINT8_MAX + 1];
  uint8_t ply;
  int halfmovecount;
  
  piecetype piecemap[64]; // for finding pieces during make/sort
  
} Board;


static constexpr const Plyinfo* board_get_currinfo(const Board* board) {
  return &board->plyinfo[board->ply];
}

static constexpr Plyinfo* board_get_nextinfo(Board* board) {
  uint8_t next = board->ply + 1;
  return &board->plyinfo[next];
}

static constexpr const Plyinfo* board_get_previnfo(const Board* board) {
  uint8_t prev = board->ply - 1;
  return &board->plyinfo[prev];
}


static inline piecetype board_get_piece(const Board *board, color color, square position) {
  for (int i = 0; i < 6; ++i) {
    if (BITS_TEST(board->side[color].pieces[i], position)) return (piecetype) i;
  }
  return NONE;
}

static inline void board_put(Board *board, color color, square position, piecetype type) {
  assert(board_get_piece(board, WHITE, position) == NONE);
  assert(board_get_piece(board, BLACK, position) == NONE);
  assert(type != NONE);
  board->piecemap[position] = type;
  BITS_SET(board->side[color].pieces[type], position);
}

#endif /* board_h */