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"
101 /* Return true if cmd is a valid gtp command. */
103 gtp_is_valid(char *cmd
)
105 if (!cmd
|| !*cmd
) return false;
106 char *s
= strcasestr(known_commands
, cmd
);
107 if (!s
) return false;
109 int len
= strlen(cmd
);
110 return s
[len
] == '\0' || s
[len
] == '\n';
113 /* XXX: THIS IS TOTALLY INSECURE!!!!
114 * Even basic input checking is missing. */
117 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
119 #define next_tok(to_) \
121 next = next + strcspn(next, " \t\r\n"); \
124 next += strspn(next, " \t\r\n"); \
127 if (strchr(buf
, '#'))
128 *strchr(buf
, '#') = 0;
130 char *cmd
, *next
= buf
;
142 if (!strcasecmp(cmd
, "protocol_version")) {
143 gtp_reply(id
, "2", NULL
);
146 } else if (!strcasecmp(cmd
, "name")) {
148 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
151 } else if (!strcasecmp(cmd
, "echo")) {
152 gtp_reply(id
, next
, NULL
);
155 } else if (!strcasecmp(cmd
, "version")) {
156 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
159 } else if (!strcasecmp(cmd
, "list_commands")) {
160 gtp_reply(id
, known_commands
, NULL
);
164 if (engine
->notify
) {
166 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
167 if (c
== P_NOREPLY
) {
169 } else if (c
== P_DONE_OK
) {
170 gtp_reply(id
, reply
, NULL
);
172 } else if (c
== P_DONE_ERROR
) {
173 gtp_error(id
, reply
, NULL
);
174 /* This is an internal error for the engine, but
175 * it is still OK from main's point of view. */
177 } else if (c
!= P_OK
) {
182 if (!strcasecmp(cmd
, "quit")) {
186 } else if (!strcasecmp(cmd
, "boardsize")) {
189 int size
= atoi(arg
);
190 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
191 gtp_error(id
, "illegal board size", NULL
);
194 board_resize(board
, size
);
197 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
200 board_print(board
, stderr
);
201 if (!strcasecmp(cmd
, "kgs-game_over")) {
203 fprintf(stderr
, "game is over\n");
204 /* Sleep before replying, so that kgs doesn't
205 * start another game immediately. */
206 sleep(GAME_OVER_SLEEP
);
209 return P_ENGINE_RESET
;
211 } else if (!strcasecmp(cmd
, "komi")) {
214 sscanf(arg
, "%f", &board
->komi
);
217 board_print(board
, stderr
);
220 } else if (!strcasecmp(cmd
, "kgs-rules")) {
223 if (!strcasecmp(arg
, "japanese")) {
224 board
->rules
= RULES_JAPANESE
;
225 } else if (!strcasecmp(arg
, "chinese")) {
226 board
->rules
= RULES_CHINESE
;
227 } else if (!strcasecmp(arg
, "aga")) {
228 board
->rules
= RULES_AGA
;
229 } else if (!strcasecmp(arg
, "new_zealand")) {
230 board
->rules
= RULES_NEW_ZEALAND
;
232 gtp_error(id
, "unknown rules", NULL
);
237 } else if (!strcasecmp(cmd
, "play")) {
242 m
.color
= str2stone(arg
);
244 coord_t
*c
= str2coord(arg
, board_size(board
));
245 m
.coord
= *c
; coord_done(c
);
249 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
251 // This is where kgs starts the timer, not at genmove!
252 time_start_timer(&ti
[stone_other(m
.color
)]);
254 if (engine
->notify_play
)
255 reply
= engine
->notify_play(engine
, board
, &m
);
256 if (board_play(board
, &m
) < 0) {
258 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
259 board_print(board
, stderr
);
261 gtp_error(id
, "illegal move", NULL
);
264 board_print_custom(board
, stderr
, engine
->printhook
);
265 gtp_reply(id
, reply
, NULL
);
268 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
271 enum stone color
= str2stone(arg
);
274 if (!ti
[color
].len
.t
.timer_start
) {
275 /* First game move. */
276 time_start_timer(&ti
[color
]);
280 /* We have an fbook, check if we cannot make
281 * a move along it right away. */
282 hash_t hi
= board
->hash
;
284 while (!is_pass(board
->fbook
->moves
[hi
& fbook_hash_mask
])) {
285 if (board
->fbook
->hashes
[hi
& fbook_hash_mask
] == board
->hash
) {
286 cf
= board
->fbook
->moves
[hi
& fbook_hash_mask
];
293 fprintf(stderr
, "fbook match %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
296 /* No match, also prevent further fbook usage
297 * until the next clear_board. */
299 fprintf(stderr
, "fbook out %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
300 fbook_done(board
->fbook
);
305 if (!c
) c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
307 struct move m
= { *c
, color
};
308 if (board_play(board
, &m
) < 0) {
309 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
312 char *str
= coord2str(*c
, board
);
314 fprintf(stderr
, "playing move %s\n", str
);
316 board_print_custom(board
, stderr
, engine
->printhook
);
318 gtp_reply(id
, str
, NULL
);
319 free(str
); coord_done(c
);
321 /* Account for spent time. If our GTP peer keeps our clock, this will
322 * be overriden by next time_left GTP command properly. */
323 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
324 * should be absolutely rare situation and we will just spend a little
325 * less time than we could on next few moves.) */
326 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
327 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
329 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
332 enum stone color
= str2stone(arg
);
336 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
337 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
338 &stats
, &stats_size
);
340 gtp_error(id
, "genmoves error", NULL
);
344 fprintf(stderr
, "proposing moves %s\n", reply
);
346 board_print_custom(board
, stderr
, engine
->printhook
);
347 gtp_reply(id
, reply
, NULL
);
348 if (stats_size
> 0) {
349 double start
= time_now();
350 fwrite(stats
, 1, stats_size
, stdout
);
353 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
354 stats_size
, (time_now() - start
)*1000);
357 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
364 coord_t
*c
= str2coord(arg
, board_size(board
));
365 m
.coord
= *c
; coord_done(c
);
367 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
369 if (board_play(board
, &m
) < 0) {
371 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
372 gtp_error(id
, "illegal move", NULL
);
378 board_print(board
, stderr
);
381 /* TODO: Engine should choose free handicap; however, it tends to take
382 * overly long to think it all out, and unless it's clever its
383 * handicap stones won't be of much help. ;-) */
384 } else if (!strcasecmp(cmd
, "place_free_handicap")
385 || !strcasecmp(cmd
, "fixed_handicap")) {
388 int stones
= atoi(arg
);
391 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
393 board_print(board
, stderr
);
394 if (id
== NO_REPLY
) return P_OK
;
398 } else if (!strcasecmp(cmd
, "final_score")) {
399 struct move_queue q
= { .moves
= 0 };
400 if (engine
->dead_group_list
)
401 engine
->dead_group_list(engine
, board
, &q
);
402 float score
= board_official_score(board
, &q
);
405 fprintf(stderr
, "counted score %.1f\n", score
);
407 gtp_reply(id
, "0", NULL
);
408 } else if (score
> 0) {
409 snprintf(str
, 64, "W+%.1f", score
);
410 gtp_reply(id
, str
, NULL
);
412 snprintf(str
, 64, "B+%.1f", -score
);
413 gtp_reply(id
, str
, NULL
);
416 /* XXX: This is a huge hack. */
417 } else if (!strcasecmp(cmd
, "final_status_list")) {
418 if (id
== NO_REPLY
) return P_OK
;
421 struct move_queue q
= { .moves
= 0 };
422 if (engine
->dead_group_list
)
423 engine
->dead_group_list(engine
, board
, &q
);
424 /* else we return empty list - i.e. engine not supporting
425 * this assumes all stones alive at the game end. */
426 if (!strcasecmp(arg
, "dead")) {
428 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
429 foreach_in_group(board
, q
.move
[i
]) {
430 printf("%s ", coord2sstr(c
, board
));
431 } foreach_in_group_end
;
437 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
439 bool printed_group
= false;
440 foreach_point(board
) { // foreach_group, effectively
441 group_t g
= group_at(board
, c
);
442 if (!g
|| g
!= c
) continue;
444 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
448 foreach_in_group(board
, g
) {
449 printf("%s ", coord2sstr(c
, board
));
450 } foreach_in_group_end
;
452 printed_group
= true;
459 gtp_error(id
, "illegal status specifier", NULL
);
462 /* Custom commands for handling UCT opening tbook */
463 } else if (!strcasecmp(cmd
, "uct_gentbook")) {
464 /* Board must be initialized properly, as if for genmove;
465 * makes sense only as 'uct_gentbook b'. */
468 enum stone color
= str2stone(arg
);
469 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
472 gtp_error(id
, "error generating tbook", NULL
);
474 } else if (!strcasecmp(cmd
, "uct_dumptbook")) {
477 enum stone color
= str2stone(arg
);
478 uct_dumptbook(engine
, board
, color
);
481 } else if (!strcasecmp(cmd
, "uct_evaluate")) {
484 enum stone color
= str2stone(arg
);
487 /* Iterate through the list of all free coordinates
488 * and call uct_evaluate() for each. uct_evaluate()
489 * will throw NAN in case of invalid moves and such. */
490 for (int i
= 0; i
< board
->flen
; i
++) {
491 float val
= uct_evaluate(engine
, board
, &ti
[color
], board
->f
[i
], color
);
494 printf("%s %1.3f\n", coord2sstr(board
->f
[i
], board
), (double) val
);
498 } else if (!strcasecmp(cmd
, "pachi-result")) {
499 /* More detailed result of the last genmove. */
500 /* For UCT, the output format is: = color move playouts winrate dynkomi */
503 reply
= engine
->result(engine
, board
);
505 gtp_reply(id
, reply
, NULL
);
507 gtp_error(id
, "unknown pachi-result command", NULL
);
509 } else if (!strcasecmp(cmd
, "kgs-chat")) {
518 reply
= engine
->chat(engine
, board
, msg
);
520 gtp_reply(id
, reply
, NULL
);
522 gtp_error(id
, "unknown kgs-chat command", NULL
);
524 } else if (!strcasecmp(cmd
, "time_left")) {
527 enum stone color
= str2stone(arg
);
529 int time
= atoi(arg
);
531 int stones
= atoi(arg
);
532 if (!ti
[color
].ignore_gtp
) {
533 time_left(&ti
[color
], time
, stones
);
535 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
540 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
543 if (!strcasecmp(cmd
, "kgs-time_settings")) {
544 next_tok(time_system
);
546 time_system
= "canadian";
549 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
550 if (!strcasecmp(time_system
, "none")) {
552 } else if (!strcasecmp(time_system
, "absolute")) {
554 main_time
= atoi(arg
);
555 } else if (!strcasecmp(time_system
, "byoyomi")) {
557 main_time
= atoi(arg
);
559 byoyomi_time
= atoi(arg
);
561 byoyomi_periods
= atoi(arg
);
562 } else if (!strcasecmp(time_system
, "canadian")) {
564 main_time
= atoi(arg
);
566 byoyomi_time
= atoi(arg
);
568 byoyomi_stones
= atoi(arg
);
572 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
573 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
574 if (!ti
[S_BLACK
].ignore_gtp
) {
575 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
576 ti
[S_WHITE
] = ti
[S_BLACK
];
578 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
584 gtp_error(id
, "unknown command", NULL
);
585 return P_UNKNOWN_COMMAND
;