-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.c
More file actions
2241 lines (1855 loc) · 55.1 KB
/
Copy patheval.c
File metadata and controls
2241 lines (1855 loc) · 55.1 KB
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
/* the evaluation functions */
#include "includes.h"
#include "knightcap.h"
#include "small_coeffs.h"
#define EVAL_ALL_DEPTH 4
#define EVAL_SHORTCUT_THRESHOLD (2.0*PAWN_VALUE)
#define EVAL_SHORTCUT_OFFSET (1.5*PAWN_VALUE)
/* coefficients we don't want changed by TD(lambda) (multiplicative factors,
the value of a pawn, and the value of a draw */
int dont_change[] = {0, 1, 34, 37, 549, 550, 551, 556, 557, -1};
extern int learning;
extern int mulling;
extern struct state *state;
int debug;
/* this is the value of pawns on open files as they move up the board
- it is doubled for passed pawns */
static CONST_EVAL etype *pawn_advance = &coefficients[IPAWN_ADVANCE];
/* value of pawn configurations around the king */
static CONST_EVAL etype *pawn_defence = &coefficients[IPAWN_DEFENCE];
static unsigned char black_pawn_loc[8];
static unsigned char white_pawn_loc[8];
#define PAWN_LOC(player) (player>0?white_pawn_loc:black_pawn_loc)
int pop_count[256];
static CONST_EVAL etype *pawn_pos_value = &coefficients[IPAWN_POS];
#if USE_NULLMOVE_FIXUP
/* this corrects the unstoppable pawn evaluation on null moves */
void update_pawns(Position *b)
{
if (whites_move(b)) {
b->eval_result += (pop_count32(b->null_stoppable_pawn & BPAWN_MASK) +
pop_count32(b->null_unstoppable_pawn & WPAWN_MASK))*
UNSTOPPABLE_PAWN;
} else {
b->eval_result -= (pop_count32(b->null_stoppable_pawn & WPAWN_MASK) +
pop_count32(b->null_unstoppable_pawn & BPAWN_MASK))*
UNSTOPPABLE_PAWN;
}
}
#endif
/* this evaluates a pawn - its more valuable if its a passed
pawn or on an open file. In those cases it gains value
as it moves up the board */
static etype eval_white_pawn(Position *b, int pi)
{
PieceStruct *piece = b->pieces + pi;
etype ret=0;
int x = XPOS(piece->pos);
int y = YPOS(piece->pos);
etype advance = 0;
int weak = 0;
ret = pawn_pos_value[piece->pos];
/* a pawn is weak if it isn't supported by a pawn and can't
move to a place where it is supported by a pawn */
if (b->topieces[piece->pos] & WPAWN_MASK)
goto not_weak;
if ((b->topieces[piece->pos+NORTH] & WPAWN_MASK) &&
abs(b->board[piece->pos+NORTH]) != PAWN)
goto not_weak;
if (y == 1 && !(b->topieces[piece->pos+NORTH] & BPAWN_MASK) &&
(b->topieces[piece->pos+2*NORTH] & WPAWN_MASK) &&
(abs(b->board[piece->pos+NORTH]) != PAWN) &&
(abs(b->board[piece->pos+2*NORTH]) != PAWN))
goto not_weak;
if (debug)
lprintf(0,"weak %s\n", posstr(piece->pos));
weak = 1;
ret -= WEAK_PAWN;
/* if its on a half open file then its even weaker */
if (black_pawn_loc[x] == 0)
ret -= MEGA_WEAK_PAWN;
/* attacks on weak pawns are worth something */
if (((b->topieces[piece->pos] & ~b->pinned_mask)>>16) & 0xFF)
ret -= pop_count[((b->topieces[piece->pos] &
~b->pinned_mask)>>16) & 0xFF] *
WEAK_PAWN_ATTACK_VALUE;
not_weak:
if (black_pawn_loc[x] < y) {
int n = 7-y;
if (!weak) {
advance = shiftr2(pawn_advance[n]);
}
if ((x==0 || black_pawn_loc[x-1] <= y) &&
(x==7 || black_pawn_loc[x+1] <= y)) {
Square wkpos = WHITEPIECES(b)[IKING].pos;
Square bkpos = BLACKPIECES(b)[IKING].pos;
b->wpassed_pawn_mask |= (1<<pi);
advance += pawn_advance[n];
if (YPOS(bkpos) >= y && abs(XPOS(bkpos) - x) <= 1)
advance = shiftr1(advance);
if (YPOS(wkpos) >= y && abs(XPOS(wkpos) - x) <= 1)
advance += KING_PASSED_PAWN_SUPPORT;
if (b->board[piece->pos+NORTH] < 0)
advance -= BLOCKED_PASSED_PAWN;
if (x != 0 && white_pawn_loc[x-1] &&
abs(y - white_pawn_loc[x-1]) <= 1) {
advance += shiftr1(pawn_advance[n]);
}
if (x != 7 && white_pawn_loc[x+1] &&
abs(y - white_pawn_loc[x+1]) <= 1) {
advance += shiftr1(pawn_advance[n]);
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & BKROOK_MASK) &&
XPOS(BLACKPIECES(b)[IKROOK].pos) == x &&
YPOS(BLACKPIECES(b)[IKROOK].pos) < y) {
/* there is a black king rook
behind our passed pawn */
advance -= PASSED_PAWN_ROOK_ATTACK;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & BQROOK_MASK) &&
XPOS(BLACKPIECES(b)[IQROOK].pos) == x &&
YPOS(BLACKPIECES(b)[IQROOK].pos) < y) {
/* there is a black queen rook
behind our passed pawn */
advance -= PASSED_PAWN_ROOK_ATTACK;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & WKROOK_MASK) &&
XPOS(WHITEPIECES(b)[IKROOK].pos) == x &&
YPOS(WHITEPIECES(b)[IKROOK].pos) < y) {
/* there is a white king rook
behind our passed pawn */
advance += PASSED_PAWN_ROOK_SUPPORT;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & WQROOK_MASK) &&
XPOS(WHITEPIECES(b)[IQROOK].pos) == x &&
YPOS(WHITEPIECES(b)[IQROOK].pos) < y) {
/* there is a white queen rook
behind our passed pawn */
advance += PASSED_PAWN_ROOK_SUPPORT;
}
/* a special case - is it unstoppable? */
if ((b->piece_mask & BLACK_MASK) == BKING_MASK) {
int kpos = BLACKPIECES(b)[IKING].pos;
int kmoves = imax(abs(XPOS(kpos) - x), 7-YPOS(kpos));
int pmoves = 7-y;
if (blacks_move(b)) kmoves--;
if (kmoves > pmoves) {
/* its a candidate for unstopppability, but we need to
count all pieces that have to move out of the way,
except those on the queening square. If we find a pawn
in front then we assume the current pawn is stoppable */
Square pos = b->pieces[pi].pos;
while (!off_board(pos+NORTH,NORTH)) {
pos += NORTH;
if (b->board[pos]) {
if (b->board[pos] == PAWN)
pmoves += 10;
else
++pmoves;
if (pmoves >= kmoves)
break;
}
}
if (kmoves > pmoves) {
ret += UNSTOPPABLE_PAWN;
if (debug)
lprintf(0,"unstoppable %s\n",
posstr(piece->pos));
/* would it become stoppable after a null move? */
if (whites_move(b) && kmoves == pmoves+1) {
b->null_stoppable_pawn |= (1<<pi);
if (debug)
lprintf(0,"null stoppable %s\n",
posstr(piece->pos));
}
/* would it become unstoppable after a null move? */
} else if (blacks_move(b) && kmoves == pmoves) {
b->null_unstoppable_pawn |= (1<<pi);
if (debug)
lprintf(0,"null unstoppable %s\n",
posstr(piece->pos));
}
}
}
if (n < 4 && (ret + advance < pawn_advance[5])) {
/* passed pawns always have some value */
advance = pawn_advance[5] - ret;
}
}
ret += advance;
}
if (white_pawn_loc[x] != y)
ret -= DOUBLED_PAWN;
if (x==0) {
if (white_pawn_loc[x+1] == 0) ret -= ISOLATED_PAWN;
} else if (x==7) {
if (white_pawn_loc[x-1] == 0) ret -= ISOLATED_PAWN;
} else if (white_pawn_loc[x-1] == 0 && white_pawn_loc[x+1] == 0) {
ret -= ISOLATED_PAWN;
}
/* get rid of this because its not calculated incrementally and not clear
that its really useful */
#if 0
if (b->stage == ENDING) {
if ((b->piece_mask & WBISHOP_MASK) == WKBISHOP_MASK) {
if (!white_square(piece->pos)) ret += ODD_BISHOPS_PAWN_POS;
} else if ((b->piece_mask & WBISHOP_MASK) == WQBISHOP_MASK) {
if (white_square(piece->pos)) ret += ODD_BISHOPS_PAWN_POS;
}
}
#endif
return ret;
}
static etype eval_black_pawn(Position *b, int pi)
{
PieceStruct *piece = b->pieces + pi;
etype ret=0;
int x = XPOS(piece->pos);
int y = YPOS(piece->pos);
etype advance = 0;
int weak = 0;
ret = pawn_pos_value[mirror_square(piece->pos)];
/* a pawn is weak if it isn't supported by a pawn and can't
move to a place where it is supported by a pawn */
if (b->topieces[piece->pos] & BPAWN_MASK)
goto not_weak;
if ((b->topieces[piece->pos+SOUTH] & BPAWN_MASK) &&
abs(b->board[piece->pos+SOUTH]) != PAWN)
goto not_weak;
if (y == 6 && !(b->topieces[piece->pos+SOUTH] & WPAWN_MASK) &&
(b->topieces[piece->pos+2*SOUTH] & BPAWN_MASK) &&
(abs(b->board[piece->pos+SOUTH]) != PAWN) &&
(abs(b->board[piece->pos+2*SOUTH]) != PAWN))
goto not_weak;
if (debug)
lprintf(0,"weak %s\n", posstr(piece->pos));
weak = 1;
ret -= WEAK_PAWN;
/* if its on a half open file then its even weaker */
if (white_pawn_loc[x] == 0)
ret -= MEGA_WEAK_PAWN;
/* attacks on weak pawns are worth something */
if (b->topieces[piece->pos] & ~b->pinned_mask & 0xFF)
ret -= pop_count[b->topieces[piece->pos] &
~b->pinned_mask & 0xFF] * WEAK_PAWN_ATTACK_VALUE;
not_weak:
if (white_pawn_loc[x] == 0 || white_pawn_loc[x] > y) {
int n = y;
if (!weak) {
advance = pawn_advance[n] / 4;
}
if ((x==0 || white_pawn_loc[x-1]==0 ||
white_pawn_loc[x-1] >= y) &&
(x==7 || white_pawn_loc[x+1]==0 ||
white_pawn_loc[x+1] >= y)) {
Square wkpos = WHITEPIECES(b)[IKING].pos;
Square bkpos = BLACKPIECES(b)[IKING].pos;
#if 1
b->bpassed_pawn_mask |= (1<<pi);
#endif
advance += pawn_advance[n];
if (YPOS(wkpos) <= y && abs(XPOS(wkpos) - x) <= 1)
advance = shiftr1(advance);
if (YPOS(bkpos) <= y && abs(XPOS(bkpos) - x) <= 1)
advance += KING_PASSED_PAWN_SUPPORT;
if (YPOS(wkpos) <= y && abs(XPOS(wkpos) - x) <= 1)
advance = shiftr1(advance);
if (b->board[piece->pos+SOUTH] > 0) {
advance -= BLOCKED_PASSED_PAWN;
}
if (x != 0 && black_pawn_loc[x-1] &&
abs(y - black_pawn_loc[x-1]) <= 1) {
advance += shiftr1(pawn_advance[n]);
}
if (x != 7 && black_pawn_loc[x+1] &&
abs(y - black_pawn_loc[x+1]) <= 1) {
advance += shiftr1(pawn_advance[n]);
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & WKROOK_MASK) &&
XPOS(WHITEPIECES(b)[IKROOK].pos) == x &&
YPOS(WHITEPIECES(b)[IKROOK].pos) > y) {
/* there is a white king rook
behind our passed pawn */
advance -= PASSED_PAWN_ROOK_ATTACK;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & WQROOK_MASK) &&
XPOS(WHITEPIECES(b)[IQROOK].pos) == x &&
YPOS(WHITEPIECES(b)[IQROOK].pos) > y) {
/* there is a white queen rook
behind our passed pawn */
advance -= PASSED_PAWN_ROOK_ATTACK;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & BKROOK_MASK) &&
XPOS(BLACKPIECES(b)[IKROOK].pos) == x &&
YPOS(BLACKPIECES(b)[IKROOK].pos) > y) {
/* there is a black king rook
behind our passed pawn */
advance += PASSED_PAWN_ROOK_SUPPORT;
}
if ((b->topieces[piece->pos] & ~b->pinned_mask & BQROOK_MASK) &&
XPOS(BLACKPIECES(b)[IQROOK].pos) == x &&
YPOS(BLACKPIECES(b)[IQROOK].pos) > y) {
/* there is a black queen rook
behind our passed pawn */
advance += PASSED_PAWN_ROOK_SUPPORT;
}
/* a special case - is it unstoppable? */
if ((b->piece_mask & WHITE_MASK) == WKING_MASK) {
int kpos = WHITEPIECES(b)[IKING].pos;
int kmoves = imax(abs(XPOS(kpos) - x), YPOS(kpos));
int pmoves = y;
if (whites_move(b)) kmoves--;
if (kmoves > pmoves) {
/* its a candidate for unstopppability, but we need to
count all pieces that have to move out of the way,
except those on the queening square. */
Square pos = piece->pos;
while (!off_board(pos+SOUTH,SOUTH)) {
pos += SOUTH;
if (b->board[pos] != 0) {
if (b->board[pos] == -PAWN)
pmoves += 10;
else
++pmoves;
if (pmoves >= kmoves)
break;
}
}
if (kmoves > pmoves) {
ret += UNSTOPPABLE_PAWN;
if (debug)
lprintf(0,"unstoppable %s %d %d\n",
posstr(piece->pos),
kmoves, pmoves);
/* would it become stoppable after a null move? */
if (blacks_move(b) && kmoves == pmoves+1) {
b->null_stoppable_pawn |= (1<<pi);
if (debug)
lprintf(0,"null stoppable %s\n",
posstr(piece->pos));
}
/* would it become unstoppable after a null move? */
} else if (whites_move(b) && kmoves == pmoves) {
b->null_unstoppable_pawn |= (1<<pi);
if (debug)
lprintf(0,"null unstoppable %s\n",
posstr(piece->pos));
}
}
}
if (n < 4 && (ret + advance < pawn_advance[5])) {
/* passed pawns always have some value */
advance = pawn_advance[5] - ret;
}
}
if (debug)
lprintf(0,"%s advance: %e\n", posstr(piece->pos), advance);
ret += advance;
}
if (black_pawn_loc[x] != y)
ret -= DOUBLED_PAWN;
if (x==0) {
if (black_pawn_loc[x+1] == 0) ret -= ISOLATED_PAWN;
} else if (x==7) {
if (black_pawn_loc[x-1] == 0) ret -= ISOLATED_PAWN;
} else if (black_pawn_loc[x-1] == 0 && black_pawn_loc[x+1] == 0) {
ret -= ISOLATED_PAWN;
}
#if 0
if (b->stage == ENDING)
if ((b->piece_mask & BBISHOP_MASK) == BKBISHOP_MASK) {
if (white_square(piece->pos)) ret += ODD_BISHOPS_PAWN_POS;
} else if ((b->piece_mask & BBISHOP_MASK) == BQBISHOP_MASK) {
if (!white_square(piece->pos)) ret += ODD_BISHOPS_PAWN_POS;
}
#endif
return ret;
}
/* these are adjustments for king position in the opening and
ending - they try to encourage a central king in the ending */
static CONST_EVAL etype *ending_kpos = &coefficients[IENDING_KPOS];
/* the king likes to be near an edge at the start of the game and
near the middle at the end */
static etype eval_white_king(Position *b, int pi)
{
PieceStruct *piece = b->pieces + pi;
etype ret=0;
if (b->piece_mask & BQUEEN_MASK) {
if (b->stage == OPENING) {
if (YPOS(piece->pos) != 0)
ret -= OPENING_KING_ADVANCE*YPOS(piece->pos);
} if (b->stage == MIDDLE) {
if (YPOS(piece->pos) != 0)
ret -= MID_KING_ADVANCE*YPOS(piece->pos);
}
} else {
ret += ending_kpos[XPOS(piece->pos)] +
ending_kpos[YPOS(piece->pos)];
if (b->stage == MATING) {
if (b->w_material < b->b_material - PAWN_VALUE) {
ret *= 10;
}
}
}
if (b->stage == ENDING)
return ret;
if (b->flags & WHITE_CASTLED) {
ret += CASTLE_BONUS;
} else if (!(b->flags & (WHITE_CASTLE_SHORT|WHITE_CASTLE_LONG))) {
ret -= CASTLE_BONUS;
}
/* forget about pawn defence once the queens are off! */
if (!(b->material_mask & BQUEEN_MASK))
return ret;
if (YPOS(piece->pos) == 0 &&
(XPOS(piece->pos) > 4 || (b->flags & WHITE_CASTLE_SHORT))) {
if (b->board[F2] == PAWN)
ret += pawn_defence[FPAWN];
if (b->board[G2] == PAWN)
ret += pawn_defence[GPAWN];
if (b->board[H2] == PAWN)
ret += pawn_defence[HPAWN];
if (b->board[H3] == PAWN && b->board[G2] == PAWN)
ret += pawn_defence[HGPAWN];
if (b->board[G3] == PAWN && b->board[H2] == PAWN &&
!(b->material_mask & BQBISHOP_MASK))
ret += pawn_defence[GHPAWN];
if (b->board[F3] == PAWN && b->board[G2] == PAWN &&
!(b->material_mask & BKBISHOP_MASK))
ret += pawn_defence[FGPAWN];
}
if (YPOS(piece->pos) == 0 && XPOS(piece->pos) < 3) {
if (b->board[A2] == PAWN)
ret += pawn_defence[APAWN];
if (b->board[B2] == PAWN)
ret += pawn_defence[BPAWN];
if (b->board[C2] == PAWN)
ret += pawn_defence[CPAWN];
if (b->board[A3] == PAWN && b->board[B2] == PAWN)
ret += pawn_defence[ABPAWN];
if (b->board[B3] == PAWN && b->board[A2] == PAWN &&
!(b->material_mask & BKBISHOP_MASK))
ret += pawn_defence[BAPAWN];
if (b->board[C3] == PAWN && b->board[B2] == PAWN &&
!(b->material_mask & BQBISHOP_MASK))
ret += pawn_defence[CBPAWN];
}
return ret;
}
static etype eval_black_king(Position *b, int pi)
{
PieceStruct *piece = b->pieces + pi;
etype ret=0;
if (b->piece_mask & WQUEEN_MASK) {
if (b->stage == OPENING) {
if (YPOS(piece->pos) != 7)
ret -= OPENING_KING_ADVANCE*(7-YPOS(piece->pos));
} if (b->stage == MIDDLE) {
if (YPOS(piece->pos) != 7)
ret -= MID_KING_ADVANCE*(7-YPOS(piece->pos));
}
} else {
ret += ending_kpos[XPOS(piece->pos)] +
ending_kpos[YPOS(piece->pos)];
if (b->stage == MATING) {
if (b->b_material < b->w_material - PAWN_VALUE) {
ret *= 10;
}
}
}
if (b->stage == ENDING)
return ret;
if (b->flags & BLACK_CASTLED) {
ret += CASTLE_BONUS;
} else if (!(b->flags & (BLACK_CASTLE_SHORT|BLACK_CASTLE_LONG))) {
ret -= CASTLE_BONUS;
}
/* forget about pawn defence once the queens are off! */
if (!(b->material_mask & WQUEEN_MASK))
return ret;
if (YPOS(piece->pos) == 7 &&
(XPOS(piece->pos) > 4 || (b->flags & BLACK_CASTLE_SHORT))) {
if (b->board[F7] == -PAWN)
ret += pawn_defence[FPAWN];
if (b->board[G7] == -PAWN)
ret += pawn_defence[GPAWN];
if (b->board[H7] == -PAWN)
ret += pawn_defence[HPAWN];
if (b->board[H6] == -PAWN && b->board[G7] == -PAWN)
ret += pawn_defence[HGPAWN];
if (b->board[G6] == -PAWN && b->board[H7] == -PAWN &&
!(b->material_mask & WQBISHOP_MASK))
ret += pawn_defence[GHPAWN];
if (b->board[F6] == -PAWN && b->board[G7] == -PAWN &&
!(b->material_mask & WKBISHOP_MASK))
ret += pawn_defence[FGPAWN];
}
if (YPOS(piece->pos) == 7 && XPOS(piece->pos) < 3) {
if (b->board[A7] == -PAWN)
ret += pawn_defence[APAWN];
if (b->board[B7] == -PAWN)
ret += pawn_defence[BPAWN];
if (b->board[C7] == -PAWN)
ret += pawn_defence[CPAWN];
if (b->board[A6] == -PAWN && b->board[B7] == -PAWN)
ret += pawn_defence[ABPAWN];
if (b->board[B6] == -PAWN && b->board[A7] == -PAWN &&
!(b->material_mask & WKBISHOP_MASK))
ret += pawn_defence[BAPAWN];
if (b->board[C6] == -PAWN && b->board[B7] == -PAWN &&
!(b->material_mask & WQBISHOP_MASK))
ret += pawn_defence[CBPAWN];
}
return ret;
}
static etype eval_queen(Position *b, int pi)
{
PieceStruct *p = b->pieces + pi;
etype ret=0;
/* penalize early queen movement */
if (b->stage == OPENING) {
if ((p->p > 0 && YPOS(p->pos) > 2) ||
(p->p < 0 && YPOS(p->pos) < 5))
ret -= EARLY_QUEEN_MOVEMENT;
}
return ret;
}
static etype eval_rook(Position *b, int pi)
{
static CONST_EVAL etype *pos_value = &coefficients[IROOK_POS];
PieceStruct *p = b->pieces + pi;
etype ret=0;
if (p->p > 0)
ret += pos_value[p->pos];
else
ret += pos_value[mirror_square(p->pos)];
if (p->p > 0) {
if (b->topieces[p->pos] & ~b->pinned_mask & WROOK_MASK)
ret += CONNECTED_ROOKS;
} else {
if (b->topieces[p->pos] & ~b->pinned_mask & BROOK_MASK)
ret += CONNECTED_ROOKS;
}
return ret;
}
static etype eval_bishop_xray(Position *b, int pi)
{
PieceStruct *p = b->pieces + pi;
etype ret=0;
int xray=0;
PieceStruct *p2;
static CONST_EVAL etype *xray_bonus = &coefficients[IBISHOP_XRAY];
/* bishops get a bonus for Xray attacks on kings, queens or rooks */
p2 = &PIECES(b, -p->p)[IKING];
if (capture_map[p->p+KING][p->pos][p2->pos])
xray++;
p2 = &PIECES(b, -p->p)[IQUEEN];
if (p2->p && capture_map[p->p+KING][p->pos][p2->pos])
xray++;
p2 = &PIECES(b, -p->p)[IQROOK];
if (p2->p && capture_map[p->p+KING][p->pos][p2->pos])
xray++;
p2 = &PIECES(b, -p->p)[IKROOK];
if (p2->p && capture_map[p->p+KING][p->pos][p2->pos])
xray++;
ret += xray_bonus[xray];
return ret;
}
static etype eval_bishop(Position *b, int pi)
{
etype ret=0;
PieceStruct *p = b->pieces + pi;
int x = XPOS(p->pos);
int y = YPOS(p->pos);
/* bishops are good on outposts */
if (p->p > 0) {
if (y < 6 && y > 3 &&
(x == 0 || black_pawn_loc[x-1] <= y) &&
(x == 7 || black_pawn_loc[x+1] <= y) &&
((x>0 && b->board[p->pos+SOUTH_WEST] == PAWN) ||
(x<7 && b->board[p->pos+SOUTH_EAST] == PAWN))) {
ret += BISHOP_OUTPOST;
}
} else {
if (y > 1 && y < 4 &&
(x == 0 || white_pawn_loc[x-1] == 0 ||
white_pawn_loc[x-1] >= y) &&
(x == 7 || white_pawn_loc[x+1] == 0 ||
white_pawn_loc[x+1] >= y) &&
((x>0 && b->board[p->pos+NORTH_WEST] == -PAWN) ||
(x<7 && b->board[p->pos+NORTH_EAST] == -PAWN))) {
ret += BISHOP_OUTPOST;
}
}
#if 1
ret += eval_bishop_xray(b, pi);
#endif
return ret;
}
static etype eval_knight(Position *b, int pi)
{
PieceStruct *p = b->pieces + pi;
etype ret=0;
int x = XPOS(p->pos);
int y = YPOS(p->pos);
static CONST_EVAL etype *pos_value = &coefficients[IKNIGHT_POS];
if (p->p > 0)
ret += pos_value[p->pos];
else
ret += pos_value[mirror_square(p->pos)];
/* knights are great on outposts */
if (p->p > 0) {
if (y < 6 && y > 3 &&
(x == 0 || black_pawn_loc[x-1] <= y) &&
(x == 7 || black_pawn_loc[x+1] <= y) &&
((x>0 && b->board[p->pos+SOUTH_WEST] == PAWN) ||
(x<7 && b->board[p->pos+SOUTH_EAST] == PAWN))) {
ret += KNIGHT_OUTPOST;
}
} else {
if (y > 1 && y < 4 &&
(x == 0 || white_pawn_loc[x-1] == 0 ||
white_pawn_loc[x-1] >= y) &&
(x == 7 || white_pawn_loc[x+1] == 0 ||
white_pawn_loc[x+1] >= y) &&
((x>0 && b->board[p->pos+NORTH_WEST] == -PAWN) ||
(x<7 && b->board[p->pos+NORTH_EAST] == -PAWN))) {
ret += KNIGHT_OUTPOST;
}
}
return ret;
}
/* build a table of the most backward pawns in each file for each color */
static void build_pawn_loc(Position *b)
{
int i;
PieceStruct *p;
memset(white_pawn_loc, 0, sizeof(white_pawn_loc));
memset(black_pawn_loc, 0, sizeof(black_pawn_loc));
p = &WHITEPIECES(b)[8];
for (i=0;i<8;i++, p++)
if (p->p == PAWN) {
int x = XPOS(p->pos);
int y = YPOS(p->pos);
if (white_pawn_loc[x] == 0 ||
white_pawn_loc[x] > y)
white_pawn_loc[x] = y;
}
p = &BLACKPIECES(b)[8];
for (i=0;i<8;i++, p++)
if (p->p == -PAWN) {
int x = XPOS(p->pos);
int y = YPOS(p->pos);
black_pawn_loc[x] = imax(black_pawn_loc[x], y);
}
}
void estimate_game_stage(Position *b, int root_node)
{
int count;
int last_stage = b->stage;
count = pop_count[b->piece_mask & 0xFF] +
pop_count[(b->piece_mask >> 16) & 0xFF];
if ((b->piece_mask & QUEEN_MASK) == QUEEN_MASK)
count += 2;
if ((root_node || last_stage == MATING) &&
b->piece_mask == b->material_mask) {
b->stage = MATING;
} else if (count <= 6) {
b->stage = ENDING;
} else if (count >= 16 && (b->flags & FLAG_CAN_CASTLE)) {
b->stage = OPENING;
} else {
b->stage = MIDDLE;
}
if (last_stage != b->stage) {
b->piece_change |= KING_MASK;
b->piece_change |= (QUEEN_MASK & b->piece_mask);
}
#if 0
if (root_node && b->stage == MATING && last_stage != MATING) {
hash_reset();
}
#endif
}
/* try to discourage specific positional features - particularly in
the opening */
static etype specifics(Position *b)
{
etype ret = 0;
/* blocking the e or d pawn is a bad idea */
if (b->board[D3] && b->board[D2] == PAWN)
ret -= BLOCKED_DPAWN;
if (b->board[E3] && b->board[E2] == PAWN)
ret -= BLOCKED_EPAWN;
if (b->board[D6] && b->board[D7] == -PAWN)
ret -= -BLOCKED_DPAWN;
if (b->board[E6] && b->board[E7] == -PAWN)
ret -= -BLOCKED_EPAWN;
/* blocking in knights is a bad idea */
if (b->board[C3] && b->board[B1] == KNIGHT)
ret -= BLOCKED_KNIGHT;
if (b->board[F3] && b->board[G1] == KNIGHT)
ret -= BLOCKED_KNIGHT;
if (b->board[C6] && b->board[B8] == -KNIGHT)
ret -= -BLOCKED_KNIGHT;
if (b->board[F6] && b->board[G8] == -KNIGHT)
ret -= -BLOCKED_KNIGHT;
/* pairs of bishops are good */
if ((b->piece_mask & WBISHOP_MASK) == WBISHOP_MASK)
ret += BISHOP_PAIR;
if ((b->piece_mask & BBISHOP_MASK) == BBISHOP_MASK)
ret -= BISHOP_PAIR;
/* opposite bishops is drawish */
if ((b->piece_mask & BISHOP_MASK) == (WKBISHOP_MASK | BKBISHOP_MASK) ||
(b->piece_mask & BISHOP_MASK) == (WQBISHOP_MASK | BQBISHOP_MASK)) {
if (b->b_material > b->w_material + PAWN_VALUE/2)
ret += OPPOSITE_BISHOPS;
else if (b->w_material > b->b_material + PAWN_VALUE/2)
ret -= OPPOSITE_BISHOPS;
}
/* these are some specialist endgame things */
if (b->w_material < b->b_material - PAWN_VALUE &&
b->stage == MATING) {
int kpos = WHITEPIECES(b)[IKING].pos;
ret -= NO_MATERIAL;
if ((b->piece_mask & BBISHOP_MASK) == BKBISHOP_MASK) {
ret -= (7-corner_distance(kpos)) * MATING_POSITION;
}
if ((b->piece_mask & BBISHOP_MASK) == BQBISHOP_MASK) {
ret -= (7-corner_distance(mirror_square(kpos))) * MATING_POSITION;
}
ret += distance(kpos, BLACKPIECES(b)[IKING].pos) * MATING_POSITION;
}
if (b->b_material < b->w_material - PAWN_VALUE &&
b->stage == MATING) {
int kpos = BLACKPIECES(b)[IKING].pos;
ret += NO_MATERIAL;
if ((b->piece_mask & WBISHOP_MASK) == WQBISHOP_MASK) {
ret += (7-corner_distance(kpos)) * MATING_POSITION;
}
if ((b->piece_mask & WBISHOP_MASK) == WKBISHOP_MASK) {
ret += (7-corner_distance(mirror_square(kpos))) * MATING_POSITION;
}
ret -= distance(kpos, WHITEPIECES(b)[IKING].pos) * MATING_POSITION;
}
return ret;
}
static CONST_EVAL etype *base_pos_value = &coefficients[IPOS_BASE];
static etype null_pos_value[NUM_SQUARES];
static CONST_EVAL etype *white_king_side = &coefficients[IPOS_KINGSIDE];
static CONST_EVAL etype *white_queen_side = &coefficients[IPOS_QUEENSIDE];
static etype black_king_side[NUM_SQUARES];
static etype black_queen_side[NUM_SQUARES];
static int new_pos_values;
static CONST_EVAL etype *sq_p1, *sq_p2;
static void recalc_pos_values(Position *b)
{
Square wkpos, bkpos;
int flags = 0;
wkpos = WHITEPIECES(b)[IKING].pos;
bkpos = BLACKPIECES(b)[IKING].pos;
sq_p1 = null_pos_value;
sq_p2 = null_pos_value;
if (b->material_mask & QUEEN_MASK) {
if (XPOS(wkpos) > 3 && YPOS(wkpos) < 3) {
sq_p1 = white_king_side;
flags |= FLAG_WHITE_KINGSIDE;
} else if (XPOS(wkpos) < 3 && YPOS(wkpos) < 3) {
sq_p1 = white_queen_side;
flags |= FLAG_WHITE_QUEENSIDE;
}
if (XPOS(bkpos) > 3 && YPOS(bkpos) > 4) {
sq_p2 = black_king_side;
flags |= FLAG_BLACK_KINGSIDE;
} else if (XPOS(bkpos) < 3 && YPOS(bkpos) > 4) {
sq_p2 = black_queen_side;
flags |= FLAG_BLACK_QUEENSIDE;
}
}
if ((b->flags & FLAG_KQ_SIDE) == flags)
return;
invert(black_king_side, white_king_side);
invert(black_queen_side, white_queen_side);
new_pos_values = 1;
b->flags &= ~FLAG_KQ_SIDE;
b->flags |= flags;
}
/* this basically tries to answer the question "can one of the players
profitably capture on that square, assuming there was something there
to capture */
static int get_control(Position *b, uint32 topieces,
int piece, Square sq)
{
int nw, nb;
/* the following masks select pieces that are of lower than
value than a piece */
static CONST_EVAL uint32 masks[2*KING+1] =
{0xFFFE, 0xFFFC, 0xFFF0, 0xFF00, 0xFF00, 0,
0,
0, 0xFF000000, 0xFF000000, 0xFFF00000, 0xFFFC0000, 0xFFFE0000};
if (!topieces) return 0;
if (topieces & masks[piece+KING])
return -piece;
/* compare pawn counts */
nw = pop_count[(topieces >> 8) & 0xFF];
nb = pop_count[(topieces >> 24) & 0xFF];
if (nw - nb) return nw - nb;
/* compare piece counts */
nw = pop_count[topieces & 0xFF];
nb = pop_count[(topieces >> 16) & 0xFF];
#if USE_SLIDING_CONTROL
nw += pop_count[b->xray_topieces[sq] & 0xFF];
nb += pop_count[(b->xray_topieces[sq] >> 16) & 0xFF];
#endif
return (nw - nb);
}
static etype board_control1(Position *b)
{
int i, j;
int control;
etype total, v;
uint32 topieces, oldflight, fchange, mchange, old_pin_mask, pin_mask;
uint32 oldtopieces, xray_topieces, oldxray_topieces;
uint32 *top, *oldtop, *xray_top, *oldxray_top, *flight;
Piece *oldboard;
/* this is a mask which governs what piece can fly to a square
given the smallest attacker on the square, so for example, the
0x00FC0000 entry means that any black piece except a queen
or king can fly to a square that is controlled by black if
the square is covered by a white rook */
static CONST_EVAL uint32 flight_mask[32] = {
0x00FE0000, 0x00FE0000, 0x00FC0000, 0x00FC0000,
0x00F00000, 0x00F00000, 0x00F00000, 0x00F00000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,