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"
127 known_commands(struct engine
*engine
)
129 static char *str
= 0;
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
);
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;
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
);
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 */
182 gogui_owner_map(struct board
*b
, struct engine
*engine
, char *reply
)
186 if (!engine
->owner_map
)
189 sprintf(reply
, "INFLUENCE");
191 if (board_at(b
, c
) == S_OFFBOARD
)
193 float p
= engine
->owner_map(engine
, b
, c
);
195 // p = -1 for WHITE, 1 for BLACK absolute ownership of point i
210 sprintf(str2
, " %3s %.1lf", coord2sstr(c
, b
), p
);
214 strcat(reply
, "\nTEXT Score Est: ");
215 gtp_final_score(b
, engine
, str2
, sizeof(str2
));
219 /* Return true if cmd is a valid gtp command. */
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;
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
);
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
));
266 if (DEBUGL(1) && debug_boardprint
)
267 engine_board_print(engine
, board
, stderr
);
270 fprintf(stderr
, "Move %3i: Predict: Correctly predicted %s %s\n", board
->moves
,
271 (color
== S_BLACK
? "b" : "w"), coord2sstr(*c
, board
));
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
);
291 /* XXX: THIS IS TOTALLY INSECURE!!!!
292 * Even basic input checking is missing. */
295 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
297 #define next_tok(to_) \
299 next = next + strcspn(next, " \t\r\n"); \
302 next += strspn(next, " \t\r\n"); \
305 if (strchr(buf
, '#'))
306 *strchr(buf
, '#') = 0;
308 char *cmd
, *next
= buf
;
320 if (!strcasecmp(cmd
, "protocol_version")) {
321 gtp_reply(id
, "2", NULL
);
324 } else if (!strcasecmp(cmd
, "name")) {
326 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
329 } else if (!strcasecmp(cmd
, "echo")) {
330 gtp_reply(id
, next
, NULL
);
333 } else if (!strcasecmp(cmd
, "version")) {
334 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, " Have a nice game!", NULL
);
337 } else if (!strcasecmp(cmd
, "list_commands")) {
338 gtp_reply(id
, known_commands(engine
), NULL
);
341 } else if (!strcasecmp(cmd
, "known_command")) {
344 if (gtp_is_valid(engine
, arg
)) {
345 gtp_reply(id
, "true", NULL
);
347 gtp_reply(id
, "false", NULL
);
352 if (engine
->notify
&& gtp_is_valid(engine
, cmd
)) {
354 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
355 if (c
== P_NOREPLY
) {
357 } else if (c
== P_DONE_OK
) {
358 gtp_reply(id
, reply
, NULL
);
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. */
365 } else if (c
!= P_OK
) {
370 if (!strcasecmp(cmd
, "quit")) {
374 } else if (!strcasecmp(cmd
, "boardsize")) {
377 int size
= atoi(arg
);
378 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
379 gtp_error(id
, "illegal board size", NULL
);
382 board_resize(board
, size
);
385 return P_ENGINE_RESET
;
387 } else if (!strcasecmp(cmd
, "clear_board")) {
390 if (DEBUGL(3) && debug_boardprint
)
391 board_print(board
, stderr
);
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. */
401 fprintf(stderr
, "game is over\n");
405 engine
->stop(engine
);
406 /* Sleep before replying, so that kgs doesn't
407 * start another game immediately. */
408 sleep(GAME_OVER_SLEEP
);
411 } else if (!strcasecmp(cmd
, "komi")) {
414 sscanf(arg
, PRIfloating
, &board
->komi
);
416 if (DEBUGL(3) && debug_boardprint
)
417 board_print(board
, stderr
);
420 } else if (!strcasecmp(cmd
, "kgs-rules")) {
423 if (!board_set_rules(board
, arg
)) {
424 gtp_error(id
, "unknown rules", NULL
);
429 } else if (!strcasecmp(cmd
, "play")) {
434 m
.color
= str2stone(arg
);
436 coord_t
*c
= str2coord(arg
, board_size(board
));
437 m
.coord
= *c
; coord_done(c
);
439 char *enginearg
= arg
;
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) {
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
);
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")) {
466 m
.color
= str2stone(arg
);
468 coord_t
*c
= str2coord(arg
, board_size(board
));
469 m
.coord
= *c
; coord_done(c
);
472 gtp_predict_move(board
, engine
, ti
, id
, &m
);
474 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
477 enum stone color
= str2stone(arg
);
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
]);
489 cf
= fbook_check(board
);
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
));
500 char *str
= coord2str(*c
, board
);
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")) {
520 enum stone color
= str2stone(arg
);
524 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
525 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
526 &stats
, &stats_size
);
528 gtp_error(id
, "genmoves error", NULL
);
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
);
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")) {
552 coord_t
*c
= str2coord(arg
, board_size(board
));
553 m
.coord
= *c
; coord_done(c
);
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) {
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
);
565 if (DEBUGL(1) && debug_boardprint
)
566 board_print(board
, stderr
);
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")) {
576 int stones
= atoi(arg
);
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
;
586 } else if (!strcasecmp(cmd
, "final_score")) {
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
;
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")) {
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
;
612 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
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
++) {
623 foreach_in_group(board
, g
) {
624 printf("%s ", coord2sstr(c
, board
));
625 } foreach_in_group_end
;
627 printed_group
= true;
634 gtp_error(id
, "illegal status specifier", NULL
);
637 } else if (!strcasecmp(cmd
, "undo")) {
638 if (board_undo(board
) < 0) {
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
);
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'. */
659 enum stone color
= str2stone(arg
);
660 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
663 gtp_error(id
, "error generating tbook", NULL
);
665 } else if (!strcasecmp(cmd
, "pachi-dumptbook")) {
668 enum stone color
= str2stone(arg
);
669 uct_dumptbook(engine
, board
, color
);
672 } else if (!strcasecmp(cmd
, "pachi-evaluate")) {
675 enum stone color
= str2stone(arg
);
677 if (!engine
->evaluate
) {
678 gtp_error(id
, "pachi-evaluate not supported by engine", NULL
);
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)
687 printf("%s %.3f\n", coord2sstr(board
->f
[i
], board
), (double) vals
[i
]);
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 */
697 reply
= engine
->result(engine
, board
);
699 gtp_reply(id
, reply
, NULL
);
701 gtp_error(id
, "unknown pachi-result command", NULL
);
703 } else if (!strcasecmp(cmd
, "kgs-chat")) {
706 bool opponent
= !strcasecmp(loc
, "game");
710 msg
+= strspn(msg
, " \n\t");
711 char *end
= strchr(msg
, '\n');
712 if (end
) *end
= '\0';
715 reply
= engine
->chat(engine
, board
, opponent
, from
, msg
);
718 gtp_reply(id
, reply
, NULL
);
720 gtp_error(id
, "unknown kgs-chat command", NULL
);
722 } else if (!strcasecmp(cmd
, "time_left")) {
725 enum stone color
= str2stone(arg
);
727 int time
= atoi(arg
);
729 int stones
= atoi(arg
);
730 if (!ti
[color
].ignore_gtp
) {
731 time_left(&ti
[color
], time
, stones
);
733 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
738 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
741 if (!strcasecmp(cmd
, "kgs-time_settings")) {
742 next_tok(time_system
);
744 time_system
= "canadian";
747 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
748 if (!strcasecmp(time_system
, "none")) {
750 } else if (!strcasecmp(time_system
, "absolute")) {
752 main_time
= atoi(arg
);
753 } else if (!strcasecmp(time_system
, "byoyomi")) {
755 main_time
= atoi(arg
);
757 byoyomi_time
= atoi(arg
);
759 byoyomi_periods
= atoi(arg
);
760 } else if (!strcasecmp(time_system
, "canadian")) {
762 main_time
= atoi(arg
);
764 byoyomi_time
= atoi(arg
);
766 byoyomi_stones
= atoi(arg
);
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
];
776 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
781 } else if (!strcasecmp(cmd
, "gogui-analyze_commands")) {
782 gtp_reply(id
, gogui_analyze_commands
, NULL
);
783 } else if (!strcasecmp(cmd
, "gogui-live_gfx")) {
786 gogui_set_live_gfx(engine
, arg
);
788 } else if (!strcasecmp(cmd
, "gogui-owner_map")) {
790 gogui_owner_map(board
, engine
, reply
);
791 gtp_reply(id
, reply
, NULL
);
792 } else if (!strcasecmp(cmd
, "gogui-best_moves")) {
795 char *reply
= gogui_best_moves(board
, engine
, arg
, false);
796 gtp_reply(id
, reply
, NULL
);
797 } else if (!strcasecmp(cmd
, "gogui-winrates")) {
800 char *reply
= gogui_best_moves(board
, engine
, arg
, true);
801 gtp_reply(id
, reply
, NULL
);
804 gtp_error(id
, "unknown command", NULL
);
805 return P_UNKNOWN_COMMAND
;