-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.c
2462 lines (1954 loc) · 67.3 KB
/
engine.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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#ifdef SSE
#include <nmmintrin.h>
#endif
// #define WASM
#include "psv/psv.h"
typedef enum { false, true } bool;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef unsigned char u8;
typedef long long i64;
typedef int i32;
typedef char i8;
typedef u64 Board;
typedef u8 Position;
#define ENGINE_NAME "some_engine"
#define ENGINE_AUTHOR "jasper"
Position pos_from_bitboard(Board b);
typedef enum {
S_WHITE,
S_BLACK,
} SideId;
typedef struct {
Board piece[6];
Board all_pieces;
Board attacks;
Board pinned_pieces;
Board pins_attack_paths[64];
bool in_check;
Board check_attack_path;
bool can_castle_k;
bool can_castle_q;
bool lost_k_castle_rights;
bool lost_q_castle_rights;
bool did_castle;
} Side;
typedef enum {
R_PLAYING,
R_WHITE_WON,
R_BLACK_WON,
R_DRAW,
} GameResult;
typedef struct {
SideId turn;
Side side[2];
Board en_passant;
Board all_pieces;
GameResult game_result;
u64 seen_position_hashes[1000];
int seen_position_hashes_count;
} GameState;
void update_game_state_info(GameState* gs);
typedef enum {
P_PAWN,
P_KNIGHT,
P_BISHOP,
P_ROOK,
P_QUEEN,
P_KING,
} Piece;
static int PIECE_VALUES[6] = {
1, // pawn
3, // knight
3, // bishop
5, // rook
9, // queen
0, // king
};
typedef enum {
PROMO_NONE,
PROMO_QUEEN,
PROMO_ROOK,
PROMO_BISHOP,
PROMO_KNIGHT,
} Promotion;
typedef enum {
CASTLE_NONE,
CASTLE_Q,
CASTLE_K,
} Castle;
typedef union {
u64 raw;
struct {
u8 piece;
Position from;
Position to;
u8 promotion;
u8 is_en_passant;
u8 castle;
} data;
} Move;
typedef struct {
Move* moves;
int length;
} MoveList;
typedef struct {
Move move;
float eval;
} EvaluatedMove;
typedef struct {
int x;
int y;
} PosOffset;
const Board W_CASTLE_MASK_K = 0x60;
const Board W_CASTLE_MASK_Q = 0xe;
const Board B_CASTLE_MASK_K = 0x6000000000000000;
const Board B_CASTLE_MASK_Q = 0xe00000000000000;
Board _side_pieces(SideId side, GameState* gs) {
Board b = 0;
for (int i = 0; i < 6; i++) {
b |= gs->side[side].piece[i];
}
return b;
}
u64 hash_position(GameState* gs) {
u64 hash = 0xc0ffeeULL;
for (u64 i = 0UL; i < 6UL; i++) {
hash ^= gs->side[S_WHITE].piece[i];
hash += i;
hash ^= gs->side[S_BLACK].piece[i];
hash += i;
}
hash += gs->turn;
hash ^= gs->en_passant * 0xdecafbadUL;
hash ^= gs->side[S_WHITE].lost_k_castle_rights * 222UL;
hash ^= gs->side[S_WHITE].lost_q_castle_rights * 444UL;
hash ^= gs->side[S_BLACK].lost_k_castle_rights * 666UL;
hash ^= gs->side[S_BLACK].lost_q_castle_rights * 777UL;
return hash;
}
void print_bitboard(Board board) {
for (int row = 7; row >= 0; row--) {
for (int col = 0; col < 8; col++) {
printf("%llu", ((board >> (row * 8 + col)) & 1UL));
if (col != 7) {
printf(" ");
}
}
printf("\n");
}
}
void write_bitboard_to_text_buf(char* buf[64], Board board, char* icon) {
for (int row = 7; row >= 0; row--) {
for (int col = 0; col < 8; col++) {
if ((board >> (row * 8 + col)) & 1UL) {
int idx = (7 - row) * 8 + col;
assert(buf[idx] == 0);
buf[idx] = icon;
}
}
}
}
int row_from_pos(Position pos) {
return pos / 8;
}
int col_from_pos(Position pos) {
return pos % 8;
}
Position pos_from_row_and_col(int row, int col) {
return row * 8 + col;
}
Board pos_to_bitboard(Position pos) {
return 1ULL << pos;
}
Position notation_to_pos(char* str) {
assert(((str[0] >= 'a' && str[0] <= 'h')
|| (str[0] >= 'A' && str[0] <= 'H'))
&& str[1] >= '1' && str[1] <= '8');
bool capital = str[0] >= 'A' || str[0] <= 'H';
int col = str[0] - (capital ? 'a' : 'A');
int row = str[1] - '1';
return pos_from_row_and_col(row, col);
}
bool position_is_legal(Position pos) {
return pos >= 0 && pos < 64;
}
void pos_to_notation(Position pos, char* buf) {
assert(position_is_legal(pos));
int row = row_from_pos(pos);
int col = col_from_pos(pos);
buf[0] = col + 'a';
buf[1] = row + '1';
buf[2] = 0;
}
char promotion_to_notation(Promotion promo) {
switch (promo) {
case PROMO_BISHOP:
return 'b';
case PROMO_KNIGHT:
return 'n';
case PROMO_ROOK:
return 'r';
case PROMO_QUEEN:
return 'q';
default:
assert(false);
}
}
void move_to_notation(Move move, char* buf, SideId side) {
// if (move.data.castle != CASTLE_NONE) {
// if (move.data.castle == CASTLE_K) {
// (void)memcpy(buf, "O-O", 4);
// } else if (move.data.castle == CASTLE_Q) {
// (void)memcpy(buf, "O-O-O", 6);
// }
// return;
// }
if (move.data.castle != CASTLE_NONE) {
char* castle;
if (move.data.castle == CASTLE_K) {
castle = side == S_WHITE ? "e1g1" : "e8g8";
} else if (move.data.castle == CASTLE_Q) {
castle = side == S_WHITE ? "e1c1" : "e8c8";
}
(void)memcpy(buf, castle, 5);
return;
}
(void)pos_to_notation(move.data.from, buf);
(void)pos_to_notation(move.data.to, buf + 2);
int len = 4;
if (move.data.promotion != PROMO_NONE) {
assert(move.data.piece == P_PAWN);
buf[len++] = promotion_to_notation(move.data.promotion);
}
buf[len] = 0;
}
Piece piece_at(Position pos, GameState* gs) {
Board b = pos_to_bitboard(pos);
for (int i = 0; i < 6; i++) {
if (((gs->side[S_WHITE].piece[i] & b)
| (gs->side[S_BLACK].piece[i] & b)) != 0) {
return i;
}
}
assert(false);
}
Move notation_to_move(char* buf, GameState* gs) {
Move move = { 0 };
// if (strcmp(buf, "O-O-O") == 0) {
// move.data.castle = CASTLE_Q;
// return move;
// }
// if (strcmp(buf, "O-O") == 0) {
// move.data.castle = CASTLE_K;
// return move;
// }
move.data.from = notation_to_pos(buf);
move.data.to = notation_to_pos(buf + 2);
if (buf[4] != 0) {
switch (buf[4]) {
case 'b':
move.data.promotion = PROMO_BISHOP;
break;
case 'n':
move.data.promotion = PROMO_KNIGHT;
break;
case 'r':
move.data.promotion = PROMO_ROOK;
break;
case 'q':
move.data.promotion = PROMO_QUEEN;
break;
default:
assert(false);
}
}
move.data.piece = piece_at(move.data.from, gs);
if (move.data.piece == P_KING) {
if (move.data.from == notation_to_pos("e1")) {
if (move.data.to == notation_to_pos("g1")) {
move.data.castle = CASTLE_K;
} else if (move.data.to == notation_to_pos("c1")) {
move.data.castle = CASTLE_Q;
}
} else if (move.data.from == notation_to_pos("e8")) {
if (move.data.to == notation_to_pos("g8")) {
move.data.castle = CASTLE_K;
} else if (move.data.to == notation_to_pos("c8")) {
move.data.castle = CASTLE_Q;
}
}
}
return move;
}
typedef struct {
int length;
Position* positions;
} PositionList;
#define sq(s) pos_to_bitboard(notation_to_pos(s))
void position_offset(Position pos, int row, int col, PositionList* out) {
int row_ = (int)row_from_pos(pos);
int col_ = (int)col_from_pos(pos);
row_ += row;
col_ += col;
if (row_ < 0 || col_ < 0 || row_ > 7 || col_ > 7) {
return;
}
out->positions[out->length++] = pos_from_row_and_col(row_, col_);
}
Board position_list_to_bitboard(PositionList* list) {
Board b = 0;
for (int i = 0; i < list->length; i++) {
b |= pos_to_bitboard(list->positions[i]);
}
return b;
}
int set_bits_count(Board value) {
#ifdef SSE
return _mm_popcnt_u64(value);
#else
int count = 0;
while (value != 0) {
count += value & 1;
value >>= 1;
}
return count;
#endif
}
int bitscan_reverse(Board value) {
return __builtin_ctzll(value);
}
void bitboard_to_position_list(Board b, PositionList* out) {
int pos_count = set_bits_count(b);
while (b != 0) {
int pos = bitscan_reverse(b);
out->positions[out->length++] = pos;
b &= ~(1ULL << pos);
}
}
typedef struct {
Board all_moves;
Board en_passant_moves;
} PawnMoves;
#define CHECK_PINS(attack_fn) Board __pos_mask = pos_to_bitboard(pos); \
Board __pinned_mask = gs->side[gs->turn].pinned_pieces; \
if ((__pos_mask & __pinned_mask) != 0) { \
Board __pin_attack_path = gs->side[gs->turn].pins_attack_paths[pos]; \
Board __own_pieces = gs->side[gs->turn].all_pieces; \
return (attack_fn(pos, gs) & __pin_attack_path) & ~__own_pieces; \
}
void assert_piece_exists(Piece p, Position pos, GameState* gs) {
assert(position_is_legal(pos));
assert((gs->side[gs->turn].piece[p] & pos_to_bitboard(pos)) != 0);
}
Board pawn_attacks(Position pos, GameState* gs) {
// NOTE: This does not include en passant. As the attacks bitboard
// is mainly used to detect checks it was not needed.
Position moves[2] = { 0 };
PositionList moves_list = { 0 };
moves_list.positions = moves;
int y_dir = gs->turn == S_WHITE ? 1 : -1;
position_offset(pos, y_dir, -1, &moves_list);
position_offset(pos, y_dir, 1, &moves_list);
return position_list_to_bitboard(&moves_list);
}
PawnMoves pawn_legal_moves(Position pos, GameState* gs) {
Board moves = 0;
Board en_passant_moves = 0;
SideId side_id = gs->turn;
SideId op_side = !side_id;
Board board_all = gs->all_pieces;
Board board_op = gs->side[op_side].all_pieces;
Board attacks = board_op & pawn_attacks(pos, gs);
Board pawn_mask = pos_to_bitboard(pos);
Board pinned_mask = gs->side[gs->turn].pinned_pieces;
if ((pawn_mask & pinned_mask) != 0) {
PawnMoves pm = { 0 };
pm.all_moves = attacks & gs->side[gs->turn].pins_attack_paths[pos];
return pm;
}
assert(gs->side[side_id].piece[P_PAWN] & pawn_mask);
moves |= attacks;
Position advances[2] = { 0 };
PositionList advances_list = { 0 };
advances_list.positions = advances;
int y_dir = gs->turn == S_WHITE ? 1 : -1;
(void)position_offset(pos, y_dir, 0, &advances_list);
int row = row_from_pos(pos);
if (row == (side_id == S_WHITE ? 1 : 6)) {
assert(advances_list.length == 1);
Board advance_mask = pos_to_bitboard(advances_list.positions[0]);
if ((advance_mask & gs->all_pieces) == 0) {
(void)position_offset(pos, y_dir * 2, 0, &advances_list);
}
}
moves |= ~board_all & position_list_to_bitboard(&advances_list);
Position en_passant_targets[2] = { 0 };
PositionList en_passant_targets_list = { 0 };
en_passant_targets_list.positions = en_passant_targets;
(void)position_offset(pos, 0, -1, &en_passant_targets_list);
(void)position_offset(pos, 0, 1, &en_passant_targets_list);
Board en_passants = gs->en_passant & board_op
& position_list_to_bitboard(&en_passant_targets_list);
if (en_passants != 0) {
PositionList ml = { 0 };
Position move_list[64] = { 0 };
ml.positions = move_list;
bitboard_to_position_list(en_passants, &ml);
for (int i = 0; i < ml.length; i++) {
Position p = ml.positions[i];
Position en_passant[1] = { 0 };
PositionList en_passant_list = { 0 };
en_passant_list.positions = en_passant;
(void)position_offset(p, y_dir, 0, &en_passant_list);
if (en_passant_list.length > 0) {
moves |= pos_to_bitboard(en_passant_list.positions[0]);
en_passant_moves |= pos_to_bitboard(en_passant_list.positions[0]);
}
}
}
if (gs->side[gs->turn].in_check) {
moves &= gs->side[gs->turn].check_attack_path;
}
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
moves &= ~gs->side[gs->turn].all_pieces;
PawnMoves pm = { 0 };
pm.all_moves = moves;
pm.en_passant_moves = en_passant_moves;
return pm;
}
static Board COLS[8];
static Board ROWS[8];
static Board KNIGHT_MOVES_LUT[64];
static Board KING_MOVES_LUT[64];
static Board BISHOP_MOVES_LUT[64];
static Board ROOK_MOVES_LUT[64];
#define ROOK_PERMS 8 * 4096
#define ROOK_SHIFT 15
#define BISHOP_PERMS 8 * 4096
#define BISHOP_SHIFT 15
static Board BISHOP_MAGIC_BOARDS[64][BISHOP_PERMS] = { 0 }; // 256 K
static Board ROOK_MAGIC_BOARDS[64][ROOK_PERMS] = { 0 }; // 2048 K
static u64 BISHOP_MAGIC_NUMBERS[64] = {
2658911450287794421,
6852417887120316938,
1159619772494454267,
4530306807827729113,
4184186518776449728,
6996729517409051523,
6694277110281440227,
4367046700827347423,
5002521922874173148,
707789975046747826,
8845803124310741094,
3053780606037994088,
782875984623577188,
1340588251091286675,
5117580335483048948,
8042612948621996202,
2783607372402123321,
1442455459770969771,
5692530968041345214,
4649229338657423104,
2305795996909174787,
4611698444337271150,
4629462408027598048,
8622995163449815274,
6456489114168196145,
510080667540968688,
4323454183829978828,
68334489678698,
3558652225865219584,
5706513065946585703,
1786281091850907230,
8201950827265312549,
694435915564839760,
840916925791039369,
1115610479759747424,
6880091598734649088,
9161611439159255552,
5414701498144979799,
3359132947749940723,
2167603366743129587,
2022505955381231761,
5998988709168406901,
3084769873457330128,
7562712105529095721,
2634012166121824780,
8481960309935617736,
1295637020739438321,
7389596876633812746,
4662164641528356395,
4274287365172863505,
2646470775556534080,
825937101719960739,
3176643519900238103,
9026745600027081100,
2125726254840799092,
7498071283202784097,
3289870684522899707,
5005907598103146988,
7827854030464587121,
2189585998322548473,
8686252344764419816,
2440490461859161454,
152347237491388123,
8496541520937575975,
};
static u64 ROOK_MAGIC_NUMBERS[64] = {
351979748819113472,
7803309538379299328,
1736271716617851776,
8109988271961577632,
432345414004662826,
6673893786975006608,
4474708985453114624,
8141247327844093598,
7425962628245966592,
1642867420846126720,
2759411911526057344,
3357996469423967328,
4521686761654498272,
5079380718783458248,
8005694169337838520,
5553027955233797434,
4089864033709443328,
5160404229161359616,
4389565480264011648,
6154169681462285184,
6751583036203832889,
7784001533825787536,
7611136035526543800,
4887443686719849878,
6633336694756633856,
3595239369322866432,
7044805029705048064,
2281069068459920160,
4379372414207810190,
3273077888100893176,
6836300768995319696,
6074204129578170796,
6511605973395510272,
188959452880595840,
9223320189913268096,
5470114901861073408,
174381527388389152,
2842330991730432984,
5162016821703487056,
6755246923111856388,
3917853114319556096,
5621910873506324480,
7574010561759584256,
7020725938020357888,
8045912710459918336,
1679284573738097840,
8085835441630863108,
1723801058847070596,
1644622125664786944,
5076042003988832512,
6561642211069593856,
4907242536322792960,
1066234342103656960,
5723721981445960192,
7032640680205726208,
5945373557256219552,
4643755003622426662,
4725706427850877098,
1489248111780252278,
2766412166700150694,
8690697145264131078,
4008559275060228042,
191737606960205964,
6096466831532416022,
};
#define add_move(x, y) position_offset(pos, x, y, &move_list)
void generate_knight_LUT() {
for (Position pos = 0; pos < 64; pos++) {
Position moves[8] = { 0 };
PositionList moves_list = { 0 };
moves_list.positions = moves;
position_offset(pos, 2, 1, &moves_list);
position_offset(pos, 2, -1, &moves_list);
position_offset(pos, -2, 1, &moves_list);
position_offset(pos, -2, -1, &moves_list);
position_offset(pos, 1, 2, &moves_list);
position_offset(pos, 1, -2, &moves_list);
position_offset(pos, -1, 2, &moves_list);
position_offset(pos, -1, -2, &moves_list);
KNIGHT_MOVES_LUT[pos] = position_list_to_bitboard(&moves_list);
}
}
void generate_king_LUT() {
for (Position pos = 0; pos < 64; pos++) {
Position moves[8] = { 0 };
PositionList move_list = { 0 };
move_list.positions = moves;
add_move(1, 1);
add_move(0, 1);
add_move(-1, 1);
add_move(-1, 0);
add_move(-1, -1);
add_move(1, -1);
add_move(0, -1);
add_move(1, 0);
KING_MOVES_LUT[pos] = position_list_to_bitboard(&move_list);
}
}
void generate_rook_LUT() {
for (Position pos = 0; pos < 64; pos++) {
int row = row_from_pos(pos);
int col = col_from_pos(pos);
ROOK_MOVES_LUT[pos] = (ROWS[row] | COLS[col]) & ~pos_to_bitboard(pos);
}
}
void generate_bishop_attack_bitboard(Position pos) {
BISHOP_MOVES_LUT[pos] = 0;
const PosOffset offsets[4] = {
{ 1, 1 },
{ -1, 1 },
{ 1, -1 },
{ -1, -1 },
};
int row = row_from_pos(pos);
int col = col_from_pos(pos);
for (int dir = 0; dir < 4; dir++) {
for (int dist = 1; dist < 8; dist++) {
int row_ = row + offsets[dir].y * dist;
int col_ = col + offsets[dir].x * dist;
if (row_ < 0 || row_ > 7 || col_ < 0 || col_ > 7) {
break;
}
Position pos_ = pos_from_row_and_col(row_, col_);
BISHOP_MOVES_LUT[pos] |= pos_to_bitboard(pos_);
}
}
}
void generate_bishop_LUT() {
for (Position pos = 0; pos < 64; pos++) {
generate_bishop_attack_bitboard(pos);
assert(BISHOP_MOVES_LUT[pos] != 0);
}
}
Board knight_attacks(Position pos, GameState* gs) {
assert_piece_exists(P_KNIGHT, pos, gs);
return KNIGHT_MOVES_LUT[pos];
}
Board knight_legal_moves(Position pos, GameState* gs) {
assert_piece_exists(P_KNIGHT, pos, gs);
CHECK_PINS(knight_attacks)
Board moves = knight_attacks(pos, gs);
moves &= ~gs->side[gs->turn].all_pieces;
if (gs->side[gs->turn].in_check) {
moves &= gs->side[gs->turn].check_attack_path;
}
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
return moves;
}
Board king_attacks(Position pos, GameState* gs) {
assert_piece_exists(P_KING, pos, gs);
Board moves = KING_MOVES_LUT[pos];
moves &= ~gs->side[!gs->turn].attacks;
// HACK: The king's attacked squares weren't in the attack bitboard.
// Probably should be addressed properly.
Position op_king_pos = pos_from_bitboard(gs->side[!gs->turn].piece[P_KING]);
moves &= ~KING_MOVES_LUT[op_king_pos];
return moves;
}
Board sliding_piece_attacks(Position pos, Board occupancies,
const PosOffset* offsets, int offset_dirs_count) {
Position moves[128] = { 0 };
PositionList move_list = { 0 };
move_list.positions = moves;
int pre_len = move_list.length - 1;
int x, y;
for (int dir = 0; dir < offset_dirs_count; dir++) {
x = offsets[dir].x;
y = offsets[dir].y;
for (int i = 1; true; i++) {
position_offset(pos, i * x, i * y, &move_list);
if (move_list.length == pre_len) {
break;
}
pre_len = move_list.length;
Position p = move_list.positions[move_list.length - 1];
if (occupancies & pos_to_bitboard(p)) {
break;
}
}
}
return position_list_to_bitboard(&move_list);
}
Board sliding_piece_attacks_from_gs(Position pos, GameState* gs,
const PosOffset* offsets, int offset_dirs_count) {
Board occupancies = gs->all_pieces &
~gs->side[!gs->turn].piece[P_KING];
return sliding_piece_attacks(pos, occupancies, offsets, offset_dirs_count);
}
Board king_legal_moves(Position pos, GameState* gs) {
assert_piece_exists(P_KING, pos, gs);
Board moves = king_attacks(pos, gs);
moves &= ~gs->side[gs->turn].all_pieces;
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
return moves;
}
static PosOffset ROOK_OFFSETS[4] = {
{ 1, 0 },
{ -1, 0 },
{ 0, 1 },
{ 0, -1 },
};
const PosOffset BISHOP_OFFSETS[4] = {
{ 1, 1 },
{ -1, 1 },
{ 1, -1 },
{ -1, -1 },
};
u64 magic_board_hash(Board occupancies, u64 moves_mask, u64 magic, int shift) {
occupancies &= moves_mask;
occupancies *= magic;
occupancies >>= 64 - shift;
return occupancies;
}
Board magic_rook_attacks(Position pos, GameState* gs) {
Board occupancies = gs->all_pieces &
~gs->side[!gs->turn].piece[P_KING];
u64 hash = magic_board_hash(occupancies, ROOK_MOVES_LUT[pos],
ROOK_MAGIC_NUMBERS[pos], ROOK_SHIFT);
assert(hash < ROOK_PERMS);
return ROOK_MAGIC_BOARDS[pos][hash];
}
Board rook_attacks(Position pos, GameState* gs) {
// return sliding_piece_attacks_from_gs(pos, gs, ROOK_OFFSETS, 4);
return magic_rook_attacks(pos, gs);
}
Board rook_legal_moves(Position pos, GameState* gs) {
assert_piece_exists(P_ROOK, pos, gs);
CHECK_PINS(rook_attacks)
Board moves = rook_attacks(pos, gs);
moves &= ~gs->side[gs->turn].all_pieces;
if (gs->side[gs->turn].in_check) {
moves &= gs->side[gs->turn].check_attack_path;
}
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
return moves;
}
Board magic_bishop_attacks(Position pos, GameState* gs) {
// King is excluded because it is impossible to block
// with the king and otherwise the king can just move
// backwards and be out of check as this attack path
// will go up to the king and stop if he is not excluded.
Board occupancies = gs->all_pieces &
~gs->side[!gs->turn].piece[P_KING];
u64 hash = magic_board_hash(occupancies, BISHOP_MOVES_LUT[pos],
BISHOP_MAGIC_NUMBERS[pos], BISHOP_SHIFT);
assert(hash < BISHOP_PERMS);
return BISHOP_MAGIC_BOARDS[pos][hash];
}
Board bishop_attacks(Position pos, GameState* gs) {
// return sliding_piece_attacks_from_gs(pos, gs, BISHOP_OFFSETS, 4);
return magic_bishop_attacks(pos, gs);
}
Board bishop_legal_moves(Position pos, GameState* gs) {
assert_piece_exists(P_BISHOP, pos, gs);
CHECK_PINS(bishop_attacks)
Board moves = bishop_attacks(pos, gs);
moves &= ~gs->side[gs->turn].all_pieces;
if (gs->side[gs->turn].in_check) {
moves &= gs->side[gs->turn].check_attack_path;
}
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
return moves;
}
Board queen_attacks(Position pos, GameState* gs) {
assert_piece_exists(P_QUEEN, pos, gs);
return bishop_attacks(pos, gs) | rook_attacks(pos, gs);
}
Board queen_legal_moves(Position pos, GameState* gs) {
assert_piece_exists(P_QUEEN, pos, gs);
CHECK_PINS(queen_attacks)
Board moves = queen_attacks(pos, gs);
moves &= ~gs->side[gs->turn].all_pieces;
if (gs->side[gs->turn].in_check) {
moves &= gs->side[gs->turn].check_attack_path;
}
moves &= ~(gs->side[S_WHITE].piece[P_KING] | gs->side[S_BLACK].piece[P_KING]);
return moves;
}
// HACK: Thank you en passant... I hate you.
static Board EN_PASSANT_MASK_LAST_GENERATED = 0;
Board legal_moves_of_piece(GameState* gs, Piece piece, Position pos) {
switch (piece) {
case P_PAWN: {
PawnMoves pm = pawn_legal_moves(pos, gs);
EN_PASSANT_MASK_LAST_GENERATED = pm.en_passant_moves;
return pm.all_moves;
}
case P_KNIGHT: return knight_legal_moves(pos, gs);
case P_BISHOP: return bishop_legal_moves(pos, gs);
case P_ROOK: return rook_legal_moves(pos, gs);
case P_QUEEN: return queen_legal_moves(pos, gs);
case P_KING: return king_legal_moves(pos, gs);
default: assert(false);
}
}
bool move_is_legal(GameState* gs, Move move) {
if (move.data.castle != CASTLE_NONE) {
if (move.data.castle == CASTLE_Q) {
return gs->side[gs->turn].can_castle_q;