15 time_parse(struct time_info
*ti
, char *s
)
18 case '_': ti
->period
= TT_TOTAL
; s
++; break;
19 default: ti
->period
= TT_MOVE
; break;
24 ti
->len
.games
= atoi(++s
);
29 ti
->dim
= TD_WALLTIME
;
30 ti
->len
.t
.recommended_time
= atof(s
);
36 /* Set correct time information before making a move, and
37 * always make it time per move for the engine. */
39 time_prepare_move(struct time_info
*ti
, struct board
*board
)
43 if (ti
->period
== TT_TOTAL
) {
44 moves_left
= board_estimated_moves_left(board
);
45 assert(moves_left
> 0);
46 if (ti
->dim
== TD_GAMES
) {
48 ti
->len
.games
/= moves_left
;
51 if (ti
->period
== TT_NULL
|| ti
->dim
!= TD_WALLTIME
)
54 double now
= time_now();
55 if (!ti
->len
.t
.timer_start
) {
56 ti
->len
.t
.timer_start
= now
; // we're playing the first game move
58 if (ti
->period
== TT_TOTAL
) {
60 ti
->len
.t
.recommended_time
/= moves_left
;
64 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
65 * unless we are making the first move of the game. */
67 time_start_timer(struct time_info
*ti
)
69 if (ti
->period
!= TT_NULL
&& ti
->dim
== TD_WALLTIME
)
70 ti
->len
.t
.timer_start
= time_now();
73 /* Returns the current time. */
78 clock_gettime(CLOCK_REALTIME
, &now
);
79 return now
.tv_sec
+ now
.tv_nsec
/1000000000.0;
82 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
84 time_sleep(double interval
)
88 ts
.tv_nsec
= (int)(modf(interval
, &sec
)*1000000000.0);
90 nanosleep(&ts
, NULL
); /* ignore error if interval was < 0 */