Fix time forfeits at classical TC
[gnushogi.git] / gnushogi / tcontrl.c
index 451ed0d..fda4534 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
  *
@@ -162,17 +163,21 @@ void SetResponseTime(short side)
             {
                 short rtf = in_opening_stage ? 8 : 2;
                 short tcq = in_opening_stage ? 2 : 4;
+                int moves = TimeControl.moves[side];
 
-                TimeControl.clock[side] += TCadd;
+                if(!xboard) /* no pre-add of increment in XBoard mode */
+                    TimeControl.clock[side] += TCadd;
+                if(TCflag == 2 && TCadd == 0) /* sudden death */
+                    moves = (moves < 30 ? 30 : moves);
                 ResponseTime = (TimeControl.clock[side])
-                    / (((TimeControl.moves[side]) * rtf) + 1);
+                    / (moves * rtf + 1);
                 TCleft = (long)ResponseTime / tcq;
                 ResponseTime += TCadd / 2;
             }
 
             if (TimeControl.moves[side] < 5)
             {
-                TCcount = MAXTCCOUNTX - 10;
+                TCcount = MAXTCCOUNTX - 6 - TimeControl.moves[side];
 
                 if (TCcount < 0)
                     TCcount = 0;
@@ -251,10 +256,15 @@ void SetResponseTime(short side)
         else
         {
             /* calculate avg time per move remaining */
-            TimeControl.clock[side] += TCadd;
+            int moves = TimeControl.moves[side];
+
+            if(!xboard) /* no pre-add of increment in XBoard mode */
+                TimeControl.clock[side] += TCadd;
 
+            if(TCflag == 2 && TCadd == 0) /* sudden death */
+                moves = (moves < 30 ? 30 : moves);
             ResponseTime = (TimeControl.clock[side])
-                / (((TimeControl.moves[side]) * 2) + 1);
+                / (moves * 2 + 1);
             TCleft = (int) ResponseTime / 3;
             ResponseTime += TCadd / 2;
 
@@ -294,7 +304,7 @@ void SetResponseTime(short side)
         TCcount = MAXTCCOUNTX;
     }
 #endif
-
+printf("# %2d. moves=%d,%d time=%d,%d ResponseTime=%d+%d\n",GameCnt,TimeControl.moves[computer],TimeControl.moves[opponent],TimeControl.clock[computer],TimeControl.clock[opponent],ResponseTime,TCleft);
     assert(TCcount <= MAXTCCOUNTX);
 }
 
@@ -345,5 +355,116 @@ CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
 #endif
 
     if (flag.timeout)
-        ShowMessage("timeout");
+        dsp->ShowMessage("timeout");
+}
+
+
+/*
+ * Determine the time that has passed since the search was started. If the
+ * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
+ * to true which will terminate the search.
+ * iop = COMPUTE_MODE calculate et, bump ETnodes
+ * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
+ *     set reference time
+ */
+void
+ElapsedTime(ElapsedTime_mode iop)
+{
+    long current_time;
+#ifdef HAVE_GETTIMEOFDAY
+    struct timeval tv;
+#endif
+
+    dsp->PollForInput();
+
+#ifdef HAVE_GETTIMEOFDAY
+    gettimeofday(&tv, NULL);
+    current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
+#else
+    et = ((current_time = time((long *) 0)) - time0) * 100;
+#endif
+
+#ifdef INTERRUPT_TEST
+    if (iop == INIT_INTERRUPT_MODE)
+    {
+        itime0 = current_time;
+    }
+    else if (iop == COMPUTE_INTERRUPT_MODE)
+    {
+        it = current_time - itime0;
+    }
+    else
+#endif
+    {
+#ifdef HAVE_GETTIMEOFDAY
+        et = current_time - time0;
+#endif
+        ETnodes = NodeCnt + znodes;
+
+        if (et < 0)
+        {
+#ifdef INTERRUPT_TEST
+            printf("elapsed time %ld not positive\n", et);
+#endif
+            et = 0;
+        }
+
+        if (iop == COMPUTE_AND_INIT_MODE)
+        {
+            if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
+                flag.timeout = true;
+
+            time0 = current_time;
+        }
+
+#ifdef QUIETBACKGROUND
+        if (!background)
+#endif
+            dsp->UpdateClocks();
+    }
+}
+
+
+void
+SetTimeControl(void)
+{
+    if (TCflag)
+    {
+        TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
+        TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
+        TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
+    }
+    else
+    {
+        TimeControl.moves[black] = TimeControl.moves[white] = 0;
+        TimeControl.clock[black] = TimeControl.clock[white] = 0;
+    }
+
+    flag.onemove = (TCmoves == 1);
+    et = 0;
+    ElapsedTime(COMPUTE_AND_INIT_MODE);
+}
+
+void
+RenewTimeControl(int side, int TCadd)
+{
+    if (flag.gamein || TCadd)
+    {
+        TimeCalc();
+    }
+    else if (TimeControl.moves[side] == 0)
+    {
+        if (XC)
+        {
+            if (XCmore < XC)
+            {
+                TCmoves   = XCmoves[XCmore];
+                TCminutes = XCminutes[XCmore];
+                TCseconds = XCseconds[XCmore];
+                XCmore++;
+            }
+        }
+
+        SetTimeControl();
+    }
 }