Improve calculation of thinking time in sudden-death games
[gnushogi.git] / gnushogi / tcontrl.c
index a9ec0d3..afdb32c 100644 (file)
@@ -163,10 +163,14 @@ 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;
             }
@@ -252,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;
 
@@ -295,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);
 }
 
@@ -346,7 +355,7 @@ CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
 #endif
 
     if (flag.timeout)
-        ShowMessage("timeout");
+        dsp->ShowMessage("timeout");
 }
 
 
@@ -366,7 +375,7 @@ ElapsedTime(ElapsedTime_mode iop)
     struct timeval tv;
 #endif
 
-    PollForInput();
+    dsp->PollForInput();
 
 #ifdef HAVE_GETTIMEOFDAY
     gettimeofday(&tv, NULL);
@@ -411,6 +420,51 @@ ElapsedTime(ElapsedTime_mode iop)
 #ifdef QUIETBACKGROUND
         if (!background)
 #endif
-            UpdateClocks();
+            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();
     }
 }