Make multi-PV mode accessible from XBoard protocol
[bonanza.git] / searchr.c
index 5484e16..f82ee69 100644 (file)
--- a/searchr.c
+++ b/searchr.c
@@ -13,7 +13,7 @@ static int mpv_set_bound( int alpha );
 static int mpv_find_min( int *pnum );
 static int mpv_add_result( tree_t * restrict ptree, int value );
 static void mpv_sub_result( unsigned int move );
-static void mpv_out( tree_t * restrict ptree, int turn, unsigned int time );
+static void mpv_out( tree_t * restrict ptree, int turn, unsigned int time, int newest );
 #endif
 
 #if defined(NO_STDOUT) && defined(NO_LOGGING)
@@ -218,6 +218,21 @@ searchr( tree_t * restrict ptree, int alpha, int beta, int turn, int depth )
 }
 
 
+#ifdef XBOARD
+void
+out_xboard_move( int move, int tt )
+{
+        int from = I2From(move), to = I2To(move);
+        char inPromoZone = tt > 0 ? from >= 6*9 || to >= 6*9 : to < 3*9|| from < 3*9;
+        if(from >= nsquare)
+          Out(" %c@%c%c", "PLNSGBR"[From2Drop(from)-1], 'a'+to%9, '9'-to/9);
+        else
+          Out(" %c%c%c%c%s", 'a'+from%9, '9'-from/9, 'a'+to%9, '9'-to/9,
+              inPromoZone ? I2IsPromote(move) ? "+" : "=" : "");
+}
+#endif
+
+
 void
 out_pv( tree_t * restrict ptree, int value, int turn, unsigned int time )
 {
@@ -261,13 +276,7 @@ out_pv( tree_t * restrict ptree, int value, int turn, unsigned int time )
     {
 #ifdef XBOARD
       if(xboard_mode && is_out) { // print PV move in WB format
-        int move = ptree->pv[0].a[ply], from = I2From(move), to = I2To(move);
-        char inPromoZone = tt > 0 ? from >= 6*9 || to >= 6*9 : to < 3*9|| from < 3*9;
-        if(from >= nsquare)
-          Out(" %c@%c%c", "PLNSGBR"[From2Drop(from)-1], 'a'+to%9, '9'-to/9);
-        else
-          Out(" %c%c%c%c%s", 'a'+from%9, '9'-from/9, 'a'+to%9, '9'-to/9,
-              inPromoZone ? I2IsPromote(move) ? "+" : "=" : "");
+        out_xboard_move( ptree->pv[0].a[ply], tt );
       } else
 #endif
       if ( is_out )
@@ -307,13 +316,7 @@ out_pv( tree_t * restrict ptree, int value, int turn, unsigned int time )
          
 #ifdef XBOARD
       if(xboard_mode && is_out) { // print PV move in WB format
-        int move = ptree->pv[0].a[ply], from = I2From(move), to = I2To(move);
-        char inPromoZone = tt > 0 ? from >= 6*9 || to >= 6*9 : to < 3*9|| from < 3*9;
-        if(from >= nsquare)
-          Out(" %c@%c%c", "PLNSGBR"[From2Drop(from)-1], 'a'+to%9, '9'-to/9);
-        else
-          Out(" %c%c%c%c%s", 'a'+from%9, '9'-from/9, 'a'+to%9, '9'-to/9,
-              inPromoZone ? I2IsPromote(move) ? "+" : "=" : "");
+        out_xboard_move( ptree->pv[0].a[ply], tt );
       } else
 #endif
          if ( is_out )
@@ -475,7 +478,7 @@ find_root_move( unsigned int move )
 
 #if defined(MPV)
 static void
-mpv_out( tree_t * restrict ptree, int turn, unsigned int time )
+mpv_out( tree_t * restrict ptree, int turn, unsigned int time, int newest )
 {
   int mpv_out, ipv, best;
 
@@ -512,12 +515,23 @@ mpv_out( tree_t * restrict ptree, int turn, unsigned int time )
 
          str = str_time_symple( time );
          ply = mpv_pv[ipv].depth;
+#ifdef XBOARD
+          if(xboard_mode) {
+            if(ipv != newest) continue; // skip all but the newest, to prevent duplicates
+            Out("%2d %6d %6d %8d ", ply, value, time/10, 1);
+          } else
+#endif
          if ( ! ipv ) { Out( "o%2d %6s %7.2f ", ply, str, dvalue ); }
          else         { Out( " %2d        %7.2f ", ply, dvalue ); }
        }
 
       for ( ply = 1; ply <= mpv_pv[ipv].length; ply++ )
        {
+#ifdef XBOARD
+      if(xboard_mode && is_out) { // print PV move in WB format
+        out_xboard_move( mpv_pv[ipv].a[ply], tt );
+      } else
+#endif
          if ( is_out )
            {
              if ( ply > 1 && ! ( (ply-1) % 4 ) )
@@ -555,6 +569,11 @@ mpv_out( tree_t * restrict ptree, int turn, unsigned int time )
                if ( mpv_pv[ipv].a[i]
                     == mpv_pv[ipv].a[ply] ) { goto rep_esc; }
              
+#ifdef XBOARD
+      if(xboard_mode && is_out) { // print PV move in WB format
+        out_xboard_move( mpv_pv[ipv].a[ply], tt );
+      } else
+#endif
              if ( is_out )
                {
                  if ( ply > 1 && ! ( (ply-1) % 4 ) )
@@ -648,7 +667,7 @@ mpv_add_result( tree_t * restrict ptree, int value )
 {
   pv_t pv_tmp, pv;
   unsigned int time;
-  int i, vmin, num;
+  int i, vmin, num, new_pv;
 
   vmin = mpv_find_min( &num );
   assert( num <= mpv_num );
@@ -693,6 +712,7 @@ mpv_add_result( tree_t * restrict ptree, int value )
 
   pv      = ptree->pv[1];
   pv.a[0] = (unsigned int)(value+32768);
+  new_pv = i; // [HGM] xboard: remember which is the new one, so we can print only that
   do {
     assert( i < mpv_num*2 );
     assert( i < root_nmove*2 );
@@ -704,7 +724,7 @@ mpv_add_result( tree_t * restrict ptree, int value )
 
   if ( get_elapsed( &time ) < 0 ) { return -1; }
 
-  mpv_out( ptree, root_turn, time - time_start );
+  mpv_out( ptree, root_turn, time - time_start, new_pv );
 
   return 1;
 }