X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=d44de7441491dc485679f43204e7eecbe38cc6a8;hp=26010a201e2b1b68af1af5d6455cae5060a1c6c1;hb=4aeefd3c3c03994a5b477935cf4d8e6c73062a14;hpb=1b936bccc1ce09ac1ea15a4186b3bbc51eacaaf8 diff --git a/UCI2WB.c b/UCI2WB.c index 26010a2..d44de74 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,15 @@ -/************************* UCI2WB by H.G.Muller ****************************/ - -#define VERSION "1.7" +/****************************************************************************/ +/* 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 "3.0" #include #include @@ -21,22 +30,32 @@ #include // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) -# define VARIANTS "normal,xiangqi" +#define VARIANTS ",normal,xiangqi" +#define STDVARS "chess,chess960,crazyhouse,3check,giveaway,suicide,losers,atomic,seirawan,shogi,xiangqi" +#define EGT ",gaviotaTbPath,syzygyPath,nalimovPath,robbotripleBaseDirectory,robbototalBaseDirectory,bitbases path," #define DPRINT if(debug) printf +#define EPRINT(X) { char f[999]; sprintf X; DPRINT("%s", f); fprintf(toE, "%s", f + 2*(*f == '#')); /* strip optional # prefix */ } #define WHITE 0 #define BLACK 1 #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; char currMove[20]; // for analyze mode +char move[2000][10], iniPos[256], hashOpt[20], pause, suspended, ponder, post, hasHash, c, sc='c', suffix[81], varOpt, searching, *binary; +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, namOpt, comp; +char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000], anaOpt[20], backLog[10000], checkOptions[8192] = "Ponder"; +char pvs[99][999], 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, scores[99], mpvSP, maxDepth, ponderAlways; +volatile int logLen, sentLen; -FILE *toE, *fromE; +FILE *toE, *fromE, *fromF; int pid; +char *strcasestr (char *p, char *q) { while(*p) { char *r=p++, *s=q; while(tolower(*r++) == tolower(*s) && *s) s++; if(!*s) return p-1; } return NULL; } + #ifdef WIN32 WinPipe(HANDLE *hRd, HANDLE *hWr) { @@ -77,18 +96,68 @@ 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(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); + 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 + EPRINT((f, "# go%s %stime %d %stime %d", ponder, bTime, (black ? myTime : hisTime) - t, wTime, (!black ? myTime : hisTime) - t)) + if(sTime > 0) EPRINT((f, " movetime %d", sTime)) else + if(mps) EPRINT((f, " 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) EPRINT((f, " %s %d %s %d", wInc, inc, bInc, inc)) + if(depth > 0) EPRINT((f, " depth %d", depth)) + if(*suffix) EPRINT((f, suffix, inc)) + EPRINT((f, "\n")); maxDepth = mpvSP = 0; } void @@ -96,17 +165,49 @@ StopPonder(int pondering) { if(!pondering) return; pause = 1; - fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' + EPRINT((f, "# stop\n")) fflush(toE); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } 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 +ReadLine (FILE *f, char *line) +{ + int x, i = 0; + while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0; + return (x != EOF); +} + +void +HandleEngineOutput() +{ + char line[1024], command[256]; static char egts[999]; while(1) { - int i=0, x; char *p, dummy; + int i=0, x; char *p, dummy, len; fflush(stdout); fflush(toE); - while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++; - line[++i] = 0; - if(x == EOF) exit(0); + if(fromF && !ReadLine(fromF, line)) fromF = 0, printf("# end fake\n"); + if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0); DPRINT("# engine said: %s", line), fflush(stdout); - sscanf(line, "%s", command); + 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) { searching = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. + else if(searching == 1) { searching = 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++]); + Release(); // send setoption commands that arrived during search 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(moveNr); } 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; - } - } - if(line[9] == 'e' && (line[14] == 'a' || line[14] == 'h')) // seirawan castling + gating at Rook - line[9] = line[14], line[11] = 'e'; // convert to RxK notation + } 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; - char *pv; + int d=0, s=0, t=(GetTickCount() - startTime)/10, n=1; + char *pv, varName[80]; if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; } + if(sscanf(line+5, "string variant %s", varName) == 1) { + if(!strstr(STDVARS, varName)) { + int files = 8, ranks = 8, hand = 0; char parent[80]; + if(p = strstr(line+18, " files ")) sscanf(p+7, "%d", &files); + if(p = strstr(line+18, " ranks ")) sscanf(p+7, "%d", &ranks); + if(p = strstr(line+18, " pocket ")) sscanf(p+8, "%d", &hand); + if(p = strstr(line+18, " template ")) sscanf(p+10, "%s", parent); else strcpy(parent, "fairy"); + if(p = strstr(line+18, " startpos ")) + printf("setup (-) %dx%d+%d_%s %s", files, ranks, hand, parent, p+10); + } + 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(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; @@ -191,25 +329,49 @@ Engine2GUI() if(p = strstr(line+4, " time ")) sscanf(p+6, "%d", &t), t /= 10, statTime = t; if(p = strstr(line+4, " currmove ")) sscanf(p+10,"%s", currMove); if(p = strstr(line+4, " currmovenumber ")) sscanf(p+16,"%d", &currNr); - if(pv = strstr(line+4, " pv ")) // convert PV info to WB thinking output + if(pv = strstr(line+4, " pv ")) { // convert PV info to WB thinking output + if(d > maxDepth) maxDepth = d, mpvSP = 0; else if(d < maxDepth) continue; // ignore depth regressions + if(p = strstr(line+4, " upperbound ")) strcat(p, "?\n"); else + if(p = strstr(line+4, " lowerbound ")) strcat(p, "!\n"); + for(i=0; i= mpvSP) strncpy(pvs[mpvSP], pv+4, 998), scores[mpvSP++] = s, // emit as thinking output if not printf("%3d %6d %6d %10d %s", lastDepth=d, lastScore=s, t, n, Convert(pv+4)); + } else if(s == -100000) lastScore = s; // when checkmated score is valid even without PV (which might not come) } } else if(!strcmp(command, "option")) { // USI option: extract data fields char name[80], type[80], buf[1024], val[256], *q; - int min=0, max=1e9; + int min=0, max=1e9; *val = 0; if(p = strstr(line+6, " type ")) sscanf(p+1, "type %s", type), *p = '\n'; 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(!strcasecmp(name, "UCI_Variant")) { if(p = strstr(line+6, " var ")) strcpy(varList, p); varOpt = 1; continue; } + if(!strcasecmp(name, "UCI_Opponent")) { namOpt = 1; continue; } + if(!strcasecmp(name+2, "I_AnalyseMode")) { strcpy(anaOpt, name); continue; } + if(frc< 0 && (strstr(name, "960") || strcasestr(name, "frc")) && !strcmp(type, "check")) { + EPRINT((f, "# 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; } + sprintf(buf, ",%s,", name); if(p = strcasestr(EGT, buf)) { // collect EGT formats + strcpy(buf, p); for(p=buf; *++p >='a';){} if(*p == ' ') strcpy(buf, ",scorpio"); *p = 0; strcat(egts, buf); continue; // clip at first non-lower-case + } // pass on engine-defined option as WB option feature if(!strcmp(type, "filename")) type[4] = 0; + else if(sc == 'c' && !strcmp(type, "string")) { // in UCI try to guess which strings are file or directory names + if(strcasestr(name, "file")) strcpy(type, "file"); else + if(strcasestr(name, "path") || strcasestr(name, "directory") || strcasestr(name, "folder")) strcpy(type, "path"); + } sprintf(buf, "feature option=\"%s -%s", name, type); q = buf + strlen(buf); if( !strcmp(type, "file") || !strcmp(type, "string")) sprintf(q, " %s\"\n", val); @@ -229,139 +391,262 @@ Engine2GUI() if(buf[0]) printf("%s", buf); } else if(!strcmp(command, "id")) { - char name[256]; + 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) { + char *p = varList, *q = varList; + while(*q && *q != '\n') if(!strncmp(q, " var ", 5)) *p++ = ',', q +=5; // replace var keywords by commas + else if(!strncmp(q-1, " chess ", 7)) strcpy(p, "normal"), p += 6, q += 5; // 'chess' is called 'normal' in CECP + else *p++ = *q++; // copy other variant names unmodified + if(frc) sprintf(p, ",normal,fischerandom"), printf("feature oocastle=%d\n", frc<0); // unannounced FRC uses O-O castling + if(*varList) printf("feature variants=\"%s\"\n", varList+1); // from UCI_Variant combo and/or UCI_Chess960 check options + if(*egts) printf("feature egt=\"%s\"\n", egts+1); + printf("feature smp=1 memory=%d done=1\n", hasHash); + if(unit == 2) { unit = 1; EPRINT((f, "# setoption usemillisec true\n")) } + Sync(WAKEUP); // done with options + } + } +} + +void * +Engine2GUI() +{ + if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); + HandleEngineOutput(); +} + +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 :-( } } +int DoCommand (); +char mySide; +char queue[10000], *qStart, *qEnd; + +void +LaunchSearch() +{ + int i; + + if((computer == stm || computer == ANALYZE && !searching) && !suspended) { + DPRINT("# start search\n"); + LoadPos(moveNr); fflush(stdout); // load position + // and set engine thinking (note USI swaps colors!) + startTime = GetTickCount(); mySide = stm; // remember side we last played for + if(computer == ANALYZE) { + EPRINT((f, "# go infinite")); maxDepth = mpvSP = 0; + if(sm & 1) { // some moves are disabled + EPRINT((f, " searchmoves")) + for(i=1; i>1, command[0] = 'e'; // gating at Rook - } - stm = WHITE+BLACK - stm; + Move4Engine(command); + stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0; // 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"); + if(searching) { // move cannot come during think + if(searching == 1 && !strcmp(command, move[moveNr])) { // ponder hit + char *draw = drawOffer ? " draw" : ""; drawOffer = 0; + searching = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now + EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); + searching = 3; // request blocking input during thinking goto nomove; } - StopPonder(1); + StopPonder(1); searching = 0; } -// sscanf(line, "usermove %s", move[moveNr++]); // possibly overwrites ponder move - strcpy(move[moveNr++], command); + strcpy(move[moveNr++], command); // possibly overwrites ponder move + } else + if(!strcmp(command, "resume")) { + searching = 0; + if(suspended == 2) StartPonder(moveNr); // restart interrupted ponder search + suspended = 0; // causes thinking to start in normal way if on move or analyzing + } else + { + if(searching == 3) { // command arrived during thinking; order abort for 'instant commands' + if(!strcmp(command, "?") || !strcmp(command, "quit") || + !strcmp(command, "force") || !strcmp(command, "result")) { EPRINT((f, "# stop\n")); fflush(toE); } + Sync(PAUSE); Release(); // block processing of difficult commands during thinking; send backlog left because of race + } + if(qStart == qEnd) qStart = qEnd = queue; + p = line; while(qEnd < queue+10000 && (*qEnd++ = *p++) != '\n') {} + if(DoCommand()) goto nomove; + } + } +} + +int +DoCommand () +{ + char line[1024], command[256], *p, *q, *r, type[99]; + int i; + + p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0; + sscanf(line, "%s", command); + + if(!strcmp(command, "new")) { + computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0; + stm = WHITE; strcpy(iniPos, "position startpos"); frc &= ~1; + if(memory != oldMem && hasHash) EPRINT((f, "# setoption %s%s %s%d\n", nameWord, hashOpt, valueWord, memory)) + oldMem = memory; + // we can set other options here + if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI + if(varOpt) EPRINT((f, "# setoption name UCI_Variant value chess\n")) + pause = 1; // wait for option settings to take effect + EPRINT((f, "# isready\n")) fflush(toE); + Sync(PAUSE); // wait for readyok + EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE); } else if(!strcmp(command, "level")) { 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; - } - else if(!strcmp(command, "option")) { - char name[80], *p; - if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 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); } + tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit; } 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 done=0\n", variants); + if(!varList[0]) strcpy(varList, sc=='s' ? ",shogi,5x5+5_shogi" : VARIANTS); + printf("feature setboard=1 usermove=1 debug=1 ping=1 name=1 reuse=0 exclude=1 pause=1 sigint=0 sigterm=0 done=0\n"); printf("feature option=\"UCI2WB debug output -check %d\"\n", debug); - fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options + printf("feature option=\"ponder always -check %d\"\n", ponderAlways); + if(sc == 's') printf("feature option=\"Floating Byoyomi -check %d\"\nfeature option=\"Byoyomi -spin %d -1 1000\"\n", flob, byo); + EPRINT((f, 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; - if(sc == 's' && (p = strchr(line+9, '['))) { char c; - *p++ = ' '; q = strchr(p, ']'); c = 'w' + 'b' - q[2]; strcpy(q+2, " 1\n"); while(*--q != ' ') q[2] = *q; *p = c; p[1] = ' '; - } - sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", line+9); - iniPos[strlen(iniPos)-1] = 0; + stm = (strstr(line+9, " b ") ? BLACK : WHITE); + if((p = strchr(line+9, '[')) && !varOpt) { 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); } else if(!strcmp(command, "variant")) { + if(varOpt) { + EPRINT((f, "# setoption name UCI_Variant value %sucinewgame\nisready\n", line+8)) + fflush(toE); Sync(PAUSE); + } 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) EPRINT((f, "# 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; + if(searching) StopPonder(1), searching = 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; + return 1; + } + 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 && !strcmp(argv[1], "-var")) { strcpy(varList+1, argv[2]); *varList = ','; 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); } + if(StartEngine(binary = argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); } Sync(INIT); @@ -470,7 +760,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