src/chess/rules.c

changeset 36
ebe0c961e9a6
parent 33
866025982aa9
child 40
47162a7621da
--- a/src/chess/rules.c	Wed Apr 09 18:11:51 2014 +0200
+++ b/src/chess/rules.c	Thu Apr 10 11:44:55 2014 +0200
@@ -31,6 +31,7 @@
 #include "chess.h"
 #include <string.h>
 #include <stdlib.h>
+#include <sys/time.h>
 
 void gamestate_cleanup(GameState *gamestate) {
     MoveList *elem;
@@ -47,26 +48,26 @@
     elem->next = NULL;
     elem->move = *move;
     
-    clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp));
+    gettimeofday(&(elem->move.timestamp), NULL);
     
     if (gamestate->lastmove) {
-        struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp);
+        struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
         time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
-        long int nanos;
-        if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) {
-            nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec);
+        suseconds_t micros;
+        if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
+            micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
             sec--;
         } else {
-            nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec;
+            micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
         }
         
         elem->move.movetime.tv_sec = sec;
-        elem->move.movetime.tv_nsec = nanos;
+        elem->move.movetime.tv_usec = micros;
         
         gamestate->lastmove->next = elem;
         gamestate->lastmove = elem;
     } else {
-        elem->move.movetime.tv_nsec = 0;
+        elem->move.movetime.tv_usec = 0;
         elem->move.movetime.tv_sec = 0;
         gamestate->movelist = gamestate->lastmove = elem;
     }
@@ -550,7 +551,7 @@
     
     if (gamestate->movelist) {
         uint16_t time = gameinfo->time;
-        long int nanos = 0;
+        suseconds_t micros = 0;
         
         MoveList *movelist = color == WHITE ?
             gamestate->movelist : gamestate->movelist->next;
@@ -558,13 +559,13 @@
         while (movelist) {
             time += gameinfo->addtime;
             
-            struct timespec *movetime = &(movelist->move.movetime);
+            struct timeval *movetime = &(movelist->move.movetime);
             if (movetime->tv_sec >= time) {
                 return 0;
             }
             
             time -= movetime->tv_sec;
-            nanos += movetime->tv_nsec;
+            micros += movetime->tv_usec;
             
             movelist = movelist->next ? movelist->next->next : NULL;
         }
@@ -572,10 +573,10 @@
         time_t sec;
         movelist = gamestate->lastmove;
         if ((movelist->move.piece & COLOR_MASK) != color) {
-            struct timespec *lastmovetstamp = &(movelist->move.timestamp);
-            struct timespec currenttstamp;
-            clock_gettime(CLOCK_REALTIME, &currenttstamp);
-            nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec;
+            struct timeval *lastmovetstamp = &(movelist->move.timestamp);
+            struct timeval currenttstamp;
+            gettimeofday(&currenttstamp, NULL);
+            micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
             sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
             if (sec >= time) {
                 return 0;
@@ -584,7 +585,7 @@
             time -= sec;
         }
         
-        sec = nanos / 1e9L;
+        sec = micros / 1e6L;
         
         if (sec >= time) {
             return 0;

mercurial