src/chess/rules.c

changeset 36
ebe0c961e9a6
parent 33
866025982aa9
child 40
47162a7621da
equal deleted inserted replaced
35:6c64b7a073af 36:ebe0c961e9a6
29 29
30 #include "rules.h" 30 #include "rules.h"
31 #include "chess.h" 31 #include "chess.h"
32 #include <string.h> 32 #include <string.h>
33 #include <stdlib.h> 33 #include <stdlib.h>
34 #include <sys/time.h>
34 35
35 void gamestate_cleanup(GameState *gamestate) { 36 void gamestate_cleanup(GameState *gamestate) {
36 MoveList *elem; 37 MoveList *elem;
37 elem = gamestate->movelist; 38 elem = gamestate->movelist;
38 while (elem) { 39 while (elem) {
45 static void addmove(GameState* gamestate, Move *move) { 46 static void addmove(GameState* gamestate, Move *move) {
46 MoveList *elem = malloc(sizeof(MoveList)); 47 MoveList *elem = malloc(sizeof(MoveList));
47 elem->next = NULL; 48 elem->next = NULL;
48 elem->move = *move; 49 elem->move = *move;
49 50
50 clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp)); 51 gettimeofday(&(elem->move.timestamp), NULL);
51 52
52 if (gamestate->lastmove) { 53 if (gamestate->lastmove) {
53 struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp); 54 struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
54 time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec; 55 time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
55 long int nanos; 56 suseconds_t micros;
56 if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) { 57 if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
57 nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec); 58 micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
58 sec--; 59 sec--;
59 } else { 60 } else {
60 nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec; 61 micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
61 } 62 }
62 63
63 elem->move.movetime.tv_sec = sec; 64 elem->move.movetime.tv_sec = sec;
64 elem->move.movetime.tv_nsec = nanos; 65 elem->move.movetime.tv_usec = micros;
65 66
66 gamestate->lastmove->next = elem; 67 gamestate->lastmove->next = elem;
67 gamestate->lastmove = elem; 68 gamestate->lastmove = elem;
68 } else { 69 } else {
69 elem->move.movetime.tv_nsec = 0; 70 elem->move.movetime.tv_usec = 0;
70 elem->move.movetime.tv_sec = 0; 71 elem->move.movetime.tv_sec = 0;
71 gamestate->movelist = gamestate->lastmove = elem; 72 gamestate->movelist = gamestate->lastmove = elem;
72 } 73 }
73 } 74 }
74 75
548 return 0; 549 return 0;
549 } 550 }
550 551
551 if (gamestate->movelist) { 552 if (gamestate->movelist) {
552 uint16_t time = gameinfo->time; 553 uint16_t time = gameinfo->time;
553 long int nanos = 0; 554 suseconds_t micros = 0;
554 555
555 MoveList *movelist = color == WHITE ? 556 MoveList *movelist = color == WHITE ?
556 gamestate->movelist : gamestate->movelist->next; 557 gamestate->movelist : gamestate->movelist->next;
557 558
558 while (movelist) { 559 while (movelist) {
559 time += gameinfo->addtime; 560 time += gameinfo->addtime;
560 561
561 struct timespec *movetime = &(movelist->move.movetime); 562 struct timeval *movetime = &(movelist->move.movetime);
562 if (movetime->tv_sec >= time) { 563 if (movetime->tv_sec >= time) {
563 return 0; 564 return 0;
564 } 565 }
565 566
566 time -= movetime->tv_sec; 567 time -= movetime->tv_sec;
567 nanos += movetime->tv_nsec; 568 micros += movetime->tv_usec;
568 569
569 movelist = movelist->next ? movelist->next->next : NULL; 570 movelist = movelist->next ? movelist->next->next : NULL;
570 } 571 }
571 572
572 time_t sec; 573 time_t sec;
573 movelist = gamestate->lastmove; 574 movelist = gamestate->lastmove;
574 if ((movelist->move.piece & COLOR_MASK) != color) { 575 if ((movelist->move.piece & COLOR_MASK) != color) {
575 struct timespec *lastmovetstamp = &(movelist->move.timestamp); 576 struct timeval *lastmovetstamp = &(movelist->move.timestamp);
576 struct timespec currenttstamp; 577 struct timeval currenttstamp;
577 clock_gettime(CLOCK_REALTIME, &currenttstamp); 578 gettimeofday(&currenttstamp, NULL);
578 nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec; 579 micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
579 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; 580 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
580 if (sec >= time) { 581 if (sec >= time) {
581 return 0; 582 return 0;
582 } 583 }
583 584
584 time -= sec; 585 time -= sec;
585 } 586 }
586 587
587 sec = nanos / 1e9L; 588 sec = micros / 1e6L;
588 589
589 if (sec >= time) { 590 if (sec >= time) {
590 return 0; 591 return 0;
591 } 592 }
592 593

mercurial