-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathposition.h
1576 lines (1341 loc) · 46.7 KB
/
position.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef POSITION_H_INCLUDED
#define POSITION_H_INCLUDED
#include <cassert>
#include <deque>
#include <memory> // For std::unique_ptr
#include <string>
#include <functional>
#include "bitboard.h"
#include "evaluate.h"
#include "psqt.h"
#include "types.h"
#include "variant.h"
#include "movegen.h"
#include "nnue/nnue_accumulator.h"
namespace Stockfish {
/// StateInfo struct stores information needed to restore a Position object to
/// its previous state when we retract a move. Whenever a move is made on the
/// board (by calling Position::do_move), a StateInfo object must be passed.
struct StateInfo {
// Copied when making a move
Key pawnKey;
Key materialKey;
Value nonPawnMaterial[COLOR_NB];
int castlingRights;
int rule50;
int pliesFromNull;
int countingPly;
int countingLimit;
CheckCount checksRemaining[COLOR_NB];
Bitboard epSquares;
Square castlingKingSquare[COLOR_NB];
Bitboard wallSquares;
Bitboard gatesBB[COLOR_NB];
// Not copied when making a move (will be recomputed anyhow)
Key key;
Bitboard checkersBB;
Piece unpromotedCapturedPiece;
Piece unpromotedBycatch[SQUARE_NB];
Bitboard promotedBycatch;
Bitboard demotedBycatch;
StateInfo* previous;
Bitboard blockersForKing[COLOR_NB];
Bitboard pinners[COLOR_NB];
Bitboard checkSquares[PIECE_TYPE_NB];
Piece capturedPiece;
Square captureSquare; // when != to_sq, e.g., en passant
Piece promotionPawn;
Bitboard nonSlidingRiders;
Bitboard flippedPieces;
Bitboard pseudoRoyalCandidates;
Bitboard pseudoRoyals;
OptBool legalCapture;
bool capturedpromoted;
bool shak;
bool bikjang;
Bitboard chased;
bool pass;
Move move;
int repetition;
// Used by NNUE
Eval::NNUE::Accumulator accumulator;
DirtyPiece dirtyPiece;
};
/// A list to keep track of the position states along the setup moves (from the
/// start position to the position just before the search starts). Needed by
/// 'draw by repetition' detection. Use a std::deque because pointers to
/// elements are not invalidated upon list resizing.
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
/// Position class stores information regarding the board representation as
/// pieces, side to move, hash keys, castling info, etc. Important methods are
/// do_move() and undo_move(), used by the search to update node info when
/// traversing the search tree.
class Thread;
class Position {
public:
static void init();
Position() = default;
Position(const Position&) = delete;
Position& operator=(const Position&) = delete;
// FEN string input/output
Position& set(const Variant* v, const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th, bool sfen = false);
Position& set(const std::string& code, Color c, StateInfo* si);
std::string fen(bool sfen = false, bool showPromoted = false, int countStarted = 0, std::string holdings = "-") const;
// Variant rule properties
const Variant* variant() const;
Rank max_rank() const;
File max_file() const;
int ranks() const;
int files() const;
bool two_boards() const;
Bitboard board_bb() const;
Bitboard board_bb(Color c, PieceType pt) const;
PieceSet piece_types() const;
const std::string& piece_to_char() const;
const std::string& piece_to_char_synonyms() const;
Bitboard promotion_zone(Color c) const;
Square promotion_square(Color c, Square s) const;
PieceType promotion_pawn_type(Color c) const;
PieceSet promotion_piece_types(Color c) const;
bool sittuyin_promotion() const;
int promotion_limit(PieceType pt) const;
PieceType promoted_piece_type(PieceType pt) const;
bool piece_promotion_on_capture() const;
bool mandatory_pawn_promotion() const;
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;
bool castling_enabled() const;
bool castling_dropped_piece() const;
File castling_kingside_file() const;
File castling_queenside_file() const;
Rank castling_rank(Color c) const;
File castling_king_file() const;
PieceType castling_king_piece(Color c) const;
PieceSet castling_rook_pieces(Color c) const;
PieceType king_type() const;
PieceType nnue_king() const;
Square nnue_king_square(Color c) const;
bool nnue_use_pockets() const;
bool nnue_applicable() const;
bool checking_permitted() const;
bool drop_checks() const;
bool must_capture() const;
bool has_capture() const;
bool must_drop() const;
bool piece_drops() const;
bool drop_loop() const;
bool captures_to_hand() const;
bool first_rank_pawn_drops() const;
bool can_drop(Color c, PieceType pt) const;
EnclosingRule enclosing_drop() const;
Bitboard drop_region(Color c) const;
Bitboard drop_region(Color c, PieceType pt) const;
bool sittuyin_rook_drop() const;
bool drop_opposite_colored_bishop() const;
bool drop_promoted() const;
PieceType drop_no_doubled() const;
bool immobility_illegal() const;
bool gating() const;
bool walling() const;
WallingRule walling_rule() const;
bool seirawan_gating() const;
bool cambodian_moves() const;
Bitboard diagonal_lines() const;
bool pass(Color c) const;
bool pass_on_stalemate(Color c) const;
Bitboard promoted_soldiers(Color c) const;
bool makpong() const;
EnclosingRule flip_enclosed_pieces() const;
// winning conditions
int n_move_rule() const;
int n_fold_rule() const;
Value stalemate_value(int ply = 0) const;
Value checkmate_value(int ply = 0) const;
Value extinction_value(int ply = 0) const;
bool extinction_claim() const;
PieceSet extinction_piece_types() const;
bool extinction_single_piece() const;
int extinction_piece_count() const;
int extinction_opponent_piece_count() const;
bool extinction_pseudo_royal() const;
PieceType flag_piece(Color c) const;
Bitboard flag_region(Color c) const;
bool flag_move() const;
bool flag_reached(Color c) const;
bool check_counting() const;
int connect_n() const;
PieceSet connect_piece_types() const;
bool connect_horizontal() const;
bool connect_vertical() const;
bool connect_diagonal() const;
const std::vector<Direction>& getConnectDirections() const;
int connect_nxn() const;
int collinear_n() const;
CheckCount checks_remaining(Color c) const;
MaterialCounting material_counting() const;
CountingRule counting_rule() const;
// Variant-specific properties
int count_in_hand(PieceType pt) const;
int count_in_hand(Color c, PieceType pt) const;
int count_with_hand(Color c, PieceType pt) const;
bool bikjang() const;
bool allow_virtual_drop(Color c, PieceType pt) const;
// Position representation
Bitboard pieces(PieceType pt = ALL_PIECES) const;
Bitboard pieces(PieceType pt1, PieceType pt2) const;
Bitboard pieces(Color c) const;
Bitboard pieces(Color c, PieceType pt) const;
Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
Bitboard pieces(Color c, PieceType pt1, PieceType pt2, PieceType pt3) const;
Bitboard major_pieces(Color c) const;
Bitboard non_sliding_riders() const;
Piece piece_on(Square s) const;
Piece unpromoted_piece_on(Square s) const;
Bitboard ep_squares() const;
Square castling_king_square(Color c) const;
Bitboard gates(Color c) const;
bool empty(Square s) const;
int count(Color c, PieceType pt) const;
template<PieceType Pt> int count(Color c) const;
template<PieceType Pt> int count() const;
template<PieceType Pt> Square square(Color c) const;
Square square(Color c, PieceType pt) const;
bool is_on_semiopen_file(Color c, Square s) const;
// Castling
CastlingRights castling_rights(Color c) const;
bool can_castle(CastlingRights cr) const;
bool castling_impeded(CastlingRights cr) const;
Square castling_rook_square(CastlingRights cr) const;
// Checking
Bitboard checkers() const;
Bitboard blockers_for_king(Color c) const;
Bitboard check_squares(PieceType pt) const;
Bitboard pinners(Color c) const;
Bitboard checked_pseudo_royals(Color c) const;
// Attacks to/from a given square
Bitboard attackers_to(Square s) const;
Bitboard attackers_to(Square s, Color c) const;
Bitboard attackers_to(Square s, Bitboard occupied) const;
Bitboard attackers_to(Square s, Bitboard occupied, Color c) const;
Bitboard attackers_to(Square s, Bitboard occupied, Color c, Bitboard janggiCannons) const;
Bitboard attacks_from(Color c, PieceType pt, Square s) const;
Bitboard moves_from(Color c, PieceType pt, Square s) const;
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners, Color c) const;
// Properties of moves
bool legal(Move m) const;
bool pseudo_legal(const Move m) const;
bool virtual_drop(Move m) const;
bool capture(Move m) const;
bool capture_or_promotion(Move m) const;
Square capture_square(Square to) const;
bool gives_check(Move m) const;
Piece moved_piece(Move m) const;
Piece captured_piece() const;
const std::string piece_to_partner() const;
// Piece specific
bool pawn_passed(Color c, Square s) const;
bool opposite_bishops() const;
bool is_promoted(Square s) const;
int pawns_on_same_color_squares(Color c, Square s) const;
// Doing and undoing moves
void do_move(Move m, StateInfo& newSt);
void do_move(Move m, StateInfo& newSt, bool givesCheck);
void undo_move(Move m);
void do_null_move(StateInfo& newSt);
void undo_null_move();
// Static Exchange Evaluation
Value blast_see(Move m) const;
bool see_ge(Move m, Value threshold = VALUE_ZERO) const;
// Accessing hash keys
Key key() const;
Key key_after(Move m) const;
Key material_key() const;
Key pawn_key() const;
// Other properties of the position
Color side_to_move() const;
int game_ply() const;
bool is_chess960() const;
Thread* this_thread() const;
bool is_immediate_game_end() const;
bool is_immediate_game_end(Value& result, int ply = 0) const;
bool is_optional_game_end() const;
bool is_optional_game_end(Value& result, int ply = 0, int countStarted = 0) const;
bool is_game_end(Value& result, int ply = 0) const;
Value material_counting_result() const;
bool is_draw(int ply) const;
bool has_game_cycle(int ply) const;
bool has_repeated() const;
Bitboard chased() const;
int count_limit(Color sideToCount) const;
int board_honor_counting_ply(int countStarted) const;
bool board_honor_counting_shorter(int countStarted) const;
int counting_limit(int countStarted) const;
int counting_ply(int countStarted) const;
int rule50_count() const;
Score psq_score() const;
Value non_pawn_material(Color c) const;
Value non_pawn_material() const;
// Position consistency check, for debugging
bool pos_is_ok() const;
void flip();
// Used by NNUE
StateInfo* state() const;
void put_piece(Piece pc, Square s, bool isPromoted = false, Piece unpromotedPc = NO_PIECE);
void remove_piece(Square s);
private:
// Initialization helpers (used while setting up a position)
void set_castling_right(Color c, Square rfrom);
void set_state(StateInfo* si) const;
void set_check_info(StateInfo* si) const;
// Other helpers
void move_piece(Square from, Square to);
template<bool Do>
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
// Data members
Piece board[SQUARE_NB];
Piece unpromotedBoard[SQUARE_NB];
Bitboard byTypeBB[PIECE_TYPE_NB];
Bitboard byColorBB[COLOR_NB];
int pieceCount[PIECE_NB];
int castlingRightsMask[SQUARE_NB];
Square castlingRookSquare[CASTLING_RIGHT_NB];
Bitboard castlingPath[CASTLING_RIGHT_NB];
Thread* thisThread;
StateInfo* st;
int gamePly;
Color sideToMove;
Score psq;
// variant-specific
const Variant* var;
bool tsumeMode;
bool chess960;
int pieceCountInHand[COLOR_NB][PIECE_TYPE_NB];
int virtualPieces;
Bitboard promotedPieces;
void add_to_hand(Piece pc);
void remove_from_hand(Piece pc);
void drop_piece(Piece pc_hand, Piece pc_drop, Square s);
void undrop_piece(Piece pc_hand, Square s);
Bitboard find_drop_region(Direction dir, Square s, Bitboard occupied) const;
};
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
inline const Variant* Position::variant() const {
assert(var != nullptr);
return var;
}
inline Rank Position::max_rank() const {
assert(var != nullptr);
return var->maxRank;
}
inline File Position::max_file() const {
assert(var != nullptr);
return var->maxFile;
}
inline int Position::ranks() const {
assert(var != nullptr);
return var->maxRank + 1;
}
inline int Position::files() const {
assert(var != nullptr);
return var->maxFile + 1;
}
inline bool Position::two_boards() const {
assert(var != nullptr);
return var->twoBoards;
}
inline Bitboard Position::board_bb() const {
assert(var != nullptr);
return board_size_bb(var->maxFile, var->maxRank) & ~st->wallSquares;
}
inline Bitboard Position::board_bb(Color c, PieceType pt) const {
assert(var != nullptr);
return var->mobilityRegion[c][pt] ? var->mobilityRegion[c][pt] & board_bb() : board_bb();
}
inline PieceSet Position::piece_types() const {
assert(var != nullptr);
return var->pieceTypes;
}
inline const std::string& Position::piece_to_char() const {
assert(var != nullptr);
return var->pieceToChar;
}
inline const std::string& Position::piece_to_char_synonyms() const {
assert(var != nullptr);
return var->pieceToCharSynonyms;
}
inline Bitboard Position::promotion_zone(Color c) const {
assert(var != nullptr);
return var->promotionRegion[c];
}
inline Square Position::promotion_square(Color c, Square s) const {
assert(var != nullptr);
Bitboard b = promotion_zone(c) & forward_file_bb(c, s) & board_bb();
return !b ? SQ_NONE : c == WHITE ? lsb(b) : msb(b);
}
inline PieceType Position::promotion_pawn_type(Color c) const {
assert(var != nullptr);
return var->promotionPawnType[c];
}
inline PieceSet Position::promotion_piece_types(Color c) const {
assert(var != nullptr);
return var->promotionPieceTypes[c];
}
inline bool Position::sittuyin_promotion() const {
assert(var != nullptr);
return var->sittuyinPromotion;
}
inline int Position::promotion_limit(PieceType pt) const {
assert(var != nullptr);
return var->promotionLimit[pt];
}
inline PieceType Position::promoted_piece_type(PieceType pt) const {
assert(var != nullptr);
return var->promotedPieceType[pt];
}
inline bool Position::piece_promotion_on_capture() const {
assert(var != nullptr);
return var->piecePromotionOnCapture;
}
inline bool Position::mandatory_pawn_promotion() const {
assert(var != nullptr);
return var->mandatoryPawnPromotion;
}
inline bool Position::mandatory_piece_promotion() const {
assert(var != nullptr);
return var->mandatoryPiecePromotion;
}
inline bool Position::piece_demotion() const {
assert(var != nullptr);
return var->pieceDemotion;
}
inline bool Position::blast_on_capture() const {
assert(var != nullptr);
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;
}
inline Bitboard Position::double_step_region(Color c) const {
assert(var != nullptr);
return var->doubleStepRegion[c];
}
inline Bitboard Position::triple_step_region(Color c) const {
assert(var != nullptr);
return var->tripleStepRegion[c];
}
inline bool Position::castling_enabled() const {
assert(var != nullptr);
return var->castling;
}
inline bool Position::castling_dropped_piece() const {
assert(var != nullptr);
return var->castlingDroppedPiece;
}
inline File Position::castling_kingside_file() const {
assert(var != nullptr);
return var->castlingKingsideFile;
}
inline File Position::castling_queenside_file() const {
assert(var != nullptr);
return var->castlingQueensideFile;
}
inline Rank Position::castling_rank(Color c) const {
assert(var != nullptr);
return relative_rank(c, var->castlingRank, max_rank());
}
inline File Position::castling_king_file() const {
assert(var != nullptr);
return var->castlingKingFile;
}
inline PieceType Position::castling_king_piece(Color c) const {
assert(var != nullptr);
return var->castlingKingPiece[c];
}
inline PieceSet Position::castling_rook_pieces(Color c) const {
assert(var != nullptr);
return var->castlingRookPieces[c];
}
inline PieceType Position::king_type() const {
assert(var != nullptr);
return var->kingType;
}
inline PieceType Position::nnue_king() const {
assert(var != nullptr);
return var->nnueKing;
}
inline Square Position::nnue_king_square(Color c) const {
return nnue_king() ? square(c, nnue_king()) : SQ_NONE;
}
inline bool Position::nnue_use_pockets() const {
assert(var != nullptr);
return var->nnueUsePockets;
}
inline bool Position::nnue_applicable() const {
// Do not use NNUE during setup phases (placement, sittuyin)
return (!count_in_hand(ALL_PIECES) || nnue_use_pockets() || !must_drop()) && !virtualPieces;
}
inline bool Position::checking_permitted() const {
assert(var != nullptr);
return var->checking;
}
inline bool Position::drop_checks() const {
assert(var != nullptr);
return var->dropChecks;
}
inline bool Position::must_capture() const {
assert(var != nullptr);
return var->mustCapture;
}
inline bool Position::has_capture() const {
// Check for cached value
if (st->legalCapture != NO_VALUE)
return st->legalCapture == VALUE_TRUE;
if (checkers())
{
for (const auto& mevasion : MoveList<EVASIONS>(*this))
if (capture(mevasion) && legal(mevasion))
{
st->legalCapture = VALUE_TRUE;
return true;
}
}
else
{
for (const auto& mcap : MoveList<CAPTURES>(*this))
if (capture(mcap) && legal(mcap))
{
st->legalCapture = VALUE_TRUE;
return true;
}
}
st->legalCapture = VALUE_FALSE;
return false;
}
inline bool Position::must_drop() const {
assert(var != nullptr);
return var->mustDrop;
}
inline bool Position::piece_drops() const {
assert(var != nullptr);
return var->pieceDrops;
}
inline bool Position::drop_loop() const {
assert(var != nullptr);
return var->dropLoop;
}
inline bool Position::captures_to_hand() const {
assert(var != nullptr);
return var->capturesToHand;
}
inline bool Position::first_rank_pawn_drops() const {
assert(var != nullptr);
return var->firstRankPawnDrops;
}
inline EnclosingRule Position::enclosing_drop() const {
assert(var != nullptr);
return var->enclosingDrop;
}
inline Bitboard Position::drop_region(Color c) const {
assert(var != nullptr);
return c == WHITE ? var->whiteDropRegion : var->blackDropRegion;
}
inline Bitboard Position::drop_region(Color c, PieceType pt) const {
Bitboard b = drop_region(c) & board_bb(c, pt);
// Pawns on back ranks
if (pt == PAWN)
{
if (!var->promotionZonePawnDrops)
b &= ~promotion_zone(c);
if (!first_rank_pawn_drops())
b &= ~rank_bb(relative_rank(c, RANK_1, max_rank()));
}
// Doubled shogi pawns
if (pt == drop_no_doubled())
for (File f = FILE_A; f <= max_file(); ++f)
if (popcount(file_bb(f) & pieces(c, pt)) >= var->dropNoDoubledCount)
b &= ~file_bb(f);
// Sittuyin rook drops
if (pt == ROOK && sittuyin_rook_drop())
b &= rank_bb(relative_rank(c, RANK_1, max_rank()));
if (enclosing_drop())
{
// Reversi start
if (var->enclosingDropStart & ~pieces())
b &= var->enclosingDropStart;
else
{
// Filter out squares where the drop does not enclose at least one opponent's piece
if (enclosing_drop() == REVERSI)
{
Bitboard theirs = pieces(~c);
b &= shift<NORTH >(theirs) | shift<SOUTH >(theirs)
| shift<NORTH_EAST>(theirs) | shift<SOUTH_WEST>(theirs)
| shift<EAST >(theirs) | shift<WEST >(theirs)
| shift<SOUTH_EAST>(theirs) | shift<NORTH_WEST>(theirs);
Bitboard b2 = b;
while (b2)
{
Square s = pop_lsb(b2);
if (!(attacks_bb(c, QUEEN, s, board_bb() & ~pieces(~c)) & ~PseudoAttacks[c][KING][s] & pieces(c)))
b ^= s;
}
}
else if (enclosing_drop() == SNORT)
{
Bitboard theirs = pieces(~c);
b &= ~(shift<NORTH >(theirs) | shift<SOUTH >(theirs)
| shift<EAST >(theirs) | shift<WEST >(theirs));
}
else if (enclosing_drop() == ANYSIDE)
{
Bitboard occupied = pieces();
b = 0ULL;
Bitboard candidates = (shift<WEST>(occupied) | file_bb(max_file())) & ~occupied;
for (Rank r = RANK_1; r <= max_rank(); ++r) {
if (!(occupied & make_square(FILE_A, r))) {
b |= lsb(candidates & rank_bb(r));
}
}
candidates = (shift<SOUTH>(occupied) | rank_bb(max_rank())) & ~occupied;
for (File f = FILE_A; f <= max_file(); ++f) {
if (!(occupied & make_square(f, RANK_1))) {
b |= lsb(candidates & file_bb(f));
}
}
candidates = (shift<NORTH>(occupied) | rank_bb(RANK_1)) & ~occupied;
for (File f = FILE_A; f <= max_file(); ++f) {
if (!(occupied & make_square(f, max_rank()))) {
b |= lsb(candidates & file_bb(f));
}
}
candidates = (shift<EAST>(occupied) | file_bb(FILE_A)) & ~occupied;
for (Rank r = RANK_1; r <= max_rank(); ++r) {
if (!(occupied & make_square(max_file(), r))) {
b |= lsb(candidates & rank_bb(r));
}
}
}
else if (enclosing_drop() == TOP)
{
b &= shift<NORTH>(pieces()) | Rank1BB;
}
else
{
assert(enclosing_drop() == ATAXX);
Bitboard ours = pieces(c);
b &= shift<NORTH >(ours) | shift<SOUTH >(ours)
| shift<NORTH_EAST>(ours) | shift<SOUTH_WEST>(ours)
| shift<EAST >(ours) | shift<WEST >(ours)
| shift<SOUTH_EAST>(ours) | shift<NORTH_WEST>(ours);
}
}
}
return b;
}
inline bool Position::sittuyin_rook_drop() const {
assert(var != nullptr);
return var->sittuyinRookDrop;
}
inline bool Position::drop_opposite_colored_bishop() const {
assert(var != nullptr);
return var->dropOppositeColoredBishop;
}
inline bool Position::drop_promoted() const {
assert(var != nullptr);
return var->dropPromoted;
}
inline PieceType Position::drop_no_doubled() const {
assert(var != nullptr);
return var->dropNoDoubled;
}
inline bool Position::immobility_illegal() const {
assert(var != nullptr);
return var->immobilityIllegal;
}
inline bool Position::gating() const {
assert(var != nullptr);
return var->gating;
}
inline bool Position::walling() const {
assert(var != nullptr);
return var->wallingRule != NO_WALLING;
}
inline WallingRule Position::walling_rule() const {
assert(var != nullptr);
return var->wallingRule;
}
inline bool Position::seirawan_gating() const {
assert(var != nullptr);
return var->seirawanGating;
}
inline bool Position::cambodian_moves() const {
assert(var != nullptr);
return var->cambodianMoves;
}
inline Bitboard Position::diagonal_lines() const {
assert(var != nullptr);
return var->diagonalLines;
}
inline bool Position::pass(Color c) const {
assert(var != nullptr);
return var->pass[c] || var->passOnStalemate[c];
}
inline bool Position::pass_on_stalemate(Color c) const {
assert(var != nullptr);
return var->passOnStalemate[c];
}
inline Bitboard Position::promoted_soldiers(Color c) const {
assert(var != nullptr);
return pieces(c, SOLDIER) & zone_bb(c, var->soldierPromotionRank, max_rank());
}
inline bool Position::makpong() const {
assert(var != nullptr);
return var->makpongRule;
}
inline int Position::n_move_rule() const {
assert(var != nullptr);
return var->nMoveRule;
}
inline int Position::n_fold_rule() const {
assert(var != nullptr);
return var->nFoldRule;
}
inline EnclosingRule Position::flip_enclosed_pieces() const {
assert(var != nullptr);
return var->flipEnclosedPieces;
}
inline Value Position::stalemate_value(int ply) const {
assert(var != nullptr);
if (var->stalematePieceCount)
{
int c = count<ALL_PIECES>(sideToMove) - count<ALL_PIECES>(~sideToMove);
return c == 0 ? VALUE_DRAW : convert_mate_value(c < 0 ? var->stalemateValue : -var->stalemateValue, ply);
}
// Check for checkmate of pseudo-royal pieces
if (var->extinctionPseudoRoyal)
{
Bitboard pseudoRoyals = st->pseudoRoyals & pieces(sideToMove);
Bitboard pseudoRoyalsTheirs = st->pseudoRoyals & pieces(~sideToMove);
while (pseudoRoyals)
{
Square sr = pop_lsb(pseudoRoyals);
if ( !(blast_on_capture() && (pseudoRoyalsTheirs & attacks_bb<KING>(sr)))
&& attackers_to(sr, ~sideToMove))
return convert_mate_value(var->checkmateValue, ply);
}
// Look for duple check
if (var->dupleCheck)
{
Bitboard pseudoRoyalCandidates = st->pseudoRoyalCandidates & pieces(sideToMove);
bool allCheck = bool(pseudoRoyalCandidates);
while (allCheck && pseudoRoyalCandidates)
{
Square sr = pop_lsb(pseudoRoyalCandidates);
// Touching pseudo-royal pieces are immune
if (!( !(blast_on_capture() && (pseudoRoyalsTheirs & attacks_bb<KING>(sr)))
&& attackers_to(sr, ~sideToMove)))
allCheck = false;
}
if (allCheck)
return convert_mate_value(var->checkmateValue, ply);
}
}
return convert_mate_value(var->stalemateValue, ply);
}
inline Value Position::checkmate_value(int ply) const {
assert(var != nullptr);
// Check for illegal mate by shogi pawn drop
if ( var->shogiPawnDropMateIllegal
&& !(checkers() & ~pieces(SHOGI_PAWN))
&& !st->capturedPiece
&& st->pliesFromNull > 0
&& (st->materialKey != st->previous->materialKey))
{
return mate_in(ply);
}
// Check for shatar mate rule
if (var->shatarMateRule)
{
// Mate by knight is illegal
if (!(checkers() & ~pieces(KNIGHT)))
return mate_in(ply);
StateInfo* stp = st;
while (stp->checkersBB)
{
// Return mate score if there is at least one shak in series of checks
if (stp->shak)
return convert_mate_value(var->checkmateValue, ply);
if (stp->pliesFromNull < 2)
break;
stp = stp->previous->previous;
}
// Niol
return VALUE_DRAW;
}
// Checkmate using virtual pieces
if (two_boards() && var->checkmateValue < VALUE_ZERO)
{
Value virtualMaterial = VALUE_ZERO;
for (PieceSet ps = piece_types(); ps;)
{
PieceType pt = pop_lsb(ps);
virtualMaterial += std::max(-count_in_hand(~sideToMove, pt), 0) * PieceValue[MG][pt];
}
if (virtualMaterial > 0)
return -VALUE_VIRTUAL_MATE + virtualMaterial / 20 + ply;
}
// Return mate value
return convert_mate_value(var->checkmateValue, ply);
}
inline Value Position::extinction_value(int ply) const {
assert(var != nullptr);
return convert_mate_value(var->extinctionValue, ply);
}
inline bool Position::extinction_claim() const {
assert(var != nullptr);
return var->extinctionClaim;
}
inline PieceSet Position::extinction_piece_types() const {
assert(var != nullptr);
return var->extinctionPieceTypes;
}
inline bool Position::extinction_single_piece() const {
assert(var != nullptr);
return var->extinctionValue == -VALUE_MATE
&& (var->extinctionPieceTypes & ~piece_set(ALL_PIECES));
}
inline int Position::extinction_piece_count() const {
assert(var != nullptr);
return var->extinctionPieceCount;
}
inline int Position::extinction_opponent_piece_count() const {
assert(var != nullptr);
return var->extinctionOpponentPieceCount;
}
inline bool Position::extinction_pseudo_royal() const {
assert(var != nullptr);
return var->extinctionPseudoRoyal;
}
inline PieceType Position::flag_piece(Color c) const {
assert(var != nullptr);
return var->flagPiece[c];
}
inline Bitboard Position::flag_region(Color c) const {
assert(var != nullptr);
return var->flagRegion[c];
}
inline bool Position::flag_move() const {
assert(var != nullptr);
return var->flagMove;
}
inline bool Position::flag_reached(Color c) const {
assert(var != nullptr);
bool simpleResult =
(flag_region(c) & pieces(c, flag_piece(c)))
&& ( popcount(flag_region(c) & pieces(c, flag_piece(c))) >= var->flagPieceCount
|| (var->flagPieceBlockedWin && !(flag_region(c) & ~pieces())));
if (simpleResult&&var->flagPieceSafe)