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"
132 "kgs-genmove_cleanup\n"
133 "set_free_handicap\n"
134 "place_free_handicap\n"
135 "final_status_list\n"
139 "kgs-time_settings", NULL
);
143 if (engine
->notify
) {
145 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
146 if (c
== P_NOREPLY
) {
148 } else if (c
== P_DONE_OK
) {
149 gtp_reply(id
, reply
, NULL
);
151 } else if (c
== P_DONE_ERROR
) {
152 gtp_error(id
, reply
, NULL
);
153 /* This is an internal error for the engine, but
154 * it is still OK from main's point of view. */
156 } else if (c
!= P_OK
) {
161 if (!strcasecmp(cmd
, "quit")) {
165 } else if (!strcasecmp(cmd
, "boardsize")) {
168 board_resize(board
, atoi(arg
));
172 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
175 board_print(board
, stderr
);
176 if (!strcasecmp(cmd
, "kgs-game_over")) {
178 fprintf(stderr
, "game is over\n");
179 /* Sleep before replying, so that kgs doesn't
180 * start another game immediately. */
181 sleep(GAME_OVER_SLEEP
);
184 return P_ENGINE_RESET
;
186 } else if (!strcasecmp(cmd
, "komi")) {
189 sscanf(arg
, "%f", &board
->komi
);
192 board_print(board
, stderr
);
195 } else if (!strcasecmp(cmd
, "play")) {
200 m
.color
= str2stone(arg
);
202 coord_t
*c
= str2coord(arg
, board_size(board
));
203 m
.coord
= *c
; coord_done(c
);
207 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
209 // This is where kgs starts the timer, not at genmove!
210 time_start_timer(&ti
[stone_other(m
.color
)]);
212 if (engine
->notify_play
)
213 reply
= engine
->notify_play(engine
, board
, &m
);
214 if (board_play(board
, &m
) < 0) {
216 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
217 board_print(board
, stderr
);
219 gtp_error(id
, "illegal move", NULL
);
222 board_print(board
, stderr
);
223 gtp_reply(id
, reply
, NULL
);
226 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
229 enum stone color
= str2stone(arg
);
231 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
232 struct move m
= { *c
, color
};
233 board_play(board
, &m
);
234 char *str
= coord2str(*c
, board
);
236 fprintf(stderr
, "playing move %s\n", str
);
238 board_print_custom(board
, stderr
, engine
->printhook
);
240 gtp_reply(id
, str
, NULL
);
241 free(str
); coord_done(c
);
243 /* Account for spent time. If our GTP peer keeps our clock, this will
244 * be overriden by next time_left GTP command properly. */
245 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
246 * should be absolutely rare situation and we will just spend a little
247 * less time than we could on next few moves.) */
248 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
249 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
);
251 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
254 enum stone color
= str2stone(arg
);
255 struct time_info
*myti
= &ti
[color
];
256 /* Get correct time from master. Keep this code in sync
257 * with distributed_genmove(). */
258 if (myti
->dim
== TD_WALLTIME
&&
259 sscanf(next
, "%lf %lf %d %d", &myti
->len
.t
.main_time
,
260 &myti
->len
.t
.byoyomi_time
, &myti
->len
.t
.byoyomi_periods
,
261 &myti
->len
.t
.byoyomi_stones
) != 4) {
262 gtp_error(id
, "incorrect time info", NULL
);
266 char *reply
= engine
->genmoves(engine
, board
, myti
, color
, !strcasecmp(cmd
, "pachi-genmoves_cleanup"));
268 fprintf(stderr
, "proposing moves %s\n", reply
);
270 board_print_custom(board
, stderr
, engine
->printhook
);
272 gtp_reply(id
, reply
, NULL
);
274 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
281 coord_t
*c
= str2coord(arg
, board_size(board
));
282 m
.coord
= *c
; coord_done(c
);
284 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
286 if (board_play(board
, &m
) < 0) {
288 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
289 gtp_error(id
, "illegal move", NULL
);
295 board_print(board
, stderr
);
298 /* TODO: Engine should choose free handicap; however, it tends to take
299 * overly long to think it all out, and unless it's clever its
300 * handicap stones won't be of much help. ;-) */
301 } else if (!strcasecmp(cmd
, "place_free_handicap")
302 || !strcasecmp(cmd
, "fixed_handicap")) {
305 int stones
= atoi(arg
);
308 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
310 board_print(board
, stderr
);
311 if (id
== NO_REPLY
) return P_OK
;
315 } else if (!strcasecmp(cmd
, "final_score")) {
316 struct move_queue q
= { .moves
= 0 };
317 if (engine
->dead_group_list
)
318 engine
->dead_group_list(engine
, board
, &q
);
319 float score
= board_official_score(board
, &q
);
322 fprintf(stderr
, "counted score %.1f\n", score
);
324 gtp_reply(id
, "0", NULL
);
325 } else if (score
> 0) {
326 snprintf(str
, 64, "W+%.1f", score
);
327 gtp_reply(id
, str
, NULL
);
329 snprintf(str
, 64, "B+%.1f", -score
);
330 gtp_reply(id
, str
, NULL
);
333 /* XXX: This is a huge hack. */
334 } else if (!strcasecmp(cmd
, "final_status_list")) {
335 if (id
== NO_REPLY
) return P_OK
;
338 struct move_queue q
= { .moves
= 0 };
339 if (engine
->dead_group_list
)
340 engine
->dead_group_list(engine
, board
, &q
);
341 /* else we return empty list - i.e. engine not supporting
342 * this assumes all stones alive at the game end. */
343 if (!strcasecmp(arg
, "dead")) {
345 for (int i
= 0; i
< q
.moves
; i
++) {
346 foreach_in_group(board
, q
.move
[i
]) {
347 printf("%s ", coord2sstr(c
, board
));
348 } foreach_in_group_end
;
354 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
356 bool printed_group
= false;
357 foreach_point(board
) { // foreach_group, effectively
358 group_t g
= group_at(board
, c
);
359 if (!g
|| g
!= c
) continue;
361 for (int i
= 0; i
< q
.moves
; i
++) {
365 foreach_in_group(board
, g
) {
366 printf("%s ", coord2sstr(c
, board
));
367 } foreach_in_group_end
;
369 printed_group
= true;
376 gtp_error(id
, "illegal status specifier", NULL
);
379 /* Custom commands for handling UCT opening book */
380 } else if (!strcasecmp(cmd
, "uct_genbook")) {
381 /* Board must be initialized properly, as if for genmove;
382 * makes sense only as 'uct_genbook b'. */
385 enum stone color
= str2stone(arg
);
386 if (uct_genbook(engine
, board
, &ti
[color
], color
))
389 gtp_error(id
, "error generating book", NULL
);
391 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
394 enum stone color
= str2stone(arg
);
395 uct_dumpbook(engine
, board
, color
);
398 } else if (!strcasecmp(cmd
, "kgs-chat")) {
407 reply
= engine
->chat(engine
, board
, msg
);
409 gtp_reply(id
, reply
, NULL
);
411 gtp_error(id
, "unknown chat command", NULL
);
413 } else if (!strcasecmp(cmd
, "time_left")) {
416 enum stone color
= str2stone(arg
);
418 int time
= atoi(arg
);
420 int stones
= atoi(arg
);
421 if (!ti
[color
].ignore_gtp
) {
422 time_left(&ti
[color
], time
, stones
);
424 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
429 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
432 if (!strcasecmp(cmd
, "kgs-time_settings")) {
433 next_tok(time_system
);
435 time_system
= "canadian";
438 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
439 if (!strcasecmp(time_system
, "none")) {
441 } else if (!strcasecmp(time_system
, "absolute")) {
443 main_time
= atoi(arg
);
444 } else if (!strcasecmp(time_system
, "byoyomi")) {
446 main_time
= atoi(arg
);
448 byoyomi_time
= atoi(arg
);
450 byoyomi_periods
= atoi(arg
);
451 } else if (!strcasecmp(time_system
, "canadian")) {
453 main_time
= atoi(arg
);
455 byoyomi_time
= atoi(arg
);
457 byoyomi_stones
= atoi(arg
);
461 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
462 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
463 if (!ti
[S_BLACK
].ignore_gtp
) {
464 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
465 ti
[S_WHITE
] = ti
[S_BLACK
];
467 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
473 gtp_error(id
, "unknown command", NULL
);
474 return P_UNKNOWN_COMMAND
;