-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.c
2006 lines (1976 loc) · 85.6 KB
/
evaluate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "chess.h"
#include "data.h"
/* last modified 06/20/10 */
/*
*******************************************************************************
* *
* Evaluate() is used to evaluate the chess board. Broadly, it addresses *
* four (4) distinct areas: (1) material score which is simply a summing of *
* piece types multiplied by piece values; (2) pawn scoring which considers *
* placement of pawns and also evaluates passed pawns, particularly in end- *
* game situations; (3) piece scoring which evaluates the placement of each *
* piece as well as things like piece mobility; (4) king safety which *
* considers the pawn shelter around the king along with material present to *
* facilitate an attack. *
* *
*******************************************************************************
*/
int Evaluate(TREE * RESTRICT tree, int ply, int wtm, int alpha, int beta) {
PAWN_HASH_ENTRY *ptable;
PXOR *pxtable;
register int score, side, majors, minors, can_win = 3;
register int phase, lscore, totalPc, cutoff;
/*
**********************************************************************
* *
* Initialize. *
* *
**********************************************************************
*/
lscore = (wtm) ? Material : -Material;
cutoff = KNIGHT_VALUE - 5;
if (lscore + cutoff < alpha || lscore - cutoff > beta)
return lscore;
tree->dangerous[white] = (Queens(white) &&
TotalPieces(white, occupied) > 13)
|| (TotalPieces(white, rook) > 1 && TotalPieces(white, occupied) > 15);
tree->dangerous[black] = (Queens(black) &&
TotalPieces(black, occupied) > 13)
|| (TotalPieces(black, rook) > 1 && TotalPieces(black, occupied) > 15);
tree->evaluations++;
tree->score_mg = 0;
tree->score_eg = 0;
EvaluateMaterial(tree, wtm);
#if defined(SKILL)
if (skill < 100) {
int i, j;
for (i = 0; i < burnc[skill / 10]; i++)
for (j = 1; j < 10; j++)
burner[j - 1] = burner[j - 1] * burner[j];
}
#endif
#ifdef DEBUGEV
printf("score[material] (MG) = %4d\n", tree->score_mg);
printf("score[material] (EG) = %4d\n", tree->score_eg);
#endif
/*
**********************************************************************
* *
* Check for draws due to insufficient material and adjust the *
* score as necessary. This code also handles a special endgame *
* case where one side has only a lone king, and the king has no *
* legal moves. This has been shown to break a few evaluation *
* terms such as bishop + wrong color rook pawn. If this case is *
* detected, a drawscore is returned. *
* *
**********************************************************************
*/
if (TotalPieces(white, occupied) < 13 && TotalPieces(black, occupied) < 13)
do {
/*
************************************************************
* *
* If neither side has any pieces, and both sides have *
* non-rookpawns, then either side can win. *
* *
************************************************************
*/
if (TotalPieces(white, occupied) == 0 &&
TotalPieces(black, occupied) == 0 && (Pawns(white) & not_rook_pawns
&& Pawns(black) & not_rook_pawns))
break;
/*
************************************************************
* *
* If one side is an exchange up, but has no pawns, then *
* that side can not possibly win. *
* *
************************************************************
*/
majors = tree->pos.majors[white] - tree->pos.majors[black];
if (abs(majors) == 1) {
minors = tree->pos.minors[white] - tree->pos.minors[black];
if (majors == -minors) {
if (TotalPieces(black, pawn) == 0)
can_win &= 1;
if (TotalPieces(white, pawn) == 0)
can_win &= 2;
}
if (can_win == 0)
break;
}
/*
************************************************************
* *
* check several special cases, such as bishop + the *
* wrong rook pawn and adjust can_win accordingly. *
* *
************************************************************
*/
if (!EvaluateWinningChances(tree, white, wtm))
can_win &= 2;
if (!EvaluateWinningChances(tree, black, wtm))
can_win &= 1;
} while (0);
/*
**********************************************************************
* *
* Determine if this position should be evaluated to force mate *
* (neither side has pawns) or if it should be evaluated normally. *
* *
* Note the special case of no pawns, one side is ahead in total *
* material, but the game is a hopeless draw. KRN vs KR is one *
* example. If EvaluateWinningChances() determines that the side *
* with extra material can not win, the score is pulled closer to a *
* draw although it can not collapse completely to the drawscore as *
* it is possible to lose KRB vs KR if the KR side lets the king *
* get trapped on the edge of the board. *
* *
**********************************************************************
*/
tree->all_pawns = Pawns(black) | Pawns(white);
if (!tree->all_pawns) {
if (TotalPieces(white, occupied) > 3 || TotalPieces(black, occupied) > 3) {
if (Material > 0)
EvaluateMate(tree, white);
else if (Material < 0)
EvaluateMate(tree, black);
if (tree->score_eg > DrawScore(1) && !(can_win & 1))
tree->score_eg = tree->score_eg / 4;
if (tree->score_eg < DrawScore(1) && !(can_win & 2))
tree->score_eg = tree->score_eg / 4;
#if defined(SKILL)
if (skill < 100)
tree->score_eg =
skill * tree->score_eg / 100 + ((100 -
skill) * PAWN_VALUE * (BITBOARD) Random32() /
0x100000000ULL) / 100;
#endif
return ((wtm) ? tree->score_eg : -tree->score_eg);
}
}
/*
**********************************************************************
* *
* Now evaluate pawns. If the pawn hash signature has not changed *
* from the last entry to Evaluate() then we already have every- *
* thing we need in the pawn hash entry. In this case, we do not *
* need to call EvaluatePawns() at all. EvaluatePawns() does all *
* of the analysis for information specifically regarding only *
* pawns. In many cases, it merely records the presence/absence of *
* positional pawn feature because that feature also depends on *
* pieces. Note that anything put into EvaluatePawns() can only *
* consider the placement of pawns. Kings or other pieces can not *
* influence the score because those pieces are not hashed into the *
* pawn hash signature. Violating this principle leads to lots of *
* very difficult and challenging debugging problems. *
* *
**********************************************************************
*/
else {
if (PawnHashKey == tree->pawn_score.key) {
tree->score_mg += tree->pawn_score.score_mg;
tree->score_eg += tree->pawn_score.score_eg;
}
/*
************************************************************
* *
* First check to see if this position has been handled *
* before. If so, we can skip the work saved in the pawn *
* hash table. *
* *
************************************************************
*/
else {
ptable = pawn_hash_table + (PawnHashKey & pawn_hash_mask);
pxtable = (PXOR *) & (tree->pawn_score);
tree->pawn_score = *ptable;
tree->pawn_score.key ^=
pxtable->entry[1] ^ pxtable->entry[2] ^ pxtable->entry[3];
if (tree->pawn_score.key != PawnHashKey) {
tree->pawn_score.key = PawnHashKey;
tree->pawn_score.open_files = 255;
tree->pawn_score.score_mg = 0;
tree->pawn_score.score_eg = 0;
for (side = black; side <= white; side++)
EvaluatePawns(tree, side);
ptable->key =
pxtable->entry[0] ^ pxtable->
entry[1] ^ pxtable->entry[2] ^ pxtable->entry[3];
memcpy((char *) ptable + 8, (char *) &(tree->pawn_score) + 8, 24);
}
tree->score_mg += tree->pawn_score.score_mg;
tree->score_eg += tree->pawn_score.score_eg;
}
#ifdef DEBUGEV
printf("score[pawns] (MG) = %4d\n", tree->score_mg);
printf("score[pawns] (EG) = %4d\n", tree->score_eg);
#endif
/*
**********************************************************************
* *
* If there are any passed pawns, first call EvaluatePassedPawns() *
* to evaluate them. Then, if one side has a passed pawn and the *
* other side has no pieces, call EvaluatePassedPawnRaces() to see *
* if the passed pawn can be stopped from promoting. *
* *
**********************************************************************
*/
if (tree->pawn_score.passed[black] || tree->pawn_score.passed[white]) {
for (side = black; side <= white; side++)
if (tree->pawn_score.passed[side])
EvaluatePassedPawns(tree, side);
if ((TotalPieces(white, occupied) == 0 &&
tree->pawn_score.passed[black])
|| (TotalPieces(black, occupied) == 0 &&
tree->pawn_score.passed[white]))
EvaluatePassedPawnRaces(tree, wtm);
}
#ifdef DEBUGEV
printf("score[passed pawns] (MG) = %4d\n", tree->score_mg);
printf("score[passed pawns] (EG) = %4d\n", tree->score_eg);
#endif
}
/*
**********************************************************************
* *
* Call EvaluateDevelopment() to evaluate development. Note that *
* we only do this when either side has not castled at the root. *
* *
**********************************************************************
*/
for (side = black; side <= white; side++)
EvaluateDevelopment(tree, ply, side);
#ifdef DEBUGEV
printf("score[development]= %4d\n", tree->score_mg);
#endif
/*
**********************************************************************
* *
* Then evaluate pieces. *
* *
**********************************************************************
*/
phase =
Min(62, TotalPieces(white, occupied) + TotalPieces(black, occupied));
score = ((tree->score_mg * phase) + (tree->score_eg * (62 - phase))) / 62;
lscore = (wtm) ? score : -score;
totalPc = tree->pos.minors[white] + tree->pos.minors[black]
+ tree->pos.majors[white] + tree->pos.majors[black];
cutoff = lazy_eval_cutoff + totalPc * 4;
if (lscore + cutoff > alpha && lscore - cutoff < beta) {
tree->tropism[white] = 0;
tree->tropism[black] = 0;
for (side = black; side <= white; side++) {
EvaluateKnights(tree, side);
EvaluateBishops(tree, side);
EvaluateRooks(tree, side);
EvaluateQueens(tree, side);
}
for (side = black; side <= white; side++)
EvaluateKings(tree, ply, side);
}
#ifdef DEBUGEV
printf("score[pieces]= (MG) %4d\n", score);
printf("score[pieces]= (EG) %4d\n", score);
#endif
/*
**********************************************************************
* *
* Adjust the score if the game is drawish but one side appears to *
* be significantly better according to the computed score. *
* *
**********************************************************************
*/
score = ((tree->score_mg * phase) + (tree->score_eg * (62 - phase))) / 62;
score = EvaluateDraws(tree, ply, can_win, score);
#if defined(SKILL)
if (skill < 100)
score =
skill * score / 100 + ((100 -
skill) * PAWN_VALUE * (BITBOARD) Random32() / 0x100000000ULL) /
100;
#endif
return ((wtm) ? score : -score);
}
/* last modified 10/29/09 */
/*
*******************************************************************************
* *
* EvaluateBishops() is used to evaluate black/white bishops. *
* *
*******************************************************************************
*/
void EvaluateBishops(TREE * RESTRICT tree, int side) {
register BITBOARD temp, moves;
register int square, t, mobility;
register int score_eg = 0, score_mg = 0, enemy = Flip(side);
/*
************************************************************
* *
* First, locate each bishop and add in its piece/square *
* score. *
* *
************************************************************
*/
for (temp = Bishops(side); temp; temp &= temp - 1) {
square = LSB(temp);
score_mg += bval[mg][side][square];
score_eg += bval[eg][side][square];
/*
************************************************************
* *
* Evaluate for "outposts" which is a bishop that can't *
* be driven off by an enemy pawn, and which is supported *
* by a friendly pawn. *
* *
* If the enemy has NO minor to take this bishop, then *
* increase the bonus. *
* *
* Note that the bishop_outpost array is overloaded to *
* serve a dual prupose. A negative value is used to *
* flag squares A7 and H7 to test for a trapped bishop. *
* This is done for speed. *
* *
************************************************************
*/
t = bishop_outpost[side][square];
if (t) {
if (t > 0) {
if (!(mask_no_pattacks[enemy][square] & Pawns(enemy))) {
score_eg += t;
score_mg += t;
if (bishop_outpost[side][square] &&
pawn_attacks[enemy][square] & Pawns(side)) {
score_eg += t / 2;
score_mg += t / 2;
if (!Knights(enemy) && !(Color(square) & Bishops(enemy))) {
score_eg += t;
score_mg += t;
}
}
}
}
/*
************************************************************
* *
* Check to see if the bishop is trapped at a7 or h7 with *
* a pawn at b6 or g6 that has trapped the bishop. *
* *
************************************************************
*/
else {
if (square == sqflip[side][A7]) {
if (SetMask(sqflip[side][B6]) & Pawns(enemy)) {
score_eg -= bishop_trapped;
score_mg -= bishop_trapped;
}
} else if (SetMask(sqflip[side][G6]) & Pawns(enemy)) {
score_eg -= bishop_trapped;
score_mg -= bishop_trapped;
}
}
}
/*
************************************************************
* *
* Mobility counts the number of squares the piece *
* attacks, and weighs each square according to *
* centralization. *
* *
************************************************************
*/
mobility = MobilityBishop(square, OccupiedSquares);
score_mg += mobility;
score_eg += mobility;
/*
************************************************************
* *
* Check for pawns on both wings, which makes a bishop *
* even more valuable against an enemy knight *
* *
************************************************************
*/
if (tree->all_pawns & mask_fgh && tree->all_pawns & mask_abc) {
score_mg += bishop_with_wing_pawns[mg];
score_eg += bishop_with_wing_pawns[eg];
}
/*
************************************************************
* *
* Adjust the tropism count for this piece. *
* *
************************************************************
*/
if (tree->dangerous[side]) {
moves = king_attacks[KingSQ(enemy)];
t = ((bishop_attacks[square] & moves)
&& ((AttacksBishop(square,
OccupiedSquares & ~(Queens(side)))) & moves)) ? 1 :
Distance(square, KingSQ(enemy));
tree->tropism[side] += king_tropism_b[t];
}
}
tree->score_mg += sign[side] * score_mg;
tree->score_eg += sign[side] * score_eg;
#ifdef DEBUGEV
printf("score[bishops(%d), MG]= %4d\n", side, score_mg);
printf("score[bishops(%d), EG]= %4d\n", side, score_eg);
printf("tropism[bishops(%d)]= %4d\n", side,
tree->tropism[side]);
#endif
}
/* last modified 10/29/09 */
/*
*******************************************************************************
* *
* EvaluateDevelopment() is used to encourage the program to develop its *
* pieces before moving its queen. Standard developmental principles are *
* applied. They include: (1) don't move the queen until minor pieces are *
* developed; (2) advance the center pawns as soon as possible; (3) don't *
* move the king unless its a castling move. *
* *
*******************************************************************************
*/
void EvaluateDevelopment(TREE * RESTRICT tree, int ply, int side) {
register int score_mg = 0;
register int enemy = Flip(side);
/*
************************************************************
* *
* First, some "thematic" things, which includes don't *
* block the c-pawn in queen-pawn openings. *
* *
************************************************************
*/
if (!(SetMask(sqflip[side][E4]) & Pawns(side)) &&
SetMask(sqflip[side][D4]) & Pawns(side)) {
if (SetMask(sqflip[side][C2]) & Pawns(side) &&
SetMask(sqflip[side][C3]) & (Knights(side) | Bishops(side)))
score_mg -= development_thematic;
}
#ifdef DEBUGDV
printf("development(%d).1 score_mg=%d\n", side, score_mg);
#endif
/*
************************************************************
* *
* If the king hasn't moved at the beginning of the *
* search, but it has moved somewhere in the current *
* search path, make *sure* it's a castle move or else *
* penalize the loss of castling privilege. *
* *
************************************************************
*/
if (Castle(1, side) > 0) {
int oq = (Queens(enemy)) ? 3 : 1;
if (Castle(ply, side) != Castle(1, side)) {
if (Castle(ply, side) == 0)
score_mg -= oq * development_losing_castle;
else if (Castle(ply, side) > 0)
score_mg -= (oq * development_losing_castle) / 2;
} else
score_mg -= oq * development_not_castled;
}
#ifdef DEBUGDV
printf("development(%d).2 score_mg=%d\n", side, score_mg);
#endif
/*
************************************************************
* *
* Check for an undeveloped knight/rook combo *
* *
************************************************************
*/
if (PcOnSq(sqflip[side][B1]) == pieces[side][knight] &&
PcOnSq(sqflip[side][A1]) == pieces[side][rook])
score_mg -= undeveloped_piece;
if (PcOnSq(sqflip[side][G1]) == pieces[side][knight] &&
PcOnSq(sqflip[side][H1]) == pieces[side][rook])
score_mg -= undeveloped_piece;
tree->score_mg += sign[side] * score_mg;
}
/* last modified 10/29/09 */
/*
*******************************************************************************
* *
* EvaluateDraws() is used to adjust the score based on whether the side *
* that appears to be better according the computed score can actually win *
* the game or not. If the answer is "no" then the score is reduced *
* significantly to reflect the lack of winning chances. *
* *
*******************************************************************************
*/
int EvaluateDraws(TREE * RESTRICT tree, int ply, int can_win, int score) {
/*
************************************************************
* *
* If the ending has only bishops of opposite colors, the *
* score is pulled closer to a draw. If the score says *
* one side is winning, but that side doesn't have enough *
* material to win, the score is also pulled closer to a *
* draw. *
* *
* If this is a pure BOC ending, it is very drawish un- *
* less one side has at least 4 pawns. More pawns makes *
* it harder for a bishop and king to stop them all from *
* advancing. *
* *
************************************************************
*/
if (TotalPieces(white, occupied) <= 8 && TotalPieces(black, occupied) <= 8) {
if (TotalPieces(white, bishop) == 1 && TotalPieces(black, bishop) == 1) {
if (square_color[LSB(Bishops(black))] !=
square_color[LSB(Bishops(white))]) {
if (TotalPieces(white, occupied) == 3 &&
TotalPieces(black, occupied) == 3 &&
((TotalPieces(white, pawn) < 4 && TotalPieces(black, pawn) < 4) ||
abs(TotalPieces(white, pawn) - TotalPieces(black, pawn)) < 2))
score = score / 2;
else if (TotalPieces(white, occupied) == TotalPieces(black, occupied))
score = 3 * score / 4;
}
}
}
if (can_win != 3) {
if (can_win != 1 && score > DrawScore(1))
score = score / 4;
else if (can_win != 2 && score < DrawScore(1))
score = score / 4;
}
/*
************************************************************
* *
* If we are running into the 50-move rule, then start *
* dragging the score toward draw. This is the idea of a *
* "weariness factor" as mentioned by Dave Slate many *
* times. This avoids slamming into a draw at move 50 *
* and having to move something quickly, rather than *
* slowly discovering that the score is dropping and that *
* pushing a pawn or capturing something will cause it to *
* go back to its correct value a bit more smoothly. *
* *
************************************************************
*/
if (Rule50Moves(ply) > 80) {
int scale = 101 - Rule50Moves(ply);
score = DrawScore(1) + score * scale / 20;
}
#ifdef DEBUGEV
printf("score[draws] = %4d\n", score);
#endif
return (score);
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* EvaluateHasOpposition() is used to determine if one king stands in *
* "opposition" to the other. If the kings are opposed on the same file or *
* else are opposed on the same diagonal, then the side not-to-move has the *
* opposition and the side-to-move must give way. *
* *
*******************************************************************************
*/
int EvaluateHasOpposition(int on_move, int king, int enemy_king) {
register int file_distance, rank_distance;
file_distance = FileDistance(king, enemy_king);
rank_distance = RankDistance(king, enemy_king);
if (rank_distance < 2)
return (1);
if (on_move) {
if (rank_distance & 1) {
rank_distance--;
if (file_distance & 1)
file_distance--;
} else if (file_distance & 1) {
file_distance--;
if (rank_distance & 1)
rank_distance--;
}
}
if (!(file_distance & 1) && !(rank_distance & 1))
return (1);
return (0);
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* EvaluateKings() is used to evaluate black/white kings. *
* *
*******************************************************************************
*/
void EvaluateKings(TREE * RESTRICT tree, int ply, int side) {
register int score_eg = 0, score_mg = 0, defects;
int enemy = Flip(side);
/*
************************************************************
* *
* First, check for where the king should be if this is *
* an endgame. Ie with pawns on one wing, the king needs *
* to be on that wing. With pawns on both wings, the *
* king belongs in the center. *
* *
************************************************************
*/
if (tree->all_pawns) {
if (tree->all_pawns & mask_efgh && tree->all_pawns & mask_abcd)
score_eg += kval_n[side][KingSQ(side)];
else if (tree->all_pawns & mask_efgh)
score_eg += kval_k[side][KingSQ(side)];
else
score_eg += kval_q[side][KingSQ(side)];
}
/*
************************************************************
* *
* Check for the "trojan horse" attack where the opponent *
* offers a piece to open the h-file with a very *
* difficult to refute attack. *
* *
************************************************************
*/
if (tree->dangerous[enemy]) {
if (trojan_check) {
if (root_wtm == side && File(KingSQ(side)) >= FILEE) {
if (!(tree->all_pawns & file_mask[FILEH])) {
if (Rooks(enemy) && Queens(enemy))
score_mg -= king_safety_mate_threat;
}
}
}
/*
************************************************************
* *
* Do castle scoring, if the king has castled, the pawns *
* in front are important. If not castled yet, the pawns *
* on the kingside should be preserved for this. *
* *
************************************************************
*/
defects = 0;
if (Castle(ply, side) <= 0) {
if (File(KingSQ(side)) >= FILEE) {
if (File(KingSQ(side)) > FILEE)
defects = tree->pawn_score.defects_k[side];
else
defects = tree->pawn_score.defects_e[side];
} else {
if (File(KingSQ(side)) < FILED)
defects = tree->pawn_score.defects_q[side];
else
defects = tree->pawn_score.defects_d[side];
}
} else {
if (Castle(ply, side) == 3)
defects =
Min(Min(tree->pawn_score.defects_k[side],
tree->pawn_score.defects_e[side]),
tree->pawn_score.defects_q[side]);
else if (Castle(ply, side) == 1)
defects =
Min(tree->pawn_score.defects_k[side],
tree->pawn_score.defects_e[side]);
else
defects =
Min(tree->pawn_score.defects_q[side],
tree->pawn_score.defects_e[side]);
if (defects < 3)
defects = 3;
}
/*
************************************************************
* *
* Fold in the king tropism and king pawn shelter scores *
* together. *
* *
************************************************************
*/
if (tree->tropism[enemy] < 0)
tree->tropism[enemy] = 0;
else if (tree->tropism[enemy] > 15)
tree->tropism[enemy] = 15;
if (defects > 15)
defects = 15;
score_mg -= king_safety[defects][tree->tropism[enemy]];
}
tree->score_mg += sign[side] * score_mg;
tree->score_eg += sign[side] * score_eg;
#ifdef DEBUGEV
printf("score[defects(%d)]= %4d\n", side, defects);
printf("score[tropism(%d)]= %4d\n", enemy,
tree->tropism[enemy]);
printf("score[kings(%d) [MG]= %4d\n", side, score_mg);
printf("score[kings(%d) [EG]= %4d\n", side, score_eg);
#endif
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* EvaluateKingsFile computes defects for a file, based on whether the file *
* is open or half-open. If there are friendly pawns still on the file, *
* they are penalized for advancing in front of the king. *
* *
*******************************************************************************
*/
int EvaluateKingsFile(TREE * RESTRICT tree, int whichfile, int side) {
register int defects = 0, file;
register int enemy = Flip(side);
for (file = whichfile - 1; file <= whichfile + 1; file++) {
if (!(file_mask[file] & tree->all_pawns))
defects += open_file[file];
else {
if (!(file_mask[file] & Pawns(enemy)))
defects += half_open_file[file] / 2;
else
defects +=
pawn_defects[side][Rank(Advanced(enemy,
file_mask[file] & Pawns(enemy)))];
if (!(file_mask[file] & Pawns(side)))
defects += half_open_file[file];
else {
if (!(Pawns(side) & SetMask(sqflip[side][A2] + file))) {
defects++;
if (!(Pawns(side) & SetMask(sqflip[side][A3] + file)))
defects++;
}
}
}
}
return (defects);
}
/* last modified 10/29/09 */
/*
*******************************************************************************
* *
* EvaluateKnights() is used to evaluate black/white knights. *
* *
*******************************************************************************
*/
void EvaluateKnights(TREE * RESTRICT tree, int side) {
register BITBOARD temp, moves;
register int square, i, t, score_eg = 0, score_mg = 0;
register int enemy = Flip(side);
/*
************************************************************
* *
* First fold in centralization score from the piece/ *
* square table "nval". *
* *
************************************************************
*/
for (temp = Knights(side); temp; temp &= temp - 1) {
square = LSB(temp);
score_mg += nval[mg][side][square];
score_eg += nval[eg][side][square];
/*
************************************************************
* *
* Evaluate for "outposts" which is a knight that can't *
* be driven off by an enemy pawn, and which is supported *
* by a friendly pawn. *
* *
* If the enemy has NO minor to take this knight, then *
* increase the bonus. *
* *
************************************************************
*/
if (!(mask_no_pattacks[enemy][square] & Pawns(enemy))) {
if (knight_outpost[side][square]) {
score_eg += knight_outpost[side][square];
score_mg += knight_outpost[side][square];
if (knight_outpost[side][square] &&
pawn_attacks[enemy][square] & Pawns(side)) {
score_eg += knight_outpost[side][square] / 2;
score_mg += knight_outpost[side][square] / 2;
if (!Knights(enemy) && !(Color(square) & Bishops(enemy))) {
score_eg += knight_outpost[side][square];
score_mg += knight_outpost[side][square];
}
}
}
}
/*
************************************************************
* *
* Mobility counts the number of squares the piece *
* attacks, excluding squares with friendly pieces, and *
* weighs each square according to centralization. *
* *
************************************************************
*/
moves = AttacksKnight(square) & ~Occupied(side);
if (tree->cache_n[square] != moves) {
tree->cache_n[square] = moves;
t = -lower_n;
for (i = 0; i < 4; i++)
t += PopCnt(moves & mobility_mask_n[i]) * mobility_score_n[i];
tree->cache_n_mobility[square] = t;
}
score_mg += tree->cache_n_mobility[square];
score_eg += tree->cache_n_mobility[square];
/*
************************************************************
* *
* Adjust the tropism count for this piece. *
* *
************************************************************
*/
if (tree->dangerous[side]) {
t = Distance(square, KingSQ(enemy));
tree->tropism[side] += king_tropism_n[t];
}
}
tree->score_mg += sign[side] * score_mg;
tree->score_eg += sign[side] * score_eg;
#ifdef DEBUGEV
printf("score[knights(%d), MG]= %4d\n", side, score_mg);
printf("score[knights(%d), EG]= %4d\n", side, score_eg);
printf("tropism[knights(%d)]= %4d\n", side,
tree->tropism[side]);
#endif
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* EvaluateMate() is used to evaluate positions where neither side has pawns *
* and one side has enough material to force checkmate. It simply trys to *
* force the losing king to the edge of the board, and then to the corner *
* where mates are easier to find. *
* *
*******************************************************************************
*/
void EvaluateMate(TREE * RESTRICT tree, int side) {
register int mate_score = 0;
register int enemy = Flip(side);
/*
************************************************************
* *
* If one side has a bishop+knight and the other side has *
* no pieces or pawns, then use the special bishop_knight *
* scoring board for the losing king to force it to the *
* right corner for mate. *
* *
************************************************************
*/
if (!TotalPieces(enemy, occupied) && tree->pos.minors[side] == 2 &&
TotalPieces(side, bishop) == 1) {
if (dark_squares & Bishops(side))
mate_score = b_n_mate_dark_squares[KingSQ(enemy)];
else
mate_score = b_n_mate_light_squares[KingSQ(enemy)];
}
/*
************************************************************
* *
* If one side is winning, force the enemy king to the *
* edge of the board. *
* *
************************************************************
*/
else {
mate_score = mate[KingSQ(enemy)];
mate_score -=
(Distance(KingSQ(side), KingSQ(enemy)) - 3) * king_king_tropism;
}
tree->score_mg += sign[side] * mate_score;
tree->score_eg += sign[side] * mate_score;
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* EvaluateMaterial() is used to evaluate material on the board. It really *
* accomplishes detecting cases where one side has made a 'bad trade' as the *
* comments below show. *
* *
*******************************************************************************
*/
void EvaluateMaterial(TREE * RESTRICT tree, int wtm) {
register int score_mg, score_eg, majors, minors, imbal;
static int bon[17] =
{ 0, 40, 40, 35, 30, 24, 16, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1 };
/*
**********************************************************************
* *
* We start with the raw Material balance for the current position, *
* then adjust this with a small bonus for the side on move. *
* *
**********************************************************************
*/
score_mg = Material + ((wtm) ? wtm_bonus[mg] : -wtm_bonus[mg]);
score_eg = Material + ((wtm) ? wtm_bonus[eg] : -wtm_bonus[eg]);
/*
**********************************************************************
* *
* If Majors or Minors are not balanced, then apply the appropriate *
* bonus/penatly from our imbalance table. *
* *
**********************************************************************
*/
majors = 4 + tree->pos.majors[white] - tree->pos.majors[black];
minors = 4 + tree->pos.minors[white] - tree->pos.minors[black];
majors = Max(Min(majors, 8), 0);
minors = Max(Min(minors, 8), 0);
imbal = imbalance[majors][minors];
score_mg += imbal;
score_eg += imbal;
/*
************************************************************
* *
* Add a bonus per side if side has a pair of bishops, *
* which can become very strong in open positions. *
* *
************************************************************
*/
if (TotalPieces(white, bishop) > 1) {
score_mg += 38;
score_eg += 56;
}
if (TotalPieces(black, bishop) > 1) {
score_mg -= 38;
score_eg -= 56;
}
/*
************************************************************
* *
* Check for pawns on both wings, which makes a bishop *
* even more valuable against an enemy knight *
* (knight vs. bishop in endgame) *
* *
************************************************************
*/
if (!imbal && !tree->pos.majors[white] && tree->pos.minors[white] == 1) {
if (tree->all_pawns & mask_fgh && tree->all_pawns & mask_abc) {
imbal = bon[(TotalPieces(white, pawn) + TotalPieces(black, pawn))];
if (Bishops(white)) {
score_mg += imbal;
score_eg += imbal;
}
if (Bishops(black)) {
score_mg -= imbal;
score_eg -= imbal;
}
}
}
tree->score_mg += score_mg;
tree->score_eg += score_eg;
#ifdef DEBUGM
printf
("Majors=%d Minors=%d TotalPieces(white, occupied)=%d TotalPieces(black, occupied)=%d\n",
Majors, Minors, TotalPieces(white, occupied), TotalPieces(black,
occupied));
printf("score[bad trade] = (MG) %4d\n", tree->score_mg);
printf("score[bad trade] = (EG) %4d\n", tree->score_eg);
#endif
}
/* last modified 10/29/09 */
/*
*******************************************************************************
* *
* EvaluatePassedPawns() is used to evaluate passed pawns and the danger *
* they produce. This code considers pieces as well, so it has been *
* separated from the normal EvaluatePawns() code that hashes information *
* based only on pawn positions. *
* *
*******************************************************************************
*/