Launch searches at end of GUI loop
[uci2wb.git] / UCI2WB.c
index 0e7f34f..b9df43c 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -42,7 +42,7 @@
 #define NONE  2\r
 #define ANALYZE 3\r
 \r
-char move[2000][10], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', suffix[81], varOpt, searching, *binary;\r
+char move[2000][10], iniPos[256], hashOpt[20], pause, suspended, ponder, post, hasHash, c, sc='c', suffix[81], varOpt, searching, *binary;\r
 int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug, flob;\r
 int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500], frc, byo = -1, namOpt, comp;\r
 char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000], anaOpt[20], backLog[10000], checkOptions[8192] = "Ponder";\r
@@ -189,7 +189,7 @@ StartPonder(int moveNr)
 {\r
        if(!move[moveNr][0]) return; // no ponder move\r
        LoadPos(moveNr+1);\r
-       pondering = 1; lastDepth = 1;\r
+       searching = 1; lastDepth = 1;\r
        StartSearch(" ponder");\r
 }\r
 \r
@@ -202,9 +202,9 @@ Analyze(char *val)
 int\r
 Release()\r
 {   // send setoption commands backlogged during thinking to engine, aborting ponder or analysis search if necessary\r
-    int len = logLen - sentLen, analyse = searching;\r
+    int len = logLen - sentLen, analyse = (searching == 2);\r
     if(len <= 0) return 0;\r
-    StopPonder(pondering | searching); pondering = searching = 0; // force new search if settings change during analysis (multi-PV!)\r
+    StopPonder(searching); searching = 0; // force new search if settings change during analysis (multi-PV!)\r
     fwrite(backLog + sentLen, 1, len, toE); sentLen += len; DPRINT("# release %d\n", len);\r
     if(ponder && computer == 1 - stm) StartPonder(moveNr); // (re)start ponder search\r
     return analyse; // return 1 if analysis search should be restarted\r
@@ -264,8 +264,8 @@ HandleEngineOutput()
        DPRINT("# engine said: %s", line), fflush(stdout);\r
        if(sscanf(line, "%s", command) != 1) continue;\r
        if(!strcmp(command, "bestmove")) {\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
+           if(pause == 1) { searching = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
+           else if(searching == 1) { searching = 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
            if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; }\r
@@ -441,12 +441,9 @@ char mySide;
 char queue[10000], *qStart, *qEnd;\r
 \r
 void\r
-GUI2Engine()\r
+LaunchSearch()\r
 {\r
-    char line[256], command[256], *p;\r
-\r
-    while(1) {\r
-       int i, difficult, think=0;\r
+    int i;\r
 \r
        if((computer == stm || computer == ANALYZE && !searching) && !suspended) {\r
            DPRINT("# start search\n");\r
@@ -459,15 +456,24 @@ GUI2Engine()
                    EPRINT((f, " searchmoves"))\r
                    for(i=1; i<nr; i++) if(on[i]) EPRINT((f, " %s", moveMap[i]))\r
                }\r
-               EPRINT((f, "\n")) searching = 1; // suppresses spurious commands during analysis starting new searches\r
-           } else pause = think = 2, StartSearch(""); // request suspending of input processing while thinking\r
+               EPRINT((f, "\n")) searching = 2; // suppresses spurious commands during analysis starting new searches\r
+           } else pause = 2, searching = 3, StartSearch(""); // request suspending of input processing while thinking\r
        } else if(ponderAlways && computer == NONE) move[moveNr][0] = 0, StartPonder(moveNr-1);\r
-      nomove:\r
+}\r
+\r
+void\r
+GUI2Engine()\r
+{\r
+    char line[256], command[256], *p;\r
+\r
+    while(1) {\r
+       int i, difficult;\r
+\r
        for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking\r
        fflush(toE); fflush(stdout);\r
        if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n");\r
-       if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause\r
-       sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen);\r
+       if(searching == 3 && !pause) Sync(PAUSE), searching = 0, Release(); // if no longer thinking, take dummy pause\r
+       sscanf(line, "%s", command); DPRINT("# '%s' searching=%d pause=%d log=%d sent=%d\n", command, searching, pause, logLen, sentLen);\r
        if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first\r
        else if(!strcmp(command, "time"))   sscanf(line+4, "%d", &myTime),  myTime  = (10*myTime)/unit;\r
        else if(!strcmp(command, "otim"))   sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit;\r
@@ -476,8 +482,8 @@ GUI2Engine()
        else if(!strcmp(command, "nopost"))post = 0;\r
        else if(!strcmp(command, "pause")) {\r
            if(computer == stm) myTime -= GetTickCount() - startTime;\r
-           suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove\r
-           StopPonder(pondering || computer == stm);\r
+           suspended = 1 + (searching == 1); // remember if we were pondering, and stop search ignoring bestmove\r
+           StopPonder(searching);\r
        }\r
        else { //convert easy & hard to "option" after treating their effect on the adapter\r
          if(!strcmp(command, "easy")) {\r
@@ -499,7 +505,7 @@ GUI2Engine()
                snprintf(backLog+logLen, 9999-logLen, "setoption %s%s %s%s", nameWord, line+7, valueWord, p);\r
            } else snprintf(backLog+logLen, 9999-logLen, "setoption %s%s\n", nameWord, line+7);\r
            DPRINT("# backlog: %s", backLog+logLen); logLen += strlen(backLog+logLen);\r
-           if(!think && Release()) break; // break will restart analysis; pondering is restarted by Release itself\r
+           if(searching < 3 && Release()) break; // break will restart analysis; pondering is restarted by Release itself\r
          }\r
          else difficult = 1; // difficult command; terminate loop for easy ones\r
        }\r
@@ -511,31 +517,35 @@ GUI2Engine()
            Move4Engine(command);\r
            stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0;\r
            // when pondering we either continue the ponder search as normal search, or abort it\r
-           if(pondering || computer == ANALYZE) {\r
-               if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
+           if(searching) { // move cannot come during think\r
+               if(searching == 1 && !strcmp(command, move[moveNr])) { // ponder hit\r
                    char *draw = drawOffer ? " draw" : ""; drawOffer = 0;\r
-                   pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now\r
+                   searching = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now\r
                    EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout);\r
-                   think = 2; // request blocking input during thinking\r
-                   goto nomove;\r
+                   searching = 3; // request blocking input during thinking\r
+                   continue;\r
                }\r
                StopPonder(1); searching = 0;\r
            }\r
            strcpy(move[moveNr++], command); // possibly overwrites ponder move\r
-           continue;\r
-       }\r
+       } else\r
        if(!strcmp(command, "resume")) {\r
+           searching = 0;\r
            if(suspended == 2) StartPonder(moveNr); // restart interrupted ponder search\r
-           suspended = think = 0; continue;  // causes thinking to start in normal way if on move or analyzing\r
-       }\r
-       if(think) { // command arrived during thinking; order abort for 'instant commands'\r
+           suspended = 0;  // causes thinking to start in normal way if on move or analyzing\r
+       } else\r
+      {\r
+       if(searching == 3) { // command arrived during thinking; order abort for 'instant commands'\r
            if(!strcmp(command, "?") || !strcmp(command, "quit") ||\r
               !strcmp(command, "force") || !strcmp(command, "result")) { EPRINT((f, "# stop\n")); fflush(toE); }\r
            Sync(PAUSE); Release(); // block processing of difficult commands during thinking; send backlog left because of race\r
        }\r
        if(qStart == qEnd) qStart = qEnd = queue;\r
        p = line; while(qEnd < queue+10000 && (*qEnd++ = *p++) != '\n') {}\r
-       if(DoCommand()) goto nomove;\r
+       if(DoCommand()) continue;\r
+      }\r
+\r
+       LaunchSearch(); // start a search if we need one\r
     }\r
 }\r
 \r
@@ -599,7 +609,7 @@ DoCommand ()
                if(!strcmp(line+8, "fischerandom\n")) { frc |= 1; if(frc > 0) EPRINT((f, "# setoption name UCI_Chess960 value true\n")) }\r
        }\r
        else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
-           if(pondering || computer == ANALYZE) StopPonder(1), searching = 0;\r
+           if(searching) StopPonder(1), searching = 0;\r
            moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0;\r
        }\r
        else if(!strcmp(command, ".")) {\r
@@ -616,7 +626,7 @@ DoCommand ()
        else if(!strcmp(command, "xboard")) ;\r
        else if(!strcmp(command, "analyze"))computer = ANALYZE, collect = 1, sm = 0, Analyze("true");\r
        else if(!strcmp(command, "exit"))   computer = NONE, StopPonder(1), searching = 0, Analyze("false");\r
-       else if(!strcmp(command, "force"))  computer = NONE, StopPonder(pondering);\r
+       else if(!strcmp(command, "force"))  computer = NONE, StopPonder(searching == 1);\r
        else if(!strcmp(command, "go"))     computer = stm;\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