Translate S-Chess gating moves
[uci2wb.git] / UCI2WB.c
index d7350a9..26010a2 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -1,6 +1,6 @@
 /************************* UCI2WB by H.G.Muller ****************************/\r
 \r
-#define VERSION "1.5"\r
+#define VERSION "1.7"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -30,7 +30,7 @@
 #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;\r
+char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, 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; char currMove[20]; // for analyze mode\r
 \r
@@ -82,13 +82,13 @@ StartSearch(char *ponder)
        int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead!\r
        fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
        DPRINT(    "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
-       if(sTime > 0) fprintf(toE, " movetime %d", sTime),printf(" movetime %d", sTime); else\r
-       if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2);\r
-       if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc);\r
-       if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth);\r
-        if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc);\r
+       if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else\r
+       if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); }\r
+       if(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); }\r
+       if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); }\r
+        if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); }\r
        fprintf(toE, "%s\n", ponder);\r
-       printf("%s\n", ponder);\r
+       DPRINT("%s\n", ponder);\r
 }\r
 \r
 void\r
@@ -106,7 +106,7 @@ LoadPos(int moveNr)
        int j;\r
        fprintf(toE, "%s moves", iniPos);\r
        DPRINT(    "# %s moves", iniPos);\r
-       for(j=0; j<moveNr; j++) fprintf(toE, " %s", move[j]),printf(" %s", move[j]);\r
+       for(j=0; j<moveNr; j++) { fprintf(toE, " %s", move[j]); DPRINT(" %s", move[j]); }\r
 }\r
 \r
 char *Convert(char *pv)\r
@@ -128,13 +128,13 @@ Engine2GUI()
     char line[1024], command[256];\r
 \r
     while(1) {\r
-       int i=0, x; char *p;\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
        line[++i] = 0;\r
        if(x == EOF) exit(0);\r
-       DPRINT("# engine said: %s", line); fflush(stdout);\r
+       DPRINT("# engine said: %s", line), fflush(stdout);\r
        sscanf(line, "%s", command);\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
@@ -171,6 +171,8 @@ Engine2GUI()
                     line[13] = '=', line[14] = 0;\r
              }\r
            }\r
+           if(line[9] == 'e' && (line[14] == 'a' || line[14] == 'h')) // seirawan castling + gating at Rook\r
+               line[9] = line[14], line[11] = 'e'; // convert to RxK notation\r
            printf("move %s\n", line+9); // send move to GUI\r
            if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; }\r
            stm = WHITE+BLACK - stm;\r
@@ -178,8 +180,9 @@ Engine2GUI()
        else if(!strcmp(command, "info")) {\r
            int d=0, s=0, t=0, n=0;\r
            char *pv;\r
+           if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; }\r
            if(!post) continue;\r
-           if(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
+           if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
                if(p = strstr(line+4, " depth "))      sscanf(p+7, "%d", &d), statDepth = d;\r
                if(p = strstr(line+4, " score cp "))   sscanf(p+10, "%d", &s), statScore = s; else\r
                if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else\r
@@ -227,7 +230,7 @@ Engine2GUI()
        }\r
        else if(!strcmp(command, "id")) {\r
            char name[256];\r
-           if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
+           if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
        }\r
        else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands\r
        else if(sscanf(command, "u%ciok", &c)==1 && c==sc)   printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options\r
@@ -281,6 +284,11 @@ GUI2Engine()
              else if(line[13] != '\n') line[13] = '+'; // cater to WB 4.4 bug :-(\r
            }\r
            sscanf(line, "usermove %s", command); // strips off linefeed\r
+           if(command[4] && (command[1] == '1' || command[1] == '8')) { // seirawan gating\r
+               command[5] = command[0], command[6] = command[1], command[7] = '\0';\r
+               if(command[2] == 'e' && (command[0] == 'a' || command[0] == 'h'))\r
+                   command[2] = (command[0]+command[2]+1)>>1, command[0] = 'e'; // gating at Rook\r
+           }\r
            stm = WHITE+BLACK - stm;\r
            // when pondering we either continue the ponder search as normal search, or abort it\r
            if(pondering || computer == ANALYZE) {\r
@@ -291,7 +299,8 @@ GUI2Engine()
                }\r
                StopPonder(1);\r
            }\r
-           sscanf(line, "usermove %s", move[moveNr++]); // possibly overwrites ponder move\r
+//         sscanf(line, "usermove %s", move[moveNr++]); // possibly overwrites ponder move\r
+           strcpy(move[moveNr++], command);\r
        }\r
        else if(!strcmp(command, "level")) {\r
            int sec = 0;\r
@@ -309,7 +318,8 @@ GUI2Engine()
            } else { fprintf(toE, "setoption name %s\n", line+7); DPRINT("# setoption name %s\n", line+7); }\r
        }\r
        else if(!strcmp(command, "protover")) {\r
-           printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS);\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 done=0\n", variants);\r
            printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
            fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options\r
        }\r
@@ -349,7 +359,7 @@ GUI2Engine()
        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, "sd"))     sscanf(line, "sd %d", &depth);\r
-       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime *= 1000, inc = 0;\r
+       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0;\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\r
@@ -445,6 +455,7 @@ main(int argc, char **argv)
 \r
        if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); }\r
        if(argc > 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; }\r
+       if(argc > 1 && !strcmp(argv[1], "-var")) { variants = argv[2]; argc-=2; argv+=2; }\r
        if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; }\r
        if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] <engine.exe> [<engine directory>]\n", sc-32); exit(-1); }\r
        if(argc > 2) dir = argv[2];\r