19 gtp_prefix(char prefix
, int id
)
22 printf("%c%d ", prefix
, id
);
24 printf("%c ", prefix
);
35 gtp_output(char prefix
, int id
, va_list params
)
37 gtp_prefix(prefix
, id
);
39 while ((s
= va_arg(params
, char *))) {
47 gtp_reply(int id
, ...)
51 gtp_output('=', id
, params
);
56 gtp_error(int id
, ...)
60 gtp_output('?', id
, params
);
65 /* XXX: THIS IS TOTALLY INSECURE!!!!
66 * Even basic input checking is missing. */
69 gtp_parse(struct board
*board
, struct engine
*engine
, struct time_info
*ti
, char *buf
)
71 #define next_tok(to_) \
73 next = next + strcspn(next, " \t\r\n"); \
76 next += strspn(next, " \t\r\n"); \
80 *strchr(buf
, '#') = 0;
82 char *cmd
, *next
= buf
;
94 if (!strcasecmp(cmd
, "protocol_version")) {
95 gtp_reply(id
, "2", NULL
);
97 } else if (!strcasecmp(cmd
, "name")) {
99 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
101 } else if (!strcasecmp(cmd
, "version")) {
102 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
104 /* TODO: known_command */
106 } else if (!strcasecmp(cmd
, "list_commands")) {
107 gtp_reply(id
, "protocol_version\nname\nversion\nlist_commands\nquit\nboardsize\nclear_board\nkomi\nplay\ngenmove\nkgs-genmove_cleanup\nset_free_handicap\nplace_free_handicap\nfinal_status_list\nkgs-chat", NULL
);
109 } else if (!strcasecmp(cmd
, "quit")) {
113 } else if (!strcasecmp(cmd
, "boardsize")) {
116 board_resize(board
, atoi(arg
));
120 } else if (!strcasecmp(cmd
, "clear_board")) {
124 board_print(board
, stderr
);
127 } else if (!strcasecmp(cmd
, "komi")) {
130 sscanf(arg
, "%f", &board
->komi
);
133 board_print(board
, stderr
);
136 } else if (!strcasecmp(cmd
, "play")) {
141 m
.color
= str2stone(arg
);
143 coord_t
*c
= str2coord(arg
, board_size(board
));
144 m
.coord
= *c
; coord_done(c
);
148 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
150 time_start_timer(&ti
[stone_other(m
.color
)]); // This is where kgs starts our timer, not at coming genmove
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") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
168 enum stone color
= str2stone(arg
);
169 time_prepare_move(ti
, board
);
171 coord_t
*c
= engine
->genmove(engine
, board
, ti
, color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
172 struct move m
= { *c
, color
};
173 board_play(board
, &m
);
174 char *str
= coord2str(*c
, board
);
176 fprintf(stderr
, "playing move %s\n", str
);
178 board_print_custom(board
, stderr
, engine
->printhook
);
180 gtp_reply(id
, str
, NULL
);
181 free(str
); coord_done(c
);
183 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
190 coord_t
*c
= str2coord(arg
, board_size(board
));
191 m
.coord
= *c
; coord_done(c
);
193 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
195 if (board_play(board
, &m
) < 0) {
197 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
198 gtp_error(id
, "illegal move", NULL
);
204 board_print(board
, stderr
);
207 /* TODO: Engine should choose free handicap; however, it tends to take
208 * overly long to think it all out, and unless it's clever its
209 * handicap stones won't be of much help. ;-) */
210 } else if (!strcasecmp(cmd
, "place_free_handicap")
211 || !strcasecmp(cmd
, "fixed_handicap")) {
214 int stones
= atoi(arg
);
217 board_handicap(board
, stones
, stdout
);
219 board_print(board
, stderr
);
223 } else if (!strcasecmp(cmd
, "final_score")) {
224 struct move_queue q
= { .moves
= 0 };
225 if (engine
->dead_group_list
)
226 engine
->dead_group_list(engine
, board
, &q
);
227 float score
= board_official_score(board
, &q
);
230 fprintf(stderr
, "counted score %.1f\n", score
);
232 gtp_reply(id
, "0", NULL
);
233 } else if (score
> 0) {
234 snprintf(str
, 64, "W+%.1f", score
);
235 gtp_reply(id
, str
, NULL
);
237 snprintf(str
, 64, "B+%.1f", -score
);
238 gtp_reply(id
, str
, NULL
);
241 /* XXX: This is a huge hack. */
242 } else if (!strcasecmp(cmd
, "final_status_list")) {
245 struct move_queue q
= { .moves
= 0 };
246 if (engine
->dead_group_list
)
247 engine
->dead_group_list(engine
, board
, &q
);
248 /* else we return empty list - i.e. engine not supporting
249 * this assumes all stones alive at the game end. */
250 if (!strcasecmp(arg
, "dead")) {
252 for (int i
= 0; i
< q
.moves
; i
++) {
253 foreach_in_group(board
, q
.move
[i
]) {
254 printf("%s ", coord2sstr(c
, board
));
255 } foreach_in_group_end
;
261 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
263 bool printed_group
= false;
264 foreach_point(board
) { // foreach_group, effectively
265 group_t g
= group_at(board
, c
);
266 if (!g
|| g
!= c
) continue;
268 for (int i
= 0; i
< q
.moves
; i
++) {
272 foreach_in_group(board
, g
) {
273 printf("%s ", coord2sstr(c
, board
));
274 } foreach_in_group_end
;
276 printed_group
= true;
283 gtp_error(id
, "illegal status specifier", NULL
);
286 /* Custom commands for handling UCT opening book */
287 } else if (!strcasecmp(cmd
, "uct_genbook")) {
288 /* Board must be initialized properly, as if for genmove;
289 * makes sense only as 'uct_genbook b'. */
292 enum stone color
= str2stone(arg
);
293 if (uct_genbook(engine
, board
, ti
, color
))
296 gtp_error(id
, "error generating book", NULL
);
298 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
301 enum stone color
= str2stone(arg
);
302 uct_dumpbook(engine
, board
, color
);
305 } else if (!strcasecmp(cmd
, "kgs-chat")) {
314 reply
= engine
->chat(engine
, board
, msg
);
316 gtp_reply(id
, reply
, NULL
);
318 gtp_error(id
, "unknown chat command", NULL
);
321 gtp_error(id
, "unknown command", NULL
);