Fix force mode after setboard
[bonanza.git] / proce.c
diff --git a/proce.c b/proce.c
index 896963d..81707f9 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -139,14 +139,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 +152,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');
@@ -245,7 +296,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,7 +304,7 @@ 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")   {
                    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; }