X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=7cdf340c0a63cd8c597b01260388d11f602233c5;hp=135b3a7601f76b0b9f98d10d724542bc9ac172dc;hb=HEAD;hpb=33b016e152d6f441cc4b2d7c5cb8ad440f480bc6 diff --git a/UCI2WB.c b/UCI2WB.c index 135b3a7..7cdf340 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 "3.0" +#define VERSION "4.0" #include #include @@ -45,11 +45,10 @@ 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, ponderAlways; -volatile int logLen, sentLen; FILE *toE, *fromE, *fromF; int pid; @@ -161,9 +160,10 @@ StartSearch(char *ponder) } void -StopPonder(int pondering) +StopSearch(int discard) { - if(!pondering) return; + 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' } @@ -188,6 +188,7 @@ StartPonder(int moveNr) if(!move[moveNr][0]) return; // no ponder move LoadPos(moveNr+1); searching = 1; lastDepth = 1; + DPRINT("# ponder on %s\n", move[moveNr]); StartSearch(" ponder"); } @@ -197,17 +198,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 == 2); - if(len <= 0) return 0; - StopPonder(searching); 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(moveNr); // (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; @@ -271,24 +261,20 @@ HandleEngineOutput() 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 + 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; + stm = WHITE+BLACK - stm; searching = 0; // first start a new ponder search, if pondering is on and we have a move to ponder on if(p = strstr(line+9, "ponder")) { sscanf(p+7, "%s", move[moveNr]); - if(computer != NONE && ponder) { - DPRINT("# ponder on %s\n", move[moveNr]); - StartPonder(moveNr); - } + if(computer != NONE && ponder) 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(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; } - return; + fflush(stdout); return; } else if(!strcmp(command, "info")) { int d=0, s=0, t=(GetTickCount() - startTime)/10, n=1; @@ -306,17 +292,6 @@ HandleEngineOutput() } continue; } - if(collect && (pv = strstr(line+5, "currmove "))) { - if(p = strstr(line+5, "currmovenumber ")) { - n = atoi(p+15); - 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; sscanf(pv+9, "%s", moveMap[n]); continue; // store move - } - } if(!post) continue; if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else { if(p = strstr(line+4, " depth ")) sscanf(p+7, "%d", &d), statDepth = d; @@ -325,8 +300,6 @@ HandleEngineOutput() 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(d > maxDepth) maxDepth = d, mpvSP = 0; else if(d < maxDepth) continue; // ignore depth regressions if(p = strstr(line+4, " upperbound ")) strcat(p, "?\n"); else @@ -336,6 +309,18 @@ HandleEngineOutput() 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; @@ -384,6 +369,7 @@ HandleEngineOutput() sprintf(buf + strlen(buf), "%s%s%s", min++ ? " /// " : " ", strcmp(type, val) ? "" : "*", val); } strcat(q, "\"\n"); + } else buf[0] = 0; // ignore unrecognized option types if(buf[0]) printf("%s", buf); @@ -400,11 +386,12 @@ HandleEngineOutput() 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) printf("feature variants=\"%s\"\n", varList+1); // from UCI_Variant combo and/or UCI_Chess960 check options + 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; EPRINT((f, "# setoption usemillisec true\n")) } - return; // done with options + fflush(stdout); return; // done with options } } } @@ -427,7 +414,7 @@ Move4Engine(char *m) } } -int DoCommand (); +void DoCommand (); char mySide; volatile char queue[10000], *qStart, *qEnd; @@ -436,7 +423,9 @@ LaunchSearch() { int i; - if((computer == stm || computer == ANALYZE && !searching) && !suspended) { + if(suspended || searching) return; + + if(computer == stm || computer == ANALYZE && sm != 1) { DPRINT("# start search\n"); LoadPos(moveNr); fflush(stdout); // load position // and set engine thinking (note USI swaps colors!) @@ -450,6 +439,7 @@ LaunchSearch() EPRINT((f, "\n")) searching = 2; // suppresses spurious commands during analysis starting new searches } else searching = 3, StartSearch(""); // request suspending of input processing while thinking } else if(ponderAlways && computer == NONE) move[moveNr][0] = 0, StartPonder(moveNr-1); + else if(BLACK+WHITE-stm == computer && ponder && moveNr) StartPonder(moveNr); } void @@ -463,18 +453,29 @@ GUI2Engine() for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking fflush(toE); fflush(stdout); if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n"); - sscanf(line, "%s", command); DPRINT("# '%s' searching=%d log=%d sent=%d\n", command, searching, logLen, sentLen); + sscanf(line, "%s", command); 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, ".")) { + printf("stat01: %d %d %d %d %d %s\n", statTime, statNodes, statDepth, nr-currNr, nr, currMove); + } else if(!strcmp(command, "pause")) { if(computer == stm) myTime -= GetTickCount() - startTime; suspended = 1 + (searching == 1); // remember if we were pondering, and stop search ignoring bestmove - StopPonder(searching); + StopSearch(1); } + else if(!strcmp(command, "xboard")) ; + else if(!strcmp(command, "random")) ; + else if(!strcmp(command, "accepted")) ; + else if(!strcmp(command, "rejected")) ; + else if(!strcmp(command, "book")) ; + else if(!strcmp(command, "ics")) ; + else if(!strcmp(command, "hint")) ; + else if(!strcmp(command, "computer")) comp = 1; 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; @@ -483,19 +484,11 @@ GUI2Engine() 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 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, '=')) { - *p++ = 0; - if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false"); - snprintf(backLog+logLen, 9999-logLen, "setoption %s%s %s%s", nameWord, line+7, valueWord, p); - } else snprintf(backLog+logLen, 9999-logLen, "setoption %s%s\n", nameWord, line+7); - DPRINT("# backlog: %s", backLog+logLen); logLen += strlen(backLog+logLen); - if(searching < 3 && Release()) break; // break will restart analysis; pondering is restarted by Release itself + difficult = 1; } else difficult = 1; // difficult command; terminate loop for easy ones } @@ -514,21 +507,21 @@ GUI2Engine() EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); continue; } - searching = 0; StopPonder(1); + StopSearch(1); } strcpy(move[moveNr++], command); // possibly overwrites ponder move *qEnd++ = '\n'; Sync(WAKEUP); // make sure engine thread considers starting a search } else if(!strcmp(command, "resume")) { - searching = 0; if(suspended == 2) StartPonder(moveNr); // restart interrupted ponder search - suspended = 0; // causes thinking to start in normal way if on move or analyzing + suspended = 0; *qEnd++ = '\n'; Sync(WAKEUP); // causes search to start in normal way if on move or analyzing } else { + DPRINT("# queue '%s', searching=%d\n", command, searching); if(searching == 3) { // command arrived during thinking; order abort for 'instant commands' if(!strcmp(command, "?") || !strcmp(command, "quit") || - !strcmp(command, "force") || !strcmp(command, "result")) StopPonder(1); - } else if(searching) searching = 0, StopPonder(1); // always abort pondering or analysis + !strcmp(command, "force") || !strcmp(command, "result")) StopSearch(0); + } else StopSearch(1); // always abort pondering or analysis // queue command for execution by engine thread if(qStart == qEnd) qStart = qEnd = queue; @@ -538,15 +531,15 @@ GUI2Engine() } } -int +void DoCommand () { char line[1024], command[256], *p, *q, *r, type[99]; int i; p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0; - if(line[0] == '\n') return 0; - sscanf(line, "%s", command); + if(line[0] == '\n') return; + sscanf(line, "%s", command); DPRINT("# command %s\n", command), fflush(stdout); if(!strcmp(command, "new")) { computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0; @@ -554,12 +547,20 @@ DoCommand () if(memory != oldMem && hasHash) EPRINT((f, "# setoption %s%s %s%d\n", nameWord, hashOpt, valueWord, memory)) oldMem = memory; // we can set other options here - if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI if(varOpt) EPRINT((f, "# setoption name UCI_Variant value chess\n")) EPRINT((f, "# isready\n")) fflush(toE); HandleEngineOutput(); // wait for readyok + if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE); } + else if(!strcmp(command, "option")) { + char *p; + if(p = strchr(line, '=')) { + *p++ = 0; + if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false"); + EPRINT((f, "# setoption %s%s %s%s", nameWord, line+7, valueWord, p)); + } else EPRINT((f, "# setoption %s%s\n", nameWord, line+7)); + } else if(!strcmp(command, "level")) { int sec = 0; sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 || @@ -567,7 +568,6 @@ DoCommand () tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit; } else if(!strcmp(command, "protover")) { - 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); @@ -600,17 +600,11 @@ DoCommand () else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) { 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); - 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 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(); + DoCommand(); LaunchSearch(); } }