Fix pondering in XBoard mode
authorH.G. Muller <h.g.muller@hccnet.nl>
Sat, 14 Sep 2013 09:24:13 +0000 (11:24 +0200)
committerH.G. Muller <h.g.muller@hccnet.nl>
Sat, 14 Sep 2013 09:24:13 +0000 (11:24 +0200)
The piece ID needed in CSA move notation is now obtained from the
root_move_list in case a ponder search is in progress, so that it
cannot be obtained from the BOARD.

proce.c

diff --git a/proce.c b/proce.c
index 0a6df1a..30ccacb 100644 (file)
--- a/proce.c
+++ b/proce.c
@@ -103,6 +103,46 @@ int myTime, hisTime, movesPerSession, inc, plyNr;
 char xboard_mode;
 char analyze_mode;
 
+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)
+    if(fromX >= 'a') fromX += 'A' - 'a';
+    switch(fromX) { // encode piece
+      case 'P': piece = pawn;   break;
+      case 'L': piece = lance;  break;
+      case 'N': piece = knight; break;
+      case 'S': piece = silver; break;
+      case 'G': piece = gold;   break;
+      case 'B': piece = bishop; break;
+      case 'R': piece = rook;   break;
+    }
+    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!
+printf("# piece from board: %d\n", piece);fflush(stdout);
+    if( game_status & ( flag_pondering | flag_puzzling | 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
+        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
+        break;
+      }
+printf("# piece corrected to %d\n", piece);fflush(stdout);
+    }
+    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
 SetTimes(void)
 { // set white and black times from own and opponent time.
@@ -162,28 +202,10 @@ Out("# command = '%s'\n", line);
                       sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
                    min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
                  }
-  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("usermove") { char buf[20];
+                   xboard_to_CSA( ptree, line+9, buf );
                    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
-                         case 'P': fromX = pawn;   break;
-                         case 'L': fromX = lance;  break;
-                         case 'N': fromX = knight; break;
-                         case 'S': fromX = silver; break;
-                         case 'G': fromX = gold;   break;
-                         case 'B': fromX = bishop; break;
-                         case 'R': fromX = rook;   break;
-                       }
-                       sprintf(line, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[(int)fromX]);
-                   } else {
-                       from = ('9' - fromY)*9 + (fromX - 'a');
-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)]);
-                   }
+                   strcpy( line, buf );
                    plyNr++;
                    return 0;
                  }