X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=3db7bbd2fa97608d88cc6b80cc96272c12c42ca2;hp=2971c3f5df7e00abec317f6574d387abb8c38c3c;hb=a8c8e03626f0cd085656c31efc672689998d5fdb;hpb=85a82d8e5887b236ac8c6bad257301b4d30b948d diff --git a/UCI2WB.c b/UCI2WB.c index 2971c3f..3db7bbd 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 ",gaviotatbpath,syzygypath,nalimovpath," #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,19 +42,20 @@ #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, varOpt; +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]; -char board[100]; // XQ board for UCCI +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; +int unit = 1, drawOffer, scores[99], mpvSP, maxDepth; +volatile int logLen, sentLen; 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) { @@ -155,7 +157,7 @@ StartSearch(char *ponder) 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")) + EPRINT((f, "\n")); maxDepth = mpvSP = 0; } void @@ -191,6 +193,23 @@ StartPonder() StartSearch(" ponder"); } +void +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; @@ -237,16 +256,16 @@ 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) { - int i=0, x; char *p, dummy; + 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) 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")) { @@ -259,6 +278,7 @@ 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; @@ -306,13 +326,17 @@ 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 + 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'; @@ -321,6 +345,7 @@ Engine2GUI() 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 } @@ -333,8 +358,13 @@ 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 // 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); @@ -367,6 +397,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 @@ -395,10 +426,10 @@ Move4Engine(char *m) void GUI2Engine() { - char line[256], command[256], *p, *q, *r, mySide, searching = 0; + char line[256], command[256], *p, *q, *r, mySide, type[99]; while(1) { - int i, x, think=0; + int i, x, difficult, think=0; if((computer == stm || computer == ANALYZE && !searching) && !suspended) { DPRINT("# start search\n"); @@ -406,7 +437,7 @@ GUI2Engine() // 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")) + EPRINT((f, "# go infinite")); maxDepth = mpvSP = 0; if(sm & 1) { // some moves are disabled EPRINT((f, " searchmoves")) 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]; @@ -651,7 +706,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);