Pass command to DoCommand through global command queue
[uci2wb.git] / UCI2WB.c
index 05600ee..0e7f34f 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -32,7 +32,7 @@
 // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".)\r
 #define VARIANTS ",normal,xiangqi"\r
 #define STDVARS "chess,chess960,crazyhouse,3check,giveaway,suicide,losers,atomic,seirawan,shogi,xiangqi"\r
-#define EGT ",gaviotaTbPath,syzygyPath,nalimovPath,robbotripleBaseDirectory,robbototalBaseDirectory,"\r
+#define EGT ",gaviotaTbPath,syzygyPath,nalimovPath,robbotripleBaseDirectory,robbototalBaseDirectory,bitbases path,"\r
 \r
 #define DPRINT if(debug) printf\r
 #define EPRINT(X) { char f[999]; sprintf X; DPRINT("%s", f); fprintf(toE, "%s", f + 2*(*f == '#')); /* strip optional # prefix */ }\r
@@ -48,7 +48,7 @@ int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, in
 char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000], anaOpt[20], backLog[10000], 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;\r
+int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways;\r
 volatile int logLen, sentLen;\r
 \r
 FILE *toE, *fromE, *fromF;\r
@@ -185,7 +185,7 @@ LoadPos(int moveNr)
 }\r
 \r
 void\r
-StartPonder()\r
+StartPonder(int moveNr)\r
 {\r
        if(!move[moveNr][0]) return; // no ponder move\r
        LoadPos(moveNr+1);\r
@@ -206,7 +206,7 @@ Release()
     if(len <= 0) return 0;\r
     StopPonder(pondering | searching); pondering = 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(); // (re)start ponder search\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
@@ -243,29 +243,24 @@ Move4GUI(char *m)
 }\r
 \r
 int\r
-GetChar()\r
+ReadLine (FILE *f, char *line)\r
 {\r
-    int c;\r
-    if(fromF) {\r
-       if((c = fgetc(fromF)) != EOF) return c;\r
-       fclose(fromF); fromF = 0; printf("# end fake\n");\r
-    }\r
-    return fgetc(fromE);\r
+    int x, i = 0;\r
+    while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0;\r
+    return (x != EOF);\r
 }\r
 \r
-void *\r
-Engine2GUI()\r
+void\r
+HandleEngineOutput()\r
 {\r
     char line[1024], command[256]; static char egts[999];\r
 \r
-    if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
     while(1) {\r
        int i=0, x; char *p, dummy, len;\r
 \r
        fflush(stdout); fflush(toE);\r
-       while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++;\r
-       line[++i] = 0;\r
-       if(x == EOF) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
+       if(fromF && !ReadLine(fromF, line))  fromF = 0, printf("# end fake\n");\r
+       if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
        DPRINT("# engine said: %s", line), fflush(stdout);\r
        if(sscanf(line, "%s", command) != 1) continue;\r
        if(!strcmp(command, "bestmove")) {\r
@@ -287,7 +282,7 @@ Engine2GUI()
              sscanf(p+7, "%s", move[moveNr]);\r
              if(computer != NONE && ponder) {\r
                DPRINT("# ponder on %s\n", move[moveNr]);\r
-               StartPonder();\r
+               StartPonder(moveNr);\r
              }\r
              p[-1] = '\n'; *p = 0; // strip off ponder move\r
            } else move[moveNr][0] = 0;\r
@@ -302,7 +297,15 @@ Engine2GUI()
            char *pv, varName[80];\r
            if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; }\r
            if(sscanf(line+5, "string variant %s", varName) == 1) {\r
-               if(!strstr(STDVARS, varName) && (p = strstr(line+18, " startpos "))) printf("setup (-) 8x8+0_fairy %s", p+10);\r
+               if(!strstr(STDVARS, varName)) {\r
+                   int files = 8, ranks = 8, hand = 0; char parent[80];\r
+                   if(p = strstr(line+18, " files ")) sscanf(p+7, "%d", &files);\r
+                   if(p = strstr(line+18, " ranks ")) sscanf(p+7, "%d", &ranks);\r
+                   if(p = strstr(line+18, " pocket ")) sscanf(p+8, "%d", &hand);\r
+                   if(p = strstr(line+18, " template ")) sscanf(p+10, "%s", parent); else strcpy(parent, "fairy");\r
+                   if(p = strstr(line+18, " startpos "))\r
+                       printf("setup (-) %dx%d+%d_%s %s", files, ranks, hand, parent, p+10);\r
+               }\r
                continue;\r
            }\r
            if(collect && (pv = strstr(line+5, "currmove "))) {\r
@@ -361,7 +364,7 @@ Engine2GUI()
            if(!strcasecmp(name, "newgame") && !strcmp(type, "button")) { newGame++; continue; }\r
            if(!strcasecmp(name, "usemillisec")) { unit = (!strcmp(val, "false") ? 2 : 1); continue; }\r
            sprintf(buf, ",%s,", name); if(p = strcasestr(EGT, buf)) { // collect EGT formats\r
-               strcpy(buf, p); for(p=buf; *++p >='a';){} *p = 0; strcat(egts, buf); continue; // clip at first non-lower-case\r
+               strcpy(buf, p); for(p=buf; *++p >='a';){} if(*p == ' ') strcpy(buf, ",scorpio"); *p = 0; strcat(egts, buf); continue; // clip at first non-lower-case\r
            }\r
            // pass on engine-defined option as WB option feature\r
            if(!strcmp(type, "filename")) type[4] = 0;\r
@@ -408,6 +411,13 @@ Engine2GUI()
     }\r
 }\r
 \r
