Improve efficiency
[uci2wb.git] / UCI2WB.c
index 997423f..9b5e70c 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -441,16 +441,25 @@ GUI2Engine()
        line[++i] = 0; if(x == EOF) { 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, "easy")) {\r
-           if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue;\r
-       }\r
-       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, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly\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, "option")) {\r
+       else if(!strcmp(command, "pause")) {\r
+           if(computer == stm) myTime -= GetTickCount() - startTime;\r
+           suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove\r
+           StopPonder(pondering || computer == stm);\r
+       }\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
+         }\r
+         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
            if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else\r
@@ -463,15 +472,30 @@ GUI2Engine()
            } 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(!think && Release()) break; // break will restart analysis; pondering is restarted by Release itself\r
+         }\r
+         else difficult = 1; // difficult command; terminate loop for easy ones\r
        }\r
-       else if(!strcmp(command, "pause")) {\r
-           if(computer == stm) myTime -= GetTickCount() - startTime;\r
-           suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove\r
-           StopPonder(pondering || computer == stm);\r
-       }\r
-       else difficult = 1; // difficult command; terminate loop for easy ones\r
        } // next command\r
 \r
+       // some commands that should never come during thinking can be safely processed here\r
+       if(difficult < 0) { // used as kludge to signal "usermove" was already matched\r
+           sscanf(line, "usermove %s", command); // strips off linefeed\r
+           Move4Engine(command);\r
+           stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0;\r
+           // when pondering we either continue the ponder search as normal search, or abort it\r
+           if(pondering || computer == ANALYZE) {\r
+               if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
+                   char *draw = drawOffer ? " draw" : ""; drawOffer = 0;\r
+                   pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now\r
+                   EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout);\r
+                   think = 2; // request blocking input during thinking\r
+                   goto nomove;\r
+               }\r
+               StopPonder(1); searching = 0;\r
+           }\r
+           strcpy(move[moveNr++], command); // possibly overwrites ponder move\r
+           continue;\r
+       }\r
        if(!strcmp(command, "resume")) {\r
            if(suspended == 2) StartPonder(); // restart interrupted ponder search\r
            suspended = think = 0; continue;  // causes thinking to start in normal way if on move or analyzing\r
@@ -494,23 +518,6 @@ GUI2Engine()
            Sync(PAUSE); // wait for readyok\r
            EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE);\r
        }\r
-       else if(!strcmp(command, "usermove")) {\r
-           sscanf(line, "usermove %s", command); // strips off linefeed\r
-           Move4Engine(command);\r
-           stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0;\r
-           // when pondering we either continue the ponder search as normal search, or abort it\r
-           if(pondering || computer == ANALYZE) {\r
-               if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
-                   char *draw = drawOffer ? " draw" : ""; drawOffer = 0;\r
-                   pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now\r
-                   EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout);\r
-                   think = 2; // request blocking input during thinking\r
-                   goto nomove;\r
-               }\r
-               StopPonder(1); searching = 0;\r
-           }\r
-           strcpy(move[moveNr++], command); // possibly overwrites ponder move\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
@@ -567,8 +574,6 @@ GUI2Engine()
        else if(!strcmp(command, "exit"))   computer = NONE, StopPonder(1), searching = 0, Analyze("false");\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*myTime)/unit;\r
-       else if(!strcmp(command, "otim"))   sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit;\r
        else if(!strcmp(command, "ping"))   { /* static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1;*/ printf("po%s", line+2); }\r
        else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
        else if(!strcmp(command, "cores")&& !!*threadOpt) { sscanf(line, "cores %d", &cores); EPRINT((f, "# setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores)) }\r