Add time increment after move in XBoard mode
[gnushogi.git] / gnushogi / search.c
index 1491057..f950a6d 100644 (file)
@@ -4,6 +4,7 @@
  * ----------------------------------------------------------------------
  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
+ * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
  *
  * GNU SHOGI is based on GNU CHESS
  *
  */
 
 #include "gnushogi.h"
-#include <poll.h>
-#include <unistd.h>
-
-#if !defined OLDTIME && defined HAVE_GETTIMEOFDAY
-double pow(double x, double y);
-#endif
 
 short background = 0;
 static short DepthBeyond;
@@ -49,6 +44,7 @@ short PVari;        /* Is this the PV */
 #endif
 
 short zwndw;
+short movesLeft, currentMove;
 
 
 
@@ -186,7 +182,7 @@ SelectMove(short side, SelectMove_mode iop)
 #ifdef QUIETBACKGROUND
     if (!background)
 #endif /* QUIETBACKGROUND */
-        ShowResponseTime();
+        dsp->ShowResponseTime();
 
     ExtraTime = 0;
 
@@ -195,12 +191,12 @@ SelectMove(short side, SelectMove_mode iop)
 #ifdef QUIETBACKGROUND
     if (!background)
 #endif /* QUIETBACKGROUND */
-        ShowSidetoMove();
+        dsp->ShowSidetoMove();
 
 #ifdef QUIETBACKGROUND
     if (!background)
 #endif /* QUIETBACKGROUND */
-        SearchStartStuff(side);
+        dsp->SearchStartStuff(side);
 
 #ifdef HISTORY
     array_zero(history, sizeof_history);
@@ -264,7 +260,7 @@ SelectMove(short side, SelectMove_mode iop)
 
     if (flag.regularstart && Book)
     {
-        flag.timeout = bookflag = OpeningBook(&hint, side);
+        flag.timeout = bookflag = OpeningBook(&hint);
 
         if (TCflag)
             ResponseTime += ResponseTime;
@@ -310,7 +306,7 @@ SelectMove(short side, SelectMove_mode iop)
 # ifdef QUIETBACKGROUND
         if (!background)
 #endif /* QUIETBACKGROUND */
-            ShowDepth(' ');
+            dsp->ShowDepth(' ');
 
         /* search at this level returns score of PV */
         score = search(side, 1, Sdepth, alpha, beta, PrVar, &rpt);
@@ -326,7 +322,7 @@ SelectMove(short side, SelectMove_mode iop)
 #ifdef QUIETBACKGROUND
             if (!background)
 #endif /* QUIETBACKGROUND */
-                ShowDepth('-');
+                dsp->ShowDepth('-');
 
             if (TCflag && TCcount < MAXTCCOUNTR)
             {
@@ -348,7 +344,7 @@ SelectMove(short side, SelectMove_mode iop)
 #ifdef QUIETBACKGROUND
             if (!background)
 #endif /* QUIETBACKGROUND */
-                ShowDepth('+');
+                dsp->ShowDepth('+');
 
             score = search(side, 1, Sdepth, -(SCORE_LIMIT + 999),
                            (SCORE_LIMIT + 999), PrVar, &rpt);
@@ -401,19 +397,19 @@ SelectMove(short side, SelectMove_mode iop)
 #ifdef QUIETBACKGROUND
         if (!background)
 #endif /* QUIETBACKGROUND */
-            ShowResults(score, PrVar, '.');
+            dsp->ShowResults(score, PrVar, '.');
     }
 
     /********************** end of main loop ***************************/
 
     /* background mode */
-    if (iop == BACKGROUND_MODE)
+    if (background) /* originally: if (iop == BACKGROUND_MODE) */
         return;
 
     if (rpt >= 3)
     {
         root->flags |= draw;
-        DRAW = CP[101];     /* Repetition */
+        DRAW = DRAW_REPETITION;
     }
     else
     {
@@ -425,7 +421,7 @@ SelectMove(short side, SelectMove_mode iop)
         if (GameCnt == MAXMOVES)
         {
             root->flags |= draw;
-            DRAW = CP[80];      /* Max Moves */
+            DRAW = DRAW_MAXMOVES;
         }
     }
 
@@ -455,10 +451,13 @@ SelectMove(short side, SelectMove_mode iop)
 
     /* If Time Control get the elapsed time */
     if (TCflag)
+    {
         ElapsedTime(COMPUTE_AND_INIT_MODE);
-
+        if(xboard) /* In XBoard increment is added after move */
+            TimeControl.clock[side] += TCadd;
+    }
     /* update time control info */
-    OutputMove();
+    dsp->OutputMove();
 
     /* if mate set flag */
     if ((score == -(SCORE_LIMIT + 999) || score == (SCORE_LIMIT + 998)))
@@ -532,8 +531,6 @@ search(short side,
     short best = -(SCORE_LIMIT + 3000);
     short bestwidth = 0;
     short mustcut;
-    static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
-                                                      /* .events = */ POLLIN } };
 
 #ifdef NULLMOVE
     short PVsave;
