Skip to content

Commit

Permalink
Merge remote-tracking branch 'fairy/master' into tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ianfab committed Aug 23, 2023
2 parents 947a355 + 430c312 commit 5245d1c
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/stockfish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
matrix:
config:
- {
name: "Ubuntu 20.04 GCC",
os: ubuntu-20.04,
name: "Ubuntu 22.04 GCC",
os: ubuntu-22.04,
compiler: g++,
comp: gcc,
run_expensive_tests: true
Expand Down
5 changes: 5 additions & 0 deletions src/apiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ inline bool has_insufficient_material(Color c, const Position& pos) {
if ((pos.pieces(c) & unbound) && (popcount(pos.pieces() ^ restricted) >= 2 || pos.stalemate_value() != VALUE_DRAW || pos.check_counting() || pos.makpong()))
return false;

// Non-draw stalemate with lone custom king
if ( pos.stalemate_value() != VALUE_DRAW && pos.king_type() != KING
&& pos.pieces(c, KING) && (pos.board_bb(c, KING) & pos.board_bb(~c, KING)))
return false;

return true;
}

Expand Down
16 changes: 1 addition & 15 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,22 +1294,8 @@ namespace {
// Connect-n
if (pos.connect_n() > 0)
{
std::vector<Direction> connect_directions;
for (const Direction& d : pos.getConnectDirections())

if (pos.connect_horizontal())
{
connect_directions.push_back(EAST);
}
if (pos.connect_vertical())
{
connect_directions.push_back(NORTH);
}
if (pos.connect_diagonal())
{
connect_directions.push_back(NORTH_EAST);
connect_directions.push_back(SOUTH_EAST);
}
for (Direction d : connect_directions)
{
// Find sufficiently large gaps
Bitboard b = pos.board_bb() & ~pos.pieces(Them);
Expand Down
16 changes: 15 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ template <bool Current, class T> bool VariantParser<DoCheck>::parse_attribute(co
{
target = T();
char token;
size_t idx;
size_t idx = std::string::npos;
std::stringstream ss(it->second);
while (ss >> token && (idx = token == '*' ? size_t(ALL_PIECES) : pieceToChar.find(toupper(token))) != std::string::npos)
set(PieceType(idx), target);
Expand Down Expand Up @@ -357,6 +357,8 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
parse_attribute("mandatoryPiecePromotion", v->mandatoryPiecePromotion);
parse_attribute("pieceDemotion", v->pieceDemotion);
parse_attribute("blastOnCapture", v->blastOnCapture);
parse_attribute("blastImmuneTypes", v->blastImmuneTypes, v->pieceToChar);
parse_attribute("mutuallyImmuneTypes", v->mutuallyImmuneTypes, v->pieceToChar);
parse_attribute("petrifyOnCapture", v->petrifyOnCapture);
parse_attribute("doubleStep", v->doubleStep);
parse_attribute("doubleStepRegionWhite", v->doubleStepRegion[WHITE]);
Expand Down Expand Up @@ -523,6 +525,7 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
// Check for limitations
if (v->pieceDrops && (v->arrowGating || v->duckGating || v->staticGating || v->pastGating))
std::cerr << "pieceDrops and arrowGating/duckGating are incompatible." << std::endl;

// Options incompatible with royal kings
if (v->pieceTypes & KING)
{
Expand All @@ -546,6 +549,17 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
std::cerr << piece_name(v->kingType) << " is not supported as kingType." << std::endl;
}
}
// Options incompatible with royal kings OR pseudo-royal kings. Possible in theory though:
// 1. In blast variants, moving a (pseudo-)royal blastImmuneType into another piece is legal.
// 2. In blast variants, capturing a piece next to a (pseudo-)royal blastImmuneType is legal.
// 3. Moving a (pseudo-)royal mutuallyImmuneType into a square threatened by the same type is legal.
if ((v->extinctionPseudoRoyal) || (v->pieceTypes & KING))
{
if (v->blastImmuneTypes)
std::cerr << "Can not use kings or pseudo-royal with blastImmuneTypes." << std::endl;
if (v->mutuallyImmuneTypes)
std::cerr << "Can not use kings or pseudo-royal with mutuallyImmuneTypes." << std::endl;
}
}
return v;
}
Expand Down
34 changes: 17 additions & 17 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,14 @@ bool Position::legal(Move m) const {
if (var->petrifyOnCapture && capture(m) && type_of(moved_piece(m)) == KING)
return false;

// mutuallyImmuneTypes (diplomacy in Atomar)-- In no-check Atomic, kings can be beside each other, but in Atomar, this prevents them from actually taking.
// Generalized to allow a custom set of pieces that can't capture a piece of the same type.
if (capture(m) &&
(mutually_immune_types() & type_of(moved_piece(m))) &&
(type_of(moved_piece(m)) == type_of(piece_on(to)))
)
return false;

// En passant captures are a tricky special case. Because they are rather
// uncommon, we do it simply by testing whether the king is attacked after
// the move is made.
Expand Down Expand Up @@ -1926,13 +1934,19 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
if (cambodian_moves() && type_of(pc) == ROOK && (square<KING>(them) & gates(them) & attacks_bb<ROOK>(to)))
st->gatesBB[them] ^= square<KING>(them);


// Remove the blast pieces
if (captured && (blast_on_capture() || var->petrifyOnCapture))
{
std::memset(st->unpromotedBycatch, 0, sizeof(st->unpromotedBycatch));
st->demotedBycatch = st->promotedBycatch = 0;
Bitboard blast = blast_on_capture() ? (attacks_bb<KING>(to) & ((pieces(WHITE) | pieces(BLACK)) ^ pieces(PAWN))) | to
: type_of(pc) != PAWN ? square_bb(to) : Bitboard(0);
Bitboard blastImmune = 0;
for (PieceSet ps = blast_immune_types(); ps;){
PieceType pt = pop_lsb(ps);
blastImmune |= pieces(pt);
};
Bitboard blast = blast_on_capture() ? ((attacks_bb<KING>(to) & ((pieces(WHITE) | pieces(BLACK)) ^ pieces(PAWN))) | to)
& (pieces() ^ blastImmune) : type_of(pc) != PAWN ? square_bb(to) : Bitboard(0);
while (blast)
{
Square bsq = pop_lsb(blast);
Expand Down Expand Up @@ -2723,22 +2737,8 @@ bool Position::is_immediate_game_end(Value& result, int ply) const {
if (connect_n() > 0)
{
Bitboard b;
std::vector<Direction> connect_directions;

if (connect_horizontal())
{
connect_directions.push_back(EAST);
}
if (connect_vertical())
{
connect_directions.push_back(NORTH);
}
if (connect_diagonal())
{
connect_directions.push_back(NORTH_EAST);
connect_directions.push_back(SOUTH_EAST);
}
for (Direction d : connect_directions)
for (Direction d : var->connect_directions)
{
b = pieces(~sideToMove);
for (int i = 1; i < connect_n() && b; i++)
Expand Down
31 changes: 24 additions & 7 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Position {
bool mandatory_piece_promotion() const;
bool piece_demotion() const;
bool blast_on_capture() const;
PieceSet blast_immune_types() const;
PieceSet mutually_immune_types() const;
bool endgame_eval() const;
Bitboard double_step_region(Color c) const;
Bitboard triple_step_region(Color c) const;
Expand Down Expand Up @@ -209,6 +211,7 @@ class Position {
bool connect_horizontal() const;
bool connect_vertical() const;
bool connect_diagonal() const;
const std::vector<Direction>& getConnectDirections() const;

CheckCount checks_remaining(Color c) const;
MaterialCounting material_counting() const;
Expand Down Expand Up @@ -514,6 +517,16 @@ inline bool Position::blast_on_capture() const {
return var->blastOnCapture;
}

inline PieceSet Position::blast_immune_types() const {
assert(var != nullptr);
return var->blastImmuneTypes;
}

inline PieceSet Position::mutually_immune_types() const {
assert(var != nullptr);
return var->mutuallyImmuneTypes;
}

inline bool Position::endgame_eval() const {
assert(var != nullptr);
return var->endgameEval && !count_in_hand(ALL_PIECES) && count<KING>() == 2;
Expand Down Expand Up @@ -990,6 +1003,10 @@ inline bool Position::connect_diagonal() const {
return var->connectDiagonal;
}

inline const std::vector<Direction>& Position::getConnectDirections() const {
assert(var != nullptr);
return var->connect_directions;
}

inline CheckCount Position::checks_remaining(Color c) const {
return st->checksRemaining[c];
Expand Down Expand Up @@ -1416,18 +1433,18 @@ inline bool Position::allow_virtual_drop(Color c, PieceType pt) const {
}

inline Value Position::material_counting_result() const {
auto weigth_count = [this](PieceType pt, int v){ return v * (count(WHITE, pt) - count(BLACK, pt)); };
auto weight_count = [this](PieceType pt, int v){ return v * (count(WHITE, pt) - count(BLACK, pt)); };
int materialCount;
Value result;
switch (var->materialCounting)
{
case JANGGI_MATERIAL:
materialCount = weigth_count(ROOK, 13)
+ weigth_count(JANGGI_CANNON, 7)
+ weigth_count(HORSE, 5)
+ weigth_count(JANGGI_ELEPHANT, 3)
+ weigth_count(WAZIR, 3)
+ weigth_count(SOLDIER, 2)
materialCount = weight_count(ROOK, 13)
+ weight_count(JANGGI_CANNON, 7)
+ weight_count(HORSE, 5)
+ weight_count(JANGGI_ELEPHANT, 3)
+ weight_count(WAZIR, 3)
+ weight_count(SOLDIER, 2)
- 1;
result = materialCount > 0 ? VALUE_MATE : -VALUE_MATE;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ namespace {
&& (ss-1)->statScore < 23767
&& eval >= beta
&& eval >= ss->staticEval
&& ss->staticEval >= beta - 20 * depth - 22 * improving + 168 * ss->ttPv + 159 + 200 * (!pos.double_step_region(pos.side_to_move()) && pos.piece_to_char()[PAWN] != ' ')
&& ss->staticEval >= beta - 20 * depth - 22 * improving + 168 * ss->ttPv + 159 + 200 * (!pos.double_step_region(pos.side_to_move()) && (pos.piece_types() & PAWN))
&& !excludedMove
&& pos.non_pawn_material(us)
&& pos.count<ALL_PIECES>(~us) != pos.count<PAWN>(~us)
Expand Down
25 changes: 25 additions & 0 deletions src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ namespace {
return v;
}

// Atomar chess
// https://web.archive.org/web/20230519082613/https://chronatog.com/wp-content/uploads/2021/09/atomar-chess-rules.pdf
Variant* atomar_variant() {
Variant* v = nocheckatomic_variant()->init();
v->blastImmuneTypes = piece_set(COMMONER);
v->mutuallyImmuneTypes = piece_set(COMMONER);
return v;
}

#ifdef ALLVARS
// Duck chess
Variant* duck_variant() {
Expand Down Expand Up @@ -1802,6 +1811,7 @@ void VariantMap::init() {
add("isolation7x7", isolation7x7_variant());
add("snailtrail", snailtrail_variant());
add("fox-and-hounds", fox_and_hounds_variant());
add("atomar", atomar_variant());
#ifdef ALLVARS
add("duck", duck_variant());
#endif
Expand Down Expand Up @@ -2017,6 +2027,21 @@ Variant* Variant::conclude() {
break;
}

connect_directions.clear();
if (connectHorizontal)
{
connect_directions.push_back(EAST);
}
if (connectVertical)
{
connect_directions.push_back(NORTH);
}
if (connectDiagonal)
{
connect_directions.push_back(NORTH_EAST);
connect_directions.push_back(SOUTH_EAST);
}

return this;
}

Expand Down
3 changes: 3 additions & 0 deletions src/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct Variant {
bool mandatoryPiecePromotion = false;
bool pieceDemotion = false;
bool blastOnCapture = false;
PieceSet blastImmuneTypes = NO_PIECE_SET;
PieceSet mutuallyImmuneTypes = NO_PIECE_SET;
bool petrifyOnCapture = false;
bool doubleStep = true;
Bitboard doubleStepRegion[COLOR_NB] = {Rank2BB, Rank7BB};
Expand Down Expand Up @@ -169,6 +171,7 @@ struct Variant {
int nnueKingSquare;
bool endgameEval = false;
bool shogiStylePromotions = false;
std::vector<Direction> connect_directions;

void add_piece(PieceType pt, char c, std::string betza = "", char c2 = ' ') {
// Avoid ambiguous definition by removing existing piece with same letter
Expand Down
2 changes: 2 additions & 0 deletions src/variants.ini
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
# mandatoryPiecePromotion: piece promotion (and demotion if enabled) is mandatory [bool] (default: false)
# pieceDemotion: enable demotion of pieces (e.g., Kyoto shogi) [bool] (default: false)
# blastOnCapture: captures explode all adjacent non-pawn pieces (e.g., atomic chess) [bool] (default: false)
# blastImmuneTypes: pieces completely immune to explosions (even at ground zero) [PieceSet] (default: none)
# mutuallyImmuneTypes: pieces that can't capture another piece of same types (e.g., kings (commoners) in atomar) [PieceSet] (default: none)
# petrifyOnCapture: non-pawn pieces are turned into wall squares when capturing [bool] (default: false)
# doubleStep: enable pawn double step [bool] (default: true)
# doubleStepRegionWhite: region where pawn double steps are allowed for white [Bitboard] (default: *2)
Expand Down
10 changes: 10 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
firstRankPawnDrops = true
promotionZonePawnDrops = true
immobilityIllegal = true
[wazirking:chess]
fers = q
king = k:W
startFen = 7k/5Kq1/8/8/8/8/8/8 w - - 0 1
stalemateValue = loss
nFoldValue = loss
"""

sf.load_variant_config(ini_text)
Expand Down Expand Up @@ -193,6 +200,9 @@
"10/5k4/10/10/10/10/10/10/5KC3/10 w - - 0 1": (False, True), # KC vs K
"10/5k4/10/10/10/10/10/10/5K4/10 w - - 0 1": (True, True), # K vs K
},
"wazirking": {
"7k/6K1/8/8/8/8/8/8 b - - 0 1": (False, False), # K vs K
},
}

invalid_variant_positions = {
Expand Down

0 comments on commit 5245d1c

Please sign in to comment.