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
=
90 "kgs-genmove_cleanup\n"
92 "place_free_handicap\n"
102 /* Return true if cmd is a valid gtp command. */
104 gtp_is_valid(char *cmd
)
106 if (!cmd
|| !*cmd
) return false;
107 char *s
= strcasestr(known_commands
, cmd
);
108 if (!s
) return false;
110 int len
= strlen(cmd
);
111 return s
[len
] == '\0' || s
[len
] == '\n';
114 /* XXX: THIS IS TOTALLY INSECURE!!!!
115 * Even basic input checking is missing. */
118 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
120 #define next_tok(to_) \
122 next = next + strcspn(next, " \t\r\n"); \
125 next += strspn(next, " \t\r\n"); \
128 if (strchr(buf
, '#'))
129 *strchr(buf
, '#') = 0;
131 char *cmd
, *next
= buf
;
143 if (!strcasecmp(cmd
, "protocol_version")) {
144 gtp_reply(id
, "2", NULL
);
147 } else if (!strcasecmp(cmd
, "name")) {
149 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
152 } else if (!strcasecmp(cmd
, "echo")) {
153 gtp_reply(id
, next
, NULL
);
156 } else if (!strcasecmp(cmd
, "version")) {
157 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
160 } else if (!strcasecmp(cmd
, "list_commands")) {
161 gtp_reply(id
, known_commands
, NULL
);
165 if (engine
->notify
&& gtp_is_valid(cmd
)) {
167 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
168 if (c
== P_NOREPLY
) {
170 } else if (c
== P_DONE_OK
) {
171 gtp_reply(id
, reply
, NULL
);
173 } else if (c
== P_DONE_ERROR
) {
174 gtp_error(id
, reply
, NULL
);
175 /* This is an internal error for the engine, but
176 * it is still OK from main's point of view. */
178 } else if (c
!= P_OK
) {
183 if (!strcasecmp(cmd
, "quit")) {
187 } else if (!strcasecmp(cmd
, "boardsize")) {
190 int size
= atoi(arg
);
191 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
192 gtp_error(id
, "illegal board size", NULL
);
195 board_resize(board
, size
);
198 } else if (!strcasecmp(cmd
, "clear_board")) {
200 if (DEBUGL(1) && debug_boardprint
)
201 board_print(board
, stderr
);
203 return P_ENGINE_RESET
;
205 } else if (!strcasecmp(cmd
, "kgs-game_over")) {
206 /* The game may not be really over, just adjourned.
207 * Do not clear the board to avoid illegal moves
208 * if the game is resumed immediately after. KGS
209 * may start directly with genmove on resumption. */
211 fprintf(stderr
, "game is over\n");
214 /* Sleep before replying, so that kgs doesn't
215 * start another game immediately. */
216 sleep(GAME_OVER_SLEEP
);
219 } else if (!strcasecmp(cmd
, "komi")) {
222 sscanf(arg
, PRIfloating
, &board
->komi
);
224 if (DEBUGL(1 && debug_boardprint
))
225 board_print(board
, stderr
);
228 } else if (!strcasecmp(cmd
, "kgs-rules")) {
231 if (!strcasecmp(arg
, "japanese")) {
232 board
->rules
= RULES_JAPANESE
;
233 } else if (!strcasecmp(arg
, "chinese")) {
234 board
->rules
= RULES_CHINESE
;
235 } else if (!strcasecmp(arg
, "aga")) {
236 board
->rules
= RULES_AGA
;
237 } else if (!strcasecmp(arg
, "new_zealand")) {
238 board
->rules
= RULES_NEW_ZEALAND
;
240 gtp_error(id
, "unknown rules", NULL
);
245 } else if (!strcasecmp(cmd
, "play")) {
250 m
.color
= str2stone(arg
);
252 coord_t
*c
= str2coord(arg
, board_size(board
));
253 m
.coord
= *c
; coord_done(c
);
257 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
259 // This is where kgs starts the timer, not at genmove!
260 time_start_timer(&ti
[stone_other(m
.color
)]);
262 if (engine
->notify_play
)
263 reply
= engine
->notify_play(engine
, board
, &m
);
264 if (board_play(board
, &m
) < 0) {
266 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
267 board_print(board
, stderr
);
269 gtp_error(id
, "illegal move", NULL
);
271 if (DEBUGL(1) && debug_boardprint
)
272 board_print_custom(board
, stderr
, engine
->printhook
);
273 gtp_reply(id
, reply
, NULL
);
276 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
279 enum stone color
= str2stone(arg
);
282 if (!ti
[color
].len
.t
.timer_start
) {
283 /* First game move. */
284 time_start_timer(&ti
[color
]);
289 cf
= fbook_check(board
);
293 c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
295 struct move m
= { *c
, color
};
296 if (board_play(board
, &m
) < 0) {
297 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
300 char *str
= coord2str(*c
, board
);
302 fprintf(stderr
, "playing move %s\n", str
);
303 if (DEBUGL(1) && debug_boardprint
) {
304 board_print_custom(board
, stderr
, engine
->printhook
);
306 gtp_reply(id
, str
, NULL
);
307 free(str
); coord_done(c
);
309 /* Account for spent time. If our GTP peer keeps our clock, this will
310 * be overriden by next time_left GTP command properly. */
311 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
312 * should be absolutely rare situation and we will just spend a little
313 * less time than we could on next few moves.) */
314 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
315 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
317 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
320 enum stone color
= str2stone(arg
);
324 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
325 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
326 &stats
, &stats_size
);
328 gtp_error(id
, "genmoves error", NULL
);
332 fprintf(stderr
, "proposing moves %s\n", reply
);
333 if (DEBUGL(4) && debug_boardprint
)
334 board_print_custom(board
, stderr
, engine
->printhook
);
335 gtp_reply(id
, reply
, NULL
);
336 if (stats_size
> 0) {
337 double start
= time_now();
338 fwrite(stats
, 1, stats_size
, stdout
);
341 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
342 stats_size
, (time_now() - start
)*1000);
345 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
352 coord_t
*c
= str2coord(arg
, board_size(board
));
353 m
.coord
= *c
; coord_done(c
);
355 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
357 if (board_play(board
, &m
) < 0) {
359 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
360 gtp_error(id
, "illegal move", NULL
);
365 if (DEBUGL(1) && debug_boardprint
)
366 board_print(board
, stderr
);
369 /* TODO: Engine should choose free handicap; however, it tends to take
370 * overly long to think it all out, and unless it's clever its
371 * handicap stones won't be of much help. ;-) */
372 } else if (!strcasecmp(cmd
, "place_free_handicap")
373 || !strcasecmp(cmd
, "fixed_handicap")) {
376 int stones
= atoi(arg
);
379 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
380 if (DEBUGL(1) && debug_boardprint
)
381 board_print(board
, stderr
);
382 if (id
== NO_REPLY
) return P_OK
;
386 } else if (!strcasecmp(cmd
, "final_score")) {
387 struct move_queue q
= { .moves
= 0 };
388 if (engine
->dead_group_list
)
389 engine
->dead_group_list(engine
, board
, &q
);
390 floating_t score
= board_official_score(board
, &q
);
393 fprintf(stderr
, "counted score %.1f\n", score
);
395 gtp_reply(id
, "0", NULL
);
396 } else if (score
> 0) {
397 snprintf(str
, 64, "W+%.1f", score
);
398 gtp_reply(id
, str
, NULL
);
400 snprintf(str
, 64, "B+%.1f", -score
);
401 gtp_reply(id
, str
, NULL
);
404 /* XXX: This is a huge hack. */
405 } else if (!strcasecmp(cmd
, "final_status_list")) {
406 if (id
== NO_REPLY
) return P_OK
;
409 struct move_queue q
= { .moves
= 0 };
410 if (engine
->dead_group_list
)
411 engine
->dead_group_list(engine
, board
, &q
);
412 /* else we return empty list - i.e. engine not supporting
413 * this assumes all stones alive at the game end. */
414 if (!strcasecmp(arg
, "dead")) {
416 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
417 foreach_in_group(board
, q
.move
[i
]) {
418 printf("%s ", coord2sstr(c
, board
));
419 } foreach_in_group_end
;
425 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
427 bool printed_group
= false;
428 foreach_point(board
) { // foreach_group, effectively
429 group_t g
= group_at(board
, c
);
430 if (!g
|| g
!= c
) continue;
432 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
436 foreach_in_group(board
, g
) {
437 printf("%s ", coord2sstr(c
, board
));
438 } foreach_in_group_end
;
440 printed_group
= true;
447 gtp_error(id
, "illegal status specifier", NULL
);
450 } else if (!strcasecmp(cmd
, "undo")) {
451 if (board_undo(board
) < 0) {
453 fprintf(stderr
, "undo on non-pass move %s\n", coord2sstr(board
->last_move
.coord
, board
));
454 board_print(board
, stderr
);
456 gtp_error(id
, "cannot undo", NULL
);
461 reply
= engine
->undo(engine
, board
);
462 if (DEBUGL(1) && debug_boardprint
)
463 board_print(board
, stderr
);
464 gtp_reply(id
, reply
, NULL
);
466 /* Custom commands for handling UCT opening tbook */
467 } else if (!strcasecmp(cmd
, "uct_gentbook")) {
468 /* Board must be initialized properly, as if for genmove;
469 * makes sense only as 'uct_gentbook b'. */
472 enum stone color
= str2stone(arg
);
473 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
476 gtp_error(id
, "error generating tbook", NULL
);
478 } else if (!strcasecmp(cmd
, "uct_dumptbook")) {
481 enum stone color
= str2stone(arg
);
482 uct_dumptbook(engine
, board
, color
);
485 } else if (!strcasecmp(cmd
, "uct_evaluate")) {
488 enum stone color
= str2stone(arg
);
491 /* Iterate through the list of all free coordinates
492 * and call uct_evaluate() for each. uct_evaluate()
493 * will throw NAN in case of invalid moves and such. */
494 for (int i
= 0; i
< board
->flen
; i
++) {
495 floating_t val
= uct_evaluate(engine
, board
, &ti
[color
], board
->f
[i
], color
);
498 printf("%s %1.3f\n", coord2sstr(board
->f
[i
], board
), (double) val
);
502 } else if (!strcasecmp(cmd
, "pachi-result")) {
503 /* More detailed result of the last genmove. */
504 /* For UCT, the output format is: = color move playouts winrate dynkomi */
507 reply
= engine
->result(engine
, board
);
509 gtp_reply(id
, reply
, NULL
);
511 gtp_error(id
, "unknown pachi-result command", NULL
);
513 } else if (!strcasecmp(cmd
, "kgs-chat")) {
522 reply
= engine
->chat(engine
, board
, msg
);
524 gtp_reply(id
, reply
, NULL
);
526 gtp_error(id
, "unknown kgs-chat command", NULL
);
528 } else if (!strcasecmp(cmd
, "time_left")) {
531 enum stone color
= str2stone(arg
);
533 int time
= atoi(arg
);
535 int stones
= atoi(arg
);
536 if (!ti
[color
].ignore_gtp
) {
537 time_left(&ti
[color
], time
, stones
);
539 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
544 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
547 if (!strcasecmp(cmd
, "kgs-time_settings")) {
548 next_tok(time_system
);
550 time_system
= "canadian";
553 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
554 if (!strcasecmp(time_system
, "none")) {
556 } else if (!strcasecmp(time_system
, "absolute")) {
558 main_time
= atoi(arg
);
559 } else if (!strcasecmp(time_system
, "byoyomi")) {
561 main_time
= atoi(arg
);
563 byoyomi_time
= atoi(arg
);
565 byoyomi_periods
= atoi(arg
);
566 } else if (!strcasecmp(time_system
, "canadian")) {
568 main_time
= atoi(arg
);
570 byoyomi_time
= atoi(arg
);
572 byoyomi_stones
= atoi(arg
);
576 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
577 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
578 if (!ti
[S_BLACK
].ignore_gtp
) {
579 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
580 ti
[S_WHITE
] = ti
[S_BLACK
];
582 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
588 gtp_error(id
, "unknown command", NULL
);
589 return P_UNKNOWN_COMMAND
;