18 gtp_prefix(char prefix
, int id
)
21 printf("%c%d ", prefix
, id
);
23 printf("%c ", prefix
);
34 gtp_output(char prefix
, int id
, va_list params
)
36 gtp_prefix(prefix
, id
);
38 while ((s
= va_arg(params
, char *))) {
46 gtp_reply(int id
, ...)
50 gtp_output('=', id
, params
);
55 gtp_error(int id
, ...)
59 gtp_output('?', id
, params
);
64 /* XXX: THIS IS TOTALLY INSECURE!!!!
65 * Even basic input checking is missing. */
68 gtp_parse(struct board
*board
, struct engine
*engine
, char *buf
)
70 #define next_tok(to_) \
72 next = next + strcspn(next, " \t\r\n"); \
75 next += strspn(next, " \t\r\n"); \
79 *strchr(buf
, '#') = 0;
81 char *cmd
, *next
= buf
;
93 if (!strcasecmp(cmd
, "protocol_version")) {
94 gtp_reply(id
, "2", NULL
);
96 } else if (!strcasecmp(cmd
, "name")) {
98 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
100 } else if (!strcasecmp(cmd
, "version")) {
101 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
103 /* TODO: known_command */
105 } else if (!strcasecmp(cmd
, "list_commands")) {
106 gtp_reply(id
, "protocol_version\nname\nversion\nlist_commands\nquit\nboardsize\nclear_board\nkomi\nplay\ngenmove\nset_free_handicap\nplace_free_handicap\nfinal_status_list\nkgs-chat", NULL
);
108 } else if (!strcasecmp(cmd
, "quit")) {
112 } else if (!strcasecmp(cmd
, "boardsize")) {
115 board_resize(board
, atoi(arg
));
119 } else if (!strcasecmp(cmd
, "clear_board")) {
121 assert(engine
->done_board_state
);
122 engine
->done_board_state(engine
, board
);
126 board_print(board
, stderr
);
129 } else if (!strcasecmp(cmd
, "komi")) {
132 sscanf(arg
, "%f", &board
->komi
);
135 board_print(board
, stderr
);
138 } else if (!strcasecmp(cmd
, "play")) {
143 m
.color
= str2stone(arg
);
145 coord_t
*c
= str2coord(arg
, board_size(board
));
146 m
.coord
= *c
; coord_done(c
);
150 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
151 if (engine
->notify_play
)
152 reply
= engine
->notify_play(engine
, board
, &m
);
153 if (board_play(board
, &m
) < 0) {
155 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
156 board_print(board
, stderr
);
158 gtp_error(id
, "illegal move", NULL
);
161 board_print(board
, stderr
);
162 gtp_reply(id
, reply
, NULL
);
165 } else if (!strcasecmp(cmd
, "genmove")) {
168 enum stone color
= str2stone(arg
);
169 coord_t
*c
= engine
->genmove(engine
, board
, color
);
170 struct move m
= { *c
, color
};
171 board_play(board
, &m
);
172 char *str
= coord2str(*c
, board
);
174 fprintf(stderr
, "playing move %s\n", str
);
176 board_print_custom(board
, stderr
, engine
->printhook
);
178 gtp_reply(id
, str
, NULL
);
179 free(str
); coord_done(c
);
181 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
188 coord_t
*c
= str2coord(arg
, board_size(board
));
189 m
.coord
= *c
; coord_done(c
);
191 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
193 if (board_play(board
, &m
) < 0) {
195 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
196 gtp_error(id
, "illegal move", NULL
);
202 board_print(board
, stderr
);
205 /* TODO: Engine should choose free handicap; however, it tends to take
206 * overly long to think it all out, and unless it's clever its
207 * handicap stones won't be of much help. ;-) */
208 } else if (!strcasecmp(cmd
, "place_free_handicap")
209 || !strcasecmp(cmd
, "fixed_handicap")) {
212 int stones
= atoi(arg
);
215 board_handicap(board
, stones
, stdout
);
217 board_print(board
, stderr
);
221 } else if (!strcasecmp(cmd
, "final_score")) {
222 struct move_queue q
= { .moves
= 0 };
223 if (engine
->dead_group_list
)
224 engine
->dead_group_list(engine
, board
, &q
);
225 float score
= board_official_score(board
, &q
);
228 fprintf(stderr
, "counted score %.1f\n", score
);
230 gtp_reply(id
, "0", NULL
);
231 } else if (score
> 0) {
232 snprintf(str
, 64, "W+%.1f", score
);
233 gtp_reply(id
, str
, NULL
);
235 snprintf(str
, 64, "B+%.1f", -score
);
236 gtp_reply(id
, str
, NULL
);
239 /* XXX: This is a huge hack. */
240 } else if (!strcasecmp(cmd
, "final_status_list")) {
243 struct move_queue q
= { .moves
= 0 };
244 if (engine
->dead_group_list
)
245 engine
->dead_group_list(engine
, board
, &q
);
246 /* else we return empty list - i.e. engine not supporting
247 * this assumes all stones alive at the game end. */
248 if (!strcasecmp(arg
, "dead")) {
250 for (int i
= 0; i
< q
.moves
; i
++) {
251 foreach_in_group(board
, q
.move
[i
]) {
252 printf("%s ", coord2sstr(c
, board
));
253 } foreach_in_group_end
;
259 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
261 bool printed_group
= false;
262 foreach_point(board
) { // foreach_group, effectively
263 group_t g
= group_at(board
, c
);
264 if (!g
|| g
!= c
) continue;
266 for (int i
= 0; i
< q
.moves
; i
++) {
270 foreach_in_group(board
, g
) {
271 printf("%s ", coord2sstr(c
, board
));
272 } foreach_in_group_end
;
274 printed_group
= true;
281 gtp_error(id
, "illegal status specifier", NULL
);
284 /* Custom commands for handling UCT opening book */
285 } else if (!strcasecmp(cmd
, "uct_genbook")) {
286 /* Board must be initialized properly, as if for genmove;
287 * makes sense only as 'uct_genbook b'. */
290 enum stone color
= str2stone(arg
);
291 if (uct_genbook(engine
, board
, color
))
294 gtp_error(id
, "error generating book", NULL
);
296 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
299 enum stone color
= str2stone(arg
);
300 uct_dumpbook(engine
, board
, color
);
303 } else if (!strcasecmp(cmd
, "kgs-chat")) {
312 reply
= engine
->chat(engine
, board
, msg
);
314 gtp_reply(id
, reply
, NULL
);
316 gtp_error(id
, "unknown chat command", NULL
);
319 gtp_error(id
, "unknown command", NULL
);