22 /* Sleep 5 seconds after a game ends to give time to kill the program. */
23 #define GAME_OVER_SLEEP 5
26 gtp_prefix(char prefix
, int id
)
28 if (id
== NO_REPLY
) return;
30 printf("%c%d ", prefix
, id
);
32 printf("%c ", prefix
);
43 gtp_output(char prefix
, int id
, va_list params
)
45 if (id
== NO_REPLY
) return;
46 gtp_prefix(prefix
, id
);
48 while ((s
= va_arg(params
, char *))) {
56 gtp_reply(int id
, ...)
60 gtp_output('=', id
, params
);
65 gtp_error(int id
, ...)
69 gtp_output('?', id
, params
);
73 /* List of known gtp commands. The internal command pachi-genmoves is not exported,
74 * it should only be used between master and slaves of the distributed engine. */
75 static char *known_commands
=
89 "kgs-genmove_cleanup\n"
91 "place_free_handicap\n"
100 /* Return true if cmd is a valid gtp command. */
102 gtp_is_valid(char *cmd
)
104 if (!cmd
|| !*cmd
) return false;
105 char *s
= strcasestr(known_commands
, cmd
);
106 if (!s
) return false;
108 int len
= strlen(cmd
);
109 return s
[len
] == '\0' || s
[len
] == '\n';
112 /* XXX: THIS IS TOTALLY INSECURE!!!!
113 * Even basic input checking is missing. */
116 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
118 #define next_tok(to_) \
120 next = next + strcspn(next, " \t\r\n"); \
123 next += strspn(next, " \t\r\n"); \
126 if (strchr(buf
, '#'))
127 *strchr(buf
, '#') = 0;
129 char *cmd
, *next
= buf
;
141 if (!strcasecmp(cmd
, "protocol_version")) {
142 gtp_reply(id
, "2", NULL
);
145 } else if (!strcasecmp(cmd
, "name")) {
147 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
150 } else if (!strcasecmp(cmd
, "echo")) {
151 gtp_reply(id
, next
, NULL
);
154 } else if (!strcasecmp(cmd
, "version")) {
155 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
158 } else if (!strcasecmp(cmd
, "list_commands")) {
159 gtp_reply(id
, known_commands
, NULL
);
163 if (engine
->notify
) {
165 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
166 if (c
== P_NOREPLY
) {
168 } else if (c
== P_DONE_OK
) {
169 gtp_reply(id
, reply
, NULL
);
171 } else if (c
== P_DONE_ERROR
) {
172 gtp_error(id
, reply
, NULL
);
173 /* This is an internal error for the engine, but
174 * it is still OK from main's point of view. */
176 } else if (c
!= P_OK
) {
181 if (!strcasecmp(cmd
, "quit")) {
185 } else if (!strcasecmp(cmd
, "boardsize")) {
188 int size
= atoi(arg
);
189 if (size
< 1 || size
> BOARD_MAX_SIZE
) {
190 gtp_error(id
, "illegal board size", NULL
);
193 board_resize(board
, size
);
196 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
199 board_print(board
, stderr
);
200 if (!strcasecmp(cmd
, "kgs-game_over")) {
202 fprintf(stderr
, "game is over\n");
203 /* Sleep before replying, so that kgs doesn't
204 * start another game immediately. */
205 sleep(GAME_OVER_SLEEP
);
208 return P_ENGINE_RESET
;
210 } else if (!strcasecmp(cmd
, "komi")) {
213 sscanf(arg
, "%f", &board
->komi
);
216 board_print(board
, stderr
);
219 } else if (!strcasecmp(cmd
, "kgs-rules")) {
222 if (!strcasecmp(arg
, "japanese")) {
223 board
->rules
= RULES_JAPANESE
;
224 } else if (!strcasecmp(arg
, "chinese")) {
225 board
->rules
= RULES_CHINESE
;
226 } else if (!strcasecmp(arg
, "aga")) {
227 board
->rules
= RULES_AGA
;
228 } else if (!strcasecmp(arg
, "new_zealand")) {
229 board
->rules
= RULES_NEW_ZEALAND
;
231 gtp_error(id
, "unknown rules", NULL
);
236 } else if (!strcasecmp(cmd
, "play")) {
241 m
.color
= str2stone(arg
);
243 coord_t
*c
= str2coord(arg
, board_size(board
));
244 m
.coord
= *c
; coord_done(c
);
248 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
250 // This is where kgs starts the timer, not at genmove!
251 time_start_timer(&ti
[stone_other(m
.color
)]);
253 if (engine
->notify_play
)
254 reply
= engine
->notify_play(engine
, board
, &m
);
255 if (board_play(board
, &m
) < 0) {
257 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
258 board_print(board
, stderr
);
260 gtp_error(id
, "illegal move", NULL
);
263 board_print_custom(board
, stderr
, engine
->printhook
);
264 gtp_reply(id
, reply
, NULL
);
267 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
270 enum stone color
= str2stone(arg
);
272 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
273 struct move m
= { *c
, color
};
274 if (board_play(board
, &m
) < 0) {
275 fprintf(stderr
, "Attempted to generate an illegal move: [%s, %s]\n", coord2sstr(m
.coord
, board
), stone2str(m
.color
));
278 char *str
= coord2str(*c
, board
);
280 fprintf(stderr
, "playing move %s\n", str
);
282 board_print_custom(board
, stderr
, engine
->printhook
);
284 gtp_reply(id
, str
, NULL
);
285 free(str
); coord_done(c
);
287 /* Account for spent time. If our GTP peer keeps our clock, this will
288 * be overriden by next time_left GTP command properly. */
289 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
290 * should be absolutely rare situation and we will just spend a little
291 * less time than we could on next few moves.) */
292 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
293 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
295 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
298 enum stone color
= str2stone(arg
);
302 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
303 !strcasecmp(cmd
, "pachi-genmoves_cleanup"),
304 &stats
, &stats_size
);
306 gtp_error(id
, "genmoves error", NULL
);
310 fprintf(stderr
, "proposing moves %s\n", reply
);
312 board_print_custom(board
, stderr
, engine
->printhook
);
313 gtp_reply(id
, reply
, NULL
);
314 if (stats_size
> 0) {
315 double start
= time_now();
316 fwrite(stats
, 1, stats_size
, stdout
);
319 fprintf(stderr
, "sent reply %d bytes in %.4fms\n",
320 stats_size
, (time_now() - start
)*1000);
323 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
330 coord_t
*c
= str2coord(arg
, board_size(board
));
331 m
.coord
= *c
; coord_done(c
);
333 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
335 if (board_play(board
, &m
) < 0) {
337 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
338 gtp_error(id
, "illegal move", NULL
);
344 board_print(board
, stderr
);
347 /* TODO: Engine should choose free handicap; however, it tends to take
348 * overly long to think it all out, and unless it's clever its
349 * handicap stones won't be of much help. ;-) */
350 } else if (!strcasecmp(cmd
, "place_free_handicap")
351 || !strcasecmp(cmd
, "fixed_handicap")) {
354 int stones
= atoi(arg
);
357 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
359 board_print(board
, stderr
);
360 if (id
== NO_REPLY
) return P_OK
;
364 } else if (!strcasecmp(cmd
, "final_score")) {
365 struct move_queue q
= { .moves
= 0 };
366 if (engine
->dead_group_list
)
367 engine
->dead_group_list(engine
, board
, &q
);
368 float score
= board_official_score(board
, &q
);
371 fprintf(stderr
, "counted score %.1f\n", score
);
373 gtp_reply(id
, "0", NULL
);
374 } else if (score
> 0) {
375 snprintf(str
, 64, "W+%.1f", score
);
376 gtp_reply(id
, str
, NULL
);
378 snprintf(str
, 64, "B+%.1f", -score
);
379 gtp_reply(id
, str
, NULL
);
382 /* XXX: This is a huge hack. */
383 } else if (!strcasecmp(cmd
, "final_status_list")) {
384 if (id
== NO_REPLY
) return P_OK
;
387 struct move_queue q
= { .moves
= 0 };
388 if (engine
->dead_group_list
)
389 engine
->dead_group_list(engine
, board
, &q
);
390 /* else we return empty list - i.e. engine not supporting
391 * this assumes all stones alive at the game end. */
392 if (!strcasecmp(arg
, "dead")) {
394 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
395 foreach_in_group(board
, q
.move
[i
]) {
396 printf("%s ", coord2sstr(c
, board
));
397 } foreach_in_group_end
;
403 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
405 bool printed_group
= false;
406 foreach_point(board
) { // foreach_group, effectively
407 group_t g
= group_at(board
, c
);
408 if (!g
|| g
!= c
) continue;
410 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
414 foreach_in_group(board
, g
) {
415 printf("%s ", coord2sstr(c
, board
));
416 } foreach_in_group_end
;
418 printed_group
= true;
425 gtp_error(id
, "illegal status specifier", NULL
);
428 /* Custom commands for handling UCT opening book */
429 } else if (!strcasecmp(cmd
, "uct_genbook")) {
430 /* Board must be initialized properly, as if for genmove;
431 * makes sense only as 'uct_genbook b'. */
434 enum stone color
= str2stone(arg
);
435 if (uct_genbook(engine
, board
, &ti
[color
], color
))
438 gtp_error(id
, "error generating book", NULL
);
440 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
443 enum stone color
= str2stone(arg
);
444 uct_dumpbook(engine
, board
, color
);
447 } else if (!strcasecmp(cmd
, "uct_evaluate")) {
450 enum stone color
= str2stone(arg
);
453 /* Iterate through the list of all free coordinates
454 * and call uct_evaluate() for each. uct_evaluate()
455 * will throw NAN in case of invalid moves and such. */
456 for (int i
= 0; i
< board
->flen
; i
++) {
457 float val
= uct_evaluate(engine
, board
, &ti
[color
], board
->f
[i
], color
);
460 printf("%s %1.3f\n", coord2sstr(board
->f
[i
], board
), (double) val
);
464 } else if (!strcasecmp(cmd
, "pachi-result")) {
465 /* More detailed result of the last genmove. */
466 /* For UCT, the output format is: = color move playouts winrate dynkomi */
469 reply
= engine
->result(engine
, board
);
471 gtp_reply(id
, reply
, NULL
);
473 gtp_error(id
, "unknown pachi-result command", NULL
);
475 } else if (!strcasecmp(cmd
, "kgs-chat")) {
484 reply
= engine
->chat(engine
, board
, msg
);
486 gtp_reply(id
, reply
, NULL
);
488 gtp_error(id
, "unknown kgs-chat command", NULL
);
490 } else if (!strcasecmp(cmd
, "time_left")) {
493 enum stone color
= str2stone(arg
);
495 int time
= atoi(arg
);
497 int stones
= atoi(arg
);
498 if (!ti
[color
].ignore_gtp
) {
499 time_left(&ti
[color
], time
, stones
);
501 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
506 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
509 if (!strcasecmp(cmd
, "kgs-time_settings")) {
510 next_tok(time_system
);
512 time_system
= "canadian";
515 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
516 if (!strcasecmp(time_system
, "none")) {
518 } else if (!strcasecmp(time_system
, "absolute")) {
520 main_time
= atoi(arg
);
521 } else if (!strcasecmp(time_system
, "byoyomi")) {
523 main_time
= atoi(arg
);
525 byoyomi_time
= atoi(arg
);
527 byoyomi_periods
= atoi(arg
);
528 } else if (!strcasecmp(time_system
, "canadian")) {
530 main_time
= atoi(arg
);
532 byoyomi_time
= atoi(arg
);
534 byoyomi_stones
= atoi(arg
);
538 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
539 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
540 if (!ti
[S_BLACK
].ignore_gtp
) {
541 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
542 ti
[S_WHITE
] = ti
[S_BLACK
];
544 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
550 gtp_error(id
, "unknown command", NULL
);
551 return P_UNKNOWN_COMMAND
;