2424
2525#include " bitcount.h"
2626#include " evaluate.h"
27+ #include " material.h"
28+ #include " pawns.h"
2729#include " thread.h"
2830#include " ucioption.h"
2931
30- using namespace Eval ;
31-
3232namespace {
3333
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+
3474 // Evaluation grain size, must be a power of 2
3575 const int GrainSize = 8 ;
3676
@@ -200,27 +240,27 @@ namespace {
200240
201241 // Function prototypes
202242 template <bool Trace>
203- Value do_evaluate (const Position& pos, Value& margin, Info& ei );
243+ Value do_evaluate (const Position& pos, Value& margin);
204244
205245 template <Color Us>
206- void init_eval_info (const Position& pos, Info & ei);
246+ void init_eval_info (const Position& pos, EvalInfo & ei);
207247
208248 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);
210250
211251 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[]);
213253
214254 template <Color Us>
215- Score evaluate_threats (const Position& pos, Info & ei);
255+ Score evaluate_threats (const Position& pos, EvalInfo & ei);
216256
217257 template <Color Us>
218- int evaluate_space (const Position& pos, Info & ei);
258+ int evaluate_space (const Position& pos, EvalInfo & ei);
219259
220260 template <Color Us>
221- Score evaluate_passed_pawns (const Position& pos, Info & ei);
261+ Score evaluate_passed_pawns (const Position& pos, EvalInfo & ei);
222262
223- Score evaluate_unstoppable_pawns (const Position& pos, Info & ei);
263+ Score evaluate_unstoppable_pawns (const Position& pos, EvalInfo & ei);
224264
225265 Value interpolate (const Score& v, Phase ph, ScaleFactor sf);
226266 Score weight_option (const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
@@ -236,8 +276,8 @@ namespace Eval {
236276 // / values, an endgame score and a middle game score, and interpolates
237277 // / between them based on the remaining material.
238278
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);
241281 }
242282
243283
@@ -273,15 +313,14 @@ namespace Eval {
273313
274314 Value margin;
275315 std::string totals;
276- Info ei;
277316
278317 Search::RootColor = pos.side_to_move ();
279318
280319 TraceStream.str (" " );
281320 TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision (2 );
282321 memset (TracedScores, 0 , 2 * 16 * sizeof (Score));
283322
284- do_evaluate<true >(pos, margin, ei );
323+ do_evaluate<true >(pos, margin);
285324
286325 totals = TraceStream.str ();
287326 TraceStream.str (" " );
@@ -317,10 +356,11 @@ namespace Eval {
317356namespace {
318357
319358template <bool Trace>
320- Value do_evaluate (const Position& pos, Value& margin, Info& ei ) {
359+ Value do_evaluate (const Position& pos, Value& margin) {
321360
322361 assert (!pos.checkers ());
323362
363+ EvalInfo ei;
324364 Value margins[COLOR_NB];
325365 Score score, mobilityWhite, mobilityBlack;
326366 Thread* th = pos.this_thread ();
@@ -443,7 +483,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
443483 // pawn attacks. To be done at the beginning of the evaluation.
444484
445485 template <Color Us>
446- void init_eval_info (const Position& pos, Info & ei) {
486+ void init_eval_info (const Position& pos, EvalInfo & ei) {
447487
448488 const Color Them = (Us == WHITE ? BLACK : WHITE);
449489
@@ -466,7 +506,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
466506 // evaluate_outposts() evaluates bishop and knight outposts squares
467507
468508 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) {
470510
471511 const Color Them = (Us == WHITE ? BLACK : WHITE);
472512
@@ -492,7 +532,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
492532 // evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
493533
494534 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) {
496536
497537 Bitboard b;
498538 Square s, ksq;
@@ -641,7 +681,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
641681 // and the type of attacked one.
642682
643683 template <Color Us>
644- Score evaluate_threats (const Position& pos, Info & ei) {
684+ Score evaluate_threats (const Position& pos, EvalInfo & ei) {
645685
646686 const Color Them = (Us == WHITE ? BLACK : WHITE);
647687
@@ -683,7 +723,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
683723 // pieces of a given color.
684724
685725 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) {
687727
688728 const Color Them = (Us == WHITE ? BLACK : WHITE);
689729
@@ -708,7 +748,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
708748 // evaluate_king<>() assigns bonuses and penalties to a king of a given color
709749
710750 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[]) {
712752
713753 const Color Them = (Us == WHITE ? BLACK : WHITE);
714754
@@ -821,7 +861,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
821861 // evaluate_passed_pawns<>() evaluates the passed pawns of the given color
822862
823863 template <Color Us>
824- Score evaluate_passed_pawns (const Position& pos, Info & ei) {
864+ Score evaluate_passed_pawns (const Position& pos, EvalInfo & ei) {
825865
826866 const Color Them = (Us == WHITE ? BLACK : WHITE);
827867
@@ -919,7 +959,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
919959 // evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides, this is quite
920960 // conservative and returns a winning score only when we are very sure that the pawn is winning.
921961
922- Score evaluate_unstoppable_pawns (const Position& pos, Info & ei) {
962+ Score evaluate_unstoppable_pawns (const Position& pos, EvalInfo & ei) {
923963
924964 Bitboard b, b2, blockers, supporters, queeningPath, candidates;
925965 Square s, blockSq, queeningSquare;
@@ -1084,7 +1124,7 @@ Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
10841124 // twice. Finally, the space bonus is scaled by a weight taken from the
10851125 // material hash table. The aim is to improve play on game opening.
10861126 template <Color Us>
1087- int evaluate_space (const Position& pos, Info & ei) {
1127+ int evaluate_space (const Position& pos, EvalInfo & ei) {
10881128
10891129 const Color Them = (Us == WHITE ? BLACK : WHITE);
10901130
0 commit comments