t-unit: two_eyes test suite
[pachi.git] / gtp.c
blob88a4f352220b34a77175165f062346ed92791292
1 #define DEBUG
2 #include <assert.h>
3 #include <ctype.h>
4 #include <math.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "board.h"
12 #include "debug.h"
13 #include "engine.h"
14 #include "fbook.h"
15 #include "gtp.h"
16 #include "mq.h"
17 #include "uct/uct.h"
18 #include "version.h"
19 #include "timeinfo.h"
20 #include "gogui.h"
22 #define NO_REPLY (-2)
24 /* Sleep 5 seconds after a game ends to give time to kill the program. */
25 #define GAME_OVER_SLEEP 5
27 void
28 gtp_prefix(char prefix, int id)
30 if (id == NO_REPLY) return;
31 if (id >= 0)
32 printf("%c%d ", prefix, id);
33 else
34 printf("%c ", prefix);
37 void
38 gtp_flush(void)
40 putchar('\n');
41 fflush(stdout);
44 void
45 gtp_output(char prefix, int id, va_list params)
47 if (id == NO_REPLY) return;
48 gtp_prefix(prefix, id);
49 char *s;
50 while ((s = va_arg(params, char *))) {
51 fputs(s, stdout);
53 putchar('\n');
54 gtp_flush();
57 void
58 gtp_reply(int id, ...)
60 va_list params;
61 va_start(params, id);
62 gtp_output('=', id, params);
63 va_end(params);
66 void
67 gtp_error(int id, ...)
69 va_list params;
70 va_start(params, id);
71 gtp_output('?', id, params);
72 va_end(params);
75 static void
76 gtp_final_score(struct board *board, struct engine *engine, char *reply, int len)
78 struct move_queue q = { .moves = 0 };
79 if (engine->dead_group_list)
80 engine->dead_group_list(engine, board, &q);
81 floating_t score = board_official_score(board, &q);
82 if (DEBUGL(1))
83 fprintf(stderr, "counted score %.1f\n", score);
84 if (score == 0)
85 snprintf(reply, len, "0");
86 else if (score > 0)
87 snprintf(reply, len, "W+%.1f", score);
88 else
89 snprintf(reply, len, "B+%.1f", -score);
92 /* List of known gtp commands. The internal command pachi-genmoves is not exported,
93 * it should only be used between master and slaves of the distributed engine. */
94 static char *known_commands_base =
95 "protocol_version\n"
96 "echo\n"
97 "name\n"
98 "version\n"
99 "list_commands\n"
100 "known_command\n"
101 "quit\n"
102 "boardsize\n"
103 "clear_board\n"
104 "kgs-game_over\n"
105 "komi\n"
106 "kgs-rules\n"
107 "play\n"
108 "genmove\n"
109 "kgs-genmove_cleanup\n"
110 "set_free_handicap\n"
111 "place_free_handicap\n"
112 "fixed_handicap\n"
113 "final_score\n"
114 "final_status_list\n"
115 "undo\n"
116 "pachi-evaluate\n"
117 "pachi-result\n"
118 "pachi-gentbook\n"
119 "pachi-dumptbook\n"
120 "pachi-predict\n"
121 "kgs-chat\n"
122 "time_left\n"
123 "time_settings\n"
124 "kgs-time_settings";
126 static char*
127 known_commands(struct engine *engine)
129 static char *str = 0;
130 if (str)
131 return str;
132 if (strcmp(engine->name, "UCT")) /* Not uct ? */
133 return known_commands_base;
134 /* For now only uct supports gogui-analyze_commands */
135 str = malloc(strlen(known_commands_base) + 32);
136 sprintf(str, "%s\ngogui-analyze_commands", known_commands_base);
137 return str;
140 static char *gogui_analyze_commands =
141 "string/ Final Score/final_score\n"
142 "gfx/gfx Best Moves B/gogui-best_moves b\n"
143 "gfx/gfx Best Moves W/gogui-best_moves w\n"
144 "gfx/gfx Winrates B/gogui-winrates b\n"
145 "gfx/gfx Winrates W/gogui-winrates w\n"
146 "gfx/gfx Owner Map/gogui-owner_map\n"
147 "gfx/Live gfx = Best Moves/gogui-live_gfx best_moves\n"
148 "gfx/Live gfx = Best Sequence/gogui-live_gfx best_seq\n"
149 "gfx/Live gfx = Winrates/gogui-live_gfx winrates\n";
152 char gogui_gfx_buf[5000];
153 enum gogui_reporting gogui_live_gfx = 0;
155 static void
156 gogui_set_live_gfx(struct engine *engine, char *arg)
158 if (!strcmp(arg, "best_moves"))
159 gogui_live_gfx = UR_GOGUI_CAN;
160 if (!strcmp(arg, "best_seq"))
161 gogui_live_gfx = UR_GOGUI_SEQ;
162 if (!strcmp(arg, "winrates"))
163 gogui_live_gfx = UR_GOGUI_WR;
164 engine->live_gfx_hook(engine);
167 static char *
168 gogui_best_moves(struct board *b, struct engine *engine, char *arg, bool winrates)
170 enum stone color = str2stone(arg);
171 assert(color != S_NONE);
172 enum gogui_reporting prev = gogui_live_gfx;
173 gogui_set_live_gfx(engine, (winrates ? "winrates" : "best_moves"));
174 gogui_gfx_buf[0] = 0;
175 engine->best_moves(engine, b, color);
176 gogui_live_gfx = prev;
177 return gogui_gfx_buf;
180 /* XXX Completely unsafe if reply buffer is not big enough */
181 static void
182 gogui_owner_map(struct board *b, struct engine *engine, char *reply)
184 char str2[32];
185 reply[0] = 0;
186 if (!engine->owner_map)
187 return;
189 sprintf(reply, "INFLUENCE");
190 foreach_point(b) {
191 if (board_at(b, c) == S_OFFBOARD)
192 continue;
193 float p = engine->owner_map(engine, b, c);
195 // p = -1 for WHITE, 1 for BLACK absolute ownership of point i
196 if (p < -.8)
197 p = -1.0;
198 else if (p < -.5)
199 p = -0.7;
200 else if (p < -.2)
201 p = -0.4;
202 else if (p < 0.2)
203 p = 0.0;
204 else if (p < 0.5)
205 p = 0.4;
206 else if (p < 0.8)
207 p = 0.7;
208 else
209 p = 1.0;
210 sprintf(str2, " %3s %.1lf", coord2sstr(c, b), p);
211 strcat(reply, str2);
212 } foreach_point_end;
214 strcat(reply, "\nTEXT Score Est: ");
215 gtp_final_score(b, engine, str2, sizeof(str2));
216 strcat(reply, str2);
219 /* Return true if cmd is a valid gtp command. */
220 bool
221 gtp_is_valid(struct engine *e, const char *cmd)
223 if (!cmd || !*cmd) return false;
224 const char *s = strcasestr(known_commands(e), cmd);
225 if (!s) return false;
226 if (s != known_commands(e) && s[-1] != '\n') return false;
228 int len = strlen(cmd);
229 return s[len] == '\0' || s[len] == '\n';
232 // For prediction stats
233 static int played_games = 0;
235 static void
236 gtp_predict_move(struct board *board, struct engine *engine, struct time_info *ti,
237 int id, struct move *m)
239 enum stone color = m->color;
241 if (m->coord == pass || m->coord == resign) {
242 int r = board_play(board, m);
243 assert(r >= 0);
244 gtp_reply(id, NULL);
245 return;
248 if (DEBUGL(5))
249 fprintf(stderr, "predict move %d,%d,%d\n", m->color, coord_x(m->coord, board), coord_y(m->coord, board));
251 // Not bothering with timer here for now.
253 coord_t *c = engine->genmove(engine, board, &ti[color], color, 0);
255 // Hack to reset engine state (if it has one) in case engine
256 // guessed wrong (engine expects us to play returned move)
257 if (engine->undo) // hackish way to reset state if engine needs it
258 engine->undo(engine, board);
260 // Play correct expected move
261 if (board_play(board, m) < 0) {
262 fprintf(stderr, "ILLEGAL EXPECTED MOVE: [%s, %s]\n", coord2sstr(m->coord, board), stone2str(m->color));
263 abort();
266 if (DEBUGL(1) && debug_boardprint)
267 engine_board_print(engine, board, stderr);
269 if (*c == m->coord)
270 fprintf(stderr, "Move %3i: Predict: Correctly predicted %s %s\n", board->moves,
271 (color == S_BLACK ? "b" : "w"), coord2sstr(*c, board));
272 else
273 fprintf(stderr, "Move %3i: Predict: Wrong prediction: %s %s != %s\n", board->moves,
274 (color == S_BLACK ? "b" : "w"), coord2sstr(*c, board), coord2sstr(m->coord, board));
276 // Display stats from time to time
278 static int total = 0;
279 static int guessed = 0;
280 guessed += (*c == m->coord);
281 if (++total % 200 == 0)
282 fprintf(stderr, "Predicted: %i/%i moves (%i%%) games: %i\n",
283 guessed, total, guessed * 100 / total, played_games);
286 gtp_reply(id, NULL);
287 coord_done(c);
291 /* XXX: THIS IS TOTALLY INSECURE!!!!
292 * Even basic input checking is missing. */
294 enum parse_code
295 gtp_parse(struct board *board, struct engine *engine, struct time_info *ti, char *buf)
297 #define next_tok(to_) \
298 to_ = next; \
299 next = next + strcspn(next, " \t\r\n"); \
300 if (*next) { \
301 *next = 0; next++; \
302 next += strspn(next, " \t\r\n"); \
305 if (strchr(buf, '#'))
306 *strchr(buf, '#') = 0;
308 char *cmd, *next = buf;
309 next_tok(cmd);
311 int id = -1;
312 if (isdigit(*cmd)) {
313 id = atoi(cmd);
314 next_tok(cmd);
317 if (!*cmd)
318 return P_OK;
320 if (!strcasecmp(cmd, "protocol_version")) {
321 gtp_reply(id, "2", NULL);
322 return P_OK;
324 } else if (!strcasecmp(cmd, "name")) {
325 /* KGS hack */
326 gtp_reply(id, "Pachi ", engine->name, NULL);
327 return P_OK;
329 } else if (!strcasecmp(cmd, "echo")) {
330 gtp_reply(id, next, NULL);
331 return P_OK;
333 } else if (!strcasecmp(cmd, "version")) {
334 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, " Have a nice game!", NULL);
335 return P_OK;
337 } else if (!strcasecmp(cmd, "list_commands")) {
338 gtp_reply(id, known_commands(engine), NULL);
339 return P_OK;
341 } else if (!strcasecmp(cmd, "known_command")) {
342 char *arg;
343 next_tok(arg);
344 if (gtp_is_valid(engine, arg)) {
345 gtp_reply(id, "true", NULL);
346 } else {
347 gtp_reply(id, "false", NULL);
349 return P_OK;
352 if (engine->notify && gtp_is_valid(engine, cmd)) {
353 char *reply;
354 enum parse_code c = engine->notify(engine, board, id, cmd, next, &reply);
355 if (c == P_NOREPLY) {
356 id = NO_REPLY;
357 } else if (c == P_DONE_OK) {
358 gtp_reply(id, reply, NULL);
359 return P_OK;
360 } else if (c == P_DONE_ERROR) {
361 gtp_error(id, reply, NULL);
362 /* This is an internal error for the engine, but
363 * it is still OK from main's point of view. */
364 return P_OK;
365 } else if (c != P_OK) {
366 return c;
370 if (!strcasecmp(cmd, "quit")) {
371 gtp_reply(id, NULL);
372 exit(0);
374 } else if (!strcasecmp(cmd, "boardsize")) {
375 char *arg;
376 next_tok(arg);
377 int size = atoi(arg);
378 if (size < 1 || size > BOARD_MAX_SIZE) {
379 gtp_error(id, "illegal board size", NULL);
380 return P_OK;
382 board_resize(board, size);
383 board_clear(board);
384 gtp_reply(id, NULL);
385 return P_ENGINE_RESET;
387 } else if (!strcasecmp(cmd, "clear_board")) {
388 board_clear(board);
389 played_games++;
390 if (DEBUGL(3) && debug_boardprint)
391 board_print(board, stderr);
392 gtp_reply(id, NULL);
393 return P_ENGINE_RESET;
395 } else if (!strcasecmp(cmd, "kgs-game_over")) {
396 /* The game may not be really over, just adjourned.
397 * Do not clear the board to avoid illegal moves
398 * if the game is resumed immediately after. KGS
399 * may start directly with genmove on resumption. */
400 if (DEBUGL(1)) {
401 fprintf(stderr, "game is over\n");
402 fflush(stderr);
404 if (engine->stop)
405 engine->stop(engine);
406 /* Sleep before replying, so that kgs doesn't
407 * start another game immediately. */
408 sleep(GAME_OVER_SLEEP);
409 gtp_reply(id, NULL);
411 } else if (!strcasecmp(cmd, "komi")) {
412 char *arg;
413 next_tok(arg);
414 sscanf(arg, PRIfloating, &board->komi);
416 if (DEBUGL(3) && debug_boardprint)
417 board_print(board, stderr);
418 gtp_reply(id, NULL);
420 } else if (!strcasecmp(cmd, "kgs-rules")) {
421 char *arg;
422 next_tok(arg);
423 if (!board_set_rules(board, arg)) {
424 gtp_error(id, "unknown rules", NULL);
425 return P_OK;
427 gtp_reply(id, NULL);
429 } else if (!strcasecmp(cmd, "play")) {
430 struct move m;
432 char *arg;
433 next_tok(arg);
434 m.color = str2stone(arg);
435 next_tok(arg);
436 coord_t *c = str2coord(arg, board_size(board));
437 m.coord = *c; coord_done(c);
438 next_tok(arg);
439 char *enginearg = arg;
440 char *reply = NULL;
442 if (DEBUGL(5))
443 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
445 // This is where kgs starts the timer, not at genmove!
446 time_start_timer(&ti[stone_other(m.color)]);
448 if (engine->notify_play)
449 reply = engine->notify_play(engine, board, &m, enginearg);
450 if (board_play(board, &m) < 0) {
451 if (DEBUGL(0)) {
452 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
453 board_print(board, stderr);
455 gtp_error(id, "illegal move", NULL);
456 } else {
457 if (DEBUGL(4) && debug_boardprint)
458 engine_board_print(engine, board, stderr);
459 gtp_reply(id, reply, NULL);
462 } else if (!strcasecmp(cmd, "pachi-predict")) {
463 struct move m;
464 char *arg;
465 next_tok(arg);
466 m.color = str2stone(arg);
467 next_tok(arg);
468 coord_t *c = str2coord(arg, board_size(board));
469 m.coord = *c; coord_done(c);
470 next_tok(arg);
472 gtp_predict_move(board, engine, ti, id, &m);
474 } else if (!strcasecmp(cmd, "genmove") || !strcasecmp(cmd, "kgs-genmove_cleanup")) {
475 char *arg;
476 next_tok(arg);
477 enum stone color = str2stone(arg);
478 coord_t *c = NULL;
479 if (DEBUGL(2) && debug_boardprint)
480 engine_board_print(engine, board, stderr);
482 if (!ti[color].len.t.timer_start) {
483 /* First game move. */
484 time_start_timer(&ti[color]);
487 coord_t cf = pass;
488 if (board->fbook)
489 cf = fbook_check(board);
490 if (!is_pass(cf)) {
491 c = coord_copy(cf);
492 } else {
493 c = engine->genmove(engine, board, &ti[color], color, !strcasecmp(cmd, "kgs-genmove_cleanup"));
495 struct move m = { *c, color };
496 if (board_play(board, &m) < 0) {
497 fprintf(stderr, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m.coord, board), stone2str(m.color));
498 abort();
500 char *str = coord2str(*c, board);
501 if (DEBUGL(4))
502 fprintf(stderr, "playing move %s\n", str);
503 if (DEBUGL(1) && debug_boardprint) {
504 engine_board_print(engine, board, stderr);
506 gtp_reply(id, str, NULL);
507 free(str); coord_done(c);
509 /* Account for spent time. If our GTP peer keeps our clock, this will
510 * be overriden by next time_left GTP command properly. */
511 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
512 * should be absolutely rare situation and we will just spend a little
513 * less time than we could on next few moves.) */
514 if (ti[color].period != TT_NULL && ti[color].dim == TD_WALLTIME)
515 time_sub(&ti[color], time_now() - ti[color].len.t.timer_start, true);
517 } else if (!strcasecmp(cmd, "pachi-genmoves") || !strcasecmp(cmd, "pachi-genmoves_cleanup")) {
518 char *arg;
519 next_tok(arg);
520 enum stone color = str2stone(arg);
521 void *stats;
522 int stats_size;
524 char *reply = engine->genmoves(engine, board, &ti[color], color, next,
525 !strcasecmp(cmd, "pachi-genmoves_cleanup"),
526 &stats, &stats_size);
527 if (!reply) {
528 gtp_error(id, "genmoves error", NULL);
529 return P_OK;
531 if (DEBUGL(3))
532 fprintf(stderr, "proposing moves %s\n", reply);
533 if (DEBUGL(4) && debug_boardprint)
534 engine_board_print(engine, board, stderr);
535 gtp_reply(id, reply, NULL);
536 if (stats_size > 0) {
537 double start = time_now();
538 fwrite(stats, 1, stats_size, stdout);
539 fflush(stdout);
540 if (DEBUGVV(2))
541 fprintf(stderr, "sent reply %d bytes in %.4fms\n",
542 stats_size, (time_now() - start)*1000);
545 } else if (!strcasecmp(cmd, "set_free_handicap")) {
546 struct move m;
547 m.color = S_BLACK;
549 char *arg;
550 next_tok(arg);
551 do {
552 coord_t *c = str2coord(arg, board_size(board));
553 m.coord = *c; coord_done(c);
554 if (DEBUGL(4))
555 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
557 if (board_play(board, &m) < 0) {
558 if (DEBUGL(0))
559 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
560 gtp_error(id, "illegal move", NULL);
562 board->handicap++;
563 next_tok(arg);
564 } while (*arg);
565 if (DEBUGL(1) && debug_boardprint)
566 board_print(board, stderr);
567 gtp_reply(id, NULL);
569 /* TODO: Engine should choose free handicap; however, it tends to take
570 * overly long to think it all out, and unless it's clever its
571 * handicap stones won't be of much help. ;-) */
572 } else if (!strcasecmp(cmd, "place_free_handicap")
573 || !strcasecmp(cmd, "fixed_handicap")) {
574 char *arg;
575 next_tok(arg);
576 int stones = atoi(arg);
578 gtp_prefix('=', id);
579 board_handicap(board, stones, id == NO_REPLY ? NULL : stdout);
580 if (DEBUGL(1) && debug_boardprint)
581 board_print(board, stderr);
582 if (id == NO_REPLY) return P_OK;
583 putchar('\n');
584 gtp_flush();
586 } else if (!strcasecmp(cmd, "final_score")) {
587 char str[64];
588 gtp_final_score(board, engine, str, sizeof(str));
589 gtp_reply(id, str, NULL);
591 /* XXX: This is a huge hack. */
592 } else if (!strcasecmp(cmd, "final_status_list")) {
593 if (id == NO_REPLY) return P_OK;
594 char *arg;
595 next_tok(arg);
596 struct move_queue q = { .moves = 0 };
597 if (engine->dead_group_list)
598 engine->dead_group_list(engine, board, &q);
599 /* else we return empty list - i.e. engine not supporting
600 * this assumes all stones alive at the game end. */
601 if (!strcasecmp(arg, "dead")) {
602 gtp_prefix('=', id);
603 for (unsigned int i = 0; i < q.moves; i++) {
604 foreach_in_group(board, q.move[i]) {
605 printf("%s ", coord2sstr(c, board));
606 } foreach_in_group_end;
607 putchar('\n');
609 if (!q.moves)
610 putchar('\n');
611 gtp_flush();
612 } else if (!strcasecmp(arg, "seki") || !strcasecmp(arg, "alive")) {
613 gtp_prefix('=', id);
614 bool printed_group = false;
615 foreach_point(board) { // foreach_group, effectively
616 group_t g = group_at(board, c);
617 if (!g || g != c) continue;
619 for (unsigned int i = 0; i < q.moves; i++) {
620 if (q.move[i] == g)
621 goto next_group;
623 foreach_in_group(board, g) {
624 printf("%s ", coord2sstr(c, board));
625 } foreach_in_group_end;
626 putchar('\n');
627 printed_group = true;
628 next_group:;
629 } foreach_point_end;
630 if (!printed_group)
631 putchar('\n');
632 gtp_flush();
633 } else {
634 gtp_error(id, "illegal status specifier", NULL);
637 } else if (!strcasecmp(cmd, "undo")) {
638 if (board_undo(board) < 0) {
639 if (DEBUGL(1)) {
640 fprintf(stderr, "undo on non-pass move %s\n", coord2sstr(board->last_move.coord, board));
641 board_print(board, stderr);
643 gtp_error(id, "cannot undo", NULL);
644 return P_OK;
646 char *reply = NULL;
647 if (engine->undo)
648 reply = engine->undo(engine, board);
649 if (DEBUGL(3) && debug_boardprint)
650 board_print(board, stderr);
651 gtp_reply(id, reply, NULL);
653 /* Custom commands for handling the tree opening tbook */
654 } else if (!strcasecmp(cmd, "pachi-gentbook")) {
655 /* Board must be initialized properly, as if for genmove;
656 * makes sense only as 'uct_gentbook b'. */
657 char *arg;
658 next_tok(arg);
659 enum stone color = str2stone(arg);
660 if (uct_gentbook(engine, board, &ti[color], color))
661 gtp_reply(id, NULL);
662 else
663 gtp_error(id, "error generating tbook", NULL);
665 } else if (!strcasecmp(cmd, "pachi-dumptbook")) {
666 char *arg;
667 next_tok(arg);
668 enum stone color = str2stone(arg);
669 uct_dumptbook(engine, board, color);
670 gtp_reply(id, NULL);
672 } else if (!strcasecmp(cmd, "pachi-evaluate")) {
673 char *arg;
674 next_tok(arg);
675 enum stone color = str2stone(arg);
677 if (!engine->evaluate) {
678 gtp_error(id, "pachi-evaluate not supported by engine", NULL);
679 } else {
680 gtp_prefix('=', id);
681 floating_t vals[board->flen];
682 engine->evaluate(engine, board, &ti[color], vals, color);
683 for (int i = 0; i < board->flen; i++) {
684 if (!board_coord_in_symmetry(board, board->f[i])
685 || isnan(vals[i]) || vals[i] < 0.001)
686 continue;
687 printf("%s %.3f\n", coord2sstr(board->f[i], board), (double) vals[i]);
689 gtp_flush();
692 } else if (!strcasecmp(cmd, "pachi-result")) {
693 /* More detailed result of the last genmove. */
694 /* For UCT, the output format is: = color move playouts winrate dynkomi */
695 char *reply = NULL;
696 if (engine->result)
697 reply = engine->result(engine, board);
698 if (reply)
699 gtp_reply(id, reply, NULL);
700 else
701 gtp_error(id, "unknown pachi-result command", NULL);
703 } else if (!strcasecmp(cmd, "kgs-chat")) {
704 char *loc;
705 next_tok(loc);
706 bool opponent = !strcasecmp(loc, "game");
707 char *from;
708 next_tok(from);
709 char *msg = next;
710 msg += strspn(msg, " \n\t");
711 char *end = strchr(msg, '\n');
712 if (end) *end = '\0';
713 char *reply = NULL;
714 if (engine->chat) {
715 reply = engine->chat(engine, board, opponent, from, msg);
717 if (reply)
718 gtp_reply(id, reply, NULL);
719 else
720 gtp_error(id, "unknown kgs-chat command", NULL);
722 } else if (!strcasecmp(cmd, "time_left")) {
723 char *arg;
724 next_tok(arg);
725 enum stone color = str2stone(arg);
726 next_tok(arg);
727 int time = atoi(arg);
728 next_tok(arg);
729 int stones = atoi(arg);
730 if (!ti[color].ignore_gtp) {
731 time_left(&ti[color], time, stones);
732 } else {
733 if (DEBUGL(2)) fprintf(stderr, "ignored time info\n");
736 gtp_reply(id, NULL);
738 } else if (!strcasecmp(cmd, "time_settings") || !strcasecmp(cmd, "kgs-time_settings")) {
739 char *time_system;
740 char *arg;
741 if (!strcasecmp(cmd, "kgs-time_settings")) {
742 next_tok(time_system);
743 } else {
744 time_system = "canadian";
747 int main_time = 0, byoyomi_time = 0, byoyomi_stones = 0, byoyomi_periods = 0;
748 if (!strcasecmp(time_system, "none")) {
749 main_time = -1;
750 } else if (!strcasecmp(time_system, "absolute")) {
751 next_tok(arg);
752 main_time = atoi(arg);
753 } else if (!strcasecmp(time_system, "byoyomi")) {
754 next_tok(arg);
755 main_time = atoi(arg);
756 next_tok(arg);
757 byoyomi_time = atoi(arg);
758 next_tok(arg);
759 byoyomi_periods = atoi(arg);
760 } else if (!strcasecmp(time_system, "canadian")) {
761 next_tok(arg);
762 main_time = atoi(arg);
763 next_tok(arg);
764 byoyomi_time = atoi(arg);
765 next_tok(arg);
766 byoyomi_stones = atoi(arg);
769 if (DEBUGL(1))
770 fprintf(stderr, "time_settings %d %d/%d*%d\n",
771 main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
772 if (!ti[S_BLACK].ignore_gtp) {
773 time_settings(&ti[S_BLACK], main_time, byoyomi_time, byoyomi_stones, byoyomi_periods);
774 ti[S_WHITE] = ti[S_BLACK];
775 } else {
776 if (DEBUGL(1)) fprintf(stderr, "ignored time info\n");
779 gtp_reply(id, NULL);
781 } else if (!strcasecmp(cmd, "gogui-analyze_commands")) {
782 gtp_reply(id, gogui_analyze_commands, NULL);
783 } else if (!strcasecmp(cmd, "gogui-live_gfx")) {
784 char *arg;
785 next_tok(arg);
786 gogui_set_live_gfx(engine, arg);
787 gtp_reply(id, NULL);
788 } else if (!strcasecmp(cmd, "gogui-owner_map")) {
789 char reply[5000];
790 gogui_owner_map(board, engine, reply);
791 gtp_reply(id, reply, NULL);
792 } else if (!strcasecmp(cmd, "gogui-best_moves")) {
793 char *arg;
794 next_tok(arg);
795 char *reply = gogui_best_moves(board, engine, arg, false);
796 gtp_reply(id, reply, NULL);
797 } else if (!strcasecmp(cmd, "gogui-winrates")) {
798 char *arg;
799 next_tok(arg);
800 char *reply = gogui_best_moves(board, engine, arg, true);
801 gtp_reply(id, reply, NULL);
803 } else {
804 gtp_error(id, "unknown command", NULL);
805 return P_UNKNOWN_COMMAND;
807 return P_OK;
809 #undef next_tok