X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=ff5ec8cdfb142053da5a38a346c25fb35b47d3d9;hp=654c749fa3bfe7a515587f314ecffb7c202dac14;hb=7bd5b32b970229a4a9cc23ca2e8849e30c6d9823;hpb=7ba87b6b2eceae92a147246af886d9cf19178057 diff --git a/UCI2WB.c b/UCI2WB.c index 654c749..ff5ec8c 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,15 @@ -/************************* UCI2WB by H.G.Muller ****************************/ - -#define VERSION "1.8" +/****************************************************************************/ +/* 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, *variants; -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, collect, nr, sm, inex, on[500]; char currMove[20], moveMap[500][10]; // 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; i0)); // 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(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); } + 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, "%s\n", ponder); - DPRINT("%s\n", ponder); + 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>1, m[0] = 'e'; // gating at Rook + else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-( } } void GUI2Engine() { - char line[256], command[256], *p, *q, *r; + 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(); + 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 @@ -298,23 +398,24 @@ GUI2Engine() } fprintf(toE, "\n"); DPRINT("\n"); // code for searchmoves goes here - } else StartSearch(""); + } else { pause = 2; StartSearch(""); fflush(stdout); fflush(toE); Sync(PAUSE); } // block input during thinking } nomove: fflush(toE); fflush(stdout); i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++; - line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); } + line[++i] = 0; if(x == EOF) { printf("# EOF\n"); fprintf(toE, "quit\n"); exit(-1); } sscanf(line, "%s", command); if(!strcmp(command, "new")) { - computer = BLACK; moveNr = 0; depth = -1; - stm = WHITE; strcpy(iniPos, "position startpos"); - if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s value %d\n", hashOpt, memory); + computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0; + stm = WHITE; strcpy(iniPos, "position startpos"); frc &= ~1; + if(memory != oldMem && hasHash) fprintf(toE, "setoption %s%s %s%d\n", nameWord, hashOpt, valueWord, memory); oldMem = memory; // we can set other options here + if(sc == 'x') { if(newGame) fprintf(toE, "setoption newgame\n"); } else // optional in UCCI pause = 1; // wait for option settings to take effect - fprintf(toE, "isready\n"); - fprintf(toE, "u%cinewgame\n", sc); fflush(toE); + fprintf(toE, "isready\n"); fflush(toE); Sync(PAUSE); // wait for readyok + fprintf(toE, "u%cinewgame\n", sc); fflush(toE); } else if(!strcmp(command, "usermove")) { sscanf(line, "usermove %s", command); // strips off linefeed @@ -323,8 +424,10 @@ GUI2Engine() // when pondering we either continue the ponder search as normal search, or abort it if(pondering || computer == ANALYZE) { if(pondering && !strcmp(command, move[moveNr])) { // ponder hit - pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now - fprintf(toE, "ponderhit\n"); DPRINT("# ponderhit\n"); + char *draw = drawOffer ? " draw" : ""; drawOffer = 0; + pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now + fprintf(toE, "ponderhit%s\n", draw); DPRINT("# ponderhit%s\n", draw); fflush(toE); fflush(stdout); + Sync(PAUSE); // block input during thinking goto nomove; } StopPonder(1); @@ -335,32 +438,36 @@ GUI2Engine() int sec = 0; sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 || sscanf(line, "level %d %d %d", &mps, &tc, &inc); - tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; + tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit; } else if(!strcmp(command, "option")) { char name[80], *p; if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else + if(sscanf(line+7, "Floating Byoyomi=%d", &flob) == 1) ; else + if(sscanf(line+7, "Byoyomi=%d", &byo) == 1) ; else if(p = strchr(line, '=')) { *p++ = 0; if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false"); - fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption name %s value %s", line+7, p); - } else { fprintf(toE, "setoption name %s\n", line+7); DPRINT("# setoption name %s\n", line+7); } + fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption %s%s %s%s", nameWord, line+7, valueWord, p); + } else { fprintf(toE, "setoption %s%s\n", nameWord, line+7); DPRINT("# setoption %s%s\n", nameWord, line+7); } } else if(!strcmp(command, "protover")) { if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS; - printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 done=0\n", variants); + printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 pause=1 sigint=0 sigterm=0 done=0\n", variants); printf("feature option=\"UCI2WB debug output -check %d\"\n", debug); - fprintf(toE, "u%ci\n", sc); fflush(toE); // this prompts UCI engine for options + if(sc == 's') printf("feature option=\"Floating Byoyomi -check %d\"\nfeature option=\"Byoyomi -spin %d -1 1000\"\n", flob, byo); + fprintf(toE, sc == 'x' ? "ucci\n" : "u%ci\n", sc); fflush(toE); // prompt UCI engine for options Sync(PAUSE); // wait for uciok } else if(!strcmp(command, "setboard")) { - if(strstr(line+9, " b ")) stm = BLACK; + stm = (strstr(line+9, " b ") ? BLACK : WHITE); if(p = strchr(line+9, '[')) { char c; *p++ = 0; q = strchr(p, ']'); *q = 0; r = q + 4; if(sc == 's') q[2] = 'w' + 'b' - q[2], strcpy(r=q+3, " 1\n"); // Shogi: reverse color else r = strchr(strchr(q+4, ' ') + 1, ' '); // skip to second space (after e.p. square) *r = 0; sprintf(command, "%s%s %s %s", line+9, q+1, p, r+1); } else strcpy(command, line+9); + if(frc == -1 && (p = strchr(command, ' '))) strncpy(p+3, "KQkq", 4); // unannounced FRC sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", command); iniPos[strlen(iniPos)-1] = sm = 0; collect = (computer == ANALYZE); } @@ -368,6 +475,7 @@ GUI2Engine() if(!strcmp(line+8, "shogi\n")) size = 9, strcpy(iniPos, "position startpos"); if(!strcmp(line+8, "5x5+5_shogi\n")) size = 5, strcpy(iniPos, "position startpos"); if(!strcmp(line+8, "xiangqi\n")) strcpy(iniPos, "fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r"); + if(!strcmp(line+8, "fischerandom\n")) { frc |= 1; if(frc > 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); @@ -384,22 +492,33 @@ GUI2Engine() if(!(sm & 2)) goto nomove; // no moves enabled; continue current search if(computer == ANALYZE) StopPonder(1); // abort old analysis } + else if(!strcmp(command, "pause")) { + if(computer == stm) myTime -= GetTickCount() - startTime; + suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove + StopPonder(pondering || computer == stm); + } + else if(!strcmp(command, "resume")) { + if(suspended == 2) StartPonder(); // restart interrupted ponder search + suspended = 0; // causes thinking to start in normal way if on move or analyzing + } else if(!strcmp(command, "xboard")) ; else if(!strcmp(command, "analyze"))computer = ANALYZE, collect = 1, sm = 0; else if(!strcmp(command, "exit")) computer = NONE, StopPonder(1); else if(!strcmp(command, "force")) computer = NONE, StopPonder(pondering); else if(!strcmp(command, "go")) computer = stm; - else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime *= 10; - else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime *= 10; + else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime = (10*myTime)/unit; + else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit; else if(!strcmp(command, "post")) post = 1; else if(!strcmp(command, "nopost")) post = 0; - else if(!strcmp(command, "easy")) ponder = 0; - else if(!strcmp(command, "hard")) ponder = 1; - else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5); + else if(!strcmp(command, "easy") && !!*canPonder) ponder = 0, StopPonder(pondering), fprintf(toE, "setoption %s%s %sfalse\n", nameWord, canPonder, valueWord); + else if(!strcmp(command, "hard") && !!*canPonder) ponder = 1, fprintf(toE, "setoption %s%s %strue\n", nameWord, canPonder, valueWord), StartPonder(); + else if(!strcmp(command, "ping")) { /* static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1;*/ printf("po%s", line+2); } else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory); - else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores); + else if(!strcmp(command, "cores")&& !!*threadOpt) sscanf(line, "cores %d", &cores), fprintf(toE, "setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores); else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth); - else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0; + else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0, sTime /= unit; + else if(!strcmp(command, "offer")) drawOffer = 1; + else if(!strcmp(command, "result")) { if(sc == 's') fprintf(toE, "gameover %s\n", line[8] == '/' ? "draw" : (line[7] == '0') == mySide ? "win" : "lose"); } else if(!strcmp(command, "quit")) fprintf(toE, "quit\n"), fflush(toE), exit(0); } } @@ -499,7 +618,11 @@ main(int argc, char **argv) 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); } @@ -510,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