X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=0b06fbbee020197633ef098a5f3a57ef69436aed;hb=0547eaa10ec8df7807fceb591c9692c77081afab;hp=2f4a7679ad148798ca5ed907318839dccd56e7c5;hpb=a630428ed1892dfc762c575417e29d43a027858c;p=hachu.git diff --git a/hachu.c b/hachu.c index 2f4a767..0b06fbb 100644 --- a/hachu.c +++ b/hachu.c @@ -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 @@ -1565,6 +1583,8 @@ FireSet (UndoInfo *tb) if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2]; } +void TerminationCheck(); + #define QSdepth 0 int @@ -1573,7 +1593,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 +1618,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 @@ -1779,6 +1799,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 +1853,7 @@ 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 + if(!(abortFlag & 1) && GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish } replyDep = iterDep; #ifdef HASH @@ -1842,6 +1867,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 +1967,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 +2114,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 +2226,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; @@ -2259,18 +2300,26 @@ printf("# setup done");fflush(stdout); 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 @@ -2280,10 +2329,11 @@ printf("# setup done");fflush(stdout); 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); + score = SearchBestMove(&move, &ponderMove); if(move == INVALID) { // game apparently ended int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos;