X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=77cb1da080d069ec37bb2a17e0412fe42a616554;hb=07b56132a78ddaa3fb6e1bfa9f5e2595307b6773;hp=764f9217d85d3db4e59d8885c0c84c51b0a8be53;hpb=35ffc8b7ee8e505a23a9ce60a5734a72560d93a5;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index 764f921..77cb1da 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,6 @@ /************************* UCI2WB by H.G.Muller ****************************/ -#define VERSION "1.5" +#define VERSION "1.10" #include #include @@ -20,27 +20,40 @@ #include #include -#ifdef _MSC_VER -#define SLEEP() Sleep(1) -#else -#define SLEEP() usleep(10) -#endif - // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) # define VARIANTS "normal,xiangqi" +#define DPRINT if(debug) printf + #define WHITE 0 #define BLACK 1 #define NONE 2 #define ANALYZE 3 -char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix; -int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime; -int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode +char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', *suffix, *variants; +int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug; +int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500]; +char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20]; +int unit = 1; -FILE *toE, *fromE; +FILE *toE, *fromE, *fromF; int pid; +#ifdef WIN32 +WinPipe(HANDLE *hRd, HANDLE *hWr) +{ + SECURITY_ATTRIBUTES saAttr; + + /* Set the bInheritHandle flag so pipe handles are inherited. */ + saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + + /* Create a pipe */ + return CreatePipe(hRd, hWr, &saAttr, 0); +} +#endif + #define INIT 0 #define WAKEUP 1 #define PAUSE 2 @@ -49,7 +62,12 @@ void Sync (int action) { #ifdef WIN32 - if(action == PAUSE) while(pause) SLEEP(); // in Windows we still rely on polling. :-( + static HANDLE hWr, hRd; DWORD d; char c; + switch(action) { + case INIT: WinPipe(&hRd, &hWr); break; + case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break; + case PAUSE: ReadFile(hRd, &c, 1, &d, NULL); + } #else static int syncPipe[2]; char c; switch(action) { @@ -63,16 +81,17 @@ Sync (int action) void StartSearch(char *ponder) { // send the 'go' command to engine. Suffix by ponder. - int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead! - fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); - printf( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); - if(sTime > 0) fprintf(toE, " movetime %d", sTime),printf(" movetime %d", sTime); else - if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); - if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc); - if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth); - if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc); + int x = (ponder[0] != 0); + int nr = moveNr + x; // we ponder for one move ahead! + fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime); + DPRINT( "\n# go btime %d wtime %d", stm == BLACK ^ x ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ x ^ sc=='s' ? myTime : hisTime); + if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else + if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); } + if(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); } + if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); } + if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); } fprintf(toE, "%s\n", ponder); - printf("%s\n", ponder); + DPRINT("%s\n", ponder); } void @@ -80,7 +99,7 @@ StopPonder(int pondering) { if(!pondering) return; pause = 1; - fprintf(toE, "stop\n"); fflush(toE); printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' + fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } @@ -89,8 +108,17 @@ LoadPos(int moveNr) { int j; fprintf(toE, "%s moves", iniPos); - printf( "# %s moves", iniPos); - for(j=0; j'0'+size-size/3 || m[3]>'0'+size-size/3) + : (m[1] <= '0'+size/3 || m[3] <= '0'+size/3)) && m[4] != '+') + m[4] = '=', m[5] = 0; + } + } +} + +int +GetChar() +{ + int c; + if(fromF) { + if((c = fgetc(fromF)) != EOF) return c; + fclose(fromF); fromF = 0; printf("# end fake\n"); + } + return fgetc(fromE); +} + void * Engine2GUI() { char line[1024], command[256]; + if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); while(1) { - int i=0, x; char *p; + int i=0, x; char *p, dummy; fflush(stdout); fflush(toE); - while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++; + while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; line[++i] = 0; if(x == EOF) exit(0); -printf("# engine said: %s", line); fflush(stdout); - sscanf(line, "%s", command); + DPRINT("# engine said: %s", line), fflush(stdout); + if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. // move was a move to be played @@ -129,41 +188,37 @@ printf("# engine said: %s", line); fflush(stdout); sscanf(line, "bestmove %s", move[moveNr++]); 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; // 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) { - sscanf(p+7, "%s", move[moveNr]); -printf("# ponder on %s\n", move[moveNr]); - LoadPos(moveNr+1); // load position - // and set engine pondering - pondering = 1; lastDepth = 1; - StartSearch(" ponder"); + DPRINT("# ponder on %s\n", move[moveNr]); + StartPonder(); } p[-1] = '\n'; *p = 0; // strip off ponder move - } - if(sc == 's') { - // convert USI move to WB format - line[11] = 'a'+'0'+size - line[11]; - line[12] = 'a'+'0'+size - line[12]; - if(line[10] == '*') { // drop - line[10] = '@'; - } else { - line[9] = 'a'+'0'+size - line[9]; - line[10] = 'a'+'0'+size - line[10]; - if((stm == WHITE ? (line[10]>'0'+size-size/3 || line[12]>'0'+size-size/3) - : (line[10] <= '0'+size/3 || line[12] <= '0'+size/3)) && line[13] != '+') - line[13] = '=', line[14] = 0; - } - } + } else move[moveNr][0] = 0; + Move4GUI(line+9); printf("move %s\n", line+9); // send move to GUI - if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; } - stm = WHITE+BLACK - stm; + if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; } } else if(!strcmp(command, "info")) { int d=0, s=0, t=0, n=0; char *pv; + if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); 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(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else { + 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; if(p = strstr(line+4, " score cp ")) sscanf(p+10, "%d", &s), statScore = s; else if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else @@ -184,6 +239,8 @@ printf("# ponder on %s\n", move[moveNr]); if(p = strstr(line+6, " max ")) sscanf(p+1, "max %d", &max), *p = '\n'; if(p = strstr(line+6, " default ")) sscanf(p+1, "default %[^\n]*", val), *p = '\n'; if(p = strstr(line+6, " name ")) sscanf(p+1, "name %[^\n]*", name); + if(!strcmp(name, "Threads")) { strcpy(threadOpt, name); continue; } + if(!strcmp(name, "Ponder") || !strcmp(name, "USI_Ponder")) { strcpy(canPonder, name); continue; } if(!strcmp(name, "Hash") || !strcmp(name, "USI_Hash")) { memory = oldMem = atoi(val); hasHash = 1; strcpy(hashOpt, name); @@ -211,28 +268,53 @@ printf("# ponder on %s\n", move[moveNr]); } else if(!strcmp(command, "id")) { char name[256]; - if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); + if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); } else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands - else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options + else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash), Sync(WAKEUP); // done with options + } +} + +void +Move4Engine(char *m) +{ + if(sc == 's') { + // convert input move to USI format + if(m[1] == '@') { // drop + m[1] = '*'; + } else { + m[0] = 'a'+'0'+size - m[0]; + m[1] = 'a'+'0'+size - m[1]; + } + m[2] = 'a'+'0'+size - m[2]; + m[3] = 'a'+'0'+size - m[3]; + if(m[4] == '=') m[4] = 0; // no '=' in USI format! + else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-( } } void GUI2Engine() { - char line[256], command[256], *p, *q; + char line[256], command[256], *p, *q, *r; while(1) { int i, x; - if(computer == stm || computer == ANALYZE) { -printf("# start search\n"); + if((computer == stm || computer == ANALYZE) && !suspended) { + DPRINT("# start search\n"); LoadPos(moveNr); // load position // and set engine thinking (note USI swaps colors!) startTime = GetTickCount(); - if(computer == ANALYZE) fprintf(toE, "\ngo infinite\n"), printf("\ngo infinite\n"); - else StartSearch(""); + if(computer == ANALYZE) { + fprintf(toE, "\ngo infinite"); DPRINT("\n# go infinite"); + if(sm & 1) { // some moves are disabled + fprintf(toE, " searchmoves"); DPRINT(" searchmoves"); + for(i=1; i i ? moveNr - i : 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); goto nomove; } + 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 && !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 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; } - if(argc < 2) { printf("usage is: U%cI2WB [-s] []\n", sc-32); exit(-1); } + if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] []\n", sc-32); exit(-1); } if(argc > 2) dir = argv[2]; if(argc > 3) suffix = argv[3];