Make some Bonanza commands available as CECP option
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index 896963d..c7df52a 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -109,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,14 +157,12 @@ int myTime, hisTime, movesPerSession, inc, plyNr;
 char xboard_mode;
 int root_pos[nsquare];
 
-void
-xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
+int
+bonanza_piece( char p )
 {
-  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)
-    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;
@@ -154,7 +170,60 @@ xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
       case 'G': piece = gold;   break;
       case 'B': piece = bishop; break;
       case 'R': piece = rook;   break;
+      case 'K': piece = king;   break;
     }
+    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');
@@ -233,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");
                  }
@@ -245,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")       { ; }
@@ -253,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)