Create infrastructure for accounting time in other units
[uci2wb.git] / UCI2WB.c
index cf50631..77cb1da 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
 \r
 char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', *suffix, *variants;\r
 int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug;\r
-int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500]; char currMove[20], moveMap[500][10]; // for analyze mode\r
+int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500];\r
+char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20];\r
+int unit = 1;\r
 \r
-FILE *toE, *fromE;\r
+FILE *toE, *fromE, *fromF;\r
 int pid;\r
 \r
 #ifdef WIN32\r
@@ -151,20 +153,32 @@ Move4GUI(char *m)
     }\r
 }\r
 \r
+int\r
+GetChar()\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
+}\r
+\r
 void *\r
 Engine2GUI()\r
 {\r
     char line[1024], command[256];\r
 \r
+    if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
     while(1) {\r
        int i=0, x; char *p, dummy;\r
 \r
        fflush(stdout); fflush(toE);\r
-       while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++;\r
+       while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++;\r
        line[++i] = 0;\r
        if(x == EOF) exit(0);\r
        DPRINT("# engine said: %s", line), fflush(stdout);\r
-       sscanf(line, "%s", command);\r
+       if(sscanf(line, "%s", command) != 1) continue;\r
        if(!strcmp(command, "bestmove")) {\r
            if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
            // move was a move to be played\r
@@ -225,6 +239,8 @@ Engine2GUI()
            if(p = strstr(line+6, " max "))  sscanf(p+1, "max %d", &max), *p = '\n';\r
            if(p = strstr(line+6, " default "))  sscanf(p+1, "default %[^\n]*", val), *p = '\n';\r
            if(p = strstr(line+6, " name ")) sscanf(p+1, "name %[^\n]*", name);\r
+           if(!strcmp(name, "Threads")) { strcpy(threadOpt, name); continue; }\r
+           if(!strcmp(name, "Ponder") || !strcmp(name, "USI_Ponder")) { strcpy(canPonder, name); continue; }\r
            if(!strcmp(name, "Hash") || !strcmp(name, "USI_Hash")) {\r
                memory = oldMem = atoi(val); hasHash = 1; \r
                strcpy(hashOpt, name);\r
@@ -335,7 +351,7 @@ GUI2Engine()
            int sec = 0;\r
            sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 ||\r
            sscanf(line, "level %d %d %d", &mps, &tc, &inc);\r
-           tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0;\r
+           tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit;\r
        }\r
        else if(!strcmp(command, "option")) {\r
            char name[80], *p;\r
@@ -354,7 +370,7 @@ GUI2Engine()
            Sync(PAUSE); // wait for uciok\r
        }\r
        else if(!strcmp(command, "setboard")) {\r
-               if(strstr(line+9, " b ")) stm = BLACK;\r
+               stm = (strstr(line+9, " b ") ? BLACK : WHITE);\r
                 if(p = strchr(line+9, '[')) { char c;\r
                     *p++ = 0; q = strchr(p, ']'); *q = 0; r = q + 4; \r
                    if(sc == 's') q[2] = 'w' + 'b' - q[2], strcpy(r=q+3, " 1\n"); // Shogi: reverse color\r
@@ -398,17 +414,17 @@ GUI2Engine()
        else if(!strcmp(command, "exit"))   computer = NONE, StopPonder(1);\r
        else if(!strcmp(command, "force"))  computer = NONE, StopPonder(pondering);\r
        else if(!strcmp(command, "go"))     computer = stm;\r
-       else if(!strcmp(command, "time"))   sscanf(line+4, "%d", &myTime),  myTime  *= 10;\r
-       else if(!strcmp(command, "otim"))   sscanf(line+4, "%d", &hisTime), hisTime *= 10;\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, "post"))   post = 1;\r
        else if(!strcmp(command, "nopost")) post = 0;\r
-       else if(!strcmp(command, "easy"))   ponder = 0, StopPonder(pondering);\r
-       else if(!strcmp(command, "hard"))   ponder = 1, StartPonder();\r
+       else if(!strcmp(command, "easy") && !!*canPonder) ponder = 0, StopPonder(pondering), fprintf(toE, "setoption name %s value false\n", canPonder);\r
+       else if(!strcmp(command, "hard") && !!*canPonder) ponder = 1, fprintf(toE, "setoption name %s value true\n", canPonder), StartPonder();\r
        else if(!strcmp(command, "ping"))   pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5);\r
        else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
-       else if(!strcmp(command, "cores"))  sscanf(line, "cores %d", &cores);\r
+       else if(!strcmp(command, "cores")&& !!*threadOpt) sscanf(line, "cores %d", &cores), fprintf(toE, "setoption name %s value %d\n", threadOpt, cores);\r
        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;\r
+       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0, sTime /= unit;\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\r