+void *\r
+Engine2GUI()\r
+{\r
+    if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
+    HandleEngineOutput();\r
+}\r
+\r
 void\r
 Move4Engine(char *m)\r
 {\r
@@ -426,13 +436,17 @@ Move4Engine(char *m)
     }\r
 }\r
 \r
+int DoCommand ();\r
+char mySide;\r
+char queue[10000], *qStart, *qEnd;\r
+\r
 void\r
 GUI2Engine()\r
 {\r
-    char line[256], command[256], *p, *q, *r, mySide, type[99];\r
+    char line[256], command[256], *p;\r
 \r
     while(1) {\r
-       int i, x, difficult, think=0;\r
+       int i, difficult, think=0;\r
 \r
        if((computer == stm || computer == ANALYZE && !searching) && !suspended) {\r
            DPRINT("# start search\n");\r
@@ -447,12 +461,11 @@ GUI2Engine()
                }\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
-       }\r
+       } else if(ponderAlways && computer == NONE) move[moveNr][0] = 0, StartPonder(moveNr-1);\r
       nomove:\r
        for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking\r
        fflush(toE); fflush(stdout);\r
-       i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++;\r
-       line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); }\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(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first\r
@@ -477,6 +490,7 @@ GUI2Engine()
            char *p;\r
            if(logLen == sentLen) logLen = 0, sentLen = 0; // engine is up to date; reset buffer\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
@@ -511,7 +525,7 @@ GUI2Engine()
            continue;\r
        }\r
        if(!strcmp(command, "resume")) {\r
-           if(suspended == 2) StartPonder(); // restart interrupted ponder search\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
@@ -519,6 +533,21 @@ GUI2Engine()
               !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
+    }\r
+}\r
+\r
+int\r
+DoCommand ()\r
+{\r
+    char line[1024], command[256], *p, *q, *r, type[99];\r
+    int i;\r
+\r
+    p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0;\r
+    sscanf(line, "%s", command);\r
+\r
        if(!strcmp(command, "new")) {\r
            computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0;\r
            stm = WHITE; strcpy(iniPos, "position startpos"); frc &= ~1;\r
@@ -542,6 +571,7 @@ GUI2Engine()
            if(!varList[0]) strcpy(varList, sc=='s' ? ",shogi,5x5+5_shogi" : VARIANTS);\r
            printf("feature setboard=1 usermove=1 debug=1 ping=1 name=1 reuse=0 exclude=1 pause=1 sigint=0 sigterm=0 done=0\n");\r
            printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
+           printf("feature option=\"ponder always -check %d\"\n", ponderAlways);\r
            if(sc == 's') printf("feature option=\"Floating Byoyomi -check %d\"\nfeature option=\"Byoyomi -spin %d -1 1000\"\n", flob, byo);\r
            EPRINT((f, sc == 'x' ? "# ucci\n" : "# u%ci\n", sc)) fflush(toE); // prompt UCI engine for options\r
            Sync(PAUSE); // wait for uciok\r
@@ -574,13 +604,13 @@ GUI2Engine()
        }\r
        else if(!strcmp(command, ".")) {\r
            printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
-           goto nomove;\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
            for(i=1; i<nr; i++) { if(!strcmp(line+8, moveMap[i]) || all) on[i] = in; sm |= on[i]+1; } // sm: 2 = enabled, 1 = disabled\r
-           if(!(sm & 2)) goto nomove; // no moves enabled; continue current search\r
+           if(!(sm & 2)) return 1; // no moves enabled; continue current search\r
            if(computer == ANALYZE) StopPonder(1), searching = 0; // abort old analysis\r
        }\r
        else if(!strcmp(command, "xboard")) ;\r
@@ -593,7 +623,7 @@ GUI2Engine()
        else if(!strcmp(command, "cores")&& !!*threadOpt) { sscanf(line, "cores %d", &cores); EPRINT((f, "# setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores)) }\r
        else if(!strcmp(command, "egtpath")){\r
            sscanf(line+8, "%s %[^\n]", type, command);\r
-            if(p = strstr(EGT, type)) strcpy(type, p), p = strchr(type, ','), *p = 0;\r
+            if(p = strstr(EGT, type)) strcpy(type, p), p = strchr(type, ','), *p = 0; else strcpy(type, "bitbases path");\r
            EPRINT((f, "# setoption name %s value %s\n", type, command));\r
        }\r
        else if(!strcmp(command, "sd"))     sscanf(line, "sd %d", &depth);\r
@@ -605,7 +635,8 @@ GUI2Engine()
            computer = NONE;\r
        }\r
        else if(!strcmp(command, "quit"))   { EPRINT((f, "# quit\n")) fflush(toE), exit(atoi(line+4)); }\r
-    }\r
+\r
+    return 0;\r
 }\r
 \r
 int\r