]> git.sesse.net Git - stockfish/blob - src/material.h
21adf964877762b40b793e64c0b8625aaea94f48
[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-2015 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 #ifndef 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 namespace Material {
29
30 /// Material::Entry contains various information about a material configuration.
31 /// It contains a material imbalance evaluation, a function pointer to a special
32 /// endgame evaluation function (which in most cases is NULL, meaning that the
33 /// standard evaluation function will be used), and scale factors.
34 ///
35 /// The scale factors are used to scale the evaluation score up or down. For
36 /// instance, in KRB vs KR endgames, the score is scaled down by a factor of 4,
37 /// which will result in scores of absolute value less than one pawn.
38
39 struct Entry {
40
41   Score imbalance() const { return make_score(value, value); }
42   Phase game_phase() const { return gamePhase; }
43   bool specialized_eval_exists() const { return evaluationFunction != NULL; }
44   Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
45
46   // scale_factor takes a position and a color as input and returns a scale factor
47   // for the given color. We have to provide the position in addition to the color
48   // because the scale factor may also be a function which should be applied to
49   // the position. For instance, in KBP vs K endgames, the scaling function looks
50   // for rook pawns and wrong-colored bishops.
51   ScaleFactor scale_factor(const Position& pos, Color c) const {
52
53     return !scalingFunction[c] || (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE
54           ? ScaleFactor(factor[c]) : (*scalingFunction[c])(pos);
55   }
56
57   Key key;
58   int16_t value;
59   uint8_t factor[COLOR_NB];
60   EndgameBase<Value>* evaluationFunction;
61   EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
62                                                        // side (e.g. KPKP, KBPsKs)
63   Phase gamePhase;
64 };
65
66 typedef HashTable<Entry, 8192> Table;
67
68 Entry* probe(const Position& pos);
69
70 } // namespace Material
71
72 #endif // #ifndef MATERIAL_H_INCLUDED