Bump version to 4.0
[uci2wb.git] / UCI2WB.c
index 41c71c8..a546859 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -9,7 +9,7 @@
 /* of which you should have received a copy together with this file.        */\r
 /****************************************************************************/\r
 \r
-#define VERSION "3.0"\r
+#define VERSION "4.0"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -260,9 +260,9 @@ 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
-           myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder\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
+           stm = WHITE+BLACK - stm; searching = 0;\r
            // first start a new ponder search, if pondering is on and we have a move to ponder on\r
            if(p = strstr(line+9, "ponder")) {\r
              sscanf(p+7, "%s", move[moveNr]);\r
@@ -276,7 +276,7 @@ HandleEngineOutput()
            printf("move %s\n", line+9); // send move to GUI\r
             if(move[moveNr][0]) printf("Hint: %s\n", move[moveNr]);\r
            if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; }\r
-           return;\r
+           fflush(stdout); return;\r
        }\r
        else if(!strcmp(command, "info")) {\r
            int d=0, s=0, t=(GetTickCount() - startTime)/10, n=1;\r
@@ -294,17 +294,6 @@ HandleEngineOutput()
                }\r
                continue;\r
            }\r
-           if(collect && (pv = strstr(line+5, "currmove "))) {\r
-               if(p = strstr(line+5, "currmovenumber ")) {\r
-                   n = atoi(p+15);\r
-                   if(collect == 1 && n != 1) continue; // wait for move 1\r
-                   if(collect + (n == 1) > 2) { // done collecting\r
-                       if(inex && collect == 2) printf("%d 0 0 0 OK to exclude\n", lastDepth);\r
-                       collect = 3; continue;\r
-                   }\r
-                   collect = 2; on[nr=n] = 1; sscanf(pv+9, "%s", moveMap[n]); continue; // store move\r
-               }\r
-           }\r
            if(!post) continue;\r
            if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
                if(p = strstr(line+4, " depth "))      sscanf(p+7, "%d", &d), statDepth = d;\r
@@ -313,8 +302,6 @@ HandleEngineOutput()
                if(p = strstr(line+4, " score "))      sscanf(p+7, "%d", &s), statScore = s;\r
                if(p = strstr(line+4, " nodes "))      sscanf(p+7, "%d", &n), statNodes = n;\r
                if(p = strstr(line+4, " time "))       sscanf(p+6, "%d", &t), t /= 10, statTime = t;\r
-               if(p = strstr(line+4, " currmove "))   sscanf(p+10,"%s", currMove);\r
-               if(p = strstr(line+4, " currmovenumber ")) sscanf(p+16,"%d", &currNr);\r
                if(pv = strstr(line+4, " pv ")) { // convert PV info to WB thinking output\r
                  if(d > maxDepth) maxDepth = d, mpvSP = 0; else if(d < maxDepth) continue; // ignore depth regressions\r
                  if(p = strstr(line+4, " upperbound ")) strcat(p, "?\n"); else\r
@@ -324,6 +311,18 @@ HandleEngineOutput()
                    printf("%3d  %6d %6d %10d %s", lastDepth=d, lastScore=s, t, n, Convert(pv+4));\r
                } else if(s == -100000) lastScore = s; // when checkmated score is valid even without PV (which might not come)\r
            }\r
+           if(collect && (pv = strstr(line+4, " currmove "))) {\r
+               sscanf(pv+10,"%s", currMove);\r
+               if(p = strstr(line+4, " currmovenumber ")) {\r
+                   n = currNr = atoi(p+16);\r
+                   if(collect == 1 && n != 1) continue; // wait for move 1\r
+                   if(collect + (n == 1) > 2) { // done collecting\r
+                       if(inex && collect == 2) printf("%d 0 0 0 OK to exclude\n", lastDepth);\r
+                       collect = 3; continue;\r
+                   }\r
+                   collect = 2; on[nr=n] = 1; strcpy(moveMap[n], currMove); continue; // store move\r
+               }\r
+           }\r
        }\r
        else if(!strcmp(command, "option")) { // USI option: extract data fields\r
            char name[80], type[80], buf[1024], val[256], *q;\r
@@ -388,11 +387,12 @@ HandleEngineOutput()
                                else if(!strncmp(q-1, " chess ", 7)) strcpy(p, "normal"), p += 6, q += 5; // 'chess' is called 'normal' in CECP\r
                                else *p++ = *q++; // copy other variant names unmodified\r
            if(frc) sprintf(p, ",normal,fischerandom"), printf("feature oocastle=%d\n", frc<0); // unannounced FRC uses O-O castling\r
-           if(*varList) printf("feature variants=\"%s\"\n", varList+1); // from UCI_Variant combo and/or UCI_Chess960 check options\r
+           if(!*varList) strcpy(varList, sc=='s' ? ",shogi,5x5+5_shogi" : VARIANTS); // without clue guess liberally\r
+           printf("feature variants=\"%s\"\n", varList+1); // from UCI_Variant combo and/or UCI_Chess960 check options\r
            if(*egts) printf("feature egt=\"%s\"\n", egts+1);\r
            printf("feature smp=1 memory=%d done=1\n", hasHash);\r
            if(unit == 2) { unit = 1; EPRINT((f, "# setoption usemillisec true\n")) }\r
-           return; // done with options\r
+           fflush(stdout); return; // done with options\r
        }\r
     }\r
 }\r
