Implement XBoard memory command
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index e385b91..6d63541 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -94,6 +94,7 @@ static int CONV cmd_undo( tree_t * restrict ptree );    // [HGM] undo
 static int CONV cmd_time( char **lasts );
 static int CONV cmd_undo( tree_t * restrict ptree );    // [HGM] undo
 static int CONV cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
+static int CONV cmd_inex( tree_t * restrict ptree, int inex, char **last ); // [HGM] exclude
 static int CONV cmd_exit( void );
 
 
@@ -128,13 +129,15 @@ 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 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 root_pos[nsquare];
 
 void
 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
@@ -156,9 +159,9 @@ xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
   } 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!
+    piece = abs( BOARD[from] ); // this only works when no search in progress!
 printf("# piece from board: %d\n", piece);fflush(stdout);
-    if( game_status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
+    if( game_status & flag_thinking ) {
       int i, to = ('9' - toY)*9 + (toX - 'a');
       piece = 0; // kludge to force illegal CSA move
       for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
@@ -167,16 +170,41 @@ printf("# piece from board: %d\n", piece);fflush(stdout);
         if( I2From(move) != from ) continue;
         if( (move & FLAG_PROMO) != flag ) continue;
         piece = I2PieceMove( move ); // we found the move; take the piece from it
-        break;
+       break;
       }
+    } else if( game_status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
 printf("# piece corrected to %d\n", piece);fflush(stdout);
-    }
-    if( promo ) piece += promote;
+    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]);
   }
 }
 
 static void
+update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
+{ // [HGM] exclude: manage list of excluded moves
+  int i;
+  if( moves_ignore[0] == MOVE_NA ) { // 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] = MOVE_NA;
+  }
+  if ( move == MOVE_NA ) { // all moves
+    if( add ) { // copy entire list of legal moves
+      for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
+    } else moves_ignore[0] = MOVE_NA; // clear list
+  } else { // single move
+    for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
+    if( add ) { // we must add the move
+      if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
+      if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
+      moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
+    } else { // we must delete the move
+      if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
+      while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
+    }
+  }
+}
+
+static void
 SetTimes(void)
 { // set white and black times from own and opponent time.
   int moves;
@@ -206,7 +234,7 @@ Out("# command = '%s'\n", line);
                    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 done=1\n");
+                       "\" 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; }
@@ -217,7 +245,7 @@ Out("# command = '%s'\n", line);
   IF("otim")     { sscanf(line+5, "%d", &hisTime); }
   IF("force")    { forceMode = 1; }
   IF("go")       { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
-  IF("memory")   { ; }
+  IF("memory")   { sprintf(line, "hash %d", value); return 0; }
   IF("cores")    { sprintf(line, "tlp num %d", value); return 0; }
   IF("sd")       { sprintf(line, "limit depth %d", value); return 0; }
   IF("st")       { ; }
@@ -245,6 +273,8 @@ Out("# command = '%s'\n", line);
   IF("undo")     { return 0; }
   IF("remove")   { ; }
   IF("ping")     { Out("pong %d\n", value); }
+  IF("exclude")  { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
+  IF("include")  { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
   return 1;
 }
 #endif
@@ -271,6 +301,8 @@ static int CONV 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 ( ! strcmp( token, "include" ) )   { return cmd_inex( ptree , 0, &last); } // [HGM] exclude
+  if ( ! strcmp( token, "exclude" ) )   { return cmd_inex( ptree , 1, &last); } // [HGM] exclude
 #if defined(MPV)
   if ( ! strcmp( token, "mpv" ) )       { return cmd_mpv( ptree, &last ); }
 #endif
@@ -548,6 +580,7 @@ do_analyze( tree_t * restrict ptree )
   time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
 #ifdef XBOARD
   if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
+  for( iret = 0; iret < nsquare; iret++ ) root_pos[iret] = BOARD[iret];
 #endif
   game_status |= flag_pondering;
   iret         = iterate( ptree );
@@ -574,6 +607,7 @@ cmd_undo( tree_t * restrict ptree )
     make_move_root( ptree, move_list[i], 0);
   }
 
+  moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
   if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
 
   return 1;
@@ -582,7 +616,7 @@ cmd_undo( tree_t * restrict ptree )
 
 static int CONV
 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;
@@ -591,6 +625,29 @@ cmd_analyze( tree_t * restrict ptree )
 
 
 static int CONV
+cmd_inex( tree_t * restrict ptree, int inex, char **last )
+{ // [HGM] analyze: switch off analysis mode
+  const char *str = strtok_r( NULL, str_delimiters, last );
+  int iret;
+  unsigned int move;
+
+  AbortDifficultCommand;
+
+  if( ! strcmp( str, "all" ) ) {
+    move = MOVE_NA;
+  } else {
+    iret = interpret_CSA_move( ptree, &move, str );
+    if ( iret < 0 ) { return iret; }
+  }
+
+  update_exclude_list( ptree, inex, move );
+
+  if ( ! analyze_mode ) return 1;
+  return do_analyze( ptree );
+}
+
+
+static int CONV
 cmd_exit( void )
 { // [HGM] analyze: switch off analysis mode
   if ( !analyze_mode ) {
@@ -600,6 +657,7 @@ cmd_exit( void )
 
   if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
   analyze_mode = 0;
+  moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
 
   return 1;
 }
@@ -1542,6 +1600,7 @@ static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
 
   } while ( str != NULL );
 
+  moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
   if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
   
   return 1;
@@ -1559,6 +1618,7 @@ static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
   AbortDifficultCommand;
 
   start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
+  moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
 
   if ( str1 != NULL )
     {