Exercise UCCI option 'newgame' also after 'isready' handshake
[uci2wb.git] / UCI2WB.c
index 5118c22..1cb0a16 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -262,7 +262,7 @@ HandleEngineOutput()
            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
            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
@@ -388,11 +388,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
@@ -466,6 +467,10 @@ GUI2Engine()
            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, "accepted")) ;\r
+       else if(!strcmp(command, "rejected")) ;\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 +478,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 +512,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
@@ -528,7 +534,7 @@ DoCommand ()
 \r
     p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0;\r
     if(line[0] == '\n') return;\r
-    sscanf(line, "%s", command);\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 +542,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 +563,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
@@ -595,7 +600,6 @@ DoCommand ()
            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
        }\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
@@ -611,12 +615,13 @@ 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
+\r
+       fflush(stdout);\r
 }\r
 \r
 void *\r