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"
104 /* Return true if cmd is a valid gtp command. */
106 gtp_is_valid(char *cmd
)
108 if (!cmd
|| !*cmd
) return false;
109 char *s
= strcasestr(known_commands
, cmd
);
110 if (!s
) return false;
111 if (s
!= known_commands
&& s
[-1] != '\n') return false;
113 int len
= strlen(cmd
);
114 return s
[len
] == '\0' || s
[len
] == '\n';
117 /* XXX: THIS IS TOTALLY INSECURE!!!!
118 * Even basic input checking is missing. */
121 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
123 #define next_tok(to_) \
125 next = next + strcspn(next, " \t\r\n"); \
128 next += strspn(next, " \t\r\n"); \
131 if (strchr(buf
, '#'))
132 *strchr(buf
, '#') = 0;
134 char *cmd
, *next
= buf
;
146 if (!strcasecmp(cmd
, "protocol_version")) {
147 gtp_reply(id
, "2", NULL
);
150 } else if (!strcasecmp(cmd
, "name")) {
152 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
155 } else if (!strcasecmp(cmd
, "echo")) {
156 gtp_reply(id
, next
, NULL
);
159 } else if (!strcasecmp(cmd
, "version")) {
160 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
163 } else if (!strcasecmp(cmd
, "list_commands")) {
164 gtp_reply(id
, known_commands
, NULL
);
167 } else if (!strcasecmp(cmd
, "known_command")) {
170 if (gtp_is_valid(arg
)) {
171 gtp_reply(id
, "true", NULL
);
173 gtp_reply(id
, "false", NULL
);
178 if (engine
->notify
&& gtp_is_valid(cmd
)) {
180 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
181 if (c
== P_NOREPLY
) {
183 } else if (c
== P_DONE_OK
) {
184 gtp_reply(id
, reply
, NULL
);
186 } else if (c
== P_DONE_ERROR
) {
187 gtp_error(id
, reply
, NULL
);
188 /* This is an internal error for the engine, but
189 * it is still OK from main's point of view. */
191 } else if (c
!= P_OK
) {
196 if (!strcasecmp(cmd
, "quit")) {
200 } else if (!strcasecmp(cmd
, "boardsize")) {
203 int size
= atoi(arg
);
204 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
205 gtp_error(id
, "illegal board size", NULL
);
208 board_resize(board
, size
);
211 } else if (!strcasecmp(cmd
, "clear_board")) {
213 if (DEBUGL(1) && debug_boardprint
)
214 board_print(board
, stderr
);
216 return P_ENGINE_RESET
;
218 } else if (!strcasecmp(cmd
, "kgs-game_over")) {
219 /* The game may not be really over, just adjourned.
220 * Do not clear the board to avoid illegal moves
221 * if the game is resumed immediately after. KGS
222 * may start directly with genmove on resumption. */
224 fprintf(stderr
, "game is over\n");
227 /* Sleep before replying, so that kgs doesn't
228 * start another game immediately. */
229 sleep(GAME_OVER_SLEEP
);
232 } else if (!strcasecmp(cmd
, "komi")) {
235 sscanf(arg
, PRIfloating
, &board
->komi
);
237 if (DEBUGL(1 && debug_boardprint
))
238 board_print(board
, stderr
);
241 } else if (!strcasecmp(cmd
, "kgs-rules")) {
244 if (!strcasecmp(arg
, "japanese")) {
245 board
->rules
= RULES_JAPANESE
;
246 } else if (!strcasecmp(arg
, "chinese")) {
247 board
->rules
= RULES_CHINESE
;
248 } else if (!strcasecmp(arg
, "aga")) {
249 board
->rules
= RULES_AGA
;
250 } else if (!strcasecmp(arg
, "new_zealand")) {
251 board
->rules
= RULES_NEW_ZEALAND
;
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
);
270 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
272 // This is where kgs starts the timer, not at genmove!
273 time_start_timer(&ti
[stone_other(m
.color
)]);
275 if (engine
->notify_play
)
276 reply
= engine
->notify_play(engine
, board
, &m
);
277 if (board_play(board
, &m
) < 0) {
279 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
280 board_print(board
, stderr
);
282 gtp_error(id
, "illegal move", NULL
);
284 if (DEBUGL(1) && debug_boardprint
)
285 board_print_custom(board
, stderr
, engine
->printhook
);
286 gtp_reply(id
, reply
, NULL
);
289 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
292 enum stone color
= str2stone(arg
);
295 if (!ti
[color
].len
.t
.timer_start
) {
296 /* First game move. */
297 time_start_timer(&ti
[color
]);
302 cf
= fbook_check(board
);
306 c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
308 struct move m
= { *c
, color
};
309 if (board_play(board
, &m
) < 0) {
310 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
313 char *str
= coord2str(*c
, board
);
315 fprintf(stderr
, "playing move %s\n", str
);
316 if (DEBUGL(1) && debug_boardprint
) {
317 board_print_custom(board
, stderr
, engine
->printhook
);
319 gtp_reply(id
, str
, NULL
);
320 free(str
); coord_done(c
);
322 /* Account for spent time. If our GTP peer keeps our clock, this will
323 * be overriden by next time_left GTP command properly. */
324 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
325 * should be absolutely rare situation and we will just spend a little
326 * less time than we could on next few moves.) */
327 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
328 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
330 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
333 enum stone color
= str2stone(arg
);
337 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
338 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
339 &stats
, &stats_size
);
341 gtp_error(id
, "genmoves error", NULL
);
345 fprintf(stderr
, "proposing moves %s\n", reply
);
346 if (DEBUGL(4) && debug_boardprint
)
347 board_print_custom(board
, stderr
, engine
->printhook
);
348 gtp_reply(id
, reply
, NULL
);
349 if (stats_size
> 0) {
350 double start
= time_now();
351 fwrite(stats
, 1, stats_size
, stdout
);
354 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
355 stats_size
, (time_now() - start
)*1000);
358 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
365 coord_t
*c
= str2coord(arg
, board_size(board
));
366 m
.coord
= *c
; coord_done(c
);
368 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
370 if (board_play(board
, &m
) < 0) {
372 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
373 gtp_error(id
, "illegal move", NULL
);
378 if (DEBUGL(1) && debug_boardprint
)
379 board_print(board
, stderr
);
382 /* TODO: Engine should choose free handicap; however, it tends to take
383 * overly long to think it all out, and unless it's clever its
384 * handicap stones won't be of much help. ;-) */
385 } else if (!strcasecmp(cmd
, "place_free_handicap")
386 || !strcasecmp(cmd
, "fixed_handicap")) {
389 int stones
= atoi(arg
);
392 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
393 if (DEBUGL(1) && debug_boardprint
)
394 board_print(board
, stderr
);
395 if (id
== NO_REPLY
) return P_OK
;
399 } else if (!strcasecmp(cmd
, "final_score")) {
400 struct move_queue q
= { .moves
= 0 };
401 if (engine
->dead_group_list
)
402 engine
->dead_group_list(engine
, board
, &q
);
403 floating_t score
= board_official_score(board
, &q
);
406 fprintf(stderr
, "counted score %.1f\n", score
);
408 gtp_reply(id
, "0", NULL
);
409 } else if (score
> 0) {
410 snprintf(str
, 64, "W+%.1f", score
);
411 gtp_reply(id
, str
, NULL
);
413 snprintf(str
, 64, "B+%.1f", -score
);
414 gtp_reply(id
, str
, NULL
);
417 /* XXX: This is a huge hack. */
418 } else if (!strcasecmp(cmd
, "final_status_list")) {
419 if (id
== NO_REPLY
) return P_OK
;
422 struct move_queue q
= { .moves
= 0 };
423 if (engine
->dead_group_list
)
424 engine
->dead_group_list(engine
, board
, &q
);
425 /* else we return empty list - i.e. engine not supporting
426 * this assumes all stones alive at the game end. */
427 if (!strcasecmp(arg
, "dead")) {
429 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
430 foreach_in_group(board
, q
.move
[i
]) {
431 printf("%s ", coord2sstr(c
, board
));
432 } foreach_in_group_end
;
438 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
440 bool printed_group
= false;
441 foreach_point(board
) { // foreach_group, effectively
442 group_t g
= group_at(board
, c
);
443 if (!g
|| g
!= c
) continue;
445 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
449 foreach_in_group(board
, g
) {
450 printf("%s ", coord2sstr(c
, board
));
451 } foreach_in_group_end
;
453 printed_group
= true;
460 gtp_error(id
, "illegal status specifier", NULL
);
463 } else if (!strcasecmp(cmd
, "undo")) {
464 if (board_undo(board
) < 0) {
466 fprintf(stderr
, "undo on non-pass move %s\n", coord2sstr(board
->last_move
.coord
, board
));
467 board_print(board
, stderr
);
469 gtp_error(id
, "cannot undo", NULL
);
474 reply
= engine
->undo(engine
, board
);
475 if (DEBUGL(1) && debug_boardprint
)
476 board_print(board
, stderr
);
477 gtp_reply(id
, reply
, NULL
);
479 /* Custom commands for handling UCT opening tbook */
480 } else if (!strcasecmp(cmd
, "uct_gentbook")) {
481 /* Board must be initialized properly, as if for genmove;
482 * makes sense only as 'uct_gentbook b'. */
485 enum stone color
= str2stone(arg
);
486 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
489 gtp_error(id
, "error generating tbook", NULL
);
491 } else if (!strcasecmp(cmd
, "uct_dumptbook")) {
494 enum stone color
= str2stone(arg
);
495 uct_dumptbook(engine
, board
, color
);
498 } else if (!strcasecmp(cmd
, "uct_evaluate")) {
501 enum stone color
= str2stone(arg
);
504 /* Iterate through the list of all free coordinates
505 * and call uct_evaluate() for each. uct_evaluate()
506 * will throw NAN in case of invalid moves and such. */
507 for (int i
= 0; i
< board
->flen
; i
++) {
508 floating_t val
= uct_evaluate(engine
, board
, &ti
[color
], board
->f
[i
], color
);
511 printf("%s %1.3f\n", coord2sstr(board
->f
[i
], board
), (double) val
);
515 } else if (!strcasecmp(cmd
, "pachi-result")) {
516 /* More detailed result of the last genmove. */
517 /* For UCT, the output format is: = color move playouts winrate dynkomi */
520 reply
= engine
->result(engine
, board
);
522 gtp_reply(id
, reply
, NULL
);
524 gtp_error(id
, "unknown pachi-result command", NULL
);
526 } else if (!strcasecmp(cmd
, "kgs-chat")) {
535 reply
= engine
->chat(engine
, board
, msg
);
537 gtp_reply(id
, reply
, NULL
);
539 gtp_error(id
, "unknown kgs-chat command", NULL
);
541 } else if (!strcasecmp(cmd
, "time_left")) {
544 enum stone color
= str2stone(arg
);
546 int time
= atoi(arg
);
548 int stones
= atoi(arg
);
549 if (!ti
[color
].ignore_gtp
) {
550 time_left(&ti
[color
], time
, stones
);
552 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
557 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
560 if (!strcasecmp(cmd
, "kgs-time_settings")) {
561 next_tok(time_system
);
563 time_system
= "canadian";
566 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
567 if (!strcasecmp(time_system
, "none")) {
569 } else if (!strcasecmp(time_system
, "absolute")) {
571 main_time
= atoi(arg
);
572 } else if (!strcasecmp(time_system
, "byoyomi")) {
574 main_time
= atoi(arg
);
576 byoyomi_time
= atoi(arg
);
578 byoyomi_periods
= atoi(arg
);
579 } else if (!strcasecmp(time_system
, "canadian")) {
581 main_time
= atoi(arg
);
583 byoyomi_time
= atoi(arg
);
585 byoyomi_stones
= atoi(arg
);
589 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
590 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
591 if (!ti
[S_BLACK
].ignore_gtp
) {
592 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
593 ti
[S_WHITE
] = ti
[S_BLACK
];
595 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
601 gtp_error(id
, "unknown command", NULL
);
602 return P_UNKNOWN_COMMAND
;