forked from MoonstoneLight/Fizbo-Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_util.cpp
1528 lines (1389 loc) · 60.7 KB
/
board_util.cpp
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
// board utilities
#include "chess.h"
#include <intrin.h>
#define USE_PREFETCH 1
extern UINT64 *eh; // eval hash pointer
extern unsigned int pv10[];
UINT64 knight_masks[64]; // knight masks - all directions.
UINT64 bishop_masks[64]; // bishop masks - 4 directions, all cells excluding the one where bishop is.
UINT64 rook_masks[64]; // rook masks - 4 directions, all cells excluding the one where rook is.
UINT64 king_masks[64]; // king masks - all directions.
UINT64 ray_segment[64][64]; // segment of a ray from X to Y. Zero if X and Y are not on a straight line. 32 Kb in size.
UINT64 dir_mask[5][64]; // mask of allowed move directions. 0-all, 1-1, 2-7, 3-8, 4-9.
static unsigned int castle_mask[64]; // move to/from, 4 bit masks. And with board to update, for both "from" and "to" cells.
UINT64 zorb_castle[16]; // castle zorb
unsigned char dir_norm[64][64]; // move to/from. Normalized direction - 1,7,8,9.
unsigned char dist[64][64]; // move to/from. Distance, 0 to 7. Max over x/y.
const UINT64 pawn_attacks[2][64]={{ // cell attacked by white/black pawns
0x0000000000000000,0x0000000000000100,0x0000000000000200,0x0000000000000400,0x0000000000000800,0x0000000000001000,0x0000000000002000,0x0000000000004000,
0x0000000000000000,0x0000000000010001,0x0000000000020002,0x0000000000040004,0x0000000000080008,0x0000000000100010,0x0000000000200020,0x0000000000400040,
0x0000000000000000,0x0000000001000100,0x0000000002000200,0x0000000004000400,0x0000000008000800,0x0000000010001000,0x0000000020002000,0x0000000040004000,
0x0000000000000000,0x0000000100010000,0x0000000200020000,0x0000000400040000,0x0000000800080000,0x0000001000100000,0x0000002000200000,0x0000004000400000,
0x0000000000000000,0x0000010001000000,0x0000020002000000,0x0000040004000000,0x0000080008000000,0x0000100010000000,0x0000200020000000,0x0000400040000000,
0x0000000000000000,0x0001000100000000,0x0002000200000000,0x0004000400000000,0x0008000800000000,0x0010001000000000,0x0020002000000000,0x0040004000000000,
0x0000000000000000,0x0100010000000000,0x0200020000000000,0x0400040000000000,0x0800080000000000,0x1000100000000000,0x2000200000000000,0x4000400000000000,
0x0000000000000000,0x0001000000000000,0x0002000000000000,0x0004000000000000,0x0008000000000000,0x0010000000000000,0x0020000000000000,0x0040000000000000},
{0x0000000000000200,0x0000000000000400,0x0000000000000800,0x0000000000001000,0x0000000000002000,0x0000000000004000,0x0000000000008000,0x0000000000000000,
0x0000000000020002,0x0000000000040004,0x0000000000080008,0x0000000000100010,0x0000000000200020,0x0000000000400040,0x0000000000800080,0x0000000000000000,
0x0000000002000200,0x0000000004000400,0x0000000008000800,0x0000000010001000,0x0000000020002000,0x0000000040004000,0x0000000080008000,0x0000000000000000,
0x0000000200020000,0x0000000400040000,0x0000000800080000,0x0000001000100000,0x0000002000200000,0x0000004000400000,0x0000008000800000,0x0000000000000000,
0x0000020002000000,0x0000040004000000,0x0000080008000000,0x0000100010000000,0x0000200020000000,0x0000400040000000,0x0000800080000000,0x0000000000000000,
0x0002000200000000,0x0004000400000000,0x0008000800000000,0x0010001000000000,0x0020002000000000,0x0040004000000000,0x0080008000000000,0x0000000000000000,
0x0200020000000000,0x0400040000000000,0x0800080000000000,0x1000100000000000,0x2000200000000000,0x4000400000000000,0x8000800000000000,0x0000000000000000,
0x0002000000000000,0x0004000000000000,0x0008000000000000,0x0010000000000000,0x0020000000000000,0x0040000000000000,0x0080000000000000,0x0000000000000000}};
static const unsigned int dir_trans[]={0,1,0,0,0,0,0,2,3,4}; // translate direction from 1/7/8/9 to 1/2/3/4
void init_moves(void){
unsigned int i,j,k;
// init castle masks
for(i=0;i<64;++i) castle_mask[i]=1+2+4+8;// init to "castling rights not lost"
castle_mask[0]&=14; // rook at 0
castle_mask[56]&=13; // rook at 56
castle_mask[7]&=11; // rook at 7
castle_mask[63]&=7; // rook at 63
castle_mask[32]&=12; // king at 32, both ways
castle_mask[39]&=3; // king at 39, both ways
// init castle zorb
for(i=0;i<16;++i){
zorb_castle[i]=0;
if( i&1 ) zorb_castle[i]^=zorb[0][0][0];
if( i&2 ) zorb_castle[i]^=zorb[0][0][8];
if( i&4 ) zorb_castle[i]^=zorb[0][0][16];
if( i&8 ) zorb_castle[i]^=zorb[0][0][24];
}
// init direction normalization table
// init distance table
memset(dir_norm,0,sizeof(dir_norm));// init to "not valid direction"=0
for(i=0;i<64;++i)
for(j=0;j<64;++j){
dist[i][j]=max(abs(((int)i>>3)-((int)j>>3)),abs(((int)i&7)-((int)j&7)));// distance
if( i==j )// skip same to same attack - it is not real.
continue;
int x1=i%8,x2=j%8,y1=i/8,y2=j/8;
if(x1==x2)
dir_norm[i][j]=8;
else if(y1==y2)
dir_norm[i][j]=1;
else if( (y1-y2)==(x1-x2) )
dir_norm[i][j]=9;
else if( (y1-y2)==(x2-x1) )
dir_norm[i][j]=7;
}
// init attack bitmasks
UINT64 one=1,m;
int dx0[8]={1,-1,1,-1,1,-1,0,0},dy0[8]={1,1,-1,-1,0,0,1,-1};// ray directions: Bishop, then Rook
int dx1[8]={1,1,-1,-1,2,2,-2,-2},dy1[8]={2,-2,2,-2,1,-1,1,-1};// knight directions: +9,+7,-7,-9,+1,-1,+8,-8
for(i=0;i<64;++i){// loop over cell
rook_masks[i]=bishop_masks[i]=knight_masks[i]=king_masks[i]=0;
for(j=0;j<8;++j){// loop over directions
int x=i&7,y=i>>3,dx=dx0[j],dy=dy0[j];
x+=dx;y+=dy;// step 1
// get king masks
if( x>=0 && x<8 && y>=0 && y<8 )
king_masks[i]|=(one<<(x+y*8));
// get rook and bishop masks
m=0;
while( x>=0 && x<8 && y>=0 && y<8 ){// steps in direction dir
m|=(one<<(x+y*8));
x+=dx;y+=dy;// next step
}
if(j<4)
bishop_masks[i]|=m;
else
rook_masks[i]|=m;
// get knight masks
x=(i&7)+dx1[j];y=(i>>3)+dy1[j];
if( x>=0 && x<8 && y>=0 && y<8 )
knight_masks[i]|=(one<<(x+y*8));
}
}
// ray_segment, from i to j excluding i/j.
for(i=0;i<64;++i){// from
for(j=0;j<64;++j){// to
ray_segment[i][j]=0;
unsigned char d=dir_norm[i][j];// Normalized direction - 1,7,8,9.
if( d ){// on a straight line.
if(j>i){
for(k=i+d;k<j;k+=d)
ray_segment[i][j]|=(one<<k);
}else{
for(k=j+d;k<i;k+=d)
ray_segment[i][j]|=(one<<k);
}
}
}
}
// mask of allowed move directions. 0-all, 1-1, 2-7, 3-8, 4-9.
for(i=0;i<64;++i){// from
dir_mask[0][i]=dir_mask[1][i]=dir_mask[2][i]=dir_mask[3][i]=dir_mask[4][i]=0xffffffffffffffff;// init to "all"
for(j=0;j<64;++j){// to
k=dir_norm[i][j];// direction
if( k==0 ) dir_mask[0][i]^=one<<j;
if( k!=1 ) dir_mask[1][i]^=one<<j;
if( k!=7 ) dir_mask[2][i]^=one<<j;
if( k!=8 ) dir_mask[3][i]^=one<<j;
if( k!=9 ) dir_mask[4][i]^=one<<j;
}
}
//test - create some masks
/*{FILE *f=fopen("c://xde//chess//out//v.csv","w");
for(i=0;i<64;++i){
UINT64 m=0;
int x0=i&7,y0=i/8,x,y,z;
for( int dx=-1;dx<2;dx++){
for( int dy=-1;dy<2;dy++){
int dz=dx+dy*8;
if( dz==0 )
continue;
// move in direction dz
x=x0+dx;y=y0+dy;z=i+dz;
//while( x<8 && y<8 && x>=0 && y>=0 ){
while( x+dx<8 && y+dy<8 && x+dx>=0 && y+dy>=0 ){
m=m|(UINT64(1)<<z);
x+=dx;y+=dy;z+=dz;
}
}
}
// write to file
fprintf(f,"%I64x\n",m);
}
fclose(f);
exit(0);}*/
}
static inline void set_piece(board *b,unsigned int cell,unsigned int piece){// set piece at position "cell"
b->piece[cell]=piece;
}
static inline unsigned int get_knight_moves(board *b,unsigned char *list,unsigned char player,unsigned int cell,UINT64 allowed_mask){
UINT64 attack_mask=knight_masks[cell]&allowed_mask;
unsigned int mc=0;
while( attack_mask ){unsigned long bit;
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
return(mc);
}
static inline unsigned int get_bishop_moves(board *b,unsigned char *list,unsigned char player,unsigned int cell,UINT64 o,UINT64 allowed_mask){
UINT64 attack_mask=attacks_bb_B(cell,o)&allowed_mask;
unsigned int mc=0;
while( attack_mask ){unsigned long bit;
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
return(mc);
}
static inline unsigned int get_rook_moves(board *b,unsigned char *list,unsigned char player,unsigned int cell,UINT64 o,UINT64 allowed_mask){
UINT64 attack_mask=attacks_bb_R(cell,o)&allowed_mask;
unsigned int mc=0;
while( attack_mask ){unsigned long bit;
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
return(mc);
}
static inline unsigned int get_queen_moves(board *b,unsigned char *list,unsigned char player,unsigned int cell,UINT64 o,UINT64 allowed_mask){
UINT64 attack_mask=(attacks_bb_R(cell,o)|attacks_bb_B(cell,o))&allowed_mask;
unsigned int mc=0;
while( attack_mask ){unsigned long bit;
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
return(mc);
}
static inline unsigned int get_king_moves_l(board *b,unsigned char *list,unsigned char player,unsigned int cell){
UINT64 p_a,attack_mask;
// eliminate cells attacked by opp pawns
if( (player&2) )
p_a=(b->piececolorBB[0][0]<<9) | (b->piececolorBB[0][0]>>7);// white pawns attack
else
p_a=(b->piececolorBB[0][1]<<7) | (b->piececolorBB[0][1]>>9);// black pawns attack
p_a|=b->colorBB[player-1];// eliminate cells occupied by player (keep opponent - those are attacks)
p_a|=king_masks[b->kp[2-player]];// eliminate cells attacked by opp king
attack_mask=king_masks[cell]&(~p_a);
unsigned long bit;
unsigned int mc=0;
while( attack_mask ){
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
if( b->castle ){// skip castle logic if all flags are off
// check for castling
// king is not in check, it does not pass through a square that is under attack by an enemy piece, and does not end up in check.
if( player==1 ){//white
if( (b->castle&1) && (b->piece[8]+b->piece[16]+b->piece[24])==0 && !cell_under_attack(b,16,1) && !cell_under_attack(b,24,1) && !cell_under_attack(b,32,1) && dist[b->kp[1]][16]>1 && dist[b->kp[1]][24]>1 && dist[b->kp[1]][32]>1 ){// up
list[0]=32;list[1]=16;mc++;list+=2;
}
if( (b->castle&2) && (b->piece[40]+b->piece[48])==0 && !cell_under_attack(b,32,1) && !cell_under_attack(b,40,1) && !cell_under_attack(b,48,1) && dist[b->kp[1]][48]>1 && dist[b->kp[1]][40]>1 && dist[b->kp[1]][32]>1 ){// down
list[0]=32;list[1]=48;mc++;list+=2;
}
}else{// black
if( (b->castle&4) && (b->piece[15]+b->piece[23]+b->piece[31])==0 && !cell_under_attack(b,23,2) && !cell_under_attack(b,31,2) && !cell_under_attack(b,39,2) && dist[b->kp[0]][23]>1 && dist[b->kp[0]][31]>1 && dist[b->kp[0]][39]>1 ){// up
list[0]=39;list[1]=23;mc++;list+=2;
}
if( (b->castle&8) && (b->piece[47]+b->piece[55])==0 && !cell_under_attack(b,39,2) && !cell_under_attack(b,47,2) && !cell_under_attack(b,55,2) && dist[b->kp[0]][55]>1 && dist[b->kp[0]][47]>1 && dist[b->kp[0]][39]>1 ){// down
list[0]=39;list[1]=55;mc++;list+=2;
}
}
}
return(mc);
}
static inline unsigned int get_king_moves_l_no_captures(board *b,unsigned char *list,unsigned char player,unsigned int cell){
UINT64 p_a,attack_mask;
// eliminate cells attacked by opp pawns
if( (player&2) )
p_a=(b->piececolorBB[0][0]<<9) | (b->piececolorBB[0][0]>>7);// white pawns attack
else
p_a=(b->piececolorBB[0][1]<<7) | (b->piececolorBB[0][1]>>9);// black pawns attack
p_a|=b->colorBB[player-1];// eliminate cells occupied by player (keep opponent - those are attacks)
p_a|=king_masks[b->kp[2-player]];// eliminate cells attacked by opp king
attack_mask=king_masks[cell]&(~p_a)&(~b->colorBB[2-player]); // eliminate cells occupied by opponent
unsigned long bit;
unsigned int mc=0;
while( attack_mask ){
GET_BIT(attack_mask)
list[0]=cell;list[1]=(unsigned char)bit;mc++;list+=2;
}
if( b->castle ){// skip castle logic if all flags are off
// check for castling
// king is not in check, it does not pass through a square that is under attack by an enemy piece, and does not end up in check.
if( player==1 ){//white
if( (b->castle&1) && (b->piece[8]+b->piece[16]+b->piece[24])==0 && !cell_under_attack(b,16,1) && !cell_under_attack(b,24,1) && !cell_under_attack(b,32,1) && dist[b->kp[1]][16]>1 && dist[b->kp[1]][24]>1 && dist[b->kp[1]][32]>1 ){// up
list[0]=32;list[1]=16;mc++;list+=2;
}
if( (b->castle&2) && (b->piece[40]+b->piece[48])==0 && !cell_under_attack(b,32,1) && !cell_under_attack(b,40,1) && !cell_under_attack(b,48,1) && dist[b->kp[1]][48]>1 && dist[b->kp[1]][40]>1 && dist[b->kp[1]][32]>1 ){// down
list[0]=32;list[1]=48;mc++;list+=2;
}
}else{// black
if( (b->castle&4) && (b->piece[15]+b->piece[23]+b->piece[31])==0 && !cell_under_attack(b,23,2) && !cell_under_attack(b,31,2) && !cell_under_attack(b,39,2) && dist[b->kp[0]][23]>1 && dist[b->kp[0]][31]>1 && dist[b->kp[0]][39]>1 ){// up
list[0]=39;list[1]=23;mc++;list+=2;
}
if( (b->castle&8) && (b->piece[47]+b->piece[55])==0 && !cell_under_attack(b,39,2) && !cell_under_attack(b,47,2) && !cell_under_attack(b,55,2) && dist[b->kp[0]][55]>1 && dist[b->kp[0]][47]>1 && dist[b->kp[0]][39]>1 ){// down
list[0]=39;list[1]=55;mc++;list+=2;
}
}
}
return(mc);
}
static unsigned int get_ep_moves_l(board *b,unsigned char *list,unsigned char player){
UINT64 one=1,m1,m2;
unsigned int move_count=0,c_l;
// get all en passant captures
// Here discovered check can be caused by movement of either pawn, as well as only by movement of both -> use full pin logic. Slow but reliable.
if( player==1 ){// white
if( b->last_move>=9 && b->piece[b->last_move-9]==65 ){// from LM-9. Don't forget pin check.
m1=(one<<b->last_move)|(one<<(b->last_move-9));
m2=(one<<(b->last_move-1));
b->piececolorBB[0][0]^=m1;b->piececolorBB[0][1]^=m2;
b->colorBB[0]^=m1;b->colorBB[1]^=m2;// update occupied bitboards
c_l=cell_under_attack(b,b->kp[0],1); // see if it puts king in check
b->colorBB[0]^=m1;b->colorBB[1]^=m2;// update occupied bitboards
b->piececolorBB[0][0]^=m1;b->piececolorBB[0][1]^=m2;
if( !c_l ){
list[0]=b->last_move-9;list[1]=b->last_move;move_count++;list+=2;
}
}
if( b->last_move<=55 && b->piece[b->last_move+7]==65 ){// from LM+7. Don't forget pin check
m1=(one<<b->last_move)|(one<<(b->last_move+7));
m2=(one<<(b->last_move-1));
b->piececolorBB[0][0]^=m1;b->piececolorBB[0][1]^=m2;
b->colorBB[0]^=m1;b->colorBB[1]^=m2;// update occupied bitboards
c_l=cell_under_attack(b,b->kp[0],1); // see if it puts king in check
b->colorBB[0]^=m1;b->colorBB[1]^=m2;// update occupied bitboards
b->piececolorBB[0][0]^=m1;b->piececolorBB[0][1]^=m2;
if( !c_l ){
list[0]=b->last_move+7;list[1]=b->last_move;move_count++;list+=2;
}
}
}else{//black
if( b->last_move>=7 && b->piece[b->last_move-7]==129 ){// from LM-7. Don't forget pin check
m1=(one<<b->last_move)|(one<<(b->last_move-7));
m2=(one<<(b->last_move+1));
b->piececolorBB[0][1]^=m1;b->piececolorBB[0][0]^=m2;
b->colorBB[1]^=m1;b->colorBB[0]^=m2;// update occupied bitboards
c_l=cell_under_attack(b,b->kp[1],2); // see if it puts king in check
b->colorBB[1]^=m1;b->colorBB[0]^=m2;// update occupied bitboards
b->piececolorBB[0][1]^=m1;b->piececolorBB[0][0]^=m2;
if( !c_l ){
list[0]=b->last_move-7;list[1]=b->last_move;move_count++;list+=2;
}
}
if( b->last_move<=55 && b->piece[b->last_move+9]==129 ){// from LM+9. Don't forget pin check
m1=(one<<b->last_move)|(one<<(b->last_move+9));
m2=(one<<(b->last_move+1));
b->piececolorBB[0][1]^=m1;b->piececolorBB[0][0]^=m2;
b->colorBB[1]^=m1;b->colorBB[0]^=m2;// update occupied bitboards
c_l=cell_under_attack(b,b->kp[1],2); // see if it puts king in check
b->colorBB[1]^=m1;b->colorBB[0]^=m2;// update occupied bitboards
b->piececolorBB[0][1]^=m1;b->piececolorBB[0][0]^=m2;
if( !c_l ){
list[0]=b->last_move+9;list[1]=b->last_move;move_count++;list+=2;
}
}
}
return(move_count);
}
unsigned int get_all_moves_new_part1(board *b,unsigned char *list,UINT64 *pinBB_r){// get list of all available moves - captures and promotions only. Return count. Put moves on the list.
UINT64 move_count=0,j,mc1,pinBB=0,pinned_pawns=0,one=1,a,a2,o=b->colorBB[0]|b->colorBB[1],allowed_mask=b->colorBB[2-b->player];// only opp cell are allowed
unsigned long bit;
unsigned int king_position=b->kp[b->player-1],p_c=0,p_cell[8],p_d[8],player=b->player;
unsigned char p_d2[64];// for each cell, direction of allowed moves: 1,7,8,9. 0 if all allowed.
// find all absolutely pinned squares. Don't forget the pinned directions.
// a is mask of all opp sliders on king attack lines, excluding the ones blocked by OPP
a=(b->piececolorBB[2][2-player]|b->piececolorBB[4][2-player])&attacks_bb_B(king_position,b->colorBB[2-player]);// exclude OPP blockers
a|=(b->piececolorBB[3][2-player]|b->piececolorBB[4][2-player])&attacks_bb_R(king_position,b->colorBB[2-player]);// exclude OPP blockers
memset(p_d2,0,64);// init to "all allowed"
while( a ){
GET_BIT(a)
a2=ray_segment[king_position][bit]&b->colorBB[player-1];
if( popcnt64l(a2)==1 ){// exactly 1 player blocker. Use popcnt to avoid branch mispredicts.
BSF64l(&bit,a2); // now bit is position of the pinned piece
pinBB|=a2; // add it to bitboard. This adds a whole ray, not just one bit. But it does not matter. Used for pawns and knights only.
unsigned int dirnorm=dir_norm[bit][king_position];
if( !(b->piece[bit]&6) ){ // pawn only
if( dirnorm!=8 ){ // exclude pawns allowed in direction 8 only - that is not a valid pawn direction!
p_cell[p_c]=bit; // pinned pawn position
p_d[p_c]=dirnorm; // pinned pawn direction
p_c++; // count it
pinned_pawns|=a2; // mask of pinned pawns
}
}else
p_d2[bit]=dir_trans[dirnorm]; // pinned piece direction2=0,1,2,3,4. By cell. Used for sliders only.
}
}
*pinBB_r=pinBB; // return BB of pins
// get all en passant captures
if( b->last_move!=INVALID_LAST_MOVE ){// valid last move. This guarantees that at least 1 opp pawn is in correct place. They can only be disallowed by pins.
mc1=get_ep_moves_l(b,list,player);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get queen moves. Pin has to be considered.
a=b->piececolorBB[4][player-1];
while( a ){
GET_BIT(a)
mc1=get_queen_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get rook moves. Pin has to be considered.
a=b->piececolorBB[3][player-1];
while( a ){
GET_BIT(a)
mc1=get_rook_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get bishop moves. Pin has to be considered.
a=b->piececolorBB[2][player-1];
while( a ){
GET_BIT(a)
mc1=get_bishop_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get knight moves. Pin has to be considered.
a=b->piececolorBB[1][player-1]&(~pinBB);// BB of unpinned knights
while( a ){
GET_BIT(a)
mc1=get_knight_moves(b,list,player,bit,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get pawn moves. Pin has to be considered.
if( player==1 ){// for white pawns***************************************************************
// first, unpinned pawns
a=b->piececolorBB[0][0]&(~pinBB);// BB of unpinned pawns
// move forward one square. Only select rank 7 - promotions
a2=((a&0x4040404040404040)<<1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to +9.
a2=(a<<9)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to -7.
a2=(a>>7)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// second, pinned pawns
a=b->piececolorBB[0][0]&pinned_pawns;// BB of pinned pawns
if( a){
// move forward one square. Only select rank 7 - promotions
a2=((a&0x4040404040404040)<<1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit-1==p_cell[j] ){// here pawn moves from bit-1
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit-1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// attack to +9.
a2=(a<<9)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit-9==p_cell[j] ){// here pawn moves from bit-9
pin=1;
break;
}
if( !pin || p_d[j]==9 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit-9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// attack to -7.
a2=(a>>7)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit+7==p_cell[j] ){// here pawn moves from bit+7
pin=1;
break;
}
if( !pin || p_d[j]==7 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit+7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
}
}else{// for black pawns*****************************************************************************
// first, unpinned pawns
a=b->piececolorBB[0][1]&(~pinBB);// BB of unpinned pawns
// move forward one square. Onlt select rank 2 - promotions.
a2=((a&0x0202020202020202)>>1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to +7.
a2=(a<<7)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to -9.
a2=(a>>9)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// second, pinned pawns
a=b->piececolorBB[0][1]&pinned_pawns;// BB of pinned pawns
if( a ){
// move forward one square. Onlt select rank 2 - promotions.
a2=((a&0x0202020202020202)>>1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit+1==p_cell[j] ){// here pawn moves from bit+1
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit+1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// attack to +7.
a2=(a<<7)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit-7==p_cell[j] ){// here pawn moves from bit-7
pin=1;
break;
}
if( !pin || p_d[j]==7 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit-7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// attack to -9.
a2=(a>>9)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit+9==p_cell[j] ){// here pawn moves from bit+9
pin=1;
break;
}
if( !pin || p_d[j]==9 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit+9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
}
}
// get king moves.
a=king_masks[b->kp[player-1]]&allowed_mask;// attacks only
if( a ) {
// eliminate cells attacked by opp pawns
if( (player&2) )
a2=(b->piececolorBB[0][0]<<9) | (b->piececolorBB[0][0]>>7);// white pawns attack
else
a2=(b->piececolorBB[0][1]<<7) | (b->piececolorBB[0][1]>>9);// black pawns attack
a2|=king_masks[b->kp[2-player]];// eliminate cells attacked by opp king
a&=(~a2);
while( a ){
GET_BIT(a)
list[0]=b->kp[player-1];list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
return(unsigned int(move_count));// return move count
}
unsigned int get_all_moves_new_part2(board *b,unsigned char *list,UINT64 pinBB){// get list of all available moves - excluding captures and promotions. Return count. Put moves on the list.
UINT64 move_count=0,mc1,pinned_pawns=0,one=1,a,a2,o=b->colorBB[0]|b->colorBB[1],allowed_mask=~o; // only empties are allowed
unsigned long bit;
unsigned int king_position=b->kp[b->player-1],j,p_c=0,p_cell[8],p_d[8],player=b->player;
unsigned char p_d2[64];// for each cell, direction of allowed moves: 1,7,8,9. 0 if all allowed.
// find all absolutely pinned squares. Don't forget the pinned directions.
// a is mask of all opp sliders on king attack lines, excluding the ones blocked by OPP
a=pinBB;
memset(p_d2,0,64);// init to "all allowed"
while( a ){
GET_BIT(a)
unsigned int dirnorm=dir_norm[bit][king_position];
if( !(b->piece[bit]&6) ){ // pawn only
if( dirnorm!=8 ){ // exclude pawns allowed in direction 8 only - that is not a valid pawn direction!
p_cell[p_c]=bit; // pinned pawn position
p_d[p_c]=dirnorm; // pinned pawn direction
p_c++; // count it
pinned_pawns|=(UINT64(1)<<bit); // mask of pinned pawns
}
}else
p_d2[bit]=dir_trans[dirnorm]; // pinned piece direction2=0,1,2,3,4. By cell. Used for sliders only.
}
// get queen moves. Pin has to be considered.
a=b->piececolorBB[4][player-1];
while( a ){
GET_BIT(a)
mc1=get_queen_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get rook moves. Pin has to be considered.
a=b->piececolorBB[3][player-1];
while( a ){
GET_BIT(a)
mc1=get_rook_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get bishop moves. Pin has to be considered.
a=b->piececolorBB[2][player-1];
while( a ){
GET_BIT(a)
mc1=get_bishop_moves(b,list,player,bit,o,allowed_mask&dir_mask[p_d2[bit]][bit]);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get knight moves. Pin has to be considered.
a=b->piececolorBB[1][player-1]&(~pinBB);// BB of unpinned knights
while( a ){
GET_BIT(a)
mc1=get_knight_moves(b,list,player,bit,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get pawn moves. Pin has to be considered.
if( player==1 ){// for white pawns***************************************************************
// first, unpinned pawns
a=b->piececolorBB[0][0]&(~pinBB);// BB of unpinned pawns
// move forward two squares. Only select pawns on row 2.
a2=((a&0x0202020202020202)<<2)&(~o)&(~(o<<1));// moving to empty squares, through empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-2;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// move forward one square. Exclude rank 7=promotions
a2=((a&0xbfbfbfbfbfbfbfbf)<<1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// second, pinned pawns
a=b->piececolorBB[0][0]&pinned_pawns;// BB of pinned pawns
if( a){
// move forward two squares. Only select pawns on row 2.
a2=((a&0x0202020202020202)<<2)&(~o)&(~(o<<1));// moving to empty squares, through empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit-2==p_cell[j] ){// here pawn moves from bit-2
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit-2;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// move forward one square. Exclude rank 7=promotions
a2=((a&0xbfbfbfbfbfbfbfbf)<<1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit-1==p_cell[j] ){// here pawn moves from bit-1
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit-1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
}
}else{// for black pawns*****************************************************************************
// first, unpinned pawns
a=b->piececolorBB[0][1]&(~pinBB);// BB of unpinned pawns
// move forward two squares. Only select pawns on row 7.
a2=((a&0x4040404040404040)>>2)&(~o)&(~(o>>1));// moving to empty squares, through empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+2;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// move forward one square. Exclude rank 2=promotions
a2=((a&0xfdfdfdfdfdfdfdfd)>>1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// second, pinned pawns
a=b->piececolorBB[0][1]&pinned_pawns;// BB of pinned pawns
if( a ){
// move forward two squares. Only select pawns on row 7.
a2=((a&0x4040404040404040)>>2)&(~o)&(~(o>>1));// moving to empty squares, through empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit+2==p_cell[j] ){// here pawn moves from bit+2
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit+2;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// move forward one square. Exclude rank 2=promotions
a2=((a&0xfdfdfdfdfdfdfdfd)>>1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
// exclude disallowed moves
unsigned int pin=0;
for(j=0;j<p_c;++j)// find pinned data
if( bit+1==p_cell[j] ){// here pawn moves from bit+1
pin=1;
break;
}
if( !pin || p_d[j]==1 ){// allowed. Or not pinned. Record it
list[0]=(unsigned char)bit+1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
}
}
// get king moves. Pin does not get into consideration.
mc1=get_king_moves_l_no_captures(b,list,player,king_position);
move_count+=mc1;
list+=(mc1<<1);// move up the list pointer
return(unsigned int(move_count));// return move count
}
unsigned int get_all_moves(board *b,unsigned char *list){// get list of all available moves. Return count. Put moves on the list.
UINT64 pi;
unsigned int mc=get_all_moves_new_part1(b,list,&pi); // captures generated
mc+=get_all_moves_new_part2(b,&list[2*mc],pi); // non-captures generated
return(mc);// return move count
}
unsigned int get_all_attack_moves(board *b,unsigned char *list){// get list of all attack moves. Return count. Put moves on the list. Order is different: now pinned and unpinned pawn moves are together!
UINT64 move_count=0,mc1,a,a2,o=b->colorBB[0]|b->colorBB[1],allowed_mask=b->colorBB[2-b->player];
unsigned long bit;
unsigned int player=b->player;
// get pawn moves.
if( player==1 ){// for white pawns***************************************************************
a=b->piececolorBB[0][0];// BB of pawns
// count promotions as attack moves
a2=((a&0x4040404040404040)<<1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to +9.
a2=(a<<9)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to -7.
a2=(a>>7)&b->colorBB[1];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}else{// for black pawns*****************************************************************************
a=b->piececolorBB[0][1];// BB of pawns
// count promotions as attack moves
a2=((a&0x0202020202020202)>>1)&(~o);// moving to empty squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+1;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to +7.
a2=(a<<7)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit-7;list[1]=(unsigned char)bit;move_count++;list+=2;
}
// attack to -9.
a2=(a>>9)&b->colorBB[0];// moving to OPP squares
// serialize
while( a2 ){
GET_BIT(a2)
list[0]=(unsigned char)bit+9;list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// get rook moves.
a=b->piececolorBB[3][player-1];
while( a ){
GET_BIT(a)
mc1=get_rook_moves(b,list,player,bit,o,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get bishop moves.
a=b->piececolorBB[2][player-1];
while( a ){
GET_BIT(a)
mc1=get_bishop_moves(b,list,player,bit,o,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get knight moves.
a=b->piececolorBB[1][player-1];
while( a ){
GET_BIT(a)
mc1=get_knight_moves(b,list,player,bit,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get king moves.
a=king_masks[b->kp[player-1]]&allowed_mask;
if( a ) {
// eliminate cells attacked by opp pawns
if( (player&2) )
a2=(b->piececolorBB[0][0]<<9) | (b->piececolorBB[0][0]>>7);// white pawns attack
else
a2=(b->piececolorBB[0][1]<<7) | (b->piececolorBB[0][1]>>9);// black pawns attack
a2|=king_masks[b->kp[2-player]];// eliminate cells attacked by opp king
a&=(~a2);// keep opponent - those are attacks
while( a ){
GET_BIT(a)
list[0]=b->kp[player-1];list[1]=(unsigned char)bit;move_count++;list+=2;
}
}
// get all en passant captures. Check them for pin, since they are so wierd.
if( b->last_move!=INVALID_LAST_MOVE ){// valid last move. This guarantees that at least 1 opp pawn is in correct place. They can only be disallowed by pins.
mc1=get_ep_moves_l(b,list,player);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
// get queen moves.
a=b->piececolorBB[4][player-1];
while( a ){
GET_BIT(a)
mc1=get_queen_moves(b,list,player,bit,o,allowed_mask);
move_count+=mc1;// count
list+=(mc1<<1);// move up the list pointer
}
return(unsigned int(move_count));// return move count
}
static unsigned int get_all_nonking_moves_to_cell(board *b,unsigned char *list,unsigned int cell,unsigned int capture){
UINT64 bb;
unsigned long bit;
unsigned int mc=0,player0=b->player-1;
// pawns
if( !capture ){// only get non-capture pawn moves. Include ep captures.
if( player0==0 ){// white
if( (cell&7)==3 && b->piece[cell-1]==0 && b->piece[cell-2]==64+1 ){// move forward 2 cells, only if both empty and first move
list[0]=cell-2;list[1]=cell;mc++;list+=2;
}else if( cell && b->piece[cell-1]==64+1 ){// move forward, only if empty and not last cell
list[0]=cell-1;list[1]=cell;mc++;list+=2;
}
}else{// black
if( (cell&7)==4 && b->piece[cell+1]==0 && b->piece[cell+2]==128+1 ){// move forward 2 cells, only if both empty and first move
list[0]=cell+2;list[1]=cell;mc++;list+=2;
}else if( cell<63 && b->piece[cell+1]==128+1 ){// move forward, only if empty and not last cell
list[0]=cell+1;list[1]=cell;mc++;list+=2;
}
}
}else{// only get capture pawn moves
if( player0==1 ){// black moves
if( cell>=7 && b->piece[cell-7]==1+128 ){//move -7
list[0]=cell-7;list[1]=cell;mc++;list+=2;
}
if( cell<=54 && b->piece[cell+9]==1+128 ){//move +9
list[0]=cell+9;list[1]=cell;mc++;list+=2;
}
}else{// white moves
if( cell>=9 && b->piece[cell-9]==1+64 ){//move -9
list[0]=cell-9;list[1]=cell;mc++;list+=2;
}
if( cell<=56 && b->piece[cell+7]==1+64 ){//move +7
list[0]=cell+7;list[1]=cell;mc++;list+=2;
}
}
}
// knight
bb=knight_masks[cell]&b->piececolorBB[1][player0];// select player's knights
while( bb ){
GET_BIT(bb)
list[0]=(unsigned char)bit;list[1]=cell;mc++;list+=2;
}
// use magics to look at all directions and see if they terminate at appropriate opp sliding piece - non-looping solution.
bb=(b->piececolorBB[2][player0]|b->piececolorBB[4][player0])&attacks_bb_B(cell,b->colorBB[0]|b->colorBB[1]);// bishop (and queen) attacks
bb|=(b->piececolorBB[3][player0]|b->piececolorBB[4][player0])&attacks_bb_R(cell,b->colorBB[0]|b->colorBB[1]);// rook (and queen) attacks
while( bb ){
GET_BIT(bb)
list[0]=(unsigned char)bit;list[1]=cell;mc++;list+=2;
}
return(mc);
}
unsigned int get_out_of_check_moves(board *b,unsigned char *list,unsigned int k_cell,unsigned int a_cell){// get list of moves that get king out of check
UINT64 one=1,attack_mask,p_a;
unsigned int i,mc=0,mc2,double_check,attacker;
unsigned char list2[256];
// 0. see if this is a double check.
i=(b->piece[a_cell]&7)-1;
b->piececolorBB[i][2-b->player]^=one<<a_cell; // reset bitboard for attackin OPP
double_check=cell_under_attack(b,k_cell,b->player); // see if i am still under attack
b->piececolorBB[i][2-b->player]^=one<<a_cell; // restore bitboard for attackin OPP