21 /* Sleep 5 seconds after a game ends to give time to kill the program. */
22 #define GAME_OVER_SLEEP 5
25 gtp_prefix(char prefix
, int id
)
27 if (id
== NO_REPLY
) return;
29 printf("%c%d ", prefix
, id
);
31 printf("%c ", prefix
);
42 gtp_output(char prefix
, int id
, va_list params
)
44 if (id
== NO_REPLY
) return;
45 gtp_prefix(prefix
, id
);
47 while ((s
= va_arg(params
, char *))) {
55 gtp_reply(int id
, ...)
59 gtp_output('=', id
, params
);
64 gtp_error(int id
, ...)
68 gtp_output('?', id
, params
);
72 /* List of known gtp commands. The internal command pachi-genmoves is not exported,
73 * it should only be used between master and slaves of the distributed engine. */
74 static char *known_commands
=
88 "kgs-genmove_cleanup\n"
90 "place_free_handicap\n"
99 /* Return true if cmd is a valid gtp command. */
101 gtp_is_valid(char *cmd
)
103 if (!cmd
|| !*cmd
) return false;
104 char *s
= strcasestr(known_commands
, cmd
);
105 if (!s
) return false;
107 int len
= strlen(cmd
);
108 return s
[len
] == '\0' || s
[len
] == '\n';
111 /* XXX: THIS IS TOTALLY INSECURE!!!!
112 * Even basic input checking is missing. */
115 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
117 #define next_tok(to_) \
119 next = next + strcspn(next, " \t\r\n"); \
122 next += strspn(next, " \t\r\n"); \
125 if (strchr(buf
, '#'))
126 *strchr(buf
, '#') = 0;
128 char *cmd
, *next
= buf
;
140 if (!strcasecmp(cmd
, "protocol_version")) {
141 gtp_reply(id
, "2", NULL
);
144 } else if (!strcasecmp(cmd
, "name")) {
146 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
149 } else if (!strcasecmp(cmd
, "echo")) {
150 gtp_reply(id
, next
, NULL
);
153 } else if (!strcasecmp(cmd
, "version")) {
154 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
157 } else if (!strcasecmp(cmd
, "list_commands")) {
158 gtp_reply(id
, known_commands
, NULL
);
162 if (engine
->notify
) {
164 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
165 if (c
== P_NOREPLY
) {
167 } else if (c
== P_DONE_OK
) {
168 gtp_reply(id
, reply
, NULL
);
170 } else if (c
== P_DONE_ERROR
) {
171 gtp_error(id
, reply
, NULL
);
172 /* This is an internal error for the engine, but
173 * it is still OK from main's point of view. */
175 } else if (c
!= P_OK
) {
180 if (!strcasecmp(cmd
, "quit")) {
184 } else if (!strcasecmp(cmd
, "boardsize")) {
187 int size
= atoi(arg
);
188 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
189 gtp_error(id
, "illegal board size", NULL
);
192 board_resize(board
, size
);
195 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
198 board_print(board
, stderr
);
199 if (!strcasecmp(cmd
, "kgs-game_over")) {
201 fprintf(stderr
, "game is over\n");
202 /* Sleep before replying, so that kgs doesn't
203 * start another game immediately. */
204 sleep(GAME_OVER_SLEEP
);
207 return P_ENGINE_RESET
;
209 } else if (!strcasecmp(cmd
, "komi")) {
212 sscanf(arg
, "%f", &board
->komi
);
215 board_print(board
, stderr
);
218 } else if (!strcasecmp(cmd
, "kgs-rules")) {
221 if (!strcasecmp(arg
, "japanese")) {
222 board
->rules
= RULES_JAPANESE
;
223 } else if (!strcasecmp(arg
, "chinese")) {
224 board
->rules
= RULES_CHINESE
;
225 } else if (!strcasecmp(arg
, "aga")) {
226 board
->rules
= RULES_AGA
;
227 } else if (!strcasecmp(arg
, "new_zealand")) {
228 board
->rules
= RULES_NEW_ZEALAND
;
230 gtp_error(id
, "unknown rules", NULL
);
235 } else if (!strcasecmp(cmd
, "play")) {
240 m
.color
= str2stone(arg
);
242 coord_t
*c
= str2coord(arg
, board_size(board
));
243 m
.coord
= *c
; coord_done(c
);
247 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
249 // This is where kgs starts the timer, not at genmove!
250 time_start_timer(&ti
[stone_other(m
.color
)]);
252 if (engine
->notify_play
)
253 reply
= engine
->notify_play(engine
, board
, &m
);
254 if (board_play(board
, &m
) < 0) {
256 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
257 board_print(board
, stderr
);
259 gtp_error(id
, "illegal move", NULL
);
262 board_print_custom(board
, stderr
, engine
->printhook
);
263 gtp_reply(id
, reply
, NULL
);
266 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
269 enum stone color
= str2stone(arg
);
271 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
272 struct move m
= { *c
, color
};
273 if (board_play(board
, &m
) < 0) {
274 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
277 char *str
= coord2str(*c
, board
);
279 fprintf(stderr
, "playing move %s\n", str
);
281 board_print_custom(board
, stderr
, engine
->printhook
);
283 gtp_reply(id
, str
, NULL
);
284 free(str
); coord_done(c
);
286 /* Account for spent time. If our GTP peer keeps our clock, this will
287 * be overriden by next time_left GTP command properly. */
288 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
289 * should be absolutely rare situation and we will just spend a little
290 * less time than we could on next few moves.) */
291 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
292 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
294 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
297 enum stone color
= str2stone(arg
);
301 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
302 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
303 &stats
, &stats_size
);
305 gtp_error(id
, "genmoves error", NULL
);
309 fprintf(stderr
, "proposing moves %s\n", reply
);
311 board_print_custom(board
, stderr
, engine
->printhook
);
312 gtp_reply(id
, reply
, NULL
);
313 if (stats_size
> 0) {
314 double start
= time_now();
315 fwrite(stats
, 1, stats_size
, stdout
);
318 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
319 stats_size
, (time_now() - start
)*1000);
322 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
329 coord_t
*c
= str2coord(arg
, board_size(board
));
330 m
.coord
= *c
; coord_done(c
);
332 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
334 if (board_play(board
, &m
) < 0) {
336 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
337 gtp_error(id
, "illegal move", NULL
);
343 board_print(board
, stderr
);
346 /* TODO: Engine should choose free handicap; however, it tends to take
347 * overly long to think it all out, and unless it's clever its
348 * handicap stones won't be of much help. ;-) */
349 } else if (!strcasecmp(cmd
, "place_free_handicap")
350 || !strcasecmp(cmd
, "fixed_handicap")) {
353 int stones
= atoi(arg
);
356 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
358 board_print(board
, stderr
);
359 if (id
== NO_REPLY
) return P_OK
;
363 } else if (!strcasecmp(cmd
, "final_score")) {
364 struct move_queue q
= { .moves
= 0 };
365 if (engine
->dead_group_list
)
366 engine
->dead_group_list(engine
, board
, &q
);
367 float score
= board_official_score(board
, &q
);
370 fprintf(stderr
, "counted score %.1f\n", score
);
372 gtp_reply(id
, "0", NULL
);
373 } else if (score
> 0) {
374 snprintf(str
, 64, "W+%.1f", score
);
375 gtp_reply(id
, str
, NULL
);
377 snprintf(str
, 64, "B+%.1f", -score
);
378 gtp_reply(id
, str
, NULL
);
381 /* XXX: This is a huge hack. */
382 } else if (!strcasecmp(cmd
, "final_status_list")) {
383 if (id
== NO_REPLY
) return P_OK
;
386 struct move_queue q
= { .moves
= 0 };
387 if (engine
->dead_group_list
)
388 engine
->dead_group_list(engine
, board
, &q
);
389 /* else we return empty list - i.e. engine not supporting
390 * this assumes all stones alive at the game end. */
391 if (!strcasecmp(arg
, "dead")) {
393 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
394 foreach_in_group(board
, q
.move
[i
]) {
395 printf("%s ", coord2sstr(c
, board
));
396 } foreach_in_group_end
;
402 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
404 bool printed_group
= false;
405 foreach_point(board
) { // foreach_group, effectively
406 group_t g
= group_at(board
, c
);
407 if (!g
|| g
!= c
) continue;
409 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
413 foreach_in_group(board
, g
) {
414 printf("%s ", coord2sstr(c
, board
));
415 } foreach_in_group_end
;
417 printed_group
= true;
424 gtp_error(id
, "illegal status specifier", NULL
);
427 /* Custom commands for handling UCT opening book */
428 } else if (!strcasecmp(cmd
, "uct_genbook")) {
429 /* Board must be initialized properly, as if for genmove;
430 * makes sense only as 'uct_genbook b'. */
433 enum stone color
= str2stone(arg
);
434 if (uct_genbook(engine
, board
, &ti
[color
], color
))
437 gtp_error(id
, "error generating book", NULL
);
439 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
442 enum stone color
= str2stone(arg
);
443 uct_dumpbook(engine
, board
, color
);
446 } else if (!strcasecmp(cmd
, "pachi-result")) {
447 /* More detailed result of the last genmove. */
448 /* For UCT, the output format is: = color move playouts winrate dynkomi */
451 reply
= engine
->result(engine
, board
);
453 gtp_reply(id
, reply
, NULL
);
455 gtp_error(id
, "unknown pachi-result command", NULL
);
457 } else if (!strcasecmp(cmd
, "kgs-chat")) {
466 reply
= engine
->chat(engine
, board
, msg
);
468 gtp_reply(id
, reply
, NULL
);
470 gtp_error(id
, "unknown kgs-chat command", NULL
);
472 } else if (!strcasecmp(cmd
, "time_left")) {
475 enum stone color
= str2stone(arg
);
477 int time
= atoi(arg
);
479 int stones
= atoi(arg
);
480 if (!ti
[color
].ignore_gtp
) {
481 time_left(&ti
[color
], time
, stones
);
483 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
488 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
491 if (!strcasecmp(cmd
, "kgs-time_settings")) {
492 next_tok(time_system
);
494 time_system
= "canadian";
497 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
498 if (!strcasecmp(time_system
, "none")) {
500 } else if (!strcasecmp(time_system
, "absolute")) {
502 main_time
= atoi(arg
);
503 } else if (!strcasecmp(time_system
, "byoyomi")) {
505 main_time
= atoi(arg
);
507 byoyomi_time
= atoi(arg
);
509 byoyomi_periods
= atoi(arg
);
510 } else if (!strcasecmp(time_system
, "canadian")) {
512 main_time
= atoi(arg
);
514 byoyomi_time
= atoi(arg
);
516 byoyomi_stones
= atoi(arg
);
520 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
521 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
522 if (!ti
[S_BLACK
].ignore_gtp
) {
523 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
524 ti
[S_WHITE
] = ti
[S_BLACK
];
526 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
532 gtp_error(id
, "unknown command", NULL
);
533 return P_UNKNOWN_COMMAND
;