Force iteration to start at 1 in analyze mode
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index 0f9223c..50eae57 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -52,7 +52,7 @@ static int cmd_thread( char **lasts );
 #endif
 
 #if defined(MPV)
-static int cmd_mpv( char **lasts );
+static int cmd_mpv( tree_t * restrict ptree, char **lasts );
 #endif
 
 static int proce_cui( tree_t * restrict ptree );
@@ -95,13 +95,84 @@ procedure( tree_t * restrict ptree )
 
 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
 int move_list[1024], move_ptr;
+char analyze_mode;
+int exclude_list[MAX_LEGAL_MOVES], all_moves[MAX_LEGAL_MOVES];
 
 #ifdef XBOARD
 #define IF(X) else if(!strcmp(command, X))
 
 int myTime, hisTime, movesPerSession, inc, plyNr;
 char xboard_mode;
-char analyze_mode;
+
+int
+xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
+{
+  char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
+  int piece=0, bonanza_move=0;
+  if(fromY == '@') { // drop (contains all info needed to convert it)
+    if(fromX >= 'a') fromX += 'A' - 'a';
+    switch(fromX) { // encode piece
+      case 'P': piece = pawn;   break;
+      case 'L': piece = lance;  break;
+      case 'N': piece = knight; break;
+      case 'S': piece = silver; break;
+      case 'G': piece = gold;   break;
+      case 'B': piece = bishop; break;
+      case 'R': piece = rook;   break;
+    }
+    bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
+    sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
+  } else { // board move (need to figure out moved piece)
+    int from = ('9' - fromY)*9 + (fromX - 'a');
+    int flag = (promo == '+' ? FLAG_PROMO : 0);
+    piece = abs( BOARD[from] ); // this only works when not searching!
+printf("# piece from board: %d\n", piece);fflush(stdout);
+    if( status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
+      int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
+      piece = 0; // kludge to force illegal CSA move
+      for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
+        int move = (l ? all_moves[i] : root_move_list[i].move);
+        if( I2To(move) != to ) continue;
+        if( I2From(move) != from ) continue;
+        if( (move & FLAG_PROMO) != flag ) continue;
+        piece = I2PieceMove( move ); // we found the move; take the piece from it
+       bonanza_move = move;
+        break;
+      }
+printf("# piece corrected to %d\n", piece);fflush(stdout);
+    }
+    if( promo ) piece += promote;
+    sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
+  }
+  return bonanza_move;
+}
+
+static void
+update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
+{ // [HGM] exclude: manage list of excluded moves
+  char dummy[20];
+  int i;
+  if( ! exclude_list[0] ) { // nothing is excluded yet; make a copy of root move list;
+    for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
+    all_moves[i] = 0;
+  }
+  if ( ! strcmp(move_str, "all") ) { // all moves
+    if( add ) { // copy entire list of legal moves
+      for( i = 0; i < root_nmove; i++ ) exclude_list[i] = all_moves[i];
+    } else exclude_list[0] = 0; // clear list
+  } else { // single move
+    int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
+    for( i = 0; exclude_list[i]; i++ ) if( move == exclude_list[i] ) break;
+    if( add ) { // we must add the move
+      if( exclude_list[i] ) return; // but it was already in list
+      if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
+      exclude_list[i] = move; exclude_list[i+1] = 0; // append move
+    } else { // we must delete the move
+      if( exclude_list[i] == 0 ) return; // but it was not there
+      while( (exclude_list[i] = exclude_list[i+1]) ) i++; // squeeze it out
+    }
+  }
+}
 
 static void
 SetTimes(void)
@@ -127,8 +198,14 @@ proce_xboard(char *line, const char *command, tree_t * restrict ptree)
   sscanf(line + strlen(command) + 1, "%d", &value);
 Out("# command = '%s'\n", line);
   if(0) ;
-  IF("protover") { Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
-                       "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 done=1\n"); }
+  IF("protover") {
+#if defined(MPV)
+                   Out("feature option=\"MultiPV -spin 1 1 100\"\n");
+                   Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
+#endif
+                   Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
+                       "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
+                 }
   IF("new")      { forceMode = plyNr = 0; SetTimes(); return 0; }
   IF("easy")     { strcpy(line, "ponder off"); return 0; }
   IF("hard")     { strcpy(line, "ponder on");  return 0; }
@@ -147,40 +224,27 @@ Out("# command = '%s'\n", line);
   IF("exit")     { return 0; }
   IF("variant")  { /* ignore, since it must be Shogi */; }
   IF("setboard") { ; }
-  IF("option")   { ; }
+  IF("option")   {
+                   if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
+                   if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
+                 }
   IF("level")    { int min, sec; float fsec=0.;
                    if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
                       sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
                    min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
                  }
