Keep track of PV
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index 1ea7853..05529dc 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -81,12 +81,12 @@ typedef struct {
 } PieceDesc;\r
 \r
 typedef struct {\r
-  int from, to, piece, victim, new, booty, epSquare, epVictim, ep2Square, ep2Victim;\r
+  int from, to, piece, victim, new, booty, epSquare, epVictim, ep2Square, ep2Victim, savKeyL, savKeyH;\r
 } UndoInfo;\r
 \r
-int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, retMSP, retFirst, level, chuFlag=1;\r
+int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, pvPtr, level, chuFlag=1;\r
 int nodes, startTime, tlim1, tlim2;\r
-Move retMove, moveStack[10000], path[100];\r
+Move retMove, moveStack[10000], path[100], pv[1000];\r
 \r
 #define X 36 /* slider              */\r
 #define J -1 /* jump                */\r
@@ -675,10 +675,13 @@ AddMove (int i, int x, int y)
 }\r
 #endif\r
 \r
+int flag;\r
+\r
 inline int\r
 NewNonCapture (int x, int y, int promoFlags)\r
 {\r
   if(board[y] != EMPTY) return 1; // edge, capture or own piece\r
+//if(flag) printf("# add %c%d%c%d, pf=%d\n", x%BW+'a',x/BW,y%BW+'a',y/BW, promoFlags);\r
   if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move\r
     moveStack[msp++] = moveStack[nonCapts];           // create space for promotion\r
     moveStack[nonCapts++] = x<<SQLEN | y | PROMOTE;   // push promotion\r
@@ -690,6 +693,7 @@ NewNonCapture (int x, int y, int promoFlags)
     }\r
   } else\r
     moveStack[msp++] = x<<SQLEN | y; // push normal move\r
+//if(flag) printf("msp=%d nc=%d\n", msp, nonCapts);    \r
   return 0;\r
 }\r
 \r
@@ -995,6 +999,8 @@ MakeMove(Move m, UndoInfo *u)
   u->piece = board[u->from];\r
   board[u->from] = EMPTY;\r
   u->booty = 0;\r
+  u->savKeyL = hashKeyL;\r
+  u->savKeyH = hashKeyH;\r
 \r
   if(m & (PROMOTE | DEFER)) {\r
     if(m & DEFER) {\r
@@ -1065,6 +1071,9 @@ UnMake(UndoInfo *u)
   p[u->new].pos = ABSENT; \r
   p[u->piece].pos = u->from; // this can be the same as above\r
   board[u->from] = u->piece;\r
+\r
+  hashKeyL = u->savKeyL;\r
+  hashKeyH = u->savKeyH;\r
 }\r
        \r
 void\r
@@ -1177,13 +1186,11 @@ Evaluate ()
   return 0;\r
 }\r
 \r
-int flag;\r
-\r
 int\r
 Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSuppress)\r
 {\r
   int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr;\r
-  int savHashL = hashKeyL, savHashH = hashKeyH;\r
+  int myPV = pvPtr;\r
   int score, bestScore, curEval, iterAlpha;\r
   Move move, nullMove;\r
   UndoInfo tb;\r
@@ -1206,6 +1213,9 @@ if(PATH) printf("search(%d) %d-%d eval=%d, stm=%d\n",depth,alpha,beta,difEval,st
   alpha -= (alpha < curEval);\r
   beta  -= (beta <= curEval);\r
 \r
+  nodes++;\r
+  pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent\r
+\r
   firstMove = curMove = sorted = msp += 20; // leave 20 empty slots in front of move list\r
   phase = 0; iterDep=1; replyDep = (depth < 1 ? depth : 1) - 1;\r
   do {\r
@@ -1318,8 +1328,6 @@ if(PATH) pmap(attacks, stm);
 attacks -= 2*BSIZE;\r
 level--;\r
       UnMake(&tb);\r
-      hashKeyL = savHashL;\r
-      hashKeyH = savHashH;\r
       xstm = stm; stm ^= WHITE;\r
 #if 1\r
 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore);\r
@@ -1343,6 +1351,10 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
            // update killer\r
            goto cutoff;\r
          }\r
+         { int i=pvPtr;\r
+           for(pvPtr = myPV+1; pv[pvPtr++] = pv[i++]; ); // copy daughter PV\r
+           pv[myPV] = move;                              // behind our move (pvPtr left at end of copy)\r
+         }\r
        }\r
 \r
       }\r
@@ -1356,7 +1368,8 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
   } while(++iterDep <= depth); // next depth\r
   retMSP = msp;\r
   retFirst = firstMove;\r
-  msp = oldMSP;\r
+  msp = oldMSP; // pop move list\r
+  pvPtr = myPV; // pop PV\r
   retMove = bestMoveNr ? moveStack[bestMoveNr] : 0;\r
 if(flag && depth >= 0) printf("return %d: %d %d\n", depth, bestScore, curEval);\r
   return bestScore + (bestScore < curEval);\r
@@ -1466,12 +1479,14 @@ MakeMove2 (int stm, MOVE move)
 {\r
   sup0 = sup1; sup1 = sup2;\r
   sup2 = MakeMove(move, &undoInfo);\r
+  rootEval = -rootEval - undoInfo.booty;\r
   return stm ^ WHITE;\r
 }\r
 \r
 void\r
 UnMake2 (MOVE move)\r
 {\r
+  rootEval = -rootEval - undoInfo.booty;\r
   UnMake(&undoInfo);\r
   sup2 = sup1; sup1 = sup0;\r
 }\r
@@ -1481,6 +1496,7 @@ Setup2 (char *fen)
 {\r
   SetUp(chuArray, chuPieces);\r
   sup0 = sup1 = sup2 = ABSENT;\r
+  rootEval = 0;\r
   return WHITE;\r
 }\r
 \r
@@ -1551,6 +1567,7 @@ MapFromScratch(attacks);
   if(i>=retMSP) {  // no exact match\r
     if(deferred) { // but maybe non-sensical deferral\r
       int flags = p[board[f]].promoFlag;\r
+printf("# deferral of %d\n", deferred);\r
       i = deferred; // in any case we take that move\r
       if(!(flags & promoBoard[t] & (CANT_DEFER | LAST_RANK))) { // but change it into a deferral if that is allowed\r
        moveStack[i] &= ~PROMOTE;\r
@@ -1629,7 +1646,7 @@ SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int ti
   tlim2 = 1.9*targetTime;\r
   nodes = 0;\r
 MapFromScratch(attacks);\r
-  score = Search(-INF-1, INF+1, 0, 20, sup1, sup2);\r
+  score = Search(-INF-1, INF+1, rootEval, 20, sup1, sup2);\r
   *move = retMove;\r
   *ponderMove = INVALID;\r
   return score;\r