Implement analysis mode
authorH.G.Muller <hgm@hgm-xboard.(none)>
Wed, 11 Apr 2018 08:53:43 +0000 (10:53 +0200)
committerH.G.Muller <hgm@hgm-xboard.(none)>
Thu, 12 Apr 2018 07:14:23 +0000 (09:14 +0200)
Functions are added to test for input (platform dependent!), and TimeIsUp()
now calls these before reading the clock. Any input command that cannot be
processed during search then forces an abort like the time is up; other
commands are processed. Without input we consider the time, except when
in analyze mode, where there by definition is infinite time.
  Before waiting for a command in the main loop, a search is started when
we are in analyze mode.

dropper.c

index 3236717..dab8dda 100644 (file)
--- a/dropper.c
+++ b/dropper.c
 #ifdef WIN32 
 #    include <windows.h>
 #    define CPUtime 1000.*clock
+     int Input()
+     {  // checks for waiting input in pipe
+       static int init; static HANDLE inp; DWORD cnt;
+       if(!init) inp = GetStdHandle(STD_INPUT_HANDLE);
+       if(!PeekNamedPipe(inp, NULL, 0, NULL, &cnt, NULL) &&
+          !GetNumberOfConsoleInputEvents(inp, &cnt)) return 1;
+       return cnt;
+    }
 #else
 #    include <sys/time.h>
 #    include <sys/times.h>
+#    include <sys/ioctl.h>
 #    include <unistd.h>
      int GetTickCount() // with thanks to Tord
      { struct timeval t;
        gettimeofday(&t, NULL);
        return t.tv_sec*1000 + t.tv_usec/1000;
      }
+     int Input()
+     {
+       int cnt;
+       if(ioctl(0, FIONREAD, &cnt)) return 1;
+       return cnt;
+     }
 #endif
 
 #define DEBUG 0
@@ -1574,6 +1589,7 @@ printf("%d:%d:%d %2d. %08x %c%d%c%d %6d %6d %6d\n",ply,depth,iterDepth,curMove,m
 
 int gameMove[MAXMOVES];  // holds the game history
 int stm = WHITE;
+int engineSide=NONE;     // side played by engine
 
 // Some routines your engine should have to do the various essential things
 void PonderUntilInput(int stm);         // Search current position for stm, deepening forever until there is input.
@@ -1581,7 +1597,10 @@ void PonderUntilInput(int stm);         // Search current position for stm, deep
 int
 TimeIsUp (int mode)
 { // determine if we should stop, depending on time already used, TC mode, time left on clock and from where it is called ('mode')
-  int t = ReadClock(0), targetTime, panicTime;
+  int t, targetTime, panicTime;
+  if(Input() && DoCommand(1)) return 1; // abort if command waiting that cannot be handled during search
+  if(engineSide == ANALYZE) return 0;    // no timing on analyze or ponder
+  t = ReadClock(0);
   if(timePerMove >= 0) {                               // fixed time per move
     targetTime = panicTime = 10*timeLeft - 30;
   } else if(mps) {                                     // classical TC
@@ -1795,7 +1814,6 @@ void PrintResult(int stm, int score)
   else printf("0-1\n");
 }
 
-int engineSide=NONE;         // side played by engine
 int ponderMove;
 char inBuf[800];
 
@@ -1838,7 +1856,7 @@ printf("# command: %s\n", inBuf);
     }
 
     if(searching) {
-      if(!strcmp(command, "usermove")) { return 1; } // TODO during search we just test for ponder hit
+//      if(!strcmp(command, "usermove")) { return 1; } // TODO during search we just test for ponder hit
       *inBuf = *command;                             // backlog command (by repairing inBuf)
       return 1;                                      // and request search abort
     }
@@ -1931,11 +1949,14 @@ main ()
     }
 
     fflush(stdout); // make sure everything is printed before we do something that might take time
-#if 0
+
     // now it is not our turn (anymore)
     if(engineSide == ANALYZE) {       // in analysis, we always ponder the position
-        PonderUntilInput(stm);
-    } else
+        nodeCount = forceMove = undoInfo.move = abortFlag = 0; ReadClock(1);
+        Search(stm^COLOR, -INF, INF, &undoInfo, maxDepth, 0, maxDepth);
+    }
+#if 0
+ else
     if(engineSide != NONE && ponder == ON && moveNr != 0) { // ponder while waiting for input
       if(ponderMove == INVALID) {     // if we have no move to ponder on, ponder the position
         PonderUntilInput(stm);