X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=77b671b7b633cefba7715f06e5f0076b82ec0635;hp=71dfd4b5e920dfe73ead029b853fbb18e740d01e;hb=d4c964cc8830407dbc9efa12a1d50e4ce48b711a;hpb=f72070254816c26ab9e52862e6191b2a59029e4d diff --git a/UCI2WB.c b/UCI2WB.c index 71dfd4b..77b671b 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,6 @@ /************************* UCI2WB by H.G.Muller ****************************/ -#define VERSION "1.8" +#define VERSION "2.0" #include #include @@ -30,11 +30,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; +char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, 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, on[500]; char currMove[20], moveMap[500][10]; // for analyze mode +int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500]; +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,25 +81,74 @@ 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); 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(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); + fprintf(toE, "\n"); DPRINT("\n"); } void StopPonder(int pondering) { if(!pondering) return; - pause = 1; + pause = 2; fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } @@ -103,10 +156,25 @@ StopPonder(int pondering) void LoadPos(int moveNr) { - int j; - fprintf(toE, "%s moves", iniPos); - DPRINT( "# %s moves", iniPos); - for(j=0; j 2) { collect = 3; continue; } // done collecting + 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 } } @@ -216,12 +296,16 @@ 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, "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); @@ -247,7 +331,11 @@ Engine2GUI() if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, 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), Sync(WAKEUP); // done with options + else if(sc == 'x'&& !strcmp(command, "ucciok") || sscanf(command, "u%ciok", &c)==1 && c==sc) { + 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 + } } } @@ -265,12 +353,7 @@ Move4Engine(char *m) 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] != '\n') m[4] = '+'; // cater to WB 4.4 bug :-( - } - if(m[4] && (m[1] == '1' || m[1] == '8')) { // seirawan gating - m[5] = m[0], m[6] = m[1], m[7] = '\0'; // copy from-square behind it, as gating square - if(m[2] == 'e' && (m[0] == 'a' || m[0] == 'h')) - m[2] = (m[0]+m[2]+1)>>1, m[0] = 'e'; // gating at Rook + else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-( } } @@ -282,9 +365,9 @@ GUI2Engine() 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) { @@ -300,17 +383,18 @@ GUI2Engine() 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; + computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0; stm = WHITE; strcpy(iniPos, "position startpos"); - if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s value %d\n", hashOpt, memory); + if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s %s%d\n", hashOpt, valueWord, memory); oldMem = memory; // we can set other options here - pause = 1; // wait for option settings to take effect - fprintf(toE, "isready\n"); + if(sc == 'x') { if(newGame) fprintf(toE, "setoption newgame\n"); } else // optional in UCCI fprintf(toE, "u%cinewgame\n", sc); fflush(toE); + pause = 1; // wait for option settings to take effect + fprintf(toE, "isready\n"); fflush(toE); Sync(PAUSE); // wait for readyok } else if(!strcmp(command, "usermove")) { @@ -320,8 +404,9 @@ 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 + char *draw = drawOffer ? " draw" : ""; drawOffer = 0; pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now - fprintf(toE, "ponderhit\n"); DPRINT("# ponderhit\n"); + fprintf(toE, "ponderhit%s\n", draw); DPRINT("# ponderhit%s\n", draw); goto nomove; } StopPonder(1); @@ -332,7 +417,7 @@ 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; @@ -340,18 +425,18 @@ GUI2Engine() 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 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 + 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 @@ -374,29 +459,39 @@ GUI2Engine() 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) { + else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude int all = !strcmp(line+8, "all"), in = command[1] == 'n'; - line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag + inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag for(i=1; i 2) dir = argv[2]; if(argc > 3) suffix = argv[3]; + if(sc == 'x') nameWord = valueWord = bTime = "", wTime = "opp", bInc = "increment", wInc = "oppincrement", unit = 1000; // switch to UCCI keywords + // spawn engine proc if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); } @@ -507,7 +604,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