Handle periodic updates with easy commands
[uci2wb.git] / UCI2WB.c
index 135b3a7..41c71c8 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
 char move[2000][10], iniPos[256], hashOpt[20], 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
+char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000], anaOpt[20], checkOptions[8192] = "Ponder";\r
 char pvs[99][999], board[100];  // XQ board for UCCI\r
 char *nameWord = "name ", *valueWord = "value ", *wTime = "w", *bTime = "b", *wInc = "winc", *bInc = "binc", newGame; // keywords that differ in UCCI\r
 int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways;\r
-volatile int logLen, sentLen;\r
 \r
 FILE *toE, *fromE, *fromF;\r
 int pid;\r
@@ -161,9 +160,10 @@ StartSearch(char *ponder)
 }\r
 \r
 void\r
-StopPonder(int pondering)\r
+StopSearch(int discard)\r
 {\r
-       if(!pondering) return;\r
+       if(!searching) return;\r
+       if(discard) searching = 0; // this causes bestmove to be ignored\r
        EPRINT((f, "# stop\n")) fflush(toE); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
 }\r
 \r
@@ -197,17 +197,6 @@ Analyze(char *val)
     if(*anaOpt) EPRINT((f, "# setoption %s%s %s%s\n", nameWord, anaOpt, valueWord, val));\r
 }\r
 \r
-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 == 2);\r
-    if(len <= 0) return 0;\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
-}\r
-\r
 char *Convert(char *pv)\r
 {   // convert Shogi coordinates to WB\r
     char *p, *q, c;\r
@@ -271,7 +260,6 @@ HandleEngineOutput()
            if(strstr(line+9, "(none)") || strstr(line+9, "null") ||\r
               strstr(line+9, "0000")) { printf("%s\n", lastScore < -99999 ? "resign" : "1/2-1/2 {stalemate}"); computer = NONE; }\r
            sscanf(line, "bestmove %s", move[moveNr++]);\r
-           Release(); // send setoption commands that arrived during search\r
            myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder\r
            if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts\r
            stm = WHITE+BLACK - stm;\r
@@ -463,17 +451,20 @@ GUI2Engine()
        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
-       sscanf(line, "%s", command); DPRINT("# '%s' searching=%d log=%d sent=%d\n", command, searching, logLen, sentLen);\r
+       sscanf(line, "%s", command);\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
        else if(!strcmp(command, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly\r
        else if(!strcmp(command, "post"))  post = 1;\r
        else if(!strcmp(command, "nopost"))post = 0;\r
+       else if(!strcmp(command, ".")) {\r
+           printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
+       }\r
        else if(!strcmp(command, "pause")) {\r
            if(computer == stm) myTime -= GetTickCount() - startTime;\r
            suspended = 1 + (searching == 1); // remember if we were pondering, and stop search ignoring bestmove\r
-           StopPonder(searching);\r
+           StopSearch(1);\r
        }\r
        else { //convert easy & hard to "option" after treating their effect on the adapter\r
          if(!strcmp(command, "easy")) {\r
@@ -482,20 +473,12 @@ GUI2Engine()
          else if(!strcmp(command, "hard")) {\r
            if(*canPonder) ponder = 1, sprintf(command, "option"), sprintf(line, "option %s=1\n", canPonder); else continue;\r
          }\r
-         if(!strcmp(command, "option")) {\r
-           char *p;\r
-           if(logLen == sentLen) logLen = 0, sentLen = 0; // engine is up to date; reset buffer\r
+         else if(!strcmp(command, "option")) {\r
            if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else\r
            if(sscanf(line+7, "ponder always=%d", &ponderAlways) == 1) ; else\r
            if(sscanf(line+7, "Floating Byoyomi=%d", &flob) == 1) ; else\r
            if(sscanf(line+7, "Byoyomi=%d", &byo) == 1) ; else\r
-           if(p = strchr(line, '=')) {\r
-               *p++ = 0;\r
-               if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false");\r
-               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(searching < 3 && Release()) break; // break will restart analysis; pondering is restarted by Release itself\r
+           difficult = 1;\r
          }\r
          else difficult = 1; // difficult command; terminate loop for easy ones\r
        }\r
@@ -514,21 +497,20 @@ GUI2Engine()
                    EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout);\r
                    continue;\r
                }\r
-               searching = 0; StopPonder(1);\r
+               StopSearch(1);\r
            }\r
            strcpy(move[moveNr++], command); // possibly overwrites ponder move\r
            *qEnd++ = '\n'; Sync(WAKEUP);    // make sure engine thread considers starting a search\r
        } else\r
        if(!strcmp(command, "resume")) {\r
-           searching = 0;\r
            if(suspended == 2) StartPonder(moveNr); // restart interrupted ponder search\r
-           suspended = 0;  // causes thinking to start in normal way if on move or analyzing\r
+           suspended = 0;  *qEnd++ = '\n'; Sync(WAKEUP); // causes search 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")) StopPonder(1);\r
-       } else if(searching) searching = 0, StopPonder(1); // always abort pondering or analysis\r
+              !strcmp(command, "force") || !strcmp(command, "result")) StopSearch(0);\r
+       } else StopSearch(1); // always abort pondering or analysis\r
 \r
        // queue command for execution by engine thread\r
        if(qStart == qEnd) qStart = qEnd = queue;\r
@@ -560,6 +542,14 @@ DoCommand ()
            HandleEngineOutput(); // wait for readyok\r
            EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE);\r
        }\r
+       else if(!strcmp(command, "option")) {\r
+           char *p;\r
+           if(p = strchr(line, '=')) {\r
+               *p++ = 0;\r
+               if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false");\r
+               EPRINT((f, "# setoption %s%s %s%s", nameWord, line+7, valueWord, p));\r
+           } else EPRINT((f, "# setoption %s%s\n", nameWord, line+7));\r
+       }\r
        else if(!strcmp(command, "level")) {\r
            int sec = 0;\r
            sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 ||\r
@@ -600,10 +590,6 @@ DoCommand ()
        else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
            moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0;\r
        }\r
-       else if(!strcmp(command, ".")) {\r
-           printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
-           return 1;\r
-       }\r
        else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude\r
            int all = !strcmp(line+8, "all"), in = command[1] == 'n';\r
            inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag\r