Implement analyze command
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index 94db5d0..0f9223c 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -74,6 +74,9 @@ static int cmd_new( tree_t * restrict ptree, char **lasts );
 static int cmd_read( tree_t * restrict ptree, char **lasts );
 static int cmd_resign( tree_t * restrict ptree, char **lasts );
 static int cmd_time( char **lasts );
+static int cmd_undo( tree_t * restrict ptree );    // [HGM] undo
+static int cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
+static int cmd_exit( void );
 static int is_move( const char *str );
 
 
@@ -90,11 +93,15 @@ procedure( tree_t * restrict ptree )
   return proce_cui( ptree );
 }
 
+char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
+int move_list[1024], move_ptr;
+
 #ifdef XBOARD
 #define IF(X) else if(!strcmp(command, X))
 
 int myTime, hisTime, movesPerSession, inc, plyNr;
 char xboard_mode;
+char analyze_mode;
 
 static void
 SetTimes(void)
@@ -136,8 +143,8 @@ Out("# command = '%s'\n", line);
   IF("sd")       { sprintf(line, "limit depth %d", value); return 0; }
   IF("st")       { ; }
   IF("quit")     { return 0; }
-  IF("analyze")  { ; }
-  IF("exit")     { ; }
+  IF("analyze")  { return 0; }
+  IF("exit")     { return 0; }
   IF("variant")  { /* ignore, since it must be Shogi */; }
   IF("setboard") { ; }
   IF("option")   { ; }
@@ -149,7 +156,7 @@ Out("# command = '%s'\n", line);
   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(forceMode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
+                   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
@@ -168,9 +175,10 @@ 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)]);
                    }
-                   plyNr++; return 0;
+                   plyNr++;
+                   return 0;
                  }
-  IF("undo")     { ; }
+  IF("undo")     { return 0; }
   IF("remove")   { ; }
   IF("ping")     { Out("pong %d\n", value); }
   return 1;
@@ -196,6 +204,11 @@ proce_cui( tree_t * restrict ptree )
     if ( ! strcmp( token, "xboard" ) )  { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
   }
 #endif
+  if ( ! strcmp( token, "analyze" ) )   { return cmd_analyze( ptree ); } // [HGM] analyze
+  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
+  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(); }
   if ( ! strcmp( token, "beep" ) )      { return cmd_beep( &last); }
@@ -203,7 +216,6 @@ proce_cui( tree_t * restrict ptree )
   if ( ! strcmp( token, "display" ) )   { return cmd_display( ptree, &last ); }
   if ( ! strcmp( token, "hash" ) )      { return cmd_hash( &last ); }
   if ( ! strcmp( token, "limit" ) )     { return cmd_limit( &last ); }
-  if ( ! strcmp( token, "move" ) )      { return cmd_move( ptree, &last ); }
   if ( ! strcmp( token, "new" ) )       { return cmd_new( ptree, &last ); }
   if ( ! strcmp( token, "peek" ) )      { return cmd_peek( &last ); }
   if ( ! strcmp( token, "ping" ) )      { return cmd_ping(); }
@@ -385,6 +397,71 @@ cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter )
 
 
 static int
+do_analyze( tree_t * restrict ptree )
+{ // [HGM] analyze: do a ponder search on the current position
+  int iret;
+  if ( get_elapsed( &time_start ) < 0 ) { return -1; }
+  time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
+#ifdef XBOARD
+  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 );
+  game_status &= ~flag_pondering;
+  return iret;
+}
+
+
+static int
+cmd_undo( tree_t * restrict ptree )
+{ // [HGM] undo: restart the game, and feed all moves except the last
+  int i, last = move_ptr;
+  char *p = start_data;
+  if( move_ptr <= 0 ) {
+    str_error = "undo past start of game ignored";
+    return -2;
+  }
+
+  AbortDifficultCommand;
+
+  last--;
+  cmd_new( ptree, &p );
+  for(i=0; i<last; i++) {
+    make_move_root( ptree, move_list[i], 0);
+  }
+
+  if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
+
+  return 1;
+}
+
+
+static int
+cmd_analyze( tree_t * restrict ptree )
+{ // [HGM] analyze: switch on analyze mode, and start analyzing
+  AbortDifficultCommand;
+
+  analyze_mode = 1;
+  return do_analyze( ptree );
+}
+
+
+static int
+cmd_exit( void )
+{ // [HGM] analyze: switch off analysis mode
+  if ( !analyze_mode ) {
+    str_error = "was not analyzing";
+    return -2;
+  }
+
+  if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
+  analyze_mode = 0;
+
+  return 1;
+}
+
+
+static int
 is_move( const char *str )
 {
   if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
@@ -1099,6 +1176,12 @@ cmd_move( tree_t * restrict ptree, char **lasts )
 
   if ( str == NULL )
     {
+      if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
+         {
+           str_error = str_bad_cmdline;
+           return -2;
+         }
+
       iret = get_elapsed( &time_turn_start );
       if ( iret < 0 ) { return iret; }
       
@@ -1107,6 +1190,12 @@ cmd_move( tree_t * restrict ptree, char **lasts )
     }
   else if ( ! strcmp( str, "restraint" ) )
     {
+      if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
+         {
+           str_error = str_bad_cmdline;
+           return -2;
+         }
+
       iret = get_elapsed( &time_turn_start );
       if ( iret < 0 ) { return iret; }
       
@@ -1142,6 +1231,8 @@ cmd_move( tree_t * restrict ptree, char **lasts )
                                          | flag_detect_hang
                                          | flag_rejections ) );
     if ( iret < 0 ) { return iret; }
+
+    if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
   }
   
   return 1;
@@ -1159,8 +1250,11 @@ cmd_new( tree_t * restrict ptree, char **lasts )
 
   AbortDifficultCommand;
 
+  start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
+
   if ( str1 != NULL )
     {
+      strncpy(start_data, str1, 511); // [HGM] undo: remember start position
       memset( &min_posi.asquare, empty, nsquare );
       min_posi.hand_black = min_posi.hand_white = 0;
       iret = read_board_rep1( str1, &min_posi );