Do not transform FEN when UCI_Variant option is supported
[uci2wb.git] / UCI2WB.c
index c3129f5..37f9de6 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -9,7 +9,7 @@
 /* of which you should have received a copy together with this file.        */\r
 /****************************************************************************/\r
 \r
-#define VERSION "2.1"\r
+#define VERSION "2.2"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #define NONE  2\r
 #define ANALYZE 3\r
 \r
-char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', suffix[81], *variants;\r
+char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', suffix[81], *variants, varOpt;\r
 int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug, flob;\r
 int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500], frc, byo = -1;\r
-char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20];\r
+char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000];\r
 char board[100];  // XQ board for UCCI\r
 char *nameWord = "name ", *valueWord = "value ", *wTime = "w", *bTime = "b", *wInc = "winc", *bInc = "binc", newGame; // keywords that differ in UCCI\r
 int unit = 1, drawOffer;\r
@@ -141,13 +141,14 @@ StartSearch(char *ponder)
        int x = (ponder[0] != 0);                   // during ponder stm is the opponent\r
        int black = (stm == BLACK ^ x ^ sc == 's'); // set if our color is what the engine calls black\r
        int nr = moveNr + x;                        // we ponder for one move ahead!\r
+       int t = (flob ? inc + myTime/40 : 1000*byo*(byo>0)); // byoyomi time \r
        if(sc == 'x') black = 1; else drawOffer = 0;// in UCCI 'black' refers to us and 'white' to opponent\r
        if(!x && drawOffer) ponder = " draw", drawOffer = 0; //pass draw offer only when not pondering\r
-       fprintf(toE, "\ngo%s %stime %d %stime %d", ponder, bTime, black ? myTime : hisTime, wTime, !black ? myTime : hisTime);\r
-       DPRINT(    "\n# go%s %stime %d %stime %d", ponder, bTime, black ? myTime : hisTime, wTime, !black ? myTime : hisTime);\r
+       fprintf(toE, "\ngo%s %stime %d %stime %d", ponder, bTime, (black ? myTime : hisTime) - t, wTime, (!black ? myTime : hisTime) - t);\r
+       DPRINT(    "\n# go%s %stime %d %stime %d", ponder, bTime, (black ? myTime : hisTime) - t, wTime, (!black ? myTime : hisTime) - t);\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(flob || byo >= 0) sprintf(suffix, " byoyomi %d", byo >=0 ? 1000*byo : inc + myTime/40); // for engines running purely on byoyomi\r
+       if(flob || byo >= 0) sprintf(suffix, " byoyomi %d", t); // for engines running purely on byoyomi\r
        if(inc && !*suffix) { fprintf(toE, " %s %d %s %d", wInc, inc, bInc, inc); DPRINT(" %s %d %s %d", wInc, inc, bInc, 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
@@ -251,6 +252,7 @@ Engine2GUI()
            // move was a move to be played\r
            if(p = strstr(line+8, " draw")) *p = 0, printf("offer draw\n"); // UCCI\r
            if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; }\r
+           if(strstr(line+9, "win")) { printf("%s {claim}\n", stm== WHITE ? "1-0" :"0-1"); computer = NONE; } // USI\r
            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
@@ -310,6 +312,7 @@ Engine2GUI()
            if(p = strstr(line+6, " default "))  sscanf(p+1, "default %[^\n]*", val), *p = '\n';\r
            if(!(p = strstr(line+6, " name "))) p = line+1; sscanf(p+6, "%[^\n]", name); // 'name' is omitted in UCCI\r
            if(!strcasecmp(name, "UCI_Chess960")) { frc=2; continue; }\r
+           if(!strcasecmp(name, "UCI_Variant")) { if(p = strstr(line+6, " var ")) strcpy(varList, p); varOpt = 1; continue; }\r
            if(frc< 0 && (strstr(name, "960") || strcasestr(name, "frc")) && !strcmp(type, "check")) {\r
                fprintf(toE, "setoption name %s value true\n", name); strcpy(val, "true"); // set non-standard suspected FRC options\r
            }\r
@@ -349,7 +352,13 @@ Engine2GUI()
        }\r
        else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands\r
        else if(sc == 'x'&& !strcmp(command, "ucciok") || sscanf(command, "u%ciok", &c)==1 && c==sc) {\r
-           if(frc) printf("feature variants=\"normal,fischerandom\" oocastle=%d\n", frc<0); // unannounced FRC uses O-O castling\r
+           char *p = varList, *q = varList;\r
+           while(*q && *q != '\n')  if(!strncmp(q, " var ", 5)) *p++ = ',', q +=5; // replace var keywords by commas\r
+                               else if(!strncmp(q-1, " chess ", 7)) strcpy(p, "normal"), p += 6, q += 5; // 'chess' is called 'normal' in CECP\r
+                               else if(!strncmp(q-1, " threecheck", 11)) *p++ = '3', q += 5; // 'threecheck' is written '3check' 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
            printf("feature smp=1 memory=%d done=1\n", hasHash);\r
            if(unit == 2) unit = 1, fprintf(toE, "setoption usemillisec true\n");\r
            Sync(WAKEUP); // done with options\r
@@ -378,7 +387,7 @@ Move4Engine(char *m)
 void\r
 GUI2Engine()\r
 {\r
-    char line[256], command[256], *p, *q, *r;\r
+    char line[256], command[256], *p, *q, *r, mySide;\r
 \r
     while(1) {\r
        int i, x;\r
@@ -387,7 +396,7 @@ GUI2Engine()
            DPRINT("# start search\n");\r
            LoadPos(moveNr); fflush(stdout); // load position\r
            // and set engine thinking (note USI swaps colors!)\r
-           startTime = GetTickCount();\r
+           startTime = GetTickCount(); mySide = stm; // remember side we last played for\r
            if(computer == ANALYZE) {\r
                fprintf(toE, "\ngo infinite"); DPRINT("\n# go infinite");\r
                if(sm & 1) { // some moves are disabled\r
@@ -410,6 +419,7 @@ GUI2Engine()
            oldMem = memory;\r
            // we can set other options here\r
            if(sc == 'x') { if(newGame) fprintf(toE, "setoption newgame\n"); } else // optional in UCCI\r
+           if(varOpt) fprintf(toE, "setoption name UCI_Variant value chess\n");\r
            pause = 1; // wait for option settings to take effect\r
            fprintf(toE, "isready\n"); fflush(toE);\r
            Sync(PAUSE); // wait for readyok\r
@@ -459,7 +469,7 @@ GUI2Engine()
        }\r
        else if(!strcmp(command, "setboard")) {\r
                stm = (strstr(line+9, " b ") ? BLACK : WHITE);\r
-                if(p = strchr(line+9, '[')) { char c;\r
+                if((p = strchr(line+9, '[')) && !varOpt) { 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
                    else r = strchr(strchr(q+4, ' ') + 1, ' '); // skip to second space (after e.p. square)\r
@@ -470,6 +480,7 @@ GUI2Engine()
                iniPos[strlen(iniPos)-1] = sm = 0; collect = (computer == ANALYZE);\r
        }\r
        else if(!strcmp(command, "variant")) {\r
+               if(varOpt) fprintf(toE, "setoption name UCI_Variant value %sucinewgame\n", strcmp(line+8, "3check\n") ? line+8 : "threecheck\n");\r
                if(!strcmp(line+8, "shogi\n")) size = 9, strcpy(iniPos, "position startpos");\r
                if(!strcmp(line+8, "5x5+5_shogi\n")) size = 5, strcpy(iniPos, "position startpos");\r
                if(!strcmp(line+8, "xiangqi\n")) strcpy(iniPos, "fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r");\r
@@ -516,6 +527,7 @@ GUI2Engine()
        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, sTime /= unit;\r
        else if(!strcmp(command, "offer"))  drawOffer = 1;\r
+       else if(!strcmp(command, "result")) { if(sc == 's') fprintf(toE, "gameover %s\n", line[8] == '/' ? "draw" : (line[7] == '0') == mySide ? "win" : "lose"); }\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\r