X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=ff5ec8cdfb142053da5a38a346c25fb35b47d3d9;hp=d7350a97aa43b25d8b658f95d3d49f35e22393c7;hb=7bd5b32b970229a4a9cc23ca2e8849e30c6d9823;hpb=5a5534346fd682f786f70694ab3e77d0ea653e3d diff --git a/UCI2WB.c b/UCI2WB.c index d7350a9..ff5ec8c 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,15 @@ -/************************* UCI2WB by H.G.Muller ****************************/ - -#define VERSION "1.5" +/****************************************************************************/ +/* UCI2WB by H.G.Muller */ +/* */ +/* UCI2WB is an adapter to run engines that communicate in various dialects */ +/* of the Universal Chess Interface in a GUI that supports XBoard protocol */ +/* (CECP). It supports UCI (when used for Xiangqi: the 'Cyclone dialect'), */ +/* as well as USI and UCCI when used with the flags -s or -x, respectively. */ +/* This version of UCI2WB is released under the GNU General Public License, */ +/* of which you should have received a copy together with this file. */ +/****************************************************************************/ + +#define VERSION "2.1" #include #include @@ -30,11 +39,15 @@ #define NONE 2 #define ANALYZE 3 -char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix; -int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug; -int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode +char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', suffix[81], *variants; +int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug, flob; +int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500], frc, byo = -1; +char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20]; +char board[100]; // XQ board for UCCI +char *nameWord = "name ", *valueWord = "value ", *wTime = "w", *bTime = "b", *wInc = "winc", *bInc = "binc", newGame; // keywords that differ in UCCI +int unit = 1, drawOffer; -FILE *toE, *fromE; +FILE *toE, *fromE, *fromF; int pid; #ifdef WIN32 @@ -77,18 +90,69 @@ Sync (int action) } void +FromFEN(char *fen) +{ int i=0; + while(*fen) { + char c = *fen++; + if(c >= 'A') board[i++] = c; else + if(c == '/') i++; else + if(c == ' ') break; else + while(c-- > '0' && i < 99) board[i++] = 0; + if(i >= 99) break; + } +} + +char * +ToFEN(int stm) +{ + int i, n=0; static char fen[200]; char *p = fen; + for(i=0; i<99; i++) { + char c = board[i]; + if(c >= 'A') { if(n) *p++ = '0' + n; n = 0; *p++ = c; } else n ++; + if(i%10 == 8) { if(n) *p++ = '0' + n; n = -1; *p++ = '/'; } + } + sprintf(p-1, " %c - - 0 1", stm); + return fen; +} + +int +Sqr(char *m, int j) +{ + int n = m[j] - 'a' + 10*('9' - m[j+1]); + if(n < 0) n = 0; else if(n > 99) n = 99; return n; +} + +int +Play(int nr) +{ + int i, last = -1; + FromFEN(iniPos + 4); // in XQ iniPos always has just "fen " prefix + for(i=0; i 0) fprintf(toE, " movetime %d", sTime),printf(" movetime %d", sTime); else - if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); - if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc); - if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth); - if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc); - fprintf(toE, "%s\n", ponder); - printf("%s\n", ponder); + int x = (ponder[0] != 0); // during ponder stm is the opponent + int black = (stm == BLACK ^ x ^ sc == 's'); // set if our color is what the engine calls black + int nr = moveNr + x; // we ponder for one move ahead! + int t = (flob ? inc + myTime/40 : 1000*byo*(byo>0)); // byoyomi time + if(sc == 'x') black = 1; else drawOffer = 0;// in UCCI 'black' refers to us and 'white' to opponent + if(!x && drawOffer) ponder = " draw", drawOffer = 0; //pass draw offer only when not pondering + fprintf(toE, "\ngo%s %stime %d %stime %d", ponder, bTime, (black ? myTime : hisTime) - t, wTime, (!black ? myTime : hisTime) - t); + DPRINT( "\n# go%s %stime %d %stime %d", ponder, bTime, (black ? myTime : hisTime) - t, wTime, (!black ? myTime : hisTime) - t); + if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else + if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); } + if(flob || byo >= 0) sprintf(suffix, " byoyomi %d", t); // for engines running purely on byoyomi + if(inc && !*suffix) { fprintf(toE, " %s %d %s %d", wInc, inc, bInc, inc); DPRINT(" %s %d %s %d", wInc, inc, bInc, inc); } + if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); } + if(*suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); } + fprintf(toE, "\n"); DPRINT("\n"); } void @@ -103,10 +167,25 @@ StopPonder(int pondering) void LoadPos(int moveNr) { - int j; - fprintf(toE, "%s moves", iniPos); - DPRINT( "# %s moves", iniPos); - for(j=0; j'0'+size-size/3 || m[3]>'0'+size-size/3) + : (m[1] <= '0'+size/3 || m[3] <= '0'+size/3)) && m[4] != '+') + m[4] = '=', m[5] = 0; + } + } +} + +int +GetChar() +{ + int c; + if(fromF) { + if((c = fgetc(fromF)) != EOF) return c; + fclose(fromF); fromF = 0; printf("# end fake\n"); + } + return fgetc(fromE); +} + void * Engine2GUI() { char line[1024], command[256]; + if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); while(1) { - int i=0, x; char *p; + int i=0, x; char *p, dummy; fflush(stdout); fflush(toE); - while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++; + while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; line[++i] = 0; if(x == EOF) exit(0); - DPRINT("# engine said: %s", line); fflush(stdout); - sscanf(line, "%s", command); + DPRINT("# engine said: %s", line), fflush(stdout); + if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { - if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. + if(pause == 1) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. + else if(pondering) { pondering = 0; printf("%d 0 0 0 UCI violation! Engine moves during ponder\n", lastDepth+1); continue; } // ignore ponder search // move was a move to be played + if(p = strstr(line+8, " draw")) *p = 0, printf("offer draw\n"); // UCCI if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; } + if(strstr(line+9, "win")) { printf("%s {claim}\n", stm== WHITE ? "1-0" :"0-1"); computer = NONE; } // USI if(strstr(line+9, "(none)") || strstr(line+9, "null") || strstr(line+9, "0000")) { printf("%s\n", lastScore < -99999 ? "resign" : "1/2-1/2 {stalemate}"); computer = NONE; } sscanf(line, "bestmove %s", move[moveNr++]); myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts + stm = WHITE+BLACK - stm; // first start a new ponder search, if pondering is on and we have a move to ponder on if(p = strstr(line+9, "ponder")) { + sscanf(p+7, "%s", move[moveNr]); if(computer != NONE && ponder) { - sscanf(p+7, "%s", move[moveNr]); DPRINT("# ponder on %s\n", move[moveNr]); - LoadPos(moveNr+1); // load position - // and set engine pondering - pondering = 1; lastDepth = 1; - StartSearch(" ponder"); + StartPonder(); } p[-1] = '\n'; *p = 0; // strip off ponder move - } - if(sc == 's') { - // convert USI move to WB format - line[11] = 'a'+'0'+size - line[11]; - line[12] = 'a'+'0'+size - line[12]; - if(line[10] == '*') { // drop - line[10] = '@'; - } else { - line[9] = 'a'+'0'+size - line[9]; - line[10] = 'a'+'0'+size - line[10]; - if((stm == WHITE ? (line[10]>'0'+size-size/3 || line[12]>'0'+size-size/3) - : (line[10] <= '0'+size/3 || line[12] <= '0'+size/3)) && line[13] != '+') - line[13] = '=', line[14] = 0; - } - } + } else move[moveNr][0] = 0; + Move4GUI(line+9); printf("move %s\n", line+9); // send move to GUI - if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; } - stm = WHITE+BLACK - stm; + if(move[moveNr][0]) printf("Hint: %s\n", move[moveNr]); + if(pause) { pause = 0; Sync(WAKEUP); } // release commands that came in during think + if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; } } else if(!strcmp(command, "info")) { - int d=0, s=0, t=0, n=0; + int d=0, s=0, t=(GetTickCount() - startTime)/10, n=1; char *pv; + if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; } + if(collect && (pv = strstr(line+5, "currmove "))) { + if(p = strstr(line+5, "currmovenumber ")) { + n = atoi(p+15); + if(collect == 1 && n != 1) continue; // wait for move 1 + if(collect + (n == 1) > 2) { // done collecting + if(inex && collect == 2) printf("%d 0 0 0 OK to exclude\n", lastDepth); + collect = 3; continue; + } + collect = 2; on[nr=n] = 1; sscanf(pv+9, "%s", moveMap[n]); continue; // store move + } + } if(!post) continue; - if(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else { + if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else { if(p = strstr(line+4, " depth ")) sscanf(p+7, "%d", &d), statDepth = d; if(p = strstr(line+4, " score cp ")) sscanf(p+10, "%d", &s), statScore = s; else if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else @@ -199,12 +310,20 @@ Engine2GUI() if(p = strstr(line+6, " min ")) sscanf(p+1, "min %d", &min), *p = '\n'; if(p = strstr(line+6, " max ")) sscanf(p+1, "max %d", &max), *p = '\n'; if(p = strstr(line+6, " default ")) sscanf(p+1, "default %[^\n]*", val), *p = '\n'; - if(p = strstr(line+6, " name ")) sscanf(p+1, "name %[^\n]*", name); - if(!strcmp(name, "Hash") || !strcmp(name, "USI_Hash")) { + if(!(p = strstr(line+6, " name "))) p = line+1; sscanf(p+6, "%[^\n]", name); // 'name' is omitted in UCCI + if(!strcasecmp(name, "UCI_Chess960")) { frc=2; continue; } + if(frc< 0 && (strstr(name, "960") || strcasestr(name, "frc")) && !strcmp(type, "check")) { + fprintf(toE, "setoption name %s value true\n", name); strcpy(val, "true"); // set non-standard suspected FRC options + } + if(!strcasecmp(name, "Threads")) { strcpy(threadOpt, name); continue; } + if(!strcasecmp(name, "Ponder") || !strcasecmp(name, "USI_Ponder")) { strcpy(canPonder, name); continue; } + if(!strcasecmp(name, "Hash") || !strcasecmp(name, "USI_Hash") || !strcasecmp(name, "hashsize")) { memory = oldMem = atoi(val); hasHash = 1; strcpy(hashOpt, name); continue; } + if(!strcasecmp(name, "newgame") && !strcmp(type, "button")) { newGame++; continue; } + if(!strcasecmp(name, "usemillisec")) { unit = (!strcmp(val, "false") ? 2 : 1); continue; } // pass on engine-defined option as WB option feature if(!strcmp(type, "filename")) type[4] = 0; sprintf(buf, "feature option=\"%s -%s", name, type); q = buf + strlen(buf); @@ -226,130 +345,180 @@ Engine2GUI() if(buf[0]) printf("%s", buf); } else if(!strcmp(command, "id")) { - char name[256]; - if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); + static char name[256], version[256]; + if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); + if(sscanf(line, "id version %[^\n]", version) == 1 && *name) printf("feature myname=\"%s %s (U%cI2WB)\"\n", name, version, sc-32); } else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands - else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options + else if(sc == 'x'&& !strcmp(command, "ucciok") || sscanf(command, "u%ciok", &c)==1 && c==sc) { + if(frc) printf("feature variants=\"normal,fischerandom\" oocastle=%d\n", frc<0); // unannounced FRC uses O-O castling + printf("feature smp=1 memory=%d done=1\n", hasHash); + if(unit == 2) unit = 1, fprintf(toE, "setoption usemillisec true\n"); + Sync(WAKEUP); // done with options + } + } +} + +void +Move4Engine(char *m) +{ + if(sc == 's') { + // convert input move to USI format + if(m[1] == '@') { // drop + m[1] = '*'; + } else { + m[0] = 'a'+'0'+size - m[0]; + m[1] = 'a'+'0'+size - m[1]; + } + m[2] = 'a'+'0'+size - m[2]; + m[3] = 'a'+'0'+size - m[3]; + if(m[4] == '=') m[4] = 0; // no '=' in USI format! + else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-( } } void GUI2Engine() { - char line[256], command[256], *p, *q; + char line[256], command[256], *p, *q, *r, mySide; while(1) { int i, x; - if(computer == stm || computer == ANALYZE) { + if((computer == stm || computer == ANALYZE) && !suspended) { DPRINT("# start search\n"); - LoadPos(moveNr); // load position + LoadPos(moveNr); fflush(stdout); // load position // and set engine thinking (note USI swaps colors!) - startTime = GetTickCount(); - if(computer == ANALYZE) fprintf(toE, "\ngo infinite\n"), printf("\ngo infinite\n"); - else StartSearch(""); + startTime = GetTickCount(); mySide = stm; // remember side we last played for + if(computer == ANALYZE) { + fprintf(toE, "\ngo infinite"); DPRINT("\n# go infinite"); + if(sm & 1) { // some moves are disabled + fprintf(toE, " searchmoves"); DPRINT(" searchmoves"); + for(i=1; i 0) fprintf(toE, "setoption name UCI_Chess960 value true\n"); } } else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) { if(pondering || computer == ANALYZE) StopPonder(1); - moveNr = moveNr > i ? moveNr - i : 0; + moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0; } else if(!strcmp(command, ".")) { printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove); goto nomove; } + else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude + int all = !strcmp(line+8, "all"), in = command[1] == 'n'; + inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag + for(i=1; i 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; } + if(argc > 1 && !strcmp(argv[1], "-var")) { variants = argv[2]; argc-=2; argv+=2; } if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; } if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] []\n", sc-32); exit(-1); } if(argc > 2) dir = argv[2]; - if(argc > 3) suffix = argv[3]; + if(argc > 3) strncpy(suffix, argv[3], 80); + + if(sc == 'x') nameWord = valueWord = bTime = "", wTime = "opp", bInc = "increment", wInc = "oppincrement", unit = 1000; // switch to UCCI keywords + else if(sc == 'f' ) frc = -1, sc = 'c'; // UCI for unannounced Chess960 + else if(sc == 'n') sc = 'c'; // UCI for normal Chess // spawn engine proc if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); } @@ -459,7 +633,7 @@ main(int argc, char **argv) #ifdef WIN32 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id); #else - { pthread_t t; signal(SIGINT, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); } + { pthread_t t; signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); } #endif // handle GUI->engine traffic in original thread