X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=a546859dd163f22136537749a7e67af1dd90f12f;hp=632df6e4dcea01e394fb80b401188c51ae881190;hb=f21b77b0e872a89e1448cb56d5cff016f7231a12;hpb=573f12ddca1f3e3eb7b9e6483210a3a8d752c03b diff --git a/UCI2WB.c b/UCI2WB.c index 632df6e..a546859 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -9,7 +9,7 @@ /* of which you should have received a copy together with this file. */ /****************************************************************************/ -#define VERSION "2.0" +#define VERSION "4.0" #include #include @@ -30,26 +30,31 @@ #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, 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; -int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500], frc; -char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20]; -char board[100]; // XQ board for UCCI +char move[2000][10], iniPos[256], hashOpt[20], 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], 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; +int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways; 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) { @@ -141,25 +146,25 @@ StartSearch(char *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, wTime, !black ? myTime : hisTime); - DPRINT( "\n# go%s %stime %d %stime %d", ponder, bTime, black ? myTime : hisTime, wTime, !black ? myTime : hisTime); - 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, " %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"); + 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 -StopPonder(int pondering) +StopSearch(int discard) { - if(!pondering) return; - pause = 1; - 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'. + if(!searching) return; + if(discard) searching = 0; // this causes bestmove to be ignored + EPRINT((f, "# stop\n")) fflush(toE); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' } void @@ -172,20 +177,26 @@ LoadPos(int moveNr) stm = (!strstr(iniPos+4, " b ") ^ lastCapt & 1 ? 'w' : 'b'); sprintf(buf, "position fen %s", ToFEN(stm)); pos = buf; // send it as FEN (with "position" in UCCI!) } - fprintf(toE, "%s moves", pos); - DPRINT( "# %s moves", pos); - for(j=lastCapt; j 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(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(!post) continue; if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else { @@ -293,23 +302,42 @@ Engine2GUI() if(p = strstr(line+4, " score ")) sscanf(p+7, "%d", &s), statScore = s; if(p = strstr(line+4, " nodes ")) sscanf(p+7, "%d", &n), statNodes = n; 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) + } + if(collect && (pv = strstr(line+4, " currmove "))) { + sscanf(pv+10,"%s", currMove); + if(p = strstr(line+4, " currmovenumber ")) { + n = currNr = atoi(p+16); + 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; strcpy(moveMap[n], currMove); continue; // store move + } } } 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 "))) 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")) { - fprintf(toE, "setoption name %s value true\n", name); strcpy(val, "true"); // set non-standard suspected FRC options + 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; } @@ -320,8 +348,15 @@ Engine2GUI() } 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); @@ -345,12 +380,19 @@ Engine2GUI() 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(!strcmp(command, "readyok")) return; // resume processing of GUI commands 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 + 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) strcpy(varList, sc=='s' ? ",shogi,5x5+5_shogi" : VARIANTS); // without clue guess liberally + 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, fprintf(toE, "setoption usemillisec true\n"); - Sync(WAKEUP); // done with options + if(unit == 2) { unit = 1; EPRINT((f, "# setoption usemillisec true\n")) } + fflush(stdout); return; // done with options } } } @@ -373,62 +415,149 @@ Move4Engine(char *m) } } +void DoCommand (); +char mySide; +volatile char queue[10000], *qStart, *qEnd; + void -GUI2Engine() +LaunchSearch() { - char line[256], command[256], *p, *q, *r; - - while(1) { - int i, x; + int i; - if((computer == stm || computer == ANALYZE) && !suspended) { + if((computer == stm || computer == ANALYZE && !searching && sm != 1) && !suspended) { DPRINT("# start search\n"); 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"); + EPRINT((f, "# go infinite")); maxDepth = mpvSP = 0; 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"); } + 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; 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) HandleEngineOutput(); // this could leave us (or fall through) pondering + while(qStart == qEnd && searching) HandleEngineOutput(); // relay ponder output until command arrives + Sync(PAUSE); // possibly wait for command silently if engine is idle + DoCommand(); LaunchSearch(); } } @@ -604,9 +729,10 @@ main(int argc, char **argv) { char *dir = NULL, *p, *q; int e; + if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); } if(argc > 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]; @@ -617,7 +743,7 @@ main(int argc, char **argv) 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);