/************************* UCI2WB by H.G.Muller ****************************/\r
\r
-#define VERSION "1.5"\r
+#define VERSION "2.0"\r
\r
#include <stdio.h>\r
-#include <windows.h>\r
-#include <io.h>\r
+#include <stdlib.h>\r
+#ifdef WIN32\r
+# include <windows.h>\r
+# include <io.h>\r
+ HANDLE process;\r
+ DWORD thread_id;\r
+#else\r
+# include <pthread.h>\r
+# include <signal.h>\r
+# define NO_ERROR 0\r
+# include <sys/time.h>\r
+ int GetTickCount() // with thanks to Tord\r
+ { struct timeval t; gettimeofday(&t, NULL); return t.tv_sec*1000 + t.tv_usec/1000; }\r
+#endif\r
#include <fcntl.h>\r
-\r
-#ifdef _MSC_VER \r
-#define SLEEP() Sleep(1) \r
-#else \r
-#define SLEEP() usleep(10) \r
-#endif \r
+#include <string.h>\r
\r
// Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".)\r
# define VARIANTS "normal,xiangqi"\r
\r
+#define DPRINT if(debug) printf\r
+\r
#define WHITE 0\r
#define BLACK 1\r
#define NONE 2\r
#define ANALYZE 3\r
\r
-char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix;\r
-int mps, tc, inc, stime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime;\r
-int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode\r
+char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, suspended, ponder, post, hasHash, c, sc='c', *suffix, *variants;\r
+int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug;\r
+int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500];\r
+char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20];\r
+char board[100]; // XQ board for UCCI\r
+char *nameWord = "name ", *valueWord = "value ", *wTime = "w", *bTime = "b", *wInc = "winc", *bInc = "binc", newGame; // keywords that differ in UCCI\r
+int unit = 1, drawOffer;\r
\r
-FILE *toE, *fromE;\r
-HANDLE process;\r
+FILE *toE, *fromE, *fromF;\r
int pid;\r
-DWORD thread_id;\r
+\r
+#ifdef WIN32\r
+WinPipe(HANDLE *hRd, HANDLE *hWr)\r
+{\r
+ SECURITY_ATTRIBUTES saAttr;\r
+\r
+ /* Set the bInheritHandle flag so pipe handles are inherited. */\r
+ saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);\r
+ saAttr.bInheritHandle = TRUE;\r
+ saAttr.lpSecurityDescriptor = NULL;\r
+\r
+ /* Create a pipe */\r
+ return CreatePipe(hRd, hWr, &saAttr, 0);\r
+}\r
+#endif\r
+\r
+#define INIT 0\r
+#define WAKEUP 1\r
+#define PAUSE 2\r
+\r
+void\r
+Sync (int action)\r
+{\r
+#ifdef WIN32\r
+ static HANDLE hWr, hRd; DWORD d; char c;\r
+ switch(action) {\r
+ case INIT: WinPipe(&hRd, &hWr); break;\r
+ case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break;\r
+ case PAUSE: ReadFile(hRd, &c, 1, &d, NULL);\r
+ }\r
+#else\r
+ static int syncPipe[2]; char c;\r
+ switch(action) {\r
+ case INIT: pipe(syncPipe); break;\r
+ case WAKEUP: write(syncPipe[1], "\n", 1); break;\r
+ case PAUSE: read(syncPipe[0], &c, 1);\r
+ }\r
+#endif\r
+}\r
+\r
+void\r
+FromFEN(char *fen)\r
+{ int i=0;\r
+ while(*fen) {\r
+ char c = *fen++;\r
+ if(c >= 'A') board[i++] = c; else\r
+ if(c == '/') i++; else\r
+ if(c == ' ') break; else\r
+ while(c-- > '0' && i < 99) board[i++] = 0;\r
+ if(i >= 99) break;\r
+ }\r
+}\r
+\r
+char *\r
+ToFEN(int stm)\r
+{\r
+ int i, n=0; static char fen[200]; char *p = fen;\r
+ for(i=0; i<99; i++) {\r
+ char c = board[i];\r
+ if(c >= 'A') { if(n) *p++ = '0' + n; n = 0; *p++ = c; } else n ++;\r
+ if(i%10 == 8) { if(n) *p++ = '0' + n; n = -1; *p++ = '/'; }\r
+ }\r
+ sprintf(p-1, " %c - - 0 1", stm);\r
+ return fen;\r
+}\r
+\r
+int\r
+Sqr(char *m, int j)\r
+{\r
+ int n = m[j] - 'a' + 10*('9' - m[j+1]);\r
+ if(n < 0) n = 0; else if(n > 99) n = 99; return n;\r
+}\r
+\r
+int\r
+Play(int nr)\r
+{\r
+ int i, last = -1;\r
+ FromFEN(iniPos + 4); // in XQ iniPos always has just "fen " prefix\r
+ for(i=0; i<nr; i++) {\r
+ int from=Sqr(move[i], 0), to=Sqr(move[i], 2);\r
+ if(board[to]) last = i;\r
+ board[to] = board[from]; board[from] = 0;\r
+ }\r
+ return last;\r
+}\r
\r
void\r
StartSearch(char *ponder)\r
{ // send the 'go' command to engine. Suffix by ponder.\r
- int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead!\r
- fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
- printf( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
- if(stime > 0) fprintf(toE, " movetime %d", stime),printf(" movetime %d", stime); else\r
- if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2);\r
- if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc);\r
- if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth);\r
- if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc);\r
- fprintf(toE, "%s\n", ponder);\r
- printf("%s\n", ponder);\r
+ int x = (ponder[0] != 0); // during ponder stm is the opponent\r
+ int black = (stm == BLACK ^ x ^ sc == 's'); // set if our color is what the engine calls black\r
+ int nr = moveNr + x; // we ponder for one move ahead!\r
+ if(sc == 'x') black = 1; else drawOffer = 0;// in UCCI 'black' refers to us and 'white' to opponent\r
+ if(!x && drawOffer) ponder = " draw", drawOffer = 0; //pass draw offer only when not pondering\r
+ fprintf(toE, "\ngo%s %stime %d %stime %d", ponder, bTime, black ? myTime : hisTime, wTime, !black ? myTime : hisTime);\r
+ DPRINT( "\n# go%s %stime %d %stime %d", ponder, bTime, black ? myTime : hisTime, wTime, !black ? myTime : hisTime);\r
+ if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else\r
+ if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); }\r
+ if(inc && !suffix) { fprintf(toE, " %s %d %s %d", wInc, inc, bInc, inc); DPRINT(" %s %d %s %d", wInc, inc, bInc, inc); }\r
+ if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); }\r
+ if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); }\r
+ fprintf(toE, "\n"); DPRINT("\n");\r
}\r
\r
void\r
StopPonder(int pondering)\r
{\r
if(!pondering) return;\r
- pause = 1;\r
- fprintf(toE, "stop\n");printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
- while(pause) SLEEP(); // wait for engine to acknowledge 'stop' with 'bestmove'.\r
+ pause = 2;\r
+ fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
+ Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'.\r
}\r
\r
void\r
LoadPos(int moveNr)\r
{\r
- int j;\r
- fprintf(toE, "%s moves", iniPos);\r
- printf( "# %s moves", iniPos);\r
- for(j=0; j<moveNr; j++) fprintf(toE, " %s", move[j]),printf(" %s", move[j]);\r
+ int j, lastCapt = 0; char *pos = iniPos, buf[200], stm;\r
+ if(sc == 'x') { // UCCI: send only reversible moves\r
+ lastCapt = Play(moveNr); // find last capture (returns -1 if none!)\r
+ Play(++lastCapt); // reconstruct board after last capture\r
+ stm = (!strstr(iniPos+4, " b ") ^ lastCapt & 1 ? 'w' : 'b');\r
+ sprintf(buf, "position fen %s", ToFEN(stm)); pos = buf; // send it as FEN (with "position" in UCCI!)\r
+ }\r
+ fprintf(toE, "%s moves", pos);\r
+ DPRINT( "# %s moves", pos);\r
+ for(j=lastCapt; j<moveNr; j++) { fprintf(toE, " %s", move[j]); DPRINT(" %s", move[j]); }\r
+}\r
+\r
+void\r
+StartPonder()\r
+{\r
+ if(!move[moveNr][0]) return; // no ponder move\r
+ LoadPos(moveNr+1);\r
+ pondering = 1; lastDepth = 1;\r
+ StartSearch(" ponder");\r
}\r
\r
char *Convert(char *pv)\r
}\r
\r
void\r
+Move4GUI(char *m)\r
+{\r
+ if(sc == 's') {\r
+ // convert USI move to WB format\r
+ m[2] = 'a'+'0'+size - m[2];\r
+ m[3] = 'a'+'0'+size - m[3];\r
+ if(m[1] == '*') { // drop\r
+ m[1] = '@';\r
+ } else {\r
+ m[0] = 'a'+'0'+size - m[0];\r
+ m[1] = 'a'+'0'+size - m[1];\r
+ if((stm == WHITE ? (m[1]>'0'+size-size/3 || m[3]>'0'+size-size/3)\r
+ : (m[1] <= '0'+size/3 || m[3] <= '0'+size/3)) && m[4] != '+')\r
+ m[4] = '=', m[5] = 0;\r
+ }\r
+ }\r
+}\r
+\r
+int\r
+GetChar()\r
+{\r
+ int c;\r
+ if(fromF) {\r
+ if((c = fgetc(fromF)) != EOF) return c;\r
+ fclose(fromF); fromF = 0; printf("# end fake\n");\r
+ }\r
+ return fgetc(fromE);\r
+}\r
+\r
+void *\r
Engine2GUI()\r
{\r
char line[1024], command[256];\r
\r
+ if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
while(1) {\r
- int i=0, x; char *p;\r
+ int i=0, x; char *p, dummy;\r
\r
fflush(stdout); fflush(toE);\r
- while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++;\r
+ while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++;\r
line[++i] = 0;\r
if(x == EOF) exit(0);\r
-printf("# engine said: %s", line);\r
- sscanf(line, "%s", command);\r
+ DPRINT("# engine said: %s", line), fflush(stdout);\r
+ if(sscanf(line, "%s", command) != 1) continue;\r
if(!strcmp(command, "bestmove")) {\r
- if(pause) { pondering = pause = 0; continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
+ if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
+ else if(pondering) { pondering = 0; printf("%d 0 0 0 UCI violation! Engine moves during ponder\n", lastDepth+1); continue; } // ignore ponder search\r
// move was a move to be played\r
+ if(p = strstr(line+8, " draw")) *p = 0, printf("offer draw\n"); // UCCI\r
if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; }\r
if(strstr(line+9, "(none)") || strstr(line+9, "null") ||\r
strstr(line+9, "0000")) { printf("%s\n", lastScore < -99999 ? "resign" : "1/2-1/2 {stalemate}"); computer = NONE; }\r
sscanf(line, "bestmove %s", move[moveNr++]);\r
myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder\r
- if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(stime) myTime = stime; // new session or move starts\r
+ if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts\r
+ stm = WHITE+BLACK - stm;\r
// first start a new ponder search, if pondering is on and we have a move to ponder on\r
if(p = strstr(line+9, "ponder")) {\r
+ sscanf(p+7, "%s", move[moveNr]);\r
if(computer != NONE && ponder) {\r
- sscanf(p+7, "%s", move[moveNr]);\r
-printf("# ponder on %s\n", move[moveNr]);\r
- LoadPos(moveNr+1); // load position\r
- // and set engine pondering\r
- pondering = 1; lastDepth = 1;\r
- StartSearch(" ponder");\r
+ DPRINT("# ponder on %s\n", move[moveNr]);\r
+ StartPonder();\r
}\r
p[-1] = '\n'; *p = 0; // strip off ponder move\r
- }\r
- if(sc == 's') {\r
- // convert USI move to WB format\r
- line[11] = 'a'+'0'+size - line[11];\r
- line[12] = 'a'+'0'+size - line[12];\r
- if(line[10] == '*') { // drop\r
- line[10] = '@';\r
- } else {\r
- line[9] = 'a'+'0'+size - line[9];\r
- line[10] = 'a'+'0'+size - line[10];\r
- if((stm == WHITE ? (line[10]>'0'+size-size/3 || line[12]>'0'+size-size/3)\r
- : (line[10] <= '0'+size/3 || line[12] <= '0'+size/3)) && line[13] != '+')\r
- line[13] = '=', line[14] = 0;\r
- }\r
- }\r
+ } else move[moveNr][0] = 0;\r
+ Move4GUI(line+9);\r
printf("move %s\n", line+9); // send move to GUI\r
- if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; }\r
- stm = WHITE+BLACK - stm;\r
+ if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; }\r
}\r
else if(!strcmp(command, "info")) {\r
int d=0, s=0, t=0, n=0;\r
char *pv;\r
+ if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; }\r
+ if(collect && (pv = strstr(line+5, "currmove "))) {\r
+ if(p = strstr(line+5, "currmovenumber ")) {\r
+ n = atoi(p+15);\r
+ if(collect == 1 && n != 1) continue; // wait for move 1\r
+ if(collect + (n == 1) > 2) { // done collecting\r
+ if(inex && collect == 2) printf("%d 0 0 0 OK to exclude\n", lastDepth);\r
+ collect = 3; continue;\r
+ }\r
+ collect = 2; on[nr=n] = 1; sscanf(pv+9, "%s", moveMap[n]); continue; // store move\r
+ }\r
+ }\r
if(!post) continue;\r
- if(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
+ if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
if(p = strstr(line+4, " depth ")) sscanf(p+7, "%d", &d), statDepth = d;\r
if(p = strstr(line+4, " score cp ")) sscanf(p+10, "%d", &s), statScore = s; else\r
if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else\r
if(p = strstr(line+6, " min ")) sscanf(p+1, "min %d", &min), *p = '\n';\r
if(p = strstr(line+6, " max ")) sscanf(p+1, "max %d", &max), *p = '\n';\r
if(p = strstr(line+6, " default ")) sscanf(p+1, "default %[^\n]*", val), *p = '\n';\r
- if(p = strstr(line+6, " name ")) sscanf(p+1, "name %[^\n]*", name);\r
- if(!strcmp(name, "Hash") || !strcmp(name, "USI_Hash")) {\r
+ if(!(p = strstr(line+6, " name "))) p = line+1; sscanf(p+6, "%[^\n]", name); // 'name' is omitted in UCCI\r
+ if(!strcasecmp(name, "Threads")) { strcpy(threadOpt, name); continue; }\r
+ if(!strcasecmp(name, "Ponder") || !strcasecmp(name, "USI_Ponder")) { strcpy(canPonder, name); continue; }\r
+ if(!strcasecmp(name, "Hash") || !strcasecmp(name, "USI_Hash") || !strcasecmp(name, "hashsize")) {\r
memory = oldMem = atoi(val); hasHash = 1; \r
strcpy(hashOpt, name);\r
continue;\r
}\r
+ if(!strcasecmp(name, "newgame") && !strcmp(type, "button")) { newGame++; continue; }\r
+ if(!strcasecmp(name, "usemillisec")) { unit = (!strcmp(val, "false") ? 2 : 1); continue; }\r
// pass on engine-defined option as WB option feature\r
if(!strcmp(type, "filename")) type[4] = 0;\r
sprintf(buf, "feature option=\"%s -%s", name, type); q = buf + strlen(buf);\r
}\r
else if(!strcmp(command, "id")) {\r
char name[256];\r
- if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
+ if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
+ }\r
+ else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands\r
+ else if(sc == 'x'&& !strcmp(command, "ucciok") || sscanf(command, "u%ciok", &c)==1 && c==sc) {\r
+ printf("feature smp=1 memory=%d done=1\n", hasHash);\r
+ if(unit == 2) unit = 1, fprintf(toE, "setoption usemillisec true\n");\r
+ Sync(WAKEUP); // done with options\r
}\r
- else if(!strcmp(command, "readyok")) pause = 0; // resume processing of GUI commands\r
- else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options\r
+ }\r
+}\r
+\r
+void\r
+Move4Engine(char *m)\r
+{\r
+ if(sc == 's') {\r
+ // convert input move to USI format\r
+ if(m[1] == '@') { // drop\r
+ m[1] = '*';\r
+ } else {\r
+ m[0] = 'a'+'0'+size - m[0];\r
+ m[1] = 'a'+'0'+size - m[1];\r
+ }\r
+ m[2] = 'a'+'0'+size - m[2];\r
+ m[3] = 'a'+'0'+size - m[3];\r
+ if(m[4] == '=') m[4] = 0; // no '=' in USI format!\r
+ else if(m[4]) m[4] = '+'; // cater to WB 4.4 bug :-(\r
}\r
}\r
\r
void\r
GUI2Engine()\r
{\r
- char line[256], command[256], *p, *q;\r
+ char line[256], command[256], *p, *q, *r;\r
\r
while(1) {\r
int i, x;\r
\r
- if(computer == stm || computer == ANALYZE) {\r
-printf("# start search\n");\r
- LoadPos(moveNr); // load position\r
+ if((computer == stm || computer == ANALYZE) && !suspended) {\r
+ DPRINT("# start search\n");\r
+ LoadPos(moveNr); fflush(stdout); // load position\r
// and set engine thinking (note USI swaps colors!)\r
startTime = GetTickCount();\r
- if(computer == ANALYZE) fprintf(toE, "\ngo infinite\n"), printf("\ngo infinite\n");\r
- else StartSearch("");\r
+ if(computer == ANALYZE) {\r
+ fprintf(toE, "\ngo infinite"); DPRINT("\n# go infinite");\r
+ if(sm & 1) { // some moves are disabled\r
+ fprintf(toE, " searchmoves"); DPRINT(" searchmoves");\r
+ for(i=1; i<nr; i++) if(on[i]) { fprintf(toE, " %s", moveMap[i]); DPRINT(" %s", moveMap[i]); }\r
+ }\r
+ fprintf(toE, "\n"); DPRINT("\n");\r
+ // code for searchmoves goes here\r
+ } else StartSearch("");\r
}\r
nomove:\r
fflush(toE); fflush(stdout);\r
i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++;\r
- line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); }\r
+ line[++i] = 0; if(x == EOF) { printf("# EOF\n"); fprintf(toE, "quit\n"); exit(-1); }\r
sscanf(line, "%s", command);\r
- while(pause) SLEEP(); // wait for readyok\r
if(!strcmp(command, "new")) {\r
- computer = BLACK; moveNr = 0; depth = -1;\r
+ computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0;\r
stm = WHITE; strcpy(iniPos, "position startpos");\r
- if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s value %d\n", hashOpt, memory);\r
+ if(memory != oldMem && hasHash) fprintf(toE, "setoption name %s %s%d\n", hashOpt, valueWord, memory);\r
oldMem = memory;\r
// we can set other options here\r
+ if(sc == 'x') { if(newGame) fprintf(toE, "setoption newgame\n"); } else // optional in UCCI\r
+ fprintf(toE, "u%cinewgame\n", sc); fflush(toE);\r
pause = 1; // wait for option settings to take effect\r
- fprintf(toE, "isready\n");\r
- fprintf(toE, "u%cinewgame\n", sc);\r
+ fprintf(toE, "isready\n"); fflush(toE);\r
+ Sync(PAUSE); // wait for readyok\r
}\r
else if(!strcmp(command, "usermove")) {\r
- if(sc == 's') {\r
- // convert input move to USI format\r
- if(line[10] == '@') { // drop\r
- line[10] = '*';\r
- } else {\r
- line[9] = 'a'+'0'+size - line[9];\r
- line[10] = 'a'+'0'+size - line[10];\r
- }\r
- line[11] = 'a'+'0'+size - line[11];\r
- line[12] = 'a'+'0'+size - line[12];\r
- if(line[13] == '=') line[13] = 0; // no '=' in USI format!\r
- else if(line[13] != '\n') line[13] = '+'; // cater to WB 4.4 bug :-(\r
- }\r
sscanf(line, "usermove %s", command); // strips off linefeed\r
- stm = WHITE+BLACK - stm;\r
+ Move4Engine(command);\r
+ stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0;\r
// when pondering we either continue the ponder search as normal search, or abort it\r
if(pondering || computer == ANALYZE) {\r
if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
+ char *draw = drawOffer ? " draw" : ""; drawOffer = 0;\r
pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now\r
- fprintf(toE, "ponderhit\n"); printf("# ponderhit\n");\r
+ fprintf(toE, "ponderhit%s\n", draw); DPRINT("# ponderhit%s\n", draw);\r
goto nomove;\r
}\r
StopPonder(1);\r
}\r
- sscanf(line, "usermove %s", move[moveNr++]); // possibly overwrites ponder move\r
+ strcpy(move[moveNr++], command); // possibly overwrites ponder move\r
}\r
else if(!strcmp(command, "level")) {\r
int sec = 0;\r
sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 ||\r
sscanf(line, "level %d %d %d", &mps, &tc, &inc);\r
- tc = (60*tc + sec)*1000; inc *= 1000; stime = 0;\r
+ tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; tc /= unit; inc /= unit;\r
}\r
else if(!strcmp(command, "option")) {\r
char name[80], *p;\r
+ if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else\r
if(p = strchr(line, '=')) {\r
*p++ = 0;\r
if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false");\r
- fprintf(toE, "setoption name %s value %s", line+7, p), printf("# setoption name %s value %s", line+7, p);\r
- } else fprintf(toE, "setoption name %s\n", line+7), printf("# setoption name %s\n", line+7);\r
+ fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption %s%s %s%s", nameWord, line+7, valueWord, p);\r
+ } else { fprintf(toE, "setoption %s%s\n", nameWord, line+7); DPRINT("# setoption %s%s\n", nameWord, line+7); }\r
}\r
else if(!strcmp(command, "protover")) {\r
- printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS);\r
- fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options\r
+ if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS;\r
+ printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 exclude=1 pause=1 sigint=0 sigterm=0 done=0\n", variants);\r
+ printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
+ fprintf(toE, sc == 'x' ? "ucci\n" : "u%ci\n", sc); fflush(toE); // prompt UCI engine for options\r
+ Sync(PAUSE); // wait for uciok\r
}\r
else if(!strcmp(command, "setboard")) {\r
- if(strstr(line+9, " b ")) stm = BLACK;\r
- if(sc == 's' && (p = strchr(line+9, '['))) { char c;\r
- *p++ = ' '; q = strchr(p, ']'); c = 'w' + 'b' - q[2]; strcpy(q+2, " 1\n"); while(*--q != ' ') q[2] = *q; *p = c; p[1] = ' '; \r
- }\r
- sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", line+9);\r
- iniPos[strlen(iniPos)-1] = 0;\r
+ stm = (strstr(line+9, " b ") ? BLACK : WHITE);\r
+ if(p = strchr(line+9, '[')) { char c;\r
+ *p++ = 0; q = strchr(p, ']'); *q = 0; r = q + 4; \r
+ if(sc == 's') q[2] = 'w' + 'b' - q[2], strcpy(r=q+3, " 1\n"); // Shogi: reverse color\r
+ else r = strchr(strchr(q+4, ' ') + 1, ' '); // skip to second space (after e.p. square)\r
+ *r = 0; sprintf(command, "%s%s %s %s", line+9, q+1, p, r+1);\r
+ } else strcpy(command, line+9);\r
+ sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", command);\r
+ iniPos[strlen(iniPos)-1] = sm = 0; collect = (computer == ANALYZE);\r
}\r
else if(!strcmp(command, "variant")) {\r
if(!strcmp(line+8, "shogi\n")) size = 9, strcpy(iniPos, "position startpos");\r
}\r
else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
if(pondering || computer == ANALYZE) StopPonder(1);\r
- moveNr = moveNr > i ? moveNr - i : 0;\r
+ moveNr = moveNr > i ? moveNr - i : 0; collect = (computer == ANALYZE); sm = 0;\r
}\r
else if(!strcmp(command, ".")) {\r
printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
goto nomove;\r
}\r
+ else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude\r
+ int all = !strcmp(line+8, "all"), in = command[1] == 'n';\r
+ inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag\r
+ for(i=1; i<nr; i++) { if(!strcmp(line+8, moveMap[i]) || all) on[i] = in; sm |= on[i]+1; } // sm: 2 = enabled, 1 = disabled\r
+ if(!(sm & 2)) goto nomove; // no moves enabled; continue current search\r
+ if(computer == ANALYZE) StopPonder(1); // abort old analysis\r
+ }\r
+ else if(!strcmp(command, "pause")) {\r
+ if(computer == stm) myTime -= GetTickCount() - startTime;\r
+ suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove\r
+ StopPonder(pondering || computer == stm);\r
+ }\r
+ else if(!strcmp(command, "resume")) {\r
+ if(suspended == 2) StartPonder(); // restart interrupted ponder search\r
+ suspended = 0; // causes thinking to start in normal way if on move or analyzing\r
+ }\r
else if(!strcmp(command, "xboard")) ;\r
- else if(!strcmp(command, "analyze"))computer = ANALYZE;\r
+ else if(!strcmp(command, "analyze"))computer = ANALYZE, collect = 1, sm = 0;\r
else if(!strcmp(command, "exit")) computer = NONE, StopPonder(1);\r
else if(!strcmp(command, "force")) computer = NONE, StopPonder(pondering);\r
else if(!strcmp(command, "go")) computer = stm;\r
- else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime *= 10;\r
- else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime *= 10;\r
+ else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime = (10*myTime)/unit;\r
+ else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit;\r
else if(!strcmp(command, "post")) post = 1;\r
else if(!strcmp(command, "nopost")) post = 0;\r
- else if(!strcmp(command, "easy")) ponder = 0;\r
- else if(!strcmp(command, "hard")) ponder = 1;\r
+ else if(!strcmp(command, "easy") && !!*canPonder) ponder = 0, StopPonder(pondering), fprintf(toE, "setoption %s%s %sfalse\n", nameWord, canPonder, valueWord);\r
+ else if(!strcmp(command, "hard") && !!*canPonder) ponder = 1, fprintf(toE, "setoption %s%s %strue\n", nameWord, canPonder, valueWord), StartPonder();\r
+ else if(!strcmp(command, "ping")) { static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1; printf("pong %s", line+5); }\r
else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
- else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores);\r
+ else if(!strcmp(command, "cores")&& !!*threadOpt) sscanf(line, "cores %d", &cores), fprintf(toE, "setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores);\r
else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth);\r
- else if(!strcmp(command, "st")) sscanf(line, "st %d", &stime), stime *= 1000, inc = 0;\r
+ else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0, sTime /= unit;\r
+ else if(!strcmp(command, "offer")) drawOffer = 1;\r
else if(!strcmp(command, "quit")) fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
}\r
}\r
int\r
StartEngine(char *cmdLine, char *dir)\r
{\r
+#ifdef WIN32\r
HANDLE hChildStdinRd, hChildStdinWr,\r
hChildStdoutRd, hChildStdoutWr;\r
- SECURITY_ATTRIBUTES saAttr;\r
BOOL fSuccess;\r
PROCESS_INFORMATION piProcInfo;\r
STARTUPINFO siStartInfo;\r
DWORD err;\r
\r
- /* Set the bInheritHandle flag so pipe handles are inherited. */\r
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);\r
- saAttr.bInheritHandle = TRUE;\r
- saAttr.lpSecurityDescriptor = NULL;\r
-\r
/* Create a pipe for the child's STDOUT. */\r
- if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) return GetLastError();\r
+ if (! WinPipe(&hChildStdoutRd, &hChildStdoutWr)) return GetLastError();\r
\r
/* Create a pipe for the child's STDIN. */\r
- if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return GetLastError();\r
+ if (! WinPipe(&hChildStdinRd, &hChildStdinWr)) return GetLastError();\r
\r
SetCurrentDirectory(dir); // go to engine directory\r
\r
pid = piProcInfo.dwProcessId;\r
fromE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdoutRd, _O_TEXT|_O_RDONLY), "r");\r
toE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdinWr, _O_WRONLY), "w");\r
-\r
+#else\r
+ char *argv[10], *p, buf[200];\r
+ int i, toEngine[2], fromEngine[2];\r
+\r
+ if (dir && dir[0] && chdir(dir)) { perror(dir); exit(1); }\r
+ pipe(toEngine); pipe(fromEngine); // create two pipes\r
+\r
+ if ((pid = fork()) == 0) { // Child\r
+ dup2(toEngine[0], 0); close(toEngine[0]); close(toEngine[1]); // stdin from toE pipe\r
+ dup2(fromEngine[1], 1); close(fromEngine[0]); close(fromEngine[1]); // stdout into fromE pipe\r
+ dup2(1, fileno(stderr)); // stderr into frome pipe\r
+\r
+ strcpy(buf, cmdLine); p = buf;\r
+ for (i=0;;) { argv[i++] = p; p = strchr(p, ' '); if (p == NULL) break; *p++ = 0; }\r
+ argv[i] = NULL;\r
+ execvp(argv[0], argv); // startup engine\r
+ \r
+ perror(argv[0]); exit(1); // could not start engine; quit.\r
+ }\r
+ signal(SIGPIPE, SIG_IGN);\r
+ close(toEngine[0]); close(fromEngine[1]); // close engine ends of pipes in adapter\r
+ \r
+ fromE = (FILE*) fdopen(fromEngine[0], "r"); // make into high-level I/O\r
+ toE = (FILE*) fdopen(toEngine[1], "w");\r
+#endif\r
return NO_ERROR;\r
}\r
\r
char *dir = NULL, *p, *q; int e;\r
\r
if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); }\r
+ if(argc > 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; }\r
+ if(argc > 1 && !strcmp(argv[1], "-var")) { variants = argv[2]; argc-=2; argv+=2; }\r
if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; }\r
- if(argc < 2) { printf("usage is: U%cI2WB [-s] <engine.exe> [<engine directory>]", sc-32); exit(-1); }\r
+ if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] <engine.exe> [<engine directory>]\n", sc-32); exit(-1); }\r
if(argc > 2) dir = argv[2];\r
if(argc > 3) suffix = argv[3];\r
\r
+ if(sc == 'x') nameWord = valueWord = bTime = "", wTime = "opp", bInc = "increment", wInc = "oppincrement", unit = 1000; // switch to UCCI keywords\r
+\r
// spawn engine proc\r
- if((errno = StartEngine(argv[1], dir)) != NO_ERROR) { perror(argv[1]), exit(-1); }\r
+ if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); }\r
+\r
+ Sync(INIT);\r
\r
// create separate thread to handle engine->GUI traffic\r
+#ifdef WIN32\r
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id);\r
+#else\r
+ { pthread_t t; signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); }\r
+#endif\r
\r
// handle GUI->engine traffic in original thread\r
GUI2Engine();\r