Implement pause / resume commands
[uci2wb.git] / UCI2WB.c
index 71dfd4b..fbd9d74 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -1,6 +1,6 @@
 /************************* UCI2WB by H.G.Muller ****************************/\r
 \r
-#define VERSION "1.8"\r
+#define VERSION "1.10"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -30,9 +30,9 @@
 #define NONE  2\r
 #define ANALYZE 3\r
 \r
-char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix, *variants;\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, 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]; char currMove[20], moveMap[500][10]; // for analyze mode\r
 \r
 FILE *toE, *fromE;\r
 int pid;\r
@@ -139,8 +139,6 @@ Move4GUI(char *m)
             m[4] = '=', m[5] = 0;\r
       }\r
     }\r
-    if(m[0] == 'e' && (m[5] == 'a' || m[5] == 'h')) // seirawan castling + gating at Rook\r
-       m[0] = m[5], m[2] = 'e'; // convert to RxK notation\r
 }\r
 \r
 void *\r
@@ -191,7 +189,10 @@ Engine2GUI()
                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) { collect = 3; continue; } // done collecting\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
@@ -265,12 +266,7 @@ Move4Engine(char *m)
       m[2] = 'a'+'0'+size - m[2];\r
       m[3] = 'a'+'0'+size - m[3];\r
       if(m[4] == '=') m[4] = 0; // no '=' in USI format!\r
-      else if(m[4] != '\n') m[4] = '+'; // cater to WB 4.4 bug :-(\r
-    }\r
-    if(m[4] && (m[1] == '1' || m[1] == '8')) { // seirawan gating\r
-       m[5] = m[0], m[6] = m[1], m[7] = '\0'; // copy from-square behind it, as gating square\r
-       if(m[2] == 'e' && (m[0] == 'a' || m[0] == 'h'))\r
-           m[2] = (m[0]+m[2]+1)>>1, m[0] = 'e'; // gating at Rook\r
+      else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-(\r
     }\r
 }\r
 \r
@@ -282,7 +278,7 @@ GUI2Engine()
     while(1) {\r
        int i, x;\r
 \r
-       if(computer == stm || computer == ANALYZE) {\r
+       if((computer == stm || computer == ANALYZE) && !suspended) {\r
            DPRINT("# start search\n");\r
            LoadPos(moveNr); // load position\r
            // and set engine thinking (note USI swaps colors!)\r
@@ -345,7 +341,7 @@ GUI2Engine()
        }\r
        else if(!strcmp(command, "protover")) {\r
            if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS;\r
-           printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 done=0\n", variants);\r
+           printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 pause=1 done=0\n", variants);\r
            printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
            fprintf(toE, "u%ci\n", sc); fflush(toE); // this prompts UCI engine for options\r
            Sync(PAUSE); // wait for uciok\r
@@ -374,13 +370,25 @@ GUI2Engine()
            printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
            goto nomove;\r
        }\r
-       else if(!strcmp(command+2, "clude") && collect > 2) {\r
+       else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude\r
            int all = !strcmp(line+8, "all"), in = command[1] == 'n';\r
-           line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag\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)) goto nomove; // no moves enabled; continue current search\r
            if(computer == ANALYZE) StopPonder(1); // abort old analysis\r
        }\r
+       else if(!strcmp(command, "pause")) {\r
+           if(computer == stm) myTime -= GetTickCount() - startTime;\r
+           suspended = 1 + pondering, StopPonder(1); // remember if we were pondering, and stop search ignoring bestmove\r
+       }\r
+       else if(!strcmp(command, "resume")) {\r
+           if(suspended == 2) { // restart interrupted ponder search\r
+               LoadPos(moveNr+1);\r
+               pondering = 1; lastDepth = 1;\r
+               StartSearch(" ponder");\r
+           }\r
+           suspended = 0; // causes thinking to start in normal way if on move or analyzing\r
+       }\r
        else if(!strcmp(command, "xboard")) ;\r
        else if(!strcmp(command, "analyze"))computer = ANALYZE, collect = 1, sm = 0;\r
        else if(!strcmp(command, "exit"))   computer = NONE, StopPonder(1);\r