]> git.sesse.net Git - stockfish/blob - src/material.h
Silence a MSVC warning in class Tie
[stockfish] / src / material.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #if !defined(MATERIAL_H_INCLUDED)
21 #define MATERIAL_H_INCLUDED
22
23 #include "endgame.h"
24 #include "misc.h"
25 #include "position.h"
26 #include "types.h"
27
28 const int MaterialTableSize = 8192;
29
30 /// Game phase
31 enum Phase {
32   PHASE_ENDGAME = 0,
33   PHASE_MIDGAME = 128
34 };
35
36
37 /// MaterialEntry is a class which contains various information about a
38 /// material configuration. It contains a material balance evaluation,
39 /// a function pointer to a special endgame evaluation function (which in
40 /// most cases is NULL, meaning that the standard evaluation function will
41 /// be used), and "scale factors" for black and white.
42 ///
43 /// The scale factors are used to scale the evaluation score up or down.
44 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
45 /// of 4, which will result in scores of absolute value less than one pawn.
46
47 class MaterialEntry {
48
49   friend struct MaterialTable;
50
51 public:
52   Score material_value() const;
53   ScaleFactor scale_factor(const Position& pos, Color c) const;
54   int space_weight() const;
55   Phase game_phase() const;
56   bool specialized_eval_exists() const;
57   Value evaluate(const Position& pos) const;
58
59 private:
60   Key key;
61   int16_t value;
62   uint8_t factor[2];
63   EndgameBase<Value>* evaluationFunction;
64   EndgameBase<ScaleFactor>* scalingFunction[2];
65   int spaceWeight;
66   Phase gamePhase;
67 };
68
69
70 /// The MaterialTable class represents a material hash table. The most important
71 /// method is probe(), which returns a pointer to a MaterialEntry object.
72
73 struct MaterialTable {
74
75   MaterialEntry* probe(const Position& pos);
76   static Phase game_phase(const Position& pos);
77   template<Color Us> static int imbalance(const int pieceCount[][8]);
78
79   HashTable<MaterialEntry, MaterialTableSize> entries;
80   Endgames endgames;
81 };
82
83
84 /// MaterialEntry::scale_factor takes a position and a color as input, and
85 /// returns a scale factor for the given color. We have to provide the
86 /// position in addition to the color, because the scale factor need not
87 /// to be a constant: It can also be a function which should be applied to
88 /// the position. For instance, in KBP vs K endgames, a scaling function
89 /// which checks for draws with rook pawns and wrong-colored bishops.
90
91 inline ScaleFactor MaterialEntry::scale_factor(const Position& pos, Color c) const {
92
93   if (!scalingFunction[c])
94       return ScaleFactor(factor[c]);
95
96   ScaleFactor sf = (*scalingFunction[c])(pos);
97   return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
98 }
99
100 inline Value MaterialEntry::evaluate(const Position& pos) const {
101   return (*evaluationFunction)(pos);
102 }
103
104 inline Score MaterialEntry::material_value() const {
105   return make_score(value, value);
106 }
107
108 inline int MaterialEntry::space_weight() const {
109   return spaceWeight;
110 }
111
112 inline Phase MaterialEntry::game_phase() const {
113   return gamePhase;
114 }
115
116 inline bool MaterialEntry::specialized_eval_exists() const {
117   return evaluationFunction != NULL;
118 }
119
120 #endif // !defined(MATERIAL_H_INCLUDED)