]> git.sesse.net Git - stockfish/blob - src/material.h
7d5c3ce1e3f5c215fe27d660ea011e816addff2a
[stockfish] / src / material.h
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9   
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14   
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 #if !defined(MATERIAL_H_INCLUDED)
21 #define MATERIAL_H_INCLUDED
22
23 ////
24 //// Includes
25 ////
26
27 #include "endgame.h"
28 #include "position.h"
29 #include "scale.h"
30
31
32 ////
33 //// Types
34 ////
35
36 /// MaterialInfo is a class which contains various information about a
37 /// material configuration.  It contains a material balance evaluation,
38 /// a function pointer to a special endgame evaluation function (which in
39 /// most cases is NULL, meaning that the standard evaluation function will
40 /// be used), and "scale factors" for black and white.
41 ///
42 /// The scale factors are used to scale the evaluation score up or down.
43 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
44 /// of 4, which will result in scores of absolute value less than one pawn.
45
46 class MaterialInfo {
47
48   friend class MaterialInfoTable;
49
50 public:
51   Value mg_value() const;
52   Value eg_value() const;
53   ScaleFactor scale_factor(const Position &pos, Color c) const;
54   bool specialized_eval_exists() const;
55   Value evaluate(const Position &pos) const;
56
57   static void init();
58
59 private:
60   void clear();
61   
62   Key key;
63   int16_t mgValue;
64   int16_t egValue;
65   uint8_t factor[2];
66   EndgameEvaluationFunction *evaluationFunction;
67   ScalingFunction *scalingFunction[2];
68 };
69
70
71 /// The MaterialInfoTable class represents a pawn hash table.  It is basically
72 /// just an array of MaterialInfo objects and a few methods for accessing these
73 /// objects.  The most important method is get_material_info, which looks up a
74 /// position in the table and returns a pointer to a MaterialInfo object.
75
76 class MaterialInfoTable {
77
78 public:
79   MaterialInfoTable(unsigned numOfEntries);
80   ~MaterialInfoTable();
81   void clear();
82   MaterialInfo *get_material_info(const Position &pos);
83
84 private:
85   unsigned size;
86   MaterialInfo *entries;
87 };
88
89
90 ////
91 //// Inline functions
92 ////
93
94 /// MaterialInfo::mg_value and MaterialInfo::eg_value simply returns the
95 /// material balance evaluation for the middle game and the endgame.
96
97 inline Value MaterialInfo::mg_value() const {
98   return Value(mgValue);
99 }
100
101 inline Value MaterialInfo::eg_value() const {
102   return Value(egValue);
103 }
104
105
106 /// MaterialInfo::clear() resets a MaterialInfo object to an empty state,
107 /// with all slots at their default values.
108
109 inline void MaterialInfo::clear() {
110   mgValue = egValue = 0;
111   factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
112   evaluationFunction = NULL;
113   scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
114 }
115
116
117 /// MaterialInfo::scale_factor takes a position and a color as input, and
118 /// returns a scale factor for the given color.  We have to provide the
119 /// position in addition to the color, because the scale factor need not
120 /// be a constant:  It can also be a function which should be applied to
121 /// the position.  For instance, in KBP vs K endgames, a scaling function
122 /// which checks for draws with rook pawns and wrong-colored bishops.
123
124 inline ScaleFactor MaterialInfo::scale_factor(const Position &pos, Color c)
125   const {
126   if(scalingFunction[c] != NULL) {
127     ScaleFactor sf = scalingFunction[c]->apply(pos);
128     if(sf != SCALE_FACTOR_NONE)
129       return sf;
130   }
131   return ScaleFactor(factor[c]);
132 }
133
134
135 /// MaterialInfo::specialized_eval_exists decides whether there is a
136 /// specialized evaluation function for the current material configuration,
137 /// or if the normal evaluation function should be used.
138
139 inline bool MaterialInfo::specialized_eval_exists() const {
140   return evaluationFunction != NULL;
141 }
142
143
144 /// MaterialInfo::evaluate applies a specialized evaluation function to a
145 /// given position object.  It should only be called when
146 /// this->specialized_eval_exists() returns 'true'.
147
148 inline Value MaterialInfo::evaluate(const Position &pos) const {
149   return evaluationFunction->apply(pos);
150 }
151
152 #endif // !defined(MATERIAL_H_INCLUDED)