# include <io.h>\r
HANDLE process;\r
DWORD thread_id;\r
+ void Bury(int s) { if(WaitForSingleObject(process, 1000*s+50) != WAIT_OBJECT_0) TerminateProcess(process, 0); }\r
#else\r
# include <pthread.h>\r
# include <signal.h>\r
# include <unistd.h>\r
# define NO_ERROR 0\r
# include <sys/time.h>\r
+# include <sys/wait.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
//# include <unistd.h>\r
int Sleep(int msec) { return usleep(1000*msec); }\r
+ int pid; void Bury(int msec) { Sleep(msec+50); if(waitpid(-1, NULL, WNOHANG) <= 0) kill(pid, SIGKILL); }\r
#endif\r
#include <fcntl.h>\r
#include <string.h>\r
#define VARIANTS ",normal,xiangqi"\r
#define STDVARS "chess,chess960,crazyhouse,3check,giveaway,suicide,losers,atomic,seirawan,shogi,xiangqi"\r
#define EGT ",gaviotaTbPath,syzygyPath,nalimovPath,robbotripleBaseDirectory,robbototalBaseDirectory,bitbases path,"\r
+#define XQPOS "position fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR"\r
\r
#define DPRINT if(debug) printf\r
#define EPRINT(X) { char f[999]; sprintf X; DPRINT("%s", f); fprintf(toE, "%s", f + 2*(*f == '#')); /* strip optional # prefix */ }\r
#define NONE 2\r
#define ANALYZE 3\r
\r
-char move[2000][10], iniPos[256], hashOpt[20], suspended, ponder, post, hasHash, c, sc='c', suffix[81], varOpt, searching, *binary;\r
+char move[2000][10], iniPos[256], hashOpt[20], suspended, ponder, post, hasHash, c, sc=' ', sc2, suffix[81], varOpt, searching, *binary;\r
int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug, flob;\r
-int statDepth, statScore, statNodes, statTime, currNr, size, collect, nr, sm, inex, on[500], frc, byo = -1, namOpt, comp;\r
+int statDepth, statScore, statNodes, statTime, currNr, hsize=9, size=9, collect, nr, sm, inex, on[500], frc, byo = -1, namOpt, comp;\r
char currMove[20], moveMap[500][10], /* for analyze mode */ canPonder[20], threadOpt[20], varList[8000], anaOpt[20], checkOptions[8192] = "Ponder";\r
char pvs[99][999], 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, scores[99], mpvSP, maxDepth, ponderAlways, newCnt, priority;\r
+int unit = 1, drawOffer, scores[99], mpvSP, maxDepth, ponderAlways, newCnt, priority, killDelay;\r
\r
FILE *toE, *fromE, *fromF;\r
-int pid;\r
\r
char *strcasestr (char *p, char *q) { while(*p) { char *r=p++, *s=q; while(tolower(*r++) == tolower(*s) && *s) s++; if(!*s) return p-1; } return NULL; }\r
\r
Play(int nr)\r
{\r
int i, last = -1;\r
- FromFEN(iniPos + 4); // in XQ iniPos always has just "fen " prefix\r
+ FromFEN(iniPos + 13); // FEN part\r
for(i=0; i<nr; i++) {\r
int from=Sqr(move[i], 0), to=Sqr(move[i], 2);\r
if(board[to] || (board[from]|32) == 'p' && move[i][1] != move[i][3]) last = i;\r
if(sTime > 0) EPRINT((f, " movetime %d", sTime)) else\r
if(mps) EPRINT((f, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2))\r
if(flob || byo >= 0) sprintf(suffix, " byoyomi %d", t); // for engines running purely on byoyomi\r
- if(inc && !*suffix) EPRINT((f, " %s %d %s %d", wInc, inc, bInc, inc))\r
+ if((inc || !mps && !sTime && sc == 'x') && !*suffix) EPRINT((f, " %s %d %s %d", wInc, inc, bInc, inc))\r
if(depth > 0) EPRINT((f, " depth %d", depth))\r
if(*suffix) EPRINT((f, suffix, inc))\r
EPRINT((f, "\n")); maxDepth = mpvSP = 0;\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
+ stm = (!strstr(iniPos+13, " 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
EPRINT((f, "# %s moves", pos))\r
if(sc != 's') return pv;\r
p = pv; q = buf;\r
while(c = *p++) {\r
- if(c >= '0' && c <= '9' || c >= 'a' && c <= 'z') *q++ = 'a'+'0'+size - c; else *q++ = c;\r
+ if(c >= '0' && c <= '9' || c >= 'a' && c <= 'z') *q++ = 'a'+'0' + (c >= 'a' ? size : hsize) - c; else *q++ = c;\r
}\r
*q++ = 0;\r
return buf;\r
{\r
if(sc == 's') {\r
// convert USI move to WB format\r
- m[2] = 'a'+'0'+size - m[2];\r
+ m[2] = 'a'+'0'+hsize - 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[0] = 'a'+'0'+hsize - 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
}\r
\r
int\r
-ReadLine (FILE *f, char *line)\r
+ReadLine (FILE *f, char *line, int len)\r
{\r
int x, i = 0;\r
- while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0;\r
+ while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i+=(i<len); line[++i] = 0;\r
return (x != EOF);\r
}\r
\r
char line[1024], command[256]; static char egts[999];\r
\r
while(1) {\r
- int i=0; char *p, dummy;\r
+ int i=0; char *p, *q, dummy;\r
\r
fflush(stdout); fflush(toE);\r
- if(fromF && !ReadLine(fromF, line)) fromF = 0, printf("# end fake\n");\r
- if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
+ if(fromF && !ReadLine(fromF, line, 1022)) fromF = 0, printf("# end fake\n");\r
+ if(!fromF && !ReadLine(fromE, line, 1022)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
DPRINT("# engine said: %s", line), fflush(stdout);\r
if(sscanf(line, "%s", command) != 1) continue;\r
if(!strcmp(command, "bestmove")) {\r
if(computer != NONE && ponder) StartPonder(moveNr);\r
p[-1] = '\n'; *p = 0; // strip off ponder move\r
} else move[moveNr][0] = 0;\r
- Move4GUI(line+9);\r
- printf("move %s\n", line+9); // send move to GUI\r
- if(move[moveNr][0]) printf("Hint: %s\n", move[moveNr]);\r
+ if((q = strchr(line+9, ','))) { // multi-leg move\r
+ *q++ = 0; Move4GUI(line+9); // split\r
+ printf("move %s,\n", line+9); // send first leg\r
+ } else q = line + 9;\r
+ Move4GUI(q);\r
+ printf("move %s\n", q); // send move to GUI\r
+ if(p) Move4GUI(p+7), printf("Hint: %s\n", p+7);\r
if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == BLACK ? "1-0" : "0-1"); computer = NONE; }\r
fflush(stdout); return;\r
}\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
+ memory = oldMem = atoi(val); hasHash = max; \r
strcpy(hashOpt, name);\r
continue;\r
}\r
if(!*varList) strcpy(varList, sc=='s' ? ",shogi,5x5+5_shogi" : VARIANTS); // without clue guess liberally\r
printf("feature variants=\"%s\"\n", varList+1); // from UCI_Variant combo and/or UCI_Chess960 check options\r
if(*egts) printf("feature egt=\"%s\"\n", egts+1);\r
- printf("feature smp=1 memory=%d done=1\n", hasHash);\r
+ printf("feature smp=1 memory=%d done=1\n", hasHash > 0);\r
if(unit == 2) { unit = 1; EPRINT((f, "# setoption usemillisec true\n")) }\r
fflush(stdout); return; // done with options\r
}\r
if(m[1] == '@') { // drop\r
m[1] = '*';\r
} else {\r
- m[0] = 'a'+'0'+size - m[0];\r
+ m[0] = 'a'+'0'+hsize - m[0];\r
m[1] = 'a'+'0'+size - m[1];\r
}\r
- m[2] = 'a'+'0'+size - m[2];\r
+ m[2] = 'a'+'0'+hsize - 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
for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking\r
fflush(toE); fflush(stdout);\r
- if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n");\r
+ if(!ReadLine(stdin, line, 254)) printf("# EOF\n"), sprintf(line, "quit -1\n");\r
if(!sscanf(line, "%s", command)) return;\r
if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first\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, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly\r
+ else if(!strcmp(command, "draw")) drawOffer = 1; // backlogged anyway, so this can be done instantly\r
else if(!strcmp(command, "post")) post = 1;\r
else if(!strcmp(command, "nopost"))post = 0;\r
else if(!strcmp(command, ".")) {\r
EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout);\r
} else {\r
if(searching) StopSearch(1); // ponder miss or analysis, as moves won't arrive during thinking\r
- p = line+7; while(qEnd < queue+10000 && (*qEnd++ = *p++) != '\n') {}\r
+ p = command; *qEnd++ = 'e'; *qEnd++ = ' '; while(qEnd < queue+10000 && (*qEnd++ = *p++)) {} *qEnd++ = '\n';\r
Sync(WAKEUP); // queue move for adding it to game (and toggle stm)\r
}\r
} else\r
p = line; while(qEnd < queue+10000 && (*qEnd++ = *p++) != '\n') {}\r
Sync(WAKEUP);\r
// when 'stop' doesn't catch engine's attention in reasonable time, so the GUI might kill us:\r
- if(!strcmp(command, "quit")) { Sleep(500); EPRINT((f, "quit\n")); } // make sure 'quit' is still sent\r
+ if(!strcmp(command, "quit")) { Bury(killDelay); exit(0); } // kill the engine and exit\r
}\r
}\r
}\r
\r
+void\r
+IsReady ()\r
+{\r
+ EPRINT((f, "# isready\n")) fflush(toE);\r
+ HandleEngineOutput(); // wait for readyok\r
+}\r
+\r
void\r
DoCommand ()\r
{\r
- char line[1024], command[256], *p, *q, *r, type[99];\r
+ char line[1024], command[256], *p, *q, *r, type[99], c;\r
int i;\r
\r
p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0;\r
oldMem = memory;\r
// we can set other options here\r
if(varOpt && strstr(varList, ",normal")) EPRINT((f, "# setoption name UCI_Variant value chess\n"))\r
- EPRINT((f, "# isready\n")) fflush(toE);\r
- HandleEngineOutput(); // wait for readyok\r
+ IsReady();\r
if(sc == 'x') { if(newGame) EPRINT((f, "# setoption newgame\n")) } else // optional in UCCI\r
- EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE);\r
+ { EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE); if(sc != 's') IsReady(); } // avoid isready in USI\r
}\r
else if(!strcmp(command, "e")) { strcpy(move[moveNr++], type); stm ^= WHITE|BLACK; return; }\r
else if(!strcmp(command, "option")) {\r
EPRINT((f, "# setoption name UCI_Variant value %sucinewgame\nisready\n", line+8))\r
fflush(toE); HandleEngineOutput(); // wait for readyok\r
}\r
- if(!strcmp(line+8, "shogi\n")) size = 9, strcpy(iniPos, "position startpos");\r
- if(!strcmp(line+8, "5x5+5_shogi\n")) size = 5, strcpy(iniPos, "position startpos");\r
- if(!strcmp(line+8, "xiangqi\n")) strcpy(iniPos, "fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r");\r
+ c = 0; if(sscanf(line+8, "%dx%d+%d_shogi%c", &hsize, &size, &i, &c) && c == '\n') strcpy(iniPos, "position startpos");\r
+ if(!strcmp(line+8, "shogi\n")) hsize = size = 9, strcpy(iniPos, "position startpos");\r
+ if(!strcmp(line+8, "xiangqi\n")) sprintf(iniPos, "%s %c", XQPOS+9*(sc2 == 'c'), sc2 == 'c' ? 'r' : 'w'); // with -c use Cyclone dialect\r
if(!strcmp(line+8, "fischerandom\n")) { frc |= 1; if(frc > 0) EPRINT((f, "# setoption name UCI_Chess960 value true\n")) }\r
}\r
else if(!strcmp(command, "undo") && (i=1) || !strcmp(command, "remove") && (i=2)) {\r
else if(!strcmp(command, "force")) computer = NONE;\r
else if(!strcmp(command, "go")) computer = stm;\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("po%s", line+2); }\r
- else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
+ else if(!strcmp(command, "memory")) { sscanf(line, "memory %d", &memory); if(memory > hasHash) memory = hasHash; }\r
else if(!strcmp(command, "cores")&& !!*threadOpt) { sscanf(line, "cores %d", &cores); EPRINT((f, "# setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores)) }\r
else if(!strcmp(command, "egtpath")){\r
sscanf(line+8, "%s %[^\n]", type, command);\r
if(sc == 's') EPRINT((f, "# gameover %s\n", line[8] == '/' ? "draw" : (line[7] == '0') == mySide ? "win" : "lose"))\r
computer = NONE;\r
}\r
- else if(!strcmp(command, "quit")) { EPRINT((f, "# quit\n")) fflush(toE), exit(atoi(line+4)); }\r
+ else if(!strcmp(command, "quit")) { EPRINT((f, "# quit\n")) fflush(toE); }\r
else printf("Error (unknown command): %s\n", command);\r
\r
fflush(stdout);\r
CloseHandle(hChildStdoutWr);\r
\r
process = piProcInfo.hProcess;\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
#else\r
if(argc > 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; }\r
if(argc > 1 && !strcmp(argv[1], "-var")) { strcpy(varList+1, argv[2]); *varList = ','; argc-=2; argv+=2; }\r
if(argc > 2 && !strcmp(argv[1], "-nice")) { sscanf(argv[2], "%d", &priority); argc-=2; argv+=2; }\r
+ if(argc > 2 && !strcmp(argv[1], "-kill")) { sscanf(argv[2], "%d", &killDelay); 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 [debug] [-s] <engine.exe> [<engine directory>]\n", sc-32); exit(-1); }\r
+ if(argc < 2) { printf("usage is: %s [debug] [-s|-x|-c|-f] <engine.exe> [<engine directory>]\n", argv[0]); exit(-1); }\r
if(argc > 2) dir = argv[2];\r
if(argc > 3) strncpy(suffix, argv[3], 80);\r
\r
if(sc == 'x') nameWord = valueWord = bTime = "", wTime = "opp", bInc = "increment", wInc = "oppincrement", unit = 1000; // switch to UCCI keywords\r
else if(sc == 'f' ) frc = -1, sc = 'c'; // UCI for unannounced Chess960\r
- else if(sc == 'n') sc = 'c'; // UCI for normal Chess\r
+ else if(sc != 's') sc2 = sc, sc = 'c'; // UCI for normal Chess\r
\r
// spawn engine proc\r
if(StartEngine(binary = argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); }\r