]> git.sesse.net Git - stockfish/blobdiff - src/material.cpp
Move game phase computation to MaterialInfo
[stockfish] / src / material.cpp
index b16710588bbed14f4c1a5c7e61cc027b9188cf4c..597d72298902179bef40bb04e4fa11d92ab6cee2 100644 (file)
@@ -37,6 +37,10 @@ using namespace std;
 
 namespace {
 
+  // Values modified by Joona Kiiski
+  const Value MidgameLimit = Value(15581);
+  const Value EndgameLimit = Value(3998);
+
   // Polynomial material balance parameters
   const Value RedundantQueenPenalty = Value(320);
   const Value RedundantRookPenalty  = Value(554);
@@ -129,6 +133,22 @@ MaterialInfoTable::~MaterialInfoTable() {
 }
 
 
+/// MaterialInfoTable::game_phase() calculate the phase given the current
+/// position. Because the phase is strictly a function of the material, it
+/// is stored in MaterialInfo.
+
+Phase MaterialInfoTable::game_phase(const Position& pos) {
+
+  Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
+
+  if (npm >= MidgameLimit)
+      return PHASE_MIDGAME;
+  else if (npm <= EndgameLimit)
+      return PHASE_ENDGAME;
+
+  return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
+}
+
 /// MaterialInfoTable::get_material_info() takes a position object as input,
 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
 /// If the material configuration is not already present in the table, it
@@ -151,6 +171,9 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
   mi->clear();
   mi->key = key;
 
+  // Calculate game phase
+  mi->gamePhase = MaterialInfoTable::game_phase(pos);
+
   // Let's look if we have a specialized evaluation function for this
   // particular material configuration. First we look for a fixed
   // configuration one, then a generic one if previous search failed.
@@ -171,14 +194,14 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
       mi->evaluationFunction = &EvaluateKKX;
       return mi;
   }
-  else if (   pos.pawns() == EmptyBoardBB
-           && pos.rooks() == EmptyBoardBB
-           && pos.queens() == EmptyBoardBB)
+  else if (   pos.pieces(PAWN) == EmptyBoardBB
+           && pos.pieces(ROOK) == EmptyBoardBB
+           && pos.pieces(QUEEN) == EmptyBoardBB)
   {
       // Minor piece endgame with at least one minor piece per side and
       // no pawns. Note that the case KmmK is already handled by KXK.
-      assert(pos.knights(WHITE) | pos.bishops(WHITE));
-      assert(pos.knights(BLACK) | pos.bishops(BLACK));
+      assert((pos.pieces(KNIGHT, WHITE) | pos.pieces(BISHOP, WHITE)));
+      assert((pos.pieces(KNIGHT, BLACK) | pos.pieces(BISHOP, BLACK)));
 
       if (   pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2
           && pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2)