X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=ebcd79c6de7915fd67ec89a1f0807e5273c4bd4f;hb=3ac3bb55a99f03831492829d8135e83f567d59b9;hp=997423f764b0c2f19e22c1abcf1a4b5406a236ef;hpb=8d74aed8f6fb583c14bf2f6622811221672b8d23;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index 997423f..ebcd79c 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -30,8 +30,9 @@ #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,threecheck,giveaway,atomic,seirawan,shogi,xiangqi" +#define EGT ",syzygypath,nalimovpath,gaviotapath," #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 */ } @@ -41,7 +42,7 @@ #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], *variants, varOpt, searching; +char move[2000][10], iniPos[256], hashOpt[20], pause, pondering, 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"; @@ -54,7 +55,7 @@ FILE *toE, *fromE, *fromF; int pid; #ifdef WIN32 -char *strcasestr (char *p, char *q) { char *r = p; while(*r) *r = tolower(*r), r++; return strstr(p, q); } +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; } WinPipe(HANDLE *hRd, HANDLE *hWr) { @@ -255,7 +256,7 @@ GetChar() void * Engine2GUI() { - char line[1024], command[256]; + char line[1024], command[256]; static char egts[999]; if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); while(1) { @@ -264,7 +265,7 @@ Engine2GUI() fflush(stdout); fflush(toE); while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; line[++i] = 0; - if(x == EOF) exit(0); + if(x == EOF) 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")) { @@ -327,11 +328,12 @@ 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 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'; @@ -353,6 +355,7 @@ 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] = 0; strcat(egts, buf); continue; } // collect EGT formats // 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); @@ -387,6 +390,7 @@ Engine2GUI() 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); } 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 @@ -415,7 +419,7 @@ Move4Engine(char *m) void GUI2Engine() { - char line[256], command[256], *p, *q, *r, mySide; + char line[256], command[256], *p, *q, *r, mySide, type[99]; while(1) { int i, x, difficult, think=0; @@ -441,16 +445,25 @@ GUI2Engine() line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); } if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen); - if(!strcmp(command, "easy")) { - if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue; - } - else if(!strcmp(command, "hard")) { - if(*canPonder) ponder = 1, sprintf(command, "option"), sprintf(line, "option %s=1\n", canPonder); else continue; - } - if(!strcmp(command, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly + if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first + 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, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly else if(!strcmp(command, "post")) post = 1; else if(!strcmp(command, "nopost"))post = 0; - else if(!strcmp(command, "option")) { + 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 { //convert easy & hard to "option" after treating their effect on the adapter + if(!strcmp(command, "easy")) { + if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue; + } + else if(!strcmp(command, "hard")) { + if(*canPonder) ponder = 1, sprintf(command, "option"), sprintf(line, "option %s=1\n", canPonder); else continue; + } + if(!strcmp(command, "option")) { char *p; if(logLen == sentLen) logLen = 0, sentLen = 0; // engine is up to date; reset buffer if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else @@ -463,15 +476,30 @@ GUI2Engine() } else snprintf(backLog+logLen, 9999-logLen, "setoption %s%s\n", nameWord, line+7); DPRINT("# backlog: %s", backLog+logLen); logLen += strlen(backLog+logLen); if(!think && Release()) break; // break will restart analysis; pondering is restarted by Release itself + } + else difficult = 1; // difficult command; terminate loop for easy ones } - 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 difficult = 1; // difficult command; terminate loop for easy ones } // next command + // some commands that should never come during thinking can be safely processed here + if(difficult < 0) { // used as kludge to signal "usermove" was already matched + sscanf(line, "usermove %s", command); // strips off linefeed + 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 + char *draw = drawOffer ? " draw" : ""; drawOffer = 0; + pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now + EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); + think = 2; // request blocking input during thinking + goto nomove; + } + StopPonder(1); searching = 0; + } + strcpy(move[moveNr++], command); // possibly overwrites ponder move + continue; + } if(!strcmp(command, "resume")) { if(suspended == 2) StartPonder(); // restart interrupted ponder search suspended = think = 0; continue; // causes thinking to start in normal way if on move or analyzing @@ -494,23 +522,6 @@ GUI2Engine() Sync(PAUSE); // wait for readyok EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE); } - else if(!strcmp(command, "usermove")) { - sscanf(line, "usermove %s", command); // strips off linefeed - 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 - char *draw = drawOffer ? " draw" : ""; drawOffer = 0; - pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now - EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); - think = 2; // request blocking input during thinking - goto nomove; - } - StopPonder(1); searching = 0; - } - strcpy(move[moveNr++], command); // possibly overwrites ponder move - } else if(!strcmp(command, "level")) { int sec = 0; sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 || @@ -518,8 +529,8 @@ GUI2Engine() 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 name=1 reuse=0 exclude=1 pause=1 sigint=0 sigterm=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); 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 @@ -567,11 +578,10 @@ GUI2Engine() else if(!strcmp(command, "exit")) computer = NONE, StopPonder(1), searching = 0, Analyze("false"); 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*myTime)/unit; - else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit; 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")&& !!*threadOpt) { sscanf(line, "cores %d", &cores); EPRINT((f, "# setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores)) } + else if(!strcmp(command, "egtpath")){ sscanf(line, "egtpath %s %[^\n]", type, command); EPRINT((f, "# setoption name %sPath value %s\n", type,command)); } 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, sTime /= unit; else if(!strcmp(command, "name")) { if(namOpt) EPRINT((f, "# setoption name UCI_Opponent value none none %s %s", comp ? "computer" : "human", line+5)) } @@ -675,7 +685,7 @@ main(int argc, char **argv) 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]; @@ -686,7 +696,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);