-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patheval.cpp
More file actions
810 lines (643 loc) · 31.2 KB
/
eval.cpp
File metadata and controls
810 lines (643 loc) · 31.2 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
#include "stdafx.h"
#include "0x88_math.h"
#include "eval.h"
#include "transposition.h"
/******************************************************************************
* We want our eval to be color-independent, i.e. the same functions ought to *
* be called for white and black pieces. This requires some way of converting *
* row and square coordinates. *
******************************************************************************/
static const int seventh[2] = { ROW(A7), ROW(A2) };
static const int eighth[2] = { ROW(A8), ROW(A1) };
static const int stepFwd[2] = { NORTH, SOUTH };
static const int stepBck[2] = { SOUTH, NORTH };
static const int inv_sq[128] = {
A8, B8, C8, D8, E8, F8, G8, H8, -1, -1, -1, -1, -1, -1, -1, -1,
A7, B7, C7, D7, E7, F7, G7, H7, -1, -1, -1, -1, -1, -1, -1, -1,
A6, B6, C6, D6, E6, F6, G6, H6, -1, -1, -1, -1, -1, -1, -1, -1,
A5, B5, C5, D5, E5, F5, G5, H5, -1, -1, -1, -1, -1, -1, -1, -1,
A4, B4, C4, D4, E4, F4, G4, H4, -1, -1, -1, -1, -1, -1, -1, -1,
A3, B3, C3, D3, E3, F3, G3, H3, -1, -1, -1, -1, -1, -1, -1, -1,
A2, B2, C2, D2, E2, F2, G2, H2, -1, -1, -1, -1, -1, -1, -1, -1,
A1, B1, C1, D1, E1, F1, G1, H1, -1, -1, -1, -1, -1, -1, -1, -1
};
#define REL_SQ(cl, sq) ((cl) == (WHITE) ? (sq) : (inv_sq[sq]))
/* adjustements of piece value based on the number of own pawns */
int n_adj[9] = { -20, -16, -12, -8, -4, 0, 4, 8, 12};
int r_adj[9] = { 15, 12, 9, 6, 3, 0, -3, -6, -9};
static const int SafetyTable[100] = {
0, 0, 1, 2, 3, 5, 7, 9, 12, 15,
18, 22, 26, 30, 35, 39, 44, 50, 56, 62,
68, 75, 82, 85, 89, 97, 105, 113, 122, 131,
140, 150, 169, 180, 191, 202, 213, 225, 237, 248,
260, 272, 283, 295, 307, 319, 330, 342, 354, 366,
377, 389, 401, 412, 424, 436, 448, 459, 471, 483,
494, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500
};
/******************************************************************************
* This struct holds data about certain aspects of evaluation, which allows *
* our program to print them if desired. *
******************************************************************************/
struct eval_vector {
int gamePhase; // function of piece material: 24 in opening, 0 in endgame
int mgMob[2]; // midgame mobility
int egMob[2]; // endgame mobility
int attCnt[2]; // no. of pieces attacking zone around enemy king
int attWeight[2]; // weight of attacking pieces - index to SafetyTable
int mgTropism[2]; // midgame king tropism score
int egTropism[2]; // endgame king tropism score
int kingShield[2];
int adjustMaterial[2];
int blockages[2];
int positionalThemes[2];
} v;
int eval( int alpha, int beta, int use_hash ) {
int result = 0, mgScore = 0, egScore = 0;
int stronger, weaker;
/**************************************************************************
* Probe the evaluatinon hashtable, unless we call eval() only in order *
* to display detailed result *
**************************************************************************/
int probeval = tteval_probe();
if (probeval != INVALID && use_hash)
return probeval;
/**************************************************************************
* Clear all eval data *
**************************************************************************/
v.gamePhase = b.piece_cnt[WHITE][KNIGHT] + b.piece_cnt[WHITE][BISHOP] + 2 * b.piece_cnt[WHITE][ROOK] + 4 * b.piece_cnt[WHITE][QUEEN]
+ b.piece_cnt[BLACK][KNIGHT] + b.piece_cnt[BLACK][BISHOP] + 2 * b.piece_cnt[BLACK][ROOK] + 4 * b.piece_cnt[BLACK][QUEEN];
for (int side = 0; side <= 1; side++) {
v.mgMob[side] = 0;
v.egMob[side] = 0;
v.attCnt[side] = 0;
v.attWeight[side] = 0;
v.mgTropism[side] = 0;
v.egTropism[side] = 0;
v.adjustMaterial[side] = 0;
v.blockages[side] = 0;
v.positionalThemes[side] = 0;
v.kingShield[side] = 0;
}
/**************************************************************************
* Sum the incrementally counted material and piece/square table values *
**************************************************************************/
mgScore = b.piece_material[WHITE] + b.pawn_material[WHITE] + b.pcsq_mg[WHITE]
- b.piece_material[BLACK] - b.pawn_material[BLACK] - b.pcsq_mg[BLACK];
egScore = b.piece_material[WHITE] + b.pawn_material[WHITE] + b.pcsq_eg[WHITE]
- b.piece_material[BLACK] - b.pawn_material[BLACK] - b.pcsq_eg[BLACK];
/**************************************************************************
* add king's pawn shield score and evaluate part of piece blockage score *
* (the rest of the latter will be done via piece eval) *
**************************************************************************/
v.kingShield[WHITE] = wKingShield();
v.kingShield[BLACK] = bKingShield();
blockedPieces(WHITE);
blockedPieces(BLACK);
mgScore += (v.kingShield[WHITE] - v.kingShield[BLACK]);
/* tempo bonus */
if ( b.stm == WHITE ) result += e.TEMPO;
else result -= e.TEMPO;
/**************************************************************************
* Adjusting material value for the various combinations of pieces. *
* Currently it scores bishop, knight and rook pairs. The first one *
* gets a bonus, the latter two - a penalty. Beside that knights lose *
* value as pawns disappear, whereas rooks gain. *
**************************************************************************/
if (b.piece_cnt[WHITE][BISHOP] > 1) v.adjustMaterial[WHITE] += e.BISHOP_PAIR;
if (b.piece_cnt[BLACK][BISHOP] > 1) v.adjustMaterial[BLACK] += e.BISHOP_PAIR;
if (b.piece_cnt[WHITE][KNIGHT] > 1) v.adjustMaterial[WHITE] -= e.P_KNIGHT_PAIR;
if (b.piece_cnt[BLACK][KNIGHT] > 1) v.adjustMaterial[BLACK] -= e.P_KNIGHT_PAIR;
if (b.piece_cnt[WHITE][ROOK] > 1 ) v.adjustMaterial[WHITE] -= e.P_ROOK_PAIR;
if (b.piece_cnt[BLACK][ROOK] > 1 ) v.adjustMaterial[BLACK] -= e.P_ROOK_PAIR;
v.adjustMaterial[WHITE] += n_adj[b.piece_cnt[WHITE][PAWN]] * b.piece_cnt[WHITE][KNIGHT];
v.adjustMaterial[BLACK] += n_adj[b.piece_cnt[BLACK][PAWN]] * b.piece_cnt[BLACK][KNIGHT];
v.adjustMaterial[WHITE] += r_adj[b.piece_cnt[WHITE][PAWN]] * b.piece_cnt[WHITE][ROOK];
v.adjustMaterial[BLACK] += r_adj[b.piece_cnt[BLACK][PAWN]] * b.piece_cnt[BLACK][ROOK];
result += getPawnScore();
/**************************************************************************
* Evaluate pieces *
**************************************************************************/
for (U8 row=0; row < 8; row++)
for (U8 col=0; col < 8; col++) {
S8 sq = SET_SQ(row, col);
if ( b.color[sq] != COLOR_EMPTY ) {
switch (b.pieces[sq]) {
case PAWN: // pawns are evaluated separately
break;
case KNIGHT:
EvalKnight(sq, b.color[sq]);
break;
case BISHOP:
EvalBishop(sq, b.color[sq]);
break;
case ROOK:
EvalRook(sq, b.color[sq]);
break;
case QUEEN:
EvalQueen(sq, b.color[sq]);
break;
case KING:
break;
}
}
}
/**************************************************************************
* Merge midgame and endgame score. We interpolate between these two *
* values, using a gamePhase value, based on remaining piece material on *
* both sides. With less pieces, endgame score becomes more influential. *
**************************************************************************/
mgScore += (v.mgMob[WHITE] - v.mgMob[BLACK]);
egScore += (v.egMob[WHITE] - v.egMob[BLACK]);
mgScore += (v.mgTropism[WHITE] - v.mgTropism[BLACK]);
egScore += (v.egTropism[WHITE] - v.egTropism[BLACK]);
if (v.gamePhase > 24) v.gamePhase = 24;
int mgWeight = v.gamePhase;
int egWeight = 24 - mgWeight;
result += ( (mgScore * mgWeight) + (egScore * egWeight) ) / 24;
/**************************************************************************
* Add phase-independent score components. *
**************************************************************************/
result += (v.blockages[WHITE] - v.blockages[BLACK]);
result += (v.positionalThemes[WHITE] - v.positionalThemes[BLACK]);
result += (v.adjustMaterial[WHITE] - v.adjustMaterial[BLACK]);
/**************************************************************************
* Merge king attack score. We don't apply this value if there are less *
* than two attackers or if the attacker has no queen. *
**************************************************************************/
if (v.attCnt[WHITE] < 2 || b.piece_cnt[WHITE][QUEEN] == 0) v.attWeight[WHITE] = 0;
if (v.attCnt[BLACK] < 2 || b.piece_cnt[BLACK][QUEEN] == 0) v.attWeight[BLACK] = 0;
result += SafetyTable[v.attWeight[WHITE]];
result -= SafetyTable[v.attWeight[BLACK]];
/**************************************************************************
* Low material correction - guarding against an illusory material advan- *
* tage. Full blown program should have more such rules, but the current *
* set ought to be useful enough. Please note that our code assumes *
* different material values for bishop and knight. *
* *
* - a single minor piece cannot win *
* - two knights cannot checkmate bare king *
* - bare rook vs minor piece is drawish *
* - rook and minor vs rook is drawish *
**************************************************************************/
if (result > 0) {
stronger = WHITE;
weaker = BLACK;
} else {
stronger = BLACK;
weaker = WHITE;
}
if (b.pawn_material[stronger] == 0) {
if (b.piece_material[stronger] < 400) return 0;
if (b.pawn_material[weaker] == 0
&& (b.piece_material[stronger] == 2 * e.PIECE_VALUE[KNIGHT]))
return 0;
if (b.piece_material[stronger] == e.PIECE_VALUE[ROOK]
&& b.piece_material[weaker] == e.PIECE_VALUE[BISHOP]) result /= 2;
if (b.piece_material[stronger] == e.PIECE_VALUE[ROOK]
&& b.piece_material[weaker] == e.PIECE_VALUE[KNIGHT]) result /= 2;
if (b.piece_material[stronger] == e.PIECE_VALUE[ROOK] + e.PIECE_VALUE[BISHOP]
&& b.piece_material[weaker] == e.PIECE_VALUE[ROOK]) result /= 2;
if (b.piece_material[stronger] == e.PIECE_VALUE[ROOK] + e.PIECE_VALUE[KNIGHT]
&& b.piece_material[weaker] == e.PIECE_VALUE[ROOK]) result /= 2;
}
/**************************************************************************
* Finally return the score relative to the side to move. *
**************************************************************************/
if ( b.stm == BLACK ) result = -result;
tteval_save(result);
return result;
}
void EvalKnight(S8 sq, S8 side) {
int att = 0;
int mob = 0;
int pos;
/**************************************************************************
* Collect data about mobility and king attacks. This resembles move *
* generation code, except that we are just incrementing the counters *
* instead of adding actual moves. *
**************************************************************************/
for (U8 dir=0; dir<8; dir++) {
pos = sq + vector[KNIGHT][dir];
if ( IS_SQ(pos) && b.color[pos] != side ) {
// we exclude mobility to squares controlled by enemy pawns
// but don't penalize possible captures
if (!b.pawn_ctrl[!side][pos]) ++mob;
if ( e.sqNearK[!side] [b.king_loc[!side] ] [pos] )
++att; // this knight is attacking zone around enemy king
}
}
/**************************************************************************
* Evaluate mobility. We try to do it in such a way that zero represents *
* average mobility, but our formula of doing so is a puer guess. *
**************************************************************************/
v.mgMob[side] += 4 * (mob-4);
v.egMob[side] += 4 * (mob-4);
/**************************************************************************
* Save data about king attacks *
**************************************************************************/
if (att) {
v.attCnt[side]++;
v.attWeight[side] += 2 * att;
}
/**************************************************************************
* Evaluate king tropism *
**************************************************************************/
int tropism = getTropism(sq, b.king_loc[!side]);
v.mgTropism[side] += 3 * tropism;
v.egTropism[side] += 3 * tropism;
}
void EvalBishop(S8 sq, S8 side) {
int att = 0;
int mob = 0;
/**************************************************************************
* Collect data about mobility and king attacks *
**************************************************************************/
for (char dir=0; dir<vectors[BISHOP]; dir++) {
for (char pos = sq;;) {
pos = pos + vector[BISHOP][dir];
if (! IS_SQ(pos)) break;
if (b.pieces[pos] == PIECE_EMPTY) {
if (!b.pawn_ctrl[!side][pos]) mob++;
// we exclude mobility to squares controlled by enemy pawns
if ( e.sqNearK[!side] [b.king_loc[!side] ] [pos] ) ++att;
} else { // non-empty square
if (b.color[pos] != side) { // opponent's piece
mob++;
if (e.sqNearK[!side][b.king_loc[!side]][pos]) ++att;
}
break; // own piece
}
}
}
v.mgMob[side] += 3 * (mob-7);
v.egMob[side] += 3 * (mob-7);
if (att) {
v.attCnt[side]++;
v.attWeight[side] += 2*att;
}
int tropism = getTropism(sq, b.king_loc[!side]);
v.mgTropism[side] += 2 * tropism;
v.egTropism[side] += 1 * tropism;
}
void EvalRook(S8 sq, S8 side) {
int att = 0;
int mob = 0;
/**************************************************************************
* Bonus for rook on the seventh rank. It is applied when there are pawns *
* to attack along that rank or if enemy king is cut off on 8th rank *
/*************************************************************************/
if (ROW(sq) == seventh[side]
&& (b.pawns_on_rank[!side][seventh[side]] || ROW(b.king_loc[!side]) == eighth[side])) {
v.mgMob[side] += 20;
v.egMob[side] += 30;
}
/**************************************************************************
* Bonus for open and half-open files is merged with mobility score. *
* Bonus for open files targetting enemy king is added to attWeight[] *
/*************************************************************************/
if (b.pawns_on_file[side][COL(sq)] == 0) {
if (b.pawns_on_file[!side][COL(sq)] == 0) { // fully open file
v.mgMob[side] += e.ROOK_OPEN;
v.egMob[side] += e.ROOK_OPEN;
if (abs(COL(sq) - COL(b.king_loc[!side])) < 2)
v.attWeight[side] += 1;
} else { // half open file
v.mgMob[side] += e.ROOK_HALF;
v.egMob[side] += e.ROOK_HALF;
if (abs(COL(sq) - COL(b.king_loc[!side])) < 2)
v.attWeight[side] += 2;
}
}
/**************************************************************************
* Collect data about mobility and king attacks *
**************************************************************************/
for (char dir=0; dir<vectors[ROOK]; dir++) {
for (char pos = sq;;) {
pos = pos + vector[ROOK][dir];
if (! IS_SQ(pos)) break;
if (b.pieces[pos] == PIECE_EMPTY) {
mob++;
if ( e.sqNearK[!side] [b.king_loc[!side] ] [pos] ) ++att;
} else { // non-empty square
if (b.color[pos] != side) { // opponent's piece
mob++;
if (e.sqNearK[!side][b.king_loc[!side]][pos]) ++att;
}
break; // own piece
}
}
}
v.mgMob[side] += 2 * (mob-7);
v.egMob[side] += 4 * (mob-7);
if (att) {
v.attCnt[side]++;
v.attWeight[side] += 3*att;
}
int tropism = getTropism(sq, b.king_loc[!side]);
v.mgTropism[side] += 2 * tropism;
v.egTropism[side] += 1 * tropism;
}
void EvalQueen(S8 sq, S8 side) {
int att = 0;
int mob = 0;
if (ROW(sq) == seventh[side]
&& (b.pawns_on_rank[!side][seventh[side]] || ROW(b.king_loc[!side]) == eighth[side])) {
v.mgMob[side] += 5;
v.egMob[side] += 10;
}
/**************************************************************************
* A queen should not be developed too early *
**************************************************************************/
if ((side == WHITE && ROW(sq) > ROW_2) || (side == BLACK && ROW(sq) < ROW_7)) {
if (isPiece(side, KNIGHT, REL_SQ(side,B1))) v.positionalThemes[side] -= 2;
if (isPiece(side, BISHOP, REL_SQ(side,C1))) v.positionalThemes[side] -= 2;
if (isPiece(side, BISHOP, REL_SQ(side,F1))) v.positionalThemes[side] -= 2;
if (isPiece(side, KNIGHT, REL_SQ(side,G1))) v.positionalThemes[side] -= 2;
}
/**************************************************************************
* Collect data about mobility and king attacks *
**************************************************************************/
for (char dir=0; dir<vectors[QUEEN]; dir++) {
for (char pos = sq;;) {
pos = pos + vector[QUEEN][dir];
if (! IS_SQ(pos)) break;
if (b.pieces[pos] == PIECE_EMPTY) {
mob++;
if ( e.sqNearK[!side] [b.king_loc[!side] ] [pos] ) ++att;
} else { // non-empty square
if (b.color[pos] != side) { // opponent's piece
mob++;
if (e.sqNearK[!side][b.king_loc[!side]][pos]) ++att;
}
break; // own piece
}
}
}
v.mgMob[side] += 1 * (mob-14);
v.egMob[side] += 2 * (mob-14);
if (att) {
v.attCnt[side]++;
v.attWeight[side] += 4*att;
}
int tropism = getTropism(sq, b.king_loc[!side]);
v.mgTropism[side] += 2 * tropism;
v.egTropism[side] += 4 * tropism;
}
int wKingShield() {
int result = 0;
/* king on the kingside */
if ( COL(b.king_loc[WHITE]) > COL_E ) {
if ( isPiece(WHITE, PAWN, F2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, F3) ) result += e.SHIELD_3;
if ( isPiece(WHITE, PAWN, G2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, G3) ) result += e.SHIELD_3;
if ( isPiece(WHITE, PAWN, H2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, H3) ) result += e.SHIELD_3;
}
/* king on the queenside */
else if ( COL(b.king_loc[WHITE]) < COL_D ) {
if ( isPiece(WHITE, PAWN, A2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, A3) ) result += e.SHIELD_3;
if ( isPiece(WHITE, PAWN, B2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, B3) ) result += e.SHIELD_3;
if ( isPiece(WHITE, PAWN, C2) ) result += e.SHIELD_2;
else if ( isPiece(WHITE, PAWN, C3) ) result += e.SHIELD_3;
}
return result;
}
int bKingShield() {
int result = 0;
/* king on the kingside */
if ( COL(b.king_loc[BLACK]) > COL_E ) {
if ( isPiece(BLACK, PAWN, F7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, F6) ) result += e.SHIELD_3;
if ( isPiece(BLACK, PAWN, G7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, G6) ) result += e.SHIELD_3;
if ( isPiece(BLACK, PAWN, H7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, H6) ) result += e.SHIELD_3;
}
/* king on the queenside */
else if ( COL(b.king_loc[BLACK]) < COL_D ) {
if ( isPiece(BLACK, PAWN, A7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, A6) ) result += e.SHIELD_3;
if ( isPiece(BLACK, PAWN, B7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, B6) ) result += e.SHIELD_3;
if ( isPiece(BLACK, PAWN, C7) ) result += e.SHIELD_2;
else if ( isPiece(BLACK, PAWN, C6) ) result += e.SHIELD_3;
}
return result;
}
/******************************************************************************
* Pawn structure evaluaton *
******************************************************************************/
int getPawnScore() {
int result;
/**************************************************************************
* This function wraps hashing mechanism around evalPawnStructure(). *
* Please note that since we use the pawn hashtable, evalPawnStructure() *
* must not take into account the piece position. In a more elaborate *
* program, pawn hashtable would contain only the characteristics of pawn *
* structure, and scoring them in conjunction with the piece position *
* would have been done elsewhere. *
**************************************************************************/
int probeval = ttpawn_probe();
if (probeval != INVALID)
return probeval;
result = evalPawnStructure();
ttpawn_save(result);
return result;
}
int evalPawnStructure() {
int result = 0;
for (U8 row = 0; row < 8; row++)
for (U8 col = 0; col < 8; col++) {
S8 sq = SET_SQ(row, col);
if (b.pieces[sq] == PAWN) {
if (b.color[sq] == WHITE) result += EvalPawn(sq, WHITE);
else result -= EvalPawn(sq, BLACK);
}
}
return result;
}
int EvalPawn(S8 sq, S8 side) {
int result = 0;
int flagIsPassed = 1; // we will be trying to disprove that
int flagIsWeak = 1; // we will be trying to disprove that
int flagIsOpposed = 0;
/**************************************************************************
* We have only very basic data structures that do not update informa- *
* tion about pawns incrementally, so we have to calculate everything *
* here. The loop below detects doubled pawns, passed pawns and sets *
* a flag on finding that our pawn is opposed by enemy pawn. *
**************************************************************************/
if (b.pawn_ctrl[!side][sq]) // if a pawn is attacked by a pawn, it is not
flagIsPassed = 0; // passed (not sure if it's the best decision)
S8 nextSq = sq + stepFwd[side];
while (IS_SQ(nextSq)) {
if (b.pieces[nextSq] == PAWN) { // either opposed by enemy pawn or doubled
flagIsPassed = 0;
if (b.color[nextSq] == side)
result -= 20; // doubled pawn penalty
else
flagIsOpposed = 1; // flag our pawn as opposed
}
if (b.pawn_ctrl[!side][nextSq])
flagIsPassed = 0;
nextSq += stepFwd[side];
}
/**************************************************************************
* Another loop, going backwards and checking whether pawn has support. *
* Here we can at least break out of it for speed optimization. *
**************************************************************************/
nextSq = sq+stepFwd[side]; // so that a pawn in a duo will not be considered weak
while (IS_SQ(nextSq)) {
if (b.pawn_ctrl[side][nextSq]) {
flagIsWeak = 0;
break;
}
nextSq += stepBck[side];
}
/**************************************************************************
* Evaluate passed pawns, scoring them higher if they are protected *
* or if their advance is supported by friendly pawns *
**************************************************************************/
if ( flagIsPassed ) {
if ( isPawnSupported(sq, side) ) result += e.protected_passer[side][sq];
else result += e.passed_pawn[side][sq];
}
/**************************************************************************
* Evaluate weak pawns, increasing the penalty if they are situated *
* on a half-open file *
**************************************************************************/
if ( flagIsWeak ) {
result += e.weak_pawn[side][sq];
if (!flagIsOpposed)
result -= 4;
}
return result;
}
int isPawnSupported(S8 sq, S8 side) {
int step;
if (side == WHITE) step = SOUTH;
else step = NORTH;
if ( IS_SQ(sq+WEST) && isPiece(side,PAWN, sq + WEST) ) return 1;
if ( IS_SQ(sq+EAST) && isPiece(side,PAWN, sq + EAST) ) return 1;
if ( IS_SQ(sq+step+WEST) && isPiece(side,PAWN, sq + step+WEST ) ) return 1;
if ( IS_SQ(sq+step+EAST) && isPiece(side,PAWN, sq + step+EAST ) ) return 1;
return 0;
}
/******************************************************************************
* Pattern detection *
******************************************************************************/
void blockedPieces(int side) {
int oppo = !side;
// central pawn blocked, bishop hard to develop
if (isPiece(side, BISHOP, REL_SQ(side,C1))
&& isPiece(side, PAWN, REL_SQ(side,D2))
&& b.color[REL_SQ(side,D3)] != COLOR_EMPTY)
v.blockages[side] -= e.P_BLOCK_CENTRAL_PAWN;
if (isPiece(side, BISHOP, REL_SQ(side,F1))
&& isPiece(side, PAWN, REL_SQ(side,E2))
&& b.color[REL_SQ(side,E3)] != COLOR_EMPTY)
v.blockages[side] -= e.P_BLOCK_CENTRAL_PAWN;
// trapped knight
if (isPiece(side, KNIGHT, REL_SQ(side,A8) )
&& (isPiece(oppo, PAWN, REL_SQ(side,A7) ) || isPiece(oppo, PAWN, REL_SQ(side,C7))))
v.blockages[side] -= e.P_KNIGHT_TRAPPED_A8;
if (isPiece(side, KNIGHT, REL_SQ(side,H8))
&& (isPiece(oppo, PAWN, REL_SQ(side,H7)) || isPiece(oppo, PAWN, REL_SQ(side,F7))))
v.blockages[side] -= e.P_KNIGHT_TRAPPED_A8;
if (isPiece(side, KNIGHT, REL_SQ(side, A7))
&& isPiece(oppo, PAWN, REL_SQ(side,A6))
&& isPiece(oppo, PAWN, REL_SQ(side,B7)))
v.blockages[side] -= e.P_KNIGHT_TRAPPED_A7;
if (isPiece(side, KNIGHT, REL_SQ(side, H7))
&& isPiece (oppo, PAWN, REL_SQ(side, H6))
&& isPiece (oppo, PAWN, REL_SQ(side, G7)))
v.blockages[side] -= e.P_KNIGHT_TRAPPED_A7;
// knight blocking queenside pawns
if (isPiece(side, KNIGHT, REL_SQ(side, C3))
&& isPiece(side, PAWN, REL_SQ(side, C2))
&& isPiece(side, PAWN, REL_SQ(side, D4))
&& !isPiece(side, PAWN, REL_SQ(side, E4)) )
v.blockages[side] -= e.P_C3_KNIGHT;
// trapped bishop
if (isPiece(side, BISHOP, REL_SQ(side,A7))
&& isPiece(oppo, PAWN, REL_SQ(side,B6)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A7;
if (isPiece(side, BISHOP, REL_SQ(side, H7))
&& isPiece(oppo, PAWN, REL_SQ(side, G6)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A7;
if (isPiece(side, BISHOP, REL_SQ(side, B8))
&& isPiece(oppo, PAWN, REL_SQ(side, C7)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A7;
if (isPiece(side, BISHOP, REL_SQ(side, G8))
&& isPiece(oppo, PAWN, REL_SQ(side, F7)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A7;
if (isPiece(side, BISHOP, REL_SQ(side, A6))
&& isPiece(oppo, PAWN, REL_SQ(side, B5)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A6;
if (isPiece(side, BISHOP, REL_SQ(side, H6))
&& isPiece(oppo, PAWN, REL_SQ(side, G5)))
v.blockages[side] -= e.P_BISHOP_TRAPPED_A6;
// bishop on initial sqare supporting castled king
if (isPiece(side, BISHOP, REL_SQ(side, F1))
&& isPiece(side, KING, REL_SQ(side, G1)))
v.positionalThemes[side] += e.RETURNING_BISHOP;
if (isPiece(side, BISHOP, REL_SQ(side, C1))
&& isPiece(side, KING, REL_SQ(side, B1)))
v.positionalThemes[side] += e.RETURNING_BISHOP;
// uncastled king blocking own rook
if ( ( isPiece(side, KING, REL_SQ(side,F1)) || isPiece(side, KING, REL_SQ(side,G1) ) )
&& ( isPiece(side, ROOK, REL_SQ(side,H1)) || isPiece(side, ROOK, REL_SQ(side,G1) ) ) )
v.blockages[side] -= e.P_KING_BLOCKS_ROOK;
if ((isPiece(side, KING, REL_SQ(side,C1)) || isPiece(side, KING, REL_SQ(side,B1)))
&& (isPiece(side, ROOK, REL_SQ(side,A1)) || isPiece(side, ROOK, REL_SQ(side,B1))) )
v.blockages[side] -= e.P_KING_BLOCKS_ROOK;
}
int isPiece(U8 color, U8 piece, S8 sq) {
return ( (b.pieces[sq] == piece) && (b.color[sq] == color) );
}
/******************************************************************************
* Printing eval results *
******************************************************************************/
void printEval() {
printf("------------------------------------------\n");
printf("Total value (for side to move): %d \n", eval(-INF,INF, 0) );
printf("Material balance : %d \n", b.piece_material[WHITE] + b.pawn_material[WHITE] - b.piece_material[BLACK] - b.pawn_material[BLACK] );
printf("Material adjustement : ");
printEvalFactor(v.adjustMaterial[WHITE], v.adjustMaterial[BLACK]);
printf("Mg Piece/square tables : ");
printEvalFactor(b.pcsq_mg[WHITE], b.pcsq_mg[BLACK]);
printf("Eg Piece/square tables : ");
printEvalFactor(b.pcsq_eg[WHITE], b.pcsq_eg[BLACK]);
printf("Mg Mobility : ");
printEvalFactor(v.mgMob[WHITE], v.mgMob[BLACK]);
printf("Eg Mobility : ");
printEvalFactor(v.egMob[WHITE], v.egMob[BLACK]);
printf("Mg Tropism : ");
printEvalFactor(v.mgTropism[WHITE], v.mgTropism[BLACK]);
printf("Eg Tropism : ");
printEvalFactor(v.egTropism[WHITE], v.egTropism[BLACK]);
printf("Pawn structure : %d \n", evalPawnStructure() );
printf("Blockages : ");
printEvalFactor(v.blockages[WHITE], v.blockages[BLACK]);
printf("Positional themes : ");
printEvalFactor(v.positionalThemes[WHITE], v.positionalThemes[BLACK]);
printf("King Shield : ");
printEvalFactor(v.kingShield[WHITE], v.kingShield[BLACK]);
printf("Tempo : ");
if ( b.stm == WHITE ) printf("%d", e.TEMPO);
else printf("%d", -e.TEMPO);
printf("\n");
printf("------------------------------------------\n");
}
void printEvalFactor(int wh, int bl) {
printf("white %4d, black %4d, total: %4d \n", wh, bl, wh - bl);
}
int getTropism(int sq1, int sq2) {
return 7 - (abs(ROW(sq1) - ROW(sq2)) + abs(COL(sq1) - COL(sq2)));
}