24
24
25
25
#include " bitcount.h"
26
26
#include " evaluate.h"
27
+ #include " material.h"
28
+ #include " pawns.h"
27
29
#include " thread.h"
28
30
#include " ucioption.h"
29
31
30
- using namespace Eval ;
31
-
32
32
namespace {
33
33
34
+ // Struct EvalInfo contains various information computed and collected
35
+ // by the evaluation functions.
36
+ struct EvalInfo {
37
+
38
+ // Pointers to material and pawn hash table entries
39
+ Material::Entry* mi;
40
+ Pawns::Entry* pi;
41
+
42
+ // attackedBy[color][piece type] is a bitboard representing all squares
43
+ // attacked by a given color and piece type, attackedBy[color][ALL_PIECES]
44
+ // contains all squares attacked by the given color.
45
+ Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB];
46
+
47
+ // kingRing[color] is the zone around the king which is considered
48
+ // by the king safety evaluation. This consists of the squares directly
49
+ // adjacent to the king, and the three (or two, for a king on an edge file)
50
+ // squares two ranks in front of the king. For instance, if black's king
51
+ // is on g8, kingRing[BLACK] is a bitboard containing the squares f8, h8,
52
+ // f7, g7, h7, f6, g6 and h6.
53
+ Bitboard kingRing[COLOR_NB];
54
+
55
+ // kingAttackersCount[color] is the number of pieces of the given color
56
+ // which attack a square in the kingRing of the enemy king.
57
+ int kingAttackersCount[COLOR_NB];
58
+
59
+ // kingAttackersWeight[color] is the sum of the "weight" of the pieces of the
60
+ // given color which attack a square in the kingRing of the enemy king. The
61
+ // weights of the individual piece types are given by the variables
62
+ // QueenAttackWeight, RookAttackWeight, BishopAttackWeight and
63
+ // KnightAttackWeight in evaluate.cpp
64
+ int kingAttackersWeight[COLOR_NB];
65
+
66
+ // kingAdjacentZoneAttacksCount[color] is the number of attacks to squares
67
+ // directly adjacent to the king of the given color. Pieces which attack
68
+ // more than one square are counted multiple times. For instance, if black's
69
+ // king is on g8 and there's a white knight on g5, this knight adds
70
+ // 2 to kingAdjacentZoneAttacksCount[BLACK].
71
+ int kingAdjacentZoneAttacksCount[COLOR_NB];
72
+ };
73
+
34
74
// Evaluation grain size, must be a power of 2
35
75
const int GrainSize = 8 ;
36
76
@@ -200,27 +240,27 @@ namespace {
200
240
201
241
// Function prototypes
202
242
template <bool Trace>
203
- Value do_evaluate (const Position& pos, Value& margin, Info& ei );
243
+ Value do_evaluate (const Position& pos, Value& margin);
204
244
205
245
template <Color Us>
206
- void init_eval_info (const Position& pos, Info & ei);
246
+ void init_eval_info (const Position& pos, EvalInfo & ei);
207
247
208
248
template <Color Us, bool Trace>
209
- Score evaluate_pieces_of_color (const Position& pos, Info & ei, Score& mobility);
249
+ Score evaluate_pieces_of_color (const Position& pos, EvalInfo & ei, Score& mobility);
210
250
211
251
template <Color Us, bool Trace>
212
- Score evaluate_king (const Position& pos, Info & ei, Value margins[]);
252
+ Score evaluate_king (const Position& pos, EvalInfo & ei, Value margins[]);
213
253
214
254
template <Color Us>
215
- Score evaluate_threats (const Position& pos, Info & ei);
255
+ Score evaluate_threats (const Position& pos, EvalInfo & ei);
216
256
217
257
template <Color Us>
218
- int evaluate_space (const Position& pos, Info & ei);
258
+ int evaluate_space (const Position& pos, EvalInfo & ei);
219
259
220
260
template <Color Us>
221
- Score evaluate_passed_pawns (const Position& pos, Info & ei);
261
+ Score evaluate_passed_pawns (const Position& pos, EvalInfo & ei);
222
262
223
- Score evaluate_unstoppable_pawns (const Position& pos, Info & ei);
263
+ Score evaluate_unstoppable_pawns (const Position& pos, EvalInfo & ei);
224
264
225
265
Value interpolate (const Score& v, Phase ph, ScaleFactor sf);
226
266
Score weight_option (const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
@@ -236,8 +276,8 @@ namespace Eval {
236
276
// / values, an endgame score and a middle game score, and interpolates
237
277
// / between them based on the remaining material.
238
278
239
- Value evaluate (const Position& pos, Value& margin, Info* ei ) {
240
- return do_evaluate<false >(pos, margin, *ei );
279
+ Value evaluate (const Position& pos, Value& margin) {
280
+ return do_evaluate<false >(pos, margin);
241
281
}
242
282
243
283
@@ -273,15 +313,14 @@ namespace Eval {
273
313
274
314
Value margin;
275
315
std::string totals;
276
- Info ei;
277
316
278
317
Search::RootColor = pos.side_to_move ();
279
318
280
319
TraceStream.str (" " );
281
320
TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision (2 );
282
321
memset (TracedScores, 0 , 2 * 16 * sizeof (Score));
283
322
284
- do_evaluate<true >(pos, margin, ei );
323
+ do_evaluate<true >(pos, margin);
285
324
286
325
totals = TraceStream.str ();
287
326
TraceStream.str (" " );
@@ -317,10 +356,11 @@ namespace Eval {
317
356
namespace {
318
357
319
358
template <bool Trace>
320
- Value do_evaluate (const Position& pos, Value& margin, Info& ei ) {
359
+ Value do_evaluate (const Position& pos, Value& margin) {
321
360
322
361
assert (!pos.checkers ());
323
362
363
+ EvalInfo ei;
324
364
Value margins[COLOR_NB];
325
365
Score score, mobilityWhite, mobilityBlack;
326
366
Thread* th = pos.this_thread ();
@@ -443,7 +483,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
443
483
// pawn attacks. To be done at the beginning of the evaluation.
444
484
445
485
template <Color Us>
446
- void init_eval_info (const Position& pos, Info & ei) {
486
+ void init_eval_info (const Position& pos, EvalInfo & ei) {
447
487
448
488
const Color Them = (Us == WHITE ? BLACK : WHITE);
449
489
@@ -466,7 +506,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
466
506
// evaluate_outposts() evaluates bishop and knight outposts squares
467
507
468
508
template <PieceType Piece, Color Us>
469
- Score evaluate_outposts (const Position& pos, Info & ei, Square s) {
509
+ Score evaluate_outposts (const Position& pos, EvalInfo & ei, Square s) {
470
510
471
511
const Color Them = (Us == WHITE ? BLACK : WHITE);
472
512
@@ -492,7 +532,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
492
532
// evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
493
533
494
534
template <PieceType Piece, Color Us, bool Trace>
495
- Score evaluate_pieces (const Position& pos, Info & ei, Score& mobility, Bitboard mobilityArea) {
535
+ Score evaluate_pieces (const Position& pos, EvalInfo & ei, Score& mobility, Bitboard mobilityArea) {
496
536
497
537
Bitboard b;
498
538
Square s, ksq;
@@ -641,7 +681,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
641
681
// and the type of attacked one.
642
682
643
683
template <Color Us>
644
- Score evaluate_threats (const Position& pos, Info & ei) {
684
+ Score evaluate_threats (const Position& pos, EvalInfo & ei) {
645
685
646
686
const Color Them = (Us == WHITE ? BLACK : WHITE);
647
687
@@ -683,7 +723,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
683
723
// pieces of a given color.
684
724
685
725
template <Color Us, bool Trace>
686
- Score evaluate_pieces_of_color (const Position& pos, Info & ei, Score& mobility) {
726
+ Score evaluate_pieces_of_color (const Position& pos, EvalInfo & ei, Score& mobility) {
687
727
688
728
const Color Them = (Us == WHITE ? BLACK : WHITE);
689
729
@@ -708,7 +748,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
708
748
// evaluate_king<>() assigns bonuses and penalties to a king of a given color
709
749
710
750
template <Color Us, bool Trace>
711
- Score evaluate_king (const Position& pos, Info & ei, Value margins[]) {
751
+ Score evaluate_king (const Position& pos, EvalInfo & ei, Value margins[]) {
712
752
713
753
const Color Them = (Us == WHITE ? BLACK : WHITE);
714
754
@@ -821,7 +861,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
821
861
// evaluate_passed_pawns<>() evaluates the passed pawns of the given color
822
862
823
863
template <Color Us>
824
- Score evaluate_passed_pawns (const Position& pos, Info & ei) {
864
+ Score evaluate_passed_pawns (const Position& pos, EvalInfo & ei) {
825
865
826
866
const Color Them = (Us == WHITE ? BLACK : WHITE);
827
867
@@ -919,7 +959,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
919
959
// evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides, this is quite
920
960
// conservative and returns a winning score only when we are very sure that the pawn is winning.
921
961
922
- Score evaluate_unstoppable_pawns (const Position& pos, Info & ei) {
962
+ Score evaluate_unstoppable_pawns (const Position& pos, EvalInfo & ei) {
923
963
924
964
Bitboard b, b2, blockers, supporters, queeningPath, candidates;
925
965
Square s, blockSq, queeningSquare;
@@ -1084,7 +1124,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
1084
1124
// twice. Finally, the space bonus is scaled by a weight taken from the
1085
1125
// material hash table. The aim is to improve play on game opening.
1086
1126
template <Color Us>
1087
- int evaluate_space (const Position& pos, Info & ei) {
1127
+ int evaluate_space (const Position& pos, EvalInfo & ei) {
1088
1128
1089
1129
const Color Them = (Us == WHITE ? BLACK : WHITE);
1090
1130
0 commit comments