20 /* Sleep 5 seconds after a game ends to give time to kill the program. */
21 #define GAME_OVER_SLEEP 5
24 gtp_prefix(char prefix
, int id
)
26 if (id
== NO_REPLY
) return;
28 printf("%c%d ", prefix
, id
);
30 printf("%c ", prefix
);
41 gtp_output(char prefix
, int id
, va_list params
)
43 if (id
== NO_REPLY
) return;
44 gtp_prefix(prefix
, id
);
46 while ((s
= va_arg(params
, char *))) {
54 gtp_reply(int id
, ...)
58 gtp_output('=', id
, params
);
63 gtp_error(int id
, ...)
67 gtp_output('?', id
, params
);
72 /* XXX: THIS IS TOTALLY INSECURE!!!!
73 * Even basic input checking is missing. */
76 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
78 #define next_tok(to_) \
80 next = next + strcspn(next, " \t\r\n"); \
83 next += strspn(next, " \t\r\n"); \
87 *strchr(buf
, '#') = 0;
89 char *cmd
, *next
= buf
;
101 if (!strcasecmp(cmd
, "protocol_version")) {
102 gtp_reply(id
, "2", NULL
);
105 } else if (!strcasecmp(cmd
, "name")) {
107 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
110 } else if (!strcasecmp(cmd
, "version")) {
111 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
114 /* TODO: known_command */
116 } else if (!strcasecmp(cmd
, "list_commands")) {
117 /* The internal command pachi-genmoves is not exported,
118 * it should only be used between master and slaves of
119 * the distributed engine. */
120 gtp_reply(id
, "protocol_version\n"
131 "kgs-genmove_cleanup\n"
132 "set_free_handicap\n"
133 "place_free_handicap\n"
134 "final_status_list\n"
138 "kgs-time_settings", NULL
);
142 if (engine
->notify
) {
144 enum parse_code c
= engine
->notify(engine
, board
, id
, cmd
, next
, &reply
);
145 if (c
== P_NOREPLY
) {
147 } else if (c
== P_DONE_OK
) {
148 gtp_reply(id
, reply
, NULL
);
150 } else if (c
== P_DONE_ERROR
) {
151 gtp_error(id
, reply
, NULL
);
152 /* This is an internal error for the engine, but
153 * it is still OK from main's point of view. */
155 } else if (c
!= P_OK
) {
160 if (!strcasecmp(cmd
, "quit")) {
164 } else if (!strcasecmp(cmd
, "boardsize")) {
167 board_resize(board
, atoi(arg
));
171 } else if (!strcasecmp(cmd
, "clear_board") || !strcasecmp(cmd
, "kgs-game_over")) {
174 board_print(board
, stderr
);
175 if (!strcasecmp(cmd
, "kgs-game_over")) {
177 fprintf(stderr
, "game is over\n");
178 /* Sleep before replying, so that kgs doesn't
179 * start another game immediately. */
180 sleep(GAME_OVER_SLEEP
);
183 return P_ENGINE_RESET
;
185 } else if (!strcasecmp(cmd
, "komi")) {
188 sscanf(arg
, "%f", &board
->komi
);
191 board_print(board
, stderr
);
194 } else if (!strcasecmp(cmd
, "play")) {
199 m
.color
= str2stone(arg
);
201 coord_t
*c
= str2coord(arg
, board_size(board
));
202 m
.coord
= *c
; coord_done(c
);
206 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
208 // This is where kgs starts the timer, not at genmove!
209 time_start_timer(&ti
[stone_other(m
.color
)]);
211 if (engine
->notify_play
)
212 reply
= engine
->notify_play(engine
, board
, &m
);
213 if (board_play(board
, &m
) < 0) {
215 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
216 board_print(board
, stderr
);
218 gtp_error(id
, "illegal move", NULL
);
221 board_print(board
, stderr
);
222 gtp_reply(id
, reply
, NULL
);
225 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
228 enum stone color
= str2stone(arg
);
230 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
231 struct move m
= { *c
, color
};
232 board_play(board
, &m
);
233 char *str
= coord2str(*c
, board
);
235 fprintf(stderr
, "playing move %s\n", str
);
237 board_print_custom(board
, stderr
, engine
->printhook
);
239 gtp_reply(id
, str
, NULL
);
240 free(str
); coord_done(c
);
242 /* Account for spent time. If our GTP peer keeps our clock, this will
243 * be overriden by next time_left GTP command properly. */
244 /* (XXX: Except if we pass to byoyomi and the peer doesn't, but that
245 * should be absolutely rare situation and we will just spend a little
246 * less time than we could on next few moves.) */
247 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
248 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
);
250 } else if (!strcasecmp(cmd
, "pachi-genmoves") || !strcasecmp(cmd
, "pachi-genmoves_cleanup")) {
253 enum stone color
= str2stone(arg
);
255 char *reply
= engine
->genmoves(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "pachi-genmoves_cleanup"));
257 fprintf(stderr
, "proposing moves %s\n", reply
);
258 gtp_reply(id
, reply
, NULL
);
260 /* See "genmove" above about time management. */
261 if (ti
[color
].period
!= TT_NULL
&& ti
[color
].dim
== TD_WALLTIME
)
262 time_sub(&ti
[color
], time_now() - ti
[color
].len
.t
.timer_start
);
264 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
271 coord_t
*c
= str2coord(arg
, board_size(board
));
272 m
.coord
= *c
; coord_done(c
);
274 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
276 if (board_play(board
, &m
) < 0) {
278 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
279 gtp_error(id
, "illegal move", NULL
);
285 board_print(board
, stderr
);
288 /* TODO: Engine should choose free handicap; however, it tends to take
289 * overly long to think it all out, and unless it's clever its
290 * handicap stones won't be of much help. ;-) */
291 } else if (!strcasecmp(cmd
, "place_free_handicap")
292 || !strcasecmp(cmd
, "fixed_handicap")) {
295 int stones
= atoi(arg
);
298 board_handicap(board
, stones
, id
== NO_REPLY
? NULL
: stdout
);
300 board_print(board
, stderr
);
301 if (id
== NO_REPLY
) return P_OK
;
305 } else if (!strcasecmp(cmd
, "final_score")) {
306 struct move_queue q
= { .moves
= 0 };
307 if (engine
->dead_group_list
)
308 engine
->dead_group_list(engine
, board
, &q
);
309 float score
= board_official_score(board
, &q
);
312 fprintf(stderr
, "counted score %.1f\n", score
);
314 gtp_reply(id
, "0", NULL
);
315 } else if (score
> 0) {
316 snprintf(str
, 64, "W+%.1f", score
);
317 gtp_reply(id
, str
, NULL
);
319 snprintf(str
, 64, "B+%.1f", -score
);
320 gtp_reply(id
, str
, NULL
);
323 /* XXX: This is a huge hack. */
324 } else if (!strcasecmp(cmd
, "final_status_list")) {
325 if (id
== NO_REPLY
) return P_OK
;
328 struct move_queue q
= { .moves
= 0 };
329 if (engine
->dead_group_list
)
330 engine
->dead_group_list(engine
, board
, &q
);
331 /* else we return empty list - i.e. engine not supporting
332 * this assumes all stones alive at the game end. */
333 if (!strcasecmp(arg
, "dead")) {
335 for (int i
= 0; i
< q
.moves
; i
++) {
336 foreach_in_group(board
, q
.move
[i
]) {
337 printf("%s ", coord2sstr(c
, board
));
338 } foreach_in_group_end
;
344 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
346 bool printed_group
= false;
347 foreach_point(board
) { // foreach_group, effectively
348 group_t g
= group_at(board
, c
);
349 if (!g
|| g
!= c
) continue;
351 for (int i
= 0; i
< q
.moves
; i
++) {
355 foreach_in_group(board
, g
) {
356 printf("%s ", coord2sstr(c
, board
));
357 } foreach_in_group_end
;
359 printed_group
= true;
366 gtp_error(id
, "illegal status specifier", NULL
);
369 /* Custom commands for handling UCT opening book */
370 } else if (!strcasecmp(cmd
, "uct_genbook")) {
371 /* Board must be initialized properly, as if for genmove;
372 * makes sense only as 'uct_genbook b'. */
375 enum stone color
= str2stone(arg
);
376 if (uct_genbook(engine
, board
, &ti
[color
], color
))
379 gtp_error(id
, "error generating book", NULL
);
381 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
384 enum stone color
= str2stone(arg
);
385 uct_dumpbook(engine
, board
, color
);
388 } else if (!strcasecmp(cmd
, "kgs-chat")) {
397 reply
= engine
->chat(engine
, board
, msg
);
399 gtp_reply(id
, reply
, NULL
);
401 gtp_error(id
, "unknown chat command", NULL
);
403 } else if (!strcasecmp(cmd
, "time_left")) {
406 enum stone color
= str2stone(arg
);
408 int time
= atoi(arg
);
410 int stones
= atoi(arg
);
411 if (!ti
[color
].ignore_gtp
) {
412 time_left(&ti
[color
], time
, stones
);
414 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
419 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
422 if (!strcasecmp(cmd
, "kgs-time_settings")) {
423 next_tok(time_system
);
425 time_system
= "canadian";
428 int main_time
= 0, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
429 if (!strcasecmp(time_system
, "none")) {
431 } else if (!strcasecmp(time_system
, "absolute")) {
433 main_time
= atoi(arg
);
434 } else if (!strcasecmp(time_system
, "byoyomi")) {
436 main_time
= atoi(arg
);
438 byoyomi_time
= atoi(arg
);
440 byoyomi_periods
= atoi(arg
);
441 } else if (!strcasecmp(time_system
, "canadian")) {
443 main_time
= atoi(arg
);
445 byoyomi_time
= atoi(arg
);
447 byoyomi_stones
= atoi(arg
);
451 fprintf(stderr
, "time_settings %d %d/%d*%d\n",
452 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
453 if (!ti
[S_BLACK
].ignore_gtp
) {
454 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
455 ti
[S_WHITE
] = ti
[S_BLACK
];
457 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
463 gtp_error(id
, "unknown command", NULL
);
464 return P_UNKNOWN_COMMAND
;