Make some Bonanza commands available as CECP option
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index cb98efa..c7df52a 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 );
 
 
@@ -108,6 +109,24 @@ int CONV is_move( const char *str )
 }
 
 
+#if defined(_WIN32)
+char *
+strtok_r( char *s, const char *t, char **next)
+{
+  char *p, *r;
+  if( ! s ) s = *next;
+  if( ! s ) return NULL;
+  while( *s && strchr(t, *s) ) s++;
+  if( ! *s ) return NULL;
+  r = s; 
+  while( *s && ! (p = strchr(t, *s)) ) s++;
+  *next = s + 1;
+  if( ! *s ) *next = NULL;
+  *s = '\0';
+  return r; 
+}
+#endif
+
 int CONV
 procedure( tree_t * restrict ptree )
 {
@@ -139,13 +158,11 @@ char xboard_mode;
 int root_pos[nsquare];
 
 int
-xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
+bonanza_piece( char p )
 {
-  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
+    int piece = 0;
+    if(p >= 'a') p += 'A' - 'a';
+    switch(p) { // encode piece
       case 'P': piece = pawn;   break;
       case 'L': piece = lance;  break;
       case 'N': piece = knight; break;
@@ -153,49 +170,97 @@ xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
       case 'G': piece = gold;   break;
       case 'B': piece = bishop; break;
       case 'R': piece = rook;   break;
+      case 'K': piece = king;   break;
     }
-    bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
+    return piece;
+}
+
+void
+read_fen( char *p )
+{
+  static char fen[128];
+  int r, f, gote = 1;
+  char *q = fen, *start = p;
+  strncpy( fen, p+9 , 127);
+  strcpy( p, "new SU" ); p += 6;
+  for ( r = 1; r <= 9; r++) {
+    for ( f = 0; *q; q++ ) {
+      int promoted = 0;
+      if( *q == '+' ) promoted = promote, q++;
+      if( isdigit( *q ) ) f += atoi( q ); else
+      if( isalpha( *q ) ) {
+       int piece = bonanza_piece( *q );
+       if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
+         gote = !gote; // switch color
+         *p++ = ( gote ? '-' : '+' );
+       }
+       sprintf( p, "%d%d%s", 9-f, r, astr_table_piece[piece + promoted] );
+       p += 4; f++;
+      } else { q++; break; }
+    }
+  }
+  while( q[-1] == ' ' ) q++;
+  if( q[-1] == '[' ) { // holdings
+    while( isalpha( *q ) ) {
+        int piece = bonanza_piece( *q );
+       if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
+         gote = !gote; // switch color
+         *p++ = ( gote ? '-' : '+' );
+       }
+       sprintf( p, "00%s", astr_table_piece[piece] );
+       p += 4; q++;
+    }
+    if( *q == '-' ) q++;
+    if( *q == ']' ) q++;
+    while( *q == ' ' ) q++;
+  } else q--;
+  sprintf(p, " %c", *q == 'w' ? '+' : '-' ); // side to move
+}
+
+void
+xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
+{
+  char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
+  int piece=0;
+  if(fromY == '@') { // drop (contains all info needed to convert it)
+    piece = bonanza_piece( fromX );
     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!
+    piece = abs( BOARD[from] ); // this only works when no search in progress!
 printf("# piece from board: %d\n", piece);fflush(stdout);
-    if( status & flag_thinking ) {
-      int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | 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; 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);
+      for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
+        int move = 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;
+       break;
       }
-    } else if( status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
+    } 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]);
   }
-  return bonanza_move;
 }
 
 static void
-update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
+update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
 { // [HGM] exclude: manage list of excluded moves
-  char dummy[20];
   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] = 0;
+    all_moves[i] = MOVE_NA;
   }
-  if ( ! strcmp(move_str, "all") ) { // all moves
+  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
-    int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
     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
@@ -237,6 +302,13 @@ Out("# command = '%s'\n", line);
                    Out("feature option=\"MultiPV -spin 1 1 100\"\n");
                    Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
 #endif
+                   Out("feature option=\"Resign threshold -spin 1000 0 2000\"\n");
+                   Out("feature option=\"Create Learn File -button\"\n");
+                   Out("feature option=\"Learn -check 1\"\n");
+                   Out("feature option=\"Use book -check 1\"\n");
+                   Out("feature option=\"Book -combo wide /// narrow\"\n");
+                   Out("feature option=\"\"\n");
+                   Out("feature option=\"\"\n");
                    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");
                  }
@@ -249,7 +321,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")       { ; }
@@ -257,10 +329,16 @@ Out("# command = '%s'\n", line);
   IF("analyze")  { return 0; }
   IF("exit")     { return 0; }
   IF("variant")  { /* ignore, since it must be Shogi */; }
-  IF("setboard") { ; }
+  IF("setboard") { forceMode = 1; plyNr = 0; read_fen( line ); return 0; }
   IF("option")   {
+                  char buf[100];
                    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(sscanf(line+7, "Resign threshold=%d", &value) == 1) { sprintf(line, "resign %d", value); return 0; }
+                   if(sscanf(line+7, "Learn=%d", &value) == 1) { sprintf(line, "hash learn %s", value ? "on" : "off"); return 0; }
+                   if(sscanf(line+7, "Create Learn File") == 1) { sprintf(line, "hash learn create"); return 0; }
+                   if(sscanf(line+7, "Use book=%d", &value) == 1) { sprintf(line, "book %s", value ? "on" : "off"); return 0; }
+                   if(sscanf(line+7, "Book=%s", buf) == 1) { sprintf(line, "book %s", buf); return 0; }
                  }
   IF("level")    { int min, sec; float fsec=0.;
                    if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
@@ -268,7 +346,7 @@ Out("# command = '%s'\n", line);
                    min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
                  }
   IF("usermove") { char buf[20];
-                   xboard_to_CSA( ptree, line+9, buf, game_status );
+                   xboard_to_CSA( ptree, line+9, buf );
                    if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
                    strcpy( line, buf );
                    plyNr++;
@@ -277,8 +355,8 @@ Out("# command = '%s'\n", line);
   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; }
+  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
@@ -305,6 +383,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
@@ -582,6 +662,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 );
@@ -626,6 +707,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 ) {