X-Git-Url: http://winboard.nl/cgi-bin?p=uci2wb.git;a=blobdiff_plain;f=UCI2WB.c;h=a546859dd163f22136537749a7e67af1dd90f12f;hp=ae6cf2c147d0a8176910a5d56eafe768e55d3441;hb=f21b77b0e872a89e1448cb56d5cff016f7231a12;hpb=ed12bdda15cb49c351d1358a8d056a1f97c59f85 diff --git a/UCI2WB.c b/UCI2WB.c index ae6cf2c..a546859 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 @@ -42,14 +42,13 @@ #define NONE 2 #define ANALYZE 3 -char move[2000][10], iniPos[256], hashOpt[20], pause, 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, ponderAlways; -volatile int logLen, sentLen; FILE *toE, *fromE, *fromF; int pid; @@ -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 @@ -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 == 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; @@ -264,8 +251,8 @@ HandleEngineOutput() DPRINT("# engine said: %s", line), fflush(stdout); if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { - if(pause == 1) { searching = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. - else if(searching == 1) { searching = 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; } @@ -273,10 +260,9 @@ 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]); @@ -289,8 +275,8 @@ HandleEngineOutput() 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; } + fflush(stdout); return; } else if(!strcmp(command, "info")) { int d=0, s=0, t=(GetTickCount() - startTime)/10, n=1; @@ -308,17 +294,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; @@ -327,8 +302,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 @@ -338,6 +311,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; @@ -395,29 +380,23 @@ HandleEngineOutput() 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 *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")) } - Sync(WAKEUP); // done with options + fflush(stdout); return; // done with options } } } -void * -Engine2GUI() -{ - if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); - HandleEngineOutput(); -} - void Move4Engine(char *m) { @@ -436,19 +415,16 @@ Move4Engine(char *m) } } -int DoCommand (); +void DoCommand (); char mySide; -char queue[10000], *qStart, *qEnd; +volatile char queue[10000], *qStart, *qEnd; void -GUI2Engine() +LaunchSearch() { - char line[256], command[256], *p; - - while(1) { - int i, difficult, think=0; + int i; - if((computer == stm || computer == ANALYZE && !searching) && !suspended) { + if((computer == stm || computer == ANALYZE && !searching && sm != 1) && !suspended) { DPRINT("# start search\n"); LoadPos(moveNr); fflush(stdout); // load position // and set engine thinking (note USI swaps colors!) @@ -460,25 +436,44 @@ GUI2Engine() 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(searching) 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); - 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 + DoCommand(); LaunchSearch(); + } } int