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
);
73 /* XXX: THIS IS TOTALLY INSECURE!!!!
74 * Even basic input checking is missing. */
77 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
79 #define next_tok(to_) \
81 next = next + strcspn(next, " \t\r\n"); \
84 next += strspn(next, " \t\r\n"); \
88 *strchr(buf
, '#') = 0;
90 char *cmd
, *next
= buf
;
102 if (!strcasecmp(cmd
, "protocol_version")) {
103 gtp_reply(id
, "2", NULL
);
106 } else if (!strcasecmp(cmd
, "name")) {
108 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
111 } else if (!strcasecmp(cmd
, "version")) {
112 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
115 /* TODO: known_command */
117 } else if (!strcasecmp(cmd
, "list_commands")) {
118 /* The internal command pachi-genmoves is not exported,
119 * it should only be used between master and slaves of
120 * the distributed engine. */
121 gtp_reply(id
, "protocol_version\n"
133 "kgs-genmove_cleanup\n"
134 "set_free_handicap\n"
135 "place_free_handicap\n"
136 "final_status_list\n"
140 "kgs-time_settings", NULL
);
144 if (engine
->notify
) {
146 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
147 if (c
== P_NOREPLY
) {
149 } else if (c
== P_DONE_OK
) {
150 gtp_reply(id
, reply
, NULL
);
152 } else if (c
== P_DONE_ERROR
) {
153 gtp_error(id
, reply
, NULL
);
154 /* This is an internal error for the engine, but
155 * it is still OK from main's point of view. */
157 } else if (c
!= P_OK
) {
162 if (!strcasecmp(cmd
, "quit")) {
166 } else if (!strcasecmp(cmd
, "boardsize")) {
169 board_resize(board
, atoi(arg
));
173 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
176 board_print(board
, stderr
);
177 if (!strcasecmp(cmd
, "kgs-game_over")) {
179 fprintf(stderr
, "game is over\n");
180 /* Sleep before replying, so that kgs doesn't
181 * start another game immediately. */
182 sleep(GAME_OVER_SLEEP
);
185 return P_ENGINE_RESET
;
187 } else if (!strcasecmp(cmd
, "komi")) {
190 sscanf(arg
, "%f", &board
->komi
);
193 board_print(board
, stderr
);
196 } else if (!strcasecmp(cmd
, "kgs-rules")) {
199 if (!strcasecmp(arg
, "japanese")) {
200 board
->rules
= RULES_JAPANESE
;
201 } else if (!strcasecmp(arg
, "chinese")) {
202 board
->rules
= RULES_CHINESE
;
203 } else if (!strcasecmp(arg
, "aga")) {
204 board
->rules
= RULES_AGA
;
205 } else if (!strcasecmp(arg
, "new_zealand")) {
206 board
->rules
= RULES_NEW_ZEALAND
;
208 gtp_error(id
, "unknown rules", NULL
);
213 } else if (!strcasecmp(cmd
, "play")) {
218 m
.color
= str2stone(arg
);
220 coord_t
*c
= str2coord(arg
, board_size(board
));
221 m
.coord
= *c
; coord_done(c
);
225 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
227 // This is where kgs starts the timer, not at genmove!
228 time_start_timer(&ti
[stone_other(m
.color
)]);
230 if (engine
->notify_play
)
231 reply
= engine
->notify_play(engine
, board
, &m
);
232 if (board_play(board
, &m
) < 0) {
234 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
235 board_print(board
, stderr
);
237 gtp_error(id
, "illegal move", NULL
);
240 board_print_custom(board
, stderr
, engine
->printhook
);
241 gtp_reply(id
, reply
, NULL
);
244 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
247 enum stone color
= str2stone(arg
);
249 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
250 struct move m
= { *c
, color
};
251 board_play(board
, &m
);
252 char *str
= coord2str(*c
, board
);
254 fprintf(stderr
, "playing move %s\n", str
);
256 board_print_custom(board
, stderr
, engine
->printhook
);
258 gtp_reply(id
, str
, NULL
);
259 free(str
); coord_done(c
);
261 /* Account for spent time. If our GTP peer keeps our clock, this will
262 * be overriden by next time_left GTP command properly. */
263 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
264 * should be absolutely rare situation and we will just spend a little
265 * less time than we could on next few moves.) */
266 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
267 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
, true);
269 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
272 enum stone color
= str2stone(arg
);
274 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, next
,
275 !strcasecmp(cmd
, "pachi-genmoves_cleanup"));
277 gtp_error(id
, "genmoves error", NULL
);
281 fprintf(stderr
, "proposing moves %s\n", reply
);
283 board_print_custom(board
, stderr
, engine
->printhook
);
284 gtp_reply(id
, reply
, NULL
);
286 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
293 coord_t
*c
= str2coord(arg
, board_size(board
));
294 m
.coord
= *c
; coord_done(c
);
296 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
298 if (board_play(board
, &m
) < 0) {
300 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
301 gtp_error(id
, "illegal move", NULL
);
307 board_print(board
, stderr
);
310 /* TODO: Engine should choose free handicap; however, it tends to take
311 * overly long to think it all out, and unless it's clever its
312 * handicap stones won't be of much help. ;-) */
313 } else if (!strcasecmp(cmd
, "place_free_handicap")
314 || !strcasecmp(cmd
, "fixed_handicap")) {
317 int stones
= atoi(arg
);
320 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
322 board_print(board
, stderr
);
323 if (id
== NO_REPLY
) return P_OK
;
327 } else if (!strcasecmp(cmd
, "final_score")) {
328 struct move_queue q
= { .moves
= 0 };
329 if (engine
->dead_group_list
)
330 engine
->dead_group_list(engine
, board
, &q
);
331 float score
= board_official_score(board
, &q
);
334 fprintf(stderr
, "counted score %.1f\n", score
);
336 gtp_reply(id
, "0", NULL
);
337 } else if (score
> 0) {
338 snprintf(str
, 64, "W+%.1f", score
);
339 gtp_reply(id
, str
, NULL
);
341 snprintf(str
, 64, "B+%.1f", -score
);
342 gtp_reply(id
, str
, NULL
);
345 /* XXX: This is a huge hack. */
346 } else if (!strcasecmp(cmd
, "final_status_list")) {
347 if (id
== NO_REPLY
) return P_OK
;
350 struct move_queue q
= { .moves
= 0 };
351 if (engine
->dead_group_list
)
352 engine
->dead_group_list(engine
, board
, &q
);
353 /* else we return empty list - i.e. engine not supporting
354 * this assumes all stones alive at the game end. */
355 if (!strcasecmp(arg
, "dead")) {
357 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
358 foreach_in_group(board
, q
.move
[i
]) {
359 printf("%s ", coord2sstr(c
, board
));
360 } foreach_in_group_end
;
366 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
368 bool printed_group
= false;
369 foreach_point(board
) { // foreach_group, effectively
370 group_t g
= group_at(board
, c
);
371 if (!g
|| g
!= c
) continue;
373 for (unsigned int i
= 0; i
< q
.moves
; i
++) {
377 foreach_in_group(board
, g
) {
378 printf("%s ", coord2sstr(c
, board
));
379 } foreach_in_group_end
;
381 printed_group
= true;
388 gtp_error(id
, "illegal status specifier", NULL
);
391 /* Custom commands for handling UCT opening book */
392 } else if (!strcasecmp(cmd
, "uct_genbook")) {
393 /* Board must be initialized properly, as if for genmove;
394 * makes sense only as 'uct_genbook b'. */
397 enum stone color
= str2stone(arg
);
398 if (uct_genbook(engine
, board
, &ti
[color
], color
))
401 gtp_error(id
, "error generating book", NULL
);
403 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
406 enum stone color
= str2stone(arg
);
407 uct_dumpbook(engine
, board
, color
);
410 } else if (!strcasecmp(cmd
, "kgs-chat")) {
419 reply
= engine
->chat(engine
, board
, msg
);
421 gtp_reply(id
, reply
, NULL
);
423 gtp_error(id
, "unknown chat command", NULL
);
425 } else if (!strcasecmp(cmd
, "time_left")) {
428 enum stone color
= str2stone(arg
);
430 int time
= atoi(arg
);
432 int stones
= atoi(arg
);
433 if (!ti
[color
].ignore_gtp
) {
434 time_left(&ti
[color
], time
, stones
);
436 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
441 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
444 if (!strcasecmp(cmd
, "kgs-time_settings")) {
445 next_tok(time_system
);
447 time_system
= "canadian";
450 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
451 if (!strcasecmp(time_system
, "none")) {
453 } else if (!strcasecmp(time_system
, "absolute")) {
455 main_time
= atoi(arg
);
456 } else if (!strcasecmp(time_system
, "byoyomi")) {
458 main_time
= atoi(arg
);
460 byoyomi_time
= atoi(arg
);
462 byoyomi_periods
= atoi(arg
);
463 } else if (!strcasecmp(time_system
, "canadian")) {
465 main_time
= atoi(arg
);
467 byoyomi_time
= atoi(arg
);
469 byoyomi_stones
= atoi(arg
);
473 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
474 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
475 if (!ti
[S_BLACK
].ignore_gtp
) {
476 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
477 ti
[S_WHITE
] = ti
[S_BLACK
];
479 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
485 gtp_error(id
, "unknown command", NULL
);
486 return P_UNKNOWN_COMMAND
;