@@ -415,7 +415,7 @@ Move4Engine(char *m)
     }\r
 }\r
 \r
-int DoCommand ();\r
+void DoCommand ();\r
 char mySide;\r
 volatile char queue[10000], *qStart, *qEnd;\r
 \r
@@ -424,7 +424,7 @@ LaunchSearch()
 {\r
     int i;\r
 \r
-       if((computer == stm || computer == ANALYZE && !searching) && !suspended) {\r
+       if((computer == stm || computer == ANALYZE && !searching && sm != 1) && !suspended) {\r
            DPRINT("# start search\n");\r
            LoadPos(moveNr); fflush(stdout); // load position\r
            // and set engine thinking (note USI swaps colors!)\r
@@ -459,13 +459,21 @@ GUI2Engine()
        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
+           printf("stat01: %d %d %d %d %d %s\n", statTime, statNodes, statDepth, nr-currNr, nr, 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
            StopSearch(1);\r
        }\r
+       else if(!strcmp(command, "xboard")) ;\r
+       else if(!strcmp(command, "random")) ;\r
+       else if(!strcmp(command, "accepted")) ;\r
+       else if(!strcmp(command, "rejected")) ;\r
+       else if(!strcmp(command, "book")) ;\r
+       else if(!strcmp(command, "ics")) ;\r
+       else if(!strcmp(command, "hint")) ;\r
+       else if(!strcmp(command, "computer")) comp = 1;\r
        else { //convert easy & hard to "option" after treating their effect on the adapter\r
          if(!strcmp(command, "easy")) {\r
            if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue;\r
@@ -473,7 +481,7 @@ 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
-         else if(!strcmp(command, "option")) {\r
+         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
@@ -507,6 +515,7 @@ GUI2Engine()
            suspended = 0;  *qEnd++ = '\n'; Sync(WAKEUP); // causes search to start in normal way if on move or analyzing\r
        } else\r
       {\r
+       DPRINT("# queue '%s', searching=%d\n", command, searching);\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")) StopSearch(0);\r
@@ -520,15 +529,15 @@ GUI2Engine()
     }\r
 }\r
 \r
-int\r
+void\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
-    if(line[0] == '\n') return 0;\r
-    sscanf(line, "%s", command);\r
+    if(line[0] == '\n') return;\r
+    sscanf(line, "%s", command); DPRINT("# command %s\n", command), fflush(stdout);\r
 \r
        if(!strcmp(command, "new")) {\r
            computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0;\r
@@ -536,10 +545,10 @@ DoCommand ()
            if(memory != oldMem && hasHash) EPRINT((f, "# setoption %s%s %s%d\n", nameWord, hashOpt, valueWord, memory))\r
            oldMem = memory;\r
            // we can set other options here\r
-           if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI\r
            if(varOpt) EPRINT((f, "# setoption name UCI_Variant value chess\n"))\r
            EPRINT((f, "# isready\n")) fflush(toE);\r
            HandleEngineOutput(); // wait for readyok\r
+           if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI\r
            EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE);\r
        }\r
        else if(!strcmp(command, "option")) {\r
@@ -557,7 +566,6 @@ DoCommand ()
            tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit;\r
        }\r
        else if(!strcmp(command, "protover")) {\r
-           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
@@ -594,9 +602,7 @@ DoCommand ()
            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)) return 1; // no moves enabled; continue current search\r
        }\r
-       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, Analyze("false");\r
        else if(!strcmp(command, "force"))  computer = NONE;\r
@@ -612,14 +618,14 @@ DoCommand ()
        else if(!strcmp(command, "sd"))     sscanf(line, "sd %d", &depth);\r
        else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0, sTime /= unit;\r
        else if(!strcmp(command, "name"))   { if(namOpt) EPRINT((f, "# setoption name UCI_Opponent value none none %s %s", comp ? "computer" : "human", line+5)) }\r
-       else if(!strcmp(command, "computer")) comp = 1;\r
        else if(!strcmp(command, "result")) {\r
            if(sc == 's') EPRINT((f, "# gameover %s\n", line[8] == '/' ? "draw" : (line[7] == '0') == mySide ? "win" : "lose"))\r
            computer = NONE;\r
        }\r
        else if(!strcmp(command, "quit"))   { EPRINT((f, "# quit\n")) fflush(toE), exit(atoi(line+4)); }\r
+       else printf("Error (unknown command): %s\n", command);\r
 \r
-    return 0;\r
+       fflush(stdout);\r
 }\r
 \r
 void *\r
@@ -630,7 +636,7 @@ Engine2GUI()
        if(searching > 1) HandleEngineOutput();  // this could leave us (or fall through) pondering\r
        while(qStart == qEnd && searching) HandleEngineOutput(); // relay ponder output until command arrives\r
        Sync(PAUSE); // possibly wait for command silently if engine is idle\r
-       if(!DoCommand()) LaunchSearch();\r
+       DoCommand(); LaunchSearch();\r
     }\r
 }\r
 \r