]> git.sesse.net Git - stockfish/blobdiff - src/material.cpp
Move game phase computation to MaterialInfo
[stockfish] / src / material.cpp
index a3d9f7c8a73958ca7c8235e6f62c81ec85479d58..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.