Value Entry::shelter_storm(const Position& pos, Square ksq) { const Color Them = (Us == WHITE ? BLACK : WHITE); enum { NoFriendlyPawn, Unblocked, BlockedByPawn, BlockedByKing }; Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq)); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Value safety = MaxSafetyBonus; File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); for (File f = center - File(1); f <= center + File(1); ++f) { b = ourPawns & file_bb(f); Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1; b = theirPawns & file_bb(f); Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1; safety -= ShelterWeakness[std::min(f, FILE_H - f)][rkUs] + StormDanger [f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing : rkUs == RANK_1 ? NoFriendlyPawn : rkThem == rkUs + 1 ? BlockedByPawn : Unblocked] [std::min(f, FILE_H - f)][rkThem]; } return safety; }
Value Entry::shelter_storm(const Position& pos, Square ksq) { const Color Them = (Us == WHITE ? BLACK : WHITE); const Bitboard Edges = (FileABB | FileHBB) & (Rank2BB | Rank3BB); Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq)); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Value safety = MaxSafetyBonus; File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); for (File f = kf - File(1); f <= kf + File(1); ++f) { b = ourPawns & file_bb(f); Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1; b = theirPawns & file_bb(f); Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1; if ( (Edges & make_square(f, rkThem)) && file_of(ksq) == f && relative_rank(Us, ksq) == rkThem - 1) safety += 200; else safety -= ShelterWeakness[rkUs] + StormDanger[rkUs == RANK_1 ? 0 : rkThem != rkUs + 1 ? 1 : 2][rkThem]; } return safety; }
TEST(SquareTest, test) { ASSERT_EQ(file_of(Square::SQ_91), FILE_9); ASSERT_EQ(file_of(Square::SQ_19), FILE_1); ASSERT_EQ(rank_of(Square::SQ_91), RANK_1); ASSERT_EQ(rank_of(Square::SQ_19), RANK_9); }
inline IndexType HalfRelativeKP<AssociatedKing>::MakeIndex( Square sq_k, BonaPiece p) { constexpr IndexType W = kBoardWidth; constexpr IndexType H = kBoardHeight; const IndexType piece_index = (p - fe_hand_end) / SQ_NB; const Square sq_p = static_cast<Square>((p - fe_hand_end) % SQ_NB); const IndexType relative_file = file_of(sq_p) - file_of(sq_k) + (W / 2); const IndexType relative_rank = rank_of(sq_p) - rank_of(sq_k) + (H / 2); return H * W * piece_index + H * relative_file + relative_rank; }
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) { assert(file_of(wpsq) <= FILE_D); unsigned idx = index(us, bksq, wksq, wpsq); return KPKBitbase[idx / 32] & (1 << (idx & 0x1F)); }
// init() initializes piece square tables: the white halves of the tables are // copied from Bonus[] adding the piece value, then the black halves of the // tables are initialized by flipping and changing the sign of the white scores. void init() { for (PieceType pt = PAWN; pt <= KING; ++pt) { PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt]; PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt]; Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]); for (Square s = SQ_A1; s <= SQ_H8; ++s) { int edgeDistance = file_of(s) < FILE_E ? file_of(s) : FILE_H - file_of(s); psq[BLACK][pt][~s] = -(psq[WHITE][pt][s] = v + Bonus[pt][rank_of(s)][edgeDistance]); } } }
Value Entry::shelter_storm(const Position& pos, Square ksq) { const Color Them = (Us == WHITE ? BLACK : WHITE); Value safety = MaxSafetyBonus; Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq)); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Rank rkUs, rkThem; File kf = file_of(ksq); kf = (kf == FILE_A) ? FILE_B : (kf == FILE_H) ? FILE_G : kf; for (int f = kf - 1; f <= kf + 1; f++) { b = ourPawns & FileBB[f]; rkUs = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1; safety -= ShelterWeakness[rkUs]; b = theirPawns & FileBB[f]; rkThem = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1; safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem]; } return safety; }
Value Entry::evaluate_shelter(const Position& pos, Square ksq) { constexpr Color Them = (Us == WHITE ? BLACK : WHITE); constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH); constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB); Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Value safety = (shift<Down>(theirPawns) & (FileABB | FileHBB) & BlockRanks & ksq) ? Value(374) : Value(5); File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); for (File f = File(center - 1); f <= File(center + 1); ++f) { b = ourPawns & file_bb(f); int ourRank = b ? relative_rank(Us, backmost_sq(Us, b)) : 0; b = theirPawns & file_bb(f); int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0; int d = std::min(f, ~f); safety += ShelterStrength[d][ourRank]; safety -= (ourRank && (ourRank == theirRank - 1)) ? BlockedStorm[theirRank] : UnblockedStorm[d][theirRank]; } return safety; }
static Key polyglot_key(const Pos *pos) { Key key = 0; Bitboard b = pieces(); while (b) { Square s = pop_lsb(&b); Piece p = piece_on(s); // PolyGlot pieces are: BP = 0, WP = 1, BN = 2, ... BK = 10, WK = 11 key ^= PG.Zobrist.psq[2 * (type_of_p(p) - 1) + (color_of(p) == WHITE)][s]; } b = can_castle_any(); while (b) key ^= PG.Zobrist.castle[pop_lsb(&b)]; if (ep_square()) key ^= PG.Zobrist.enpassant[file_of(ep_square())]; if (pos_stm() == WHITE) key ^= PG.Zobrist.turn; return key; }
// init() initializes piece-square tables: the white halves of the tables are // copied from Bonus[] adding the piece value, then the black halves of the // tables are initialized by flipping and changing the sign of the white scores. void init() { for (Piece pc = W_PAWN; pc <= W_KING; ++pc) { PieceValue[MG][~pc] = PieceValue[MG][pc]; PieceValue[EG][~pc] = PieceValue[EG][pc]; Score v = make_score(PieceValue[MG][pc], PieceValue[EG][pc]); for (Square s = SQ_A1; s <= SQ_H8; ++s) { File f = std::min(file_of(s), FILE_H - file_of(s)); psq[ pc][ s] = v + Bonus[pc][rank_of(s)][f]; psq[~pc][~s] = -psq[pc][s]; } } }
// init() initializes piece-square tables: the white halves of the tables are // copied from Bonus[] adding the piece value, then the black halves of the // tables are initialized by flipping and changing the sign of the white scores. void init() { for (Piece pc = W_PAWN; pc <= W_KING; ++pc) { PieceValue[MG][~pc] = PieceValue[MG][pc]; PieceValue[EG][~pc] = PieceValue[EG][pc]; Score score = make_score(PieceValue[MG][pc], PieceValue[EG][pc]); for (Square s = SQ_A1; s <= SQ_H8; ++s) { File f = std::min(file_of(s), ~file_of(s)); psq[ pc][ s] = score + (type_of(pc) == PAWN ? PBonus[rank_of(s)][file_of(s)] : Bonus[pc][rank_of(s)][f]); psq[~pc][~s] = -psq[pc][s]; } } }
// init() initializes piece-square tables: the white halves of the tables are // copied from Bonus[] adding the piece value, then the black halves of the // tables are initialized by flipping and changing the sign of the white scores. void init() { for (PieceType pt = PAWN; pt <= KING; ++pt) { PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt]; PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt]; Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]); for (Square s = SQ_A1; s <= SQ_H8; ++s) { File f = std::min(file_of(s), FILE_H - file_of(s)); psq[WHITE][pt][ s] = v + Bonus[pt][rank_of(s)][f]; psq[BLACK][pt][~s] = -psq[WHITE][pt][s]; } } }
Value Entry::shelter_storm(const Position& pos, Square ksq) { const Color Them = (Us == WHITE ? BLACK : WHITE); Value safety = MaxSafetyBonus; Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq)); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Rank rkUs, rkThem; File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq))); for (File f = kf - File(1); f <= kf + File(1); ++f) { b = ourPawns & file_bb(f); rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1; safety -= ShelterWeakness[rkUs]; b = theirPawns & file_bb(f); rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1; safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem]; } return safety; }
oyster *builtin_get_char(machine *m){ ARG(file); FILE *a = file_of(file); char c = fgetc(a); return make_character(c); }