File: clock.c

package info (click to toggle)
gnugo 3.8-13
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,656 kB
  • sloc: ansic: 56,447; perl: 3,771; lisp: 2,804; sh: 722; python: 682; makefile: 653; awk: 113; sed: 22
file content (414 lines) | stat: -rw-r--r-- 11,781 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see       *
 * http://www.gnu.org/software/gnugo/ for more information.          *
 *                                                                   *
 * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,   *
 * 2008 and 2009 by the Free Software Foundation.                    *
 *                                                                   *
 * This program is free software; you can redistribute it and/or     *
 * modify it under the terms of the GNU General Public License as    *
 * published by the Free Software Foundation - version 3 or          *
 * (at your option) any later version.                               *
 *                                                                   *
 * This program is distributed in the hope that it will be useful,   *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     *
 * GNU General Public License in file COPYING for more details.      *
 *                                                                   *
 * You should have received a copy of the GNU General Public         *
 * License along with this program; if not, write to the Free        *
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,       *
 * Boston, MA 02111, USA.                                            *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* ============================================================= *\
 *                        Time handling                          *
 *                          for GNU Go                           *
 *                         __       __                           *
 *                        <  >     <  >                          *
 *                      +--++-------++--+                        *
 *                      |  .'11 12 1'.  |                        *
 *                      |  :10 \    2:  |                        *
 *                      |  :9   @-> 3:  |                        *
 *                      |  :8       4;  |                        *
 *                      |  '..7 6 5..'  |                        *
 *                      |_______________|                        *
 *                                                               *
\* ============================================================= */

#include "clock.h"
#include "gg_utils.h"
#include "board.h"

/* Level data */
static int level             = DEFAULT_LEVEL; /* current level */
static int level_offset      = 0;
static int min_level         = 0;
static int max_level         = gg_max(DEFAULT_LEVEL, 10);


/*************************/
/* Datas and other stuff */
/*************************/

/* clock parameters */
static int main_time = -1;
static int byoyomi_time = -1;
static int byoyomi_stones = -1; /* <= 0 if no byo-yomi */

/* Keep track of the remaining time left.
 * If stones_left is zero, .._time_left is the remaining main time.
 * Otherwise, the remaining time for this byoyomi period.
 */
struct remaining_time_data {
  double time_left;
  double time_for_last_move;
  int stones;
  int movenum;
  int in_byoyomi;
};

struct timer_data {
  struct remaining_time_data official;
  struct remaining_time_data estimated;
  int time_out;
};

static struct timer_data black_time_data;
static struct timer_data white_time_data;


/* Echo a time value in STANDARD format */
static void
timeval_print(FILE *outfile, double tv)
{
  int min;
  double sec;

  min = (int) tv / 60;
  sec = tv - min*60;

  fprintf(outfile, "%3dmin %.2fsec ", min, sec);
}


/* Print the clock status for one side. */
void
clock_print(int color)
{
  struct timer_data *const td
    = (color == BLACK) ? &black_time_data : &white_time_data;

  fprintf(stderr, "clock: "); 
  fprintf(stderr, "%s ", color_to_string(color));

  if (td->time_out)
    fprintf(stderr, "TIME OUT! ");
  else {
    if (td->estimated.in_byoyomi) {
      fprintf(stderr, "byoyomi");
      timeval_print(stderr, td->estimated.time_left);
      fprintf(stderr, "for %d stones.", td->estimated.stones);
    }
    else
      timeval_print(stderr, td->estimated.time_left);

  }
  fprintf(stderr, "\n");
}


/******************************/
/*  Initialization functions  */
/******************************/

/*
 * Initialize the time settings for this game.
 * -1 means "do not modify this value".
 *
 *  byo_time > 0 and byo_stones == 0 means no time settings.
 */
void
clock_settings(int time, int byo_time, int byo_stones)
{
  if (time >= 0)
    main_time = time;
  if (byo_time >= 0)
    byoyomi_time = byo_time;
  if (byo_stones >= 0)
    byoyomi_stones = byo_stones;
  init_timers();
}

/* Get time settings. Returns 1 if any time settings have been made,
 * 0 otherwise.
 */
int
have_time_settings(void)
{
  /* According to the semantics of the GTP command 'time_settings', the
   * following signifies no time limits.
   */
  if (byoyomi_time > 0 && byoyomi_stones == 0)
    return 0;
  else
    return (main_time >= 0 || byoyomi_time >= 0);
}


/* Initialize all timers. */
void
init_timers()
{
  white_time_data.official.time_left = main_time;
  white_time_data.official.time_for_last_move = -1.0;
  white_time_data.official.stones = 0;
  white_time_data.official.movenum = 0;
  white_time_data.official.in_byoyomi = 0;
  white_time_data.estimated = white_time_data.official;
  white_time_data.time_out = 0;
  black_time_data = white_time_data;

  level_offset = 0;
}


/*****************************/
/*  Clock access functions.  */
/*****************************/


void
update_time_left(int color, int time_left, int stones)
{
  struct timer_data *const td
    = ((color == BLACK) ? &black_time_data : &white_time_data);
  int time_used = td->official.time_left - time_left;

  if (time_left > 0)
    td->time_out = 0;
  else 
    td->time_out = 1;

  /* Did our estimate for time usage go wrong? */
  if (time_used > 0
      && gg_abs(time_used - td->estimated.time_for_last_move) >= 1.0)
    td->estimated.time_for_last_move = time_used;
  td->estimated.stones = stones;
  td->estimated.movenum = movenum;
  /* Did our clock go wrong? */
  if (gg_abs(td->estimated.time_left - time_left) >= 1.0)
    td->estimated.time_left = time_left;
  if (stones > 0)
    td->estimated.in_byoyomi = 1;
  else
    td->estimated.in_byoyomi = 0;

  td->official.stones = stones;
  td->official.movenum = movenum;
  td->official.time_for_last_move = td->official.time_for_last_move - time_left;
  td->official.time_left = time_left;
  td->official.in_byoyomi = td->estimated.in_byoyomi;
}

