src/chess/rules.c

changeset 36
ebe0c961e9a6
parent 33
866025982aa9
child 40
47162a7621da
     1.1 --- a/src/chess/rules.c	Wed Apr 09 18:11:51 2014 +0200
     1.2 +++ b/src/chess/rules.c	Thu Apr 10 11:44:55 2014 +0200
     1.3 @@ -31,6 +31,7 @@
     1.4  #include "chess.h"
     1.5  #include <string.h>
     1.6  #include <stdlib.h>
     1.7 +#include <sys/time.h>
     1.8  
     1.9  void gamestate_cleanup(GameState *gamestate) {
    1.10      MoveList *elem;
    1.11 @@ -47,26 +48,26 @@
    1.12      elem->next = NULL;
    1.13      elem->move = *move;
    1.14      
    1.15 -    clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp));
    1.16 +    gettimeofday(&(elem->move.timestamp), NULL);
    1.17      
    1.18      if (gamestate->lastmove) {
    1.19 -        struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp);
    1.20 +        struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
    1.21          time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
    1.22 -        long int nanos;
    1.23 -        if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) {
    1.24 -            nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec);
    1.25 +        suseconds_t micros;
    1.26 +        if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
    1.27 +            micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
    1.28              sec--;
    1.29          } else {
    1.30 -            nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec;
    1.31 +            micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
    1.32          }
    1.33          
    1.34          elem->move.movetime.tv_sec = sec;
    1.35 -        elem->move.movetime.tv_nsec = nanos;
    1.36 +        elem->move.movetime.tv_usec = micros;
    1.37          
    1.38          gamestate->lastmove->next = elem;
    1.39          gamestate->lastmove = elem;
    1.40      } else {
    1.41 -        elem->move.movetime.tv_nsec = 0;
    1.42 +        elem->move.movetime.tv_usec = 0;
    1.43          elem->move.movetime.tv_sec = 0;
    1.44          gamestate->movelist = gamestate->lastmove = elem;
    1.45      }
    1.46 @@ -550,7 +551,7 @@
    1.47      
    1.48      if (gamestate->movelist) {
    1.49          uint16_t time = gameinfo->time;
    1.50 -        long int nanos = 0;
    1.51 +        suseconds_t micros = 0;
    1.52          
    1.53          MoveList *movelist = color == WHITE ?
    1.54              gamestate->movelist : gamestate->movelist->next;
    1.55 @@ -558,13 +559,13 @@
    1.56          while (movelist) {
    1.57              time += gameinfo->addtime;
    1.58              
    1.59 -            struct timespec *movetime = &(movelist->move.movetime);
    1.60 +            struct timeval *movetime = &(movelist->move.movetime);
    1.61              if (movetime->tv_sec >= time) {
    1.62                  return 0;
    1.63              }
    1.64              
    1.65              time -= movetime->tv_sec;
    1.66 -            nanos += movetime->tv_nsec;
    1.67 +            micros += movetime->tv_usec;
    1.68              
    1.69              movelist = movelist->next ? movelist->next->next : NULL;
    1.70          }
    1.71 @@ -572,10 +573,10 @@
    1.72          time_t sec;
    1.73          movelist = gamestate->lastmove;
    1.74          if ((movelist->move.piece & COLOR_MASK) != color) {
    1.75 -            struct timespec *lastmovetstamp = &(movelist->move.timestamp);
    1.76 -            struct timespec currenttstamp;
    1.77 -            clock_gettime(CLOCK_REALTIME, &currenttstamp);
    1.78 -            nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec;
    1.79 +            struct timeval *lastmovetstamp = &(movelist->move.timestamp);
    1.80 +            struct timeval currenttstamp;
    1.81 +            gettimeofday(&currenttstamp, NULL);
    1.82 +            micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
    1.83              sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
    1.84              if (sec >= time) {
    1.85                  return 0;
    1.86 @@ -584,7 +585,7 @@
    1.87              time -= sec;
    1.88          }
    1.89          
    1.90 -        sec = nanos / 1e9L;
    1.91 +        sec = micros / 1e6L;
    1.92          
    1.93          if (sec >= time) {
    1.94              return 0;

mercurial