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 "pachi-genmoves_cleanup\n"
95 "place_free_handicap\n"
110 /* Return true if cmd is a valid gtp command. */
112 gtp_is_valid(char *cmd
)
114 if (!cmd
|| !*cmd
) return false;
115 char *s
= strcasestr(known_commands
, cmd
);
116 if (!s
) return false;
117 if (s
!= known_commands
&& s
[-1] != '\n') return false;
119 int len
= strlen(cmd
);
120 return s
[len
] == '\0' || s
[len
] == '\n';
123 /* XXX: THIS IS TOTALLY INSECURE!!!!
124 * Even basic input checking is missing. */
127 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
129 #define next_tok(to_) \
131 next = next + strcspn(next, " \t\r\n"); \
134 next += strspn(next, " \t\r\n"); \
137 if (strchr(buf
, '#'))
138 *strchr(buf
, '#') = 0;
140 char *cmd
, *next
= buf
;
152 if (!strcasecmp(cmd
, "protocol_version")) {
153 gtp_reply(id
, "2", NULL
);
156 } else if (!strcasecmp(cmd
, "name")) {
158 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
161 } else if (!strcasecmp(cmd
, "echo")) {
162 gtp_reply(id
, next
, NULL
);
165 } else if (!strcasecmp(cmd
, "version")) {
166 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, " Have a nice game!", NULL
);
169 } else if (!strcasecmp(cmd
, "list_commands")) {
170 gtp_reply(id
, known_commands
, NULL
);
173 } else if (!strcasecmp(cmd
, "known_command")) {
176 if (gtp_is_valid(arg
)) {
177 gtp_reply(id
, "true", NULL
);
179 gtp_reply(id
, "false", NULL
);
184 if (engine
->notify
&& gtp_is_valid(cmd
)) {
186 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
187 if (c
== P_NOREPLY
) {
189 } else if (c
== P_DONE_OK
) {
190 gtp_reply(id
, reply
, NULL
);
192 } else if (c
== P_DONE_ERROR
) {
193 gtp_error(id
, reply
, NULL
);
194 /* This is an internal error for the engine, but
195 * it is still OK from main's point of view. */
197 } else if (c
!= P_OK
) {
202 if (!strcasecmp(cmd
, "quit")) {
206 } else if (!strcasecmp(cmd
, "boardsize")) {
209 int size
= atoi(arg
);
210 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
211 gtp_error(id
, "illegal board size", NULL
);
214 board_resize(board
, size
);
217 return P_ENGINE_RESET
;
219 } else if (!strcasecmp(cmd
, "clear_board")) {
221 if (DEBUGL(3) && debug_boardprint
)
222 board_print(board
, stderr
);
224 return P_ENGINE_RESET
;
226 } else if (!strcasecmp(cmd
, "kgs-game_over")) {
227 /* The game may not be really over, just adjourned.
228 * Do not clear the board to avoid illegal moves
229 * if the game is resumed immediately after. KGS
230 * may start directly with genmove on resumption. */
232 fprintf(stderr
, "game is over\n");
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 (!strcasecmp(arg
, "japanese")) {
253 board
->rules
= RULES_JAPANESE
;
254 } else if (!strcasecmp(arg
, "chinese")) {
255 board
->rules
= RULES_CHINESE
;
256 } else if (!strcasecmp(arg
, "aga")) {
257 board
->rules
= RULES_AGA
;
258 } else if (!strcasecmp(arg
, "new_zealand")) {
259 board
->rules
= RULES_NEW_ZEALAND
;
261 gtp_error(id
, "unknown rules", NULL
);
266 } else if (!strcasecmp(cmd
, "play")) {
271 m
.color
= str2stone(arg
);
273 coord_t
*c
= str2coord(arg
, board_size(board
));
274 m
.coord
= *c
; coord_done(c
);
278 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
280 // This is where kgs starts the timer, not at genmove!
281 time_start_timer(&ti
[stone_other(m
.color
)]);
283 if (engine
->notify_play
)
284 reply
= engine
->notify_play(engine
, board
, &m
);
285 if (board_play(board
, &m
) < 0) {
287 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
288 board_print(board
, stderr
);
290 gtp_error(id
, "illegal move", NULL
);
292 if (DEBUGL(4) && debug_boardprint
)
293 board_print_custom(board
, stderr
, engine
->printhook
);
294 gtp_reply(id
, reply
, NULL
);
297 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
300 enum stone color
= str2stone(arg
);
302 if (DEBUGL(2) && debug_boardprint
)
303 board_print_custom(board
, stderr
, engine
->printhook
);
305 if (!ti
[color
].len
.t
.timer_start
) {
306 /* First game move. */
307 time_start_timer(&ti
[color
]);
312 cf
= fbook_check(board
);
316 c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
318 struct move m
= { *c
, color
};
319 if (board_play(board
, &m
) < 0) {
320 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
323 char *str
= coord2str(*c
, board
);
325 fprintf(stderr
, "playing move %s\n", str
);
326 if (DEBUGL(1) && debug_boardprint
) {
327 board_print_custom(board
, stderr
, engine
->printhook
);
329 gtp_reply(id
, str
, NULL
);
330 free(str
); coord_done(c
);
332 /* Account for spent time. If our GTP peer keeps our clock, this will
333 * be overriden by next time_left GTP command properly. */
334 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
335 * should be absolutely rare situation and we will just spend a little
336 * less time than we could on next few moves.) */
337 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
338 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
340 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
343 enum stone color
= str2stone(arg
);
347 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
348 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
349 &stats
, &stats_size
);
351 gtp_error(id
, "genmoves error", NULL
);
355 fprintf(stderr
, "proposing moves %s\n", reply
);
356 if (DEBUGL(4) && debug_boardprint
)
357 board_print_custom(board
, stderr
, engine
->printhook
);
358 gtp_reply(id
, reply
, NULL
);
359 if (stats_size
> 0) {
360 double start
= time_now();
361 fwrite(stats
, 1, stats_size
, stdout
);
364 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
365 stats_size
, (time_now() - start
)*1000);
368 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
375 coord_t
*c
= str2coord(arg
, board_size(board
));
376 m
.coord
= *c
; coord_done(c
);
378 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
380 if (board_play(board
, &m
) < 0) {
382 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
383 gtp_error(id
, "illegal move", NULL
);
388 if (DEBUGL(1) && debug_boardprint
)
389 board_print(board
, stderr
);
392 /* TODO: Engine should choose free handicap; however, it tends to take
393 * overly long to think it all out, and unless it's clever its
394 * handicap stones won't be of much help. ;-) */
395 } else if (!strcasecmp(cmd
, "place_free_handicap")
396 || !strcasecmp(cmd
, "fixed_handicap")) {
399 int stones
= atoi(arg
);
402 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
403 if (DEBUGL(1) && debug_boardprint
)
404 board_print(board
, stderr
);
405 if (id
== NO_REPLY
) return P_OK
;
409 } else if (!strcasecmp(cmd
, "final_score")) {
410 struct move_queue q
= { .moves
= 0 };
411 if (engine
->dead_group_list
)
412 engine
->dead_group_list(engine
, board
, &q
);
413 floating_t score
= board_official_score(board
, &q
);
416 fprintf(stderr
, "counted score %.1f\n", score
);
418 gtp_reply(id
, "0", NULL
);
419 } else if (score
> 0) {
420 snprintf(str
, 64, "W+%.1f", score
);
421 gtp_reply(id
, str
, NULL
);
423 snprintf(str
, 64, "B+%.1f", -score
);
424 gtp_reply(id
, str
, NULL
);
427 /* XXX: This is a huge hack. */
428 } else if (!strcasecmp(cmd
, "final_status_list")) {
429 if (id
== NO_REPLY
) return P_OK
;
432 struct move_queue q
= { .moves
= 0 };
433 if (engine
->dead_group_list
)
434 engine
->dead_group_list(engine
, board
, &q
);
435 /* else we return empty list - i.e. engine not supporting
436 * this assumes all stones alive at the game end. */
437 if (!strcasecmp(arg
, "dead")) {
439 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
440 foreach_in_group(board
, q
.move
[i
]) {
441 printf("%s ", coord2sstr(c
, board
));
442 } foreach_in_group_end
;
448 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
450 bool printed_group
= false;
451 foreach_point(board
) { // foreach_group, effectively
452 group_t g
= group_at(board
, c
);
453 if (!g
|| g
!= c
) continue;
455 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
459 foreach_in_group(board
, g
) {
460 printf("%s ", coord2sstr(c
, board
));
461 } foreach_in_group_end
;
463 printed_group
= true;
470 gtp_error(id
, "illegal status specifier", NULL
);
473 } else if (!strcasecmp(cmd
, "undo")) {
474 if (board_undo(board
) < 0) {
476 fprintf(stderr
, "undo on non-pass move %s\n", coord2sstr(board
->last_move
.coord
, board
));
477 board_print(board
, stderr
);
479 gtp_error(id
, "cannot undo", NULL
);
484 reply
= engine
->undo(engine
, board
);
485 if (DEBUGL(3) && debug_boardprint
)
486 board_print(board
, stderr
);
487 gtp_reply(id
, reply
, NULL
);
489 /* Custom commands for handling the tree opening tbook */
490 } else if (!strcasecmp(cmd
, "pachi-gentbook")) {
491 /* Board must be initialized properly, as if for genmove;
492 * makes sense only as 'uct_gentbook b'. */
495 enum stone color
= str2stone(arg
);
496 if (uct_gentbook(engine
, board
, &ti
[color
], color
))
499 gtp_error(id
, "error generating tbook", NULL
);
501 } else if (!strcasecmp(cmd
, "pachi-dumptbook")) {
504 enum stone color
= str2stone(arg
);
505 uct_dumptbook(engine
, board
, color
);
508 } else if (!strcasecmp(cmd
, "pachi-evaluate")) {
511 enum stone color
= str2stone(arg
);
513 if (!engine
->evaluate
) {
514 gtp_error(id
, "pachi-evaluate not supported by engine", NULL
);
517 floating_t vals
[board
->flen
];
518 engine
->evaluate(engine
, board
, &ti
[color
], vals
, color
);
519 for (int i
= 0; i
< board
->flen
; i
++) {
520 if (!board_coord_in_symmetry(board
, board
->f
[i
])
523 printf("%s %1.3f\n", coord2sstr(board
->f
[i
], board
), (double) vals
[i
]);
528 } else if (!strcasecmp(cmd
, "pachi-result")) {
529 /* More detailed result of the last genmove. */
530 /* For UCT, the output format is: = color move playouts winrate dynkomi */
533 reply
= engine
->result(engine
, board
);
535 gtp_reply(id
, reply
, NULL
);
537 gtp_error(id
, "unknown pachi-result command", NULL
);
539 } else if (!strcasecmp(cmd
, "kgs-chat")) {
548 reply
= engine
->chat(engine
, board
, msg
);
550 gtp_reply(id
, reply
, NULL
);
552 gtp_error(id
, "unknown kgs-chat command", NULL
);
554 } else if (!strcasecmp(cmd
, "time_left")) {
557 enum stone color
= str2stone(arg
);
559 int time
= atoi(arg
);
561 int stones
= atoi(arg
);
562 if (!ti
[color
].ignore_gtp
) {
563 time_left(&ti
[color
], time
, stones
);
565 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
570 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
573 if (!strcasecmp(cmd
, "kgs-time_settings")) {
574 next_tok(time_system
);
576 time_system
= "canadian";
579 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
580 if (!strcasecmp(time_system
, "none")) {
582 } else if (!strcasecmp(time_system
, "absolute")) {
584 main_time
= atoi(arg
);
585 } else if (!strcasecmp(time_system
, "byoyomi")) {
587 main_time
= atoi(arg
);
589 byoyomi_time
= atoi(arg
);
591 byoyomi_periods
= atoi(arg
);
592 } else if (!strcasecmp(time_system
, "canadian")) {
594 main_time
= atoi(arg
);
596 byoyomi_time
= atoi(arg
);
598 byoyomi_stones
= atoi(arg
);
602 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
603 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
604 if (!ti
[S_BLACK
].ignore_gtp
) {
605 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
606 ti
[S_WHITE
] = ti
[S_BLACK
];
608 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
614 gtp_error(id
, "unknown command", NULL
);
615 return P_UNKNOWN_COMMAND
;