Fix blocking of input in ponder games
[uci2wb.git] / UCI2WB.c
index 3baeeba..9f1ad06 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -1,4 +1,13 @@
-/************************* UCI2WB by H.G.Muller ****************************/\r
+/****************************************************************************/\r
+/*                           UCI2WB by H.G.Muller                           */\r
+/*                                                                          */\r
+/* UCI2WB is an adapter to run engines that communicate in various dialects */\r
+/* of the Universal Chess Interface in a GUI that supports XBoard protocol  */\r
+/* (CECP). It supports UCI (when used for Xiangqi: the 'Cyclone dialect'),  */\r
+/* as well as USI and UCCI when used with the flags -s or -x, respectively. */\r
+/* This version of UCI2WB is released under the GNU General Public License, */\r
+/* of which you should have received a copy together with this file.        */\r
+/****************************************************************************/\r
 \r
 #define VERSION "2.0"\r
 \r
@@ -148,7 +157,7 @@ void
 StopPonder(int pondering)\r
 {\r
        if(!pondering) return;\r
-       pause = 2;\r
+       pause = 1;\r
        fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
        Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'.\r
 }\r
@@ -236,7 +245,7 @@ Engine2GUI()
        DPRINT("# engine said: %s", line), fflush(stdout);\r
        if(sscanf(line, "%s", command) != 1) continue;\r
        if(!strcmp(command, "bestmove")) {\r
-           if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
+           if(pause == 1) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
            else if(pondering) { pondering = 0; printf("%d 0 0 0 UCI violation! Engine moves during ponder\n", lastDepth+1); continue; } // ignore ponder search\r
            // move was a move to be played\r
            if(p = strstr(line+8, " draw")) *p = 0, printf("offer draw\n"); // UCCI\r
@@ -258,6 +267,7 @@ Engine2GUI()
            } else move[moveNr][0] = 0;\r
            Move4GUI(line+9);\r
            printf("move %s\n", line+9); // send move to GUI\r
+           if(pause) { pause = 0; Sync(WAKEUP); } // release commands that came in during think\r
            if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; }\r
        }\r
        else if(!strcmp(command, "info")) {\r
@@ -378,7 +388,7 @@ GUI2Engine()
                }\r
                fprintf(toE, "\n"); DPRINT("\n");\r
            // code for searchmoves goes here\r
-           } else StartSearch("");\r
+           } else { pause = 2; StartSearch(""); fflush(stdout); fflush(toE); Sync(PAUSE); } // block input during thinking\r
        }\r
       nomove:\r
        fflush(toE); fflush(stdout);\r
@@ -392,10 +402,10 @@ GUI2Engine()
            oldMem = memory;\r
            // we can set other options here\r
            if(sc == 'x') { if(newGame) fprintf(toE, "setoption newgame\n"); } else // optional in UCCI\r
-           fprintf(toE, "u%cinewgame\n", sc); fflush(toE);\r
            pause = 1; // wait for option settings to take effect\r
            fprintf(toE, "isready\n"); fflush(toE);\r
            Sync(PAUSE); // wait for readyok\r
+           fprintf(toE, "u%cinewgame\n", sc); fflush(toE);\r
        }\r
        else if(!strcmp(command, "usermove")) {\r
            sscanf(line, "usermove %s", command); // strips off linefeed\r
@@ -407,6 +417,7 @@ GUI2Engine()
                    char *draw = drawOffer ? " draw" : ""; drawOffer = 0;\r
                    pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now\r
                    fprintf(toE, "ponderhit%s\n", draw); DPRINT("# ponderhit%s\n", draw);\r
+                   fflush(toE); fflush(stdout); pause = 2; Sync(PAUSE); // block input during thinking\r
                    goto nomove;\r
                }\r
                StopPonder(1);\r
@@ -486,7 +497,7 @@ GUI2Engine()
        else if(!strcmp(command, "nopost")) post = 0;\r
        else if(!strcmp(command, "easy") && !!*canPonder) ponder = 0, StopPonder(pondering), fprintf(toE, "setoption %s%s %sfalse\n", nameWord, canPonder, valueWord);\r
        else if(!strcmp(command, "hard") && !!*canPonder) ponder = 1, fprintf(toE, "setoption %s%s %strue\n", nameWord, canPonder, valueWord), StartPonder();\r
-       else if(!strcmp(command, "ping"))   { static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1; printf("pong %s", line+5); }\r
+       else if(!strcmp(command, "ping"))   { /* static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1;*/ printf("po%s", line+2); }\r
        else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
        else if(!strcmp(command, "cores")&& !!*threadOpt) sscanf(line, "cores %d", &cores), fprintf(toE, "setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores);\r
        else if(!strcmp(command, "sd"))     sscanf(line, "sd %d", &depth);\r