23 /* Sleep 5 seconds after a game ends to give time to kill the program. */
24 #define GAME_OVER_SLEEP 5
27 gtp_prefix(char prefix
, int id
)
29 if (id
== NO_REPLY
) return;
31 printf("%c%d ", prefix
, id
);
33 printf("%c ", prefix
);
44 gtp_output(char prefix
, int id
, va_list params
)
46 if (id
== NO_REPLY
) return;
47 gtp_prefix(prefix
, id
);
49 while ((s
= va_arg(params
, char *))) {
57 gtp_reply(int id
, ...)
61 gtp_output('=', id
, params
);
66 gtp_error(int id
, ...)
70 gtp_output('?', id
, params
);
74 /* List of known gtp commands. The internal command pachi-genmoves is not exported,
75 * it should only be used between master and slaves of the distributed engine. */
76 static char *known_commands
=
91 "kgs-genmove_cleanup\n"
93 "place_free_handicap\n"
108 /* Return true if cmd is a valid gtp command. */
110 gtp_is_valid(const char *cmd
)
112 if (!cmd
|| !*cmd
) return false;
113 const char *s
= strcasestr(known_commands
, cmd
);
114 if (!s
) return false;
115 if (s
!= known_commands
&& s
[-1] != '\n') return false;
117 int len
= strlen(cmd
);
118 return s
[len
] == '\0' || s
[len
] == '\n';
121 /* XXX: THIS IS TOTALLY INSECURE!!!!
122 * Even basic input checking is missing. */
125 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
127 #define next_tok(to_) \
129 next = next + strcspn(next, " \t\r\n"); \
132 next += strspn(next, " \t\r\n"); \
135 if (strchr(buf
, '#'))
136 *strchr(buf
, '#') = 0;
138 char *cmd
, *next
= buf
;
150 if (!strcasecmp(cmd
, "protocol_version")) {
151 gtp_reply(id
, "2", NULL
);
154 } else if (!strcasecmp(cmd
, "name")) {
156 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
159 } else if (!strcasecmp(cmd
, "echo")) {
160 gtp_reply(id
, next
, NULL
);
163 } else if (!strcasecmp(cmd
, "version")) {
164 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, " Have a nice game!", NULL
);
167 } else if (!strcasecmp(cmd
, "list_commands")) {
168 gtp_reply(id
, known_commands
, NULL
);
171 } else if (!strcasecmp(cmd
, "known_command")) {
174 if (gtp_is_valid(arg
)) {
175 gtp_reply(id
, "true", NULL
);
177 gtp_reply(id
, "false", NULL
);
182 if (engine
->notify
&& gtp_is_valid(cmd
)) {
184 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
185 if (c
== P_NOREPLY
) {
187 } else if (c
== P_DONE_OK
) {
188 gtp_reply(id
, reply
, NULL
);
190 } else if (c
== P_DONE_ERROR
) {
191 gtp_error(id
, reply
, NULL
);
192 /* This is an internal error for the engine, but
193 * it is still OK from main's point of view. */
195 } else if (c
!= P_OK
) {
200 if (!strcasecmp(cmd
, "quit")) {
204 } else if (!strcasecmp(cmd
, "boardsize")) {
207 int size
= atoi(arg
);
208 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
209 gtp_error(id
, "illegal board size", NULL
);
212 board_resize(board
, size
);
215 return P_ENGINE_RESET
;
217 } else if (!strcasecmp(cmd
, "clear_board")) {
219 if (DEBUGL(3) && debug_boardprint
)
220 board_print(board
, stderr
);
222 return P_ENGINE_RESET
;
224 } else if (!strcasecmp(cmd
, "kgs-game_over")) {
225 /* The game may not be really over, just adjourned.
226 * Do not clear the board to avoid illegal moves
227 * if the game is resumed immediately after. KGS
228 * may start directly with genmove on resumption. */
230 fprintf(stderr
, "game is over\n");
234 engine
->stop(engine
);
235 /* Sleep before replying, so that kgs doesn't
236 * start another game immediately. */
237 sleep(GAME_OVER_SLEEP
);
240 } else if (!strcasecmp(cmd
, "komi")) {
243 sscanf(arg
, PRIfloating
, &board
->komi
);
245 if (DEBUGL(3) && debug_boardprint
)
246 board_print(board
, stderr
);
249 } else if (!strcasecmp(cmd
, "kgs-rules")) {
252 if (!board_set_rules(board
, arg
)) {
253 gtp_error(id
, "unknown rules", NULL
);
258 } else if (!strcasecmp(cmd
, "play")) {
263 m
.color
= str2stone(arg
);
265 coord_t
*c
= str2coord(arg
, board_size(board
));
266 m
.coord
= *c
; coord_done(c
);
268 char *enginearg
= arg
;
272 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
274 // This is where kgs starts the timer, not at genmove!
275 time_start_timer(&ti
[stone_other(m
.color
)]);
277 if (engine
->notify_play
)
278 reply
= engine
->notify_play(engine
, board
, &m
, enginearg
);
279 if (board_play(board
, &m
) < 0) {
281 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
282 board_print(board
, stderr
);
284 gtp_error(id
, "illegal move", NULL
);
286 if (DEBUGL(4) && debug_boardprint
)
287 board_print_custom(board
, stderr
, engine
->printhook
);
288 gtp_reply(id
, reply
, NULL
);
291 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
294 enum stone color
= str2stone(arg
);
296 if (DEBUGL(2) && debug_boardprint
)
297 board_print_custom(board
, stderr
, engine
->printhook
);
299 if (!ti
[color
].len
.t
.timer_start
) {
300 /* First game move. */
301 time_start_timer(&ti
[color
]);
306 cf
= fbook_check(board
);
310 c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
312 struct move m
= { *c
, color
};
313 if (board_play(board
, &m
) < 0) {
314 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
317 char *str
= coord2str(*c
, board
);
319 fprintf(stderr
, "playing move %s\n", str
);
320 if (DEBUGL(1) && debug_boardprint
) {
321 board_print_custom(board
, stderr
, engine
->printhook
);
323 gtp_reply(id
, str
, NULL
);
324 free(str
); coord_done(c
);
326 /* Account for spent time. If our GTP peer keeps our clock, this will
327 * be overriden by next time_left GTP command properly. */
328 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
329 * should be absolutely rare situation and we will just spend a little
330 * less time than we could on next few moves.) */
331 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
332 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
334 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
337 enum stone color
= str2stone(arg
);
341 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
342 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
343 &stats
, &stats_size
);
345 gtp_error(id
, "genmoves error", NULL
);
349 fprintf(stderr
, "proposing moves %s\n", reply
);
350 if (DEBUGL(4) && debug_boardprint
)
351 board_print_custom(board
, stderr
, engine
->printhook
);
352 gtp_reply(id
, reply
, NULL
);
353 if (stats_size
> 0) {
354 double start
= time_now();
355 fwrite(stats
, 1, stats_size
, stdout
);
358 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
359 stats_size
, (time_now() - start
)*1000);
362 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
369 coord_t
*c
= str2coord(arg
, board_size(board
));
370 m
.coord
= *c
; coord_done(c
);
372 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
374 if (board_play(board
, &m
) < 0) {
376 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
377 gtp_error(id
, "illegal move", NULL
);
382 if (DEBUGL(1) && debug_boardprint
)
383 board_print(board
, stderr
);
386 /* TODO: Engine should choose free handicap; however, it tends to take
387 * overly long to think it all out, and unless it's clever its
388 * handicap stones won't be of much help. ;-) */
389 } else if (!strcasecmp(cmd
, "place_free_handicap")
390 || !strcasecmp(cmd
, "fixed_handicap")) {
393 int stones
= atoi(arg
);
396 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
397 if (DEBUGL(1) && debug_boardprint
)
398 board_print(board
, stderr
);
399 if (id
== NO_REPLY
) return P_OK
;
403 } else if (!strcasecmp(cmd
, "final_score")) {
404 struct move_queue q
= { .moves
= 0 };
405 if (engine
->dead_group_list
)
406 engine
->dead_group_list(engine
, board
, &q
);
407 floating_t score
= board_official_score(board
, &q
);
410 fprintf(stderr
, "counted score %.1f\n", score
);
412 gtp_reply(id
, "0", NULL
);
413 } else if (score
> 0) {
414 snprintf(str
, 64, "W+%.1f", score
);
415 gtp_reply(id
, str
, NULL
);
417 snprintf(str
, 64, "B+%.1f", -score
);
418 gtp_reply(id
, str
, NULL
);
421 /* XXX: This is a huge hack. */
422 } else if (!strcasecmp(cmd
, "final_status_list")) {
423 if (id
== NO_REPLY
) return P_OK
;
426 struct move_queue q
= { .moves
= 0 };
427 if (engine
->dead_group_list
)
428 engine
->dead_group_list(engine
, board
, &q
);
429 /* else we return empty list - i.e. engine not supporting
430 * this assumes all stones alive at the game end. */
431 if (!strcasecmp(arg
, "dead")) {
433 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
434 foreach_in_group(board
, q
.move
[i
]) {
435 printf("%s ", coord2sstr(c
, board
));
436 } foreach_in_group_end
;
442 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
444 bool printed_group
= false;
445 foreach_point(board
) { // foreach_group, effectively
446 group_t g
= group_at(board
, c
);
447 if (!g
|| g
!= c
) continue;
449 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
453 foreach_in_group(board
, g
) {
454 printf("%s ", coord2sstr(c
, board
));
455 } foreach_in_group_end
;
457 printed_group
= true;
464 gtp_error(id
, "illegal status specifier", NULL
);
467 } else if (!strcasecmp(cmd
, "undo")) {
468 if (board_undo(board
) < 0) {
470 fprintf(stderr
, "undo on non-pass move %s\n", coord2sstr(board
->last_move
.coord
, board
));
471 board_print(board
, stderr
);
473 gtp_error(id
, "cannot undo", NULL
);
478 reply
= engine
->undo(engine
, board
);
479 if (DEBUGL(3) && debug_boardprint
)
480 board_print(board
, stderr
);
481 gtp_reply(id
, reply
, NULL
);
483 /* Custom commands for handling the tree opening tbook */
484 } else if (!strcasecmp(cmd
, "pachi-gentbook")) {
485 /* Board must be initialized properly, as if for genmove;
486 * makes sense only as 'uct_gentbook b'. */
489 enum stone color
= str2stone(arg
);
490 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
493 gtp_error(id
, "error generating tbook", NULL
);
495 } else if (!strcasecmp(cmd
, "pachi-dumptbook")) {
498 enum stone color
= str2stone(arg
);
499 uct_dumptbook(engine
, board
, color
);
502 } else if (!strcasecmp(cmd
, "pachi-evaluate")) {
505 enum stone color
= str2stone(arg
);
507 if (!engine
->evaluate
) {
508 gtp_error(id
, "pachi-evaluate not supported by engine", NULL
);
511 floating_t vals
[board
->flen
];
512 engine
->evaluate(engine
, board
, &ti
[color
], vals
, color
);
513 for (int i
= 0; i
< board
->flen
; i
++) {
514 if (!board_coord_in_symmetry(board
, board
->f
[i
])
515 || isnan(vals
[i
]) || vals
[i
] < 0.001)
517 printf("%s %.3f\n", coord2sstr(board
->f
[i
], board
), (double) vals
[i
]);
522 } else if (!strcasecmp(cmd
, "pachi-result")) {
523 /* More detailed result of the last genmove. */
524 /* For UCT, the output format is: = color move playouts winrate dynkomi */
527 reply
= engine
->result(engine
, board
);
529 gtp_reply(id
, reply
, NULL
);
531 gtp_error(id
, "unknown pachi-result command", NULL
);
533 } else if (!strcasecmp(cmd
, "kgs-chat")) {
536 bool opponent
= !strcasecmp(loc
, "game");
540 msg
+= strspn(msg
, " \n\t");
541 char *end
= strchr(msg
, '\n');
542 if (end
) *end
= '\0';
545 reply
= engine
->chat(engine
, board
, opponent
, from
, msg
);
548 gtp_reply(id
, reply
, NULL
);
550 gtp_error(id
, "unknown kgs-chat command", NULL
);
552 } else if (!strcasecmp(cmd
, "time_left")) {
555 enum stone color
= str2stone(arg
);
557 int time
= atoi(arg
);
559 int stones
= atoi(arg
);
560 if (!ti
[color
].ignore_gtp
) {
561 time_left(&ti
[color
], time
, stones
);
563 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
568 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
571 if (!strcasecmp(cmd
, "kgs-time_settings")) {
572 next_tok(time_system
);
574 time_system
= "canadian";
577 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
578 if (!strcasecmp(time_system
, "none")) {
580 } else if (!strcasecmp(time_system
, "absolute")) {
582 main_time
= atoi(arg
);
583 } else if (!strcasecmp(time_system
, "byoyomi")) {
585 main_time
= atoi(arg
);
587 byoyomi_time
= atoi(arg
);
589 byoyomi_periods
= atoi(arg
);
590 } else if (!strcasecmp(time_system
, "canadian")) {
592 main_time
= atoi(arg
);
594 byoyomi_time
= atoi(arg
);
596 byoyomi_stones
= atoi(arg
);
600 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
601 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
602 if (!ti
[S_BLACK
].ignore_gtp
) {
603 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
604 ti
[S_WHITE
] = ti
[S_BLACK
];
606 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
612 gtp_error(id
, "unknown command", NULL
);
613 return P_UNKNOWN_COMMAND
;