@@ -551,19 +548,6 @@ search(short side,
         {
             ElapsedTime(COMPUTE_MODE);
 
-            if(background) {
-                int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
-                if (cnt < 0) {
-                    perror("polling standard input");
-                    ExitShogi();
-                }
-                if (cnt) { /* if anything to read, or error occured */
-                    if (!flag.timeout)
-                        flag.back = true; /* previous: flag.timeout = true; */
-                    flag.bothsides = false;
-                }
-            }
-
             if (flag.back)
             {
                 flag.back = false;
@@ -601,7 +585,7 @@ search(short side,
 #ifdef QUIETBACKGROUND
             if (!background)
 #endif
-                ShowResponseTime();
+                dsp->ShowResponseTime();
         }
         else if (!TCflag && flag.musttimeout && Sdepth > MINDEPTH)
         {
@@ -731,7 +715,7 @@ search(short side,
                  && (ProbeFTable(side, depth, ply, &alpha, &beta, &score)
                      == true))
         {
-            PutInTTable(side, score, depth, ply, alpha, beta, PV);
+            PutInTTable(side, score, depth, ply, beta, PV);
             bstline[ply] = PV;
             bstline[ply + 1] = 0;
 
@@ -882,7 +866,6 @@ search(short side,
         nxtline[ply + 1] = 0;
 
         /* if at top level */
-#if !defined NOPOST
         if (ply == 1)
         {
 /* at the top update search status */
@@ -891,10 +874,11 @@ search(short side,
 #ifdef QUIETBACKGROUND
                 if (!background)
 #endif /* QUIETBACKGROUND */
-                    ShowCurrentMove(pnt, node->f, node->t);
+                    dsp->ShowCurrentMove(pnt, node->f, node->t);
             }
+            movesLeft = TrPnt[2] - pnt; /* to report with XBoard periodic updates */
+            currentMove = node->f << 8 | node->t;
         }
-#endif
 
         if (!(node->flags & exact))
         {
@@ -932,7 +916,7 @@ search(short side,
                      && !ChkFlag[ply])))
             {
                 node->flags |= (draw | exact);
-                DRAW = CP[58];  /* Draw */
+                DRAW = DRAW_JUSTDRAW;
                 node->score = ((side == computer) ? contempt : -contempt);
             }
 
@@ -1005,15 +989,15 @@ search(short side,
                     {
                         if (best > beta)
                         {
-                            ShowResults(best, bstline, '+');
+                            dsp->ShowResults(best, bstline, '+');
                         }
                         else if (best < alpha)
                         {
-                            ShowResults(best, bstline, '-');
+                            dsp->ShowResults(best, bstline, '-');
                         }
                         else
                         {
-                            ShowResults (best, bstline, '&');
+                            dsp->ShowResults(best, bstline, '&');
                         }
                     }
 #ifdef QUIETBACKGROUND
@@ -1046,13 +1030,13 @@ search(short side,
     {
 #  ifdef HASHFILE /* MCV: warning: this confuses the formatter. */
         if (use_ttable
-            && PutInTTable(side, best, depth, ply, alpha, beta, mv)
+            && PutInTTable(side, best, depth, ply, beta, mv)
             && hashfile
             && (depth > HashDepth)
             && (GameCnt < HashMoveLimit))
 #  else
         if (use_ttable
-            && PutInTTable(side, best, depth, ply, alpha, beta, mv))
+            && PutInTTable(side, best, depth, ply, beta, mv))
 #  endif
         {
             PutInFTable(side, best, depth, ply,
@@ -1137,8 +1121,8 @@ UpdatePieceList(short side, short sq, UpdatePieceList_mode iop)
 
 /* Make or Unmake drop move. */
 
-void
-drop(short side, short piece, short f, short t, short iop)
+static void
+drop(short side, short piece, short t, short iop)
 {
     if (iop == 1)
     {
@@ -1306,7 +1290,7 @@ MakeMove(short side,
         *tempst = svalue[t];
 #endif
 
-        (void)drop(side, g->fpiece, f, t, 1);
+        (void)drop(side, g->fpiece, t, 1);
     }
     else
     {
@@ -1434,7 +1418,7 @@ UnmakeMove(short side,
 
     if (node->flags & dropmask)
     {
-        (void)drop(side, (node->flags & pmask), f, t, 2);
+        (void)drop(side, (node->flags & pmask), t, 2);
 
 #if !defined SAVE_SVALUE
         svalue[t] = *tempst;