X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=7f90ba6a4b49abe6292b652f38aa5c9d49628bbd;hb=0b8da57d48c47856aac3b742aecc930f3c7b668d;hp=e709384f4d7d716d55d37fd5888dff9f92d3fcc3;hpb=64739477abdd86edbea682069de301b3affddddc;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index e709384..7f90ba6 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,6 +1,6 @@ /************************* UCI2WB by H.G.Muller ****************************/ -#define VERSION "1.5" +#define VERSION "1.7" #include #include @@ -20,27 +20,38 @@ #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; +char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, 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; char currMove[20]; // for analyze mode FILE *toE, *fromE; 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 +60,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) { @@ -65,14 +81,14 @@ 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); + DPRINT( "\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); 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 +96,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 +105,8 @@ LoadPos(int moveNr) { int j; fprintf(toE, "%s moves", iniPos); - printf( "# %s moves", iniPos); - for(j=0; j0 ? 100000 : -100000, statScore = s; else @@ -211,7 +228,7 @@ 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 @@ -227,7 +244,7 @@ GUI2Engine() int i, x; if(computer == stm || computer == ANALYZE) { -printf("# start search\n"); + DPRINT("# start search\n"); LoadPos(moveNr); // load position // and set engine thinking (note USI swaps colors!) startTime = GetTickCount(); @@ -270,7 +287,7 @@ printf("# start search\n"); if(pondering || computer == ANALYZE) { if(pondering && !strcmp(command, move[moveNr])) { // ponder hit pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now - fprintf(toE, "ponderhit\n"); printf("# ponderhit\n"); + fprintf(toE, "ponderhit\n"); DPRINT("# ponderhit\n"); goto nomove; } StopPonder(1); @@ -285,14 +302,17 @@ printf("# start search\n"); } else if(!strcmp(command, "option")) { char name[80], *p; + if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else if(p = strchr(line, '=')) { *p++ = 0; if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false"); - fprintf(toE, "setoption name %s value %s", line+7, p), printf("# setoption name %s value %s", line+7, p); - } else fprintf(toE, "setoption name %s\n", line+7), printf("# setoption name %s\n", line+7); + fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption name %s value %s", line+7, p); + } else { fprintf(toE, "setoption name %s\n", line+7); DPRINT("# setoption name %s\n", line+7); } } else if(!strcmp(command, "protover")) { - printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS); + if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS; + printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", variants); + printf("feature option=\"UCI2WB debug output -check %d\"\n", debug); fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options } else if(!strcmp(command, "setboard")) { @@ -327,10 +347,11 @@ printf("# start search\n"); else if(!strcmp(command, "nopost")) post = 0; else if(!strcmp(command, "easy")) ponder = 0; else if(!strcmp(command, "hard")) ponder = 1; + else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5); else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory); else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores); else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth); - else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime *= 1000, inc = 0; + else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0; else if(!strcmp(command, "quit")) fprintf(toE, "quit\n"), fflush(toE), exit(0); } } @@ -341,22 +362,16 @@ StartEngine(char *cmdLine, char *dir) #ifdef WIN32 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr; - SECURITY_ATTRIBUTES saAttr; BOOL fSuccess; PROCESS_INFORMATION piProcInfo; STARTUPINFO siStartInfo; DWORD err; - /* Set the bInheritHandle flag so pipe handles are inherited. */ - saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = TRUE; - saAttr.lpSecurityDescriptor = NULL; - /* Create a pipe for the child's STDOUT. */ - if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdoutRd, &hChildStdoutWr)) return GetLastError(); /* Create a pipe for the child's STDIN. */ - if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdinRd, &hChildStdinWr)) return GetLastError(); SetCurrentDirectory(dir); // go to engine directory @@ -431,8 +446,10 @@ main(int argc, char **argv) char *dir = NULL, *p, *q; int e; if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); } + if(argc > 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];