Debug recent changes
[uci2wb.git] / UCI2WB.c
index 8715276..1632b81 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -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
@@ -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
@@ -415,7 +416,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 +425,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
@@ -452,12 +453,16 @@ GUI2Engine()
        fflush(toE); fflush(stdout);\r
        if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n");\r
        sscanf(line, "%s", command);\r
+       DPRINT("# %s searching=%d\n", command, searching);\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, ".")) {\r
+           printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, 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
@@ -470,7 +475,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
@@ -517,15 +522,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
@@ -554,7 +559,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
@@ -587,15 +591,10 @@ DoCommand ()
        else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
            moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0;\r
        }\r
-       else if(!strcmp(command, ".")) {\r
-           printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\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)) 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
@@ -620,7 +619,7 @@ DoCommand ()
        }\r
        else if(!strcmp(command, "quit"))   { EPRINT((f, "# quit\n")) fflush(toE), exit(atoi(line+4)); }\r
 \r
-    return 0;\r
+       fflush(stdout);\r
 }\r
 \r
 void *\r
@@ -631,7 +630,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