24 /* Sleep 5 seconds after a game ends to give time to kill the program. */
25 #define GAME_OVER_SLEEP 5
28 gtp_prefix(char prefix
, int id
)
30 if (id
== NO_REPLY
) return;
32 printf("%c%d ", prefix
, id
);
34 printf("%c ", prefix
);
45 gtp_output(char prefix
, int id
, va_list params
)
47 if (id
== NO_REPLY
) return;
48 gtp_prefix(prefix
, id
);
50 while ((s
= va_arg(params
, char *))) {
58 gtp_reply(int id
, ...)
62 gtp_output('=', id
, params
);
67 gtp_error(int id
, ...)
71 gtp_output('?', id
, params
);
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
);
83 fprintf(stderr
, "counted score %.1f\n", score
);
85 snprintf(reply
, len
, "0");
87 snprintf(reply
, len
, "W+%.1f", score
);
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
=
109 "kgs-genmove_cleanup\n"
110 "set_free_handicap\n"
111 "place_free_handicap\n"
114 "final_status_list\n"
126 known_commands(struct engine
*engine
)
128 static char *str
= 0;
131 if (strcmp(engine
->name
, "UCT")) /* Not uct ? */
132 return known_commands_base
;
133 /* For now only uct supports gogui-analyze_commands */
134 str
= malloc(strlen(known_commands_base
) + 32);
135 sprintf(str
, "%s\ngogui-analyze_commands", known_commands_base
);
139 static char *gogui_analyze_commands
=
140 "string/ Final Score/final_score\n"
141 "gfx/gfx Best Moves B/gogui-best_moves b\n"
142 "gfx/gfx Best Moves W/gogui-best_moves w\n"
143 "gfx/gfx Winrates B/gogui-winrates b\n"
144 "gfx/gfx Winrates W/gogui-winrates w\n"
145 "gfx/gfx Owner Map/gogui-owner_map\n"
146 "gfx/Live gfx = Best Moves/gogui-live_gfx best_moves\n"
147 "gfx/Live gfx = Best Sequence/gogui-live_gfx best_seq\n"
148 "gfx/Live gfx = Winrates/gogui-live_gfx winrates\n";
151 char gogui_gfx_buf
[5000];
152 enum gogui_reporting gogui_live_gfx
= 0;
155 gogui_set_live_gfx(struct engine
*engine
, char *arg
)
157 if (!strcmp(arg
, "best_moves"))
158 gogui_live_gfx
= UR_GOGUI_CAN
;
159 if (!strcmp(arg
, "best_seq"))
160 gogui_live_gfx
= UR_GOGUI_SEQ
;
161 if (!strcmp(arg
, "winrates"))
162 gogui_live_gfx
= UR_GOGUI_WR
;
163 engine
->live_gfx_hook(engine
);
167 gogui_best_moves(struct board
*b
, struct engine
*engine
, char *arg
, bool winrates
)
169 enum stone color
= str2stone(arg
);
170 assert(color
!= S_NONE
);
171 enum gogui_reporting prev
= gogui_live_gfx
;
172 gogui_set_live_gfx(engine
, (winrates
? "winrates" : "best_moves"));
173 gogui_gfx_buf
[0] = 0;
174 engine
->best_moves(engine
, b
, color
);
175 gogui_live_gfx
= prev
;
176 return gogui_gfx_buf
;
179 /* XXX Completely unsafe if reply buffer is not big enough */
181 gogui_owner_map(struct board
*b
, struct engine
*engine
, char *reply
)
185 if (!engine
->owner_map
)
188 sprintf(reply
, "INFLUENCE");
190 if (board_at(b
, c
) == S_OFFBOARD
)
192 float p
= engine
->owner_map(engine
, b
, c
);
194 // p = -1 for WHITE, 1 for BLACK absolute ownership of point i
209 sprintf(str2
, " %3s %.1lf", coord2sstr(c
, b
), p
);
213 strcat(reply
, "\nTEXT Score Est: ");
214 gtp_final_score(b
, engine
, str2
, sizeof(str2
));
218 /* Return true if cmd is a valid gtp command. */
220 gtp_is_valid(struct engine
*e
, const char *cmd
)
222 if (!cmd
|| !*cmd
) return false;
223 const char *s
= strcasestr(known_commands(e
), cmd
);
224 if (!s
) return false;
225 if (s
!= known_commands(e
) && s
[-1] != '\n') return false;
227 int len
= strlen(cmd
);
228 return s
[len
] == '\0' || s
[len
] == '\n';
231 /* XXX: THIS IS TOTALLY INSECURE!!!!
232 * Even basic input checking is missing. */
235 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
237 #define next_tok(to_) \
239 next = next + strcspn(next, " \t\r\n"); \
242 next += strspn(next, " \t\r\n"); \
245 if (strchr(buf
, '#'))
246 *strchr(buf
, '#') = 0;
248 char *cmd
, *next
= buf
;
260 if (!strcasecmp(cmd
, "protocol_version")) {
261 gtp_reply(id
, "2", NULL
);
264 } else if (!strcasecmp(cmd
, "name")) {
266 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
269 } else if (!strcasecmp(cmd
, "echo")) {
270 gtp_reply(id
, next
, NULL
);
273 } else if (!strcasecmp(cmd
, "version")) {
274 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, " Have a nice game!", NULL
);
277 } else if (!strcasecmp(cmd
, "list_commands")) {
278 gtp_reply(id
, known_commands(engine
), NULL
);
281 } else if (!strcasecmp(cmd
, "known_command")) {
284 if (gtp_is_valid(engine
, arg
)) {
285 gtp_reply(id
, "true", NULL
);
287 gtp_reply(id
, "false", NULL
);
292 if (engine
->notify
&& gtp_is_valid(engine
, cmd
)) {
294 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
295 if (c
== P_NOREPLY
) {
297 } else if (c
== P_DONE_OK
) {
298 gtp_reply(id
, reply
, NULL
);
300 } else if (c
== P_DONE_ERROR
) {
301 gtp_error(id
, reply
, NULL
);
302 /* This is an internal error for the engine, but
303 * it is still OK from main's point of view. */
305 } else if (c
!= P_OK
) {
310 if (!strcasecmp(cmd
, "quit")) {
314 } else if (!strcasecmp(cmd
, "boardsize")) {
317 int size
= atoi(arg
);
318 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
319 gtp_error(id
, "illegal board size", NULL
);
322 board_resize(board
, size
);
325 return P_ENGINE_RESET
;
327 } else if (!strcasecmp(cmd
, "clear_board")) {
329 if (DEBUGL(3) && debug_boardprint
)
330 board_print(board
, stderr
);
332 return P_ENGINE_RESET
;
334 } else if (!strcasecmp(cmd
, "kgs-game_over")) {
335 /* The game may not be really over, just adjourned.
336 * Do not clear the board to avoid illegal moves
337 * if the game is resumed immediately after. KGS
338 * may start directly with genmove on resumption. */
340 fprintf(stderr
, "game is over\n");
344 engine
->stop(engine
);
345 /* Sleep before replying, so that kgs doesn't
346 * start another game immediately. */
347 sleep(GAME_OVER_SLEEP
);
350 } else if (!strcasecmp(cmd
, "komi")) {
353 sscanf(arg
, PRIfloating
, &board
->komi
);
355 if (DEBUGL(3) && debug_boardprint
)
356 board_print(board
, stderr
);
359 } else if (!strcasecmp(cmd
, "kgs-rules")) {
362 if (!board_set_rules(board
, arg
)) {
363 gtp_error(id
, "unknown rules", NULL
);
368 } else if (!strcasecmp(cmd
, "play")) {
373 m
.color
= str2stone(arg
);
375 coord_t
*c
= str2coord(arg
, board_size(board
));
376 m
.coord
= *c
; coord_done(c
);
378 char *enginearg
= arg
;
382 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
384 // This is where kgs starts the timer, not at genmove!
385 time_start_timer(&ti
[stone_other(m
.color
)]);
387 if (engine
->notify_play
)
388 reply
= engine
->notify_play(engine
, board
, &m
, enginearg
);
389 if (board_play(board
, &m
) < 0) {
391 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
392 board_print(board
, stderr
);
394 gtp_error(id
, "illegal move", NULL
);
396 if (DEBUGL(4) && debug_boardprint
)
397 board_print_custom(board
, stderr
, engine
->printhook
);
398 gtp_reply(id
, reply
, NULL
);
401 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
404 enum stone color
= str2stone(arg
);
406 if (DEBUGL(2) && debug_boardprint
)
407 board_print_custom(board
, stderr
, engine
->printhook
);
409 if (!ti
[color
].len
.t
.timer_start
) {
410 /* First game move. */
411 time_start_timer(&ti
[color
]);
416 cf
= fbook_check(board
);
420 c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
422 struct move m
= { *c
, color
};
423 if (board_play(board
, &m
) < 0) {
424 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
427 char *str
= coord2str(*c
, board
);
429 fprintf(stderr
, "playing move %s\n", str
);
430 if (DEBUGL(1) && debug_boardprint
) {
431 board_print_custom(board
, stderr
, engine
->printhook
);
433 gtp_reply(id
, str
, NULL
);
434 free(str
); coord_done(c
);
436 /* Account for spent time. If our GTP peer keeps our clock, this will
437 * be overriden by next time_left GTP command properly. */
438 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
439 * should be absolutely rare situation and we will just spend a little
440 * less time than we could on next few moves.) */
441 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
442 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
444 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
447 enum stone color
= str2stone(arg
);
451 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
452 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
453 &stats
, &stats_size
);
455 gtp_error(id
, "genmoves error", NULL
);
459 fprintf(stderr
, "proposing moves %s\n", reply
);
460 if (DEBUGL(4) && debug_boardprint
)
461 board_print_custom(board
, stderr
, engine
->printhook
);
462 gtp_reply(id
, reply
, NULL
);
463 if (stats_size
> 0) {
464 double start
= time_now();
465 fwrite(stats
, 1, stats_size
, stdout
);
468 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
469 stats_size
, (time_now() - start
)*1000);
472 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
479 coord_t
*c
= str2coord(arg
, board_size(board
));
480 m
.coord
= *c
; coord_done(c
);
482 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
484 if (board_play(board
, &m
) < 0) {
486 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
487 gtp_error(id
, "illegal move", NULL
);
492 if (DEBUGL(1) && debug_boardprint
)
493 board_print(board
, stderr
);
496 /* TODO: Engine should choose free handicap; however, it tends to take
497 * overly long to think it all out, and unless it's clever its
498 * handicap stones won't be of much help. ;-) */
499 } else if (!strcasecmp(cmd
, "place_free_handicap")
500 || !strcasecmp(cmd
, "fixed_handicap")) {
503 int stones
= atoi(arg
);
506 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
507 if (DEBUGL(1) && debug_boardprint
)
508 board_print(board
, stderr
);
509 if (id
== NO_REPLY
) return P_OK
;
513 } else if (!strcasecmp(cmd
, "final_score")) {
515 gtp_final_score(board
, engine
, str
, sizeof(str
));
516 gtp_reply(id
, str
, NULL
);
518 /* XXX: This is a huge hack. */
519 } else if (!strcasecmp(cmd
, "final_status_list")) {
520 if (id
== NO_REPLY
) return P_OK
;
523 struct move_queue q
= { .moves
= 0 };
524 if (engine
->dead_group_list
)
525 engine
->dead_group_list(engine
, board
, &q
);
526 /* else we return empty list - i.e. engine not supporting
527 * this assumes all stones alive at the game end. */
528 if (!strcasecmp(arg
, "dead")) {
530 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
531 foreach_in_group(board
, q
.move
[i
]) {
532 printf("%s ", coord2sstr(c
, board
));
533 } foreach_in_group_end
;
539 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
541 bool printed_group
= false;
542 foreach_point(board
) { // foreach_group, effectively
543 group_t g
= group_at(board
, c
);
544 if (!g
|| g
!= c
) continue;
546 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
550 foreach_in_group(board
, g
) {
551 printf("%s ", coord2sstr(c
, board
));
552 } foreach_in_group_end
;
554 printed_group
= true;
561 gtp_error(id
, "illegal status specifier", NULL
);
564 } else if (!strcasecmp(cmd
, "undo")) {
565 if (board_undo(board
) < 0) {
567 fprintf(stderr
, "undo on non-pass move %s\n", coord2sstr(board
->last_move
.coord
, board
));
568 board_print(board
, stderr
);
570 gtp_error(id
, "cannot undo", NULL
);
575 reply
= engine
->undo(engine
, board
);
576 if (DEBUGL(3) && debug_boardprint
)
577 board_print(board
, stderr
);
578 gtp_reply(id
, reply
, NULL
);
580 /* Custom commands for handling the tree opening tbook */
581 } else if (!strcasecmp(cmd
, "pachi-gentbook")) {
582 /* Board must be initialized properly, as if for genmove;
583 * makes sense only as 'uct_gentbook b'. */
586 enum stone color
= str2stone(arg
);
587 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
590 gtp_error(id
, "error generating tbook", NULL
);
592 } else if (!strcasecmp(cmd
, "pachi-dumptbook")) {
595 enum stone color
= str2stone(arg
);
596 uct_dumptbook(engine
, board
, color
);
599 } else if (!strcasecmp(cmd
, "pachi-evaluate")) {
602 enum stone color
= str2stone(arg
);
604 if (!engine
->evaluate
) {
605 gtp_error(id
, "pachi-evaluate not supported by engine", NULL
);
608 floating_t vals
[board
->flen
];
609 engine
->evaluate(engine
, board
, &ti
[color
], vals
, color
);
610 for (int i
= 0; i
< board
->flen
; i
++) {
611 if (!board_coord_in_symmetry(board
, board
->f
[i
])
612 || isnan(vals
[i
]) || vals
[i
] < 0.001)
614 printf("%s %.3f\n", coord2sstr(board
->f
[i
], board
), (double) vals
[i
]);
619 } else if (!strcasecmp(cmd
, "pachi-result")) {
620 /* More detailed result of the last genmove. */
621 /* For UCT, the output format is: = color move playouts winrate dynkomi */
624 reply
= engine
->result(engine
, board
);
626 gtp_reply(id
, reply
, NULL
);
628 gtp_error(id
, "unknown pachi-result command", NULL
);
630 } else if (!strcasecmp(cmd
, "kgs-chat")) {
633 bool opponent
= !strcasecmp(loc
, "game");
637 msg
+= strspn(msg
, " \n\t");
638 char *end
= strchr(msg
, '\n');
639 if (end
) *end
= '\0';
642 reply
= engine
->chat(engine
, board
, opponent
, from
, msg
);
645 gtp_reply(id
, reply
, NULL
);
647 gtp_error(id
, "unknown kgs-chat command", NULL
);
649 } else if (!strcasecmp(cmd
, "time_left")) {
652 enum stone color
= str2stone(arg
);
654 int time
= atoi(arg
);
656 int stones
= atoi(arg
);
657 if (!ti
[color
].ignore_gtp
) {
658 time_left(&ti
[color
], time
, stones
);
660 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
665 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
668 if (!strcasecmp(cmd
, "kgs-time_settings")) {
669 next_tok(time_system
);
671 time_system
= "canadian";
674 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
675 if (!strcasecmp(time_system
, "none")) {
677 } else if (!strcasecmp(time_system
, "absolute")) {
679 main_time
= atoi(arg
);
680 } else if (!strcasecmp(time_system
, "byoyomi")) {
682 main_time
= atoi(arg
);
684 byoyomi_time
= atoi(arg
);
686 byoyomi_periods
= atoi(arg
);
687 } else if (!strcasecmp(time_system
, "canadian")) {
689 main_time
= atoi(arg
);
691 byoyomi_time
= atoi(arg
);
693 byoyomi_stones
= atoi(arg
);
697 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
698 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
699 if (!ti
[S_BLACK
].ignore_gtp
) {
700 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
701 ti
[S_WHITE
] = ti
[S_BLACK
];
703 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
708 } else if (!strcasecmp(cmd
, "gogui-analyze_commands")) {
709 gtp_reply(id
, gogui_analyze_commands
, NULL
);
710 } else if (!strcasecmp(cmd
, "gogui-live_gfx")) {
713 gogui_set_live_gfx(engine
, arg
);
715 } else if (!strcasecmp(cmd
, "gogui-owner_map")) {
717 gogui_owner_map(board
, engine
, reply
);
718 gtp_reply(id
, reply
, NULL
);
719 } else if (!strcasecmp(cmd
, "gogui-best_moves")) {
722 char *reply
= gogui_best_moves(board
, engine
, arg
, false);
723 gtp_reply(id
, reply
, NULL
);
724 } else if (!strcasecmp(cmd
, "gogui-winrates")) {
727 char *reply
= gogui_best_moves(board
, engine
, arg
, true);
728 gtp_reply(id
, reply
, NULL
);
731 gtp_error(id
, "unknown command", NULL
);
732 return P_UNKNOWN_COMMAND
;