-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovgen.c
1037 lines (1026 loc) · 47.5 KB
/
movgen.c
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
#include "chess.h"
#include "data.h"
/* modified 10/28/09 */
/*
*******************************************************************************
* *
* GenerateCaptures() is used to generate capture and pawn promotion moves *
* from the current position. *
* *
* The destination square set is the set of squares occupied by opponent *
* pieces, plus the set of squares on the 8th rank that pawns can advance to *
* and promote. *
* *
*******************************************************************************
*/
int *GenerateCaptures(TREE * RESTRICT tree, int ply, int wtm, int *move) {
register BITBOARD target, piecebd, moves;
register BITBOARD promotions, pcapturesl, pcapturesr;
register int from, to, temp, common, btm = Flip(wtm);
/*
************************************************************
* *
* Now, produce knight moves by cycling through the *
* *_knight board to locate a [from] square and then *
* cycling through knight_attacks[] to locate to squares *
* that a knight on [from] attacks. *
* *
************************************************************
*/
for (piecebd = Knights(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = knight_attacks[from] & Occupied(btm);
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce bishop moves by cycling through the *
* *_bishop board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Bishops(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksBishop(from, OccupiedSquares) & Occupied(btm);
temp = from + (bishop << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce rook moves by cycling through the *
* *_rook board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Rooks(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksRook(from, OccupiedSquares) & Occupied(btm);
temp = from + (rook << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce queen moves by cycling through the *
* *_queen board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Queens(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksQueen(from, OccupiedSquares) & Occupied(btm);
temp = from + (queen << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce king moves by cycling through the *
* *_king board to locate a [from] square and then *
* cycling through king_attacks[] to locate to squares *
* that a king on [from] attacks. *
* *
************************************************************
*/
from = KingSQ(wtm);
moves = king_attacks[from] & Occupied(btm);
temp = from + (king << 12);
Unpack(wtm, move, moves, temp);
/*
************************************************************
* *
* Now, produce pawn moves. This is done differently due *
* to inconsistencies in the way a pawn moves when it *
* captures as opposed to normal non-capturing moves. *
* Another exception is capturing enpassant. The first *
* step is to generate all possible pawn promotions. We *
* do this by masking all pawns but those on the 7th *
* rank and then advancing them ahead if the square in *
* front is empty. *
* *
************************************************************
*/
promotions =
((wtm) ? (Pawns(white) & rank_mask[RANK7]) << 8 : (Pawns(black) &
rank_mask[RANK2]) >> 8) & ~OccupiedSquares;
for (; promotions; Clear(to, promotions)) {
to = LSB(promotions);
*move++ = (to + pawnadv1[wtm]) | (to << 6) | (pawn << 12) | (queen << 18);
}
target = Occupied(btm) | EnPassantTarget(ply);
pcapturesl =
((wtm) ? (Pawns(wtm) & mask_left_edge) << 7 : (Pawns(wtm) &
mask_left_edge) >> 9) & target;
pcapturesr =
((wtm) ? (Pawns(wtm) & mask_right_edge) << 9 : (Pawns(wtm) &
mask_right_edge) >> 7) & target;
for (; pcapturesl; Clear(to, pcapturesl)) {
to = Advanced(wtm, pcapturesl);
common = (to + capleft[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56))
*move++ = common | (((PcOnSq(to)) ? Abs(PcOnSq(to)) : pawn) << 15);
else
*move++ = common | (Abs(PcOnSq(to)) << 15) | (queen << 18);
}
for (; pcapturesr; Clear(to, pcapturesr)) {
to = Advanced(wtm, pcapturesr);
common = (to + capright[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56))
*move++ = common | (((PcOnSq(to)) ? Abs(PcOnSq(to)) : pawn) << 15);
else
*move++ = common | (Abs(PcOnSq(to)) << 15) | (queen << 18);
}
return (move);
}
/* modified 10/28/09 */
/*
*******************************************************************************
* *
* GenerateChecks() is used to generate non-capture moves from the current *
* position. *
* *
* The first pass produces a bitmap that contains the squares a particular *
* piece type would attack if sitting on the square the enemy king sits on. *
* We then use each of these squares as a source and check to see if the *
* same piece type attacks one of these common targets. If so, we can move *
* that piece to that square and it will directly attack the king. We do *
* this for pawns, knights, bishops, rooks and queens to produce the set of *
* "direct checking moves." *
* *
* Then we generate discovered checks in two passes, once for diagonal *
* attacks and once for rank/file attacks (we do it in two passes since a *
* rook can't produce a discovered check along a rank or file since it moves *
* in that direction as well. For diagonals, we first generate the bishop *
* attacks from the enemy king square and mask them with the friendly piece *
* occupied squares bitmap. This gives us a set of up to 4 "blocking *
* pieces" that could be preventing a check. We then remove them via the *
* "magic move generation" tricks, and see if we now reach friendly bishops *
* or queens on those diagonals. If we have a friendly blocker, and a *
* friendly diagonal mover behind that blocker, then moving the blocker is *
* a discovered check (and there could be double-checks included but we do *
* not check for that since a single check is good enough). We repeat this *
* for the ranks/files and we are done. *
* *
* For the present, this code does not produce discovered checks by the *
* king since all king moves are not discovered checks because the king can *
* move in the same direction as the piece it blocks and not uncover the *
* attack. This might be fixed at some point, but it is rare enough to not *
* be an issue except in far endgames. *
* *
*******************************************************************************
*/
int *GenerateChecks(TREE * RESTRICT tree, int ply, int wtm, int *move) {
register BITBOARD temp_target, target, piecebd, moves;
register BITBOARD padvances1, blockers, checkers;
register int from, to, promote, temp, btm = Flip(wtm);
/*
*********************************************************************
* *
* First pass: produce direct checks. For each piece type, we *
* pretend that a piece of that type stands on the square of the *
* king and we generate attacks from that square for that piece. *
* Now, if we can find any piece of that type that attacks one of *
* those squares, then that piece move would deliver a direct *
* check to the enemy king. Easy, wasn't it? *
* *
*********************************************************************
*/
target = ~OccupiedSquares;
/*
************************************************************
* *
* Knight direct checks. *
* *
************************************************************
*/
temp_target = target & knight_attacks[KingSQ(btm)];
for (piecebd = Knights(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = knight_attacks[from] & temp_target;
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Bishop direct checks. *
* *
************************************************************
*/
temp_target = target & AttacksBishop(KingSQ(btm), OccupiedSquares);
for (piecebd = Bishops(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksBishop(from, OccupiedSquares) & temp_target;
temp = from + (bishop << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Rook direct checks. *
* *
************************************************************
*/
temp_target = target & AttacksRook(KingSQ(btm), OccupiedSquares);
for (piecebd = Rooks(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksRook(from, OccupiedSquares) & temp_target;
temp = from + (rook << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Queen direct checks. *
* *
************************************************************
*/
temp_target = target & AttacksQueen(KingSQ(btm), OccupiedSquares);
for (piecebd = Queens(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksQueen(from, OccupiedSquares) & temp_target;
temp = from + (queen << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Pawn direct checks. *
* *
************************************************************
*/
temp_target = target & pawn_attacks[btm][KingSQ(btm)];
padvances1 = ((wtm) ? Pawns(wtm) << 8 : Pawns(wtm) >> 8) & temp_target;
for (; padvances1; Clear(to, padvances1)) {
to = Advanced(wtm, padvances1);
*move++ = (to + pawnadv1[wtm]) | (to << 6) | (pawn << 12);
}
/*
*********************************************************************
* *
* Second pass: produce discovered checks. Here we do things a *
* bit different. We first take diagonal movers. From the enemy *
* king's position, we generate diagonal moves to see if any of *
* them end at one of our pieces that does not slide diagonally, *
* such as a rook, knight or pawn. If we find one, we look on down *
* that diagonal to see if we now find a diagonal mover (queen or *
* bishop). If so, any legal move by this piece (except captures *
* which have already been generated) will be a discovered check *
* that needs to be searched. We do the same for vertical / *
* horizontal rays that are blocked by pawns, bishops, knights or *
* kings that would hide a discovered check by a rook or queen. *
* *
*********************************************************************
*/
/*
************************************************************
* *
* First we look for diagonal discovered attacks. Once *
* we know which squares hold pieces that create a *
* discovered check when they move, we generate them *
* piece type by piece type. *
* *
************************************************************
*/
blockers =
AttacksBishop(KingSQ(btm),
OccupiedSquares) & (Rooks(wtm) | Knights(wtm) | Pawns(wtm));
if (blockers) {
checkers =
AttacksBishop(KingSQ(btm),
OccupiedSquares & ~blockers) & (Bishops(wtm) | Queens(wtm));
if (checkers) {
if ((plus7dir[KingSQ(btm)] & blockers) &&
!(plus7dir[KingSQ(btm)] & checkers))
blockers &= ~plus7dir[KingSQ(btm)];
if ((plus9dir[KingSQ(btm)] & blockers) &&
!(plus9dir[KingSQ(btm)] & checkers))
blockers &= ~plus9dir[KingSQ(btm)];
if ((minus7dir[KingSQ(btm)] & blockers) &&
!(minus7dir[KingSQ(btm)] & checkers))
blockers &= ~minus7dir[KingSQ(btm)];
if ((minus9dir[KingSQ(btm)] & blockers) &&
!(minus9dir[KingSQ(btm)] & checkers))
blockers &= ~minus9dir[KingSQ(btm)];
/*
************************************************************
* *
* Knight discovered checks. *
* *
************************************************************
*/
target = ~OccupiedSquares;
temp_target = target & ~knight_attacks[KingSQ(btm)];
for (piecebd = Knights(wtm) & blockers; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = knight_attacks[from] & temp_target;
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Rook discovered checks. *
* *
************************************************************
*/
target = ~OccupiedSquares;
temp_target = target & ~AttacksRook(KingSQ(btm), OccupiedSquares);
for (piecebd = Rooks(wtm) & blockers; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksRook(from, OccupiedSquares) & temp_target;
temp = from + (rook << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Pawn discovered checks. *
* *
************************************************************
*/
piecebd =
Pawns(wtm) & blockers & ((wtm) ? ~OccupiedSquares >> 8 :
~OccupiedSquares << 8);
for (; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
to = from + pawnadv1[btm];
if ((wtm && to > 55) || (btm && to < 8))
promote = queen;
else
promote = 0;
*move++ = from | (to << 6) | (pawn << 12) | (promote << 18);
}
}
}
/*
************************************************************
* *
* Next, we look for rank/file discovered attacks. Once *
* we know which squares hold pieces that create a *
* discovered check when they move, we generate them *
* piece type by piece type. *
* *
************************************************************
*/
blockers =
AttacksRook(KingSQ(btm),
OccupiedSquares) & (Bishops(wtm) | Knights(wtm) | (Pawns(wtm) &
rank_mask[Rank(KingSQ(btm))]));
if (blockers) {
checkers =
AttacksRook(KingSQ(btm),
OccupiedSquares & ~blockers) & (Rooks(wtm) | Queens(wtm));
if (checkers) {
if ((plus1dir[KingSQ(btm)] & blockers) &&
!(plus1dir[KingSQ(btm)] & checkers))
blockers &= ~plus1dir[KingSQ(btm)];
if ((plus8dir[KingSQ(btm)] & blockers) &&
!(plus8dir[KingSQ(btm)] & checkers))
blockers &= ~plus8dir[KingSQ(btm)];
if ((minus1dir[KingSQ(btm)] & blockers) &&
!(minus1dir[KingSQ(btm)] & checkers))
blockers &= ~minus1dir[KingSQ(btm)];
if ((minus8dir[KingSQ(btm)] & blockers) &&
!(minus8dir[KingSQ(btm)] & checkers))
blockers &= ~minus8dir[KingSQ(btm)];
/*
************************************************************
* *
* Knight discovered checks. *
* *
************************************************************
*/
target = ~OccupiedSquares;
temp_target = target & ~knight_attacks[KingSQ(btm)];
for (piecebd = Knights(wtm) & blockers; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = knight_attacks[from] & temp_target;
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Bishop discovered checks. *
* *
************************************************************
*/
target = ~OccupiedSquares;
temp_target = target & ~AttacksBishop(KingSQ(btm), OccupiedSquares);
for (piecebd = Bishops(wtm) & blockers; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksBishop(from, OccupiedSquares) & temp_target;
temp = from + (bishop << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Pawn discovered checks. *
* *
************************************************************
*/
piecebd =
Pawns(wtm) & blockers & ((wtm) ? ~OccupiedSquares >> 8 :
~OccupiedSquares << 8);
for (; piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
to = from + pawnadv1[btm];
if ((wtm && to > 55) || (btm && to < 8))
promote = queen;
else
promote = 0;
*move++ = from | (to << 6) | (pawn << 12) | (promote << 18);
}
}
}
return (move);
}
/* modified 10/28/09 */
/*
*******************************************************************************
* *
* GenerateCheckEvasions() is used to generate moves when the king is in *
* check. *
* *
* Three types of check-evasion moves are generated: *
* *
* (1) Generate king moves to squares that are not attacked by the *
* opponent's pieces. this includes capture and non-capture moves. *
* *
* (2) Generate interpositions along the rank/file that the checking attack *
* is coming along (assuming (a) only one piece is checking the king, and *
* (b) the checking piece is a sliding piece [bishop, rook, queen]). *
* *
* (3) Generate capture moves, but only to the square(s) that are giving *
* check. Captures are a special case. If there is one checking piece, then*
* capturing it by any piece is tried. If there are two pieces checking the *
* king, then the only legal capture to try is for the king to capture one of*
* the checking pieces that is on an un-attacked square. *
* *
*******************************************************************************
*/
int *GenerateCheckEvasions(TREE * RESTRICT tree, int ply, int wtm, int *move) {
register BITBOARD target, targetc, targetp, piecebd, moves;
register BITBOARD padvances1, padvances2, pcapturesl, pcapturesr;
register BITBOARD padvances1_all, empty, checksqs, ep;
register int from, to, temp, common, btm = Flip(wtm);
register int king_square, checkers, checking_square;
register int check_direction1 = 0, check_direction2 = 0;
/*
************************************************************
* *
* First, determine how many pieces are attacking the *
* king and where they are, so we can figure out how to *
* legally get out of check. *
* *
************************************************************
*/
king_square = KingSQ(wtm);
checksqs = AttacksTo(tree, king_square) & Occupied(btm);
checkers = PopCnt(checksqs);
if (checkers == 1) {
checking_square = LSB(checksqs);
if (PcOnSq(checking_square) != pieces[btm][pawn])
check_direction1 = directions[checking_square][king_square];
target = InterposeSquares(check_direction1, king_square, checking_square);
target |= checksqs;
target |= Kings(btm);
} else {
target = Kings(btm);
checking_square = LSB(checksqs);
if (PcOnSq(checking_square) != pieces[btm][pawn])
check_direction1 = directions[checking_square][king_square];
Clear(checking_square, checksqs);
checking_square = LSB(checksqs);
if (PcOnSq(checking_square) != pieces[btm][pawn])
check_direction2 = directions[checking_square][king_square];
}
/*
************************************************************
* *
* The next step is to produce the set of valid *
* destination squares. For king moves, this is simply *
* the set of squares that are not attacked by enemy *
* pieces (if there are any such squares.) *
* *
* Then, if the checking piece is not a knight, we need *
* to know the checking direction so that we can either *
* move the king "off" of that direction, or else "block" *
* that direction. *
* *
* First produce king moves by cycling through the *
* *_king board to locate a [from] square and then *
* cycling through attacks_to[] to locate to squares *
* that the king on [from] attacks. *
* *
************************************************************
*/
from = king_square;
temp = from + (king << 12);
for (moves = king_attacks[from] & ~Occupied(wtm); moves; Clear(to, moves)) {
to = Advanced(wtm, moves);
if (!Attacks(tree, to, btm)
&& (directions[from][to] != check_direction1)
&& (directions[from][to] != check_direction2))
*move++ = temp | (to << 6) | (Abs(PcOnSq(to)) << 15);
}
/*
************************************************************
* *
* Now, produce knight moves by cycling through the *
* *_knight board to locate a [from] square and then *
* cycling through knight_attacks[] to locate to squares *
* that a knight on [from] attacks. *
* *
************************************************************
*/
if (checkers == 1) {
for (piecebd = Knights(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
if (!PinnedOnKing(tree, wtm, from)) {
moves = knight_attacks[from] & target;
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
}
/*
************************************************************
* *
* Now, produce bishop moves by cycling through the *
* *_bishop board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Bishops(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
if (!PinnedOnKing(tree, wtm, from)) {
moves = AttacksBishop(from, OccupiedSquares) & target;
temp = from + (bishop << 12);
Unpack(wtm, move, moves, temp);
}
}
/*
************************************************************
* *
* Now, produce rook moves by cycling through the *
* *_rook board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Rooks(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
if (!PinnedOnKing(tree, wtm, from)) {
moves = AttacksRook(from, OccupiedSquares) & target;
temp = from + (rook << 12);
Unpack(wtm, move, moves, temp);
}
}
/*
************************************************************
* *
* Now, produce queen moves by cycling through the *
* *_queen board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Queens(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
if (!PinnedOnKing(tree, wtm, from)) {
moves = AttacksQueen(from, OccupiedSquares) & target;
temp = from + (queen << 12);
Unpack(wtm, move, moves, temp);
}
}
/*
************************************************************
* *
* Now, produce pawn moves. This is done differently due *
* to inconsistencies in the way a pawn moves when it *
* captures as opposed to normal non-capturing moves. *
* another exception is capturing enpassant. The first *
* step is to generate all possible pawn moves. We do *
* this in 2 operations: (1) shift the pawns forward one *
* rank then and with empty squares; (2) shift the pawns *
* forward two ranks and then and with empty squares. *
* *
************************************************************
*/
empty = ~OccupiedSquares;
targetp = target & empty;
if (wtm) {
padvances1 = Pawns(white) << 8 & targetp;
padvances1_all = Pawns(white) << 8 & empty;
padvances2 = ((padvances1_all & ((BITBOARD) 255 << 16)) << 8) & targetp;
} else {
padvances1 = Pawns(black) >> 8 & targetp;
padvances1_all = Pawns(black) >> 8 & empty;
padvances2 = ((padvances1_all & ((BITBOARD) 255 << 40)) >> 8) & targetp;
}
/*
************************************************************
* *
* Now that we got 'em, we simply enumerate the to *
* squares as before, but in four steps since we have *
* four sets of potential moves. *
* *
************************************************************
*/
for (; padvances2; Clear(to, padvances2)) {
to = Advanced(wtm, padvances2);
if (!PinnedOnKing(tree, wtm, to + pawnadv2[wtm]))
*move++ = (to + pawnadv2[wtm]) | (to << 6) | (pawn << 12);
}
for (; padvances1; Clear(to, padvances1)) {
to = Advanced(wtm, padvances1);
if (!PinnedOnKing(tree, wtm, to + pawnadv1[wtm])) {
common = (to + pawnadv1[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56))
*move++ = common;
else {
*move++ = common | (queen << 18);
*move++ = common | (rook << 18);
*move++ = common | (bishop << 18);
*move++ = common | (knight << 18);
}
}
}
targetc = Occupied(btm) | EnPassantTarget(ply);
targetc = targetc & target;
ep = Pawns(btm) & target & ((wtm) ? EnPassantTarget(ply) >> 8 :
EnPassantTarget(ply) << 8);
if (Pawns(btm) & target & ((wtm) ? EnPassantTarget(ply) >> 8 :
EnPassantTarget(ply) << 8))
targetc = targetc | EnPassantTarget(ply);
if (wtm) {
pcapturesl = (Pawns(white) & mask_left_edge) << 7 & targetc;
pcapturesr = (Pawns(white) & mask_right_edge) << 9 & targetc;
} else {
pcapturesl = (Pawns(black) & mask_left_edge) >> 9 & targetc;
pcapturesr = (Pawns(black) & mask_right_edge) >> 7 & targetc;
}
for (; pcapturesl; Clear(to, pcapturesl)) {
to = Advanced(wtm, pcapturesl);
if (!PinnedOnKing(tree, wtm, to + capleft[wtm])) {
common = (to + capleft[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56)) {
*move++ = common | (((PcOnSq(to)) ? Abs(PcOnSq(to)) : pawn) << 15);
} else {
*move++ = common | (Abs(PcOnSq(to)) << 15) | (queen << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (rook << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (bishop << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (knight << 18);
}
}
}
for (; pcapturesr; Clear(to, pcapturesr)) {
to = Advanced(wtm, pcapturesr);
if (!PinnedOnKing(tree, wtm, to + capright[wtm])) {
common = (to + capright[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56)) {
*move++ = common | (((PcOnSq(to)) ? Abs(PcOnSq(to)) : pawn) << 15);
} else {
*move++ = common | (Abs(PcOnSq(to)) << 15) | (queen << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (rook << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (bishop << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (knight << 18);
}
}
}
}
return (move);
}
/* modified 10/28/09 */
/*
*******************************************************************************
* *
* GenerateNoncaptures() is used to generate non-capture moves from the *
* current position. *
* *
* Once the valid destination squares are known, we have to locate a friendly*
* piece to get a attacks_to[] entry. We then produce the moves for this *
* piece by using the source square as [from] and enumerating each square it *
* attacks into [to]. *
* *
* Pawns are handled differently. Regular pawn moves are produced by *
* shifting the pawn bitmap 8 bits "forward" and anding this with the *
* complement of the occupied squares bitmap double advances are then *
* produced by anding the pawn bitmap with a mask containing 1's on the *
* second rank, shifting this 16 bits "forward" and then anding this with *
* the complement of the occupied squares bitmap as before. If [to] *
* reaches the 8th rank, we produce a set of four moves, promoting the pawn *
* to knight, bishop, rook and queen. *
* *
*******************************************************************************
*/
int *GenerateNoncaptures(TREE * RESTRICT tree, int ply, int wtm, int *move) {
register BITBOARD target, piecebd, moves;
register BITBOARD padvances1, padvances2, pcapturesl, pcapturesr;
register int from, to, temp, common, btm = Flip(wtm);
/*
************************************************************
* *
* First, produce castling moves if it is legal. *
* *
************************************************************
*/
if (Castle(ply, wtm) > 0) {
if ((Castle(ply, wtm) & 1) && !(OccupiedSquares & OO[wtm]) &&
!Attacks(tree, OOsqs[wtm][0], btm) &&
!Attacks(tree, OOsqs[wtm][1], btm)
&& !Attacks(tree, OOsqs[wtm][2], btm)) {
*move++ = (king << 12) + (OOto[wtm] << 6) + OOfrom[wtm];
}
if ((Castle(ply, wtm) & 2) && !(OccupiedSquares & OOO[wtm]) &&
!Attacks(tree, OOOsqs[wtm][0], btm) &&
!Attacks(tree, OOOsqs[wtm][1], btm) &&
!Attacks(tree, OOOsqs[wtm][2], btm)) {
*move++ = (king << 12) + (OOOto[wtm] << 6) + OOfrom[wtm];
}
}
/*
************************************************************
* *
* Now, produce knight moves by cycling through the *
* *_knight board to locate a [from] square and then *
* cycling through knight_attacks[] to locate to squares *
* that a knight on [from] attacks. *
* *
************************************************************
*/
target = ~OccupiedSquares;
for (piecebd = Knights(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = knight_attacks[from] & target;
temp = from + (knight << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce bishop moves by cycling through the *
* *_bishop board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Bishops(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksBishop(from, OccupiedSquares) & target;
temp = from + (bishop << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce rook moves by cycling through the *
* *_rook board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Rooks(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksRook(from, OccupiedSquares) & target;
temp = from + (rook << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce queen moves by cycling through the *
* *_queen board to locate a [from] square and then *
* generate the AttacksFrom() bitmap which supplies the *
* list of valid <to> squares. *
* *
************************************************************
*/
for (piecebd = Queens(wtm); piecebd; Clear(from, piecebd)) {
from = Advanced(wtm, piecebd);
moves = AttacksQueen(from, OccupiedSquares) & target;
temp = from + (queen << 12);
Unpack(wtm, move, moves, temp);
}
/*
************************************************************
* *
* Now, produce king moves by cycling through the *
* *_king board to locate a [from] square and then *
* cycling through king_attacks[] to locate to squares *
* that a king on [from] attacks. *
* *
************************************************************
*/
from = KingSQ(wtm);
moves = king_attacks[from] & target;
temp = from + (king << 12);
Unpack(wtm, move, moves, temp);
/*
************************************************************
* *
* Now, produce pawn moves. This is done differently due *
* to inconsistencies in the way a pawn moves when it *
* captures as opposed to normal non-capturing moves. *
* First we generate all possible pawn moves. We do *
* this in 4 operations: (1) shift the pawns forward one *
* rank then and with empty squares; (2) shift the pawns *
* forward two ranks and then and with empty squares; *
* (3) remove the a-pawn(s) then shift the pawns *
* diagonally left then and with enemy occupied squares; *
* (4) remove the h-pawn(s) then shift the pawns *
* diagonally right then and with enemy occupied squares. *
* note that the only captures produced are under- *
* promotions, because the rest were done in GenCap. *
* *
************************************************************
*/
padvances1 = ((wtm) ? Pawns(wtm) << 8 : Pawns(wtm) >> 8) & target;
padvances2 =
((wtm) ? (padvances1 & mask_advance_2_w) << 8 : (padvances1 &
mask_advance_2_b) >> 8) & target;
/*
************************************************************
* *
* Now that we got 'em, we simply enumerate the to *
* squares as before, but in four steps since we have *
* four sets of potential moves. *
* *
************************************************************
*/
for (; padvances2; Clear(to, padvances2)) {
to = Advanced(wtm, padvances2);
*move++ = (to + pawnadv2[wtm]) | (to << 6) | (pawn << 12);
}
for (; padvances1; Clear(to, padvances1)) {
to = Advanced(wtm, padvances1);
common = (to + pawnadv1[wtm]) | (to << 6) | (pawn << 12);
if ((wtm == 0 && to > 7) || (wtm == 1 && to < 56))
*move++ = common;
else {
*move++ = common | (rook << 18);
*move++ = common | (bishop << 18);
*move++ = common | (knight << 18);
}
}
/*
************************************************************
* *
* Generate the rest of the capture/promotions here since *
* GenerateCaptures() only generates captures that are *
* promotions to a queen. *
* *
************************************************************
*/
target = Occupied(btm) & rank_mask[RANK1 + wtm * 7];
pcapturesl =
((wtm) ? (Pawns(white) & mask_left_edge) << 7 : (Pawns(black) &
mask_left_edge) >> 9) & target;
pcapturesr =
((wtm) ? (Pawns(white) & mask_right_edge) << 9 : (Pawns(black) &
mask_right_edge) >> 7) & target;
for (; pcapturesl; Clear(to, pcapturesl)) {
to = Advanced(wtm, pcapturesl);
common = (to + capleft[wtm]) | (to << 6) | (pawn << 12);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (rook << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (bishop << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (knight << 18);
}
for (; pcapturesr; Clear(to, pcapturesr)) {
to = Advanced(wtm, pcapturesr);
common = (to + capright[wtm]) | (to << 6) | (pawn << 12);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (rook << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (bishop << 18);
*move++ = common | (Abs(PcOnSq(to)) << 15) | (knight << 18);
}
return (move);
}
/*
*******************************************************************************
* *
* InterposeSquares() is used to compute the set of squares that block an *
* attack on the king by a sliding piece, by interposing any piece between *
* the attacking piece and the king on the same ray. *
* *
*******************************************************************************
*/
BITBOARD InterposeSquares(int check_direction, int king_square,
int checking_square) {
register BITBOARD target;
/*
************************************************************
* *
* If this is a check from a single sliding piece, then *
* we can interpose along the checking rank/file/diagonal *
* and block the check. Otherwise, interposing is not a *
* possibility. *
* *
************************************************************
*/
switch (check_direction) {
case +1:
target = plus1dir[king_square - 1] ^ plus1dir[checking_square];
break;
case +7:
target = plus7dir[king_square - 7] ^ plus7dir[checking_square];
break;
case +8:
target = plus8dir[king_square - 8] ^ plus8dir[checking_square];
break;
case +9:
target = plus9dir[king_square - 9] ^ plus9dir[checking_square];
break;
case -1:
target = minus1dir[king_square + 1] ^ minus1dir[checking_square];
break;
case -7:
target = minus7dir[king_square + 7] ^ minus7dir[checking_square];
break;
case -8:
target = minus8dir[king_square + 8] ^ minus8dir[checking_square];
break;
case -9:
target = minus9dir[king_square + 9] ^ minus9dir[checking_square];
break;
default:
target = 0;
break;
}
return (target);
}
/* modified 09/23/09 */
/*
*******************************************************************************
* *
* PinnedOnKing() is used to determine if the piece on <square> is pinned *
* against the king, so that it's illegal to move it. This is used to cull *
* potential moves by GenerateCheckEvasions() so that illegal moves are not *
* produced. *
* *
*******************************************************************************
*/
int PinnedOnKing(TREE * RESTRICT tree, int wtm, int square) {
register int ray;
register int btm = Flip(wtm);
/*
************************************************************
* *
* First, determine if the piece being moved is on the *
* same diagonal, rank or file as the king. *
* *
************************************************************
*/