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\n"
117 "kgs-genmove_cleanup\n"
118 "set_free_handicap\n"
119 "place_free_handicap\n"
120 "final_status_list\n"
124 "kgs-time_settings", NULL
);
126 } else if (!strcasecmp(cmd
, "quit")) {
130 } else if (!strcasecmp(cmd
, "boardsize")) {
133 board_resize(board
, atoi(arg
));
137 } else if (!strcasecmp(cmd
, "clear_board")) {
141 board_print(board
, stderr
);
144 } else if (!strcasecmp(cmd
, "komi")) {
147 sscanf(arg
, "%f", &board
->komi
);
150 board_print(board
, stderr
);
153 } else if (!strcasecmp(cmd
, "play")) {
158 m
.color
= str2stone(arg
);
160 coord_t
*c
= str2coord(arg
, board_size(board
));
161 m
.coord
= *c
; coord_done(c
);
165 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
167 // This is where kgs starts our timer, not at coming genmove!
168 time_start_timer(&ti
[stone_other(m
.color
)]);
170 if (engine
->notify_play
)
171 reply
= engine
->notify_play(engine
, board
, &m
);
172 if (board_play(board
, &m
) < 0) {
174 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
175 board_print(board
, stderr
);
177 gtp_error(id
, "illegal move", NULL
);
180 board_print(board
, stderr
);
181 gtp_reply(id
, reply
, NULL
);
184 } else if (!strcasecmp(cmd
, "genmove") || !strcasecmp(cmd
, "kgs-genmove_cleanup")) {
187 enum stone color
= str2stone(arg
);
189 coord_t
*c
= engine
->genmove(engine
, board
, &ti
[color
], color
, !strcasecmp(cmd
, "kgs-genmove_cleanup"));
190 struct move m
= { *c
, color
};
191 board_play(board
, &m
);
192 char *str
= coord2str(*c
, board
);
194 fprintf(stderr
, "playing move %s\n", str
);
196 board_print_custom(board
, stderr
, engine
->printhook
);
198 gtp_reply(id
, str
, NULL
);
199 free(str
); coord_done(c
);
201 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
208 coord_t
*c
= str2coord(arg
, board_size(board
));
209 m
.coord
= *c
; coord_done(c
);
211 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
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 gtp_error(id
, "illegal move", NULL
);
222 board_print(board
, stderr
);
225 /* TODO: Engine should choose free handicap; however, it tends to take
226 * overly long to think it all out, and unless it's clever its
227 * handicap stones won't be of much help. ;-) */
228 } else if (!strcasecmp(cmd
, "place_free_handicap")
229 || !strcasecmp(cmd
, "fixed_handicap")) {
232 int stones
= atoi(arg
);
235 board_handicap(board
, stones
, stdout
);
237 board_print(board
, stderr
);
241 } else if (!strcasecmp(cmd
, "final_score")) {
242 struct move_queue q
= { .moves
= 0 };
243 if (engine
->dead_group_list
)
244 engine
->dead_group_list(engine
, board
, &q
);
245 float score
= board_official_score(board
, &q
);
248 fprintf(stderr
, "counted score %.1f\n", score
);
250 gtp_reply(id
, "0", NULL
);
251 } else if (score
> 0) {
252 snprintf(str
, 64, "W+%.1f", score
);
253 gtp_reply(id
, str
, NULL
);
255 snprintf(str
, 64, "B+%.1f", -score
);
256 gtp_reply(id
, str
, NULL
);
259 /* XXX: This is a huge hack. */
260 } else if (!strcasecmp(cmd
, "final_status_list")) {
263 struct move_queue q
= { .moves
= 0 };
264 if (engine
->dead_group_list
)
265 engine
->dead_group_list(engine
, board
, &q
);
266 /* else we return empty list - i.e. engine not supporting
267 * this assumes all stones alive at the game end. */
268 if (!strcasecmp(arg
, "dead")) {
270 for (int i
= 0; i
< q
.moves
; i
++) {
271 foreach_in_group(board
, q
.move
[i
]) {
272 printf("%s ", coord2sstr(c
, board
));
273 } foreach_in_group_end
;
279 } else if (!strcasecmp(arg
, "seki") || !strcasecmp(arg
, "alive")) {
281 bool printed_group
= false;
282 foreach_point(board
) { // foreach_group, effectively
283 group_t g
= group_at(board
, c
);
284 if (!g
|| g
!= c
) continue;
286 for (int i
= 0; i
< q
.moves
; i
++) {
290 foreach_in_group(board
, g
) {
291 printf("%s ", coord2sstr(c
, board
));
292 } foreach_in_group_end
;
294 printed_group
= true;
301 gtp_error(id
, "illegal status specifier", NULL
);
304 /* Custom commands for handling UCT opening book */
305 } else if (!strcasecmp(cmd
, "uct_genbook")) {
306 /* Board must be initialized properly, as if for genmove;
307 * makes sense only as 'uct_genbook b'. */
310 enum stone color
= str2stone(arg
);
311 if (uct_genbook(engine
, board
, &ti
[color
], color
))
314 gtp_error(id
, "error generating book", NULL
);
316 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
319 enum stone color
= str2stone(arg
);
320 uct_dumpbook(engine
, board
, color
);
323 } else if (!strcasecmp(cmd
, "kgs-chat")) {
332 reply
= engine
->chat(engine
, board
, msg
);
334 gtp_reply(id
, reply
, NULL
);
336 gtp_error(id
, "unknown chat command", NULL
);
338 } else if (!strcasecmp(cmd
, "time_left")) {
341 enum stone color
= str2stone(arg
);
343 int time
= atoi(arg
);
345 int stones
= atoi(arg
);
346 if (!ti
[color
].ignore_gtp
) {
347 time_left(&ti
[color
], time
, stones
);
349 if (DEBUGL(2)) fprintf(stderr
, "ignored time info\n");
354 } else if (!strcasecmp(cmd
, "time_settings") || !strcasecmp(cmd
, "kgs-time_settings")) {
355 char *time_system
= "canadian";
357 int main_time
= -1, byoyomi_time
= 0, byoyomi_stones
= 0, byoyomi_periods
= 0;
358 if (!strcasecmp(cmd
, "kgs-time_settings")) {
359 next_tok(time_system
);
360 if (!strcasecmp(time_system
, "none")) {
361 // time > 0, stones 0: convention for unlimited
364 } else if (!strcasecmp(time_system
, "absolute")) {
366 main_time
= atoi(arg
);
367 } else if (!strcasecmp(time_system
, "byoyomi")) {
369 main_time
= atoi(arg
);
371 byoyomi_time
= atoi(arg
);
374 byoyomi_periods
= atoi(arg
);
377 if (main_time
< 0) { // canadian time system
379 main_time
= atoi(arg
);
381 byoyomi_time
= atoi(arg
);
383 byoyomi_stones
= atoi(arg
);
386 fprintf(stderr
, "time_settings %d %d %d %d\n",
387 main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
388 if (!ti
[S_BLACK
].ignore_gtp
) {
389 time_settings(&ti
[S_BLACK
], main_time
, byoyomi_time
, byoyomi_stones
, byoyomi_periods
);
390 ti
[S_WHITE
] = ti
[S_BLACK
];
392 if (DEBUGL(1)) fprintf(stderr
, "ignored time info\n");
398 gtp_error(id
, "unknown command", NULL
);