X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=41c71c81f97880a7f7de80da1b5d1f675a32d47d;hp=d1096d0568a6cd406fc3c079d7af72f4df25e6c6;hb=990ae69c6d5ff8468541372acc4c6fcb854f64e2;hpb=4b420a9e9f950b82c07165e309ba62aff0e8483b diff --git a/UCI2WB.c b/UCI2WB.c index d1096d0..41c71c8 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.3" +#define VERSION "3.0" #include #include @@ -31,8 +31,8 @@ // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) #define VARIANTS ",normal,xiangqi" -#define STDVARS "chess,chess960,crazyhouse,threecheck,giveaway,atomic,seirawan,shogi,xiangqi" -#define EGT ",gaviotatbpath,syzygypath,nalimovpath," +#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 */ } @@ -42,21 +42,20 @@ #define NONE 2 #define ANALYZE 3 -char move[2000][10], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', suffix[81], varOpt, searching, *binary; +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], backLog[10000], checkOptions[8192] = "Ponder"; +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, scores[99], mpvSP, maxDepth; -volatile int logLen, sentLen; +int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways; FILE *toE, *fromE, *fromF; int pid; -#ifdef WIN32 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) { SECURITY_ATTRIBUTES saAttr; @@ -161,12 +160,11 @@ StartSearch(char *ponder) } void -StopPonder(int pondering) +StopSearch(int discard) { - if(!pondering) return; - pause = 1; + 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' - Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } void @@ -185,11 +183,11 @@ LoadPos(int moveNr) } void -StartPonder() +StartPonder(int moveNr) { if(!move[moveNr][0]) return; // no ponder move LoadPos(moveNr+1); - pondering = 1; lastDepth = 1; + searching = 1; lastDepth = 1; StartSearch(" ponder"); } @@ -199,17 +197,6 @@ Analyze(char *val) if(*anaOpt) EPRINT((f, "# setoption %s%s %s%s\n", nameWord, anaOpt, valueWord, val)); } -int -Release() -{ // send setoption commands backlogged during thinking to engine, aborting ponder or analysis search if necessary - int len = logLen - sentLen, analyse = searching; - if(len <= 0) return 0; - StopPonder(pondering | searching); pondering = searching = 0; // force new search if settings change during analysis (multi-PV!) - fwrite(backLog + sentLen, 1, len, toE); sentLen += len; DPRINT("# release %d\n", len); - if(ponder && computer == 1 - stm) StartPonder(); // (re)start ponder search - return analyse; // return 1 if analysis search should be restarted -} - char *Convert(char *pv) { // convert Shogi coordinates to WB char *p, *q, c; @@ -243,34 +230,29 @@ Move4GUI(char *m) } int -GetChar() +ReadLine (FILE *f, char *line) { - int c; - if(fromF) { - if((c = fgetc(fromF)) != EOF) return c; - fclose(fromF); fromF = 0; printf("# end fake\n"); - } - return fgetc(fromE); + int x, i = 0; + while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0; + return (x != EOF); } -void * -Engine2GUI() +void +HandleEngineOutput() { char line[1024], command[256]; static char egts[999]; - if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); while(1) { int i=0, x; char *p, dummy, len; fflush(stdout); fflush(toE); - while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; - line[++i] = 0; - if(x == EOF) printf("tellusererror UCI2WB: %s died on me\n", binary), 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); if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { - 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 + if(searching == 1) { searching = 0; printf("%d 0 0 0 UCI violation! Engine moves during ponder\n", lastDepth+1); return; } // ignore ponder search + else if(searching != 3) { searching = 0; return; } // ponder miss or analysis result; ignore. // 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; } @@ -278,7 +260,6 @@ Engine2GUI() 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; @@ -287,22 +268,30 @@ Engine2GUI() sscanf(p+7, "%s", move[moveNr]); if(computer != NONE && ponder) { DPRINT("# ponder on %s\n", move[moveNr]); - StartPonder(); + StartPonder(moveNr); } p[-1] = '\n'; *p = 0; // strip off ponder move } else move[moveNr][0] = 0; Move4GUI(line+9); printf("move %s\n", line+9); // send move to GUI 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; } + return; } else if(!strcmp(command, "info")) { 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) && (p = strstr(line+18, " startpos "))) printf("setup (-) 8x8+0_fairy %s", p+10); + 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 "))) { @@ -328,6 +317,8 @@ Engine2GUI() 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(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)); @@ -358,7 +349,9 @@ 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(strcasestr(EGT, buf)) { buf[strlen(buf)-5-2*(buf[3]=='v')] = 0; strcat(egts, buf); continue; } // collect EGT formats + 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 @@ -388,19 +381,18 @@ 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) { 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 if(!strncmp(q-1, " threecheck", 11)) *p++ = '3', q += 5; // 'threecheck' is written '3check' 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) { for(p=egts; *p = tolower(*p); p++); printf("feature egt=\"%s\"\n", egts+1); } + 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 + return; // done with options } } } @@ -423,13 +415,14 @@ Move4Engine(char *m) } } +int DoCommand (); +char mySide; +volatile char queue[10000], *qStart, *qEnd; + void -GUI2Engine() +LaunchSearch() { - char line[256], command[256], *p, *q, *r, mySide, type[99]; - - while(1) { - int i, x, difficult, think=0; + int i; if((computer == stm || computer == ANALYZE && !searching) && !suspended) { DPRINT("# start search\n"); @@ -442,26 +435,36 @@ GUI2Engine() EPRINT((f, " searchmoves")) for(i=1; i 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), 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; - } 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 + if(!DoCommand()) LaunchSearch(); } } @@ -693,6 +723,7 @@ 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")) { strcpy(varList+1, argv[2]); *varList = ','; argc-=2; argv+=2; }