/*
 * Update the estimated timer after a move has been made.
 */
void
clock_push_button(int color)
{
  static double last_time = -1.0;
  static int last_movenum = -1;
  struct timer_data *const td
    = (color == BLACK) ? &black_time_data : &white_time_data;
  double now = gg_gettimeofday();

  if (!have_time_settings())
    return;

  if (last_movenum >= 0
      && movenum == last_movenum + 1
      && movenum > td->estimated.movenum) {
    double time_used = now - last_time;
    td->estimated.time_left -= time_used;
    td->estimated.movenum = movenum;
    td->estimated.time_for_last_move = time_used;
    if (td->estimated.time_left < 0) {
      if (td->estimated.in_byoyomi || byoyomi_stones == 0) {
	DEBUG(DEBUG_TIME, "%s ran out of time.\n", color_to_string(color));
	if (debug & DEBUG_TIME)
	  clock_print(color);
	td->time_out = 1;
      }
      else {
	/* Entering byoyomi. */
	gg_assert(!(td->estimated.in_byoyomi));
	td->estimated.in_byoyomi = 1;
	td->estimated.stones = byoyomi_stones - 1;
	td->estimated.time_left += byoyomi_time;
	if (td->estimated.time_left < 0)
	  td->time_out = 1;
      }
    }
    else if (td->estimated.stones > 0) {
      gg_assert(td->estimated.in_byoyomi);
      td->estimated.stones = td->estimated.stones - 1;
      if (td->estimated.stones == 0) {
	td->estimated.time_left = byoyomi_time;
	td->estimated.stones = byoyomi_stones;
      }
    }
  }

  last_movenum = movenum;
  last_time = now;

  /* Update main timer. */
  if (debug & DEBUG_TIME)
    clock_print(color);
}


/**********************/
/*  Autolevel system  */
/**********************/


/* Analyze the two most recent time reports and determine the time
 * spent on the last moves, the (effective) number of stones left and
 * the (effective) remaining time.
 */
static int
analyze_time_data(int color, double *time_for_last_move, double *time_left,
		  int *stones_left)
{
  struct remaining_time_data *const timer
    = (color == BLACK) ? &black_time_data.estimated
	               : &white_time_data.estimated;

  /* Do we have any time limits. */
  if (!have_time_settings())
    return 0;

  /* If we don't have consistent time information yet, just return. */
  if (timer->time_for_last_move < 0.0)
    return 0;

  *time_for_last_move = timer->time_for_last_move;

  if (timer->stones == 0) {
    /* Main time running. */
    *time_left = timer->time_left + byoyomi_time;
    if (byoyomi_time > 0)
      *stones_left = byoyomi_stones;
    else {
      /* Absolute time. Here we aim to be able to play at least X more
       * moves or a total of Y moves. We choose Y as a third of the
       * number of vertices and X as 40% of Y. For 19x19 this means
       * that we aim to play at least a total of 120 moves
       * (corresponding to a 240 move game) or another 24 moves.
       *
       * FIXME: Maybe we should use the game_status of
       * influence_evaluate_position() here to guess how many moves
       * are remaining.
       */
      int nominal_moves = board_size * board_size / 3;
      *stones_left = gg_max(nominal_moves - movenum / 2,
			    2 * nominal_moves / 5);
    }
  }
  else {
    *time_left = timer->time_left;
    *stones_left = timer->stones;
  }
  return 1;
}


/* Adjust the level offset given information of current playing speed
 * and remaining time and stones.
 */
void
adjust_level_offset(int color)
{
  double time_for_last_move;
  double time_left;
  int stones_left;

  if (!analyze_time_data(color, &time_for_last_move, &time_left, &stones_left))
    return;


  /* These rules are both crude and ad hoc.
   *
   * FIXME: Use rules with at least some theoretical basis.
   */
  if (time_left < time_for_last_move * (stones_left + 3))
    level_offset--;
  if (time_left < time_for_last_move * stones_left)
    level_offset--;
  if (3 * time_left < 2 * time_for_last_move * stones_left)
    level_offset--;
  if (2 * time_left < time_for_last_move * stones_left)
    level_offset--;
  if (3 * time_left < time_for_last_move * stones_left)
    level_offset--;

  if (time_for_last_move == 0)
    time_for_last_move = 1;
  if (time_left > time_for_last_move * (stones_left + 6))
    level_offset++;
  if (time_left > 2 * time_for_last_move * (stones_left + 6))
    level_offset++;

  if (level + level_offset < min_level)
    level_offset = min_level - level;

  if (level + level_offset > max_level)
    level_offset = max_level - level;

  DEBUG(DEBUG_TIME, "New level %d (%d %C %f %f %d)\n", level + level_offset,
	movenum / 2, color, time_for_last_move, time_left, stones_left);
}


/********************************/
/* Interface to level settings. */
/********************************/

int
get_level()
{
  return level + level_offset;
}

void
set_level(int new_level)
{
  level = new_level;
  level_offset = 0;
  if (level > max_level)
    max_level = level;
  if (level < min_level)
    min_level = level;
}

void
set_max_level(int new_max)
{
  max_level = new_max;
}

void
set_min_level(int new_min)
{
  min_level = new_min;
}


/*
 * Local Variables:
 * tab-width: 8
 * c-basic-offset: 2
 * End:
 */