-  IF("usermove") { char fromX=line[9], fromY=line[10], toX=line[11], toY=line[12], promo=line[13];
-                   int from;
-{int i,j;for(i=0;i<81;i+=9){printf("# ");for(j=0;j<9;j++)printf(" %3d", BOARD[i+j]);printf("\n");}}
+  IF("usermove") { char buf[20];
+                   xboard_to_CSA( ptree, line+9, buf, game_status );
                    if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
-                   if(fromY == '@') { // drop
-                       if(fromX >= 'a') fromX += 'A' - 'a';
-                       switch(fromX) { // encode piece
-                         case 'P': fromX = pawn;   break;
-                         case 'L': fromX = lance;  break;
-                         case 'N': fromX = knight; break;
-                         case 'S': fromX = silver; break;
-                         case 'G': fromX = gold;   break;
-                         case 'B': fromX = bishop; break;
-                         case 'R': fromX = rook;   break;
-                       }
-                       sprintf(line, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[(int)fromX]);
-                   } else {
-                       from = ('9' - fromY)*9 + (fromX - 'a');
-Out("# from=%d\n",from);
-                       sprintf(line, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY,
-                                 astr_table_piece[abs(BOARD[from]) + (promo == '+' ? promote : 0)]);
-                   }
+                   strcpy( line, buf );
                    plyNr++;
                    return 0;
                  }
   IF("undo")     { return 0; }
   IF("remove")   { ; }
   IF("ping")     { Out("pong %d\n", value); }
+  IF("exclude")  { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
+  IF("include")  { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
   return 1;
 }
 #endif
@@ -208,6 +272,9 @@ proce_cui( tree_t * restrict ptree )
   if ( ! strcmp( token, "exit" ) )      { return cmd_exit(); }           // [HGM] analyze
   if ( ! strcmp( token, "move" ) )      { return cmd_move( ptree, &last ); }
   if ( ! strcmp( token, "undo" ) )      { return cmd_undo( ptree ); }    // [HGM] undo
+#if defined(MPV)
+  if ( ! strcmp( token, "mpv" ) )       { return cmd_mpv( ptree, &last ); }
+#endif
   analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
   if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
   if ( ! strcmp( token, "s" ) )         { return cmd_move_now(); }
@@ -235,9 +302,6 @@ proce_cui( tree_t * restrict ptree )
 #if defined(DEKUNOBOU)
   if ( ! strcmp( token, "dekunobou" ) ) { return cmd_dek( &last ); }
 #endif
-#if defined(MPV)
-  if ( ! strcmp( token, "mpv" ) )       { return cmd_mpv( &last ); }
-#endif
 #if defined(TLP)
   if ( ! strcmp( token, "tlp" ) )       { return cmd_thread( &last ); }
 #endif
@@ -406,7 +470,7 @@ do_analyze( tree_t * restrict ptree )
   Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
 #endif
   game_status |= flag_pondering;
-  iret         = iterate( ptree, 0 );
+  iret         = iterate( ptree, flag_refer_rest );
   game_status &= ~flag_pondering;
   return iret;
 }
@@ -430,6 +494,7 @@ cmd_undo( tree_t * restrict ptree )
     make_move_root( ptree, move_list[i], 0);
   }
 
+  exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
   if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
 
   return 1;
@@ -438,7 +503,7 @@ cmd_undo( tree_t * restrict ptree )
 
 static int
 cmd_analyze( tree_t * restrict ptree )
-{ // [HGM] analyze: switch on analyze mode, and start analyzing
+{ // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
   AbortDifficultCommand;
 
   analyze_mode = 1;
@@ -456,6 +521,7 @@ cmd_exit( void )
 
   if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
   analyze_mode = 0;
+  exclude_list[0] = 0; // [HGM] exclude: exclude list cleared after analysis
 
   return 1;
 }
@@ -1232,6 +1298,7 @@ cmd_move( tree_t * restrict ptree, char **lasts )
                                          | flag_rejections ) );
     if ( iret < 0 ) { return iret; }
 
+    exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
     if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
   }
   
@@ -1251,6 +1318,7 @@ cmd_new( tree_t * restrict ptree, char **lasts )
   AbortDifficultCommand;
 
   start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
+  exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
 
   if ( str1 != NULL )
     {
@@ -1537,7 +1605,7 @@ cmd_learn( tree_t * restrict ptree, char **lasts )
 
 #if defined(MPV)
 static int
-cmd_mpv( char **lasts )
+cmd_mpv( tree_t * restrict ptree, char **lasts )
 {
   const char *str = strtok_r( NULL, str_delimiters, lasts );
   char *ptr;
@@ -1567,6 +1635,8 @@ cmd_mpv( char **lasts )
 
       mpv_num = (int)l;
 
+      if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
+
       return 1;
     }
   else if ( ! strcmp( str, "width" ) )
@@ -1588,6 +1658,8 @@ cmd_mpv( char **lasts )
 
       mpv_width = (int)l;
 
+      if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
+
       return 1;
     }