]> git.sesse.net Git - stockfish/blob - src/material.h
Remove unnecessary conditions from if-clauses and replace them with asserts
[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-2009 Marco Costalba
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
21 #if !defined(MATERIAL_H_INCLUDED)
22 #define MATERIAL_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "endgame.h"
29 #include "position.h"
30 #include "scale.h"
31
32
33 ////
34 //// Types
35 ////
36
37 /// MaterialInfo 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 MaterialInfo {
48
49   friend class MaterialInfoTable;
50
51 public:
52   MaterialInfo() : key(0) { clear(); }
53
54   Score material_value() const;
55   ScaleFactor scale_factor(const Position& pos, Color c) const;
56   int space_weight() const;
57   Phase game_phase() const;
58   bool specialized_eval_exists() const;
59   Value evaluate(const Position& pos) const;
60
61 private:
62   inline void clear();
63
64   Key key;
65   int16_t value;
66   uint8_t factor[2];
67   EndgameEvaluationFunctionBase* evaluationFunction;
68   EndgameScalingFunctionBase* scalingFunction[2];
69   int spaceWeight;
70   Phase gamePhase;
71 };
72
73 /// The MaterialInfoTable class represents a pawn hash table. It is basically
74 /// just an array of MaterialInfo objects and a few methods for accessing these
75 /// objects. The most important method is get_material_info, which looks up a
76 /// position in the table and returns a pointer to a MaterialInfo object.
77 class EndgameFunctions;
78
79 class MaterialInfoTable {
80
81 public:
82   MaterialInfoTable(unsigned numOfEntries);
83   ~MaterialInfoTable();
84   MaterialInfo* get_material_info(const Position& pos);
85
86   static Phase game_phase(const Position& pos);
87
88 private:
89   unsigned size;
90   MaterialInfo* entries;
91   EndgameFunctions* funcs;
92 };
93
94
95 ////
96 //// Inline functions
97 ////
98
99
100 /// MaterialInfo::material_value simply returns the material balance
101 /// evaluation that is independent from game phase.
102
103 inline Score MaterialInfo::material_value() const {
104
105   return make_score(value, value);
106 }
107
108
109 /// MaterialInfo::clear() resets a MaterialInfo object to an empty state,
110 /// with all slots at their default values but the key.
111
112 inline void MaterialInfo::clear() {
113
114   value = 0;
115   factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
116   evaluationFunction = NULL;
117   scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
118   spaceWeight = 0;
119 }
120
121
122 /// MaterialInfo::scale_factor takes a position and a color as input, and
123 /// returns a scale factor for the given color. We have to provide the
124 /// position in addition to the color, because the scale factor need not
125 /// to be a constant: It can also be a function which should be applied to
126 /// the position. For instance, in KBP vs K endgames, a scaling function
127 /// which checks for draws with rook pawns and wrong-colored bishops.
128
129 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
130
131   if (scalingFunction[c] != NULL)
132   {
133       ScaleFactor sf = scalingFunction[c]->apply(pos);
134       if (sf != SCALE_FACTOR_NONE)
135           return sf;
136   }
137   return ScaleFactor(factor[c]);
138 }
139
140
141 /// MaterialInfo::space_weight() simply returns the weight for the space
142 /// evaluation for this material configuration.
143
144 inline int MaterialInfo::space_weight() const {
145
146   return spaceWeight;
147 }
148
149 /// MaterialInfo::game_phase() returns the game phase according
150 /// to this material configuration.
151
152 inline Phase MaterialInfo::game_phase() const {
153
154   return gamePhase;
155 }
156
157
158 /// MaterialInfo::specialized_eval_exists decides whether there is a
159 /// specialized evaluation function for the current material configuration,
160 /// or if the normal evaluation function should be used.
161
162 inline bool MaterialInfo::specialized_eval_exists() const {
163
164   return evaluationFunction != NULL;
165 }
166
167
168 /// MaterialInfo::evaluate applies a specialized evaluation function
169 /// to a given position object. It should only be called when
170 /// specialized_eval_exists() returns 'true'.
171
172 inline Value MaterialInfo::evaluate(const Position& pos) const {
173
174   return evaluationFunction->apply(pos);
175 }
176
177 #endif // !defined(MATERIAL_H_INCLUDED)