X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=0e7f34f0fe7bea3bafa54ca4e62162d7484cc9e9;hp=9d568f73b961f697add7f51b31470ea0245b88e2;hb=0bcc18ddfe1e849b345a565850e2a14cdab30d1a;hpb=9ab6e3faf717202a5ce807808975a4c29711a8e5 diff --git a/UCI2WB.c b/UCI2WB.c index 9d568f7..0e7f34f 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -48,7 +48,7 @@ int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, in 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; +int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways; volatile int logLen, sentLen; FILE *toE, *fromE, *fromF; @@ -185,7 +185,7 @@ LoadPos(int moveNr) } void -StartPonder() +StartPonder(int moveNr) { if(!move[moveNr][0]) return; // no ponder move LoadPos(moveNr+1); @@ -206,7 +206,7 @@ Release() 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 + if(ponder && computer == 1 - stm) StartPonder(moveNr); // (re)start ponder search return analyse; // return 1 if analysis search should be restarted } @@ -243,29 +243,24 @@ 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")) { @@ -287,7 +282,7 @@ 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; @@ -416,6 +411,13 @@ Engine2GUI() } } +void * +Engine2GUI() +{ + if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); + HandleEngineOutput(); +} + void Move4Engine(char *m) { @@ -434,13 +436,17 @@ Move4Engine(char *m) } } +int DoCommand (); +char mySide; +char queue[10000], *qStart, *qEnd; + void GUI2Engine() { - char line[256], command[256], *p, *q, *r, mySide, type[99]; + char line[256], command[256], *p; while(1) { - int i, x, difficult, think=0; + int i, difficult, think=0; if((computer == stm || computer == ANALYZE && !searching) && !suspended) { DPRINT("# start search\n"); @@ -455,12 +461,11 @@ GUI2Engine() } EPRINT((f, "\n")) searching = 1; // suppresses spurious commands during analysis starting new searches } else pause = think = 2, StartSearch(""); // request suspending of input processing while thinking - } + } else if(ponderAlways && computer == NONE) move[moveNr][0] = 0, StartPonder(moveNr-1); nomove: for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking fflush(toE); fflush(stdout); - i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++; - line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); } + if(!ReadLine(stdin, line)) 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, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first @@ -485,6 +490,7 @@ GUI2Engine() 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 + if(sscanf(line+7, "ponder always=%d", &ponderAlways) == 1) ; else if(sscanf(line+7, "Floating Byoyomi=%d", &flob) == 1) ; else if(sscanf(line+7, "Byoyomi=%d", &byo) == 1) ; else if(p = strchr(line, '=')) { @@ -519,7 +525,7 @@ GUI2Engine() continue; } if(!strcmp(command, "resume")) { - if(suspended == 2) StartPonder(); // restart interrupted ponder search + if(suspended == 2) StartPonder(moveNr); // restart interrupted ponder search suspended = think = 0; continue; // causes thinking to start in normal way if on move or analyzing } if(think) { // command arrived during thinking; order abort for 'instant commands' @@ -527,6 +533,21 @@ GUI2Engine() !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; @@ -550,6 +571,7 @@ GUI2Engine() 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); + 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 @@ -582,13 +604,13 @@ GUI2Engine() } 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