X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=aa37ab3f44b6648e853fe784e1e3426c0a784385;hb=c40c2b9d2eb9abc3cd530a1ff96aaf0e6fc91643;hp=2f4a7679ad148798ca5ed907318839dccd56e7c5;hpb=a630428ed1892dfc762c575417e29d43a027858c;p=hachu.git diff --git a/hachu.c b/hachu.c index 2f4a767..aa37ab3 100644 --- a/hachu.c +++ b/hachu.c @@ -16,9 +16,9 @@ #define PATH level==0 || path[0] == 0xc4028 && (level==1 /*|| path[1] == 0x75967 && (level == 2 || path[2] == 0x3400b && (level == 3))*/) //define PATH 0 -#define HASH -#define KILLERS -#define NULLMOVE +#define XHASH +#define XKILLERS +#define XNULLMOVE #include #include @@ -29,8 +29,25 @@ #ifdef WIN32 # include + int InputWaiting() + { // checks for waiting input in pipe + static int pipe, init; + static HANDLE inp; + DWORD cnt; + + if(!init) inp = GetStdHandle(STD_INPUT_HANDLE); + if(!PeekNamedPipe(inp, NULL, 0, NULL, &cnt, NULL)) return 1; + return cnt; + } #else # include +# include + int InputWaiting() + { + int cnt; + if(ioctl(0, FIONREAD, &cnt)) return 1; + return cnt; + } int GetTickCount() // with thanks to Tord { struct timeval t; gettimeofday(&t, NULL); @@ -114,7 +131,8 @@ typedef struct { char *array, fenArray[4000], *reason; int bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws; int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore; -int nodes, startTime, tlim1, tlim2, repCnt, comp; +int nodes, startTime, tlim1, tlim2, tlim3, repCnt, comp, abortFlag; +Move ponderMove; Move retMove, moveStack[10000], path[100], repStack[300], pv[1000], repeatMove[300], killer[100][2]; int maxDepth; // used by search @@ -1482,6 +1500,7 @@ GenCapts(int sqr, int victimValue) NewCapture(x, SPECIAL + 8*(i-1&7) + (i+1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag); v = kStep[i+1]; if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr]) + NewCapture(x, SPECIAL + 8*(i+1&7) + (i-1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag); } } else { // primary victim on first ring @@ -1565,6 +1584,8 @@ FireSet (UndoInfo *tb) if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2]; } +void TerminationCheck(); + #define QSdepth 0 int @@ -1573,7 +1594,7 @@ Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSupp int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr; int resDep; int myPV = pvPtr; - int score, bestScore, curEval, iterAlpha; + int score, bestScore, oldBest, curEval, iterAlpha; Move move, nullMove; UndoInfo tb; #ifdef HASH @@ -1598,7 +1619,7 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval= alpha -= (alpha < curEval); beta -= (beta <= curEval); - nodes++; + if(!(nodes++ & 4095)) TerminationCheck(); pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent @@ -1620,8 +1641,8 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval= } #endif - replyDep = (depth < 1 ? depth : iterDep < 1 ? 1 : iterDep); - while(++iterDep <= depth) { + replyDep = (depth < 1 ? depth-1 : iterDep); + while(++iterDep <= depth || iterDep == 1) { if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout); iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0; resDep = 60; for(curMove = firstMove; ; curMove++) { // loop over moves @@ -1779,6 +1800,11 @@ level--; repetition: UnMake(&tb); xstm = stm; stm ^= WHITE; + if(abortFlag > 0) { // unwind search +printf("# abort (%d) @ %d\n", abortFlag, level); + if(curMove == firstMove) bestScore = oldBest, bestMoveNr = firstMove; // none searched yet + goto leave; + } #if 1 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore); @@ -1828,7 +1854,8 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM printf("\n"); fflush(stdout); } - if(GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish +printf("# s=%d t=%d 1=%d 3=%d f=%d\n",startTime,GetTickCount(),tlim1,tlim3,abortFlag); + if(!(abortFlag & 1) && GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish } replyDep = iterDep; #ifdef HASH @@ -1842,6 +1869,7 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM } else hashTable[index].move[hit] = 0; #endif } // next depth +leave: retMSP = msp; retFirst = firstMove; msp = oldMSP; // pop move list @@ -1941,7 +1969,7 @@ typedef Move MOVE; void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB char *MoveToText(MOVE move, int m); // converts the move from your internal format to text like e2e2, e1g1, a7a8q. MOVE ParseMove(char *moveText); // converts a long-algebraic text move to your internal move format - int SearchBestMove(int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove); + int SearchBestMove(MOVE *move, MOVE *ponderMove); void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input. UndoInfo undoInfo; @@ -2088,7 +2116,7 @@ ListMoves () int i; for(i=0; i< BSIZE; i++) boardCopy[i] = !!board[i]; MapFromScratch(attacks); - postThinking--; repCnt = 0; tlim1 = tlim2 = 1e8; msp = 0; + postThinking--; repCnt = 0; tlim1 = tlim2 = tlim3 = 1e8; abortFlag = msp = 0; Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2); postThinking++; listStart = retFirst; listEnd = msp = retMSP; @@ -2200,31 +2228,46 @@ Highlight(char *coords) printf("highlight %s\n", buf); } -int -SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove) +int timeLeft; // timeleft on engine's clock +int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search +char inBuf[8000], command[80], ponderMoveText[20]; + +void +SetSearchTimes (int timeLeft) { - int score, targetTime, movesLeft = BW*BH/4 + 20; + int targetTime, movesLeft = BW*BH/4 + 20; if(mps) movesLeft = mps - (moveNr>>1)%mps; - targetTime = timeLeft*10 / (movesLeft + 2) + 1000 * inc; + targetTime = (timeLeft - 1000*inc) / (movesLeft + 2) + 1000 * inc; if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening - if(timePerMove > 0) targetTime = timeLeft * 5; - startTime = GetTickCount(); + if(timePerMove > 0) targetTime = 0.5*timeLeft, movesLeft = 1; tlim1 = 0.2*targetTime; tlim2 = 1.9*targetTime; + tlim3 = 5*timeLeft / (movesLeft + 4.1); +} + +int +SearchBestMove (MOVE *move, MOVE *ponderMove) +{ + int score; + startTime = GetTickCount(); nodes = 0; MapFromScratch(attacks); retMove = INVALID; repCnt = 0; score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2); *move = retMove; - *ponderMove = INVALID; + *ponderMove = pv[1]; return score; } + void PonderUntilInput (int stm) { +MapFromScratch(attacks); + repCnt = 0; abortFlag = -1; + Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2); } - + int TakeBack(int n) { // reset the game and then replay it to the desired point int last, stm; @@ -2254,36 +2297,49 @@ printf("# setup done");fflush(stdout); // extract the first word sscanf(inBuf, "%s", command); +printf("# in (mode = %d,%d): %s\n", root, abortFlag, command); if(!strcmp(command, "otim")) { continue; } // do not start pondering after receiving time commands, as move will follow immediately if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); continue; } if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; } // ditto if(!strcmp(command, ".")) { inBuf[0] = 0; return; } // ignore for now if(!strcmp(command, "lift")) { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here + abortFlag = 1; return; } } + void + TerminationCheck() + { + if(abortFlag < 0) { // check for input + if(InputWaiting()) GetLine(0); // read & examine input command + } else { // check for time + if(GetTickCount() - startTime > tlim3) abortFlag = 2; + } + } + main() { int engineSide=NONE; // side played by engine - int timeLeft; // timeleft on engine's clock - int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search - MOVE move, ponderMove; + MOVE move; int i, score, curVarNr; - char inBuf[8000], command[80]; Init(V_CHU); // Chu + listEnd = 1; while(1) { // infinite loop fflush(stdout); // make sure everything is printed before we do something that might take time + *inBuf = 0; if(listEnd == 0) ListMoves(); // always maintain a list of legal moves in root position + abortFlag = 0; if(stm == engineSide) { // if it is the engine's turn to move, set it thinking, and let it move pboard(board); - score = SearchBestMove(stm, timeLeft, mps, timeControl, inc, timePerMove, &move, &ponderMove); + SetSearchTimes(10*timeLeft); + score = SearchBestMove(&move, &ponderMove); if(move == INVALID) { // game apparently ended int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos; @@ -2307,6 +2363,7 @@ pboard(board); gameMove[moveNr++] = move; // remember game printf("move %s\n", MoveToText(move, 1)); listEnd = 0; + continue; } }